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/activitypub/actor.ts | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) (limited to 'server/models/activitypub') 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 + } } -- cgit v1.2.3