-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
142 lines (122 loc) · 3.16 KB
/
app.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
var flx = require('./lib/flx'),
web = require('./lib/web'),
api = require('./public/endpoints.js');
web.static('/console', __dirname + '/console');
web.static('/public', __dirname + '/public');
web.static('/', __dirname + '/client');
web.route(api.read(), 'read', 'input');
web.route(api.register(), 'register', 'input');
web.route(api.follow(), 'follow', 'input');
web.route(api.followers(), 'followers', 'input');
web.route(api.post(), 'post', 'input');
function user(msg) {
this.read = function(msg) {
return this.m('output',
{
cid: msg.cid,
source: this.name,
data: this.messages
});
};
this.post = function(msg) {
this.messages.push(msg.data);
if (msg.data.user === this.name) {
var dest = this.followers.slice(0);
dest.push('output');
return this.m(dest,
{
cid: msg.cid,
source: this.name,
command: 'post',
data: msg.data
});
}
return undefined;
};
this.follow = function(msg) {
if (this.followers.indexOf(msg.data.user) == -1)
this.followers.push(msg.data.user);
return this.m('output',
{
cid: msg.cid,
source: this.name,
data: 'Success: user added to followers'
});
};
this.getFollowers = function(msg) {
return this.m('output',
{
cid: msg.cid,
source: this.name,
data: this.followers
});
};
return this[msg.command](msg);
}
user.scope = function(name) {
return {
name: name,
messages: [],
followers: []
};
};
flx.register('register', function (msg) {
if (!msg.data.user)
return this.m('output',
{
cid: msg.cid,
source: 'register',
data: 'Error: username missing.'
});
// Register the user (a user is a fluxion)
if (flx.register(msg.data.user,
user,
user.scope(msg.data.user)) === false) {
return this.m('output',
{
cid: msg.cid,
source: 'register',
data: 'Error: username already exist'
});
}
return this.m('output',
{
cid: msg.cid,
source: 'register',
data: 'Success: account created'
});
}, {});
flx.register('read', function (msg) {
msg.command = 'read';
msg.source = 'read';
return this.m(msg.data.user, msg);
}, {});
flx.register('post', function (msg) {
msg.command = 'post';
msg.source = 'post';
return this.m('filter', msg);
}, {});
flx.register('filter', function (msg) {
msg.source = 'filter';
var key, keyReplacement;
for (key in this.censured) {
if (msg.data.message.indexOf(key) !== -1) {
keyReplacement = this.censured[key];
msg.data.message = msg.data.message.replace(key, keyReplacement);
}
}
return this.m(msg.data.user, msg);
}, {
censured: {'lol' : 'apple', 'yop' : 'kiwi', 'kikoo': 'abricot' }
});
flx.register('follow', function (msg) {
msg.command = 'follow';
msg.source = 'follow';
return this.m(msg.data.followee, msg);
}, {});
flx.register('followers', function (msg) {
msg.command = 'getFollowers';
msg.source = 'followers';
return this.m(msg.data.user, msg);
}, {});
web.listen();