1 import { Actor as ActorServer, Avatar } from '@shared/models'
2 import { getAbsoluteAPIUrl } from '@app/helpers'
4 export abstract class Actor implements ActorServer {
10 followersCount: number
11 createdAt: Date | string
12 updatedAt: Date | string
17 static GET_ACTOR_AVATAR_URL (actor: { avatar?: { url?: string, path: string } }) {
18 if (actor?.avatar?.url) return actor.avatar.url
20 if (actor && actor.avatar) {
21 const absoluteAPIUrl = getAbsoluteAPIUrl()
23 return absoluteAPIUrl + actor.avatar.path
26 return this.GET_DEFAULT_AVATAR_URL()
29 static GET_DEFAULT_AVATAR_URL () {
30 return window.location.origin + '/client/assets/images/default-avatar.png'
33 static CREATE_BY_STRING (accountName: string, host: string, forceHostname = false) {
34 const absoluteAPIUrl = getAbsoluteAPIUrl()
35 const thisHost = new URL(absoluteAPIUrl).host
37 if (host.trim() === thisHost && !forceHostname) return accountName
39 return accountName + '@' + host
42 protected constructor (hash: ActorServer) {
47 this.followingCount = hash.followingCount
48 this.followersCount = hash.followersCount
49 this.createdAt = new Date(hash.createdAt.toString())
50 this.updatedAt = new Date(hash.updatedAt.toString())
51 this.avatar = hash.avatar
53 this.updateComputedAttributes()
56 updateAvatar (newAvatar: Avatar) {
57 this.avatar = newAvatar
59 this.updateComputedAttributes()
62 private updateComputedAttributes () {
63 this.avatarUrl = Actor.GET_ACTOR_AVATAR_URL(this)