aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/local-actor.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib/local-actor.ts')
-rw-r--r--server/lib/local-actor.ts102
1 files changed, 0 insertions, 102 deletions
diff --git a/server/lib/local-actor.ts b/server/lib/local-actor.ts
deleted file mode 100644
index 611e6d0af..000000000
--- a/server/lib/local-actor.ts
+++ /dev/null
@@ -1,102 +0,0 @@
1import { remove } from 'fs-extra'
2import { join } from 'path'
3import { Transaction } from 'sequelize/types'
4import { ActorModel } from '@server/models/actor/actor'
5import { getLowercaseExtension } from '@shared/core-utils'
6import { buildUUID } from '@shared/extra-utils'
7import { ActivityPubActorType, ActorImageType } from '@shared/models'
8import { retryTransactionWrapper } from '../helpers/database-utils'
9import { CONFIG } from '../initializers/config'
10import { ACTOR_IMAGES_SIZE, WEBSERVER } from '../initializers/constants'
11import { sequelizeTypescript } from '../initializers/database'
12import { MAccountDefault, MActor, MChannelDefault } from '../types/models'
13import { deleteActorImages, updateActorImages } from './activitypub/actors'
14import { sendUpdateActor } from './activitypub/send'
15import { processImageFromWorker } from './worker/parent-process'
16
17export function buildActorInstance (type: ActivityPubActorType, url: string, preferredUsername: string) {
18 return new ActorModel({
19 type,
20 url,
21 preferredUsername,
22 publicKey: null,
23 privateKey: null,
24 followersCount: 0,
25 followingCount: 0,
26 inboxUrl: url + '/inbox',
27 outboxUrl: url + '/outbox',
28 sharedInboxUrl: WEBSERVER.URL + '/inbox',
29 followersUrl: url + '/followers',
30 followingUrl: url + '/following'
31 }) as MActor
32}
33
34export async function updateLocalActorImageFiles (
35 accountOrChannel: MAccountDefault | MChannelDefault,
36 imagePhysicalFile: Express.Multer.File,
37 type: ActorImageType
38) {
39 const processImageSize = async (imageSize: { width: number, height: number }) => {
40 const extension = getLowercaseExtension(imagePhysicalFile.filename)
41
42 const imageName = buildUUID() + extension
43 const destination = join(CONFIG.STORAGE.ACTOR_IMAGES_DIR, imageName)
44 await processImageFromWorker({ path: imagePhysicalFile.path, destination, newSize: imageSize, keepOriginal: true })
45
46 return {
47 imageName,
48 imageSize
49 }
50 }
51
52 const processedImages = await Promise.all(ACTOR_IMAGES_SIZE[type].map(processImageSize))
53 await remove(imagePhysicalFile.path)
54
55 return retryTransactionWrapper(() => sequelizeTypescript.transaction(async t => {
56 const actorImagesInfo = processedImages.map(({ imageName, imageSize }) => ({
57 name: imageName,
58 fileUrl: null,
59 height: imageSize.height,
60 width: imageSize.width,
61 onDisk: true
62 }))
63
64 const updatedActor = await updateActorImages(accountOrChannel.Actor, type, actorImagesInfo, t)
65 await updatedActor.save({ transaction: t })
66
67 await sendUpdateActor(accountOrChannel, t)
68
69 return type === ActorImageType.AVATAR
70 ? updatedActor.Avatars
71 : updatedActor.Banners
72 }))
73}
74
75export async function deleteLocalActorImageFile (accountOrChannel: MAccountDefault | MChannelDefault, type: ActorImageType) {
76 return retryTransactionWrapper(() => {
77 return sequelizeTypescript.transaction(async t => {
78 const updatedActor = await deleteActorImages(accountOrChannel.Actor, type, t)
79 await updatedActor.save({ transaction: t })
80
81 await sendUpdateActor(accountOrChannel, t)
82
83 return updatedActor.Avatars
84 })
85 })
86}
87
88// ---------------------------------------------------------------------------
89
90export async function findAvailableLocalActorName (baseActorName: string, transaction?: Transaction) {
91 let actor = await ActorModel.loadLocalByName(baseActorName, transaction)
92 if (!actor) return baseActorName
93
94 for (let i = 1; i < 30; i++) {
95 const name = `${baseActorName}-${i}`
96
97 actor = await ActorModel.loadLocalByName(name, transaction)
98 if (!actor) return name
99 }
100
101 throw new Error('Cannot find available actor local name (too much iterations).')
102}