]>
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 | ||
94148c90 C |
44 | static IS_LOCAL (host: string) { |
45 | const absoluteAPIUrl = getAbsoluteAPIUrl() | |
46 | const thisHost = new URL(absoluteAPIUrl).host | |
47 | ||
48 | return host.trim() === thisHost | |
49 | } | |
50 | ||
d3e91a5f C |
51 | protected constructor (hash: ActorServer) { |
52 | this.id = hash.id | |
d3e91a5f C |
53 | this.url = hash.url |
54 | this.name = hash.name | |
55 | this.host = hash.host | |
56 | this.followingCount = hash.followingCount | |
57 | this.followersCount = hash.followersCount | |
8ca56654 C |
58 | |
59 | if (hash.createdAt) this.createdAt = new Date(hash.createdAt.toString()) | |
60 | if (hash.updatedAt) this.updatedAt = new Date(hash.updatedAt.toString()) | |
61 | ||
d3e91a5f | 62 | this.avatar = hash.avatar |
94148c90 | 63 | this.isLocal = Actor.IS_LOCAL(this.host) |
cfde28ba | 64 | |
52d9f792 C |
65 | this.updateComputedAttributes() |
66 | } | |
67 | ||
68 | updateAvatar (newAvatar: Avatar) { | |
69 | this.avatar = newAvatar | |
70 | ||
71 | this.updateComputedAttributes() | |
72 | } | |
73 | ||
74 | private updateComputedAttributes () { | |
d3e91a5f C |
75 | this.avatarUrl = Actor.GET_ACTOR_AVATAR_URL(this) |
76 | } | |
77 | } |