1 import { Transaction } from 'sequelize'
11 } from '@shared/models'
12 import { logger } from '../../../helpers/logger'
13 import { VideoModel } from '../../../models/video/video'
21 MVideoRedundancyVideo,
23 } from '../../../types/models'
24 import { audiencify, getAudience } from '../audience'
25 import { getUndoActivityPubUrl, getVideoDislikeActivityPubUrlByLocalActor, getVideoLikeActivityPubUrlByLocalActor } from '../url'
26 import { buildAnnounceWithVideoAudience } from './send-announce'
27 import { buildCreateActivity } from './send-create'
28 import { buildDislikeActivity } from './send-dislike'
29 import { buildFollowActivity } from './send-follow'
30 import { buildLikeActivity } from './send-like'
31 import { broadcastToFollowers, sendVideoActivityToOrigin, sendVideoRelatedActivity, unicastTo } from './shared/send-utils'
33 function sendUndoFollow (actorFollow: MActorFollowActors, t: Transaction) {
34 const me = actorFollow.ActorFollower
35 const following = actorFollow.ActorFollowing
37 // Same server as ours
38 if (!following.serverId) return
40 logger.info('Creating job to send an unfollow request to %s.', following.url)
42 const undoUrl = getUndoActivityPubUrl(actorFollow.url)
44 const followActivity = buildFollowActivity(actorFollow.url, me, following)
45 const undoActivity = undoActivityData(undoUrl, me, followActivity)
51 toActorUrl: following.inboxUrl,
57 // ---------------------------------------------------------------------------
59 async function sendUndoAnnounce (byActor: MActorLight, videoShare: MVideoShare, video: MVideo, transaction: Transaction) {
60 logger.info('Creating job to undo announce %s.', videoShare.url)
62 const undoUrl = getUndoActivityPubUrl(videoShare.url)
64 const { activity: announce, actorsInvolvedInVideo } = await buildAnnounceWithVideoAudience(byActor, videoShare, video, transaction)
65 const undoActivity = undoActivityData(undoUrl, byActor, announce)
67 return broadcastToFollowers({
70 toFollowersOf: actorsInvolvedInVideo,
72 actorsException: [ byActor ],
73 contextType: 'Announce'
77 async function sendUndoCacheFile (byActor: MActor, redundancyModel: MVideoRedundancyVideo, transaction: Transaction) {
78 logger.info('Creating job to undo cache file %s.', redundancyModel.url)
80 const associatedVideo = redundancyModel.getVideo()
81 if (!associatedVideo) {
82 logger.warn('Cannot send undo activity for redundancy %s: no video files associated.', redundancyModel.url)
86 const video = await VideoModel.loadFull(associatedVideo.id)
87 const createActivity = buildCreateActivity(redundancyModel.url, byActor, redundancyModel.toActivityPubObject())
89 return sendUndoVideoRelatedActivity({
92 url: redundancyModel.url,
93 activity: createActivity,
94 contextType: 'CacheFile',
99 // ---------------------------------------------------------------------------
101 async function sendUndoLike (byActor: MActor, video: MVideoAccountLight, t: Transaction) {
102 logger.info('Creating job to undo a like of video %s.', video.url)
104 const likeUrl = getVideoLikeActivityPubUrlByLocalActor(byActor, video)
105 const likeActivity = buildLikeActivity(likeUrl, byActor, video)
107 return sendUndoVideoRateToOriginActivity({ byActor, video, url: likeUrl, activity: likeActivity, transaction: t })
110 async function sendUndoDislike (byActor: MActor, video: MVideoAccountLight, t: Transaction) {
111 logger.info('Creating job to undo a dislike of video %s.', video.url)
113 const dislikeUrl = getVideoDislikeActivityPubUrlByLocalActor(byActor, video)
114 const dislikeActivity = buildDislikeActivity(dislikeUrl, byActor, video)
116 return sendUndoVideoRateToOriginActivity({ byActor, video, url: dislikeUrl, activity: dislikeActivity, transaction: t })
119 // ---------------------------------------------------------------------------
129 // ---------------------------------------------------------------------------
131 function undoActivityData (
133 byActor: MActorAudience,
134 object: ActivityFollow | ActivityLike | ActivityDislike | ActivityCreate | ActivityAnnounce,
135 audience?: ActivityAudience
137 if (!audience) audience = getAudience(byActor)
141 type: 'Undo' as 'Undo',
150 async function sendUndoVideoRelatedActivity (options: {
152 video: MVideoAccountLight
154 activity: ActivityFollow | ActivityCreate | ActivityAnnounce
155 contextType: ContextType
156 transaction: Transaction
158 const activityBuilder = (audience: ActivityAudience) => {
159 const undoUrl = getUndoActivityPubUrl(options.url)
161 return undoActivityData(undoUrl, options.byActor, options.activity, audience)
164 return sendVideoRelatedActivity(activityBuilder, options)
167 async function sendUndoVideoRateToOriginActivity (options: {
169 video: MVideoAccountLight
171 activity: ActivityLike | ActivityDislike
172 transaction: Transaction
174 const activityBuilder = (audience: ActivityAudience) => {
175 const undoUrl = getUndoActivityPubUrl(options.url)
177 return undoActivityData(undoUrl, options.byActor, options.activity, audience)
180 return sendVideoActivityToOrigin(activityBuilder, { ...options, contextType: 'Rate' })