summaryrefslogtreecommitdiff
path: root/src/services/note/read-specified-note.ts
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2021-03-19 20:43:24 +0900
committersyuilo <Syuilotan@yahoo.co.jp>2021-03-19 20:43:24 +0900
commit87c8f9ff953499340496e9c5db09c93eaff08851 (patch)
tree13c00ab0edf7bae614216a06a015c9c50b056b83 /src/services/note/read-specified-note.ts
parentperf(server): Improver performance (diff)
downloadsharkey-87c8f9ff953499340496e9c5db09c93eaff08851.tar.gz
sharkey-87c8f9ff953499340496e9c5db09c93eaff08851.tar.bz2
sharkey-87c8f9ff953499340496e9c5db09c93eaff08851.zip
perf: Reduce database query
Diffstat (limited to 'src/services/note/read-specified-note.ts')
-rw-r--r--src/services/note/read-specified-note.ts29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/services/note/read-specified-note.ts b/src/services/note/read-specified-note.ts
new file mode 100644
index 0000000000..0fcb66bf98
--- /dev/null
+++ b/src/services/note/read-specified-note.ts
@@ -0,0 +1,29 @@
+import { publishMainStream } from '../stream';
+import { Note } from '../../models/entities/note';
+import { User } from '../../models/entities/user';
+import { NoteUnreads } from '../../models';
+import { In } from 'typeorm';
+
+/**
+ * Mark a specified note as read
+ */
+export async function readSpecifiedNote(
+ userId: User['id'],
+ noteIds: Note['id'][]
+) {
+ // Remove the records
+ await NoteUnreads.delete({
+ userId: userId,
+ noteId: In(noteIds),
+ });
+
+ const specifiedCount = await NoteUnreads.count({
+ userId: userId,
+ isSpecified: true
+ });
+
+ if (specifiedCount === 0) {
+ // 全て既読になったイベントを発行
+ publishMainStream(userId, 'readAllUnreadSpecifiedNotes');
+ }
+}