Reduce AP context size on specific activities

This commit is contained in:
Chocobozzz 2020-02-03 11:12:29 +01:00
parent d6ebf0cac4
commit 598edb8af1
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
8 changed files with 121 additions and 104 deletions

View File

@ -234,7 +234,7 @@ async function videoAnnounceController (req: express.Request, res: express.Respo
const { activity } = await buildAnnounceWithVideoAudience(share.Actor, share, res.locals.videoAll, undefined) const { activity } = await buildAnnounceWithVideoAudience(share.Actor, share, res.locals.videoAll, undefined)
return activityPubResponse(activityPubContextify(activity), res) return activityPubResponse(activityPubContextify(activity, 'Announce'), res)
} }
async function videoAnnouncesController (req: express.Request, res: express.Response) { async function videoAnnouncesController (req: express.Request, res: express.Response) {

View File

@ -8,13 +8,15 @@ import { pageToStartAndCount } from './core-utils'
import { URL } from 'url' import { URL } from 'url'
import { MActor, MVideoAccountLight } from '../typings/models' import { MActor, MVideoAccountLight } from '../typings/models'
function activityPubContextify <T> (data: T) { export type ContextType = 'All' | 'View' | 'Announce'
return Object.assign(data, {
'@context': [ function activityPubContextify <T> (data: T, type: ContextType = 'All') {
'https://www.w3.org/ns/activitystreams', const base = {
'https://w3id.org/security/v1', RsaSignature2017: 'https://w3id.org/security#RsaSignature2017'
{ }
RsaSignature2017: 'https://w3id.org/security#RsaSignature2017',
if (type === 'All') {
Object.assign(base, {
pt: 'https://joinpeertube.org/ns#', pt: 'https://joinpeertube.org/ns#',
sc: 'http://schema.org#', sc: 'http://schema.org#',
Hashtag: 'as:Hashtag', Hashtag: 'as:Hashtag',
@ -71,9 +73,7 @@ function activityPubContextify <T> (data: T) {
support: { support: {
'@type': 'sc:Text', '@type': 'sc:Text',
'@id': 'pt:support' '@id': 'pt:support'
}
}, },
{
likes: { likes: {
'@id': 'as:likes', '@id': 'as:likes',
'@type': '@id' '@type': '@id'
@ -94,7 +94,14 @@ function activityPubContextify <T> (data: T) {
'@id': 'as:comments', '@id': 'as:comments',
'@type': '@id' '@type': '@id'
} }
})
} }
return Object.assign({}, data, {
'@context': [
'https://www.w3.org/ns/activitystreams',
'https://w3id.org/security/v1',
base
] ]
}) })
} }
@ -148,8 +155,8 @@ async function activityPubCollectionPagination (
} }
function buildSignedActivity (byActor: MActor, data: Object) { function buildSignedActivity (byActor: MActor, data: Object, contextType?: ContextType) {
const activity = activityPubContextify(data) const activity = activityPubContextify(data, contextType)
return signJsonLDObject(byActor, activity) as Promise<Activity> return signJsonLDObject(byActor, activity) as Promise<Activity>
} }

View File

@ -28,7 +28,7 @@ async function sendVideoAnnounce (byActor: MActorLight, videoShare: MVideoShare,
logger.info('Creating job to send announce %s.', videoShare.url) logger.info('Creating job to send announce %s.', videoShare.url)
const followersException = [ byActor ] const followersException = [ byActor ]
return broadcastToFollowers(activity, byActor, actorsInvolvedInVideo, t, followersException) return broadcastToFollowers(activity, byActor, actorsInvolvedInVideo, t, followersException, 'Announce')
} }
function buildAnnounceActivity (url: string, byActor: MActorLight, object: string, audience?: ActivityAudience): ActivityAnnounce { function buildAnnounceActivity (url: string, byActor: MActorLight, object: string, audience?: ActivityAudience): ActivityAnnounce {

View File

@ -16,7 +16,7 @@ async function sendView (byActor: ActorModel, video: MVideoAccountLight, t: Tran
return buildViewActivity(url, byActor, video, audience) return buildViewActivity(url, byActor, video, audience)
} }
return sendVideoRelatedActivity(activityBuilder, { byActor, video, transaction: t }) return sendVideoRelatedActivity(activityBuilder, { byActor, video, transaction: t, contextType: 'View' })
} }
function buildViewActivity (url: string, byActor: MActorAudience, video: MVideoUrl, audience?: ActivityAudience): ActivityView { function buildViewActivity (url: string, byActor: MActorAudience, video: MVideoUrl, audience?: ActivityAudience): ActivityView {

View File

@ -8,13 +8,15 @@ import { getActorsInvolvedInVideo, getAudienceFromFollowersOf, getRemoteVideoAud
import { getServerActor } from '../../../helpers/utils' import { getServerActor } from '../../../helpers/utils'
import { afterCommitIfTransaction } from '../../../helpers/database-utils' import { afterCommitIfTransaction } from '../../../helpers/database-utils'
import { MActorWithInboxes, MActor, MActorId, MActorLight, MVideo, MVideoAccountLight } from '../../../typings/models' import { MActorWithInboxes, MActor, MActorId, MActorLight, MVideo, MVideoAccountLight } from '../../../typings/models'
import { ContextType } from '@server/helpers/activitypub'
async function sendVideoRelatedActivity (activityBuilder: (audience: ActivityAudience) => Activity, options: { async function sendVideoRelatedActivity (activityBuilder: (audience: ActivityAudience) => Activity, options: {
byActor: MActorLight byActor: MActorLight
video: MVideoAccountLight video: MVideoAccountLight
transaction?: Transaction transaction?: Transaction,
contextType?: ContextType
}) { }) {
const { byActor, video, transaction } = options const { byActor, video, transaction, contextType } = options
const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, transaction) const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, transaction)
@ -24,7 +26,7 @@ async function sendVideoRelatedActivity (activityBuilder: (audience: ActivityAud
const activity = activityBuilder(audience) const activity = activityBuilder(audience)
return afterCommitIfTransaction(transaction, () => { return afterCommitIfTransaction(transaction, () => {
return unicastTo(activity, byActor, video.VideoChannel.Account.Actor.getSharedInbox()) return unicastTo(activity, byActor, video.VideoChannel.Account.Actor.getSharedInbox(), contextType)
}) })
} }
@ -34,7 +36,7 @@ async function sendVideoRelatedActivity (activityBuilder: (audience: ActivityAud
const actorsException = [ byActor ] const actorsException = [ byActor ]
return broadcastToFollowers(activity, byActor, actorsInvolvedInVideo, transaction, actorsException) return broadcastToFollowers(activity, byActor, actorsInvolvedInVideo, transaction, actorsException, contextType)
} }
async function forwardVideoRelatedActivity ( async function forwardVideoRelatedActivity (
@ -90,11 +92,12 @@ async function broadcastToFollowers (
byActor: MActorId, byActor: MActorId,
toFollowersOf: MActorId[], toFollowersOf: MActorId[],
t: Transaction, t: Transaction,
actorsException: MActorWithInboxes[] = [] actorsException: MActorWithInboxes[] = [],
contextType?: ContextType
) { ) {
const uris = await computeFollowerUris(toFollowersOf, actorsException, t) const uris = await computeFollowerUris(toFollowersOf, actorsException, t)
return afterCommitIfTransaction(t, () => broadcastTo(uris, data, byActor)) return afterCommitIfTransaction(t, () => broadcastTo(uris, data, byActor, contextType))
} }
async function broadcastToActors ( async function broadcastToActors (
@ -102,13 +105,14 @@ async function broadcastToActors (
byActor: MActorId, byActor: MActorId,
toActors: MActor[], toActors: MActor[],
t?: Transaction, t?: Transaction,
actorsException: MActorWithInboxes[] = [] actorsException: MActorWithInboxes[] = [],
contextType?: ContextType
) { ) {
const uris = await computeUris(toActors, actorsException) const uris = await computeUris(toActors, actorsException)
return afterCommitIfTransaction(t, () => broadcastTo(uris, data, byActor)) return afterCommitIfTransaction(t, () => broadcastTo(uris, data, byActor, contextType))
} }
function broadcastTo (uris: string[], data: any, byActor: MActorId) { function broadcastTo (uris: string[], data: any, byActor: MActorId, contextType?: ContextType) {
if (uris.length === 0) return undefined if (uris.length === 0) return undefined
logger.debug('Creating broadcast job.', { uris }) logger.debug('Creating broadcast job.', { uris })
@ -116,19 +120,21 @@ function broadcastTo (uris: string[], data: any, byActor: MActorId) {
const payload = { const payload = {
uris, uris,
signatureActorId: byActor.id, signatureActorId: byActor.id,
body: data body: data,
contextType
} }
return JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload }) return JobQueue.Instance.createJob({ type: 'activitypub-http-broadcast', payload })
} }
function unicastTo (data: any, byActor: MActorId, toActorUrl: string) { function unicastTo (data: any, byActor: MActorId, toActorUrl: string, contextType?: ContextType) {
logger.debug('Creating unicast job.', { uri: toActorUrl }) logger.debug('Creating unicast job.', { uri: toActorUrl })
const payload = { const payload = {
uri: toActorUrl, uri: toActorUrl,
signatureActorId: byActor.id, signatureActorId: byActor.id,
body: data body: data,
contextType
} }
JobQueue.Instance.createJob({ type: 'activitypub-http-unicast', payload }) JobQueue.Instance.createJob({ type: 'activitypub-http-unicast', payload })

View File

@ -5,11 +5,13 @@ import { doRequest } from '../../../helpers/requests'
import { buildGlobalHeaders, buildSignedRequestOptions, computeBody } from './utils/activitypub-http-utils' import { buildGlobalHeaders, buildSignedRequestOptions, computeBody } from './utils/activitypub-http-utils'
import { BROADCAST_CONCURRENCY, JOB_REQUEST_TIMEOUT } from '../../../initializers/constants' import { BROADCAST_CONCURRENCY, JOB_REQUEST_TIMEOUT } from '../../../initializers/constants'
import { ActorFollowScoreCache } from '../../files-cache' import { ActorFollowScoreCache } from '../../files-cache'
import { ContextType } from '@server/helpers/activitypub'
export type ActivitypubHttpBroadcastPayload = { export type ActivitypubHttpBroadcastPayload = {
uris: string[] uris: string[]
signatureActorId?: number signatureActorId?: number
body: any body: any
contextType?: ContextType
} }
async function processActivityPubHttpBroadcast (job: Bull.Job) { async function processActivityPubHttpBroadcast (job: Bull.Job) {

View File

@ -4,11 +4,13 @@ import { doRequest } from '../../../helpers/requests'
import { buildGlobalHeaders, buildSignedRequestOptions, computeBody } from './utils/activitypub-http-utils' import { buildGlobalHeaders, buildSignedRequestOptions, computeBody } from './utils/activitypub-http-utils'
import { JOB_REQUEST_TIMEOUT } from '../../../initializers/constants' import { JOB_REQUEST_TIMEOUT } from '../../../initializers/constants'
import { ActorFollowScoreCache } from '../../files-cache' import { ActorFollowScoreCache } from '../../files-cache'
import { ContextType } from '@server/helpers/activitypub'
export type ActivitypubHttpUnicastPayload = { export type ActivitypubHttpUnicastPayload = {
uri: string uri: string
signatureActorId?: number signatureActorId?: number
body: any body: any
contextType?: ContextType
} }
async function processActivityPubHttpUnicast (job: Bull.Job) { async function processActivityPubHttpUnicast (job: Bull.Job) {

View File

@ -1,11 +1,11 @@
import { buildSignedActivity } from '../../../../helpers/activitypub' import { buildSignedActivity, ContextType } from '../../../../helpers/activitypub'
import { getServerActor } from '../../../../helpers/utils' import { getServerActor } from '../../../../helpers/utils'
import { ActorModel } from '../../../../models/activitypub/actor' import { ActorModel } from '../../../../models/activitypub/actor'
import { sha256 } from '../../../../helpers/core-utils' import { sha256 } from '../../../../helpers/core-utils'
import { HTTP_SIGNATURE } from '../../../../initializers/constants' import { HTTP_SIGNATURE } from '../../../../initializers/constants'
import { MActor } from '../../../../typings/models' import { MActor } from '../../../../typings/models'
type Payload = { body: any, signatureActorId?: number } type Payload = { body: any, contextType?: ContextType, signatureActorId?: number }
async function computeBody (payload: Payload) { async function computeBody (payload: Payload) {
let body = payload.body let body = payload.body
@ -13,7 +13,7 @@ async function computeBody (payload: Payload) {
if (payload.signatureActorId) { if (payload.signatureActorId) {
const actorSignature = await ActorModel.load(payload.signatureActorId) const actorSignature = await ActorModel.load(payload.signatureActorId)
if (!actorSignature) throw new Error('Unknown signature actor id.') if (!actorSignature) throw new Error('Unknown signature actor id.')
body = await buildSignedActivity(actorSignature, payload.body) body = await buildSignedActivity(actorSignature, payload.body, payload.contextType)
} }
return body return body