-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.ts
144 lines (125 loc) · 3.05 KB
/
user.ts
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
export class SlackUser {
public id: string;
public name: string;
public teamId: string;
public status: string;
// Profile data
public realName: string;
public tzOffset: number;
public tzLabel: string;
constructor(userObj: any) {
this.id = userObj.id;
this.name = userObj.name;
this.tzOffset = userObj.tzOffset || userObj.tz_offset;
this.tzLabel = userObj.tzLabel || userObj.tz_label;
this.realName = userObj.realName || userObj.real_name;
this.teamId = userObj.teamId || userObj.team_id;
if (userObj.profile) {
this.realName = userObj.profile.real_name;
}
}
}
export class TwilioUser {
public id: string;
constructor(data: any) {
this.id = data.id;
}
}
export class AlexaUser {
public id: string;
public name;
string;
constructor(data: any) {
this.id = data.id;
this.name = data.name;
}
}
export class PushUser {
public deviceToken: string;
constructor(data: any) {
this.deviceToken = data.deviceToken;
}
}
export class FacebookUser {
public id: string;
constructor(data: any) {
this.id = data.id;
}
}
export default class User {
public id: string;
public slack: SlackUser;
public twilio: TwilioUser;
public alexa: AlexaUser;
public push: PushUser;
public facebook: FacebookUser;
public authToken: string;
// TODO: add other oauth users, e.g. google, facebook, twitter, fitbit, etc
constructor(data: any) {
if (!data.id) {
this.id = Math.random()
.toString(36)
.slice(2);
} else {
this.id = data.id;
}
if (data.slack) {
this.updateSlackUser(data.slack);
}
if (data.twilio) {
this.updateTwilioUser(data.slack);
}
if (data.alexa) {
this.updateAlexaUser(data.alexa);
}
if (data.push) {
this.updatePushUser(data.push);
}
if (data.facebook) {
this.updateFacebookUser(data.facebook);
}
if (data.authToken) {
this.authToken = data.authToken;
}
}
public updateSlackUser(slackUserObj: any) {
this.slack = new SlackUser(slackUserObj);
}
public updateTwilioUser(twilioUserObj: any) {
this.twilio = new TwilioUser(twilioUserObj);
}
public updateAlexaUser(alexaUserObj: any) {
this.alexa = new AlexaUser(alexaUserObj);
}
public updatePushUser(pushUserObj: any) {
this.push = new PushUser(pushUserObj);
}
public updateFacebookUser(facebookUserObj: any) {
this.facebook = new FacebookUser(facebookUserObj);
}
// See if any subusers have the same id
public containsId(id: string): boolean {
if (this.id === id) {
return true;
} else if (this.slack && this.slack.id === id) {
return true;
} else if (this.twilio && this.twilio.id === id) {
return true;
} else if (this.facebook && this.facebook.id === id) {
return true;
}
return false;
}
public onTeam(id: string): boolean {
if (!id) {
return false;
}
if (this.slack) {
return this.slack.teamId === id;
}
return false;
}
public serialize() {
return JSON.parse(JSON.stringify(this));
}
}