-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
131 lines (114 loc) · 3.49 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
var Promise = require("bluebird")
var lodash = require("lodash")
var assert = require("assert")
var uuid = require("uuid/v4")
var highland = require("highland")
var State = require('statesync')
var utils = require("./utils")
var Room = require('./room')
module.exports = function (resume, upsert, remove) {
resume = resume || []
upsert = upsert || function () {};
remove = remove || function () {};
const DEFAULT_ROOMS = ["en", "ru", "us", "de", "pl", "fr", "cn"];
const PRIMARY_ROOM = "en";
var Rooms = State({});
function getRoom(id) {
var room = Rooms(id)
assert(room, 'Room not found with that id')
return room
}
function getRoomScope(id) {
var room = Room(Rooms.scope(id))
assert(room, 'Room not found with that id')
return room
}
function closeScope(room) {
if (room) room.disconnect()
}
Rooms.listRooms = function () {
return lodash.keys(Rooms())
}
Rooms.getRoom = function (roomid) {
roomid = roomid || PRIMARY_ROOM;
assert(Rooms.has(roomid), 'Room does not exist!')
return getRoom(roomid)
}
Rooms.createRoom = function (roomid) {
assert(!Rooms.has(roomid), 'Room already exists!')
var room = Room().create(roomid)
upsert(room)
Rooms.set(room.id, room)
return room
}
Rooms.removeMessage = Promise.method(function (messageid, roomid) {
assert(messageid, "requires messageid");
roomid = roomid || PRIMARY_ROOM;
var room = getRoomScope(roomid)
room.removeMessage(messageid)
upsert(room.get())
closeScope(room)
return true
})
Rooms.removeUserMessages = Promise.method(function (userid, roomid) {
assert(messageid, "requires messageid");
roomid = roomid || PRIMARY_ROOM;
var room = getRoomScope(roomid)
room.removeUserMessages(userid)
upsert(room.get())
closeScope(room)
return true
})
Rooms.removeRoom = Promise.method(function (roomid) {
assert(roomid, "requires roomid");
assert(roomid != PRIMARY_ROOM, `You cannot delete the ${PRIMARY_ROOM} room.`);
Rooms.delete(roomid)
remove(roomid)
return true;
});
function formatMessage(message, user) {
message = lodash.escape(message)
return {
id: uuid(),
user: utils.stripUser(user),
message: utils.linkify(message),
time: Date.now()
};
}
Rooms.speak = Promise.method(function (message, user, roomid) {
roomid = roomid || PRIMARY_ROOM;
assert(message, "you cant send empty message");
assert(lodash.isString(message), "message is not a string!");
assert(
message.replace(/\s+/g, '').length > 1,
"You need to type at least a 2 character word..."
);
var formattedMessage = formatMessage(message, user);
assert(formattedMessage.message, "You need to say somthing...");
assert(formattedMessage.message != "", "You need to say somthing...");
assert(
formattedMessage.message.split(" ").length > 0,
"you need to say more than one word..."
);
var room = getRoomScope(roomid)
room.addMessage(formattedMessage)
upsert(room.get())
closeScope(room)
return formattedMessage
});
function init() {
return Promise.fromCallback(done => {
highland(resume)
.doto(room => {
return Rooms.set(room.id, room)
})
.errors(done)
.done(function () {
var neededRooms = lodash.difference(DEFAULT_ROOMS, Rooms.listRooms())
lodash.each(neededRooms, Rooms.createRoom);
return done(null, Rooms);
});
});
}
return init();
};