Welcome to mirror list, hosted at ThFree Co, Russian Federation.

chatUser.js « core « src - github.com/candy-chat/candy.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5067afbe22864f1ba9202197ec7461c3431b4d5f (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/** File: chatUser.js
 * Candy - Chats are not dead yet.
 *
 * Authors:
 *   - Patrick Stadler <patrick.stadler@gmail.com>
 *   - Michael Weibel <michael.weibel@gmail.com>
 *
 * Copyright:
 *   (c) 2011 Amiado Group AG. All rights reserved.
 *   (c) 2012-2014 Patrick Stadler & Michael Weibel. All rights reserved.
 */
'use strict';

/* global Candy, Strophe */

/** Class: Candy.Core.ChatUser
 * Chat User
 */
Candy.Core.ChatUser = function(jid, nick, affiliation, role, realJid) {
	/** Constant: ROLE_MODERATOR
	 * Moderator role
	 */
	this.ROLE_MODERATOR    = 'moderator';

	/** Constant: AFFILIATION_OWNER
	 * Affiliation owner
	 */
	this.AFFILIATION_OWNER = 'owner';

	/** Object: data
	 * User data containing:
	 * - jid
	 * - realJid
	 * - nick
	 * - affiliation
	 * - role
	 * - privacyLists
	 * - customData to be used by e.g. plugins
	 */
	this.data = {
		jid: jid,
		realJid: realJid,
		nick: Strophe.unescapeNode(nick),
		affiliation: affiliation,
		role: role,
		privacyLists: {},
		customData: {},
		previousNick: undefined,
		status: 'unavailable'
	};
};

/** Function: getJid
 * Gets an unescaped user jid
 *
 * See:
 *   <Candy.Util.unescapeJid>
 *
 * Returns:
 *   (String) - jid
 */
Candy.Core.ChatUser.prototype.getJid = function() {
	if(this.data.jid) {
		return Candy.Util.unescapeJid(this.data.jid);
	}
	return;
};

/** Function: getEscapedJid
 * Escapes the user's jid (node & resource get escaped)
 *
 * See:
 *   <Candy.Util.escapeJid>
 *
 * Returns:
 *   (String) - escaped jid
 */
Candy.Core.ChatUser.prototype.getEscapedJid = function() {
	return Candy.Util.escapeJid(this.data.jid);
};

/** Function: setJid
 * Sets a user's jid
 *
 * Parameters:
 *   (String) jid - New Jid
 */
Candy.Core.ChatUser.prototype.setJid = function(jid) {
	this.data.jid = jid;
};

/** Function: getRealJid
 * Gets an unescaped real jid if known
 *
 * See:
 *   <Candy.Util.unescapeJid>
 *
 * Returns:
 *   (String) - realJid
 */
Candy.Core.ChatUser.prototype.getRealJid = function() {
	if(this.data.realJid) {
		return Candy.Util.unescapeJid(this.data.realJid);
	}
	return;
};

/** Function: getNick
 * Gets user nick
 *
 * Returns:
 *   (String) - nick
 */
Candy.Core.ChatUser.prototype.getNick = function() {
	return Strophe.unescapeNode(this.data.nick);
};

/** Function: setNick
 * Sets a user's nick
 *
 * Parameters:
 *   (String) nick - New nick
 */
Candy.Core.ChatUser.prototype.setNick = function(nick) {
	this.data.nick = nick;
};

/** Function: getName
 * Gets user's name (from contact or nick)
 *
 * Returns:
 *   (String) - name
 */
Candy.Core.ChatUser.prototype.getName = function() {
	var contact = this.getContact();
	if (contact) {
		return contact.getName();
	} else {
		return this.getNick();
	}
};

/** Function: getRole
 * Gets user role
 *
 * Returns:
 *   (String) - role
 */
Candy.Core.ChatUser.prototype.getRole = function() {
	return this.data.role;
};

/** Function: setRole
 * Sets user role
 *
 * Parameters:
 *   (String) role - Role
 */
Candy.Core.ChatUser.prototype.setRole = function(role) {
	this.data.role = role;
};

/** Function: setAffiliation
 * Sets user affiliation
 *
 * Parameters:
 *   (String) affiliation - new affiliation
 */
Candy.Core.ChatUser.prototype.setAffiliation = function(affiliation) {
	this.data.affiliation = affiliation;
};

/** Function: getAffiliation
 * Gets user affiliation
 *
 * Returns:
 *   (String) - affiliation
 */
Candy.Core.ChatUser.prototype.getAffiliation = function() {
	return this.data.affiliation;
};

/** Function: isModerator
 * Check if user is moderator. Depends on the room.
 *
 * Returns:
 *   (Boolean) - true if user has role moderator or affiliation owner
 */
Candy.Core.ChatUser.prototype.isModerator = function() {
	return this.getRole() === this.ROLE_MODERATOR || this.getAffiliation() === this.AFFILIATION_OWNER;
};

/** Function: addToOrRemoveFromPrivacyList
 * Convenience function for adding/removing users from ignore list.
 *
 * Check if user is already in privacy list. If yes, remove it. If no, add it.
 *
 * Parameters:
 *   (String) list - To which privacy list the user should be added / removed from. Candy supports curently only the "ignore" list.
 *   (String) jid  - User jid to add/remove
 *
 * Returns:
 *   (Array) - Current privacy list.
 */
Candy.Core.ChatUser.prototype.addToOrRemoveFromPrivacyList = function(list, jid) {
	if (!this.data.privacyLists[list]) {
		this.data.privacyLists[list] = [];
	}
	var index = -1;
	if ((index = this.data.privacyLists[list].indexOf(jid)) !== -1) {
		this.data.privacyLists[list].splice(index, 1);
	} else {
		this.data.privacyLists[list].push(jid);
	}
	return this.data.privacyLists[list];
};

/** Function: getPrivacyList
 * Returns the privacy list of the listname of the param.
 *
 * Parameters:
 *   (String) list - To which privacy list the user should be added / removed from. Candy supports curently only the "ignore" list.
 *
 * Returns:
 *   (Array) - Privacy List
 */
Candy.Core.ChatUser.prototype.getPrivacyList = function(list) {
	if (!this.data.privacyLists[list]) {
		this.data.privacyLists[list] = [];
	}
	return this.data.privacyLists[list];
};

/** Function: setPrivacyLists
 * Sets privacy lists.
 *
 * Parameters:
 *   (Object) lists - List object
 */
Candy.Core.ChatUser.prototype.setPrivacyLists = function(lists) {
	this.data.privacyLists = lists;
};

/** Function: isInPrivacyList
 * Tests if this user ignores the user provided by jid.
 *
 * Parameters:
 *   (String) list - Privacy list
 *   (String) jid  - Jid to test for
 *
 * Returns:
 *   (Boolean)
 */
Candy.Core.ChatUser.prototype.isInPrivacyList = function(list, jid) {
	if (!this.data.privacyLists[list]) {
		return false;
	}
	return this.data.privacyLists[list].indexOf(jid) !== -1;
};

/** Function: setCustomData
 * Stores custom data
 *
 * Parameter:
 *   (Object) data - Object containing custom data
 */
Candy.Core.ChatUser.prototype.setCustomData = function(data) {
	this.data.customData = data;
};

/** Function: getCustomData
 * Retrieve custom data
 *
 * Returns:
 *   (Object) - Object containing custom data
 */
Candy.Core.ChatUser.prototype.getCustomData = function() {
	return this.data.customData;
};

/** Function: setPreviousNick
 * If user has nickname changed, set previous nickname.
 *
 * Parameters:
 *   (String) previousNick - the previous nickname
 */
Candy.Core.ChatUser.prototype.setPreviousNick = function(previousNick) {
	this.data.previousNick = previousNick;
};

/** Function: hasNicknameChanged
 * Gets the previous nickname if available.
 *
 * Returns:
 *   (String) - previous nickname
 */
Candy.Core.ChatUser.prototype.getPreviousNick = function() {
	return this.data.previousNick;
};

/** Function: getContact
 * Gets the contact matching this user from our roster
 *
 * Returns:
 *   (Candy.Core.Contact) - contact from roster
 */
Candy.Core.ChatUser.prototype.getContact = function() {
	return Candy.Core.getRoster().get(Strophe.getBareJidFromJid(this.data.realJid));
};

/** Function: setStatus
 * Set the user's status
 *
 * Parameters:
 *   (String) status - the new status
 */
Candy.Core.ChatUser.prototype.setStatus = function(status) {
	this.data.status = status;
};

/** Function: getStatus
 * Gets the user's status.
 *
 * Returns:
 *   (String) - status
 */
Candy.Core.ChatUser.prototype.getStatus = function() {
	return this.data.status;
};