]>
Commit | Line | Data |
---|---|---|
67ed6552 C |
1 | import { Actor as ActorServer, Avatar } from '@shared/models' |
2 | import { getAbsoluteAPIUrl } from '@app/helpers' | |
d3e91a5f C |
3 | |
4 | export abstract class Actor implements ActorServer { | |
5 | id: number | |
d3e91a5f C |
6 | url: string |
7 | name: string | |
8 | host: string | |
9 | followingCount: number | |
10 | followersCount: number | |
db400f44 C |
11 | createdAt: Date | string |
12 | updatedAt: Date | string | |
d3e91a5f C |
13 | avatar: Avatar |
14 | ||
15 | avatarUrl: string | |
16 | ||
cfde28ba C |
17 | isLocal: boolean |
18 | ||
d95d1559 | 19 | static GET_ACTOR_AVATAR_URL (actor: { avatar?: { url?: string, path: string } }) { |
5fb2e288 | 20 | if (actor?.avatar?.url) return actor.avatar.url |
d3e91a5f | 21 | |
5fb2e288 C |
22 | if (actor && actor.avatar) { |
23 | const absoluteAPIUrl = getAbsoluteAPIUrl() | |
24 | ||
25 | return absoluteAPIUrl + actor.avatar.path | |
26 | } | |
d3e91a5f | 27 | |
62fca05d | 28 | return this.GET_DEFAULT_AVATAR_URL() |
c511c3f0 RK |
29 | } |
30 | ||
31 | static GET_DEFAULT_AVATAR_URL () { | |
d3e91a5f C |
32 | return window.location.origin + '/client/assets/images/default-avatar.png' |
33 | } | |
34 | ||
e379f813 | 35 | static CREATE_BY_STRING (accountName: string, host: string, forceHostname = false) { |
d3e91a5f C |
36 | const absoluteAPIUrl = getAbsoluteAPIUrl() |
37 | const thisHost = new URL(absoluteAPIUrl).host | |
38 | ||
e379f813 | 39 | if (host.trim() === thisHost && !forceHostname) return accountName |
d3e91a5f C |
40 | |
41 | return accountName + '@' + host | |
42 | } | |
43 | ||
44 | protected constructor (hash: ActorServer) { | |
45 | this.id = hash.id | |
d3e91a5f C |
46 | this.url = hash.url |
47 | this.name = hash.name | |
48 | this.host = hash.host | |
49 | this.followingCount = hash.followingCount | |
50 | this.followersCount = hash.followersCount | |
8ca56654 C |
51 | |
52 | if (hash.createdAt) this.createdAt = new Date(hash.createdAt.toString()) | |
53 | if (hash.updatedAt) this.updatedAt = new Date(hash.updatedAt.toString()) | |
54 | ||
d3e91a5f C |
55 | this.avatar = hash.avatar |
56 | ||
cfde28ba C |
57 | const absoluteAPIUrl = getAbsoluteAPIUrl() |
58 | const thisHost = new URL(absoluteAPIUrl).host | |
59 | this.isLocal = this.host.trim() === thisHost | |
60 | ||
52d9f792 C |
61 | this.updateComputedAttributes() |
62 | } | |
63 | ||
64 | updateAvatar (newAvatar: Avatar) { | |
65 | this.avatar = newAvatar | |
66 | ||
67 | this.updateComputedAttributes() | |
68 | } | |
69 | ||
70 | private updateComputedAttributes () { | |
d3e91a5f C |
71 | this.avatarUrl = Actor.GET_ACTOR_AVATAR_URL(this) |
72 | } | |
73 | } |