2019-02-05 14:14:23 +09:00
|
|
|
import { publishNoteStream } from '../../stream';
|
2019-01-20 03:07:12 +09:00
|
|
|
import renderLike from '../../../remote/activitypub/renderer/like';
|
|
|
|
import renderUndo from '../../../remote/activitypub/renderer/undo';
|
2019-01-31 02:29:36 +09:00
|
|
|
import { renderActivity } from '../../../remote/activitypub/renderer';
|
2019-01-20 03:07:12 +09:00
|
|
|
import { deliver } from '../../../queue';
|
2019-02-22 11:46:58 +09:00
|
|
|
import { IdentifiableError } from '../../../misc/identifiable-error';
|
2019-04-07 21:50:36 +09:00
|
|
|
import { User } from '../../../models/entities/user';
|
|
|
|
import { Note } from '../../../models/entities/note';
|
|
|
|
import { NoteReactions, Users, Notes } from '../../../models';
|
2019-01-20 03:07:12 +09:00
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
export default async (user: User, note: Note) => {
|
2019-01-20 03:07:12 +09:00
|
|
|
// if already unreacted
|
2019-04-07 21:50:36 +09:00
|
|
|
const exist = await NoteReactions.findOne({
|
|
|
|
noteId: note.id,
|
|
|
|
userId: user.id,
|
2019-01-20 03:07:12 +09:00
|
|
|
});
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
if (exist == null) {
|
2019-02-22 11:46:58 +09:00
|
|
|
throw new IdentifiableError('60527ec9-b4cb-4a88-a6bd-32d3ad26817d', 'not reacted');
|
2019-01-20 03:07:12 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete reaction
|
2019-04-07 21:50:36 +09:00
|
|
|
await NoteReactions.delete(exist.id);
|
2019-01-20 03:07:12 +09:00
|
|
|
|
|
|
|
// Decrement reactions count
|
2019-04-07 21:50:36 +09:00
|
|
|
const sql = `jsonb_set("reactions", '{${exist.reaction}}', (COALESCE("reactions"->>'${exist.reaction}', '0')::int - 1)::text::jsonb)`;
|
|
|
|
await Notes.createQueryBuilder().update()
|
|
|
|
.set({
|
|
|
|
reactions: () => sql,
|
|
|
|
})
|
|
|
|
.where('id = :id', { id: note.id })
|
|
|
|
.execute();
|
|
|
|
// v11 dec score
|
|
|
|
|
|
|
|
publishNoteStream(note.id, 'unreacted', {
|
2019-01-20 03:07:12 +09:00
|
|
|
reaction: exist.reaction,
|
2019-04-07 21:50:36 +09:00
|
|
|
userId: user.id
|
2019-01-20 03:07:12 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
//#region 配信
|
|
|
|
// リアクターがローカルユーザーかつリアクション対象がリモートユーザーの投稿なら配送
|
2019-04-07 21:50:36 +09:00
|
|
|
if (Users.isLocalUser(user) && (note.userHost !== null)) {
|
2019-01-31 02:29:36 +09:00
|
|
|
const content = renderActivity(renderUndo(renderLike(user, note, exist.reaction), user));
|
2019-04-12 00:42:39 +09:00
|
|
|
Users.findOne(note.userId).then(u => {
|
|
|
|
deliver(user, content, u.inbox);
|
|
|
|
});
|
2019-01-20 03:07:12 +09:00
|
|
|
}
|
|
|
|
//#endregion
|
2019-02-22 11:46:58 +09:00
|
|
|
};
|