2017-11-20 02:43:39 -06:00
|
|
|
import { Transaction } from 'sequelize'
|
2018-05-11 08:10:13 -05:00
|
|
|
import {
|
|
|
|
ActivityAnnounce,
|
|
|
|
ActivityAudience,
|
|
|
|
ActivityCreate,
|
|
|
|
ActivityFollow,
|
|
|
|
ActivityLike,
|
|
|
|
ActivityUndo
|
|
|
|
} from '../../../../shared/models/activitypub'
|
2017-12-14 10:38:41 -06:00
|
|
|
import { ActorModel } from '../../../models/activitypub/actor'
|
|
|
|
import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
|
2017-12-12 10:53:50 -06:00
|
|
|
import { VideoModel } from '../../../models/video/video'
|
2017-12-14 10:38:41 -06:00
|
|
|
import { getActorFollowActivityPubUrl, getUndoActivityPubUrl, getVideoDislikeActivityPubUrl, getVideoLikeActivityPubUrl } from '../url'
|
2018-05-25 04:32:36 -05:00
|
|
|
import { broadcastToFollowers, unicastTo } from './utils'
|
|
|
|
import { audiencify, getActorsInvolvedInVideo, getAudience, getObjectFollowersAudience, getVideoAudience } from '../audience'
|
2018-09-11 09:27:07 -05:00
|
|
|
import { buildCreateActivity, buildDislikeActivity } from './send-create'
|
|
|
|
import { buildFollowActivity } from './send-follow'
|
|
|
|
import { buildLikeActivity } from './send-like'
|
2018-05-11 08:10:13 -05:00
|
|
|
import { VideoShareModel } from '../../../models/video/video-share'
|
2018-09-11 09:27:07 -05:00
|
|
|
import { buildAnnounceWithVideoAudience } from './send-announce'
|
2018-07-30 10:02:40 -05:00
|
|
|
import { logger } from '../../../helpers/logger'
|
2018-09-11 09:27:07 -05:00
|
|
|
import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy'
|
2017-11-20 02:43:39 -06:00
|
|
|
|
2017-12-14 10:38:41 -06:00
|
|
|
async function sendUndoFollow (actorFollow: ActorFollowModel, t: Transaction) {
|
|
|
|
const me = actorFollow.ActorFollower
|
|
|
|
const following = actorFollow.ActorFollowing
|
2017-11-20 02:43:39 -06:00
|
|
|
|
2018-08-16 08:25:20 -05:00
|
|
|
// Same server as ours
|
|
|
|
if (!following.serverId) return
|
|
|
|
|
2018-07-30 10:02:40 -05:00
|
|
|
logger.info('Creating job to send an unfollow request to %s.', following.url)
|
|
|
|
|
2017-12-14 10:38:41 -06:00
|
|
|
const followUrl = getActorFollowActivityPubUrl(actorFollow)
|
2017-11-20 02:43:39 -06:00
|
|
|
const undoUrl = getUndoActivityPubUrl(followUrl)
|
|
|
|
|
2018-09-11 09:27:07 -05:00
|
|
|
const followActivity = buildFollowActivity(followUrl, me, following)
|
|
|
|
const undoActivity = undoActivityData(undoUrl, me, followActivity)
|
2017-11-20 02:43:39 -06:00
|
|
|
|
2018-09-11 09:27:07 -05:00
|
|
|
return unicastTo(undoActivity, me, following.inboxUrl)
|
2017-11-20 02:43:39 -06:00
|
|
|
}
|
|
|
|
|
2018-03-27 06:33:56 -05:00
|
|
|
async function sendUndoLike (byActor: ActorModel, video: VideoModel, t: Transaction) {
|
2018-07-30 10:02:40 -05:00
|
|
|
logger.info('Creating job to undo a like of video %s.', video.url)
|
|
|
|
|
2017-12-14 10:38:41 -06:00
|
|
|
const likeUrl = getVideoLikeActivityPubUrl(byActor, video)
|
2017-11-23 07:19:55 -06:00
|
|
|
const undoUrl = getUndoActivityPubUrl(likeUrl)
|
|
|
|
|
2017-12-14 10:38:41 -06:00
|
|
|
const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
|
2018-09-11 09:27:07 -05:00
|
|
|
const likeActivity = buildLikeActivity(likeUrl, byActor, video)
|
2017-11-23 07:19:55 -06:00
|
|
|
|
2018-03-27 06:33:56 -05:00
|
|
|
// Send to origin
|
|
|
|
if (video.isOwned() === false) {
|
2018-05-25 04:32:36 -05:00
|
|
|
const audience = getVideoAudience(video, actorsInvolvedInVideo)
|
2018-09-11 09:27:07 -05:00
|
|
|
const undoActivity = undoActivityData(undoUrl, byActor, likeActivity, audience)
|
2017-11-23 07:19:55 -06:00
|
|
|
|
2018-09-11 09:27:07 -05:00
|
|
|
return unicastTo(undoActivity, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
|
2018-03-27 06:33:56 -05:00
|
|
|
}
|
2017-11-23 07:19:55 -06:00
|
|
|
|
2018-03-27 06:33:56 -05:00
|
|
|
const audience = getObjectFollowersAudience(actorsInvolvedInVideo)
|
2018-09-11 09:27:07 -05:00
|
|
|
const undoActivity = undoActivityData(undoUrl, byActor, likeActivity, audience)
|
2017-11-23 07:19:55 -06:00
|
|
|
|
2017-12-14 10:38:41 -06:00
|
|
|
const followersException = [ byActor ]
|
2018-09-11 09:27:07 -05:00
|
|
|
return broadcastToFollowers(undoActivity, byActor, actorsInvolvedInVideo, t, followersException)
|
2017-11-23 07:19:55 -06:00
|
|
|
}
|
|
|
|
|
2018-03-27 06:33:56 -05:00
|
|
|
async function sendUndoDislike (byActor: ActorModel, video: VideoModel, t: Transaction) {
|
2018-07-30 10:02:40 -05:00
|
|
|
logger.info('Creating job to undo a dislike of video %s.', video.url)
|
|
|
|
|
2017-12-14 10:38:41 -06:00
|
|
|
const dislikeUrl = getVideoDislikeActivityPubUrl(byActor, video)
|
2017-11-23 07:19:55 -06:00
|
|
|
const undoUrl = getUndoActivityPubUrl(dislikeUrl)
|
|
|
|
|
2017-12-14 10:38:41 -06:00
|
|
|
const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
|
2018-09-11 09:27:07 -05:00
|
|
|
const dislikeActivity = buildDislikeActivity(byActor, video)
|
|
|
|
const createDislikeActivity = buildCreateActivity(dislikeUrl, byActor, dislikeActivity)
|
2017-11-23 07:19:55 -06:00
|
|
|
|
2018-03-27 06:33:56 -05:00
|
|
|
if (video.isOwned() === false) {
|
2018-05-25 04:32:36 -05:00
|
|
|
const audience = getVideoAudience(video, actorsInvolvedInVideo)
|
2018-09-11 09:27:07 -05:00
|
|
|
const undoActivity = undoActivityData(undoUrl, byActor, createDislikeActivity, audience)
|
2017-11-23 07:19:55 -06:00
|
|
|
|
2018-09-11 09:27:07 -05:00
|
|
|
return unicastTo(undoActivity, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
|
2018-03-27 06:33:56 -05:00
|
|
|
}
|
2017-11-23 07:19:55 -06:00
|
|
|
|
2018-09-11 09:27:07 -05:00
|
|
|
const undoActivity = undoActivityData(undoUrl, byActor, createDislikeActivity)
|
2017-11-23 07:19:55 -06:00
|
|
|
|
2017-12-14 10:38:41 -06:00
|
|
|
const followersException = [ byActor ]
|
2018-09-11 09:27:07 -05:00
|
|
|
return broadcastToFollowers(undoActivity, byActor, actorsInvolvedInVideo, t, followersException)
|
2017-11-23 07:19:55 -06:00
|
|
|
}
|
|
|
|
|
2018-05-11 08:10:13 -05:00
|
|
|
async function sendUndoAnnounce (byActor: ActorModel, videoShare: VideoShareModel, video: VideoModel, t: Transaction) {
|
2018-07-30 10:02:40 -05:00
|
|
|
logger.info('Creating job to undo announce %s.', videoShare.url)
|
|
|
|
|
2018-05-11 08:10:13 -05:00
|
|
|
const undoUrl = getUndoActivityPubUrl(videoShare.url)
|
|
|
|
|
2018-09-11 09:27:07 -05:00
|
|
|
const { activity: announceActivity, actorsInvolvedInVideo } = await buildAnnounceWithVideoAudience(byActor, videoShare, video, t)
|
|
|
|
const undoActivity = undoActivityData(undoUrl, byActor, announceActivity)
|
2018-05-11 08:10:13 -05:00
|
|
|
|
|
|
|
const followersException = [ byActor ]
|
2018-09-11 09:27:07 -05:00
|
|
|
return broadcastToFollowers(undoActivity, byActor, actorsInvolvedInVideo, t, followersException)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function sendUndoCacheFile (byActor: ActorModel, redundancyModel: VideoRedundancyModel, t: Transaction) {
|
|
|
|
logger.info('Creating job to undo cache file %s.', redundancyModel.url)
|
|
|
|
|
|
|
|
const undoUrl = getUndoActivityPubUrl(redundancyModel.url)
|
|
|
|
|
|
|
|
const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(redundancyModel.VideoFile.Video.id)
|
|
|
|
const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
|
|
|
|
|
|
|
|
const audience = getVideoAudience(video, actorsInvolvedInVideo)
|
|
|
|
const createActivity = buildCreateActivity(redundancyModel.url, byActor, redundancyModel.toActivityPubObject())
|
|
|
|
|
|
|
|
const undoActivity = undoActivityData(undoUrl, byActor, createActivity, audience)
|
|
|
|
|
|
|
|
return unicastTo(undoActivity, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
|
2018-05-11 08:10:13 -05:00
|
|
|
}
|
|
|
|
|
2017-11-20 02:43:39 -06:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
2017-11-23 07:19:55 -06:00
|
|
|
sendUndoFollow,
|
2018-03-27 06:33:56 -05:00
|
|
|
sendUndoLike,
|
2018-05-11 08:10:13 -05:00
|
|
|
sendUndoDislike,
|
2018-09-11 09:27:07 -05:00
|
|
|
sendUndoAnnounce,
|
|
|
|
sendUndoCacheFile
|
2017-11-20 02:43:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2018-06-12 13:04:58 -05:00
|
|
|
function undoActivityData (
|
2017-11-24 06:41:10 -06:00
|
|
|
url: string,
|
2017-12-14 10:38:41 -06:00
|
|
|
byActor: ActorModel,
|
2018-05-11 08:10:13 -05:00
|
|
|
object: ActivityFollow | ActivityLike | ActivityCreate | ActivityAnnounce,
|
2017-11-24 06:41:10 -06:00
|
|
|
audience?: ActivityAudience
|
2018-06-12 13:04:58 -05:00
|
|
|
): ActivityUndo {
|
|
|
|
if (!audience) audience = getAudience(byActor)
|
|
|
|
|
|
|
|
return audiencify(
|
|
|
|
{
|
|
|
|
type: 'Undo' as 'Undo',
|
|
|
|
id: url,
|
|
|
|
actor: byActor.url,
|
|
|
|
object
|
|
|
|
},
|
|
|
|
audience
|
|
|
|
)
|
2017-11-20 02:43:39 -06:00
|
|
|
}
|