From c44e8e6bf1086759239166f57af95977259e43be Mon Sep 17 00:00:00 2001 From: syuilo Date: Thu, 30 Jan 2020 06:06:50 +0900 Subject: [PATCH] Emoji copy --- locales/ja-JP.yml | 1 + src/server/api/endpoints/admin/emoji/copy.ts | 64 ++++++++++++++++++++ src/services/drive/add-file.ts | 38 ++++++------ src/services/drive/upload-from-url.ts | 2 +- 4 files changed, 87 insertions(+), 18 deletions(-) create mode 100644 src/server/api/endpoints/admin/emoji/copy.ts diff --git a/locales/ja-JP.yml b/locales/ja-JP.yml index 5673a2d416..5746ee1bb8 100644 --- a/locales/ja-JP.yml +++ b/locales/ja-JP.yml @@ -328,6 +328,7 @@ misskeySource: "ソースコードはここで公開されています:" administrator: "管理者" token: "トークン" twoStepAuthentication: "二段階認証" +moderator: "モデレーター" _2fa: registerDevice: "デバイスを登録" diff --git a/src/server/api/endpoints/admin/emoji/copy.ts b/src/server/api/endpoints/admin/emoji/copy.ts new file mode 100644 index 0000000000..4b4319ca81 --- /dev/null +++ b/src/server/api/endpoints/admin/emoji/copy.ts @@ -0,0 +1,64 @@ +import $ from 'cafy'; +import define from '../../../define'; +import { Emojis } from '../../../../../models'; +import { genId } from '../../../../../misc/gen-id'; +import { getConnection } from 'typeorm'; +import { ApiError } from '../../../error'; +import { DriveFile } from '../../../../../models/entities/drive-file'; +import { ID } from '../../../../../misc/cafy-id'; +import uploadFromUrl from '../../../../../services/drive/upload-from-url'; + +export const meta = { + tags: ['admin'], + + requireCredential: true, + requireModerator: true, + + params: { + emojiId: { + validator: $.type(ID) + }, + }, + + errors: { + noSuchEmoji: { + message: 'No such emoji.', + code: 'NO_SUCH_EMOJI', + id: 'e2785b66-dca3-4087-9cac-b93c541cc425' + } + } +}; + +export default define(meta, async (ps, me) => { + const emoji = await Emojis.findOne(ps.emojiId); + + if (emoji == null) { + throw new ApiError(meta.errors.noSuchEmoji); + } + + let driveFile: DriveFile; + + try { + // Create file + driveFile = await uploadFromUrl(emoji.url, null, null, null, false, true); + } catch (e) { + throw new ApiError(); + } + + const copied = await Emojis.save({ + id: genId(), + updatedAt: new Date(), + name: emoji.name, + host: null, + aliases: [], + url: driveFile.url, + type: driveFile.type, + fileId: driveFile.id, + }); + + await getConnection().queryResultCache!.remove(['meta_emojis']); + + return { + id: copied.id + }; +}); diff --git a/src/services/drive/add-file.ts b/src/services/drive/add-file.ts index 7cc710c8b6..962c9d771d 100644 --- a/src/services/drive/add-file.ts +++ b/src/services/drive/add-file.ts @@ -257,7 +257,7 @@ async function deleteOldFile(user: IRemoteUser) { * @return Created drive file */ export default async function( - user: User, + user: User | null, path: string, name: string | null = null, comment: string | null = null, @@ -274,7 +274,7 @@ export default async function( // detect name const detectedName = name || (info.type.ext ? `untitled.${info.type.ext}` : 'untitled'); - if (!force) { + if (user && !force) { // Check if there is a file with the same hash const much = await DriveFiles.findOne({ md5: info.md5, @@ -288,7 +288,7 @@ export default async function( } //#region Check drive usage - if (!isLink) { + if (user && !isLink) { const usage = await DriveFiles.clacDriveUsageOf(user); const instance = await fetchMeta(); @@ -315,7 +315,7 @@ export default async function( const driveFolder = await DriveFolders.findOne({ id: folderId, - userId: user.id + userId: user ? user.id : null }); if (driveFolder == null) throw new Error('folder-not-found'); @@ -338,23 +338,25 @@ export default async function( properties['avgColor'] = `rgb(${info.avgColor.join(',')}`; } - const profile = await UserProfiles.findOne(user.id); + const profile = user ? await UserProfiles.findOne(user.id) : null; const folder = await fetchFolder(); let file = new DriveFile(); file.id = genId(); file.createdAt = new Date(); - file.userId = user.id; - file.userHost = user.host; + file.userId = user ? user.id : null; + file.userHost = user ? user.host : null; file.folderId = folder !== null ? folder.id : null; file.comment = comment; file.properties = properties; file.isLink = isLink; - file.isSensitive = Users.isLocalUser(user) && profile!.alwaysMarkNsfw ? true : - (sensitive !== null && sensitive !== undefined) - ? sensitive - : false; + file.isSensitive = user + ? Users.isLocalUser(user) && profile!.alwaysMarkNsfw ? true : + (sensitive !== null && sensitive !== undefined) + ? sensitive + : false + : false; if (url !== null) { file.src = url; @@ -388,7 +390,7 @@ export default async function( file = await DriveFiles.findOne({ uri: file.uri, - userId: user.id + userId: user ? user.id : null }) as DriveFile; } else { logger.error(e); @@ -401,11 +403,13 @@ export default async function( logger.succ(`drive file has been created ${file.id}`); - DriveFiles.pack(file, { self: true }).then(packedFile => { - // Publish driveFileCreated event - publishMainStream(user.id, 'driveFileCreated', packedFile); - publishDriveStream(user.id, 'fileCreated', packedFile); - }); + if (user) { + DriveFiles.pack(file, { self: true }).then(packedFile => { + // Publish driveFileCreated event + publishMainStream(user.id, 'driveFileCreated', packedFile); + publishDriveStream(user.id, 'fileCreated', packedFile); + }); + } // 統計を更新 driveChart.update(file, true); diff --git a/src/services/drive/upload-from-url.ts b/src/services/drive/upload-from-url.ts index bfe8c5c3b2..ff2f9a7281 100644 --- a/src/services/drive/upload-from-url.ts +++ b/src/services/drive/upload-from-url.ts @@ -11,7 +11,7 @@ const logger = driveLogger.createSubLogger('downloader'); export default async ( url: string, - user: User, + user: User | null, folderId: DriveFolder['id'] | null = null, uri: string | null = null, sensitive = false,