blob: 4707a65ade7593042e9a7379683edb74a15e7398 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
/**
* @copyright Copyright (c) 2018 John Molakvoæ <skjnldsv@protonmail.com>
*
* @author John Molakvoæ <skjnldsv@protonmail.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
import Circle from './circle'
import { MemberLevel, MemberLevels, MemberType, MemberTypes } from './constants'
export default class Member {
_data: any = {}
_circle: Circle
/**
* Creates an instance of Member
*/
constructor(data: any, circle: Circle) {
if (typeof data !== 'object') {
throw new Error('Invalid member')
}
// if no uid set, fail
if (data.id && typeof data.id !== 'string') {
console.error('This member do not have a proper uid', data)
throw new Error('This member do not have a proper uid')
}
this._circle = circle
this._data = data
}
/**
* Get the circle of this member
*/
get circle(): Circle {
return this._circle
}
/**
* Set the circle of this member
*/
set circle(circle: Circle) {
if (circle.constructor.name !== Circle.name) {
throw new Error('circle must be a Circle type')
}
this._circle = circle
}
/**
* Member id
*/
get id(): string {
return this._data.id
}
/**
* Single uid
*/
get singleId(): string {
return this._data.singleId
}
/**
* Formatted display name
*/
get displayName(): string {
return this._data.displayName
}
/**
* Member userId
*/
get userId(): string {
return this._data.userId
}
/**
* Member type
*/
get userType(): MemberType {
// If the user type is a circle, this could originate from multiple sources
return this._data.userType !== MemberTypes.CIRCLE
? this._data.userType
: this.basedOn.source
}
/**
* Member based on source
*/
get basedOn(): any {
return this._data.basedOn
}
/**
* Member level
*
*/
get level(): MemberLevel {
return this._data.level
}
/**
* Set member level
*/
set level(level: MemberLevel) {
if (!(level in MemberLevels)) {
throw new Error('Invalid level')
}
this._data.level = level
}
/**
* Is the current member a user?
*/
get isUser() {
return this._data.userType === MemberLevels.MEMBER
}
/**
* Is the current member without a circle?
*/
get isOrphan() {
return this._circle?.constructor?.name !== 'Circle'
}
/**
* Delete this member and any reference from its circle
*/
delete() {
if (this.isOrphan) {
throw new Error('Cannot delete this member as it doesn\'t belong to any circle')
}
this.circle.deleteMember(this)
this._data = undefined
}
}
|