This repository has been archived by the owner on Jun 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
messageStore.js
70 lines (60 loc) · 1.91 KB
/
messageStore.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
if (!atc) var atc = {}
if (!atc.setup) atc.setup = {}
atc.setup.createMailStore = function () {
// console.log('setup mailStore')
// test for local storage and make a ref to it if true
var storage = atc.provider.storage
var usersMailsStore = {}
// if there is a storage build initial store from it
if (storage) {
for (var key in storage) {
var userData = locGet(key, storage)
usersMailsStore[key] = userData
}
}
function createUser (user) {
var uName = user.toLowerCase()
if (usersMailsStore[uName]) return
usersMailsStore[uName] = createUserStore()
}
function createUserStore (uName) {
if (storage && storage[uName] !== undefined) {
return locGet(uName, storage)
} else return {inbox: [], outbox: []}
}
function locSet (user, data, locStore) {
locStore.setItem(user.toLowerCase(), JSON.stringify(data))
}
function locGet (user, locStore) {
if (locStore.getItem(user.toLowerCase()) !== null) {
return JSON.parse(locStore[user.toLowerCase()])
}
}
function getMails (user) {
var uName = user.toLowerCase()
if (!usersMailsStore[uName]) return createUserStore(uName)
else return usersMailsStore[uName]
}
function sendMail (msg) {
var from = msg.from.toLowerCase()
var to = msg.to.toLowerCase()
// test if the user boxes exist, and create them if not
if (!usersMailsStore[to]) usersMailsStore[to] = createUserStore()
if (!usersMailsStore[from]) usersMailsStore[from] = createUserStore()
// push mail in inbox
usersMailsStore[to].inbox.push(msg)
usersMailsStore[from].outbox.push(msg)
// set if localstorage if available
if (storage) locSet(from, getMails(from), storage)
}
return {
msgStore: usersMailsStore,
createUser: createUser,
getMail: getMails,
sendMail: sendMail,
storage: storage
}
}
atc.setup.init = function (atcO) {
atcO.msgStore = this.createMailStore()
}