From c5911fd347c76e8bdc05ea9f3ee9efed4a58c236 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 29 Dec 2017 19:10:13 +0100 Subject: Begin to add avatar to actors --- server/models/account/account.ts | 6 ++++-- server/models/account/user.ts | 8 +++++--- server/models/activitypub/actor.ts | 37 ++++++++++++++++++++++++++++-------- server/models/avatar/avatar.ts | 30 ++++++++++++++++++++++++++++- server/models/video/video-comment.ts | 2 +- 5 files changed, 68 insertions(+), 15 deletions(-) (limited to 'server/models') diff --git a/server/models/account/account.ts b/server/models/account/account.ts index 1ee232537..d3503aaa3 100644 --- a/server/models/account/account.ts +++ b/server/models/account/account.ts @@ -13,6 +13,7 @@ import { Table, UpdatedAt } from 'sequelize-typescript' +import { Account } from '../../../shared/models/actors' import { isUserUsernameValid } from '../../helpers/custom-validators/users' import { sendDeleteActor } from '../../lib/activitypub/send' import { ActorModel } from '../activitypub/actor' @@ -165,11 +166,12 @@ export class AccountModel extends Model { return AccountModel.findOne(query) } - toFormattedJSON () { + toFormattedJSON (): Account { const actor = this.Actor.toFormattedJSON() const account = { id: this.id, - name: this.name, + name: this.Actor.preferredUsername, + displayName: this.name, createdAt: this.createdAt, updatedAt: this.updatedAt } diff --git a/server/models/account/user.ts b/server/models/account/user.ts index d7e09e328..4226bcb35 100644 --- a/server/models/account/user.ts +++ b/server/models/account/user.ts @@ -4,6 +4,7 @@ import { Scopes, Table, UpdatedAt } from 'sequelize-typescript' import { hasUserRight, USER_ROLE_LABELS, UserRight } from '../../../shared' +import { User } from '../../../shared/models/users' import { isUserAutoPlayVideoValid, isUserDisplayNSFWValid, isUserPasswordValid, isUserRoleValid, isUserUsernameValid, isUserVideoQuotaValid @@ -210,7 +211,7 @@ export class UserModel extends Model { return comparePassword(password, this.password) } - toFormattedJSON () { + toFormattedJSON (): User { const json = { id: this.id, username: this.username, @@ -221,11 +222,12 @@ export class UserModel extends Model { roleLabel: USER_ROLE_LABELS[ this.role ], videoQuota: this.videoQuota, createdAt: this.createdAt, - account: this.Account.toFormattedJSON() + account: this.Account.toFormattedJSON(), + videoChannels: [] } if (Array.isArray(this.Account.VideoChannels) === true) { - json['videoChannels'] = this.Account.VideoChannels + json.videoChannels = this.Account.VideoChannels .map(c => c.toFormattedJSON()) .sort((v1, v2) => { if (v1.createdAt < v2.createdAt) return -1 diff --git a/server/models/activitypub/actor.ts b/server/models/activitypub/actor.ts index 3d96b3706..8422653df 100644 --- a/server/models/activitypub/actor.ts +++ b/server/models/activitypub/actor.ts @@ -1,5 +1,5 @@ import { values } from 'lodash' -import { join } from 'path' +import { extname, join } from 'path' import * as Sequelize from 'sequelize' import { AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, DefaultScope, ForeignKey, HasMany, HasOne, Is, IsUUID, Model, Scopes, @@ -30,6 +30,10 @@ enum ScopeNames { { model: () => ServerModel, required: false + }, + { + model: () => AvatarModel, + required: false } ] }) @@ -47,6 +51,10 @@ enum ScopeNames { { model: () => ServerModel, required: false + }, + { + model: () => AvatarModel, + required: false } ] } @@ -141,7 +149,7 @@ export class ActorModel extends Model { foreignKey: { allowNull: true }, - onDelete: 'cascade' + onDelete: 'set null' }) Avatar: AvatarModel @@ -253,11 +261,7 @@ export class ActorModel extends Model { toFormattedJSON () { let avatar: Avatar = null if (this.Avatar) { - avatar = { - path: join(AVATARS_DIR.ACCOUNT, this.Avatar.filename), - createdAt: this.Avatar.createdAt, - updatedAt: this.Avatar.updatedAt - } + avatar = this.Avatar.toFormattedJSON() } let score: number @@ -286,6 +290,16 @@ export class ActorModel extends Model { activityPubType = 'Group' as 'Group' } + let icon = undefined + if (this.avatarId) { + const extension = extname(this.Avatar.filename) + icon = { + type: 'Image', + mediaType: extension === '.png' ? 'image/png' : 'image/jpeg', + url: this.getAvatarUrl() + } + } + const json = { type: activityPubType, id: this.url, @@ -304,7 +318,8 @@ export class ActorModel extends Model { id: this.getPublicKeyUrl(), owner: this.url, publicKeyPem: this.publicKey - } + }, + icon } return activityPubContextify(json) @@ -353,4 +368,10 @@ export class ActorModel extends Model { getHost () { return this.Server ? this.Server.host : CONFIG.WEBSERVER.HOST } + + getAvatarUrl () { + if (!this.avatarId) return undefined + + return CONFIG.WEBSERVER.URL + this.Avatar.getWebserverPath + } } diff --git a/server/models/avatar/avatar.ts b/server/models/avatar/avatar.ts index 2e7a8ae2c..7493c3d75 100644 --- a/server/models/avatar/avatar.ts +++ b/server/models/avatar/avatar.ts @@ -1,4 +1,10 @@ -import { AllowNull, Column, CreatedAt, Model, Table, UpdatedAt } from 'sequelize-typescript' +import { join } from 'path' +import { AfterDestroy, AllowNull, Column, CreatedAt, Model, Table, UpdatedAt } from 'sequelize-typescript' +import { Avatar } from '../../../shared/models/avatars/avatar.model' +import { unlinkPromise } from '../../helpers/core-utils' +import { logger } from '../../helpers/logger' +import { CONFIG, STATIC_PATHS } from '../../initializers' +import { sendDeleteVideo } from '../../lib/activitypub/send' @Table({ tableName: 'avatar' @@ -14,4 +20,26 @@ export class AvatarModel extends Model { @UpdatedAt updatedAt: Date + + @AfterDestroy + static removeFilesAndSendDelete (instance: AvatarModel) { + return instance.removeAvatar() + } + + toFormattedJSON (): Avatar { + return { + path: this.getWebserverPath(), + createdAt: this.createdAt, + updatedAt: this.updatedAt + } + } + + getWebserverPath () { + return join(STATIC_PATHS.AVATARS, this.filename) + } + + removeAvatar () { + const avatarPath = join(CONFIG.STORAGE.AVATARS_DIR, this.filename) + return unlinkPromise(avatarPath) + } } diff --git a/server/models/video/video-comment.ts b/server/models/video/video-comment.ts index d381ccafa..829022a51 100644 --- a/server/models/video/video-comment.ts +++ b/server/models/video/video-comment.ts @@ -214,7 +214,7 @@ export class VideoCommentModel extends Model { static listThreadCommentsForApi (videoId: number, threadId: number) { const query = { - order: [ [ 'id', 'ASC' ] ], + order: [ [ 'createdAt', 'DESC' ] ], where: { videoId, [ Sequelize.Op.or ]: [ -- cgit v1.2.3