X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Factivitypub%2Fsend-request.ts;h=abc1b598db58b8ee887c4d72734f0ff4b990537c;hb=21e0727a84734cb0c81c1c9bb22a49b13e46fe5f;hp=f942a2ebad3fbb6e27b13be89ec7210f89478414;hpb=51548b31815c6f96f314ae96588a9adca150519d;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/activitypub/send-request.ts b/server/lib/activitypub/send-request.ts index f942a2eba..abc1b598d 100644 --- a/server/lib/activitypub/send-request.ts +++ b/server/lib/activitypub/send-request.ts @@ -8,64 +8,109 @@ import { } from '../../models' import { httpRequestJobScheduler } from '../jobs' import { signObject, activityPubContextify } from '../../helpers' -import { Activity } from '../../../shared' +import { Activity, VideoAbuseObject } from '../../../shared' +import { VideoAbuseInstance } from '../../models/video/video-abuse-interface' +import { getActivityPubUrl } from '../../helpers/activitypub' +import { logger } from '../../helpers/logger' async function sendCreateVideoChannel (videoChannel: VideoChannelInstance, t: Sequelize.Transaction) { const videoChannelObject = videoChannel.toActivityPubObject() const data = await createActivityData(videoChannel.url, videoChannel.Account, videoChannelObject) - return broadcastToFollowers(data, videoChannel.Account, t) + return broadcastToFollowers(data, [ videoChannel.Account ], t) } async function sendUpdateVideoChannel (videoChannel: VideoChannelInstance, t: Sequelize.Transaction) { const videoChannelObject = videoChannel.toActivityPubObject() const data = await updateActivityData(videoChannel.url, videoChannel.Account, videoChannelObject) - return broadcastToFollowers(data, videoChannel.Account, t) + const accountsInvolved = await db.VideoChannelShare.loadAccountsByShare(videoChannel.id) + accountsInvolved.push(videoChannel.Account) + + return broadcastToFollowers(data, accountsInvolved, t) } async function sendDeleteVideoChannel (videoChannel: VideoChannelInstance, t: Sequelize.Transaction) { const data = await deleteActivityData(videoChannel.url, videoChannel.Account) - return broadcastToFollowers(data, videoChannel.Account, t) + const accountsInvolved = await db.VideoChannelShare.loadAccountsByShare(videoChannel.id) + accountsInvolved.push(videoChannel.Account) + + return broadcastToFollowers(data, accountsInvolved, t) } async function sendAddVideo (video: VideoInstance, t: Sequelize.Transaction) { const videoObject = video.toActivityPubObject() const data = await addActivityData(video.url, video.VideoChannel.Account, video.VideoChannel.url, videoObject) - return broadcastToFollowers(data, video.VideoChannel.Account, t) + return broadcastToFollowers(data, [ video.VideoChannel.Account ], t) } async function sendUpdateVideo (video: VideoInstance, t: Sequelize.Transaction) { const videoObject = video.toActivityPubObject() const data = await updateActivityData(video.url, video.VideoChannel.Account, videoObject) - return broadcastToFollowers(data, video.VideoChannel.Account, t) + const accountsInvolved = await db.VideoShare.loadAccountsByShare(video.id) + accountsInvolved.push(video.VideoChannel.Account) + + return broadcastToFollowers(data, accountsInvolved, t) } async function sendDeleteVideo (video: VideoInstance, t: Sequelize.Transaction) { const data = await deleteActivityData(video.url, video.VideoChannel.Account) - return broadcastToFollowers(data, video.VideoChannel.Account, t) + const accountsInvolved = await db.VideoShare.loadAccountsByShare(video.id) + accountsInvolved.push(video.VideoChannel.Account) + + return broadcastToFollowers(data, accountsInvolved, t) } async function sendDeleteAccount (account: AccountInstance, t: Sequelize.Transaction) { const data = await deleteActivityData(account.url, account) - return broadcastToFollowers(data, account, t) + return broadcastToFollowers(data, [ account ], t) +} + +async function sendVideoChannelAnnounce (byAccount: AccountInstance, videoChannel: VideoChannelInstance, t: Sequelize.Transaction) { + const url = getActivityPubUrl('videoChannel', videoChannel.uuid) + '#announce' + const announcedActivity = await createActivityData(url, videoChannel.Account, videoChannel.toActivityPubObject(), true) + + const data = await announceActivityData(url, byAccount, announcedActivity) + return broadcastToFollowers(data, [ byAccount ], t) +} + +async function sendVideoAnnounce (byAccount: AccountInstance, video: VideoInstance, t: Sequelize.Transaction) { + const url = getActivityPubUrl('video', video.uuid) + '#announce' + + const videoChannel = video.VideoChannel + const announcedActivity = await addActivityData(url, videoChannel.Account, videoChannel.url, video.toActivityPubObject(), true) + + const data = await announceActivityData(url, byAccount, announcedActivity) + return broadcastToFollowers(data, [ byAccount ], t) +} + +async function sendVideoAbuse ( + fromAccount: AccountInstance, + videoAbuse: VideoAbuseInstance, + video: VideoInstance, + t: Sequelize.Transaction +) { + const url = getActivityPubUrl('videoAbuse', videoAbuse.id.toString()) + const data = await createActivityData(url, fromAccount, videoAbuse.toActivityPubObject()) + + return unicastTo(data, video.VideoChannel.Account.sharedInboxUrl, t) } async function sendAccept (fromAccount: AccountInstance, toAccount: AccountInstance, t: Sequelize.Transaction) { const data = await acceptActivityData(fromAccount) - return unicastTo(data, toAccount, t) + return unicastTo(data, toAccount.inboxUrl, t) } async function sendFollow (fromAccount: AccountInstance, toAccount: AccountInstance, t: Sequelize.Transaction) { const data = await followActivityData(toAccount.url, fromAccount) - return unicastTo(data, toAccount, t) + return unicastTo(data, toAccount.inboxUrl, t) } // --------------------------------------------------------------------------- @@ -79,13 +124,21 @@ export { sendDeleteVideo, sendDeleteAccount, sendAccept, - sendFollow + sendFollow, + sendVideoAbuse, + sendVideoChannelAnnounce, + sendVideoAnnounce } // --------------------------------------------------------------------------- -async function broadcastToFollowers (data: any, fromAccount: AccountInstance, t: Sequelize.Transaction) { - const result = await db.AccountFollow.listAcceptedFollowerUrlsForApi(fromAccount.id, 0) +async function broadcastToFollowers (data: any, toAccountFollowers: AccountInstance[], t: Sequelize.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, @@ -95,9 +148,9 @@ async function broadcastToFollowers (data: any, fromAccount: AccountInstance, t: return httpRequestJobScheduler.createJob(t, 'httpRequestBroadcastHandler', jobPayload) } -async function unicastTo (data: any, toAccount: AccountInstance, t: Sequelize.Transaction) { +async function unicastTo (data: any, toAccountUrl: string, t: Sequelize.Transaction) { const jobPayload = { - uris: [ toAccount.inboxUrl ], + uris: [ toAccountUrl ], body: data } @@ -116,7 +169,7 @@ async function getPublicActivityTo (account: AccountInstance) { return inboxUrls.concat('https://www.w3.org/ns/activitystreams#Public') } -async function createActivityData (url: string, byAccount: AccountInstance, object: any) { +async function createActivityData (url: string, byAccount: AccountInstance, object: any, raw = false) { const to = await getPublicActivityTo(byAccount) const base = { type: 'Create', @@ -126,6 +179,8 @@ async function createActivityData (url: string, byAccount: AccountInstance, obje object } + if (raw === true) return base + return buildSignedActivity(byAccount, base) } @@ -152,7 +207,7 @@ async function deleteActivityData (url: string, byAccount: AccountInstance) { return buildSignedActivity(byAccount, base) } -async function addActivityData (url: string, byAccount: AccountInstance, target: string, object: any) { +async function addActivityData (url: string, byAccount: AccountInstance, target: string, object: any, raw = false) { const to = await getPublicActivityTo(byAccount) const base = { type: 'Add', @@ -163,6 +218,19 @@ async function addActivityData (url: string, byAccount: AccountInstance, target: target } + if (raw === true) return base + + return buildSignedActivity(byAccount, base) +} + +async function announceActivityData (url: string, byAccount: AccountInstance, object: any) { + const base = { + type: 'Announce', + id: url, + actor: byAccount.url, + object + } + return buildSignedActivity(byAccount, base) }