-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdoc-invites.js
160 lines (139 loc) · 4.83 KB
/
doc-invites.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
//noinspection JSLint
var g = typeof global === 'undefined' ? window : global;
//noinspection JSLint,JSHint
(function (papyrus) {
require('./doc');
var Doc = papyrus.Doc;
var Auth = papyrus.Auth;
var spcf = papyrus.spcf;
papyrus.user2contact = function (user) {
return {
uid: user.uid,
provider: '',
kind: 'user',
contactId: user.login || '',
inviteId: '',
auth: user.auth,
dn: user.displayName,
avatarUrl: user.avatarUrl,
exit: user.exit,
contribution: user.contribution
};
};
Doc._p.getInviteLink = function () {
var doc = this;
var inv = doc.get('.ui') || '';
return inv ? papyrus.getInviteLinkBySpec(doc.spec + inv) : null;
};
Doc._p.getInvite = function (invId) {
var doc = this;
var contactJSON = doc.get(invId + '.ii');
if (!contactJSON) { return null; }
var contact = JSON.parse(contactJSON);
contact.inviteId = invId;
contact.kind = 'invite';
contact.auth = doc.get(invId + '.ic') || contact.auth;
return contact;
};
Doc._p.getAllInvites = function () {
var doc = this;
var invIdList = spcf.split(doc.get('.IL') || '');
var res = [];
var i, len;
for (i = 0, len = invIdList.length; i < len; i++) {
var invId = invIdList[i];
var invite = doc.getInvite(invId);
if (!invite || Auth.nobody.has(invite.auth)) { continue; }
res.push(invite);
}
return res;
};
Doc._p.getInviteCount = function () {
var doc = this;
var invIdList = spcf.split(doc.get('.IL') || '');
var count = 0;
var i, len;
for (i = 0, len = invIdList.length; i < len; i++) {
var inviteId = invIdList[i];
var lastNotified = doc.get(inviteId + '.it');
if (lastNotified) {
lastNotified = new Date(lastNotified);
}
var now = new Date();
var oneDay = 24 * 60 * 60 * 1000; // 1day
if (lastNotified &&
(now.getTime() - lastNotified.getTime()) < oneDay) {
count++;
}
}
return count;
};
function onAddressBook(spec, abook) {
try {
abook = JSON.parse(abook);
} catch (err) {
console.warn('address book contacts not parsed');
}
var contacts = abook.data || [];
papyrus.emit('contacts', contacts, abook.page >= abook.pagesCount);
}
function onInvitesListChange(spec, invitesList) {
var doc = this;
var invitesList = doc.getAllInvites();
papyrus.emit('invitesCount', invitesList.length);
}
function onInviteAuthChange(spec, auth) {
if (spcf.pattern(spec) !== '$.') { return; }
var doc = this;
var invId = spcf.get(spec, '$');
var err;
auth = Auth.as(auth);
//change invite authority
var contactJSON = doc.get(invId + '.ii');
if (!contactJSON) {
err = new Error(invId);
err.code = 'invite_not_found';
throw err;
}
var contact;
try {
contact = JSON.parse(contactJSON);
} catch (ex) {
var error = new Error(invId);
error.code = 'error_parsing_invite';
throw error;
}
contact.auth = auth.code;
doc.set(invId + '.ii', JSON.stringify(contact));
papyrus.emit('inviteChanged', doc.getInvite(invId));
}
function onInviteChange(spec) {
//skip spec with striped id
if (spcf.pattern(spec) !== '$.') { return; }
var doc = this;
var invId = spcf.get(spec, '$');
var contact = doc.getInvite(invId);
var invIdList = doc.get('.IL') || '';
if (Auth.nobody.has(contact.auth)) {
if (invIdList.indexOf(invId) > -1) {
doc.set('.IL', spcf.replace(invIdList, invId, ''));
}
} else if (invIdList.indexOf(invId) === -1) {
doc.set('.IL', spcf.add(invIdList, invId));
}
papyrus.emit('inviteChanged', contact);
}
function onShortInviteUrl(spec, shortInviteUrl) {
this.shortInviteUrl = shortInviteUrl;
papyrus.emit('shortInviteUrlChanged', this, shortInviteUrl);
}
var stripSeqMark = Doc._p.stripSeqMark;
papyrus.addFilter(".ui", stripSeqMark);
papyrus.addFilter('.ic', stripSeqMark);
papyrus.addFilter('.ii', stripSeqMark);
papyrus.addFilter(',ab', onAddressBook);
papyrus.addFilter('.IL', onInvitesListChange);
papyrus.addFilter('.ic', onInviteAuthChange);
papyrus.addFilter('.ii', onInviteChange);
papyrus.addFilter(',si', onShortInviteUrl);
}(g['papyrus'] = g['papyrus'] || {}));