2017-11-20 02:43:39 -06:00
|
|
|
import { Transaction } from 'sequelize'
|
2023-06-05 08:51:16 -05:00
|
|
|
import { ActivityAudience, ActivityDislike, ActivityLike, ActivityUndo, ActivityUndoObject, ContextType } from '@shared/models'
|
2018-07-30 10:02:40 -05:00
|
|
|
import { logger } from '../../../helpers/logger'
|
2020-11-20 04:21:08 -06:00
|
|
|
import { VideoModel } from '../../../models/video/video'
|
2019-08-15 04:53:26 -05:00
|
|
|
import {
|
2020-11-20 04:21:08 -06:00
|
|
|
MActor,
|
|
|
|
MActorAudience,
|
2019-08-15 04:53:26 -05:00
|
|
|
MActorFollowActors,
|
|
|
|
MActorLight,
|
|
|
|
MVideo,
|
|
|
|
MVideoAccountLight,
|
|
|
|
MVideoRedundancyVideo,
|
|
|
|
MVideoShare
|
2020-06-18 03:45:25 -05:00
|
|
|
} from '../../../types/models'
|
2020-11-20 04:21:08 -06:00
|
|
|
import { audiencify, getAudience } from '../audience'
|
|
|
|
import { getUndoActivityPubUrl, getVideoDislikeActivityPubUrlByLocalActor, getVideoLikeActivityPubUrlByLocalActor } from '../url'
|
|
|
|
import { buildAnnounceWithVideoAudience } from './send-announce'
|
|
|
|
import { buildCreateActivity } from './send-create'
|
|
|
|
import { buildDislikeActivity } from './send-dislike'
|
|
|
|
import { buildFollowActivity } from './send-follow'
|
|
|
|
import { buildLikeActivity } from './send-like'
|
2022-03-18 05:17:35 -05:00
|
|
|
import { broadcastToFollowers, sendVideoActivityToOrigin, sendVideoRelatedActivity, unicastTo } from './shared/send-utils'
|
2019-08-15 04:53:26 -05:00
|
|
|
|
2020-01-31 09:56:52 -06:00
|
|
|
function sendUndoFollow (actorFollow: MActorFollowActors, t: Transaction) {
|
2017-12-14 10:38:41 -06:00
|
|
|
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)
|
|
|
|
|
2020-11-20 04:21:08 -06:00
|
|
|
const undoUrl = getUndoActivityPubUrl(actorFollow.url)
|
2017-11-20 02:43:39 -06:00
|
|
|
|
2020-11-20 04:21:08 -06:00
|
|
|
const followActivity = buildFollowActivity(actorFollow.url, me, following)
|
2018-09-11 09:27:07 -05:00
|
|
|
const undoActivity = undoActivityData(undoUrl, me, followActivity)
|
2017-11-20 02:43:39 -06:00
|
|
|
|
2022-05-05 03:29:35 -05:00
|
|
|
t.afterCommit(() => {
|
2022-03-23 10:14:33 -05:00
|
|
|
return unicastTo({
|
|
|
|
data: undoActivity,
|
|
|
|
byActor: me,
|
|
|
|
toActorUrl: following.inboxUrl,
|
|
|
|
contextType: 'Follow'
|
|
|
|
})
|
|
|
|
})
|
2017-11-20 02:43:39 -06:00
|
|
|
}
|
|
|
|
|
2022-03-18 05:17:35 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2022-03-23 10:14:33 -05:00
|
|
|
async function sendUndoAnnounce (byActor: MActorLight, videoShare: MVideoShare, video: MVideo, transaction: Transaction) {
|
2018-09-14 09:51:35 -05:00
|
|
|
logger.info('Creating job to undo announce %s.', videoShare.url)
|
2018-07-30 10:02:40 -05:00
|
|
|
|
2018-09-14 09:51:35 -05:00
|
|
|
const undoUrl = getUndoActivityPubUrl(videoShare.url)
|
2017-11-23 07:19:55 -06:00
|
|
|
|
2022-03-23 10:14:33 -05:00
|
|
|
const { activity: announce, actorsInvolvedInVideo } = await buildAnnounceWithVideoAudience(byActor, videoShare, video, transaction)
|
|
|
|
const undoActivity = undoActivityData(undoUrl, byActor, announce)
|
|
|
|
|
|
|
|
return broadcastToFollowers({
|
|
|
|
data: undoActivity,
|
|
|
|
byActor,
|
|
|
|
toFollowersOf: actorsInvolvedInVideo,
|
|
|
|
transaction,
|
|
|
|
actorsException: [ byActor ],
|
|
|
|
contextType: 'Announce'
|
|
|
|
})
|
2018-09-14 09:51:35 -05:00
|
|
|
}
|
2017-11-23 07:19:55 -06:00
|
|
|
|
2022-03-23 10:14:33 -05:00
|
|
|
async function sendUndoCacheFile (byActor: MActor, redundancyModel: MVideoRedundancyVideo, transaction: Transaction) {
|
2022-03-18 05:17:35 -05:00
|
|
|
logger.info('Creating job to undo cache file %s.', redundancyModel.url)
|
|
|
|
|
|
|
|
const associatedVideo = redundancyModel.getVideo()
|
|
|
|
if (!associatedVideo) {
|
|
|
|
logger.warn('Cannot send undo activity for redundancy %s: no video files associated.', redundancyModel.url)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-28 07:57:51 -05:00
|
|
|
const video = await VideoModel.loadFull(associatedVideo.id)
|
2022-03-18 05:17:35 -05:00
|
|
|
const createActivity = buildCreateActivity(redundancyModel.url, byActor, redundancyModel.toActivityPubObject())
|
|
|
|
|
2022-03-23 10:14:33 -05:00
|
|
|
return sendUndoVideoRelatedActivity({
|
|
|
|
byActor,
|
|
|
|
video,
|
|
|
|
url: redundancyModel.url,
|
|
|
|
activity: createActivity,
|
|
|
|
contextType: 'CacheFile',
|
|
|
|
transaction
|
|
|
|
})
|
2022-03-18 05:17:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2019-08-15 04:53:26 -05:00
|
|
|
async function sendUndoLike (byActor: MActor, video: MVideoAccountLight, t: Transaction) {
|
2018-09-14 09:51:35 -05:00
|
|
|
logger.info('Creating job to undo a like of video %s.', video.url)
|
2017-11-23 07:19:55 -06:00
|
|
|
|
2020-11-20 04:21:08 -06:00
|
|
|
const likeUrl = getVideoLikeActivityPubUrlByLocalActor(byActor, video)
|
2018-09-14 09:51:35 -05:00
|
|
|
const likeActivity = buildLikeActivity(likeUrl, byActor, video)
|
2017-11-23 07:19:55 -06:00
|
|
|
|
2022-03-23 10:14:33 -05:00
|
|
|
return sendUndoVideoRateToOriginActivity({ byActor, video, url: likeUrl, activity: likeActivity, transaction: t })
|
2017-11-23 07:19:55 -06:00
|
|
|
}
|
|
|
|
|
2019-08-15 04:53:26 -05:00
|
|
|
async function sendUndoDislike (byActor: MActor, video: MVideoAccountLight, t: Transaction) {
|
2018-07-30 10:02:40 -05:00
|
|
|
logger.info('Creating job to undo a dislike of video %s.', video.url)
|
|
|
|
|
2020-11-20 04:21:08 -06:00
|
|
|
const dislikeUrl = getVideoDislikeActivityPubUrlByLocalActor(byActor, video)
|
2018-11-14 08:01:28 -06:00
|
|
|
const dislikeActivity = buildDislikeActivity(dislikeUrl, byActor, video)
|
2017-11-23 07:19:55 -06:00
|
|
|
|
2022-03-23 10:14:33 -05:00
|
|
|
return sendUndoVideoRateToOriginActivity({ byActor, video, url: dislikeUrl, activity: dislikeActivity, transaction: t })
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2023-06-05 08:51:16 -05:00
|
|
|
function undoActivityData <T extends ActivityUndoObject> (
|
2017-11-24 06:41:10 -06:00
|
|
|
url: string,
|
2019-08-15 04:53:26 -05:00
|
|
|
byActor: MActorAudience,
|
2023-06-05 08:51:16 -05:00
|
|
|
object: T,
|
2017-11-24 06:41:10 -06:00
|
|
|
audience?: ActivityAudience
|
2023-06-05 08:51:16 -05:00
|
|
|
): ActivityUndo<T> {
|
2018-06-12 13:04:58 -05:00
|
|
|
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
|
|
|
}
|
2018-09-14 09:51:35 -05:00
|
|
|
|
|
|
|
async function sendUndoVideoRelatedActivity (options: {
|
2020-01-31 09:56:52 -06:00
|
|
|
byActor: MActor
|
|
|
|
video: MVideoAccountLight
|
|
|
|
url: string
|
2023-06-05 08:51:16 -05:00
|
|
|
activity: ActivityUndoObject
|
2022-03-23 10:14:33 -05:00
|
|
|
contextType: ContextType
|
2018-09-14 09:51:35 -05:00
|
|
|
transaction: Transaction
|
|
|
|
}) {
|
|
|
|
const activityBuilder = (audience: ActivityAudience) => {
|
|
|
|
const undoUrl = getUndoActivityPubUrl(options.url)
|
|
|
|
|
|
|
|
return undoActivityData(undoUrl, options.byActor, options.activity, audience)
|
|
|
|
}
|
|
|
|
|
|
|
|
return sendVideoRelatedActivity(activityBuilder, options)
|
|
|
|
}
|
2022-03-18 05:17:35 -05:00
|
|
|
|
2022-03-23 10:14:33 -05:00
|
|
|
async function sendUndoVideoRateToOriginActivity (options: {
|
2022-03-18 05:17:35 -05:00
|
|
|
byActor: MActor
|
|
|
|
video: MVideoAccountLight
|
|
|
|
url: string
|
|
|
|
activity: ActivityLike | ActivityDislike
|
|
|
|
transaction: Transaction
|
|
|
|
}) {
|
|
|
|
const activityBuilder = (audience: ActivityAudience) => {
|
|
|
|
const undoUrl = getUndoActivityPubUrl(options.url)
|
|
|
|
|
|
|
|
return undoActivityData(undoUrl, options.byActor, options.activity, audience)
|
|
|
|
}
|
|
|
|
|
2022-03-23 10:14:33 -05:00
|
|
|
return sendVideoActivityToOrigin(activityBuilder, { ...options, contextType: 'Rate' })
|
2022-03-18 05:17:35 -05:00
|
|
|
}
|