summaryrefslogtreecommitdiff
path: root/src/services/note
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2021-03-22 15:27:08 +0900
committersyuilo <Syuilotan@yahoo.co.jp>2021-03-22 15:27:08 +0900
commit52d577c7dd7bf87b3fae34f539bb6e656c7c0ed2 (patch)
tree9805c625a7fba9d8631db8a92772b2772d8632ec /src/services/note
parentMerge branch 'develop' (diff)
parent12.75.0 (diff)
downloadmisskey-52d577c7dd7bf87b3fae34f539bb6e656c7c0ed2.tar.gz
misskey-52d577c7dd7bf87b3fae34f539bb6e656c7c0ed2.tar.bz2
misskey-52d577c7dd7bf87b3fae34f539bb6e656c7c0ed2.zip
Merge branch 'develop'
Diffstat (limited to 'src/services/note')
-rw-r--r--src/services/note/create.ts50
-rw-r--r--src/services/note/polls/vote.ts2
-rw-r--r--src/services/note/reaction/create.ts48
-rw-r--r--src/services/note/read.ts80
-rw-r--r--src/services/note/unread.ts6
-rw-r--r--src/services/note/watch.ts2
6 files changed, 95 insertions, 93 deletions
diff --git a/src/services/note/create.ts b/src/services/note/create.ts
index 563eaac758..4a737e8516 100644
--- a/src/services/note/create.ts
+++ b/src/services/note/create.ts
@@ -247,7 +247,7 @@ export default async (user: User, data: Option, silent = false) => new Promise<N
for (const u of us) {
checkWordMute(note, { id: u.userId }, u.mutedWords).then(shouldMute => {
if (shouldMute) {
- MutedNotes.save({
+ MutedNotes.insert({
id: genId(),
userId: u.userId,
noteId: note.id,
@@ -259,21 +259,21 @@ export default async (user: User, data: Option, silent = false) => new Promise<N
});
// Antenna
- Antennas.find().then(async antennas => {
- const followings = await Followings.createQueryBuilder('following')
- .andWhere(`following.followeeId = :userId`, { userId: note.userId })
- .getMany();
-
- const followers = followings.map(f => f.followerId);
-
- for (const antenna of antennas) {
- checkHitAntenna(antenna, note, user, followers).then(hit => {
- if (hit) {
- addNoteToAntenna(antenna, note, user);
+ Followings.createQueryBuilder('following')
+ .andWhere(`following.followeeId = :userId`, { userId: note.userId })
+ .getMany()
+ .then(followings => {
+ const followers = followings.map(f => f.followerId);
+ Antennas.find().then(async antennas => {
+ for (const antenna of antennas) {
+ checkHitAntenna(antenna, note, user, followers).then(hit => {
+ if (hit) {
+ addNoteToAntenna(antenna, note, user);
+ }
+ });
}
});
- }
- });
+ });
// Channel
if (note.channelId) {
@@ -444,8 +444,13 @@ async function renderNoteOrRenoteActivity(data: Option, note: Note) {
}
function incRenoteCount(renote: Note) {
- Notes.increment({ id: renote.id }, 'renoteCount', 1);
- Notes.increment({ id: renote.id }, 'score', 1);
+ Notes.createQueryBuilder().update()
+ .set({
+ renoteCount: () => '"renoteCount" + 1',
+ score: () => '"score" + 1'
+ })
+ .where('id = :id', { id: renote.id })
+ .execute();
}
async function insertNote(user: User, data: Option, tags: string[], emojis: string[], mentionedUsers: User[]) {
@@ -525,7 +530,7 @@ async function insertNote(user: User, data: Option, tags: string[], emojis: stri
await Notes.insert(insert);
}
- return await Notes.findOneOrFail(insert.id);
+ return insert;
} catch (e) {
// duplicate key error
if (isDuplicateKeyValueError(e)) {
@@ -594,10 +599,13 @@ function saveReply(reply: Note, note: Note) {
}
function incNotesCountOfUser(user: User) {
- Users.increment({ id: user.id }, 'notesCount', 1);
- Users.update({ id: user.id }, {
- updatedAt: new Date()
- });
+ Users.createQueryBuilder().update()
+ .set({
+ updatedAt: new Date(),
+ notesCount: () => '"notesCount" + 1'
+ })
+ .where('id = :id', { id: user.id })
+ .execute();
}
async function extractMentionedUsers(user: User, tokens: ReturnType<typeof parse>): Promise<User[]> {
diff --git a/src/services/note/polls/vote.ts b/src/services/note/polls/vote.ts
index bfcaaa09be..b4ce03ab60 100644
--- a/src/services/note/polls/vote.ts
+++ b/src/services/note/polls/vote.ts
@@ -29,7 +29,7 @@ export default async function(user: User, note: Note, choice: number) {
}
// Create vote
- await PollVotes.save({
+ await PollVotes.insert({
id: genId(),
createdAt: new Date(),
noteId: note.id,
diff --git a/src/services/note/reaction/create.ts b/src/services/note/reaction/create.ts
index adc96ddc1f..181099cc2d 100644
--- a/src/services/note/reaction/create.ts
+++ b/src/services/note/reaction/create.ts
@@ -11,45 +11,53 @@ import { perUserReactionsChart } from '../../chart';
import { genId } from '../../../misc/gen-id';
import { createNotification } from '../../create-notification';
import deleteReaction from './delete';
+import { isDuplicateKeyValueError } from '../../../misc/is-duplicate-key-value-error';
+import { NoteReaction } from '../../../models/entities/note-reaction';
export default async (user: User, note: Note, reaction?: string) => {
+ // TODO: cache
reaction = await toDbReaction(reaction, user.host);
- const exist = await NoteReactions.findOne({
+ let record: NoteReaction = {
+ id: genId(),
+ createdAt: new Date(),
noteId: note.id,
userId: user.id,
- });
+ reaction
+ };
- if (exist) {
- if (exist.reaction !== reaction) {
- // 別のリアクションがすでにされていたら置き換える
- await deleteReaction(user, note);
+ // Create reaction
+ try {
+ await NoteReactions.insert(record);
+ } catch (e) {
+ if (isDuplicateKeyValueError(e)) {
+ record = await NoteReactions.findOneOrFail({
+ noteId: note.id,
+ userId: user.id,
+ });
+
+ if (record.reaction !== reaction) {
+ // 別のリアクションがすでにされていたら置き換える
+ await deleteReaction(user, note);
+ } else {
+ // 同じリアクションがすでにされていたら何もしない
+ return;
+ }
} else {
- // 同じリアクションがすでにされていたら何もしない
- return;
+ throw e;
}
}
- // Create reaction
- const inserted = await NoteReactions.save({
- id: genId(),
- createdAt: new Date(),
- noteId: note.id,
- userId: user.id,
- reaction
- });
-
// Increment reactions count
const sql = `jsonb_set("reactions", '{${reaction}}', (COALESCE("reactions"->>'${reaction}', '0')::int + 1)::text::jsonb)`;
await Notes.createQueryBuilder().update()
.set({
reactions: () => sql,
+ score: () => '"score" + 1'
})
.where('id = :id', { id: note.id })
.execute();
- Notes.increment({ id: note.id }, 'score', 1);
-
perUserReactionsChart.update(user, note);
// カスタム絵文字リアクションだったら絵文字情報も送る
@@ -101,7 +109,7 @@ export default async (user: User, note: Note, reaction?: string) => {
//#region 配信
if (Users.isLocalUser(user) && !note.localOnly) {
- const content = renderActivity(await renderLike(inserted, note));
+ const content = renderActivity(await renderLike(record, note));
const dm = new DeliverManager(user, content);
if (note.userHost !== null) {
const reactee = await Users.findOne(note.userId);
diff --git a/src/services/note/read.ts b/src/services/note/read.ts
index 5a39ab30b7..35279db411 100644
--- a/src/services/note/read.ts
+++ b/src/services/note/read.ts
@@ -2,70 +2,54 @@ 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';
+import { Not, IsNull, In } from 'typeorm';
/**
- * Mark a note as read
+ * Mark notes as read
*/
export default async function(
userId: User['id'],
- noteId: Note['id']
+ noteIds: 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,
+ noteId: In(noteIds),
});
- if (exist.isMentioned) {
- NoteUnreads.count({
- userId: userId,
- isMentioned: true
- }).then(mentionsCount => {
- if (mentionsCount === 0) {
- // 全て既読になったイベントを発行
- publishMainStream(userId, 'readAllUnreadMentions');
- }
- });
- }
+ 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');
- }
- });
- }
+ 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');
- }
- });
- }
+ 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 => {
@@ -78,7 +62,7 @@ export default async function(
await AntennaNotes.update({
antennaId: antenna.id,
- noteId: noteId
+ noteId: In(noteIds)
}, {
read: true
});
diff --git a/src/services/note/unread.ts b/src/services/note/unread.ts
index 6fd9ee2cfe..8e6fb4abe8 100644
--- a/src/services/note/unread.ts
+++ b/src/services/note/unread.ts
@@ -17,7 +17,7 @@ export default async function(userId: User['id'], note: Note, params: {
if (mute.map(m => m.muteeId).includes(note.userId)) return;
//#endregion
- const unread = await NoteUnreads.save({
+ const unread = {
id: genId(),
noteId: note.id,
userId: userId,
@@ -25,7 +25,9 @@ export default async function(userId: User['id'], note: Note, params: {
isMentioned: params.isMentioned,
noteChannelId: note.channelId,
noteUserId: note.userId,
- });
+ };
+
+ await NoteUnreads.insert(unread);
// 2秒経っても既読にならなかったら「未読の投稿がありますよ」イベントを発行する
setTimeout(async () => {
diff --git a/src/services/note/watch.ts b/src/services/note/watch.ts
index d3c9553696..966b7f0054 100644
--- a/src/services/note/watch.ts
+++ b/src/services/note/watch.ts
@@ -10,7 +10,7 @@ export default async (me: User['id'], note: Note) => {
return;
}
- await NoteWatchings.save({
+ await NoteWatchings.insert({
id: genId(),
createdAt: new Date(),
noteId: note.id,