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'
|
|
|
|
import { AccountModel } from '../../../models/account/account'
|
|
|
|
import { AccountFollowModel } from '../../../models/account/account-follow'
|
|
|
|
import { VideoModel } from '../../../models/video/video'
|
|
|
|
import { VideoChannelModel } from '../../../models/video/video-channel'
|
|
|
|
import { VideoChannelShareModel } from '../../../models/video/video-channel-share'
|
|
|
|
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-12 16:53:50 +00:00
|
|
|
followersException: AccountModel[] = []
|
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-12 16:53:50 +00:00
|
|
|
const toAccountFollowers = await AccountModel.listByFollowersUrls(followersUrls, t)
|
2017-11-30 10:31:15 +00:00
|
|
|
const uris = await computeFollowerUris(toAccountFollowers, followersException, t)
|
2017-11-24 12:41:10 +00:00
|
|
|
|
|
|
|
if (uris.length === 0) {
|
|
|
|
logger.info('0 followers for %s, no forwarding.', toAccountFollowers.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-12 16:53:50 +00:00
|
|
|
byAccount: AccountModel,
|
|
|
|
toAccountFollowers: AccountModel[],
|
2017-11-22 15:25:03 +00:00
|
|
|
t: Transaction,
|
2017-12-12 16:53:50 +00:00
|
|
|
followersException: AccountModel[] = []
|
2017-11-22 15:25:03 +00:00
|
|
|
) {
|
2017-11-30 10:31:15 +00:00
|
|
|
const uris = await computeFollowerUris(toAccountFollowers, followersException, t)
|
2017-11-24 12:41:10 +00:00
|
|
|
if (uris.length === 0) {
|
|
|
|
logger.info('0 followers for %s, no broadcasting.', toAccountFollowers.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-11-20 08:43:39 +00:00
|
|
|
signatureAccountId: byAccount.id,
|
|
|
|
body: data
|
|
|
|
}
|
|
|
|
|
|
|
|
return activitypubHttpJobScheduler.createJob(t, 'activitypubHttpBroadcastHandler', jobPayload)
|
|
|
|
}
|
|
|
|
|
2017-12-12 16:53:50 +00:00
|
|
|
async function unicastTo (data: any, byAccount: AccountModel, toAccountUrl: string, t: Transaction) {
|
2017-11-24 12:41:10 +00:00
|
|
|
logger.debug('Creating unicast job.', { uri: toAccountUrl })
|
|
|
|
|
|
|
|
const jobPayload: ActivityPubHttpPayload = {
|
2017-11-20 08:43:39 +00:00
|
|
|
uris: [ toAccountUrl ],
|
|
|
|
signatureAccountId: byAccount.id,
|
|
|
|
body: data
|
|
|
|
}
|
|
|
|
|
|
|
|
return activitypubHttpJobScheduler.createJob(t, 'activitypubHttpUnicastHandler', jobPayload)
|
|
|
|
}
|
|
|
|
|
2017-12-12 16:53:50 +00:00
|
|
|
function getOriginVideoAudience (video: VideoModel, accountsInvolvedInVideo: AccountModel[]) {
|
2017-11-23 13:19:55 +00:00
|
|
|
return {
|
|
|
|
to: [ video.VideoChannel.Account.url ],
|
2017-11-24 12:41:10 +00:00
|
|
|
cc: accountsInvolvedInVideo.map(a => a.followersUrl)
|
2017-11-23 13:19:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-12 16:53:50 +00:00
|
|
|
function getOriginVideoChannelAudience (videoChannel: VideoChannelModel, accountsInvolved: AccountModel[]) {
|
2017-11-27 13:44:51 +00:00
|
|
|
return {
|
|
|
|
to: [ videoChannel.Account.url ],
|
|
|
|
cc: accountsInvolved.map(a => a.followersUrl)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-12 16:53:50 +00:00
|
|
|
function getObjectFollowersAudience (accountsInvolvedInObject: AccountModel[]) {
|
2017-11-23 13:19:55 +00:00
|
|
|
return {
|
2017-11-27 13:44:51 +00:00
|
|
|
to: accountsInvolvedInObject.map(a => a.followersUrl),
|
2017-11-23 13:19:55 +00:00
|
|
|
cc: []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-12 16:53:50 +00:00
|
|
|
async function getAccountsInvolvedInVideo (video: VideoModel, t: Transaction) {
|
|
|
|
const accountsToForwardView = await VideoShareModel.loadAccountsByShare(video.id, t)
|
2017-11-23 13:19:55 +00:00
|
|
|
accountsToForwardView.push(video.VideoChannel.Account)
|
|
|
|
|
|
|
|
return accountsToForwardView
|
|
|
|
}
|
|
|
|
|
2017-12-12 16:53:50 +00:00
|
|
|
async function getAccountsInvolvedInVideoChannel (videoChannel: VideoChannelModel, t: Transaction) {
|
|
|
|
const accountsToForwardView = await VideoChannelShareModel.loadAccountsByShare(videoChannel.id, t)
|
2017-11-27 13:44:51 +00:00
|
|
|
accountsToForwardView.push(videoChannel.Account)
|
|
|
|
|
|
|
|
return accountsToForwardView
|
|
|
|
}
|
|
|
|
|
2017-12-12 16:53:50 +00:00
|
|
|
async function getAudience (accountSender: AccountModel, t: Transaction, isPublic = true) {
|
2017-11-30 10:31:15 +00:00
|
|
|
const followerInboxUrls = await accountSender.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-12 16:53:50 +00:00
|
|
|
async function computeFollowerUris (toAccountFollower: AccountModel[], followersException: AccountModel[], t: Transaction) {
|
2017-11-24 12:41:10 +00:00
|
|
|
const toAccountFollowerIds = toAccountFollower.map(a => a.id)
|
|
|
|
|
2017-12-12 16:53:50 +00:00
|
|
|
const result = await AccountFollowModel.listAcceptedFollowerSharedInboxUrls(toAccountFollowerIds, 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,
|
2017-11-27 13:44:51 +00:00
|
|
|
getOriginVideoChannelAudience,
|
2017-11-20 08:43:39 +00:00
|
|
|
unicastTo,
|
2017-11-23 13:19:55 +00:00
|
|
|
getAudience,
|
|
|
|
getOriginVideoAudience,
|
2017-11-24 12:41:10 +00:00
|
|
|
getAccountsInvolvedInVideo,
|
2017-11-27 13:44:51 +00:00
|
|
|
getAccountsInvolvedInVideoChannel,
|
|
|
|
getObjectFollowersAudience,
|
2017-11-24 12:41:10 +00:00
|
|
|
forwardActivity
|
2017-11-20 08:43:39 +00:00
|
|
|
}
|