1 import { Transaction } from 'sequelize'
10 } from '@shared/models'
11 import { logger } from '../../../helpers/logger'
12 import { VideoModel } from '../../../models/video/video'
20 MVideoRedundancyVideo,
22 } from '../../../types/models'
23 import { audiencify, getAudience } from '../audience'
24 import { getUndoActivityPubUrl, getVideoDislikeActivityPubUrlByLocalActor, getVideoLikeActivityPubUrlByLocalActor } from '../url'
25 import { buildAnnounceWithVideoAudience } from './send-announce'
26 import { buildCreateActivity } from './send-create'
27 import { buildDislikeActivity } from './send-dislike'
28 import { buildFollowActivity } from './send-follow'
29 import { buildLikeActivity } from './send-like'
30 import { broadcastToFollowers, sendVideoActivityToOrigin, sendVideoRelatedActivity, unicastTo } from './shared/send-utils'
32 function sendUndoFollow (actorFollow: MActorFollowActors, t: Transaction) {
33 const me = actorFollow.ActorFollower
34 const following = actorFollow.ActorFollowing
36 // Same server as ours
37 if (!following.serverId) return
39 logger.info('Creating job to send an unfollow request to %s.', following.url)
41 const undoUrl = getUndoActivityPubUrl(actorFollow.url)
43 const followActivity = buildFollowActivity(actorFollow.url, me, following)
44 const undoActivity = undoActivityData(undoUrl, me, followActivity)
46 t.afterCommit(() => unicastTo(undoActivity, me, following.inboxUrl))
49 // ---------------------------------------------------------------------------
51 async function sendUndoAnnounce (byActor: MActorLight, videoShare: MVideoShare, video: MVideo, t: Transaction) {
52 logger.info('Creating job to undo announce %s.', videoShare.url)
54 const undoUrl = getUndoActivityPubUrl(videoShare.url)
56 const { activity: announceActivity, actorsInvolvedInVideo } = await buildAnnounceWithVideoAudience(byActor, videoShare, video, t)
57 const undoActivity = undoActivityData(undoUrl, byActor, announceActivity)
59 const followersException = [ byActor ]
60 return broadcastToFollowers(undoActivity, byActor, actorsInvolvedInVideo, t, followersException)
63 async function sendUndoCacheFile (byActor: MActor, redundancyModel: MVideoRedundancyVideo, t: Transaction) {
64 logger.info('Creating job to undo cache file %s.', redundancyModel.url)
66 const associatedVideo = redundancyModel.getVideo()
67 if (!associatedVideo) {
68 logger.warn('Cannot send undo activity for redundancy %s: no video files associated.', redundancyModel.url)
72 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(associatedVideo.id)
73 const createActivity = buildCreateActivity(redundancyModel.url, byActor, redundancyModel.toActivityPubObject())
75 return sendUndoVideoRelatedActivity({ byActor, video, url: redundancyModel.url, activity: createActivity, transaction: t })
78 // ---------------------------------------------------------------------------
80 async function sendUndoLike (byActor: MActor, video: MVideoAccountLight, t: Transaction) {
81 logger.info('Creating job to undo a like of video %s.', video.url)
83 const likeUrl = getVideoLikeActivityPubUrlByLocalActor(byActor, video)
84 const likeActivity = buildLikeActivity(likeUrl, byActor, video)
86 return sendUndoVideoToOriginActivity({ byActor, video, url: likeUrl, activity: likeActivity, transaction: t })
89 async function sendUndoDislike (byActor: MActor, video: MVideoAccountLight, t: Transaction) {
90 logger.info('Creating job to undo a dislike of video %s.', video.url)
92 const dislikeUrl = getVideoDislikeActivityPubUrlByLocalActor(byActor, video)
93 const dislikeActivity = buildDislikeActivity(dislikeUrl, byActor, video)
95 return sendUndoVideoToOriginActivity({ byActor, video, url: dislikeUrl, activity: dislikeActivity, transaction: t })
98 // ---------------------------------------------------------------------------
108 // ---------------------------------------------------------------------------
110 function undoActivityData (
112 byActor: MActorAudience,
113 object: ActivityFollow | ActivityLike | ActivityDislike | ActivityCreate | ActivityAnnounce,
114 audience?: ActivityAudience
116 if (!audience) audience = getAudience(byActor)
120 type: 'Undo' as 'Undo',
129 async function sendUndoVideoRelatedActivity (options: {
131 video: MVideoAccountLight
133 activity: ActivityFollow | ActivityCreate | ActivityAnnounce
134 transaction: Transaction
136 const activityBuilder = (audience: ActivityAudience) => {
137 const undoUrl = getUndoActivityPubUrl(options.url)
139 return undoActivityData(undoUrl, options.byActor, options.activity, audience)
142 return sendVideoRelatedActivity(activityBuilder, options)
145 async function sendUndoVideoToOriginActivity (options: {
147 video: MVideoAccountLight
149 activity: ActivityLike | ActivityDislike
150 transaction: Transaction
152 const activityBuilder = (audience: ActivityAudience) => {
153 const undoUrl = getUndoActivityPubUrl(options.url)
155 return undoActivityData(undoUrl, options.byActor, options.activity, audience)
158 return sendVideoActivityToOrigin(activityBuilder, options)