Add more info logging
[oweals/peertube.git] / server / lib / activitypub / send / send-undo.ts
1 import { Transaction } from 'sequelize'
2 import {
3   ActivityAnnounce,
4   ActivityAudience,
5   ActivityCreate,
6   ActivityFollow,
7   ActivityLike,
8   ActivityUndo
9 } from '../../../../shared/models/activitypub'
10 import { ActorModel } from '../../../models/activitypub/actor'
11 import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
12 import { VideoModel } from '../../../models/video/video'
13 import { getActorFollowActivityPubUrl, getUndoActivityPubUrl, getVideoDislikeActivityPubUrl, getVideoLikeActivityPubUrl } from '../url'
14 import { broadcastToFollowers, unicastTo } from './utils'
15 import { audiencify, getActorsInvolvedInVideo, getAudience, getObjectFollowersAudience, getVideoAudience } from '../audience'
16 import { createActivityData, createDislikeActivityData } from './send-create'
17 import { followActivityData } from './send-follow'
18 import { likeActivityData } from './send-like'
19 import { VideoShareModel } from '../../../models/video/video-share'
20 import { buildVideoAnnounce } from './send-announce'
21 import { logger } from '../../../helpers/logger'
22
23 async function sendUndoFollow (actorFollow: ActorFollowModel, t: Transaction) {
24   const me = actorFollow.ActorFollower
25   const following = actorFollow.ActorFollowing
26
27   logger.info('Creating job to send an unfollow request to %s.', following.url)
28
29   const followUrl = getActorFollowActivityPubUrl(actorFollow)
30   const undoUrl = getUndoActivityPubUrl(followUrl)
31
32   const object = followActivityData(followUrl, me, following)
33   const data = undoActivityData(undoUrl, me, object)
34
35   return unicastTo(data, me, following.inboxUrl)
36 }
37
38 async function sendUndoLike (byActor: ActorModel, video: VideoModel, t: Transaction) {
39   logger.info('Creating job to undo a like of video %s.', video.url)
40
41   const likeUrl = getVideoLikeActivityPubUrl(byActor, video)
42   const undoUrl = getUndoActivityPubUrl(likeUrl)
43
44   const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
45   const object = likeActivityData(likeUrl, byActor, video)
46
47   // Send to origin
48   if (video.isOwned() === false) {
49     const audience = getVideoAudience(video, actorsInvolvedInVideo)
50     const data = undoActivityData(undoUrl, byActor, object, audience)
51
52     return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
53   }
54
55   const audience = getObjectFollowersAudience(actorsInvolvedInVideo)
56   const data = undoActivityData(undoUrl, byActor, object, audience)
57
58   const followersException = [ byActor ]
59   return broadcastToFollowers(data, byActor, actorsInvolvedInVideo, t, followersException)
60 }
61
62 async function sendUndoDislike (byActor: ActorModel, video: VideoModel, t: Transaction) {
63   logger.info('Creating job to undo a dislike of video %s.', video.url)
64
65   const dislikeUrl = getVideoDislikeActivityPubUrl(byActor, video)
66   const undoUrl = getUndoActivityPubUrl(dislikeUrl)
67
68   const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
69   const dislikeActivity = createDislikeActivityData(byActor, video)
70   const object = createActivityData(dislikeUrl, byActor, dislikeActivity)
71
72   if (video.isOwned() === false) {
73     const audience = getVideoAudience(video, actorsInvolvedInVideo)
74     const data = undoActivityData(undoUrl, byActor, object, audience)
75
76     return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
77   }
78
79   const data = undoActivityData(undoUrl, byActor, object)
80
81   const followersException = [ byActor ]
82   return broadcastToFollowers(data, byActor, actorsInvolvedInVideo, t, followersException)
83 }
84
85 async function sendUndoAnnounce (byActor: ActorModel, videoShare: VideoShareModel, video: VideoModel, t: Transaction) {
86   logger.info('Creating job to undo announce %s.', videoShare.url)
87
88   const undoUrl = getUndoActivityPubUrl(videoShare.url)
89
90   const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
91   const object = await buildVideoAnnounce(byActor, videoShare, video, t)
92   const data = undoActivityData(undoUrl, byActor, object)
93
94   const followersException = [ byActor ]
95   return broadcastToFollowers(data, byActor, actorsInvolvedInVideo, t, followersException)
96 }
97
98 // ---------------------------------------------------------------------------
99
100 export {
101   sendUndoFollow,
102   sendUndoLike,
103   sendUndoDislike,
104   sendUndoAnnounce
105 }
106
107 // ---------------------------------------------------------------------------
108
109 function undoActivityData (
110   url: string,
111   byActor: ActorModel,
112   object: ActivityFollow | ActivityLike | ActivityCreate | ActivityAnnounce,
113   audience?: ActivityAudience
114 ): ActivityUndo {
115   if (!audience) audience = getAudience(byActor)
116
117   return audiencify(
118     {
119       type: 'Undo' as 'Undo',
120       id: url,
121       actor: byActor.url,
122       object
123     },
124     audience
125   )
126 }