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