Improve activity pub actors implementation

This commit is contained in:
Chocobozzz 2018-03-22 18:40:33 +01:00
parent 9e841674da
commit 6be84cbcea
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
9 changed files with 32 additions and 12 deletions

View File

@ -1,5 +1,5 @@
import { ResultList } from '../../shared/models' import { ResultList } from '../../shared/models'
import { Activity } from '../../shared/models/activitypub' import { Activity, ActivityPubActor } from '../../shared/models/activitypub'
import { ACTIVITY_PUB } from '../initializers' import { ACTIVITY_PUB } from '../initializers'
import { ActorModel } from '../models/activitypub/actor' import { ActorModel } from '../models/activitypub/actor'
import { signObject } from './peertube-crypto' import { signObject } from './peertube-crypto'
@ -98,9 +98,16 @@ function buildSignedActivity (byActor: ActorModel, data: Object) {
return signObject(byActor, activity) as Promise<Activity> return signObject(byActor, activity) as Promise<Activity>
} }
function getActorUrl (activityActor: string | ActivityPubActor) {
if (typeof activityActor === 'string') return activityActor
return activityActor.id
}
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
export { export {
getActorUrl,
activityPubContextify, activityPubContextify,
activityPubCollectionPagination, activityPubCollectionPagination,
activityPubCollection, activityPubCollection,

View File

@ -26,7 +26,7 @@ function isRootActivityValid (activity: any) {
) || ) ||
( (
isActivityPubUrlValid(activity.id) && isActivityPubUrlValid(activity.id) &&
isActivityPubUrlValid(activity.actor) (isActivityPubUrlValid(activity.actor) || isActivityPubUrlValid(activity.actor.id))
) )
} }

View File

