| 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503 | 1
1
1
1
1
1
48
2
48
48
12
12
48
1
2
2
1
1
871
871
98
98
98
2
98
98
98
8
6
1
5
5
5
6
6
2
1
1
6
6
6
6
57
65
65
55
10
6
6
2
2
2
6
1
1
5
5
5
5
113
113
52
52
52
52
38
2
2
1
1
1
5
5
2
2
2
9
9
2
2
2
6
6
3
3
3
2
2
2
2
2
2
6
1
3
3
2
3
3
8
8
8
2
1
2
2
2
6
6
1
| var Confidential = require("./../confidentiality_encryption/Confidential").Confidential;
var DatumFields = require("./../datum/DatumFields").DatumFields;
var FieldDBObject = require("./../FieldDBObject").FieldDBObject;
var UserMask = require("./UserMask").UserMask;
var DEFAULT_CORPUS_MODEL = require("./../corpus/corpus.json");
/**
* @class The Speaker represents a source of data, usually a
* language consultant who is a native speaker of the language,
* or a psycholinguistics experiment participant.
* Each consultant has their own I-language and/or dialects which they
* speak (unlike a published source which usually discusses an E-language
* or standard or normal expected production of an utterance.)
* Speakers can have any number of additional fields or metadata
* that a team might use to help cluster or understand variation in data.
*
* As "Informant" is not politically correct in many contexts, and "consultant" is
* ambigious word outside of field work, the word "speaker" is used in communication with
* users and in the url of db queries/api.
*
* A speaker might also be associated to a user. In this case a speaker
* has the same information as a user plus extra, some info (e.g. date of birth)
* which must be kept confidential. Speaker's gravatar are locked to
* default unless he/she wants to be public associated with/his username.
* Speakers which are also users have permissions about the level of
* access to the data (read only, add/edit).
*
* @name Speaker
* @extends UserMask
* @constructs
*/
var Speaker = function Speaker(options) {
if (!this._fieldDBtype) {
this._fieldDBtype = "Speaker";
}
this.debug("Constructing Speaker: ", options);
if (!options || (!options._rev && !options.fields)) {
//If its a new participant with out a revision and without fields use the defaults
options = options || {};
options.fields = this.defaults.fields;
}
UserMask.apply(this, arguments);
};
Speaker.prototype = Object.create(UserMask.prototype, /** @lends Speaker.prototype */ {
constructor: {
value: Speaker
},
api: {
value: "speakers"
},
INTERNAL_MODELS: {
value: {
username: FieldDBObject.DEFAULT_STRING,
anonymousCode: FieldDBObject.DEFAULT_STRING,
gravatar: FieldDBObject.DEFAULT_STRING,
fields: DatumFields,
user: UserMask,
confidential: Confidential
}
},
defaults: {
get: function() {
var doc = {
fields: DEFAULT_CORPUS_MODEL.speakerFields
};
return JSON.parse(JSON.stringify(doc));
}
},
confidentiality: {
get: function() {
if (this.fields) {
return this.fields.confidentiality.value;
} else {
return;
}
},
set: function(value) {
Iif (!this.fields) {
this.fields = new DatumFields(this.defaults.fields);
}
// this.warn("Cannot change the public/private of " + this.collection + " (it must be anonymous). " + value);
this.fields.confidentiality.value = value;
}
},
fields: {
get: function() {
this.debug("getting fields");
return this._fields;
},
set: function(value) {
Iif (value === this._fields) {
return;
}
Iif (!value) {
delete this._fields;
return;
} else {
if (typeof this.INTERNAL_MODELS["fields"] === "function" && Object.prototype.toString.call(value) === "[object Array]") {
value = new this.INTERNAL_MODELS["fields"](value);
}
}
Eif (!value.confidential) {
value.confidential = this.confidential;
}
this._fields = value;
}
},
buildGravatar: {
value: function() {
this._gravatar = "968b8e7fb72b5ffe2915256c28a9414c";
return this._gravatar;
}
},
gravatar: {
get: function() {
return this.buildGravatar();
},
set: function(value) {
this.warn("Cannot set the gravatar of a " + this.fieldDBtype + " (it must be anonymous)." + value);
}
},
username: {
get: function() {
if (this.fields && this.fields.username && this.fields.username.value) {
// this.debug("this.fields.username.value :", this.fields.username.value + ":");
if (this.fields.confidentiality.value === "generalize") {
this.fields.username.mask = "A native speaker";
} else Iif (this.fields.confidentiality.value === "team") {
this.todo("IF the user is part of the team, they can see the username of the consultant.");
this.fields.username.mask = this.anonymousCode;
} else Eif (this.fields.confidentiality.value === "anonymous") {
this.fields.username.mask = this.anonymousCode || this.fields.username.mask;
} else if (this.fields.confidentiality.value === "public") {
this.fields.username.mask = this.fields.username.value;
} else {
this.fields.username.mask = "A native speaker";
}
Iif (this.fields.username.decryptedMode) {
return this.fields.username.value;
} else {
return this.fields.username.mask;
}
} else {
if (this.id) {
return this.id;
}
return;
}
},
set: function(value) {
Iif (!this.confidential) {
this.warn("Cannot set the username before the confidential is set");
return;
}
Iif (!this.fields) {
this.fields = new DatumFields(this.defaults.fields);
}
// this.fields.username.debugMode = true;
// this.fields.username.decryptedMode = true;
this.fields.username.confidential = this.confidential;
this.fields.username.value = value;
}
},
encryptByCorpus: {
value: true
},
id: {
get: function() {
return this.anonymousCode;
},
set: function(value) {
this.anonymousCode = value;
}
},
anonymousCode: {
get: function() {
try {
if (this.fields && this.fields.anonymousCode) {
return this.fields.anonymousCode.value.toUpperCase();
} else {
return this._id;
}
} catch (e) {
this.warn("there was a problem getting the anonymousCode of this speaker", e);
return this._id;
}
},
set: function(value) {
var actualUsername;
if (this.fields && this.fields.username && this.fields.username.value) {
this.fields.username.decryptedMode = true;
actualUsername = this.fields.username.value;
this.fields.username.decryptedMode = false;
}
if (actualUsername && value.toLowerCase().indexOf(actualUsername) > -1) {
this.bug("Cannot set the anonymous code to contain any part of the user's actual username, this would potentially breach their confidentiality.");
return;
}
Iif (!this.fields) {
this.fields = new DatumFields(this.defaults.fields);
}
this.fields.anonymousCode.value = this._id = value;
this.debug("Set the id and the anonymousCode of this user" + this._id, this);
Iif (!this.encryptByCorpus) {
this.confidential = new Confidential({
secretkey: value
});
}
}
},
confidential: {
get: function() {
this.debug("using this speaker's confidential encrypter");
return this.confidentialEncrypter;
},
set: function(value) {
Iif (value === this.confidentialEncrypter) {
return;
}
Iif (typeof value.encrypt !== "function" && value.secretkey) {
value = new this.INTERNAL_MODELS["confidential"](value);
}
this.confidentialEncrypter = value;
if (this.fields) {
// this.debug("setting speaker fields confidential in the Speaker.confidential set function.");
this.fields.confidential = value;
}
}
},
dateOfBirth: {
configurable: true,
get: function() {
Eif (this.fields && this.fields.dateOfBirth) {
return this.fields.dateOfBirth.value;
} else {
return;
}
},
set: function(value) {
this.debug("Setting dateOfBirth " + value, this.confidential);
Iif (!this.fields) {
this.fields = new DatumFields(this.defaults.fields);
}
// this.fields.debugMode = true;
this.fields.dateOfBirth.value = value;
}
},
firstname: {
configurable: true,
get: function() {
Eif (this.fields && this.fields.firstname) {
return this.fields.firstname.value;
} else {
return;
}
},
set: function(value) {
Iif (!this.fields) {
this.fields = new DatumFields(this.defaults.fields);
}
Eif (this.fields && this.fields.firstname) {
// this.fields.debugMode = true;
this.fields.firstname.value = value;
} else {
this.fields.firstname.value = value;
}
}
},
lastname: {
configurable: true,
get: function() {
Eif (this.fields && this.fields.lastname) {
return this.fields.lastname.value;
} else {
return;
}
},
set: function(value) {
Iif (!this.fields) {
this.fields = new DatumFields(this.defaults.fields);
}
Eif (this.fields && this.fields.lastname) {
// this.fields.debugMode = true;
this.fields.lastname.value = value;
} else {
this.fields.lastname.value = value;
}
}
},
languages: {
get: function() {
Eif (this.fields) {
return this.fields.languages.value;
} else {
return;
}
},
set: function(value) {
var stringvalue;
var objectvalue;
if (typeof value === "string") {
this.debug("User set the languages with a string");
Eif (this.fields.languages && this.fields.languages && this.fields.languages.json) {
this.confirm("Do you want to set the languages from " + JSON.stringify(this.fields.languages.json) + " to " + value);
}
stringvalue = value;
objectvalue = {
value: value,
label: "languages",
json: {
languages: value.split(",")
}
};
objectvalue.json.languages = objectvalue.json.languages.map(function(languageName) {
return {
iso: languageName.toLowerCase().trim(),
name: languageName.trim(),
nativeName: languageName.trim()
};
});
} else {
objectvalue = value;
}
Iif (!this.fields) {
this.fields = new DatumFields(this.defaults.fields);
}
if (stringvalue) {
this.fields.languages.value = stringvalue;
}
this.debug("setting language ", objectvalue);
for (var property in objectvalue) {
Iif (!objectvalue.hasOwnProperty(property)) {
continue;
}
this.debug("looking at " + property);
this.fields.languages[property] = objectvalue[property];
}
}
},
dialects: {
get: function() {
return this.languages;
},
set: function(value) {
return this.languages = value;
}
},
user: {
get: function() {
if (!this.userMask) {
var self = this;
if (this.public && this.username) {
this.userMask = new this.INTERNAL_MODELS["user"]({});
this.userMask.username = this.username;
this.userMask.fetch().then(function(result) {
self.debug("Fetched speaker\"s user mask", result);
}, function(error) {
self.debug("Failed to fetch speaker\"s user mask", error);
}).fail(function(error) {
console.error(error.stack, self);
});
} else {
this.userMask = {};
}
this.userMask = {
username: this.anonymousCode,
gravatar: this.gravatar
};
}
return this.userMask;
},
set: function(value) {
if (value === this.userMask) {
return;
}
if (!value) {
value = {};
}
this.userMask = value;
}
},
languageOne: {
get: function() {
return this.getLanguageNumber(0);
},
set: function(value) {
return this.setLanguageNumber(0, value);
}
},
languageTwo: {
get: function() {
return this.getLanguageNumber(1);
},
set: function(value) {
return this.setLanguageNumber(1, value);
}
},
languageThree: {
get: function() {
return this.getLanguageNumber(2);
},
set: function(value) {
return this.setLanguageNumber(2, value);
}
},
languageFour: {
get: function() {
return this.getLanguageNumber(3);
},
set: function(value) {
return this.setLanguageNumber(3, value);
}
},
languageFive: {
get: function() {
return this.getLanguageNumber(4);
},
set: function(value) {
return this.setLanguageNumber(4, value);
}
},
getLanguageNumber: {
value: function(number) {
Iif (!this.fields || !this.fields.languages || !this.fields.languages.json || !this.fields.languages.json.languages || !this.fields.languages.json.languages[number]) {
return;
}
return this.fields.languages.json.languages[number];
}
},
setLanguageNumber: {
value: function(number, value) {
if (!this.fields || !this.fields.languages) {
return;
}
this.fields.languages.json = this.fields.languages.json || {
languages: []
};
if (value === this.fields.languages.json.languages[number]) {
return;
}
if (value.iso) {
value = {
language: value,
fluency: {
"comprehensionFluency": "native",
"speakingFluency": "native"
},
dates: {
start: "",
end: "",
proportionOfUse: ""
}
};
}
value.fluency = value.fluency || {};
value.dates = value.dates || {};
value.language = value.language || {};
this.fields.languages.json.languages[number] = value;
return this.fields.languages.json.languages[number];
}
}
});
exports.Speaker = Speaker;
|