2017-11-20 02:43:39 -06:00
|
|
|
import { Transaction } from 'sequelize'
|
2018-09-14 09:51:35 -05:00
|
|
|
import { Activity, ActivityAudience } from '../../../../shared/models/activitypub'
|
2017-12-28 04:16:08 -06:00
|
|
|
import { logger } from '../../../helpers/logger'
|
2017-12-14 10:38:41 -06:00
|
|
|
import { ActorModel } from '../../../models/activitypub/actor'
|
|
|
|
import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
|
2018-01-25 08:05:18 -06:00
|
|
|
import { JobQueue } from '../../job-queue'
|
2018-05-31 03:23:56 -05:00
|
|
|
import { VideoModel } from '../../../models/video/video'
|
2018-09-14 09:51:35 -05:00
|
|
|
import { getActorsInvolvedInVideo, getAudienceFromFollowersOf, getRemoteVideoAudience } from '../audience'
|
2018-08-16 08:25:20 -05:00
|
|
|
import { getServerActor } from '../../../helpers/utils'
|
2019-07-29 04:59:29 -05:00
|
|
|
import { afterCommitIfTransaction } from '../../../helpers/database-utils'
|
2019-08-09 01:17:16 -05:00
|
|
|
import { ActorFollowerException, ActorModelId, ActorModelOnly } from '../../../typings/models'
|
2018-05-31 03:23:56 -05:00
|
|
|
|
2018-09-14 09:51:35 -05:00
|
|
|
async function sendVideoRelatedActivity (activityBuilder: (audience: ActivityAudience) => Activity, options: {
|
2019-08-09 01:17:16 -05:00
|
|
|
byActor: ActorModelOnly,
|
2018-09-14 09:51:35 -05:00
|
|
|
video: VideoModel,
|
|
|
|
transaction?: Transaction
|
|
|
|
}) {
|
2019-08-09 01:17:16 -05:00
|
|
|
const { byActor, video, transaction } = options
|
|
|
|
|
|
|
|
const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, transaction)
|
2018-09-14 09:51:35 -05:00
|
|
|
|
|
|
|
// Send to origin
|
2019-08-09 01:17:16 -05:00
|
|
|
if (video.isOwned() === false) {
|
|
|
|
const audience = getRemoteVideoAudience(video, actorsInvolvedInVideo)
|
2018-09-14 09:51:35 -05:00
|
|
|
const activity = activityBuilder(audience)
|
|
|
|
|
2019-08-09 01:17:16 -05:00
|
|
|
return afterCommitIfTransaction(transaction, () => {
|
|
|
|
return unicastTo(activity, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
|
2019-07-29 04:59:29 -05:00
|
|
|
})
|
2018-09-14 09:51:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send to followers
|
|
|
|
const audience = getAudienceFromFollowersOf(actorsInvolvedInVideo)
|
|
|
|
const activity = activityBuilder(audience)
|
|
|
|
|
2019-08-09 01:17:16 -05:00
|
|
|
const actorsException = [ byActor ]
|
2019-07-29 04:59:29 -05:00
|
|
|
|
2019-08-09 01:17:16 -05:00
|
|
|
return broadcastToFollowers(activity, byActor, actorsInvolvedInVideo, transaction, actorsException)
|
2018-09-14 09:51:35 -05:00
|
|
|
}
|
|
|
|
|
2018-05-31 03:23:56 -05:00
|
|
|
async function forwardVideoRelatedActivity (
|
|
|
|
activity: Activity,
|
|
|
|
t: Transaction,
|
2019-08-09 01:17:16 -05:00
|
|
|
followersException: ActorFollowerException[] = [],
|
2018-05-31 03:23:56 -05:00
|
|
|
video: VideoModel
|
|
|
|
) {
|
|
|
|
// Mastodon does not add our announces in audience, so we forward to them manually
|
|
|
|
const additionalActors = await getActorsInvolvedInVideo(video, t)
|
|
|
|
const additionalFollowerUrls = additionalActors.map(a => a.followersUrl)
|
|
|
|
|
|
|
|
return forwardActivity(activity, t, followersException, additionalFollowerUrls)
|
|
|
|
}
|
2017-11-24 06:41:10 -06:00
|
|
|
|
|
|
|
async function forwardActivity (
|
|
|
|
activity: Activity,
|
|
|
|
t: Transaction,
|
2019-08-09 01:17:16 -05:00
|
|
|
followersException: ActorFollowerException[] = [],
|
2018-01-08 03:00:35 -06:00
|
|
|
additionalFollowerUrls: string[] = []
|
2017-11-24 06:41:10 -06:00
|
|
|
) {
|
2018-07-30 10:02:40 -05:00
|
|
|
logger.info('Forwarding activity %s.', activity.id)
|
|
|
|
|
2017-11-24 06:41:10 -06:00
|
|
|
const to = activity.to || []
|
|
|
|
const cc = activity.cc || []
|
|
|
|
|
2018-01-08 03:00:35 -06:00
|
|
|
const followersUrls = additionalFollowerUrls
|
2017-11-24 06:41:10 -06:00
|
|
|
for (const dest of to.concat(cc)) {
|
|
|
|
if (dest.endsWith('/followers')) {
|
|
|
|
followersUrls.push(dest)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 10:38:41 -06:00
|
|
|
const toActorFollowers = await ActorModel.listByFollowersUrls(followersUrls, t)
|
|
|
|
const uris = await computeFollowerUris(toActorFollowers, followersException, t)
|
2017-11-24 06:41:10 -06:00
|
|
|
|
|
|
|
if (uris.length === 0) {
|
2017-12-14 10:38:41 -06:00
|
|
|
logger.info('0 followers for %s, no forwarding.', toActorFollowers.map(a => a.id).join(', '))
|
2017-11-24 08:00:10 -06:00
|
|
|
return undefined
|
2017-11-24 06:41:10 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
logger.debug('Creating forwarding job.', { uris })
|
|
|
|
|
2018-01-25 08:05:18 -06:00
|
|
|
const payload = {
|
2017-11-24 06:41:10 -06:00
|
|
|
uris,
|
|
|
|
body: activity
|
|
|
|
}
|
2019-07-29 04:59:29 -05:00
|
|
|
return afterCommitIfTransaction(t, () => JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload }))
|
2017-11-24 06:41:10 -06:00
|
|
|
}
|
2017-11-20 02:43:39 -06:00
|
|
|
|
2017-11-22 09:25:03 -06:00
|
|
|
async function broadcastToFollowers (
|
|
|
|
data: any,
|
2019-08-09 01:17:16 -05:00
|
|
|
byActor: ActorModelId,
|
|
|
|
toFollowersOf: ActorModelId[],
|
2017-11-22 09:25:03 -06:00
|
|
|
t: Transaction,
|
2019-08-09 01:17:16 -05:00
|
|
|
actorsException: ActorFollowerException[] = []
|
2017-11-22 09:25:03 -06:00
|
|
|
) {
|
2018-09-11 09:27:07 -05:00
|
|
|
const uris = await computeFollowerUris(toFollowersOf, actorsException, t)
|
2019-07-29 04:59:29 -05:00
|
|
|
|
|
|
|
return afterCommitIfTransaction(t, () => broadcastTo(uris, data, byActor))
|
2018-01-08 03:00:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
async function broadcastToActors (
|
|
|
|
data: any,
|
2019-08-09 01:17:16 -05:00
|
|
|
byActor: ActorModelId,
|
|
|
|
toActors: ActorModelOnly[],
|
2019-07-29 04:59:29 -05:00
|
|
|
t?: Transaction,
|
2019-08-09 01:17:16 -05:00
|
|
|
actorsException: ActorFollowerException[] = []
|
2018-01-08 03:00:35 -06:00
|
|
|
) {
|
|
|
|
const uris = await computeUris(toActors, actorsException)
|
2019-07-29 04:59:29 -05:00
|
|
|
return afterCommitIfTransaction(t, () => broadcastTo(uris, data, byActor))
|
2018-01-08 03:00:35 -06:00
|
|
|
}
|
|
|
|
|
2019-08-09 01:17:16 -05:00
|
|
|
function broadcastTo (uris: string[], data: any, byActor: ActorModelId) {
|
2018-01-08 03:00:35 -06:00
|
|
|
if (uris.length === 0) return undefined
|
2017-11-20 02:43:39 -06:00
|
|
|
|
2017-11-24 06:41:10 -06:00
|
|
|
logger.debug('Creating broadcast job.', { uris })
|
2017-11-22 09:25:03 -06:00
|
|
|
|
2018-01-25 08:05:18 -06:00
|
|
|
const payload = {
|
2017-11-22 09:25:03 -06:00
|
|
|
uris,
|
2017-12-14 10:38:41 -06:00
|
|
|
signatureActorId: byActor.id,
|
2017-11-20 02:43:39 -06:00
|
|
|
body: data
|
|
|
|
}
|
|
|
|
|
2018-01-25 08:05:18 -06:00
|
|
|
return JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload })
|
2017-11-20 02:43:39 -06:00
|
|
|
}
|
|
|
|
|
2019-08-09 01:17:16 -05:00
|
|
|
function unicastTo (data: any, byActor: ActorModelId, toActorUrl: string) {
|
2017-12-14 10:38:41 -06:00
|
|
|
logger.debug('Creating unicast job.', { uri: toActorUrl })
|
2017-11-24 06:41:10 -06:00
|
|
|
|
2018-01-25 08:05:18 -06:00
|
|
|
const payload = {
|
|
|
|
uri: toActorUrl,
|
2017-12-14 10:38:41 -06:00
|
|
|
signatureActorId: byActor.id,
|
2017-11-20 02:43:39 -06:00
|
|
|
body: data
|
|
|
|
}
|
|
|
|
|
2019-07-29 04:59:29 -05:00
|
|
|
JobQueue.Instance.createJob({ type: 'activitypub-http-unicast', payload })
|
2017-11-20 02:43:39 -06:00
|
|
|
}
|
|
|
|
|
2018-05-25 04:32:36 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
2017-11-20 02:43:39 -06:00
|
|
|
|
2018-05-25 04:32:36 -05:00
|
|
|
export {
|
|
|
|
broadcastToFollowers,
|
|
|
|
unicastTo,
|
|
|
|
forwardActivity,
|
2018-05-31 03:23:56 -05:00
|
|
|
broadcastToActors,
|
2018-09-14 09:51:35 -05:00
|
|
|
forwardVideoRelatedActivity,
|
|
|
|
sendVideoRelatedActivity
|
2017-11-20 02:43:39 -06:00
|
|
|
}
|
|
|
|
|
2018-05-25 04:32:36 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
2017-12-19 03:34:56 -06:00
|
|
|
|
2019-08-09 01:17:16 -05:00
|
|
|
async function computeFollowerUris (toFollowersOf: ActorModelId[], actorsException: ActorFollowerException[], t: Transaction) {
|
2018-09-11 09:27:07 -05:00
|
|
|
const toActorFollowerIds = toFollowersOf.map(a => a.id)
|
2017-11-24 06:41:10 -06:00
|
|
|
|
2017-12-14 10:38:41 -06:00
|
|
|
const result = await ActorFollowModel.listAcceptedFollowerSharedInboxUrls(toActorFollowerIds, t)
|
2018-08-16 08:25:20 -05:00
|
|
|
const sharedInboxesException = await buildSharedInboxesException(actorsException)
|
|
|
|
|
2018-01-08 03:00:35 -06:00
|
|
|
return result.data.filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1)
|
|
|
|
}
|
|
|
|
|
2019-08-09 01:17:16 -05:00
|
|
|
async function computeUris (toActors: ActorModelOnly[], actorsException: ActorFollowerException[] = []) {
|
2018-08-16 08:25:20 -05:00
|
|
|
const serverActor = await getServerActor()
|
|
|
|
const targetUrls = toActors
|
|
|
|
.filter(a => a.id !== serverActor.id) // Don't send to ourselves
|
|
|
|
.map(a => a.sharedInboxUrl || a.inboxUrl)
|
|
|
|
|
|
|
|
const toActorSharedInboxesSet = new Set(targetUrls)
|
2018-01-08 03:00:35 -06:00
|
|
|
|
2018-08-16 08:25:20 -05:00
|
|
|
const sharedInboxesException = await buildSharedInboxesException(actorsException)
|
2018-01-08 03:00:35 -06:00
|
|
|
return Array.from(toActorSharedInboxesSet)
|
2018-05-25 04:32:36 -05:00
|
|
|
.filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1)
|
2017-11-20 02:43:39 -06:00
|
|
|
}
|
2018-08-16 08:25:20 -05:00
|
|
|
|
2019-08-09 01:17:16 -05:00
|
|
|
async function buildSharedInboxesException (actorsException: ActorFollowerException[]) {
|
2018-08-16 08:25:20 -05:00
|
|
|
const serverActor = await getServerActor()
|
|
|
|
|
|
|
|
return actorsException
|
|
|
|
.map(f => f.sharedInboxUrl || f.inboxUrl)
|
|
|
|
.concat([ serverActor.sharedInboxUrl ])
|
|
|
|
}
|