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