1 import { Actor as ActorServer } from '../../../../../shared/models/actors/actor.model'
2 import { Avatar } from '../../../../../shared/models/avatars/avatar.model'
3 import { getAbsoluteAPIUrl } from '@app/shared/misc/utils'
5 export abstract class Actor implements ActorServer {
10 followingCount: number
11 followersCount: number
12 createdAt: Date | string
13 updatedAt: Date | string
18 static GET_ACTOR_AVATAR_URL (actor: { avatar?: Avatar }) {
19 if (actor?.avatar?.url) return actor.avatar.url
21 if (actor && actor.avatar) {
22 const absoluteAPIUrl = getAbsoluteAPIUrl()
24 return absoluteAPIUrl + actor.avatar.path
27 return this.GET_DEFAULT_AVATAR_URL()
30 static GET_DEFAULT_AVATAR_URL () {
31 return window.location.origin + '/client/assets/images/default-avatar.png'
34 static CREATE_BY_STRING (accountName: string, host: string, forceHostname = false) {
35 const absoluteAPIUrl = getAbsoluteAPIUrl()
36 const thisHost = new URL(absoluteAPIUrl).host
38 if (host.trim() === thisHost && !forceHostname) return accountName
40 return accountName + '@' + host
43 protected constructor (hash: ActorServer) {
48 this.followingCount = hash.followingCount
49 this.followersCount = hash.followersCount
50 this.createdAt = new Date(hash.createdAt.toString())
51 this.updatedAt = new Date(hash.updatedAt.toString())
52 this.avatar = hash.avatar
54 this.updateComputedAttributes()
57 updateAvatar (newAvatar: Avatar) {
58 this.avatar = newAvatar
60 this.updateComputedAttributes()
63 private updateComputedAttributes () {
64 this.avatarUrl = Actor.GET_ACTOR_AVATAR_URL(this)