-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
184 lines (158 loc) · 5.96 KB
/
index.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
const azure = require('azure-storage');
function StorageAccessor(conn) {
var retryOperations = new azure.ExponentialRetryPolicyFilter();
const table = azure.createTableService(conn).withFilter(retryOperations);
this.exec = function (action, ...args) {
return new Promise((resolve, reject) => {
let promiseHandling = (err, result) => {
if (err) {
reject(err);
} else {
resolve(result);
}
};
args.push(promiseHandling);
table[action].apply(table, args);
});
};
this.query = function () {
return new azure.TableQuery();
};
}
function ChatStorageAccessor(conn, tableName) {
var accessor = new StorageAccessor(conn);
this.queryChat = async function (chatKey, count) {
query = accessor.query().where("PartitionKey eq '" + chatKey + "'").select('sentTime', 'content').top(count);
await accessor.exec('createTableIfNotExists', tableName, t => {
if (t) throw t;
});
var chats = (await accessor.exec('queryEntities', tableName, query, null)).entries.map(i => {
return {
time: i.sentTime['_'],
content: i.content['_']
};
}).sort(i => i.time).map(i => i.content).join(',');
return '[' + chats + ']';
};
this.addChat = async function (chatKey, content) {
const date = new Date().toISOString();
var chatEntity = {
PartitionKey: { '_': chatKey },
RowKey: { '_': date },
sentTime: date,
content: { '_': content },
};
await accessor.exec('insertOrReplaceEntity', tableName, chatEntity);
}
this.queryEntity = async function (partitionKey, columnName, count) {
var query = accessor.query().where(`PartitionKey eq '${partitionKey}'`).select(columnName).top(count);
return (await accessor.exec('queryEntities', tableName, query, null)).entries.map(i => i[columnName]['_']).sort();
};
this.updateEntity = async function (entity) {
await accessor.exec('createTableIfNotExists', tableName);
await accessor.exec('insertOrReplaceEntity', tableName, entity);
};
this.removeEntity = function (partitionKey, rowKey) {
var entity = {
PartitionKey: { '_': partitionKey },
RowKey: { '_': rowKey },
}
return accessor.exec('deleteEntity', tableName, entity);
};
}
function UsersAccessor(accessor, context) {
const usersKey = '_users';
this.user = userName => new UserAccessor(accessor, userName, context);
this.add = function (user) {
var userEntity = {
PartitionKey: { '_': usersKey },
RowKey: { '_': user },
user: { '_': user },
connectTime: { '_': new Date().toISOString() },
}
return accessor.updateEntity(userEntity);
};
this.remove = async function (user) {
try {
await accessor.removeEntity(usersKey, user);
} catch (err) { }
};
this.load = function (count) {
return accessor.queryEntity(usersKey, 'user', count);
};
}
function UserAccessor(accessor, user, context) {
const getChatKey = function (recipient) {
return '_chats_user_' + [user, recipient].sort().join(';');
};
this.load = function (recipient, count = 20) {
const chatKey = getChatKey(recipient);
return accessor.queryChat(chatKey, count);
};
this.addChat = function (recipient, content) {
const chatKey = getChatKey(recipient);
return accessor.addChat(chatKey, content);
}
}
function GroupAccessor(accessor, group, context) {
const chatKey = '_chats_group_' + group;
this.load = function (count = 20) {
return accessor.queryChat(chatKey, count);
};
this.addChat = function (content) {
return accessor.addChat(chatKey, content);
};
}
function PublicChatAccessor(accessor, context) {
const chatKey = '_chats_broadcast';
this.load = function (count = 20) {
return accessor.queryChat(chatKey, count);
};
this.addChat = function (content) {
return accessor.addChat(chatKey, content);
};
}
function UserGroupsChatAccessor(accessor, context) {
const getChatKey = user => '_usergroups_' + user;
this.load = function (user, count) {
return accessor.queryEntity(getChatKey(user), 'group', count);
}
this.add = function (group, user) {
const date = new Date().toISOString();
var userGroup = {
PartitionKey: { '_': getChatKey(user) },
RowKey: { '_': group },
user: { '_': user },
joinedTime: { '_': date },
group: { '_': group },
}
return accessor.updateEntity(userGroup);
}
this.remove = async function (group, user) {
try {
await accessor.removeEntity(getChatKey(user), group);
} catch (err) { }
}
}
function ChatStorageManager(accessor, context) {
this.users = new UsersAccessor(accessor, context);
this.public = new PublicChatAccessor(accessor, context);
this.usergroups = new UserGroupsChatAccessor(accessor, context);
this.group = groupName => new GroupAccessor(accessor, groupName, context);
this.user = userName => new UserAccessor(accessor, userName, context);
}
function ChatAccessor() { }
ChatAccessor.prototype.accessor = function (conn, tableName, context) {
var conn = process.env['AzureWebJobsStorage'];
var tableName = process.env['ChatTableName']
context = context || console;
var accessor = new ChatStorageAccessor(conn, tableName);
return new ChatStorageManager(accessor, context);
}
ChatAccessor.prototype.default = function (context) {
var conn = process.env['AzureWebJobsStorage'];
var tableName = process.env['ChatTableName']
return this.accessor(conn, tableName, context);
}
var accessor = new ChatAccessor();
module.exports = accessor;