This repository has been archived by the owner on Aug 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile.js
123 lines (101 loc) · 3.07 KB
/
profile.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
class Profile {
constructor(data, userModel) {
this.data = data;
this.userModel = userModel;
if (data.identities) {
this.provider = data.identities[0].provider;
}
this.displayName = data.name;
this.id = this.getId();
this.email = this.getEmail();
this.isEmailVerified = this.isEmailVerified();
this.getDescription();
this.name = {
familyName: data.family_name,
givenName: data.given_name,
};
if (data.emails) {
this.emails = data.emails.map(email => ({
value: email,
}));
} else if (data.email) {
this.emails = [{
value: data.email,
}];
}
//copy these fields
let copy = ['picture',
'locale',
'nickname',
'gender',
'identities',
];
copy.filter(k => k in data).forEach(k => {
this[k] = data[k];
});
}
getId() {
return typeof this.data.user_id !== undefined ?
this.data.user_id :
this.data.sub;
}
getUserName() {
return new Promise((resolve, reject) => {
let username = '';
let available = false;
if (this.data.username) {
username = this.data.username;
} else if (this.data.screen_name) {
username = this.data.username;
}
if (_.empty(useername)) {
username = this.data.name.replace(' ', '');
}
while (available === false) {
this.userModel.count({
username: username,
}, function(err, count) {
if (err) {
debug('Error while counting user', err);
return reject(false);
}
available = count > 0;
if (available === true) {
resolve(username);
} else {
username = username + _.random(0, 5000);
}
});
}
});
}
isEmailVerified() {
return (typeof this.data.email_verified !== undefined) &&
this.data.email_verified === true;
}
getEmail() {
let id = null;
if (this.data.email) {
id = this.data.email;
} else {
const usersplit = this.getId(user).split('|');
id = usersplit[1] + '@' + 'change_this_email.com';
}
return id;
}
getDescription() {
let description;
if (this.headline) {
description = this.headline;
} else if (this.description) {
description = this.description;
} else if (this.bio) {
description = this.bio;
} else if (this.about) {
description = this.data.about;
}
this.description = description;
return description;
}
}
module.exports = Profile;