From d0800f7661f13fabe7bb6f4aa0ea50764f106405 Mon Sep 17 00:00:00 2001 From: kontrollanten <6680299+kontrollanten@users.noreply.github.com> Date: Mon, 28 Feb 2022 08:34:43 +0100 Subject: 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 --- server/models/account/account-blocklist.ts | 83 +++++++++++++---------------- server/models/account/account-video-rate.ts | 56 +++++++++++-------- server/models/account/account.ts | 59 ++++++++++---------- 3 files changed, 103 insertions(+), 95 deletions(-) (limited to 'server/models/account') diff --git a/server/models/account/account-blocklist.ts b/server/models/account/account-blocklist.ts index 1162962bf..a7b8db076 100644 --- a/server/models/account/account-blocklist.ts +++ b/server/models/account/account-blocklist.ts @@ -1,7 +1,7 @@ -import { Op, QueryTypes } from 'sequelize' -import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript' +import { FindOptions, Op, QueryTypes } from 'sequelize' +import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript' import { handlesToNameAndHost } from '@server/helpers/actors' -import { MAccountBlocklist, MAccountBlocklistAccounts, MAccountBlocklistFormattable } from '@server/types/models' +import { MAccountBlocklist, MAccountBlocklistFormattable } from '@server/types/models' import { AttributesOnly } from '@shared/typescript-utils' import { AccountBlock } from '../../../shared/models' import { ActorModel } from '../actor/actor' @@ -9,27 +9,6 @@ import { ServerModel } from '../server/server' import { createSafeIn, getSort, searchAttribute } from '../utils' import { AccountModel } from './account' -enum ScopeNames { - WITH_ACCOUNTS = 'WITH_ACCOUNTS' -} - -@Scopes(() => ({ - [ScopeNames.WITH_ACCOUNTS]: { - include: [ - { - model: AccountModel, - required: true, - as: 'ByAccount' - }, - { - model: AccountModel, - required: true, - as: 'BlockedAccount' - } - ] - } -})) - @Table({ tableName: 'accountBlocklist', indexes: [ @@ -123,33 +102,45 @@ export class AccountBlocklistModel extends Model { + const query: FindOptions = { + offset: start, + limit: count, + order: getSort(sort), + where: { accountId } + } - const where = { - accountId - } + if (search) { + Object.assign(query.where, { + [Op.or]: [ + searchAttribute(search, '$BlockedAccount.name$'), + searchAttribute(search, '$BlockedAccount.Actor.url$') + ] + }) + } - if (search) { - Object.assign(where, { - [Op.or]: [ - searchAttribute(search, '$BlockedAccount.name$'), - searchAttribute(search, '$BlockedAccount.Actor.url$') + if (forCount !== true) { + query.include = [ + { + model: AccountModel, + required: true, + as: 'ByAccount' + }, + { + model: AccountModel, + required: true, + as: 'BlockedAccount' + } ] - }) - } + } - Object.assign(query, { where }) + return query + } - return AccountBlocklistModel - .scope([ ScopeNames.WITH_ACCOUNTS ]) - .findAndCountAll(query) - .then(({ rows, count }) => { - return { total: count, data: rows } - }) + return Promise.all([ + AccountBlocklistModel.count(getQuery(true)), + AccountBlocklistModel.findAll(getQuery(false)) + ]).then(([ total, data ]) => ({ total, data })) } static listHandlesBlockedBy (accountIds: number[]): Promise { diff --git a/server/models/account/account-video-rate.ts b/server/models/account/account-video-rate.ts index e89d31adf..7303651eb 100644 --- a/server/models/account/account-video-rate.ts +++ b/server/models/account/account-video-rate.ts @@ -121,29 +121,40 @@ export class AccountVideoRateModel extends Model { + const query: FindOptions = { + offset: options.start, + limit: options.count, + order: getSort(options.sort), + where: { + accountId: options.accountId } - ] + } + + if (options.type) query.where['type'] = options.type + + if (forCount !== true) { + query.include = [ + { + model: VideoModel, + required: true, + include: [ + { + model: VideoChannelModel.scope({ method: [ VideoChannelScopeNames.SUMMARY, { withAccount: true } as SummaryOptions ] }), + required: true + } + ] + } + ] + } + + return query } - if (options.type) query.where['type'] = options.type - return AccountVideoRateModel.findAndCountAll(query) + return Promise.all([ + AccountVideoRateModel.count(getQuery(true)), + AccountVideoRateModel.findAll(getQuery(false)) + ]).then(([ total, data ]) => ({ total, data })) } static listRemoteRateUrlsOfLocalVideos () { @@ -232,7 +243,10 @@ export class AccountVideoRateModel extends Model(query) + return Promise.all([ + AccountVideoRateModel.count(query), + AccountVideoRateModel.findAll(query) + ]).then(([ total, data ]) => ({ total, data })) } static cleanOldRatesOf (videoId: number, type: VideoRateType, beforeUpdatedAt: Date) { diff --git a/server/models/account/account.ts b/server/models/account/account.ts index 619a598dd..8a7dfba94 100644 --- a/server/models/account/account.ts +++ b/server/models/account/account.ts @@ -54,6 +54,7 @@ export type SummaryOptions = { whereActor?: WhereOptions whereServer?: WhereOptions withAccountBlockerIds?: number[] + forCount?: boolean } @DefaultScope(() => ({ @@ -73,22 +74,24 @@ export type SummaryOptions = { where: options.whereServer } - const queryInclude: Includeable[] = [ - { - attributes: [ 'id', 'preferredUsername', 'url', 'serverId', 'avatarId' ], - model: ActorModel.unscoped(), - required: options.actorRequired ?? true, - where: options.whereActor, - include: [ - serverInclude, + const actorInclude: Includeable = { + attributes: [ 'id', 'preferredUsername', 'url', 'serverId' ], + model: ActorModel.unscoped(), + required: options.actorRequired ?? true, + where: options.whereActor, + include: [ serverInclude ] + } - { - model: ActorImageModel.unscoped(), - as: 'Avatar', - required: false - } - ] - } + if (options.forCount !== true) { + actorInclude.include.push({ + model: ActorImageModel, + as: 'Avatars', + required: false + }) + } + + const queryInclude: Includeable[] = [ + actorInclude ] const query: FindOptions = { @@ -349,13 +352,10 @@ export class AccountModel extends Model>> { order: getSort(sort) } - return AccountModel.findAndCountAll(query) - .then(({ rows, count }) => { - return { - data: rows, - total: count - } - }) + return Promise.all([ + AccountModel.count(), + AccountModel.findAll(query) + ]).then(([ total, data ]) => ({ total, data })) } static loadAccountIdFromVideo (videoId: number): Promise { @@ -407,16 +407,15 @@ export class AccountModel extends Model>> { } toFormattedJSON (this: MAccountFormattable): Account { - const actor = this.Actor.toFormattedJSON() - const account = { + return { + ...this.Actor.toFormattedJSON(), + id: this.id, displayName: this.getDisplayName(), description: this.description, updatedAt: this.updatedAt, - userId: this.userId ? this.userId : undefined + userId: this.userId ?? undefined } - - return Object.assign(actor, account) } toFormattedSummaryJSON (this: MAccountSummaryFormattable): AccountSummary { @@ -424,10 +423,14 @@ export class AccountModel extends Model>> { return { id: this.id, - name: actor.name, displayName: this.getDisplayName(), + + name: actor.name, url: actor.url, host: actor.host, + avatars: actor.avatars, + + // TODO: remove, deprecated in 4.2 avatar: actor.avatar } } -- cgit v1.2.3