@ -24,7 +24,7 @@ function isBaseActivityValid (activity: any, type: string) {
return (activity['@context'] === undefined || Array.isArray(activity['@context'])) && return (activity['@context'] === undefined || Array.isArray(activity['@context'])) &&
activity.type === type && activity.type === type &&
isActivityPubUrlValid(activity.id) && isActivityPubUrlValid(activity.id) &&
isActivityPubUrlValid(activity.actor) && (isActivityPubUrlValid(activity.actor) || isActivityPubUrlValid(activity.actor.id)) &&
( (
activity.to === undefined || activity.to === undefined ||
(Array.isArray(activity.to) && activity.to.every(t => isActivityPubUrlValid(t))) (Array.isArray(activity.to) && activity.to.every(t => isActivityPubUrlValid(t)))

View File

@ -5,6 +5,7 @@ import * as url from 'url'
import * as uuidv4 from 'uuid/v4' import * as uuidv4 from 'uuid/v4'
import { ActivityPubActor, ActivityPubActorType } from '../../../shared/models/activitypub' import { ActivityPubActor, ActivityPubActorType } from '../../../shared/models/activitypub'
import { ActivityPubAttributedTo } from '../../../shared/models/activitypub/objects' import { ActivityPubAttributedTo } from '../../../shared/models/activitypub/objects'
import { getActorUrl } from '../../helpers/activitypub'
import { isActorObjectValid } from '../../helpers/custom-validators/activitypub/actor' import { isActorObjectValid } from '../../helpers/custom-validators/activitypub/actor'
import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
import { retryTransactionWrapper, updateInstanceWithAnother } from '../../helpers/database-utils' import { retryTransactionWrapper, updateInstanceWithAnother } from '../../helpers/database-utils'
@ -34,7 +35,9 @@ function setAsyncActorKeys (actor: ActorModel) {
}) })
} }
async function getOrCreateActorAndServerAndModel (actorUrl: string, recurseIfNeeded = true) { async function getOrCreateActorAndServerAndModel (activityActor: string | ActivityPubActor, recurseIfNeeded = true) {
const actorUrl = getActorUrl(activityActor)
let actor = await ActorModel.loadByUrl(actorUrl) let actor = await ActorModel.loadByUrl(actorUrl)
// We don't have this actor in our database, fetch it on remote // We don't have this actor in our database, fetch it on remote

View File

@ -1,4 +1,5 @@
import { ActivityAccept } from '../../../../shared/models/activitypub' import { ActivityAccept } from '../../../../shared/models/activitypub'
import { getActorUrl } from '../../../helpers/activitypub'
import { ActorModel } from '../../../models/activitypub/actor' import { ActorModel } from '../../../models/activitypub/actor'
import { ActorFollowModel } from '../../../models/activitypub/actor-follow' import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
import { addFetchOutboxJob } from '../fetch' import { addFetchOutboxJob } from '../fetch'
@ -6,7 +7,8 @@ import { addFetchOutboxJob } from '../fetch'
async function processAcceptActivity (activity: ActivityAccept, inboxActor?: ActorModel) { async function processAcceptActivity (activity: ActivityAccept, inboxActor?: ActorModel) {
if (inboxActor === undefined) throw new Error('Need to accept on explicit inbox.') if (inboxActor === undefined) throw new Error('Need to accept on explicit inbox.')
const targetActor = await ActorModel.loadByUrl(activity.actor) const actorUrl = getActorUrl(activity.actor)
const targetActor = await ActorModel.loadByUrl(actorUrl)
return processAccept(inboxActor, targetActor) return processAccept(inboxActor, targetActor)
} }

View File

@ -1,4 +1,5 @@
import { ActivityReject } from '../../../../shared/models/activitypub/activity' import { ActivityReject } from '../../../../shared/models/activitypub/activity'
import { getActorUrl } from '../../../helpers/activitypub'
import { sequelizeTypescript } from '../../../initializers' import { sequelizeTypescript } from '../../../initializers'
import { ActorModel } from '../../../models/activitypub/actor' import { ActorModel } from '../../../models/activitypub/actor'
import { ActorFollowModel } from '../../../models/activitypub/actor-follow' import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
@ -6,7 +7,8 @@ import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
async function processRejectActivity (activity: ActivityReject, inboxActor?: ActorModel) { async function processRejectActivity (activity: ActivityReject, inboxActor?: ActorModel) {
if (inboxActor === undefined) throw new Error('Need to reject on explicit inbox.') if (inboxActor === undefined) throw new Error('Need to reject on explicit inbox.')
const targetActor = await ActorModel.loadByUrl(activity.actor) const actorUrl = getActorUrl(activity.actor)
const targetActor = await ActorModel.loadByUrl(actorUrl)
return processReject(inboxActor, targetActor) return processReject(inboxActor, targetActor)
} }

View File

@ -1,5 +1,6 @@
import { ActivityFollow, ActivityLike, ActivityUndo } from '../../../../shared/models/activitypub' import { ActivityFollow, ActivityLike, ActivityUndo } from '../../../../shared/models/activitypub'
import { DislikeObject } from '../../../../shared/models/activitypub/objects' import { DislikeObject } from '../../../../shared/models/activitypub/objects'
import { getActorUrl } from '../../../helpers/activitypub'
import { retryTransactionWrapper } from '../../../helpers/database-utils' import { retryTransactionWrapper } from '../../../helpers/database-utils'
import { logger } from '../../../helpers/logger' import { logger } from '../../../helpers/logger'
import { sequelizeTypescript } from '../../../initializers' import { sequelizeTypescript } from '../../../initializers'
@ -13,12 +14,14 @@ import { getOrCreateAccountAndVideoAndChannel } from '../videos'
async function processUndoActivity (activity: ActivityUndo) { async function processUndoActivity (activity: ActivityUndo) {
const activityToUndo = activity.object const activityToUndo = activity.object
const actorUrl = getActorUrl(activity.actor)
if (activityToUndo.type === 'Like') { if (activityToUndo.type === 'Like') {
return processUndoLike(activity.actor, activity) return processUndoLike(actorUrl, activity)
} else if (activityToUndo.type === 'Create' && activityToUndo.object.type === 'Dislike') { } else if (activityToUndo.type === 'Create' && activityToUndo.object.type === 'Dislike') {
return processUndoDislike(activity.actor, activity) return processUndoDislike(actorUrl, activity)
} else if (activityToUndo.type === 'Follow') { } else if (activityToUndo.type === 'Follow') {
return processUndoFollow(activity.actor, activityToUndo) return processUndoFollow(actorUrl, activityToUndo)
} }
logger.warn('Unknown activity object type %s -> %s when undo activity.', activityToUndo.type, { activity: activity.id }) logger.warn('Unknown activity object type %s -> %s when undo activity.', activityToUndo.type, { activity: activity.id })

View File

@ -1,4 +1,5 @@
import { Activity, ActivityType } from '../../../../shared/models/activitypub' import { Activity, ActivityType } from '../../../../shared/models/activitypub'
import { getActorUrl } from '../../../helpers/activitypub'
import { logger } from '../../../helpers/logger' import { logger } from '../../../helpers/logger'
import { ActorModel } from '../../../models/activitypub/actor' import { ActorModel } from '../../../models/activitypub/actor'
import { processAcceptActivity } from './process-accept' import { processAcceptActivity } from './process-accept'
@ -25,9 +26,11 @@ const processActivity: { [ P in ActivityType ]: (activity: Activity, inboxActor?
async function processActivities (activities: Activity[], signatureActor?: ActorModel, inboxActor?: ActorModel) { async function processActivities (activities: Activity[], signatureActor?: ActorModel, inboxActor?: ActorModel) {
for (const activity of activities) { for (const activity of activities) {
const actorUrl = getActorUrl(activity.actor)
// When we fetch remote data, we don't have signature // When we fetch remote data, we don't have signature
if (signatureActor && activity.actor !== signatureActor.url) { if (signatureActor && actorUrl !== signatureActor.url) {
logger.warn('Signature mismatch between %s and %s.', activity.actor, signatureActor.url) logger.warn('Signature mismatch between %s and %s.', actorUrl, signatureActor.url)
continue continue
} }

View File

@ -22,7 +22,7 @@ export interface BaseActivity {
id: string id: string
to?: string[] to?: string[]
cc?: string[] cc?: string[]
actor: string actor: string | ActivityPubActor
type: ActivityType type: ActivityType
signature?: ActivityPubSignature signature?: ActivityPubSignature
} }