From 4198246351a64c3f3c0cd05bbd1e7146d95fcd32 Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 12 Apr 2019 01:30:10 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=88=E3=83=A9=E3=83=B3=E3=82=B6=E3=82=AF?= =?UTF-8?q?=E3=82=B7=E3=83=A7=E3=83=B3=E3=82=92=E4=BD=BF=E7=94=A8=E3=81=97?= =?UTF-8?q?=E3=81=A6=E3=82=A2=E3=83=B3=E3=82=B1=E3=83=BC=E3=83=88=E3=83=AC?= =?UTF-8?q?=E3=82=B3=E3=83=BC=E3=83=89=E3=81=AE=E6=8C=BF=E5=85=A5=E3=81=AB?= =?UTF-8?q?=E5=A4=B1=E6=95=97=E3=81=97=E3=81=9F=E5=A0=B4=E5=90=88=E3=81=AB?= =?UTF-8?q?=E6=8A=95=E7=A8=BF=E3=83=AC=E3=82=B3=E3=83=BC=E3=83=89=E3=81=AE?= =?UTF-8?q?=E6=8C=BF=E5=85=A5=E3=82=82=E3=81=AA=E3=81=8B=E3=81=A3=E3=81=9F?= =?UTF-8?q?=E3=81=93=E3=81=A8=E3=81=AB=E3=81=99=E3=82=8B=E3=82=88=E3=81=86?= =?UTF-8?q?=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/models/entities/note.ts | 8 ++++++++ src/models/entities/poll.ts | 8 ++++++++ src/services/note/create.ts | 40 ++++++++++++++++++++++--------------- 3 files changed, 40 insertions(+), 16 deletions(-) diff --git a/src/models/entities/note.ts b/src/models/entities/note.ts index 176fc626b..6c7aebdd0 100644 --- a/src/models/entities/note.ts +++ b/src/models/entities/note.ts @@ -214,6 +214,14 @@ export class Note { }) public renoteUserHost: string | null; //#endregion + + constructor(data: Partial) { + if (data == null) return; + + for (const [k, v] of Object.entries(data)) { + (this as any)[k] = v; + } + } } export type IMentionedRemoteUsers = { diff --git a/src/models/entities/poll.ts b/src/models/entities/poll.ts index 894f07e68..c0ad5547b 100644 --- a/src/models/entities/poll.ts +++ b/src/models/entities/poll.ts @@ -53,6 +53,14 @@ export class Poll { }) public userHost: string | null; //#endregion + + constructor(data: Partial) { + if (data == null) return; + + for (const [k, v] of Object.entries(data)) { + (this as any)[k] = v; + } + } } export type IPoll = { diff --git a/src/services/note/create.ts b/src/services/note/create.ts index 9f79bf7a6..b66f61ae8 100644 --- a/src/services/note/create.ts +++ b/src/services/note/create.ts @@ -17,10 +17,10 @@ import extractMentions from '../../misc/extract-mentions'; import extractEmojis from '../../misc/extract-emojis'; import extractHashtags from '../../misc/extract-hashtags'; import { Note } from '../../models/entities/note'; -import { Mutings, Users, NoteWatchings, Followings, Notes, Instances, Polls, UserProfiles } from '../../models'; +import { Mutings, Users, NoteWatchings, Followings, Notes, Instances, UserProfiles } from '../../models'; import { DriveFile } from '../../models/entities/drive-file'; import { App } from '../../models/entities/app'; -import { Not } from 'typeorm'; +import { Not, getConnection } from 'typeorm'; import { User, ILocalUser, IRemoteUser } from '../../models/entities/user'; import { genId } from '../../misc/gen-id'; import { notesChart, perUserNotesChart, activeUsersChart, instanceChart } from '../chart'; @@ -349,7 +349,7 @@ async function publish(user: User, note: Note, reply: Note, renote: Note, noteAc } async function insertNote(user: User, data: Option, tags: string[], emojis: string[], mentionedUsers: User[]) { - const insert: Partial = { + const insert = new Note({ id: genId(data.createdAt), createdAt: data.createdAt, fileIds: data.files ? data.files.map(file => file.id) : [], @@ -381,7 +381,7 @@ async function insertNote(user: User, data: Option, tags: string[], emojis: stri renoteUserId: data.renote ? data.renote.userId : null, renoteUserHost: data.renote ? data.renote.userHost : null, userHost: user.host, - }; + }); if (data.uri != null) insert.uri = data.uri; @@ -397,19 +397,27 @@ async function insertNote(user: User, data: Option, tags: string[], emojis: stri // 投稿を作成 try { - const note = await Notes.save(insert); + let note: Note; + if (insert.hasPoll) { + // Start transaction + await getConnection().transaction(async transactionalEntityManager => { + note = await transactionalEntityManager.save(insert); - if (note.hasPoll) { - await Polls.save({ - noteId: note.id, - choices: data.poll.choices, - expiresAt: data.poll.expiresAt, - multiple: data.poll.multiple, - votes: new Array(data.poll.choices.length).fill(0), - noteVisibility: note.visibility, - userId: user.id, - userHost: user.host - } as Poll); + const poll = new Poll({ + noteId: note.id, + choices: data.poll.choices, + expiresAt: data.poll.expiresAt, + multiple: data.poll.multiple, + votes: new Array(data.poll.choices.length).fill(0), + noteVisibility: note.visibility, + userId: user.id, + userHost: user.host + }); + + await transactionalEntityManager.save(poll); + }); + } else { + note = await Notes.save(insert); } return note;