unoffical wafrn mirror
wafrn.net
atproto
social-network
activitypub
1import {
2 Blocks,
3 EmojiReaction,
4 Follows,
5 Notification,
6 Post,
7 User,
8 UserLikesPostRelations,
9} from "../../../models/index.js";
10import { activityPubObject } from "../../../interfaces/fediverse/activityPubObject.js";
11import { deletePostCommon } from "../../deletePost.js";
12import { logger } from "../../logger.js";
13import { redisCache } from "../../redis.js";
14import { getPostThreadRecursive } from "../getPostThreadRecursive.js";
15import { signAndAccept } from "../signAndAccept.js";
16
17async function UndoActivity(
18 body: activityPubObject,
19 remoteUser: User,
20 user: User
21) {
22 const apObject: activityPubObject =
23 body.object?.id && body.object?.type ? body.object : body;
24 // TODO divide this one in files too
25
26 // Unfollow? Destroy post? what else can be undone
27 switch (apObject.type) {
28 case "Block": {
29 const blockToRemove = await Blocks.findOne({
30 where: {
31 remoteBlockId: apObject.id,
32 },
33 });
34 if (blockToRemove) {
35 await blockToRemove.destroy();
36 }
37 redisCache.del("blocks:mutes:onlyUser:" + user.id);
38 redisCache.del("blocks:mutes:" + user.id);
39 redisCache.del("blocks:mutes:" + user.id);
40 redisCache.del("blocks:" + user.id);
41 // await signAndAccept({ body: body }, remoteUser, user)
42 break;
43 }
44 case "Follow": {
45 const remoteFollow = await Follows.findOne({
46 where: {
47 // I think i was doing something wrong here. Changed so when remote unfollow does not cause you to unfollow them instead lol
48 remoteFollowId: apObject.id,
49 },
50 });
51 if (remoteFollow) {
52 Notification.destroy({
53 where: {
54 notificationType: "FOLLOW",
55 notifiedUserId: remoteFollow.followedId,
56 userId: remoteFollow.followerId,
57 },
58 });
59 await remoteFollow.destroy();
60 }
61 // await signAndAccept({ body: body }, remoteUser, user)
62 break;
63 }
64 case "Undo": {
65 // just undo? Might be like might be something else.
66 const likeToRemove = await UserLikesPostRelations.findOne({
67 where: {
68 remoteId: apObject.id,
69 },
70 });
71 if (likeToRemove) {
72 await likeToRemove.destroy();
73 }
74 const emojiReactionToRemove = await EmojiReaction.findOne({
75 where: {
76 remoteId: apObject.id,
77 },
78 });
79 if (emojiReactionToRemove) {
80 await emojiReactionToRemove.destroy();
81 }
82 // await signAndAccept({ body: body }, remoteUser, user)
83
84 break;
85 }
86 case "Announce": {
87 const postToDelete = await Post.findOne({
88 where: {
89 remotePostId: apObject.id,
90 },
91 });
92 if (postToDelete) {
93 await deletePostCommon(postToDelete.id);
94 }
95 // await signAndAccept({ body: body }, remoteUser, user)
96 break;
97 }
98 case "Like": {
99 const likeToRemove = await UserLikesPostRelations.findOne({
100 where: {
101 remoteId: apObject.id,
102 },
103 });
104 if (likeToRemove) {
105 await Notification.destroy({
106 where: {
107 notificationType: "LIKE",
108 postId: likeToRemove.postId,
109 userId: likeToRemove.userId,
110 },
111 });
112 likeToRemove.destroy();
113 }
114 }
115 // eslint-disable-next-line no-fallthrough
116 case "EmojiReact": {
117 const reactionToRemove = await EmojiReaction.findOne({
118 where: {
119 remoteId: apObject.id,
120 },
121 });
122 if (reactionToRemove) {
123 await Notification.destroy({
124 where: {
125 emojiReactionId: reactionToRemove.id,
126 },
127 });
128 await reactionToRemove.destroy();
129 }
130 // await signAndAccept({ body: body }, remoteUser, user)
131 break;
132 }
133 // activities that we ignore:
134 case "View": {
135 // await signAndAccept({ body: body }, remoteUser, user)
136 break;
137 }
138 default: {
139 logger.debug({
140 message: `UNDO NOT IMPLEMENTED: ${apObject.type} attemping to delete post`,
141 object: apObject,
142 });
143 const postToDelete = await getPostThreadRecursive(user, apObject.object);
144 if (postToDelete) {
145 await deletePostCommon(postToDelete.id);
146 }
147 // await signAndAccept({ body: body }, remoteUser, user)
148 logger.debug(apObject);
149 }
150 }
151}
152
153export { UndoActivity };