/
index.erl
62 lines (55 loc) 路 1.86 KB
/
index.erl
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
-module(index).
-compile(export_all).
-include_lib("nitro/include/nitro.hrl").
-include_lib("n2o/include/n2o.hrl").
-include_lib("kvs/include/cursors.hrl").
event(init) ->
Room = n2o:session(room),
Key = {topic, Room},
n2o:reg(Key),
n2o:reg(n2o:sid()),
nitro:clear(history),
nitro:update(logout,
#button{id = logout, body = "Logout " ++ n2o:user(),
postback = logout}),
nitro:update(heading, [#h2{id = heading, body = Room}]),
nitro:update(upload, #upload{}),
nitro:update(send,
#button{id = send, body = <<"Chat">>, postback = chat,
source = [message]}),
[event(#client{data = E})
|| E <- lists:reverse(kvs:feed(Key))];
event(logout) ->
n2o:user([]),
nitro:redirect("/app/login.htm");
event(chat) -> chat(nitro:q(message), nitro);
event(#client{data =
{'$msg', _, _, _, User, Message}}) ->
nitro:wire(#jq{target = message,
method = [focus, select]}),
nitro:insert_top(history,
nitro:render(#message{body =
[#author{body = User}, Message]}));
event(#ftp{sid = Sid, filename = Filename,
status = {event, stop}} =
Data) ->
Name =
hd(lists:reverse(string:tokens(nitro:to_list(Filename),
"/"))),
chat(nitro:render(#link{href =
iolist_to_binary(["/app/", Sid, "/", Name]),
body = Name}),
index);
event(Event) -> ok.
jse(X) -> X.
chat(Message, F) ->
Room = n2o:session(room),
User = n2o:user(),
Msg = {'$msg',
kvs:seq([], []),
[],
[],
User,
F:jse(Message)},
kvs:append(Msg, {topic, Room}),
n2o:send({topic, Room}, #client{data = Msg}).