From 2295ce6c4e7ba805cc100ff961527bebc2cd89e5 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 4 Dec 2017 10:34:40 +0100 Subject: Add account avatar --- server/models/account/account-interface.ts | 3 +++ server/models/account/account.ts | 27 ++++++++++++++++++++++++++- server/models/account/user.ts | 5 +---- 3 files changed, 30 insertions(+), 5 deletions(-) (limited to 'server/models/account') diff --git a/server/models/account/account-interface.ts b/server/models/account/account-interface.ts index b369766dc..46fe068e3 100644 --- a/server/models/account/account-interface.ts +++ b/server/models/account/account-interface.ts @@ -1,6 +1,7 @@ import * as Bluebird from 'bluebird' import * as Sequelize from 'sequelize' import { Account as FormattedAccount, ActivityPubActor } from '../../../shared' +import { AvatarInstance } from '../avatar' import { ServerInstance } from '../server/server-interface' import { VideoChannelInstance } from '../video/video-channel-interface' @@ -51,6 +52,7 @@ export interface AccountAttributes { serverId?: number userId?: number applicationId?: number + avatarId?: number } export interface AccountInstance extends AccountClass, AccountAttributes, Sequelize.Instance { @@ -68,6 +70,7 @@ export interface AccountInstance extends AccountClass, AccountAttributes, Sequel Server: ServerInstance VideoChannels: VideoChannelInstance[] + Avatar: AvatarInstance } export interface AccountModel extends AccountClass, Sequelize.Model {} diff --git a/server/models/account/account.ts b/server/models/account/account.ts index 61a88524c..15be1126b 100644 --- a/server/models/account/account.ts +++ b/server/models/account/account.ts @@ -1,4 +1,6 @@ +import { join } from 'path' import * as Sequelize from 'sequelize' +import { Avatar } from '../../../shared/models/avatars/avatar.model' import { activityPubContextify, isAccountFollowersCountValid, @@ -8,8 +10,10 @@ import { isUserUsernameValid } from '../../helpers' import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' +import { AVATARS_DIR } from '../../initializers' import { CONFIG, CONSTRAINTS_FIELDS } from '../../initializers/constants' import { sendDeleteAccount } from '../../lib/activitypub/send/send-delete' +import { AvatarModel } from '../avatar' import { addMethodsToModel } from '../utils' import { AccountAttributes, AccountInstance, AccountMethods } from './account-interface' @@ -252,6 +256,14 @@ function associate (models) { as: 'followers', onDelete: 'cascade' }) + + Account.hasOne(models.Avatar, { + foreignKey: { + name: 'avatarId', + allowNull: true + }, + onDelete: 'cascade' + }) } function afterDestroy (account: AccountInstance) { @@ -265,6 +277,15 @@ function afterDestroy (account: AccountInstance) { toFormattedJSON = function (this: AccountInstance) { let host = CONFIG.WEBSERVER.HOST let score: number + let avatar: Avatar = null + + if (this.Avatar) { + avatar = { + path: join(AVATARS_DIR.ACCOUNT, this.Avatar.filename), + createdAt: this.Avatar.createdAt, + updatedAt: this.Avatar.updatedAt + } + } if (this.Server) { host = this.Server.host @@ -273,11 +294,15 @@ toFormattedJSON = function (this: AccountInstance) { const json = { id: this.id, + uuid: this.uuid, host, score, name: this.name, + followingCount: this.followingCount, + followersCount: this.followersCount, createdAt: this.createdAt, - updatedAt: this.updatedAt + updatedAt: this.updatedAt, + avatar } return json diff --git a/server/models/account/user.ts b/server/models/account/user.ts index 8f7c9b013..3705947c0 100644 --- a/server/models/account/user.ts +++ b/server/models/account/user.ts @@ -157,10 +157,7 @@ toFormattedJSON = function (this: UserInstance) { roleLabel: USER_ROLE_LABELS[this.role], videoQuota: this.videoQuota, createdAt: this.createdAt, - account: { - id: this.Account.id, - uuid: this.Account.uuid - } + account: this.Account.toFormattedJSON() } if (Array.isArray(this.Account.VideoChannels) === true) { -- cgit v1.2.3