summaryrefslogtreecommitdiff
path: root/src/services
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2019-04-12 01:30:10 +0900
committersyuilo <syuilotan@yahoo.co.jp>2019-04-12 01:30:10 +0900
commit4198246351a64c3f3c0cd05bbd1e7146d95fcd32 (patch)
tree1cb90e123affec540c9830167816023eab72fd6f /src/services
parentFix bug (diff)
downloadsharkey-4198246351a64c3f3c0cd05bbd1e7146d95fcd32.tar.gz
sharkey-4198246351a64c3f3c0cd05bbd1e7146d95fcd32.tar.bz2
sharkey-4198246351a64c3f3c0cd05bbd1e7146d95fcd32.zip
トランザクションを使用してアンケートレコードの挿入に失敗した場合に投稿レコードの挿入もなかったことにするように
Diffstat (limited to 'src/services')
-rw-r--r--src/services/note/create.ts40
1 files changed, 24 insertions, 16 deletions
diff --git a/src/services/note/create.ts b/src/services/note/create.ts
index 9f79bf7a63..b66f61ae8e 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<Note> = {
+ 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);
+
+ 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
+ });
- 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);
+ await transactionalEntityManager.save(poll);
+ });
+ } else {
+ note = await Notes.save(insert);
}
return note;