2017-11-20 03:43:39 -05:00
|
|
|
import { Transaction } from 'sequelize'
|
2017-12-19 04:34:56 -05:00
|
|
|
import { Activity, ActivityAudience } from '../../../../shared/models/activitypub'
|
2017-12-28 05:16:08 -05:00
|
|
|
import { logger } from '../../../helpers/logger'
|
2017-12-12 11:53:50 -05:00
|
|
|
import { ACTIVITY_PUB } from '../../../initializers'
|
2017-12-14 11:38:41 -05:00
|
|
|
import { ActorModel } from '../../../models/activitypub/actor'
|
|
|
|
import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
|
2017-12-12 11:53:50 -05:00
|
|
|
import { VideoModel } from '../../../models/video/video'
|
2018-01-05 05:19:25 -05:00
|
|
|
import { VideoCommentModel } from '../../../models/video/video-comment'
|
2017-12-12 11:53:50 -05:00
|
|
|
import { VideoShareModel } from '../../../models/video/video-share'
|
2018-01-25 09:05:18 -05:00
|
|
|
import { JobQueue } from '../../job-queue'
|
2017-11-24 07:41:10 -05:00
|
|
|
|
|
|
|
async function forwardActivity (
|
|
|
|
activity: Activity,
|
|
|
|
t: Transaction,
|
2018-01-08 04:00:35 -05:00
|
|
|
followersException: ActorModel[] = [],
|
|
|
|
additionalFollowerUrls: string[] = []
|
2017-11-24 07:41:10 -05:00
|
|
|
) {
|
|
|
|
const to = activity.to || []
|
|
|
|
const cc = activity.cc || []
|
|
|
|
|
2018-01-08 04:00:35 -05:00
|
|
|
const followersUrls = additionalFollowerUrls
|
2017-11-24 07:41:10 -05:00
|
|
|
for (const dest of to.concat(cc)) {
|
|
|
|
if (dest.endsWith('/followers')) {
|
|
|
|
followersUrls.push(dest)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 11:38:41 -05:00
|
|
|
const toActorFollowers = await ActorModel.listByFollowersUrls(followersUrls, t)
|
|
|
|
const uris = await computeFollowerUris(toActorFollowers, followersException, t)
|
2017-11-24 07:41:10 -05:00
|
|
|
|
|
|
|
if (uris.length === 0) {
|
2017-12-14 11:38:41 -05:00
|
|
|
logger.info('0 followers for %s, no forwarding.', toActorFollowers.map(a => a.id).join(', '))
|
2017-11-24 09:00:10 -05:00
|
|
|
return undefined
|
2017-11-24 07:41:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
logger.debug('Creating forwarding job.', { uris })
|
|
|
|
|
2018-01-25 09:05:18 -05:00
|
|
|
const payload = {
|
2017-11-24 07:41:10 -05:00
|
|
|
uris,
|
|
|
|
body: activity
|
|
|
|
}
|
2018-01-25 09:05:18 -05:00
|
|
|
return JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload })
|
2017-11-24 07:41:10 -05:00
|
|
|
}
|
2017-11-20 03:43:39 -05:00
|
|
|
|
2017-11-22 10:25:03 -05:00
|
|
|
async function broadcastToFollowers (
|
|
|
|
data: any,
|
2017-12-14 11:38:41 -05:00
|
|
|
byActor: ActorModel,
|
|
|
|
toActorFollowers: ActorModel[],
|
2017-11-22 10:25:03 -05:00
|
|
|
t: Transaction,
|
2018-01-08 04:00:35 -05:00
|
|
|
actorsException: ActorModel[] = []
|
2017-11-22 10:25:03 -05:00
|
|
|
) {
|
2018-01-08 04:00:35 -05:00
|
|
|
const uris = await computeFollowerUris(toActorFollowers, actorsException, t)
|
2018-01-25 09:05:18 -05:00
|
|
|
return broadcastTo(uris, data, byActor)
|
2018-01-08 04:00:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
async function broadcastToActors (
|
|
|
|
data: any,
|
|
|
|
byActor: ActorModel,
|
|
|
|
toActors: ActorModel[],
|
|
|
|
actorsException: ActorModel[] = []
|
|
|
|
) {
|
|
|
|
const uris = await computeUris(toActors, actorsException)
|
2018-01-25 09:05:18 -05:00
|
|
|
return broadcastTo(uris, data, byActor)
|
2018-01-08 04:00:35 -05:00
|
|
|
}
|
|
|
|
|
2018-01-25 09:05:18 -05:00
|
|
|
async function broadcastTo (uris: string[], data: any, byActor: ActorModel) {
|
2018-01-08 04:00:35 -05:00
|
|
|
if (uris.length === 0) return undefined
|
2017-11-20 03:43:39 -05:00
|
|
|
|
2017-11-24 07:41:10 -05:00
|
|
|
logger.debug('Creating broadcast job.', { uris })
|
2017-11-22 10:25:03 -05:00
|
|
|
|
2018-01-25 09:05:18 -05:00
|
|
|
const payload = {
|
2017-11-22 10:25:03 -05:00
|
|
|
uris,
|
2017-12-14 11:38:41 -05:00
|
|
|
signatureActorId: byActor.id,
|
2017-11-20 03:43:39 -05:00
|
|
|
body: data
|
|
|
|
}
|
|
|
|
|
2018-01-25 09:05:18 -05:00
|
|
|
return JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload })
|
2017-11-20 03:43:39 -05:00
|
|
|
}
|
|
|
|
|
2018-01-25 09:05:18 -05:00
|
|
|
async function unicastTo (data: any, byActor: ActorModel, toActorUrl: string) {
|
2017-12-14 11:38:41 -05:00
|
|
|
logger.debug('Creating unicast job.', { uri: toActorUrl })
|
2017-11-24 07:41:10 -05:00
|
|
|
|
2018-01-25 09:05:18 -05:00
|
|
|
const payload = {
|
|
|
|
uri: toActorUrl,
|
2017-12-14 11:38:41 -05:00
|
|
|
signatureActorId: byActor.id,
|
2017-11-20 03:43:39 -05:00
|
|
|
body: data
|
|
|
|
}
|
|
|
|
|
2018-01-25 09:05:18 -05:00
|
|
|
return JobQueue.Instance.createJob({ type: 'activitypub-http-unicast', payload })
|
2017-11-20 03:43:39 -05:00
|
|
|
}
|
|
|
|
|
2017-12-14 11:38:41 -05:00
|
|
|
function getOriginVideoAudience (video: VideoModel, actorsInvolvedInVideo: ActorModel[]) {
|
2017-11-23 08:19:55 -05:00
|
|
|
return {
|
2017-12-14 11:38:41 -05:00
|
|
|
to: [ video.VideoChannel.Account.Actor.url ],
|
|
|
|
cc: actorsInvolvedInVideo.map(a => a.followersUrl)
|
2017-11-23 08:19:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-05 05:19:25 -05:00
|
|
|
function getOriginVideoCommentAudience (
|
|
|
|
videoComment: VideoCommentModel,
|
|
|
|
threadParentComments: VideoCommentModel[],
|
|
|
|
actorsInvolvedInVideo: ActorModel[],
|
|
|
|
isOrigin = false
|
|
|
|
) {
|
|
|
|
const to = [ ACTIVITY_PUB.PUBLIC ]
|
|
|
|
const cc = [ ]
|
|
|
|
|
|
|
|
// Owner of the video we comment
|
|
|
|
if (isOrigin === false) {
|
|
|
|
cc.push(videoComment.Video.VideoChannel.Account.Actor.url)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Followers of the poster
|
|
|
|
cc.push(videoComment.Account.Actor.followersUrl)
|
|
|
|
|
|
|
|
// Send to actors we reply to
|
|
|
|
for (const parentComment of threadParentComments) {
|
|
|
|
cc.push(parentComment.Account.Actor.url)
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
to,
|
|
|
|
cc: cc.concat(actorsInvolvedInVideo.map(a => a.followersUrl))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 11:38:41 -05:00
|
|
|
function getObjectFollowersAudience (actorsInvolvedInObject: ActorModel[]) {
|
2017-11-27 08:44:51 -05:00
|
|
|
return {
|
2018-03-23 08:27:21 -04:00
|
|
|
to: [ ACTIVITY_PUB.PUBLIC ].concat(actorsInvolvedInObject.map(a => a.followersUrl)),
|
2017-11-23 08:19:55 -05:00
|
|
|
cc: []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 11:38:41 -05:00
|
|
|
async function getActorsInvolvedInVideo (video: VideoModel, t: Transaction) {
|
2018-01-05 05:19:25 -05:00
|
|
|
const actors = await VideoShareModel.loadActorsByShare(video.id, t)
|
|
|
|
actors.push(video.VideoChannel.Account.Actor)
|
2017-11-27 08:44:51 -05:00
|
|
|
|
2018-01-05 05:19:25 -05:00
|
|
|
return actors
|
2017-11-27 08:44:51 -05:00
|
|
|
}
|
|
|
|
|
2017-12-14 11:38:41 -05:00
|
|
|
async function getAudience (actorSender: ActorModel, t: Transaction, isPublic = true) {
|
|
|
|
const followerInboxUrls = await actorSender.getFollowerSharedInboxUrls(t)
|
2017-11-20 03:43:39 -05:00
|
|
|
|
2018-01-18 08:59:27 -05:00
|
|
|
return buildAudience(followerInboxUrls, isPublic)
|
|
|
|
}
|
|
|
|
|
|
|
|
function buildAudience (followerInboxUrls: string[], isPublic = true) {
|
2017-11-20 03:43:39 -05: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
|
2018-02-21 04:07:02 -05:00
|
|
|
to = [ ]
|
|
|
|
cc = [ ]
|
2017-11-20 03:43:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return { to, cc }
|
|
|
|
}
|
|
|
|
|
2017-12-19 04:34:56 -05:00
|
|
|
function audiencify (object: any, audience: ActivityAudience) {
|
|
|
|
return Object.assign(object, audience)
|
|
|
|
}
|
|
|
|
|
2018-01-08 04:00:35 -05:00
|
|
|
async function computeFollowerUris (toActorFollower: ActorModel[], actorsException: ActorModel[], t: Transaction) {
|
2017-12-14 11:38:41 -05:00
|
|
|
const toActorFollowerIds = toActorFollower.map(a => a.id)
|
2017-11-24 07:41:10 -05:00
|
|
|
|
2017-12-14 11:38:41 -05:00
|
|
|
const result = await ActorFollowModel.listAcceptedFollowerSharedInboxUrls(toActorFollowerIds, t)
|
2018-01-11 13:17:43 -05:00
|
|
|
const sharedInboxesException = actorsException.map(f => f.sharedInboxUrl || f.inboxUrl)
|
2018-01-08 04:00:35 -05:00
|
|
|
return result.data.filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function computeUris (toActors: ActorModel[], actorsException: ActorModel[] = []) {
|
2018-01-11 13:17:43 -05:00
|
|
|
const toActorSharedInboxesSet = new Set(toActors.map(a => a.sharedInboxUrl || a.inboxUrl))
|
2018-01-08 04:00:35 -05:00
|
|
|
|
2018-01-11 13:17:43 -05:00
|
|
|
const sharedInboxesException = actorsException.map(f => f.sharedInboxUrl || f.inboxUrl)
|
2018-01-08 04:00:35 -05:00
|
|
|
return Array.from(toActorSharedInboxesSet)
|
|
|
|
.filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1)
|
2017-11-24 07:41:10 -05:00
|
|
|
}
|
|
|
|
|
2017-11-20 03:43:39 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
broadcastToFollowers,
|
|
|
|
unicastTo,
|
2018-01-18 08:59:27 -05:00
|
|
|
buildAudience,
|
2017-11-23 08:19:55 -05:00
|
|
|
getAudience,
|
|
|
|
getOriginVideoAudience,
|
2017-12-14 11:38:41 -05:00
|
|
|
getActorsInvolvedInVideo,
|
2017-11-27 08:44:51 -05:00
|
|
|
getObjectFollowersAudience,
|
2017-12-19 04:34:56 -05:00
|
|
|
forwardActivity,
|
2018-01-05 05:19:25 -05:00
|
|
|
audiencify,
|
2018-01-08 04:00:35 -05:00
|
|
|
getOriginVideoCommentAudience,
|
|
|
|
computeUris,
|
|
|
|
broadcastToActors
|
2017-11-20 03:43:39 -05:00
|
|
|
}
|