From 1ea7da819e5bfae7b443ed722c18c4165d101439 Mon Sep 17 00:00:00 2001 From: Rigel Kent Date: Wed, 13 Jan 2021 09:12:55 +0100 Subject: add ability to remove one's avatar for account and channels (#3467) * add ability to remove one's avatar for account and channels * add ability to remove one's avatar for account and channels * only display avatar edition options after input change --- server/lib/activitypub/actor.ts | 14 ++++++++++++++ server/lib/avatar.ts | 22 +++++++++++++++++++--- 2 files changed, 33 insertions(+), 3 deletions(-) (limited to 'server/lib') diff --git a/server/lib/activitypub/actor.ts b/server/lib/activitypub/actor.ts index 52547536c..086d656f9 100644 --- a/server/lib/activitypub/actor.ts +++ b/server/lib/activitypub/actor.ts @@ -199,6 +199,19 @@ async function updateActorAvatarInstance (actor: MActorDefault, info: AvatarInfo return actor } +async function deleteActorAvatarInstance (actor: MActorDefault, t: Transaction) { + try { + await actor.Avatar.destroy({ transaction: t }) + } catch (err) { + logger.error('Cannot remove old avatar of actor %s.', actor.url, { err }) + } + + actor.avatarId = null + actor.Avatar = null + + return actor +} + async function fetchActorTotalItems (url: string) { const options = { uri: url, @@ -337,6 +350,7 @@ export { fetchActorTotalItems, getAvatarInfoIfExists, updateActorInstance, + deleteActorAvatarInstance, refreshActorIfNeeded, updateActorAvatarInstance, addFetchOutboxJob diff --git a/server/lib/avatar.ts b/server/lib/avatar.ts index be6657b6f..9d59a4966 100644 --- a/server/lib/avatar.ts +++ b/server/lib/avatar.ts @@ -1,7 +1,7 @@ import 'multer' import { sendUpdateActor } from './activitypub/send' import { AVATARS_SIZE, LRU_CACHE, QUEUE_CONCURRENCY } from '../initializers/constants' -import { updateActorAvatarInstance } from './activitypub/actor' +import { updateActorAvatarInstance, deleteActorAvatarInstance } from './activitypub/actor' import { processImage } from '../helpers/image-utils' import { extname, join } from 'path' import { retryTransactionWrapper } from '../helpers/database-utils' @@ -14,8 +14,8 @@ import { downloadImage } from '../helpers/requests' import { MAccountDefault, MChannelDefault } from '../types/models' async function updateActorAvatarFile ( - avatarPhysicalFile: Express.Multer.File, - accountOrChannel: MAccountDefault | MChannelDefault + accountOrChannel: MAccountDefault | MChannelDefault, + avatarPhysicalFile: Express.Multer.File ) { const extension = extname(avatarPhysicalFile.filename) const avatarName = uuidv4() + extension @@ -40,6 +40,21 @@ async function updateActorAvatarFile ( }) } +async function deleteActorAvatarFile ( + accountOrChannel: MAccountDefault | MChannelDefault +) { + return retryTransactionWrapper(() => { + return sequelizeTypescript.transaction(async t => { + const updatedActor = await deleteActorAvatarInstance(accountOrChannel.Actor, t) + await updatedActor.save({ transaction: t }) + + await sendUpdateActor(accountOrChannel, t) + + return updatedActor.Avatar + }) + }) +} + type DownloadImageQueueTask = { fileUrl: string, filename: string } const downloadImageQueue = queue((task, cb) => { @@ -64,5 +79,6 @@ const avatarPathUnsafeCache = new LRUCache({ max: LRU_CACHE.AVAT export { avatarPathUnsafeCache, updateActorAvatarFile, + deleteActorAvatarFile, pushAvatarProcessInQueue } -- cgit v1.2.3