forked from remotestorage/modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontacts.js
207 lines (184 loc) · 5.85 KB
/
contacts.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
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
if(!RemoteStorage) {
RemoteStorage = remoteStorage;
}
RemoteStorage.defineModule('contacts', function(privateClient, publicClient) {
/**
*
* Apart from the "contacts" module, this file contains a
* proof-of-concept ngrams based search index.
*
* The code is highly redundant and needs refactoring for real-world
* usecases. However - it works!
*
*/
var util = remoteStorage.util;
var indexPathPrefix = "index/";
function rmRf(path) {
if(util.isDir(path)) {
return privateClient.getListing(path).then(function(items) {
return util.asyncEach(items, function(item) {
return rmRf(path + item);
});
});
} else {
return privateClient.remove(path);
}
}
// declaring data type "contact"
privateClient.declareType('contact', {
"$schema": "http://json-schema.org/draft-03/schema#",
"description": "A representation of a person, company, organization, or place",
"type": "object",
"properties": {
"fn": {
"description": "Formatted Name",
"type": "string",
"required": true
},
"familyName": { "type": "string" },
"givenName": { "type": "string" },
"additionalName": { "type": "array", "items": { "type": "string" } },
"honorificPrefix": { "type": "array", "items": { "type": "string" } },
"honorificSuffix": { "type": "array", "items": { "type": "string" } },
"nickname": { "type": "string" },
"url": { "type": "string", "format": "uri" },
"emails": {
"type": "array",
"items": {
"type": "object",
"properties": {
"type": { "type": "string" },
"value": { "type": "string", "format": "email" }
}
}
},
"tels": {
"type": "array",
"items": {
"type": "object",
"properties": {
"type": { "type": "string" },
"value": { "type": "string", "format": "phone" }
}
}
},
"adr": { "$ref": "http: //json-schema.org/address" },
"geo": { "$ref": "http: //json-schema.org/geo" },
"tz": { "type": "string" },
"photo": { "type": "string" },
"logo": { "type": "string" },
"sound": { "type": "string" },
"bday": { "type": "string", "format": "date" },
"title": { "type": "string" },
"role": { "type": "string" },
"org": {
"type": "object",
"properties": {
"organizationName": { "type": "string" },
"organizationUnit": { "type": "string" }
}
},
"impp": {
"type": "string",
"format": "uri"
}
}
});
function indexNodePath(type, attributeKey, attributeValue) {
return indexPathPrefix + type + '/' + encodeURIComponent(attributeKey) + '/' + encodeURIComponent(attributeValue);
}
function queryIndex(type, attributeKey, attributeValue) {
return privateClient.getObject(indexNodePath(type, attributeKey, attributeValue)).
then(function(list) {
if(! list) {
return [];
} else {
return util.asyncMap(list, function(id) {
return privateClient.getObject('card/' + id);
});
}
});
}
function indexAttribute(type, id, attributeKey, attributeValue) {
var path = indexNodePath(type, attributeKey, attributeValue);
return privateClient.getObject(path).then(function(list) {
return privateClient.storeObject('index-node', path, (list || []).concat([id]));
});
}
function unindexAttribute(type, id, attributeKey, attributeValue) {
var path = indexNodePath(type, attributeKey, attributeValue);
return privateClient.getObject(path).then(function(list) {
var newList = [];
if(list) {
list.forEach(function(item) {
if(item !== id) {
newList.push(item);
}
});
return privateClient.storeObject(path);
}
});
}
var INDEX_ATTRIBUTES = { email: true, impp: true };
var INDEX_ATTRIBUTE_KEYS = Object.keys(INDEX_ATTRIBUTES);
function indexContact(contact) {
return util.asyncEach(INDEX_ATTRIBUTE_KEYS, function(key) {
return indexAttribute('contact', contact.id, key, contact[key]);
});
}
function unindexContact(contact) {
return util.asyncEach(INDEX_ATTRIBUTE_KEYS, function(key) {
return unindexAttribute('contact', contact.id, key, contact[key]);
});
}
return {
exports: {
on: privateClient.on,
getAll: function() {
return privateClient.getListing('card/').then(function(ids) {
return util.asyncMap(ids, function(id) {
return privateClient.getObject('card/' + id);
});
});
},
byKey: function(key, value) {
if(! INDEX_ATTRIBUTES[key]) {
throw new Error("Key '" + key + "' is not indexed!");
}
return queryIndex('contact', key, value);
},
add: function (contact) {
return this.save(contact);
},
save: function (contact) {
if (!contact.id) {
contact.id = privateClient.uuid();
}
return privateClient.storeObject("contact", "card/" + contact.id, contact).
then(function() {
// don't wait until indexing is done. instead return immediately.
indexContact(contact);
});
},
get: function (uuid) {
return privateClient.getObject("card/" + uuid);
},
remove: function(contact) {
return privateClient.remove('card/' + contact.id).
then(function() {
unindexContact(contact);
});
},
rebuildIndex: function() {
return this.clearIndex().
then(this.all).
then(function(contacts) {
return util.asyncEach(contacts, indexContact);
});
},
clearIndex: function() {
return rmRf('index/');
}
}
};
});