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
19 static GET_ACTOR_AVATAR_URL (actor: { avatar?: { url?: string, path: string } }) {
20 if (actor?.avatar?.url) return actor.avatar.url
22 if (actor && actor.avatar) {
23 const absoluteAPIUrl = getAbsoluteAPIUrl()
25 return absoluteAPIUrl + actor.avatar.path
28 return this.GET_DEFAULT_AVATAR_URL()
31 static GET_DEFAULT_AVATAR_URL () {
32 return window.location.origin + '/client/assets/images/default-avatar.png'
35 static CREATE_BY_STRING (accountName: string, host: string, forceHostname = false) {
36 const absoluteAPIUrl = getAbsoluteAPIUrl()
37 const thisHost = new URL(absoluteAPIUrl).host
39 if (host.trim() === thisHost && !forceHostname) return accountName
41 return accountName + '@' + host
44 static IS_LOCAL (host: string) {
45 const absoluteAPIUrl = getAbsoluteAPIUrl()
46 const thisHost = new URL(absoluteAPIUrl).host
48 return host.trim() === thisHost
51 protected constructor (hash: ActorServer) {
56 this.followingCount = hash.followingCount
57 this.followersCount = hash.followersCount
59 if (hash.createdAt) this.createdAt = new Date(hash.createdAt.toString())
60 if (hash.updatedAt) this.updatedAt = new Date(hash.updatedAt.toString())
62 this.avatar = hash.avatar
63 this.isLocal = Actor.IS_LOCAL(this.host)
65 this.updateComputedAttributes()
68 updateAvatar (newAvatar: Avatar) {
69 this.avatar = newAvatar
71 this.updateComputedAttributes()
74 private updateComputedAttributes () {
75 this.avatarUrl = Actor.GET_ACTOR_AVATAR_URL(this)