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