From 6be1db00d11aea64c984f7df74be246ebd46c10a Mon Sep 17 00:00:00 2001 From: syuilo Date: Mon, 3 Jan 2022 02:20:30 +0900 Subject: [PATCH] refactor(server): use insert instead of save --- .../backend/src/queue/processors/db/import-user-lists.ts | 4 ++-- packages/backend/src/remote/activitypub/models/note.ts | 4 ++-- packages/backend/src/server/api/common/signin.ts | 4 ++-- .../src/server/api/endpoints/admin/announcements/create.ts | 4 ++-- packages/backend/src/server/api/endpoints/admin/emoji/add.ts | 4 ++-- .../src/server/api/endpoints/auth/session/generate.ts | 4 ++-- packages/backend/src/server/api/endpoints/pages/create.ts | 4 ++-- .../backend/src/server/api/endpoints/users/report-abuse.ts | 4 ++-- packages/backend/src/services/create-notification.ts | 5 +++-- 9 files changed, 19 insertions(+), 18 deletions(-) diff --git a/packages/backend/src/queue/processors/db/import-user-lists.ts b/packages/backend/src/queue/processors/db/import-user-lists.ts index 8245010de0..e060e86dd8 100644 --- a/packages/backend/src/queue/processors/db/import-user-lists.ts +++ b/packages/backend/src/queue/processors/db/import-user-lists.ts @@ -46,13 +46,13 @@ export async function importUserLists(job: Bull.Job, done: }); if (list == null) { - list = await UserLists.save({ + list = await UserLists.insert({ id: genId(), createdAt: new Date(), userId: user.id, name: listName, userIds: [], - }); + }).then(x => UserLists.findOneOrFail(x.identifiers[0])); } let target = isSelfHost(host!) ? await Users.findOne({ diff --git a/packages/backend/src/remote/activitypub/models/note.ts b/packages/backend/src/remote/activitypub/models/note.ts index cca054b52e..a0fdf7f239 100644 --- a/packages/backend/src/remote/activitypub/models/note.ts +++ b/packages/backend/src/remote/activitypub/models/note.ts @@ -342,7 +342,7 @@ export async function extractEmojis(tags: IObject | IObject[], host: string): Pr logger.info(`register emoji host=${host}, name=${name}`); - return await Emojis.save({ + return await Emojis.insert({ id: genId(), host, name, @@ -350,6 +350,6 @@ export async function extractEmojis(tags: IObject | IObject[], host: string): Pr url: tag.icon!.url, updatedAt: new Date(), aliases: [], - } as Partial); + } as Partial).then(x => Emojis.findOneOrFail(x.identifiers[0])); })); } diff --git a/packages/backend/src/server/api/common/signin.ts b/packages/backend/src/server/api/common/signin.ts index b713260ac6..df986fc457 100644 --- a/packages/backend/src/server/api/common/signin.ts +++ b/packages/backend/src/server/api/common/signin.ts @@ -29,14 +29,14 @@ export default function(ctx: Koa.Context, user: ILocalUser, redirect = false) { (async () => { // Append signin history - const record = await Signins.save({ + const record = await Signins.insert({ id: genId(), createdAt: new Date(), userId: user.id, ip: ctx.ip, headers: ctx.headers, success: true, - }); + }).then(x => Signins.findOneOrFail(x.identifiers[0])); // Publish signin event publishMainStream(user.id, 'signin', await Signins.pack(record)); diff --git a/packages/backend/src/server/api/endpoints/admin/announcements/create.ts b/packages/backend/src/server/api/endpoints/admin/announcements/create.ts index feb874db74..7d169d6caf 100644 --- a/packages/backend/src/server/api/endpoints/admin/announcements/create.ts +++ b/packages/backend/src/server/api/endpoints/admin/announcements/create.ts @@ -59,14 +59,14 @@ export const meta = { // eslint-disable-next-line import/no-default-export export default define(meta, async (ps) => { - const announcement = await Announcements.save({ + const announcement = await Announcements.insert({ id: genId(), createdAt: new Date(), updatedAt: null, title: ps.title, text: ps.text, imageUrl: ps.imageUrl, - }); + }).then(x => Announcements.findOneOrFail(x.identifiers[0])); return announcement; }); diff --git a/packages/backend/src/server/api/endpoints/admin/emoji/add.ts b/packages/backend/src/server/api/endpoints/admin/emoji/add.ts index bafa658a57..f7a0fdb875 100644 --- a/packages/backend/src/server/api/endpoints/admin/emoji/add.ts +++ b/packages/backend/src/server/api/endpoints/admin/emoji/add.ts @@ -38,7 +38,7 @@ export default define(meta, async (ps, me) => { const name = file.name.split('.')[0].match(/^[a-z0-9_]+$/) ? file.name.split('.')[0] : `_${rndstr('a-z0-9', 8)}_`; - const emoji = await Emojis.save({ + const emoji = await Emojis.insert({ id: genId(), updatedAt: new Date(), name: name, @@ -47,7 +47,7 @@ export default define(meta, async (ps, me) => { aliases: [], url: file.url, type: file.type, - }); + }).then(x => Emojis.findOneOrFail(x.identifiers[0])); await getConnection().queryResultCache!.remove(['meta_emojis']); diff --git a/packages/backend/src/server/api/endpoints/auth/session/generate.ts b/packages/backend/src/server/api/endpoints/auth/session/generate.ts index 2b46c4828d..b9e5e84f6e 100644 --- a/packages/backend/src/server/api/endpoints/auth/session/generate.ts +++ b/packages/backend/src/server/api/endpoints/auth/session/generate.ts @@ -57,12 +57,12 @@ export default define(meta, async (ps) => { const token = uuid(); // Create session token document - const doc = await AuthSessions.save({ + const doc = await AuthSessions.insert({ id: genId(), createdAt: new Date(), appId: app.id, token: token, - }); + }).then(x => AuthSessions.findOneOrFail(x.identifiers[0])); return { token: doc.token, diff --git a/packages/backend/src/server/api/endpoints/pages/create.ts b/packages/backend/src/server/api/endpoints/pages/create.ts index 65e579e4ea..dcf916909d 100644 --- a/packages/backend/src/server/api/endpoints/pages/create.ts +++ b/packages/backend/src/server/api/endpoints/pages/create.ts @@ -107,7 +107,7 @@ export default define(meta, async (ps, user) => { } }); - const page = await Pages.save(new Page({ + const page = await Pages.insert(new Page({ id: genId(), createdAt: new Date(), updatedAt: new Date(), @@ -123,7 +123,7 @@ export default define(meta, async (ps, user) => { alignCenter: ps.alignCenter, hideTitleWhenPinned: ps.hideTitleWhenPinned, font: ps.font, - })); + })).then(x => Pages.findOneOrFail(x.identifiers[0])); return await Pages.pack(page); }); diff --git a/packages/backend/src/server/api/endpoints/users/report-abuse.ts b/packages/backend/src/server/api/endpoints/users/report-abuse.ts index a1d8376651..bd80710fec 100644 --- a/packages/backend/src/server/api/endpoints/users/report-abuse.ts +++ b/packages/backend/src/server/api/endpoints/users/report-abuse.ts @@ -62,7 +62,7 @@ export default define(meta, async (ps, me) => { throw new ApiError(meta.errors.cannotReportAdmin); } - const report = await AbuseUserReports.save({ + const report = await AbuseUserReports.insert({ id: genId(), createdAt: new Date(), targetUserId: user.id, @@ -70,7 +70,7 @@ export default define(meta, async (ps, me) => { reporterId: me.id, reporterHost: null, comment: ps.comment, - }); + }).then(x => AbuseUserReports.findOneOrFail(x.identifiers[0])); // Publish event to moderators setTimeout(async () => { diff --git a/packages/backend/src/services/create-notification.ts b/packages/backend/src/services/create-notification.ts index fc43ab29d7..1c1c1fcdff 100644 --- a/packages/backend/src/services/create-notification.ts +++ b/packages/backend/src/services/create-notification.ts @@ -20,7 +20,7 @@ export async function createNotification( const isMuted = profile?.mutingNotificationTypes.includes(type); // Create notification - const notification = await Notifications.save({ + const notification = await Notifications.insert({ id: genId(), createdAt: new Date(), notifieeId: notifieeId, @@ -28,7 +28,8 @@ export async function createNotification( // 相手がこの通知をミュートしているようなら、既読を予めつけておく isRead: isMuted, ...data, - } as Partial); + } as Partial) + .then(x => Notifications.findOneOrFail(x.identifiers[0])); const packed = await Notifications.pack(notification, {});