i18n Support and Task improvements (#80)

* fix: release workflow

* feat: move mostly to internal tasks system

* feat: migrate object clean to new task system

* fix: release not  getting good base version

* chore: set version v0.3.0

* chore: style

* feat: basic task concurrency

* feat: temp pages to fill in page links

* feat: inital i18n support

* feat: localize store page

* chore: style

* fix: weblate doesn't like multifile thing

* fix: update nuxt

* feat: improved error logging

* fix: using old task api

* feat: basic translation docs

* feat: add i18n eslint plugin

* feat: translate store and auth pages

* feat: more translation progress

* feat: admin dash i18n progress

* feat: enable update check by default in prod

* fix: using wrong i18n keys

* fix: crash in library sources page

* feat: finish i18n work

* fix: missing i18n translations

* feat: use twemoji for emojis

* feat: sanatize object ids

* fix: EmojiText's alt text

* fix: UserWidget not using links

* feat: cache and auth for emoji api

* fix: add more missing translations
This commit is contained in:
Husky
2025-06-04 19:53:30 -04:00
committed by GitHub
parent c7fab132ab
commit 681efe95af
86 changed files with 5175 additions and 2816 deletions

View File

@@ -1,150 +0,0 @@
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");
const currVerStr = systemConfig.getDropVersion();
const currVer = semver.coerce(currVerStr);
if (currVer === null) {
const msg = "Drop provided a invalid semver tag";
console.log("[Task check:update]:", msg);
return {
result: {
success: false,
error: {
message: msg,
},
},
};
}
try {
const response = await fetch(
"https://api.github.com/repos/Drop-OSS/drop/releases/latest",
);
// if response failed somehow
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,
},
},
};
}
// parse and validate response
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,
},
},
};
}
// parse remote version
const latestVer = semver.coerce(body.tag_name);
if (latestVer === null) {
const msg = "Github Api returned invalid semver tag";
console.log("[Task check:update]:", msg);
return {
result: {
success: false,
error: {
message: msg,
},
},
};
}
// TODO: handle prerelease identifiers https://github.com/npm/node-semver#prerelease-identifiers
// check if is newer version
if (semver.gt(latestVer, currVer)) {
console.log("[Task check:update]: Update available");
notificationSystem.systemPush({
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}`],
acls: ["system:notifications:read"],
});
} 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,
},
};
},
});

View File

@@ -1,23 +0,0 @@
import prisma from "~/server/internal/db/database";
export default defineTask({
meta: {
name: "cleanup:invitations",
},
async run() {
console.log("[Task cleanup:invitations]: Cleaning invitations");
const now = new Date();
await prisma.invitation.deleteMany({
where: {
expires: {
lt: now,
},
},
});
console.log("[Task cleanup:invitations]: Done");
return { result: true };
},
});

View File

@@ -1,167 +0,0 @@
import prisma from "~/server/internal/db/database";
import objectHandler from "~/server/internal/objects";
import type { TaskReturn } from "../../h3";
type FieldReferenceMap = {
[modelName: string]: {
model: unknown; // Prisma model
fields: string[]; // Fields that may contain IDs
arrayFields: string[]; // Fields that are arrays that may contain IDs
};
};
export default defineTask<TaskReturn>({
meta: {
name: "cleanup:objects",
},
async run() {
console.log("[Task cleanup:objects]: Cleaning unreferenced objects");
// get all objects
const objects = await objectHandler.listAll();
console.log(
`[Task cleanup:objects]: searching for ${objects.length} objects`,
);
// find unreferenced objects
const refMap = buildRefMap();
console.log("[Task cleanup:objects]: Building reference map");
console.log(
`[Task cleanup:objects]: Found ${Object.keys(refMap).length} models with reference fields`,
);
console.log("[Task cleanup:objects]: Searching for unreferenced objects");
const unrefedObjects = await findUnreferencedStrings(objects, refMap);
console.log(
`[Task cleanup:objects]: found ${unrefedObjects.length} Unreferenced objects`,
);
// console.log(unrefedObjects);
// remove objects
const deletePromises: Promise<boolean>[] = [];
for (const obj of unrefedObjects) {
console.log(`[Task cleanup:objects]: Deleting object ${obj}`);
deletePromises.push(objectHandler.deleteAsSystem(obj));
}
await Promise.all(deletePromises);
// Remove any possible leftover metadata
objectHandler.cleanupMetadata();
console.log("[Task cleanup:objects]: Done");
return {
result: {
success: true,
data: unrefedObjects,
},
};
},
});
/**
* Builds a map of Prisma models and their fields that may contain object IDs
* @returns
*/
function buildRefMap(): FieldReferenceMap {
const tables = Object.keys(prisma).filter(
(v) => !(v.startsWith("$") || v.startsWith("_") || v === "constructor"),
);
// type test = Prisma.ModelName
// prisma.game.fields.mIconId.
const result: FieldReferenceMap = {};
for (const model of tables) {
// @ts-expect-error can't get model to typematch key names
const fields = Object.keys(prisma[model]["fields"]);
const single = fields.filter((v) => v.toLowerCase().endsWith("objectid"));
const array = fields.filter((v) => v.toLowerCase().endsWith("objectids"));
result[model] = {
// @ts-expect-error im not dealing with this
model: prisma[model],
fields: single,
arrayFields: array,
};
}
return result;
}
/**
* Searches all models for a given id in their fields
* @param id
* @param fieldRefMap
* @returns
*/
async function isReferencedInModelFields(
id: string,
fieldRefMap: FieldReferenceMap,
): Promise<boolean> {
// TODO: optimize the built queries
// rn it runs a query for every id over each db table
for (const { model, fields, arrayFields } of Object.values(fieldRefMap)) {
const singleFieldOrConditions = fields
? fields.map((field) => ({
[field]: {
equals: id,
},
}))
: [];
const arrayFieldOrConditions = arrayFields
? arrayFields.map((field) => ({
[field]: {
has: id,
},
}))
: [];
// prisma.game.findFirst({
// where: {
// OR: [
// // single item
// {
// mIconId: {
// equals: "",
// },
// },
// // array
// {
// mImageCarousel: {
// has: "",
// },
// },
// ],
// },
// });
// @ts-expect-error using unknown because im not typing this mess omg
const found = await model.findFirst({
where: { OR: [...singleFieldOrConditions, ...arrayFieldOrConditions] },
});
if (found) return true;
}
return false;
}
/**
* Takes a list of objects and checks if they are referenced in any model fields
* @param objects
* @param fieldRefMap
* @returns
*/
async function findUnreferencedStrings(
objects: string[],
fieldRefMap: FieldReferenceMap,
): Promise<string[]> {
const unreferenced: string[] = [];
for (const obj of objects) {
const isRef = await isReferencedInModelFields(obj, fieldRefMap);
if (!isRef) unreferenced.push(obj);
}
return unreferenced;
}

View File

@@ -1,13 +0,0 @@
import sessionHandler from "~/server/internal/session";
export default defineTask({
meta: {
name: "cleanup:sessions",
},
async run() {
console.log("[Task cleanup:sessions]: Cleaning up sessions");
await sessionHandler.cleanupSessions();
console.log("[Task cleanup:sessions]: Done");
return { result: true };
},
});

View File

@@ -0,0 +1,12 @@
import taskHandler from "~/server/internal/tasks";
export default defineTask({
meta: {
name: "dailyTasks",
},
async run() {
taskHandler.triggerDailyTasks();
return {};
},
});