blob: 127634ad64f3174dc1a2318eaa3697846fc6f54e [file] [log] [blame]
Adrià Vilanova Martínez5dd7a6f2023-03-04 18:32:27 +01001import {kUserRoleEnum, kUserRoleRank} from './enums/UserRole.js';
2import StartupData from './StartupData.js';
3
4export default class UserModel {
5 constructor(data, startupData) {
6 this.data = data ?? {};
7 this.startupData = startupData ?? new StartupData();
8 }
9
10 getRawAccountAbuse() {
11 return this.data[8]?.[1] ?? null;
12 }
13
14 hasAccountAbuse() {
15 return this.getRawAccountAbuse() !== null;
16 }
17
18 isAccountDisabled() {
19 return this.hasAccountAbuse();
20 }
21
22 getRole(forumId) {
23 const forumsInfo = this.startupData.getRawForumsInfo() ?? [];
24 for (const f of forumsInfo) {
25 const itForumId = f[1] ?? f[2]?.[1]?.[1];
26 if (itForumId == forumId) {
27 return f[3]?.[1]?.[3] ?? kUserRoleEnum.ROLE_USER;
28 }
29 }
30 return kUserRoleEnum.ROLE_USER;
31 }
32
33 #isRoleAtLeast(a, b) {
34 const aRank = kUserRoleRank[a] ?? 0;
35 const bRank = kUserRoleRank[b] ?? 0;
36 return aRank >= bRank;
37 }
38
39 getHighestRole() {
40 const forumsInfo = this.startupData.getRawForumsInfo() ?? [];
41 const roles = forumsInfo.map(f => {
42 return f[3]?.[1]?.[3] ?? kUserRoleEnum.ROLE_USER;
43 });
44 return roles.reduce((prev, current) => {
45 return this.#isRoleAtLeast(current, prev) ? current : prev;
46 });
47 }
48
49 isAtLeastCommunityManager(forumId = null) {
50 const role = forumId ? this.getRole(forumId) : this.getHighestRole();
51 return this.#isRoleAtLeast(role, kUserRoleEnum.ROLE_COMMUNITY_MANAGER);
52 }
53
54 isAtLeastSilverRole(forumId = null) {
55 const role = forumId ? this.getRole(forumId) : this.getHighestRole();
56 return this.#isRoleAtLeast(role, kUserRoleEnum.ROLE_PRODUCT_EXPERT_LEVEL_2);
57 }
58}