]>
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 | ||
5fb2e288 C |
17 | static GET_ACTOR_AVATAR_URL (actor: { avatar?: Avatar }) { |
18 | if (actor?.avatar?.url) return actor.avatar.url | |
d3e91a5f | 19 | |
5fb2e288 C |
20 | if (actor && actor.avatar) { |
21 | const absoluteAPIUrl = getAbsoluteAPIUrl() | |
22 | ||
23 | return absoluteAPIUrl + actor.avatar.path | |
24 | } | |
d3e91a5f | 25 | |
62fca05d | 26 | return this.GET_DEFAULT_AVATAR_URL() |
c511c3f0 RK |
27 | } |
28 | ||
29 | static GET_DEFAULT_AVATAR_URL () { | |
d3e91a5f C |
30 | return window.location.origin + '/client/assets/images/default-avatar.png' |
31 | } | |
32 | ||
e379f813 | 33 | static CREATE_BY_STRING (accountName: string, host: string, forceHostname = false) { |
d3e91a5f C |
34 | const absoluteAPIUrl = getAbsoluteAPIUrl() |
35 | const thisHost = new URL(absoluteAPIUrl).host | |
36 | ||
e379f813 | 37 | if (host.trim() === thisHost && !forceHostname) return accountName |
d3e91a5f C |
38 | |
39 | return accountName + '@' + host | |
40 | } | |
41 | ||
42 | protected constructor (hash: ActorServer) { | |
43 | this.id = hash.id | |
d3e91a5f C |
44 | this.url = hash.url |
45 | this.name = hash.name | |
46 | this.host = hash.host | |
47 | this.followingCount = hash.followingCount | |
48 | this.followersCount = hash.followersCount | |
db400f44 C |
49 | this.createdAt = new Date(hash.createdAt.toString()) |
50 | this.updatedAt = new Date(hash.updatedAt.toString()) | |
d3e91a5f C |
51 | this.avatar = hash.avatar |
52 | ||
52d9f792 C |
53 | this.updateComputedAttributes() |
54 | } | |
55 | ||
56 | updateAvatar (newAvatar: Avatar) { | |
57 | this.avatar = newAvatar | |
58 | ||
59 | this.updateComputedAttributes() | |
60 | } | |
61 | ||
62 | private updateComputedAttributes () { | |
d3e91a5f C |
63 | this.avatarUrl = Actor.GET_ACTOR_AVATAR_URL(this) |
64 | } | |
65 | } |