-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrosterCache.js
77 lines (72 loc) · 1.71 KB
/
rosterCache.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
define(function () {
var rosterCache = null;
/*{
"user@server": {
contacts: {
"[email protected]": {
"name": "Some One",
"ask": "subscribe",
"subscription": "none",
"groups": ["Friends", "Hobby"],
"resources": {
"homelaptop": {
// ...
}, // ... more resources
}
}, // ... more contacts
},
version: "abc123"
}, // ... more users
}*/
var loadFromStorage = function () {
try {
rosterCache = JSON.parse(localStorage["RosterCache"]) || {};
} catch (error) {
rosterCache = {};
}
};
window.addEventListener("storage", function (event) {
if (event.key === "RosterCache") {
loadFromStorage();
}
}, false);
return {
save: function (jid, roster) {
if (!rosterCache) {
loadFromStorage();
}
var cloneCache = {
version: roster.version,
contacts: {}
};
Object.keys(roster.contacts).forEach(function (contactJid) {
cloneCache.contacts[contactJid] = {resources: {}};
["name", "ask", "subscription", "groups"].forEach(function (property) {
cloneCache.contacts[contactJid][property] = roster.contacts[contactJid][property];
});
});
rosterCache[jid] = cloneCache;
try {
localStorage["RosterCache"] = JSON.stringify(rosterCache);
} catch (error) {
console.warn("[RosterCache] Failed to persist roster cache");
}
},
load: function (jid) {
if (!rosterCache) {
loadFromStorage();
}
return Object.hasOwnProperty.call(rosterCache, jid)
? rosterCache[jid]
: {version: "", contacts: {}};
},
version: function (jid) {
if (!rosterCache) {
loadFromStorage();
}
return Object.hasOwnProperty.call(rosterCache, jid)
? rosterCache[jid].version
: "";
}
};
});