Add shares forward and collection on videos/video channels
[oweals/peertube.git] / server / lib / activitypub / send / send-undo.ts
1 import { Transaction } from 'sequelize'
2 import {
3   ActivityAudience,
4   ActivityCreate,
5   ActivityFollow,
6   ActivityLike,
7   ActivityUndo
8 } from '../../../../shared/models/activitypub/activity'
9 import { AccountInstance } from '../../../models'
10 import { AccountFollowInstance } from '../../../models/account/account-follow-interface'
11 import { VideoInstance } from '../../../models/video/video-interface'
12 import { getAccountFollowActivityPubUrl, getUndoActivityPubUrl, getVideoDislikeActivityPubUrl, getVideoLikeActivityPubUrl } from '../url'
13 import { broadcastToFollowers, getAccountsInvolvedInVideo, getAudience, getObjectFollowersAudience, unicastTo } from './misc'
14 import { createActivityData, createDislikeActivityData } from './send-create'
15 import { followActivityData } from './send-follow'
16 import { likeActivityData } from './send-like'
17
18 async function sendUndoFollow (accountFollow: AccountFollowInstance, t: Transaction) {
19   const me = accountFollow.AccountFollower
20   const following = accountFollow.AccountFollowing
21
22   const followUrl = getAccountFollowActivityPubUrl(accountFollow)
23   const undoUrl = getUndoActivityPubUrl(followUrl)
24
25   const object = await followActivityData(followUrl, me, following)
26   const data = await undoActivityData(undoUrl, me, object)
27
28   return unicastTo(data, me, following.inboxUrl, t)
29 }
30
31 async function sendUndoLikeToOrigin (byAccount: AccountInstance, video: VideoInstance, t: Transaction) {
32   const likeUrl = getVideoLikeActivityPubUrl(byAccount, video)
33   const undoUrl = getUndoActivityPubUrl(likeUrl)
34
35   const object = await likeActivityData(likeUrl, byAccount, video)
36   const data = await undoActivityData(undoUrl, byAccount, object)
37
38   return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t)
39 }
40
41 async function sendUndoLikeToVideoFollowers (byAccount: AccountInstance, video: VideoInstance, t: Transaction) {
42   const likeUrl = getVideoLikeActivityPubUrl(byAccount, video)
43   const undoUrl = getUndoActivityPubUrl(likeUrl)
44
45   const toAccountsFollowers = await getAccountsInvolvedInVideo(video)
46   const audience = getObjectFollowersAudience(toAccountsFollowers)
47   const object = await likeActivityData(likeUrl, byAccount, video)
48   const data = await undoActivityData(undoUrl, byAccount, object, audience)
49
50   const followersException = [ byAccount ]
51   return broadcastToFollowers(data, byAccount, toAccountsFollowers, t, followersException)
52 }
53
54 async function sendUndoDislikeToOrigin (byAccount: AccountInstance, video: VideoInstance, t: Transaction) {
55   const dislikeUrl = getVideoDislikeActivityPubUrl(byAccount, video)
56   const undoUrl = getUndoActivityPubUrl(dislikeUrl)
57
58   const dislikeActivity = createDislikeActivityData(byAccount, video)
59   const object = await createActivityData(undoUrl, byAccount, dislikeActivity)
60
61   const data = await undoActivityData(undoUrl, byAccount, object)
62
63   return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t)
64 }
65
66 async function sendUndoDislikeToVideoFollowers (byAccount: AccountInstance, video: VideoInstance, t: Transaction) {
67   const dislikeUrl = getVideoDislikeActivityPubUrl(byAccount, video)
68   const undoUrl = getUndoActivityPubUrl(dislikeUrl)
69
70   const dislikeActivity = createDislikeActivityData(byAccount, video)
71   const object = await createActivityData(undoUrl, byAccount, dislikeActivity)
72
73   const data = await undoActivityData(undoUrl, byAccount, object)
74
75   const toAccountsFollowers = await getAccountsInvolvedInVideo(video)
76
77   const followersException = [ byAccount ]
78   return broadcastToFollowers(data, byAccount, toAccountsFollowers, t, followersException)
79 }
80
81 // ---------------------------------------------------------------------------
82
83 export {
84   sendUndoFollow,
85   sendUndoLikeToOrigin,
86   sendUndoLikeToVideoFollowers,
87   sendUndoDislikeToOrigin,
88   sendUndoDislikeToVideoFollowers
89 }
90
91 // ---------------------------------------------------------------------------
92
93 async function undoActivityData (
94   url: string,
95   byAccount: AccountInstance,
96   object: ActivityFollow | ActivityLike | ActivityCreate,
97   audience?: ActivityAudience
98 ) {
99   if (!audience) {
100     audience = await getAudience(byAccount)
101   }
102
103   const activity: ActivityUndo = {
104     type: 'Undo',
105     id: url,
106     actor: byAccount.url,
107     to: audience.to,
108     cc: audience.cc,
109     object
110   }
111
112   return activity
113 }