From 265ba139ebf56bbdc1c65f6ea4f367774c691fc0 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 3 Jan 2018 16:38:50 +0100 Subject: Send account activitypub update events --- server/lib/activitypub/actor.ts | 86 ++++++++++++++++++++++------------------- 1 file changed, 47 insertions(+), 39 deletions(-) (limited to 'server/lib/activitypub/actor.ts') diff --git a/server/lib/activitypub/actor.ts b/server/lib/activitypub/actor.ts index e557896e8..b6ba2cc22 100644 --- a/server/lib/activitypub/actor.ts +++ b/server/lib/activitypub/actor.ts @@ -5,13 +5,13 @@ import * as url from 'url' import * as uuidv4 from 'uuid/v4' import { ActivityPubActor, ActivityPubActorType } from '../../../shared/models/activitypub' import { ActivityPubAttributedTo } from '../../../shared/models/activitypub/objects' -import { isRemoteActorValid } from '../../helpers/custom-validators/activitypub/actor' +import { isActorObjectValid } from '../../helpers/custom-validators/activitypub/actor' import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' import { retryTransactionWrapper } from '../../helpers/database-utils' import { logger } from '../../helpers/logger' import { createPrivateAndPublicKeys } from '../../helpers/peertube-crypto' import { doRequest, doRequestAndSaveToFile } from '../../helpers/requests' -import { CONFIG, sequelizeTypescript } from '../../initializers' +import { AVATAR_MIMETYPE_EXT, CONFIG, sequelizeTypescript } from '../../initializers' import { AccountModel } from '../../models/account/account' import { ActorModel } from '../../models/activitypub/actor' import { AvatarModel } from '../../models/avatar/avatar' @@ -84,10 +84,52 @@ function buildActorInstance (type: ActivityPubActorType, url: string, preferredU }) } +async function fetchActorTotalItems (url: string) { + const options = { + uri: url, + method: 'GET', + json: true, + activityPub: true + } + + let requestResult + try { + requestResult = await doRequest(options) + } catch (err) { + logger.warn('Cannot fetch remote actor count %s.', url, err) + return undefined + } + + return requestResult.totalItems ? requestResult.totalItems : 0 +} + +async function fetchAvatarIfExists (actorJSON: ActivityPubActor) { + if ( + actorJSON.icon && actorJSON.icon.type === 'Image' && AVATAR_MIMETYPE_EXT[actorJSON.icon.mediaType] !== undefined && + isActivityPubUrlValid(actorJSON.icon.url) + ) { + const extension = AVATAR_MIMETYPE_EXT[actorJSON.icon.mediaType] + + const avatarName = uuidv4() + extension + const destPath = join(CONFIG.STORAGE.AVATARS_DIR, avatarName) + + await doRequestAndSaveToFile({ + method: 'GET', + uri: actorJSON.icon.url + }, destPath) + + return avatarName + } + + return undefined +} + export { getOrCreateActorAndServerAndModel, buildActorInstance, - setAsyncActorKeys + setAsyncActorKeys, + fetchActorTotalItems, + fetchAvatarIfExists } // --------------------------------------------------------------------------- @@ -166,7 +208,7 @@ async function fetchRemoteActor (actorUrl: string): Promise