mirror of
https://github.com/BillyOutlast/drop.git
synced 2026-02-04 08:41:17 +01:00
feat: update checker based gh releases
This commit is contained in:
4
server/h3.d.ts
vendored
4
server/h3.d.ts
vendored
@@ -1 +1,5 @@
|
||||
export type MinimumRequestObject = { headers: Headers };
|
||||
|
||||
export type TaskReturn<T = unknown> =
|
||||
| { success: true; data: T; error?: never }
|
||||
| { success: false; data?: never; error: { message: string } };
|
||||
|
||||
@@ -1,6 +1,12 @@
|
||||
class SystemConfig {
|
||||
private libraryFolder = process.env.LIBRARY ?? "./.data/library";
|
||||
private dataFolder = process.env.DATA ?? "./.data/data";
|
||||
private dropVersion = "v0.3.0";
|
||||
private checkForUpdates =
|
||||
process.env.CHECK_FOR_UPDATES !== undefined &&
|
||||
process.env.CHECK_FOR_UPDATES.toLocaleLowerCase() === "true"
|
||||
? true
|
||||
: false;
|
||||
|
||||
getLibraryFolder() {
|
||||
return this.libraryFolder;
|
||||
@@ -9,6 +15,14 @@ class SystemConfig {
|
||||
getDataFolder() {
|
||||
return this.dataFolder;
|
||||
}
|
||||
|
||||
getDropVersion() {
|
||||
return this.dropVersion;
|
||||
}
|
||||
|
||||
shouldCheckForUpdates() {
|
||||
return this.checkForUpdates;
|
||||
}
|
||||
}
|
||||
|
||||
export const systemConfig = new SystemConfig();
|
||||
|
||||
@@ -10,6 +10,7 @@ import type {
|
||||
} from "./types";
|
||||
import { ObjectTransactionalHandler } from "../objects/transactional";
|
||||
import { PriorityListIndexed } from "../utils/prioritylist";
|
||||
import { systemConfig } from "../config/sys-conf";
|
||||
|
||||
export class MissingMetadataProviderConfig extends Error {
|
||||
private providerName: string;
|
||||
@@ -25,7 +26,7 @@ export class MissingMetadataProviderConfig extends Error {
|
||||
}
|
||||
|
||||
// TODO: add useragent to all outbound api calls (best practice)
|
||||
export const DropUserAgent = "Drop/0.2";
|
||||
export const DropUserAgent = `Drop/${systemConfig.getDropVersion()}`;
|
||||
|
||||
export abstract class MetadataProvider {
|
||||
abstract name(): string;
|
||||
|
||||
@@ -9,6 +9,7 @@ Design goals:
|
||||
import type { Notification } from "~/prisma/client";
|
||||
import prisma from "../db/database";
|
||||
|
||||
// TODO: document notification action format
|
||||
export type NotificationCreateArgs = Pick<
|
||||
Notification,
|
||||
"title" | "description" | "actions" | "nonce"
|
||||
@@ -61,14 +62,18 @@ class NotificationSystem {
|
||||
throw new Error("No nonce in notificationCreateArgs");
|
||||
const notification = await prisma.notification.upsert({
|
||||
where: {
|
||||
nonce: notificationCreateArgs.nonce,
|
||||
userId_nonce: {
|
||||
nonce: notificationCreateArgs.nonce,
|
||||
userId,
|
||||
},
|
||||
},
|
||||
update: {
|
||||
userId: userId,
|
||||
// we don't need to update the userid right?
|
||||
// userId: userId,
|
||||
...notificationCreateArgs,
|
||||
},
|
||||
create: {
|
||||
userId: userId,
|
||||
userId,
|
||||
...notificationCreateArgs,
|
||||
},
|
||||
});
|
||||
@@ -84,13 +89,34 @@ class NotificationSystem {
|
||||
},
|
||||
});
|
||||
|
||||
const res: Promise<void>[] = [];
|
||||
for (const user of users) {
|
||||
await this.push(user.id, notificationCreateArgs);
|
||||
res.push(this.push(user.id, notificationCreateArgs));
|
||||
}
|
||||
// wait for all notifications to pass
|
||||
await Promise.all(res);
|
||||
}
|
||||
|
||||
async systemPush(notificationCreateArgs: NotificationCreateArgs) {
|
||||
return await this.push("system", notificationCreateArgs);
|
||||
await this.push("system", notificationCreateArgs);
|
||||
}
|
||||
|
||||
async pushAllAdmins(notificationCreateArgs: NotificationCreateArgs) {
|
||||
const users = await prisma.user.findMany({
|
||||
where: {
|
||||
admin: true,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
},
|
||||
});
|
||||
|
||||
const res: Promise<void>[] = [];
|
||||
for (const user of users) {
|
||||
res.push(this.push(user.id, notificationCreateArgs));
|
||||
}
|
||||
// wait for all notifications to pass
|
||||
await Promise.all(res);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,5 +3,8 @@ export default defineNitroPlugin(async (_nitro) => {
|
||||
await Promise.all([
|
||||
runTask("cleanup:invitations"),
|
||||
runTask("cleanup:sessions"),
|
||||
// TODO: maybe implement some sort of rate limit thing to prevent this from calling github api a bunch in the event of crashloop or whatever?
|
||||
// probably will require custom task scheduler for object cleanup anyway, so something to thing about
|
||||
runTask("check:update"),
|
||||
]);
|
||||
});
|
||||
|
||||
143
server/tasks/check/update.ts
Normal file
143
server/tasks/check/update.ts
Normal file
@@ -0,0 +1,143 @@
|
||||
import { type } from "arktype";
|
||||
import { systemConfig } from "../../internal/config/sys-conf";
|
||||
import * as semver from "semver";
|
||||
import type { TaskReturn } from "../../h3";
|
||||
import notificationSystem from "../../internal/notifications";
|
||||
|
||||
const latestRelease = type({
|
||||
url: "string", // api url for specific release
|
||||
html_url: "string", // user facing url
|
||||
id: "number", // release id
|
||||
tag_name: "string", // tag used for release
|
||||
name: "string", // release name
|
||||
draft: "boolean",
|
||||
prerelease: "boolean",
|
||||
created_at: "string",
|
||||
published_at: "string",
|
||||
});
|
||||
|
||||
export default defineTask<TaskReturn>({
|
||||
meta: {
|
||||
name: "check:update",
|
||||
},
|
||||
async run() {
|
||||
if (systemConfig.shouldCheckForUpdates()) {
|
||||
console.log("[Task check:update]: Checking for update");
|
||||
try {
|
||||
const response = await fetch(
|
||||
"https://api.github.com/repos/Drop-OSS/drop/releases/latest",
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
console.log("[Task check:update]: Failed to check for update", {
|
||||
status: response.status,
|
||||
body: response.body,
|
||||
});
|
||||
|
||||
return {
|
||||
result: {
|
||||
success: false,
|
||||
error: {
|
||||
message: "" + response.status,
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const resJson = await response.json();
|
||||
const body = latestRelease(resJson);
|
||||
if (body instanceof type.errors) {
|
||||
console.error(body.summary);
|
||||
console.log("GitHub Api response", resJson);
|
||||
return {
|
||||
result: {
|
||||
success: false,
|
||||
error: {
|
||||
message: body.summary,
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
// const currVerStr = systemConfig.getDropVersion()
|
||||
const currVerStr = "v0.1";
|
||||
|
||||
const latestVer = semver.coerce(body.tag_name);
|
||||
const currVer = semver.coerce(currVerStr);
|
||||
if (latestVer === null) {
|
||||
const msg = "Github Api returned invalid semver tag";
|
||||
console.log("[Task check:update]:", msg);
|
||||
return {
|
||||
result: {
|
||||
success: false,
|
||||
error: {
|
||||
message: msg,
|
||||
},
|
||||
},
|
||||
};
|
||||
} else if (currVer === null) {
|
||||
const msg = "Drop provided a invalid semver tag";
|
||||
console.log("[Task check:update]:", msg);
|
||||
return {
|
||||
result: {
|
||||
success: false,
|
||||
error: {
|
||||
message: msg,
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (semver.gt(latestVer, currVer)) {
|
||||
console.log("[Task check:update]: Update available");
|
||||
notificationSystem.pushAllAdmins({
|
||||
nonce: `drop-update-available-${currVer}-to-${latestVer}`,
|
||||
title: `Update available to v${latestVer}`,
|
||||
description: `A new version of Drop is available v${latestVer}`,
|
||||
actions: [`View|${body.html_url}`],
|
||||
});
|
||||
} else {
|
||||
console.log("[Task check:update]: no update available");
|
||||
}
|
||||
|
||||
console.log("[Task check:update]: Done");
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
if (typeof e === "string") {
|
||||
return {
|
||||
result: {
|
||||
success: false,
|
||||
error: {
|
||||
message: e,
|
||||
},
|
||||
},
|
||||
};
|
||||
} else if (e instanceof Error) {
|
||||
return {
|
||||
result: {
|
||||
success: false,
|
||||
error: {
|
||||
message: e.message,
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
result: {
|
||||
success: false,
|
||||
error: {
|
||||
message: "unknown cause, please check console",
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
return {
|
||||
result: {
|
||||
success: true,
|
||||
data: undefined,
|
||||
},
|
||||
};
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user