-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuser.js
175 lines (143 loc) · 4.22 KB
/
user.js
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import Ember from 'ember';
import { inject as service } from '@ember/service';
import { htmlSafe } from '@ember/template';
import Model, { hasMany, attr } from '@ember-data/model';
import { isEmpty } from '@ember/utils';
import moment from 'moment';
import { AvatarThumbFallback, AvatarFallback } from 'amber-ui/constants';
export default class User extends Model {
@service session;
// General properties
@attr firstName;
@attr lastNamePrefix;
@attr lastName;
@attr nickname;
@attr('date-only') birthday;
@attr address;
@attr postcode;
@attr city;
@attr phoneNumber;
@attr email;
@attr username;
@attr study;
// Preferences / settings
@attr foodPreferences;
@attr vegetarian;
@attr('date-only') startStudy;
@attr emergencyContact;
@attr emergencyNumber;
@attr almanakSubscriptionPreference;
@attr digtusSubscriptionPreference;
// Privacy settings
@attr picturePublicationPreference;
@attr ifesDataSharingPreference;
@attr({ allowNull: true }) allowTomatoSharing;
@attr infoInAlmanak;
@attr userDetailsSharingPreference;
// Security properties
@attr otpRequired;
@attr icalSecretKey;
@attr icalCategories;
@attr webdavSecretKey;
@attr password;
// Technical properties
@attr loginEnabled;
@attr('date') activatedAt;
@attr('date') createdAt;
@attr('date') updatedAt;
// Avatar
@attr avatar;
@attr avatarUrl;
@attr avatarThumbUrl;
// Relations
@hasMany permissions;
@hasMany('permissions') userPermissions;
@hasMany memberships;
@hasMany groups;
@hasMany('debit/mandate') mandates;
@hasMany mailAliases;
@hasMany('mail-alias') groupMailAliases;
@hasMany('photo-tags', { inverse: 'author' }) createdPhotoTags;
@hasMany('photo-tags', { inverse: 'taggedUser' }) photoTags;
@hasMany('photos') photos;
// Computed properties
get fullName() {
if (this.lastNamePrefix === null) {
return `${this.firstName} ${this.lastName}`;
}
return `${this.firstName} ${this.lastNamePrefix} ${this.lastName}`;
}
get fullNickname() {
if (this.lastNamePrefix === null) {
return `${this.nickname} ${this.lastName}`;
}
return `${this.nickname} ${this.lastNamePrefix} ${this.lastName}`;
}
get fullNameWithNickname() {
return this._fullNameWithNickname();
}
get fullNameWithNicknameMd() {
return this._fullNameWithNickname(true);
}
_fullNameWithNickname(markdown = false) {
let escape = Ember.Handlebars.Utils.escapeExpression;
let name = `${escape(this.firstName)}`;
if (this.nickname !== null) {
if (markdown) name += ` *${escape(this.nickname)}*`;
else name += ` <i>${escape(this.nickname)}</i>`;
}
if (this.lastNamePrefix !== null) {
name += ` ${escape(this.lastNamePrefix)}`;
}
return htmlSafe(`${name} ${escape(this.lastName)}`);
}
get age() {
return moment().diff(this.birthday, 'years');
}
get upcomingBirthdayAge() {
return this.age + 1;
}
get upcomingBirthdayDate() {
return moment(this.birthday)
.add(this.upcomingBirthdayAge, 'years')
.toDate();
}
get hasBirthdayToday() {
const birthdayDate = moment(this.birthday);
const monthIsTheSame = moment().month() === birthdayDate.month();
const dayOfMonthIsTheSame = moment().date() === birthdayDate.date();
return monthIsTheSame && dayOfMonthIsTheSame;
}
hasPermission(name) {
return this.permissions.findBy('name', name) !== undefined;
}
get isCurrentUser() {
return this.session.currentUser.id === this.id;
}
get currentMemberships() {
return this.memberships.filter((membership) =>
membership.get('userIsCurrentlyMember')
);
}
get avatarUrlOrDefault() {
return this.avatarUrl || AvatarFallback;
}
get avatarThumbUrlOrDefault() {
return this.avatarThumbUrl || AvatarThumbFallback;
}
get sortedPhotos() {
return this.photos?.sortBy('exifDateTimeOriginal', 'createdAt');
}
// Methods
setNullIfEmptyString(property) {
const value = this.get(property);
if (value !== undefined && value !== null && isEmpty(value.trim())) {
this.set(property, null);
}
}
save(...args) {
this.setNullIfEmptyString('lastNamePrefix');
this.setNullIfEmptyString('foodPreferences');
return super.save(...args);
}
}