diff options
| author | syuilo <Syuilotan@yahoo.co.jp> | 2022-07-13 21:59:47 +0900 |
|---|---|---|
| committer | syuilo <Syuilotan@yahoo.co.jp> | 2022-07-13 21:59:47 +0900 |
| commit | bce48dfee933d9feb3be381ad22ea765dd6a3aab (patch) | |
| tree | b21a3a765040469885d286487245aeaed373bba6 /packages/backend/src/services | |
| parent | Merge branch 'develop' (diff) | |
| parent | 12.113.0 (diff) | |
| download | misskey-bce48dfee933d9feb3be381ad22ea765dd6a3aab.tar.gz misskey-bce48dfee933d9feb3be381ad22ea765dd6a3aab.tar.bz2 misskey-bce48dfee933d9feb3be381ad22ea765dd6a3aab.zip | |
Merge branch 'develop'
Diffstat (limited to 'packages/backend/src/services')
| -rw-r--r-- | packages/backend/src/services/detect-sensitive.ts | 24 | ||||
| -rw-r--r-- | packages/backend/src/services/fetch-instance-metadata.ts | 6 | ||||
| -rw-r--r-- | packages/backend/src/services/push-notification.ts | 1 |
3 files changed, 26 insertions, 5 deletions
diff --git a/packages/backend/src/services/detect-sensitive.ts b/packages/backend/src/services/detect-sensitive.ts index 0fa263599b..2ade39d524 100644 --- a/packages/backend/src/services/detect-sensitive.ts +++ b/packages/backend/src/services/detect-sensitive.ts @@ -2,19 +2,34 @@ import * as fs from 'node:fs'; import { fileURLToPath } from 'node:url'; import { dirname } from 'node:path'; import * as nsfw from 'nsfwjs'; -import * as tf from '@tensorflow/tfjs-node'; +import si from 'systeminformation'; const _filename = fileURLToPath(import.meta.url); const _dirname = dirname(_filename); +const REQUIRED_CPU_FLAGS = ['avx2', 'fma']; +let isSupportedCpu: undefined | boolean = undefined; + let model: nsfw.NSFWJS; export async function detectSensitive(path: string): Promise<nsfw.predictionType[] | null> { try { + if (isSupportedCpu === undefined) { + const cpuFlags = await getCpuFlags(); + isSupportedCpu = REQUIRED_CPU_FLAGS.every(required => cpuFlags.includes(required)); + } + + if (!isSupportedCpu) { + console.error('This CPU cannot use TensorFlow.'); + return null; + } + + const tf = await import('@tensorflow/tfjs-node'); + if (model == null) model = await nsfw.load(`file://${_dirname}/../../nsfw-model/`, { size: 299 }); const buffer = await fs.promises.readFile(path); - const image = await tf.node.decodeImage(buffer, 3) as tf.Tensor3D; + const image = await tf.node.decodeImage(buffer, 3) as any; try { const predictions = await model.classify(image); return predictions; @@ -26,3 +41,8 @@ export async function detectSensitive(path: string): Promise<nsfw.predictionType return null; } } + +async function getCpuFlags(): Promise<string[]> { + const str = await si.cpuFlags(); + return str.split(/\s+/); +} diff --git a/packages/backend/src/services/fetch-instance-metadata.ts b/packages/backend/src/services/fetch-instance-metadata.ts index 029c388dc2..ee1245132a 100644 --- a/packages/backend/src/services/fetch-instance-metadata.ts +++ b/packages/backend/src/services/fetch-instance-metadata.ts @@ -34,7 +34,7 @@ export async function fetchInstanceMetadata(instance: Instance, force = false): const [favicon, icon, themeColor, name, description] = await Promise.all([ fetchFaviconUrl(instance, dom).catch(() => null), fetchIconUrl(instance, dom, manifest).catch(() => null), - getThemeColor(dom, manifest).catch(() => null), + getThemeColor(info, dom, manifest).catch(() => null), getSiteName(info, dom, manifest).catch(() => null), getDescription(info, dom, manifest).catch(() => null), ]); @@ -208,8 +208,8 @@ async function fetchIconUrl(instance: Instance, doc: DOMWindow['document'] | nul return null; } -async function getThemeColor(doc: DOMWindow['document'] | null, manifest: Record<string, any> | null): Promise<string | null> { - const themeColor = doc?.querySelector('meta[name="theme-color"]')?.getAttribute('content') || manifest?.theme_color; +async function getThemeColor(info: NodeInfo | null, doc: DOMWindow['document'] | null, manifest: Record<string, any> | null): Promise<string | null> { + const themeColor = info?.metadata?.themeColor || doc?.querySelector('meta[name="theme-color"]')?.getAttribute('content') || manifest?.theme_color; if (themeColor) { const color = new tinycolor(themeColor); diff --git a/packages/backend/src/services/push-notification.ts b/packages/backend/src/services/push-notification.ts index 5c3bafbb34..393a23d050 100644 --- a/packages/backend/src/services/push-notification.ts +++ b/packages/backend/src/services/push-notification.ts @@ -64,6 +64,7 @@ export async function pushNotification<T extends keyof pushNotificationsTypes>(u type, body: type === 'notification' ? truncateNotification(body as Packed<'Notification'>) : body, userId, + dateTime: (new Date()).getTime(), }), { proxy: config.proxy, }).catch((err: any) => { |