+ }) as MActor
+}
+
+async function updateActorInstance (actorInstance: ActorModel, attributes: ActivityPubActor) {
+ const followersCount = await fetchActorTotalItems(attributes.followers)
+ const followingCount = await fetchActorTotalItems(attributes.following)
+
+ actorInstance.type = attributes.type
+ actorInstance.preferredUsername = attributes.preferredUsername
+ actorInstance.url = attributes.id
+ actorInstance.publicKey = attributes.publicKey.publicKeyPem
+ actorInstance.followersCount = followersCount
+ actorInstance.followingCount = followingCount
+ actorInstance.inboxUrl = attributes.inbox
+ actorInstance.outboxUrl = attributes.outbox
+ actorInstance.sharedInboxUrl = attributes.endpoints.sharedInbox
+ actorInstance.followersUrl = attributes.followers
+ actorInstance.followingUrl = attributes.following
+}
+
+type AvatarInfo = { name: string, onDisk: boolean, fileUrl: string }
+async function updateActorAvatarInstance (actor: MActorDefault, info: AvatarInfo, t: Transaction) {
+ if (info.name !== undefined) {
+ if (actor.avatarId) {
+ try {
+ await actor.Avatar.destroy({ transaction: t })
+ } catch (err) {
+ logger.error('Cannot remove old avatar of actor %s.', actor.url, { err })
+ }
+ }
+
+ const avatar = await AvatarModel.create({
+ filename: info.name,
+ onDisk: info.onDisk,
+ fileUrl: info.fileUrl
+ }, { transaction: t })
+
+ actor.avatarId = avatar.id
+ actor.Avatar = avatar
+ }
+
+ return actor
+}
+
+async function fetchActorTotalItems (url: string) {
+ const options = {
+ uri: url,
+ method: 'GET',
+ json: true,
+ activityPub: true
+ }
+
+ try {
+ const { body } = await doRequest(options)
+ return body.totalItems ? body.totalItems : 0
+ } catch (err) {
+ logger.warn('Cannot fetch remote actor count %s.', url, { err })
+ return 0
+ }
+}
+
+async function getAvatarInfoIfExists (actorJSON: ActivityPubActor) {
+ if (
+ actorJSON.icon && actorJSON.icon.type === 'Image' && MIMETYPES.IMAGE.MIMETYPE_EXT[actorJSON.icon.mediaType] !== undefined &&
+ isActivityPubUrlValid(actorJSON.icon.url)
+ ) {
+ const extension = MIMETYPES.IMAGE.MIMETYPE_EXT[actorJSON.icon.mediaType]
+
+ return {
+ name: uuidv4() + extension,
+ fileUrl: actorJSON.icon.url
+ }
+ }
+
+ return undefined
+}
+
+async function addFetchOutboxJob (actor: Pick<ActorModel, 'id' | 'outboxUrl'>) {
+ // Don't fetch ourselves
+ const serverActor = await getServerActor()
+ if (serverActor.id === actor.id) {
+ logger.error('Cannot fetch our own outbox!')
+ return undefined
+ }
+
+ const payload = {
+ uri: actor.outboxUrl,
+ type: 'activity' as 'activity'
+ }
+
+ return JobQueue.Instance.createJob({ type: 'activitypub-http-fetcher', payload })
+}
+
+async function refreshActorIfNeeded <T extends MActorFull | MActorAccountChannelId> (
+ actorArg: T,
+ fetchedType: ActorFetchByUrlType
+): Promise<{ actor: T | MActorFull, refreshed: boolean }> {
+ if (!actorArg.isOutdated()) return { actor: actorArg, refreshed: false }
+
+ // We need more attributes
+ const actor = fetchedType === 'all'
+ ? actorArg as MActorFull
+ : await ActorModel.loadByUrlAndPopulateAccountAndChannel(actorArg.url)
+
+ try {
+ let actorUrl: string
+ try {
+ actorUrl = await getUrlFromWebfinger(actor.preferredUsername + '@' + actor.getHost())
+ } catch (err) {
+ logger.warn('Cannot get actor URL from webfinger, keeping the old one.', err)
+ actorUrl = actor.url
+ }
+
+ const { result, statusCode } = await fetchRemoteActor(actorUrl)
+
+ if (statusCode === 404) {
+ logger.info('Deleting actor %s because there is a 404 in refresh actor.', actor.url)
+ actor.Account ? actor.Account.destroy() : actor.VideoChannel.destroy()
+ return { actor: undefined, refreshed: false }
+ }
+
+ if (result === undefined) {
+ logger.warn('Cannot fetch remote actor in refresh actor.')
+ return { actor, refreshed: false }
+ }
+
+ return sequelizeTypescript.transaction(async t => {
+ updateInstanceWithAnother(actor, result.actor)
+
+ if (result.avatar !== undefined) {
+ const avatarInfo = {
+ name: result.avatar.name,
+ fileUrl: result.avatar.fileUrl,
+ onDisk: false
+ }
+
+ await updateActorAvatarInstance(actor, avatarInfo, t)
+ }
+
+ // Force update
+ actor.setDataValue('updatedAt', new Date())
+ await actor.save({ transaction: t })
+
+ if (actor.Account) {
+ actor.Account.name = result.name
+ actor.Account.description = result.summary
+
+ await actor.Account.save({ transaction: t })
+ } else if (actor.VideoChannel) {
+ actor.VideoChannel.name = result.name
+ actor.VideoChannel.description = result.summary
+ actor.VideoChannel.support = result.support
+
+ await actor.VideoChannel.save({ transaction: t })
+ }
+
+ return { refreshed: true, actor }
+ })
+ } catch (err) {
+ logger.warn('Cannot refresh actor %s.', actor.url, { err })
+ return { actor, refreshed: false }
+ }