]>
Commit | Line | Data |
---|---|---|
1 | import { getAbsoluteAPIUrl, getAPIHost } from '@app/helpers' | |
2 | import { Actor as ServerActor, ActorImage } from '@shared/models' | |
3 | ||
4 | export abstract class Actor implements ServerActor { | |
5 | id: number | |
6 | name: string | |
7 | ||
8 | host: string | |
9 | url: string | |
10 | ||
11 | followingCount: number | |
12 | followersCount: number | |
13 | ||
14 | createdAt: Date | string | |
15 | ||
16 | // TODO: remove, deprecated in 4.2 | |
17 | avatar: never | |
18 | ||
19 | avatars: ActorImage[] | |
20 | ||
21 | isLocal: boolean | |
22 | ||
23 | static GET_ACTOR_AVATAR_URL (actor: { avatars: { width: number, url?: string, path: string }[] }, size?: number) { | |
24 | const avatarsAscWidth = actor.avatars.sort((a, b) => a.width - b.width) | |
25 | ||
26 | const avatar = size && avatarsAscWidth.length > 1 | |
27 | ? avatarsAscWidth.find(a => a.width >= size) | |
28 | : avatarsAscWidth[avatarsAscWidth.length - 1] // Bigger one | |
29 | ||
30 | if (!avatar) return '' | |
31 | if (avatar.url) return avatar.url | |
32 | ||
33 | const absoluteAPIUrl = getAbsoluteAPIUrl() | |
34 | ||
35 | return absoluteAPIUrl + avatar.path | |
36 | } | |
37 | ||
38 | static CREATE_BY_STRING (accountName: string, host: string, forceHostname = false) { | |
39 | const thisHost = getAPIHost() | |
40 | ||
41 | if (host.trim() === thisHost && !forceHostname) return accountName | |
42 | ||
43 | return accountName + '@' + host | |
44 | } | |
45 | ||
46 | static IS_LOCAL (host: string) { | |
47 | const thisHost = getAPIHost() | |
48 | ||
49 | return host.trim() === thisHost | |
50 | } | |
51 | ||
52 | protected constructor (hash: Partial<ServerActor>) { | |
53 | this.id = hash.id | |
54 | this.url = hash.url ?? '' | |
55 | this.name = hash.name ?? '' | |
56 | this.host = hash.host ?? '' | |
57 | this.followingCount = hash.followingCount | |
58 | this.followersCount = hash.followersCount | |
59 | ||
60 | if (hash.createdAt) this.createdAt = new Date(hash.createdAt.toString()) | |
61 | ||
62 | this.avatars = hash.avatars || [] | |
63 | this.isLocal = Actor.IS_LOCAL(this.host) | |
64 | } | |
65 | } |