From cd8809e927c2d04727264b6cd540723a3594d92a Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Wed, 29 Nov 2023 17:28:20 +0100 Subject: [PATCH] [client] Fix update check --- .../server/api/endpoints/latest-version.ts | 3 ++- packages/client/src/pages/admin/index.vue | 22 ++--------------- .../client/src/scripts/is-update-available.ts | 16 +++++++++++++ packages/client/src/ui/_common_/navbar.vue | 24 +++---------------- 4 files changed, 23 insertions(+), 42 deletions(-) create mode 100644 packages/client/src/scripts/is-update-available.ts diff --git a/packages/backend/src/server/api/endpoints/latest-version.ts b/packages/backend/src/server/api/endpoints/latest-version.ts index 338c01a5d..8c44d15f2 100644 --- a/packages/backend/src/server/api/endpoints/latest-version.ts +++ b/packages/backend/src/server/api/endpoints/latest-version.ts @@ -1,4 +1,5 @@ import define from "../define.js"; +import config from "@/config/index.js"; export const meta = { tags: ["meta"], @@ -16,7 +17,7 @@ export const paramDef = { export default define(meta, paramDef, async () => { let tag_name; await fetch( - "https://iceshrimp.dev/api/v1/repos/iceshrimp/iceshrimp/releases?draft=false&pre-release=false&page=1&limit=1", + `https://iceshrimp.dev/api/v1/repos/iceshrimp/iceshrimp/releases?draft=false&pre-release=${config.version.includes('-pre')}&page=1&limit=1`, ) .then((response) => response.json()) .then((data) => { diff --git a/packages/client/src/pages/admin/index.vue b/packages/client/src/pages/admin/index.vue index 53701dcea..f0f492d4f 100644 --- a/packages/client/src/pages/admin/index.vue +++ b/packages/client/src/pages/admin/index.vue @@ -88,6 +88,7 @@ import { provideMetadataReceiver, setPageMetadata, } from "@/scripts/page-metadata"; +import { isUpdateAvailable } from "@/scripts/is-update-available.js"; const isEmpty = (x: string | null) => x == null || x === ""; const el = ref(null); @@ -125,26 +126,7 @@ os.api("admin/abuse-user-reports", { }); if (defaultStore.state.showAdminUpdates) { - os.api("latest-version").then((res) => { - if (!res?.tag_name) { - updateAvailable = false; - return; - } - - const tag = res.tag_name as string; - if (tag === `v${version}`) { - updateAvailable = false; - return; - } - const tagDate = tag.includes('-') ? tag.substring(0, tag.indexOf('-')) : tag; - const versionDate = version.includes('-') ? version.substring(0, version.indexOf('-')) : version; - if (tagDate < versionDate) { - updateAvailable = false; - return; - } - updateAvailable = true; - return; - }); + updateAvailable = await isUpdateAvailable(); } const NARROW_THRESHOLD = 600; diff --git a/packages/client/src/scripts/is-update-available.ts b/packages/client/src/scripts/is-update-available.ts new file mode 100644 index 000000000..8ff6adc14 --- /dev/null +++ b/packages/client/src/scripts/is-update-available.ts @@ -0,0 +1,16 @@ +import { version } from "@/config.js"; +import * as os from "@/os.js"; + +type LatestVersionResponse = { + tag_name?: string; +}; + +export async function isUpdateAvailable(): Promise { + if (version.includes('-dev-')) return false; + + return os.api("latest-version").then((res: LatestVersionResponse) => { + if (!res?.tag_name) return false; + const tag = res.tag_name as string; + return (version.includes('-pre') && !tag.includes('-pre')) || tag > `v${version}`; + }); +} diff --git a/packages/client/src/ui/_common_/navbar.vue b/packages/client/src/ui/_common_/navbar.vue index 10d1b20f3..058ed0eef 100644 --- a/packages/client/src/ui/_common_/navbar.vue +++ b/packages/client/src/ui/_common_/navbar.vue @@ -149,6 +149,7 @@ import { defaultStore } from "@/store"; import { i18n } from "@/i18n"; import { instance } from "@/instance"; import { version } from "@/config"; +import { isUpdateAvailable } from "@/scripts/is-update-available.js"; const isEmpty = (x: string | null) => x == null || x === ""; @@ -196,26 +197,7 @@ if ($i?.isAdmin) { }); if (defaultStore.state.showAdminUpdates) { - os.api("latest-version").then((res) => { - if (!res?.tag_name) { - updateAvailable = false; - return; - } - - const tag = res.tag_name as string; - if (tag === `v${version}`) { - updateAvailable = false; - return; - } - const tagDate = tag.includes('-') ? tag.substring(0, tag.indexOf('-')) : tag; - const versionDate = version.includes('-') ? version.substring(0, version.indexOf('-')) : version; - if (tagDate < versionDate) { - updateAvailable = false; - return; - } - updateAvailable = true; - return; - }); + updateAvailable = await isUpdateAvailable(); } } @@ -639,7 +621,7 @@ function more(ev: MouseEvent) { opacity: 1; } } - + &:hover, &:focus-within { &:before {