2017-11-20 08:43:39 +00:00
|
|
|
import { Transaction } from 'sequelize'
|
2017-12-12 16:53:50 +00:00
|
|
|
import { Activity } from '../../../../shared/models/activitypub'
|
|
|
|
import { logger } from '../../../helpers'
|
|
|
|
import { ACTIVITY_PUB } from '../../../initializers'
|
2017-12-14 16:38:41 +00:00
|
|
|
import { ActorModel } from '../../../models/activitypub/actor'
|
|
|
|
import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
|
2017-12-12 16:53:50 +00:00
|
|
|
import { VideoModel } from '../../../models/video/video'
|
|
|
|
import { VideoShareModel } from '../../../models/video/video-share'
|
|
|
|
import { activitypubHttpJobScheduler, ActivityPubHttpPayload } from '../../jobs/activitypub-http-job-scheduler'
|
2017-11-24 12:41:10 +00:00
|
|
|
|
|
|
|
async function forwardActivity (
|
|
|
|
activity: Activity,
|
|
|
|
t: Transaction,
|
2017-12-14 16:38:41 +00:00
|
|
|
followersException: ActorModel[] = []
|
2017-11-24 12:41:10 +00:00
|
|
|
) {
|
|
|
|
const to = activity.to || []
|
|
|
|
const cc = activity.cc || []
|
|
|
|
|
|
|
|
const followersUrls: string[] = []
|
|
|
|
for (const dest of to.concat(cc)) {
|
|
|
|
if (dest.endsWith('/followers')) {
|
|
|
|
followersUrls.push(dest)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 16:38:41 +00:00
|
|
|
const toActorFollowers = await ActorModel.listByFollowersUrls(followersUrls, t)
|
|
|
|
const uris = await computeFollowerUris(toActorFollowers, followersException, t)
|
2017-11-24 12:41:10 +00:00
|
|
|
|
|
|
|
if (uris.length === 0) {
|
2017-12-14 16:38:41 +00:00
|
|
|
logger.info('0 followers for %s, no forwarding.', toActorFollowers.map(a => a.id).join(', '))
|
2017-11-24 14:00:10 +00:00
|
|
|
return undefined
|
2017-11-24 12:41:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
logger.debug('Creating forwarding job.', { uris })
|
|
|
|
|
|
|
|
const jobPayload: ActivityPubHttpPayload = {
|
|
|
|
uris,
|
|
|
|
body: activity
|
|
|
|
}
|
|
|
|
|
|
|
|
return activitypubHttpJobScheduler.createJob(t, 'activitypubHttpBroadcastHandler', jobPayload)
|
|
|
|
}
|
2017-11-20 08:43:39 +00:00
|
|
|
|
2017-11-22 15:25:03 +00:00
|
|
|
async function broadcastToFollowers (
|
|
|
|
data: any,
|
2017-12-14 16:38:41 +00:00
|
|
|
byActor: ActorModel,
|
|
|
|
toActorFollowers: ActorModel[],
|
2017-11-22 15:25:03 +00:00
|
|
|
t: Transaction,
|
2017-12-14 16:38:41 +00:00
|
|
|
followersException: ActorModel[] = []
|
2017-11-22 15:25:03 +00:00
|
|
|
) {
|
2017-12-14 16:38:41 +00:00
|
|
|
const uris = await computeFollowerUris(toActorFollowers, followersException, t)
|
2017-11-24 12:41:10 +00:00
|
|
|
if (uris.length === 0) {
|
2017-12-14 16:38:41 +00:00
|
|
|
logger.info('0 followers for %s, no broadcasting.', toActorFollowers.map(a => a.id).join(', '))
|
2017-11-24 14:00:10 +00:00
|
|
|
return undefined
|
2017-11-20 08:43:39 +00:00
|
|
|
}
|
|
|
|
|
2017-11-24 12:41:10 +00:00
|
|
|
logger.debug('Creating broadcast job.', { uris })
|
2017-11-22 15:25:03 +00:00
|
|
|
|
2017-11-24 12:41:10 +00:00
|
|
|
const jobPayload: ActivityPubHttpPayload = {
|
2017-11-22 15:25:03 +00:00
|
|
|
uris,
|
2017-12-14 16:38:41 +00:00
|
|
|
signatureActorId: byActor.id,
|
2017-11-20 08:43:39 +00:00
|
|
|
body: data
|
|
|
|
}
|
|
|
|
|
|
|
|
return activitypubHttpJobScheduler.createJob(t, 'activitypubHttpBroadcastHandler', jobPayload)
|
|
|
|
}
|
|
|
|
|
2017-12-14 16:38:41 +00:00
|
|
|
async function unicastTo (data: any, byActor: ActorModel, toActorUrl: string, t: Transaction) {
|
|
|
|
logger.debug('Creating unicast job.', { uri: toActorUrl })
|
2017-11-24 12:41:10 +00:00
|
|
|
|
|
|
|
const jobPayload: ActivityPubHttpPayload = {
|
2017-12-14 16:38:41 +00:00
|
|
|
uris: [ toActorUrl ],
|
|
|
|
signatureActorId: byActor.id,
|
2017-11-20 08:43:39 +00:00
|
|
|
body: data
|
|
|
|
}
|
|
|
|
|
|
|
|
return activitypubHttpJobScheduler.createJob(t, 'activitypubHttpUnicastHandler', jobPayload)
|
|
|
|
}
|
|
|
|
|
2017-12-14 16:38:41 +00:00
|
|
|
function getOriginVideoAudience (video: VideoModel, actorsInvolvedInVideo: ActorModel[]) {
|
2017-11-23 13:19:55 +00:00
|
|
|
return {
|
2017-12-14 16:38:41 +00:00
|
|
|
to: [ video.VideoChannel.Account.Actor.url ],
|
|
|
|
cc: actorsInvolvedInVideo.map(a => a.followersUrl)
|
2017-11-23 13:19:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 16:38:41 +00:00
|
|
|
function getObjectFollowersAudience (actorsInvolvedInObject: ActorModel[]) {
|
2017-11-27 13:44:51 +00:00
|
|
|
return {
|
2017-12-14 16:38:41 +00:00
|
|
|
to: actorsInvolvedInObject.map(a => a.followersUrl),
|
2017-11-23 13:19:55 +00:00
|
|
|
cc: []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 16:38:41 +00:00
|
|
|
async function getActorsInvolvedInVideo (video: VideoModel, t: Transaction) {
|
|
|
|
const actorsToForwardView = await VideoShareModel.loadActorsByShare(video.id, t)
|
|
|
|
actorsToForwardView.push(video.VideoChannel.Account.Actor)
|
2017-11-27 13:44:51 +00:00
|
|
|
|
2017-12-14 16:38:41 +00:00
|
|
|
return actorsToForwardView
|
2017-11-27 13:44:51 +00:00
|
|
|
}
|
|
|
|
|
2017-12-14 16:38:41 +00:00
|
|
|
async function getAudience (actorSender: ActorModel, t: Transaction, isPublic = true) {
|
|
|
|
const followerInboxUrls = await actorSender.getFollowerSharedInboxUrls(t)
|
2017-11-20 08:43:39 +00:00
|
|
|
|
|
|
|
// Thanks Mastodon: https://github.com/tootsuite/mastodon/blob/master/app/lib/activitypub/tag_manager.rb#L47
|
|
|
|
let to = []
|
|
|
|
let cc = []
|
|
|
|
|
|
|
|
if (isPublic) {
|
|
|
|
to = [ ACTIVITY_PUB.PUBLIC ]
|
|
|
|
cc = followerInboxUrls
|
|
|
|
} else { // Unlisted
|
|
|
|
to = followerInboxUrls
|
|
|
|
cc = [ ACTIVITY_PUB.PUBLIC ]
|
|
|
|
}
|
|
|
|
|
|
|
|
return { to, cc }
|
|
|
|
}
|
|
|
|
|
2017-12-14 16:38:41 +00:00
|
|
|
async function computeFollowerUris (toActorFollower: ActorModel[], followersException: ActorModel[], t: Transaction) {
|
|
|
|
const toActorFollowerIds = toActorFollower.map(a => a.id)
|
2017-11-24 12:41:10 +00:00
|
|
|
|
2017-12-14 16:38:41 +00:00
|
|
|
const result = await ActorFollowModel.listAcceptedFollowerSharedInboxUrls(toActorFollowerIds, t)
|
2017-11-24 12:41:10 +00:00
|
|
|
const followersSharedInboxException = followersException.map(f => f.sharedInboxUrl)
|
2017-12-12 16:53:50 +00:00
|
|
|
return result.data.filter(sharedInbox => followersSharedInboxException.indexOf(sharedInbox) === -1)
|
2017-11-24 12:41:10 +00:00
|
|
|
}
|
|
|
|
|
2017-11-20 08:43:39 +00:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
broadcastToFollowers,
|
|
|
|
unicastTo,
|
2017-11-23 13:19:55 +00:00
|
|
|
getAudience,
|
|
|
|
getOriginVideoAudience,
|
2017-12-14 16:38:41 +00:00
|
|
|
getActorsInvolvedInVideo,
|
2017-11-27 13:44:51 +00:00
|
|
|
getObjectFollowersAudience,
|
2017-11-24 12:41:10 +00:00
|
|
|
forwardActivity
|
2017-11-20 08:43:39 +00:00
|
|
|
}
|