-import * as Sequelize from 'sequelize'
-
-import { database as db } from '../../initializers'
+import { Transaction } from 'sequelize'
import {
- AccountInstance,
- VideoInstance,
- VideoChannelInstance
-} from '../../models'
-import { httpRequestJobScheduler } from '../jobs'
-import { signObject, activityPubContextify } from '../../helpers'
-import { Activity } from '../../../shared'
-
-function sendCreateVideoChannel (videoChannel: VideoChannelInstance, t: Sequelize.Transaction) {
+ ActivityAccept,
+ ActivityAdd,
+ ActivityCreate,
+ ActivityDelete,
+ ActivityFollow,
+ ActivityUpdate
+} from '../../../shared/models/activitypub/activity'
+import { getActivityPubUrl } from '../../helpers/activitypub'
+import { logger } from '../../helpers/logger'
+import { database as db } from '../../initializers'
+import { AccountInstance, VideoChannelInstance, VideoInstance } from '../../models'
+import { VideoAbuseInstance } from '../../models/video/video-abuse-interface'
+import { activitypubHttpJobScheduler } from '../jobs'
+
+async function sendCreateVideoChannel (videoChannel: VideoChannelInstance, t: Transaction) {
+ const byAccount = videoChannel.Account
+
const videoChannelObject = videoChannel.toActivityPubObject()
- const data = createActivityData(videoChannel.url, videoChannel.Account, videoChannelObject)
+ const data = await createActivityData(videoChannel.url, byAccount, videoChannelObject)
- return broadcastToFollowers(data, videoChannel.Account, t)
+ return broadcastToFollowers(data, byAccount, [ byAccount ], t)
}
-function sendUpdateVideoChannel (videoChannel: VideoChannelInstance, t: Sequelize.Transaction) {
+async function sendUpdateVideoChannel (videoChannel: VideoChannelInstance, t: Transaction) {
+ const byAccount = videoChannel.Account
+
const videoChannelObject = videoChannel.toActivityPubObject()
- const data = updateActivityData(videoChannel.url, videoChannel.Account, videoChannelObject)
+ const data = await updateActivityData(videoChannel.url, byAccount, videoChannelObject)
+
+ const accountsInvolved = await db.VideoChannelShare.loadAccountsByShare(videoChannel.id)
+ accountsInvolved.push(byAccount)
- return broadcastToFollowers(data, videoChannel.Account, t)
+ return broadcastToFollowers(data, byAccount, accountsInvolved, t)
}
-function sendDeleteVideoChannel (videoChannel: VideoChannelInstance, t: Sequelize.Transaction) {
- const data = deleteActivityData(videoChannel.url, videoChannel.Account)
+async function sendDeleteVideoChannel (videoChannel: VideoChannelInstance, t: Transaction) {
+ const byAccount = videoChannel.Account
+
+ const data = await deleteActivityData(videoChannel.url, byAccount)
+
+ const accountsInvolved = await db.VideoChannelShare.loadAccountsByShare(videoChannel.id)
+ accountsInvolved.push(byAccount)
- return broadcastToFollowers(data, videoChannel.Account, t)
+ return broadcastToFollowers(data, byAccount, accountsInvolved, t)
}
-function sendAddVideo (video: VideoInstance, t: Sequelize.Transaction) {
+async function sendAddVideo (video: VideoInstance, t: Transaction) {
+ const byAccount = video.VideoChannel.Account
+
const videoObject = video.toActivityPubObject()
- const data = addActivityData(video.url, video.VideoChannel.Account, video.VideoChannel.url, videoObject)
+ const data = await addActivityData(video.url, byAccount, video.VideoChannel.url, videoObject)
- return broadcastToFollowers(data, video.VideoChannel.Account, t)
+ return broadcastToFollowers(data, byAccount, [ byAccount ], t)
}
-function sendUpdateVideo (video: VideoInstance, t: Sequelize.Transaction) {
+async function sendUpdateVideo (video: VideoInstance, t: Transaction) {
+ const byAccount = video.VideoChannel.Account
+
const videoObject = video.toActivityPubObject()
- const data = updateActivityData(video.url, video.VideoChannel.Account, videoObject)
+ const data = await updateActivityData(video.url, byAccount, videoObject)
+
+ const accountsInvolved = await db.VideoShare.loadAccountsByShare(video.id)
+ accountsInvolved.push(byAccount)
- return broadcastToFollowers(data, video.VideoChannel.Account, t)
+ return broadcastToFollowers(data, byAccount, accountsInvolved, t)
}
-function sendDeleteVideo (video: VideoInstance, t: Sequelize.Transaction) {
- const data = deleteActivityData(video.url, video.VideoChannel.Account)
+async function sendDeleteVideo (video: VideoInstance, t: Transaction) {
+ const byAccount = video.VideoChannel.Account
+
+ const data = await deleteActivityData(video.url, byAccount)
+
+ const accountsInvolved = await db.VideoShare.loadAccountsByShare(video.id)
+ accountsInvolved.push(byAccount)
- return broadcastToFollowers(data, video.VideoChannel.Account, t)
+ return broadcastToFollowers(data, byAccount, accountsInvolved, t)
}
-function sendDeleteAccount (account: AccountInstance, t: Sequelize.Transaction) {
- const data = deleteActivityData(account.url, account)
+async function sendDeleteAccount (account: AccountInstance, t: Transaction) {
+ const data = await deleteActivityData(account.url, account)
- return broadcastToFollowers(data, account, t)
+ return broadcastToFollowers(data, account, [ account ], t)
}
-function sendAccept (fromAccount: AccountInstance, toAccount: AccountInstance, t: Sequelize.Transaction) {
- const data = acceptActivityData(fromAccount)
+async function sendVideoChannelAnnounce (byAccount: AccountInstance, videoChannel: VideoChannelInstance, t: Transaction) {
+ const url = getActivityPubUrl('videoChannel', videoChannel.uuid) + '#announce'
+ const announcedActivity = await createActivityData(url, videoChannel.Account, videoChannel.toActivityPubObject())
+
+ const data = await announceActivityData(url, byAccount, announcedActivity)
+ return broadcastToFollowers(data, byAccount, [ byAccount ], t)
+}
+
+async function sendVideoAnnounce (byAccount: AccountInstance, video: VideoInstance, t: Transaction) {
+ const url = getActivityPubUrl('video', video.uuid) + '#announce'
+
+ const videoChannel = video.VideoChannel
+ const announcedActivity = await addActivityData(url, videoChannel.Account, videoChannel.url, video.toActivityPubObject())
- return unicastTo(data, toAccount, t)
+ const data = await announceActivityData(url, byAccount, announcedActivity)
+ return broadcastToFollowers(data, byAccount, [ byAccount ], t)
}
-function sendFollow (fromAccount: AccountInstance, toAccount: AccountInstance, t: Sequelize.Transaction) {
- const data = followActivityData(toAccount.url, fromAccount)
+async function sendVideoAbuse (byAccount: AccountInstance, videoAbuse: VideoAbuseInstance, video: VideoInstance, t: Transaction) {
+ const url = getActivityPubUrl('videoAbuse', videoAbuse.id.toString())
+ const data = await createActivityData(url, byAccount, videoAbuse.toActivityPubObject())
- return unicastTo(data, toAccount, t)
+ return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t)
+}
+
+async function sendAccept (byAccount: AccountInstance, toAccount: AccountInstance, t: Transaction) {
+ const data = await acceptActivityData(byAccount)
+
+ return unicastTo(data, byAccount, toAccount.inboxUrl, t)
+}
+
+async function sendFollow (byAccount: AccountInstance, toAccount: AccountInstance, t: Transaction) {
+ const data = await followActivityData(toAccount.url, byAccount)
+
+ return unicastTo(data, byAccount, toAccount.inboxUrl, t)
}
// ---------------------------------------------------------------------------
sendDeleteVideo,
sendDeleteAccount,
sendAccept,
- sendFollow
+ sendFollow,
+ sendVideoAbuse,
+ sendVideoChannelAnnounce,
+ sendVideoAnnounce
}
// ---------------------------------------------------------------------------
-async function broadcastToFollowers (data: any, fromAccount: AccountInstance, t: Sequelize.Transaction) {
- const result = await db.Account.listFollowerUrlsForApi(fromAccount.id, 0)
+async function broadcastToFollowers (data: any, byAccount: AccountInstance, toAccountFollowers: AccountInstance[], t: Transaction) {
+ const toAccountFollowerIds = toAccountFollowers.map(a => a.id)
+ const result = await db.AccountFollow.listAcceptedFollowerSharedInboxUrls(toAccountFollowerIds)
+ if (result.data.length === 0) {
+ logger.info('Not broadcast because of 0 followers for %s.', toAccountFollowerIds.join(', '))
+ return
+ }
const jobPayload = {
uris: result.data,
+ signatureAccountId: byAccount.id,
body: data
}
- return httpRequestJobScheduler.createJob(t, 'httpRequestBroadcastHandler', jobPayload)
+ return activitypubHttpJobScheduler.createJob(t, 'activitypubHttpBroadcastHandler', jobPayload)
}
-async function unicastTo (data: any, toAccount: AccountInstance, t: Sequelize.Transaction) {
+async function unicastTo (data: any, byAccount: AccountInstance, toAccountUrl: string, t: Transaction) {
const jobPayload = {
- uris: [ toAccount.url ],
+ uris: [ toAccountUrl ],
+ signatureAccountId: byAccount.id,
body: data
}
- return httpRequestJobScheduler.createJob(t, 'httpRequestUnicastHandler', jobPayload)
-}
-
-function buildSignedActivity (byAccount: AccountInstance, data: Object) {
- const activity = activityPubContextify(data)
-
- return signObject(byAccount, activity) as Promise<Activity>
+ return activitypubHttpJobScheduler.createJob(t, 'activitypubHttpUnicastHandler', jobPayload)
}
async function getPublicActivityTo (account: AccountInstance) {
async function createActivityData (url: string, byAccount: AccountInstance, object: any) {
const to = await getPublicActivityTo(byAccount)
- const base = {
+ const activity: ActivityCreate = {
type: 'Create',
id: url,
actor: byAccount.url,
object
}
- return buildSignedActivity(byAccount, base)
+ return activity
}
async function updateActivityData (url: string, byAccount: AccountInstance, object: any) {
const to = await getPublicActivityTo(byAccount)
- const base = {
+ const activity: ActivityUpdate = {
type: 'Update',
id: url,
actor: byAccount.url,
object
}
- return buildSignedActivity(byAccount, base)
+ return activity
}
async function deleteActivityData (url: string, byAccount: AccountInstance) {
- const base = {
+ const activity: ActivityDelete = {
type: 'Delete',
id: url,
actor: byAccount.url
}
- return buildSignedActivity(byAccount, base)
+ return activity
}
async function addActivityData (url: string, byAccount: AccountInstance, target: string, object: any) {
const to = await getPublicActivityTo(byAccount)
- const base = {
+ const activity: ActivityAdd = {
type: 'Add',
id: url,
actor: byAccount.url,
target
}
- return buildSignedActivity(byAccount, base)
+ return activity
+}
+
+async function announceActivityData (url: string, byAccount: AccountInstance, object: any) {
+ const activity = {
+ type: 'Announce',
+ id: url,
+ actor: byAccount.url,
+ object
+ }
+
+ return activity
}
async function followActivityData (url: string, byAccount: AccountInstance) {
- const base = {
+ const activity: ActivityFollow = {
type: 'Follow',
id: byAccount.url,
actor: byAccount.url,
object: url
}
- return buildSignedActivity(byAccount, base)
+ return activity
}
async function acceptActivityData (byAccount: AccountInstance) {
- const base = {
+ const activity: ActivityAccept = {
type: 'Accept',
id: byAccount.url,
actor: byAccount.url
}
- return buildSignedActivity(byAccount, base)
+ return activity
}