From 21a7cb954678e6f888d4192800cd165174a937ed 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 8245010de..e060e86dd 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 cca054b52..a0fdf7f23 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 b713260ac..df986fc45 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 feb874db7..7d169d6ca 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 bafa658a5..f7a0fdb87 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 2b46c4828..b9e5e84f6 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 65e579e4e..dcf916909 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 a1d837665..bd80710fe 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 fc43ab29d..1c1c1fcdf 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, {});