2019-02-05 06:14:23 +01:00
|
|
|
import { publishNoteStream } from '../../stream';
|
2019-01-19 19:07:12 +01:00
|
|
|
import renderLike from '../../../remote/activitypub/renderer/like';
|
|
|
|
import renderUndo from '../../../remote/activitypub/renderer/undo';
|
2019-01-30 18:29:36 +01:00
|
|
|
import { renderActivity } from '../../../remote/activitypub/renderer';
|
2020-02-04 00:26:00 +01:00
|
|
|
import DeliverManager from '../../../remote/activitypub/deliver-manager';
|
2019-02-22 03:46:58 +01:00
|
|
|
import { IdentifiableError } from '../../../misc/identifiable-error';
|
2020-02-04 00:26:00 +01:00
|
|
|
import { User, IRemoteUser } from '../../../models/entities/user';
|
2019-04-07 14:50:36 +02:00
|
|
|
import { Note } from '../../../models/entities/note';
|
|
|
|
import { NoteReactions, Users, Notes } from '../../../models';
|
2019-01-19 19:07:12 +01:00
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
export default async (user: User, note: Note) => {
|
2019-01-19 19:07:12 +01:00
|
|
|
// if already unreacted
|
2019-04-07 14:50:36 +02:00
|
|
|
const exist = await NoteReactions.findOne({
|
|
|
|
noteId: note.id,
|
|
|
|
userId: user.id,
|
2019-01-19 19:07:12 +01:00
|
|
|
});
|
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
if (exist == null) {
|
2019-02-22 03:46:58 +01:00
|
|
|
throw new IdentifiableError('60527ec9-b4cb-4a88-a6bd-32d3ad26817d', 'not reacted');
|
2019-01-19 19:07:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete reaction
|
2019-04-07 14:50:36 +02:00
|
|
|
await NoteReactions.delete(exist.id);
|
2019-01-19 19:07:12 +01:00
|
|
|
|
|
|
|
// Decrement reactions count
|
2019-04-07 14:50:36 +02: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();
|
2019-04-14 13:28:57 +02:00
|
|
|
|
|
|
|
Notes.decrement({ id: note.id }, 'score', 1);
|
2019-04-07 14:50:36 +02:00
|
|
|
|
|
|
|
publishNoteStream(note.id, 'unreacted', {
|
2019-01-19 19:07:12 +01:00
|
|
|
reaction: exist.reaction,
|
2019-04-07 14:50:36 +02:00
|
|
|
userId: user.id
|
2019-01-19 19:07:12 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
//#region 配信
|
2020-02-04 00:26:00 +01:00
|
|
|
if (Users.isLocalUser(user) && !note.localOnly) {
|
2019-01-30 18:29:36 +01:00
|
|
|
const content = renderActivity(renderUndo(renderLike(user, note, exist.reaction), user));
|
2020-02-04 00:26:00 +01:00
|
|
|
const dm = new DeliverManager(user, content);
|
|
|
|
if (note.userHost !== null) {
|
|
|
|
const reactee = await Users.findOne(note.userId)
|
|
|
|
dm.addDirectRecipe(reactee as IRemoteUser);
|
|
|
|
}
|
|
|
|
dm.addFollowersRecipe();
|
|
|
|
dm.execute();
|
2019-01-19 19:07:12 +01:00
|
|
|
}
|
|
|
|
//#endregion
|
2019-02-22 03:46:58 +01:00
|
|
|
};
|