X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Factivitypub%2Factor.ts;h=09d96b24dc29fd028d6328fba8e0ddbe2a1faa75;hb=f479685678406a5df864d89615b33d29085ebfc6;hp=e547d2c0c19b00cfd932d318bf4e492675c96e64;hpb=dafbad0caef873bc01c0ce43748b01641d1d8795;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/activitypub/actor.ts b/server/models/activitypub/actor.ts index e547d2c0c..09d96b24d 100644 --- a/server/models/activitypub/actor.ts +++ b/server/models/activitypub/actor.ts @@ -1,5 +1,6 @@ import { values } from 'lodash' import { extname } from 'path' +import { literal, Op, Transaction } from 'sequelize' import { AllowNull, BelongsTo, @@ -16,8 +17,9 @@ import { Table, UpdatedAt } from 'sequelize-typescript' +import { ModelCache } from '@server/models/model-cache' import { ActivityIconObject, ActivityPubActorType } from '../../../shared/models/activitypub' -import { Avatar } from '../../../shared/models/avatars/avatar.model' +import { ActorImage } from '../../../shared/models/actors/actor-image.model' import { activityPubContextify } from '../../helpers/activitypub' import { isActorFollowersCountValid, @@ -28,13 +30,6 @@ import { } from '../../helpers/custom-validators/activitypub/actor' import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' import { ACTIVITY_PUB, ACTIVITY_PUB_ACTOR_TYPES, CONSTRAINTS_FIELDS, SERVER_ACTOR_NAME, WEBSERVER } from '../../initializers/constants' -import { AccountModel } from '../account/account' -import { AvatarModel } from '../avatar/avatar' -import { ServerModel } from '../server/server' -import { isOutdated, throwIfNotValid } from '../utils' -import { VideoChannelModel } from '../video/video-channel' -import { ActorFollowModel } from './actor-follow' -import { VideoModel } from '../video/video' import { MActor, MActorAccountChannelId, @@ -43,12 +38,17 @@ import { MActorFull, MActorHost, MActorServer, - MActorSummaryFormattable, MActorUrl, + MActorSummaryFormattable, + MActorUrl, MActorWithInboxes -} from '../../typings/models' -import * as Bluebird from 'bluebird' -import { Op, Transaction, literal } from 'sequelize' -import { ModelCache } from '@server/models/model-cache' +} from '../../types/models' +import { AccountModel } from '../account/account' +import { ActorImageModel } from '../account/actor-image' +import { ServerModel } from '../server/server' +import { isOutdated, throwIfNotValid } from '../utils' +import { VideoModel } from '../video/video' +import { VideoChannelModel } from '../video/video-channel' +import { ActorFollowModel } from './actor-follow' enum ScopeNames { FULL = 'FULL' @@ -62,7 +62,6 @@ export const unusedActorAttributesForAPI = [ 'sharedInboxUrl', 'followersUrl', 'followingUrl', - 'url', 'createdAt', 'updatedAt' ] @@ -74,7 +73,8 @@ export const unusedActorAttributesForAPI = [ required: false }, { - model: AvatarModel, + model: ActorImageModel, + as: 'Avatar', required: false } ] @@ -101,7 +101,8 @@ export const unusedActorAttributesForAPI = [ required: false }, { - model: AvatarModel, + model: ActorImageModel, + as: 'Avatar', required: false } ] @@ -123,13 +124,13 @@ export const unusedActorAttributesForAPI = [ } } }, - // { - // fields: [ 'preferredUsername' ], - // unique: true, - // where: { - // serverId: null - // } - // }, + { + fields: [ 'preferredUsername' ], + unique: true, + where: { + serverId: null + } + }, { fields: [ 'inboxUrl', 'sharedInboxUrl' ] }, @@ -147,7 +148,7 @@ export const unusedActorAttributesForAPI = [ } ] }) -export class ActorModel extends Model { +export class ActorModel extends Model { @AllowNull(false) @Column(DataType.ENUM(...values(ACTIVITY_PUB_ACTOR_TYPES))) @@ -214,18 +215,35 @@ export class ActorModel extends Model { @UpdatedAt updatedAt: Date - @ForeignKey(() => AvatarModel) + @ForeignKey(() => ActorImageModel) @Column avatarId: number - @BelongsTo(() => AvatarModel, { + @ForeignKey(() => ActorImageModel) + @Column + bannerId: number + + @BelongsTo(() => ActorImageModel, { + foreignKey: { + name: 'avatarId', + allowNull: true + }, + as: 'Avatar', + onDelete: 'set null', + hooks: true + }) + Avatar: ActorImageModel + + @BelongsTo(() => ActorImageModel, { foreignKey: { + name: 'bannerId', allowNull: true }, + as: 'Banner', onDelete: 'set null', hooks: true }) - Avatar: AvatarModel + Banner: ActorImageModel @HasMany(() => ActorFollowModel, { foreignKey: { @@ -277,15 +295,15 @@ export class ActorModel extends Model { }) VideoChannel: VideoChannelModel - static load (id: number): Bluebird { + static load (id: number): Promise { return ActorModel.unscoped().findByPk(id) } - static loadFull (id: number): Bluebird { + static loadFull (id: number): Promise { return ActorModel.scope(ScopeNames.FULL).findByPk(id) } - static loadFromAccountByVideoId (videoId: number, transaction: Transaction): Bluebird { + static loadFromAccountByVideoId (videoId: number, transaction: Transaction): Promise { const query = { include: [ { @@ -329,7 +347,7 @@ export class ActorModel extends Model { .then(a => !!a) } - static listByFollowersUrls (followersUrls: string[], transaction?: Transaction): Bluebird { + static listByFollowersUrls (followersUrls: string[], transaction?: Transaction): Promise { const query = { where: { followersUrl: { @@ -342,7 +360,7 @@ export class ActorModel extends Model { return ActorModel.scope(ScopeNames.FULL).findAll(query) } - static loadLocalByName (preferredUsername: string, transaction?: Transaction): Bluebird { + static loadLocalByName (preferredUsername: string, transaction?: Transaction): Promise { const fun = () => { const query = { where: { @@ -365,7 +383,7 @@ export class ActorModel extends Model { }) } - static loadLocalUrlByName (preferredUsername: string, transaction?: Transaction): Bluebird { + static loadLocalUrlByName (preferredUsername: string, transaction?: Transaction): Promise { const fun = () => { const query = { attributes: [ 'url' ], @@ -389,7 +407,7 @@ export class ActorModel extends Model { }) } - static loadByNameAndHost (preferredUsername: string, host: string): Bluebird { + static loadByNameAndHost (preferredUsername: string, host: string): Promise { const query = { where: { preferredUsername @@ -408,7 +426,7 @@ export class ActorModel extends Model { return ActorModel.scope(ScopeNames.FULL).findOne(query) } - static loadByUrl (url: string, transaction?: Transaction): Bluebird { + static loadByUrl (url: string, transaction?: Transaction): Promise { const query = { where: { url @@ -431,7 +449,7 @@ export class ActorModel extends Model { return ActorModel.unscoped().findOne(query) } - static loadByUrlAndPopulateAccountAndChannel (url: string, transaction?: Transaction): Bluebird { + static loadByUrlAndPopulateAccountAndChannel (url: string, transaction?: Transaction): Promise { const query = { where: { url @@ -462,7 +480,7 @@ export class ActorModel extends Model { }, { where, transaction }) } - static loadAccountActorByVideoId (videoId: number): Bluebird { + static loadAccountActorByVideoId (videoId: number): Promise { const query = { include: [ { @@ -497,7 +515,7 @@ export class ActorModel extends Model { } toFormattedSummaryJSON (this: MActorSummaryFormattable) { - let avatar: Avatar = null + let avatar: ActorImage = null if (this.Avatar) { avatar = this.Avatar.toFormattedJSON() }