]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/send/send-undo.ts
Save
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / send / send-undo.ts
CommitLineData
54141398 1import { Transaction } from 'sequelize'
63c93323
C
2import {
3 ActivityAudience,
4 ActivityCreate,
5 ActivityFollow,
6 ActivityLike,
7 ActivityUndo
3fd3ab2d
C
8} from '../../../../shared/models/activitypub'
9import { AccountModel } from '../../../models/account/account'
10import { AccountFollowModel } from '../../../models/account/account-follow'
11import { VideoModel } from '../../../models/video/video'
d8553faa 12import { getAccountFollowActivityPubUrl, getUndoActivityPubUrl, getVideoDislikeActivityPubUrl, getVideoLikeActivityPubUrl } from '../url'
5e3bb76c
C
13import {
14 broadcastToFollowers,
15 getAccountsInvolvedInVideo,
16 getAudience,
17 getObjectFollowersAudience,
18 getOriginVideoAudience,
19 unicastTo
20} from './misc'
d8553faa 21import { createActivityData, createDislikeActivityData } from './send-create'
54141398 22import { followActivityData } from './send-follow'
0032ebe9 23import { likeActivityData } from './send-like'
54141398 24
3fd3ab2d 25async function sendUndoFollow (accountFollow: AccountFollowModel, t: Transaction) {
54141398
C
26 const me = accountFollow.AccountFollower
27 const following = accountFollow.AccountFollowing
28
29 const followUrl = getAccountFollowActivityPubUrl(accountFollow)
30 const undoUrl = getUndoActivityPubUrl(followUrl)
31
25ed141c
C
32 const object = followActivityData(followUrl, me, following)
33 const data = await undoActivityData(undoUrl, me, object, t)
54141398
C
34
35 return unicastTo(data, me, following.inboxUrl, t)
36}
37
3fd3ab2d 38async function sendUndoLikeToOrigin (byAccount: AccountModel, video: VideoModel, t: Transaction) {
0032ebe9
C
39 const likeUrl = getVideoLikeActivityPubUrl(byAccount, video)
40 const undoUrl = getUndoActivityPubUrl(likeUrl)
41
25ed141c 42 const accountsInvolvedInVideo = await getAccountsInvolvedInVideo(video, t)
5e3bb76c 43 const audience = getOriginVideoAudience(video, accountsInvolvedInVideo)
25ed141c
C
44 const object = await likeActivityData(likeUrl, byAccount, video, t)
45 const data = await undoActivityData(undoUrl, byAccount, object, t, audience)
0032ebe9
C
46
47 return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t)
48}
49
3fd3ab2d 50async function sendUndoLikeToVideoFollowers (byAccount: AccountModel, video: VideoModel, t: Transaction) {
0032ebe9
C
51 const likeUrl = getVideoLikeActivityPubUrl(byAccount, video)
52 const undoUrl = getUndoActivityPubUrl(likeUrl)
53
25ed141c 54 const toAccountsFollowers = await getAccountsInvolvedInVideo(video, t)
4e50b6a1 55 const audience = getObjectFollowersAudience(toAccountsFollowers)
25ed141c
C
56 const object = await likeActivityData(likeUrl, byAccount, video, t)
57 const data = await undoActivityData(undoUrl, byAccount, object, t, audience)
0032ebe9
C
58
59 const followersException = [ byAccount ]
63c93323 60 return broadcastToFollowers(data, byAccount, toAccountsFollowers, t, followersException)
0032ebe9
C
61}
62
3fd3ab2d 63async function sendUndoDislikeToOrigin (byAccount: AccountModel, video: VideoModel, t: Transaction) {
0032ebe9
C
64 const dislikeUrl = getVideoDislikeActivityPubUrl(byAccount, video)
65 const undoUrl = getUndoActivityPubUrl(dislikeUrl)
66
25ed141c 67 const accountsInvolvedInVideo = await getAccountsInvolvedInVideo(video, t)
5e3bb76c 68 const audience = getOriginVideoAudience(video, accountsInvolvedInVideo)
0032ebe9 69 const dislikeActivity = createDislikeActivityData(byAccount, video)
fef2c716 70 const object = await createActivityData(undoUrl, byAccount, dislikeActivity, t)
0032ebe9 71
fef2c716 72 const data = await undoActivityData(undoUrl, byAccount, object, t, audience)
0032ebe9
C
73
74 return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t)
75}
76
3fd3ab2d 77async function sendUndoDislikeToVideoFollowers (byAccount: AccountModel, video: VideoModel, t: Transaction) {
0032ebe9
C
78 const dislikeUrl = getVideoDislikeActivityPubUrl(byAccount, video)
79 const undoUrl = getUndoActivityPubUrl(dislikeUrl)
80
81 const dislikeActivity = createDislikeActivityData(byAccount, video)
25ed141c 82 const object = await createActivityData(undoUrl, byAccount, dislikeActivity, t)
0032ebe9 83
25ed141c 84 const data = await undoActivityData(undoUrl, byAccount, object, t)
0032ebe9 85
25ed141c 86 const toAccountsFollowers = await getAccountsInvolvedInVideo(video, t)
0032ebe9
C
87
88 const followersException = [ byAccount ]
63c93323 89 return broadcastToFollowers(data, byAccount, toAccountsFollowers, t, followersException)
0032ebe9
C
90}
91
54141398
C
92// ---------------------------------------------------------------------------
93
94export {
0032ebe9
C
95 sendUndoFollow,
96 sendUndoLikeToOrigin,
97 sendUndoLikeToVideoFollowers,
98 sendUndoDislikeToOrigin,
99 sendUndoDislikeToVideoFollowers
54141398
C
100}
101
102// ---------------------------------------------------------------------------
103
63c93323
C
104async function undoActivityData (
105 url: string,
3fd3ab2d 106 byAccount: AccountModel,
63c93323 107 object: ActivityFollow | ActivityLike | ActivityCreate,
25ed141c 108 t: Transaction,
63c93323 109 audience?: ActivityAudience
3fd3ab2d 110): Promise<ActivityUndo> {
63c93323 111 if (!audience) {
25ed141c 112 audience = await getAudience(byAccount, t)
63c93323
C
113 }
114
3fd3ab2d 115 return {
54141398
C
116 type: 'Undo',
117 id: url,
118 actor: byAccount.url,
63c93323
C
119 to: audience.to,
120 cc: audience.cc,
54141398
C
121 object
122 }
54141398 123}