2017-11-20 09:43:39 +01:00
|
|
|
import { Transaction } from 'sequelize'
|
2018-09-14 16:51:35 +02:00
|
|
|
import { Activity, ActivityAudience } from '../../../../shared/models/activitypub'
|
2017-12-28 11:16:08 +01:00
|
|
|
import { logger } from '../../../helpers/logger'
|
2017-12-14 17:38:41 +01:00
|
|
|
import { ActorModel } from '../../../models/activitypub/actor'
|
|
|
|
import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
|
2018-01-25 15:05:18 +01:00
|
|
|
import { JobQueue } from '../../job-queue'
|
2018-09-14 16:51:35 +02:00
|
|
|
import { getActorsInvolvedInVideo, getAudienceFromFollowersOf, getRemoteVideoAudience } from '../audience'
|
2018-08-16 15:25:20 +02:00
|
|
|
import { getServerActor } from '../../../helpers/utils'
|
2019-07-29 11:59:29 +02:00
|
|
|
import { afterCommitIfTransaction } from '../../../helpers/database-utils'
|
2020-02-04 16:14:33 +01:00
|
|
|
import { MActor, MActorId, MActorLight, MActorWithInboxes, MVideoAccountLight, MVideoId, MVideoImmutable } from '../../../typings/models'
|
2020-02-03 11:12:29 +01:00
|
|
|
import { ContextType } from '@server/helpers/activitypub'
|
2018-05-31 10:23:56 +02:00
|
|
|
|
2018-09-14 16:51:35 +02:00
|
|
|
async function sendVideoRelatedActivity (activityBuilder: (audience: ActivityAudience) => Activity, options: {
|
2020-01-31 16:56:52 +01:00
|
|
|
byActor: MActorLight
|
2020-02-04 16:14:33 +01:00
|
|
|
video: MVideoImmutable | MVideoAccountLight
|
2020-02-04 09:12:08 +01:00
|
|
|
transaction?: Transaction
|
2020-02-03 11:12:29 +01:00
|
|
|
contextType?: ContextType
|
2018-09-14 16:51:35 +02:00
|
|
|
}) {
|
2020-02-03 11:12:29 +01:00
|
|
|
const { byActor, video, transaction, contextType } = options
|
2019-08-09 08:17:16 +02:00
|
|
|
|
|
|
|
const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, transaction)
|
2018-09-14 16:51:35 +02:00
|
|
|
|
|
|
|
// Send to origin
|
2019-08-09 08:17:16 +02:00
|
|
|
if (video.isOwned() === false) {
|
2020-02-04 16:14:33 +01:00
|
|
|
const accountActor = (video as MVideoAccountLight).VideoChannel?.Account?.Actor || await ActorModel.loadAccountActorByVideoId(video.id)
|
|
|
|
|
|
|
|
const audience = getRemoteVideoAudience(accountActor, actorsInvolvedInVideo)
|
2018-09-14 16:51:35 +02:00
|
|
|
const activity = activityBuilder(audience)
|
|
|
|
|
2019-08-09 08:17:16 +02:00
|
|
|
return afterCommitIfTransaction(transaction, () => {
|
2020-02-04 16:14:33 +01:00
|
|
|
return unicastTo(activity, byActor, accountActor.getSharedInbox(), contextType)
|
2019-07-29 11:59:29 +02:00
|
|
|
})
|
2018-09-14 16:51:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send to followers
|
|
|
|
const audience = getAudienceFromFollowersOf(actorsInvolvedInVideo)
|
|
|
|
const activity = activityBuilder(audience)
|
|
|
|
|
2019-08-09 08:17:16 +02:00
|
|
|
const actorsException = [ byActor ]
|
2019-07-29 11:59:29 +02:00
|
|
|
|
2020-02-03 11:12:29 +01:00
|
|
|
return broadcastToFollowers(activity, byActor, actorsInvolvedInVideo, transaction, actorsException, contextType)
|
2018-09-14 16:51:35 +02:00
|
|
|
}
|
|
|
|
|
2018-05-31 10:23:56 +02:00
|
|
|
async function forwardVideoRelatedActivity (
|
|
|
|
activity: Activity,
|
|
|
|
t: Transaction,
|
2019-10-23 11:33:53 +02:00
|
|
|
followersException: MActorWithInboxes[] = [],
|
2020-02-04 15:45:41 +01:00
|
|
|
video: MVideoId
|
2018-05-31 10:23:56 +02:00
|
|
|
) {
|
|
|
|
// 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 13:41:10 +01:00
|
|
|
|
|
|
|
async function forwardActivity (
|
|
|
|
activity: Activity,
|
|
|
|
t: Transaction,
|
2019-10-23 11:33:53 +02:00
|
|
|
followersException: MActorWithInboxes[] = [],
|
2018-01-08 10:00:35 +01:00
|
|
|
additionalFollowerUrls: string[] = []
|
2017-11-24 13:41:10 +01:00
|
|
|
) {
|
2018-07-30 17:02:40 +02:00
|
|
|
logger.info('Forwarding activity %s.', activity.id)
|
|
|
|
|
2017-11-24 13:41:10 +01:00
|
|
|
const to = activity.to || []
|
|
|
|
const cc = activity.cc || []
|
|
|
|
|
2018-01-08 10:00:35 +01:00
|
|
|
const followersUrls = additionalFollowerUrls
|
2017-11-24 13:41:10 +01:00
|
|
|
for (const dest of to.concat(cc)) {
|
|
|
|
if (dest.endsWith('/followers')) {
|
|
|
|
followersUrls.push(dest)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 17:38:41 +01:00
|
|
|
const toActorFollowers = await ActorModel.listByFollowersUrls(followersUrls, t)
|
|
|
|
const uris = await computeFollowerUris(toActorFollowers, followersException, t)
|
2017-11-24 13:41:10 +01:00
|
|
|
|
|
|
|
if (uris.length === 0) {
|
2017-12-14 17:38:41 +01:00
|
|
|
logger.info('0 followers for %s, no forwarding.', toActorFollowers.map(a => a.id).join(', '))
|
2017-11-24 15:00:10 +01:00
|
|
|
return undefined
|
2017-11-24 13:41:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
logger.debug('Creating forwarding job.', { uris })
|
|
|
|
|
2018-01-25 15:05:18 +01:00
|
|
|
const payload = {
|
2017-11-24 13:41:10 +01:00
|
|
|
uris,
|
|
|
|
body: activity
|
|
|
|
}
|
2019-07-29 11:59:29 +02:00
|
|
|
return afterCommitIfTransaction(t, () => JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload }))
|
2017-11-24 13:41:10 +01:00
|
|
|
}
|
2017-11-20 09:43:39 +01:00
|
|
|
|
2017-11-22 16:25:03 +01:00
|
|
|
async function broadcastToFollowers (
|
|
|
|
data: any,
|
2019-08-15 11:53:26 +02:00
|
|
|
byActor: MActorId,
|
|
|
|
toFollowersOf: MActorId[],
|
2017-11-22 16:25:03 +01:00
|
|
|
t: Transaction,
|
2020-02-03 11:12:29 +01:00
|
|
|
actorsException: MActorWithInboxes[] = [],
|
|
|
|
contextType?: ContextType
|
2017-11-22 16:25:03 +01:00
|
|
|
) {
|
2018-09-11 16:27:07 +02:00
|
|
|
const uris = await computeFollowerUris(toFollowersOf, actorsException, t)
|
2019-07-29 11:59:29 +02:00
|
|
|
|
2020-02-03 11:12:29 +01:00
|
|
|
return afterCommitIfTransaction(t, () => broadcastTo(uris, data, byActor, contextType))
|
2018-01-08 10:00:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async function broadcastToActors (
|
|
|
|
data: any,
|
2019-08-15 11:53:26 +02:00
|
|
|
byActor: MActorId,
|
|
|
|
toActors: MActor[],
|
2019-07-29 11:59:29 +02:00
|
|
|
t?: Transaction,
|
2020-02-03 11:12:29 +01:00
|
|
|
actorsException: MActorWithInboxes[] = [],
|
|
|
|
contextType?: ContextType
|
2018-01-08 10:00:35 +01:00
|
|
|
) {
|
|
|
|
const uris = await computeUris(toActors, actorsException)
|
2020-02-03 11:12:29 +01:00
|
|
|
return afterCommitIfTransaction(t, () => broadcastTo(uris, data, byActor, contextType))
|
2018-01-08 10:00:35 +01:00
|
|
|
}
|
|
|
|
|
2020-02-03 11:12:29 +01:00
|
|
|
function broadcastTo (uris: string[], data: any, byActor: MActorId, contextType?: ContextType) {
|
2018-01-08 10:00:35 +01:00
|
|
|
if (uris.length === 0) return undefined
|
2017-11-20 09:43:39 +01:00
|
|
|
|
2017-11-24 13:41:10 +01:00
|
|
|
logger.debug('Creating broadcast job.', { uris })
|
2017-11-22 16:25:03 +01:00
|
|
|
|
2018-01-25 15:05:18 +01:00
|
|
|
const payload = {
|
2017-11-22 16:25:03 +01:00
|
|
|
uris,
|
2017-12-14 17:38:41 +01:00
|
|
|
signatureActorId: byActor.id,
|
2020-02-03 11:12:29 +01:00
|
|
|
body: data,
|
|
|
|
contextType
|
2017-11-20 09:43:39 +01:00
|
|
|
}
|
|
|
|
|
2018-01-25 15:05:18 +01:00
|
|
|
return JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload })
|
2017-11-20 09:43:39 +01:00
|
|
|
}
|
|
|
|
|
2020-02-03 11:12:29 +01:00
|
|
|
function unicastTo (data: any, byActor: MActorId, toActorUrl: string, contextType?: ContextType) {
|
2017-12-14 17:38:41 +01:00
|
|
|
logger.debug('Creating unicast job.', { uri: toActorUrl })
|
2017-11-24 13:41:10 +01:00
|
|
|
|
2018-01-25 15:05:18 +01:00
|
|
|
const payload = {
|
|
|
|
uri: toActorUrl,
|
2017-12-14 17:38:41 +01:00
|
|
|
signatureActorId: byActor.id,
|
2020-02-03 11:12:29 +01:00
|
|
|
body: data,
|
|
|
|
contextType
|
2017-11-20 09:43:39 +01:00
|
|
|
}
|
|
|
|
|
2019-07-29 11:59:29 +02:00
|
|
|
JobQueue.Instance.createJob({ type: 'activitypub-http-unicast', payload })
|
2017-11-20 09:43:39 +01:00
|
|
|
}
|
|
|
|
|
2018-05-25 11:32:36 +02:00
|
|
|
// ---------------------------------------------------------------------------
|
2017-11-20 09:43:39 +01:00
|
|
|
|
2018-05-25 11:32:36 +02:00
|
|
|
export {
|
|
|
|
broadcastToFollowers,
|
|
|
|
unicastTo,
|
|
|
|
forwardActivity,
|
2018-05-31 10:23:56 +02:00
|
|
|
broadcastToActors,
|
2018-09-14 16:51:35 +02:00
|
|
|
forwardVideoRelatedActivity,
|
|
|
|
sendVideoRelatedActivity
|
2017-11-20 09:43:39 +01:00
|
|
|
}
|
|
|
|
|
2018-05-25 11:32:36 +02:00
|
|
|
// ---------------------------------------------------------------------------
|
2017-12-19 10:34:56 +01:00
|
|
|
|
2019-10-23 11:33:53 +02:00
|
|
|
async function computeFollowerUris (toFollowersOf: MActorId[], actorsException: MActorWithInboxes[], t: Transaction) {
|
2018-09-11 16:27:07 +02:00
|
|
|
const toActorFollowerIds = toFollowersOf.map(a => a.id)
|
2017-11-24 13:41:10 +01:00
|
|
|
|
2017-12-14 17:38:41 +01:00
|
|
|
const result = await ActorFollowModel.listAcceptedFollowerSharedInboxUrls(toActorFollowerIds, t)
|
2018-08-16 15:25:20 +02:00
|
|
|
const sharedInboxesException = await buildSharedInboxesException(actorsException)
|
|
|
|
|
2018-01-08 10:00:35 +01:00
|
|
|
return result.data.filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1)
|
|
|
|
}
|
|
|
|
|
2019-10-23 11:33:53 +02:00
|
|
|
async function computeUris (toActors: MActor[], actorsException: MActorWithInboxes[] = []) {
|
2018-08-16 15:25:20 +02:00
|
|
|
const serverActor = await getServerActor()
|
|
|
|
const targetUrls = toActors
|
|
|
|
.filter(a => a.id !== serverActor.id) // Don't send to ourselves
|
2019-10-23 11:33:53 +02:00
|
|
|
.map(a => a.getSharedInbox())
|
2018-08-16 15:25:20 +02:00
|
|
|
|
|
|
|
const toActorSharedInboxesSet = new Set(targetUrls)
|
2018-01-08 10:00:35 +01:00
|
|
|
|
2018-08-16 15:25:20 +02:00
|
|
|
const sharedInboxesException = await buildSharedInboxesException(actorsException)
|
2018-01-08 10:00:35 +01:00
|
|
|
return Array.from(toActorSharedInboxesSet)
|
2018-05-25 11:32:36 +02:00
|
|
|
.filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1)
|
2017-11-20 09:43:39 +01:00
|
|
|
}
|
2018-08-16 15:25:20 +02:00
|
|
|
|
2019-10-23 11:33:53 +02:00
|
|
|
async function buildSharedInboxesException (actorsException: MActorWithInboxes[]) {
|
2018-08-16 15:25:20 +02:00
|
|
|
const serverActor = await getServerActor()
|
|
|
|
|
|
|
|
return actorsException
|
2019-10-23 11:33:53 +02:00
|
|
|
.map(f => f.getSharedInbox())
|
2018-08-16 15:25:20 +02:00
|
|
|
.concat([ serverActor.sharedInboxUrl ])
|
|
|
|
}
|