mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2025-10-05 02:39:33 +02:00
Implement avatar miniatures (#4639)
* client: remove unused file * refactor(client/my-actor-avatar): size from input Read size from component input instead of scss, to make it possible to use smaller avatar images when implemented. * implement avatar miniatures close #4560 * fix(test): max file size * fix(search-index): normalize res acc to avatarMini * refactor avatars to an array * client/search: resize channel avatar to 120 * refactor(client/videos): remove unused function * client(actor-avatar): set default size * fix tests and avatars full result When findOne is used only an array containting one avatar is returned. * update migration version and version notations * server/search: harmonize normalizing * Cleanup avatar miniature PR Co-authored-by: Chocobozzz <me@florianbigard.com>
This commit is contained in:
parent
5cad2ca9db
commit
d0800f7661
150 changed files with 2027 additions and 1276 deletions
101
server/models/shared/model-builder.ts
Normal file
101
server/models/shared/model-builder.ts
Normal file
|
@ -0,0 +1,101 @@
|
|||
import { isPlainObject } from 'lodash'
|
||||
import { Model as SequelizeModel, Sequelize } from 'sequelize'
|
||||
import { logger } from '@server/helpers/logger'
|
||||
|
||||
export class ModelBuilder <T extends SequelizeModel> {
|
||||
private readonly modelRegistry = new Map<string, T>()
|
||||
|
||||
constructor (private readonly sequelize: Sequelize) {
|
||||
|
||||
}
|
||||
|
||||
createModels (jsonArray: any[], baseModelName: string): T[] {
|
||||
const result: T[] = []
|
||||
|
||||
for (const json of jsonArray) {
|
||||
const { created, model } = this.createModel(json, baseModelName, json.id + '.' + baseModelName)
|
||||
|
||||
if (created) result.push(model)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
private createModel (json: any, modelName: string, keyPath: string) {
|
||||
if (!json.id) return { created: false, model: null }
|
||||
|
||||
const { created, model } = this.createOrFindModel(json, modelName, keyPath)
|
||||
|
||||
for (const key of Object.keys(json)) {
|
||||
const value = json[key]
|
||||
if (!value) continue
|
||||
|
||||
// Child model
|
||||
if (isPlainObject(value)) {
|
||||
const { created, model: subModel } = this.createModel(value, key, keyPath + '.' + json.id + '.' + key)
|
||||
if (!created || !subModel) continue
|
||||
|
||||
const Model = this.findModelBuilder(modelName)
|
||||
const association = Model.associations[key]
|
||||
|
||||
if (!association) {
|
||||
logger.error('Cannot find association %s of model %s', key, modelName, { associations: Object.keys(Model.associations) })
|
||||
continue
|
||||
}
|
||||
|
||||
if (association.isMultiAssociation) {
|
||||
if (!Array.isArray(model[key])) model[key] = []
|
||||
|
||||
model[key].push(subModel)
|
||||
} else {
|
||||
model[key] = subModel
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { created, model }
|
||||
}
|
||||
|
||||
private createOrFindModel (json: any, modelName: string, keyPath: string) {
|
||||
const registryKey = this.getModelRegistryKey(json, keyPath)
|
||||
if (this.modelRegistry.has(registryKey)) {
|
||||
return {
|
||||
created: false,
|
||||
model: this.modelRegistry.get(registryKey)
|
||||
}
|
||||
}
|
||||
|
||||
const Model = this.findModelBuilder(modelName)
|
||||
|
||||
if (!Model) {
|
||||
logger.error(
|
||||
'Cannot build model %s that does not exist', this.buildSequelizeModelName(modelName),
|
||||
{ existing: this.sequelize.modelManager.all.map(m => m.name) }
|
||||
)
|
||||
return undefined
|
||||
}
|
||||
|
||||
// FIXME: typings
|
||||
const model = new (Model as any)(json)
|
||||
this.modelRegistry.set(registryKey, model)
|
||||
|
||||
return { created: true, model }
|
||||
}
|
||||
|
||||
private findModelBuilder (modelName: string) {
|
||||
return this.sequelize.modelManager.getModel(this.buildSequelizeModelName(modelName))
|
||||
}
|
||||
|
||||
private buildSequelizeModelName (modelName: string) {
|
||||
if (modelName === 'Avatars') return 'ActorImageModel'
|
||||
if (modelName === 'ActorFollowing') return 'ActorModel'
|
||||
if (modelName === 'ActorFollower') return 'ActorModel'
|
||||
if (modelName === 'FlaggedAccount') return 'AccountModel'
|
||||
|
||||
return modelName + 'Model'
|
||||
}
|
||||
|
||||
private getModelRegistryKey (json: any, keyPath: string) {
|
||||
return keyPath + json.id
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue