summaryrefslogtreecommitdiff
path: root/src/services/note
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2021-03-21 15:35:02 +0900
committersyuilo <Syuilotan@yahoo.co.jp>2021-03-21 15:35:02 +0900
commit630464f38d2524ddc5d11d2abd4fddcccc4240d4 (patch)
tree54d61708d279205f1da4fb81cfd42959f0bbcaf5 /src/services/note
parentperf: 各ストリーミング接続ごとにポーリングしないように (diff)
downloadsharkey-630464f38d2524ddc5d11d2abd4fddcccc4240d4.tar.gz
sharkey-630464f38d2524ddc5d11d2abd4fddcccc4240d4.tar.bz2
sharkey-630464f38d2524ddc5d11d2abd4fddcccc4240d4.zip
Revert "perf: Reduce database query"
This reverts commit 87c8f9ff953499340496e9c5db09c93eaff08851.
Diffstat (limited to 'src/services/note')
-rw-r--r--src/services/note/read-mention.ts29
-rw-r--r--src/services/note/read-specified-note.ts29
-rw-r--r--src/services/note/read.ts105
3 files changed, 105 insertions, 58 deletions
diff --git a/src/services/note/read-mention.ts b/src/services/note/read-mention.ts
deleted file mode 100644
index 2a668ecd6c..0000000000
--- a/src/services/note/read-mention.ts
+++ /dev/null
@@ -1,29 +0,0 @@
-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 mention note as read
- */
-export async function readMention(
- userId: User['id'],
- noteIds: Note['id'][]
-) {
- // Remove the records
- await NoteUnreads.delete({
- userId: userId,
- noteId: In(noteIds),
- });
-
- const mentionsCount = await NoteUnreads.count({
- userId: userId,
- isMentioned: true
- });
-
- if (mentionsCount === 0) {
- // 全て既読になったイベントを発行
- publishMainStream(userId, 'readAllUnreadMentions');
- }
-}
diff --git a/src/services/note/read-specified-note.ts b/src/services/note/read-specified-note.ts
deleted file mode 100644
index 0fcb66bf98..0000000000
--- a/src/services/note/read-specified-note.ts
+++ /dev/null
@@ -1,29 +0,0 @@
-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');
- }
-}
diff --git a/src/services/note/read.ts b/src/services/note/read.ts
new file mode 100644
index 0000000000..5a39ab30b7
--- /dev/null
+++ b/src/services/note/read.ts
@@ -0,0 +1,105 @@
+import { publishMainStream } from '../stream';
+import { Note } from '../../models/entities/note';
+import { User } from '../../models/entities/user';
+import { NoteUnreads, Antennas, AntennaNotes, Users } from '../../models';
+import { Not, IsNull } from 'typeorm';
+
+/**
+ * Mark a note as read
+ */
+export default async function(
+ userId: User['id'],
+ noteId: Note['id']
+) {
+ async function careNoteUnreads() {
+ const exist = await NoteUnreads.findOne({
+ userId: userId,
+ noteId: noteId,
+ });
+
+ if (!exist) return;
+
+ // Remove the record
+ await NoteUnreads.delete({
+ userId: userId,
+ noteId: noteId,
+ });
+
+ if (exist.isMentioned) {
+ NoteUnreads.count({
+ userId: userId,
+ isMentioned: true
+ }).then(mentionsCount => {
+ if (mentionsCount === 0) {
+ // 全て既読になったイベントを発行
+ publishMainStream(userId, 'readAllUnreadMentions');
+ }
+ });
+ }
+
+ if (exist.isSpecified) {
+ NoteUnreads.count({
+ userId: userId,
+ isSpecified: true
+ }).then(specifiedCount => {
+ if (specifiedCount === 0) {
+ // 全て既読になったイベントを発行
+ publishMainStream(userId, 'readAllUnreadSpecifiedNotes');
+ }
+ });
+ }
+
+ if (exist.noteChannelId) {
+ NoteUnreads.count({
+ userId: userId,
+ noteChannelId: Not(IsNull())
+ }).then(channelNoteCount => {
+ if (channelNoteCount === 0) {
+ // 全て既読になったイベントを発行
+ publishMainStream(userId, 'readAllChannels');
+ }
+ });
+ }
+ }
+
+ async function careAntenna() {
+ const beforeUnread = await Users.getHasUnreadAntenna(userId);
+ if (!beforeUnread) return;
+
+ const antennas = await Antennas.find({ userId });
+
+ await Promise.all(antennas.map(async antenna => {
+ const countBefore = await AntennaNotes.count({
+ antennaId: antenna.id,
+ read: false
+ });
+
+ if (countBefore === 0) return;
+
+ await AntennaNotes.update({
+ antennaId: antenna.id,
+ noteId: noteId
+ }, {
+ read: true
+ });
+
+ const countAfter = await AntennaNotes.count({
+ antennaId: antenna.id,
+ read: false
+ });
+
+ if (countAfter === 0) {
+ publishMainStream(userId, 'readAntenna', antenna);
+ }
+ }));
+
+ Users.getHasUnreadAntenna(userId).then(unread => {
+ if (!unread) {
+ publishMainStream(userId, 'readAllAntennas');
+ }
+ });
+ }
+
+ careNoteUnreads();
+ careAntenna();
+}