/ucengine/src/controllers/event_controller.erl
Erlang | 217 lines | 179 code | 21 blank | 17 comment | 0 complexity | b8fffe17b381ddf682f12db546d9c6f2 MD5 | raw file
1%% 2%% U.C.Engine - Unified Collaboration Engine 3%% Copyright (C) 2011 af83 4%% 5%% This program is free software: you can redistribute it and/or modify 6%% it under the terms of the GNU Affero General Public License as published by 7%% the Free Software Foundation, either version 3 of the License, or 8%% (at your option) any later version. 9%% 10%% This program is distributed in the hope that it will be useful, 11%% but WITHOUT ANY WARRANTY; without even the implied warranty of 12%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13%% GNU Affero General Public License for more details. 14%% 15%% You should have received a copy of the GNU Affero General Public License 16%% along with this program. If not, see <http://www.gnu.org/licenses/>. 17%% 18-module(event_controller). 19 20-export([init/0, get/4, list/4, add/4, add2/4, live/4]). 21 22-include("uce.hrl"). 23-include_lib("yaws/include/yaws_api.hrl"). 24 25init() -> 26 [#uce_route{method='POST', 27 path=["event", '...'], 28 content_type="application/json", 29 callback={?MODULE, add2, []}}, 30 31 #uce_route{method='POST', 32 path=["event", '...'], 33 callback={?MODULE, add, 34 [{"uid", required, string}, 35 {"sid", required, string}, 36 {"type", required, string}, 37 {"to", "", string}, 38 {"parent", "", string}, 39 {"metadata", [], dictionary}]}}, 40 41 #uce_route{method='GET', 42 path=["event", meeting, id], 43 callback={?MODULE, get, 44 [{"uid", required, string}, 45 {"sid", required, string}]}}, 46 47 #uce_route{method='GET', 48 path=["live", '...'], 49 callback={?MODULE, live, 50 [{"uid", required, string}, 51 {"sid", required, string}, 52 {"search", "", string}, 53 {"type", "", string}, 54 {"from", "", string}, 55 {"start", 0, integer}, 56 {"parent", "", string}, 57 {"mode", "longpolling", string}]}}, 58 59 #uce_route{method='GET', 60 path=["event", '...'], 61 callback={?MODULE, list, 62 [{"uid", required, string}, 63 {"sid", required, string}, 64 {"search", "", string}, 65 {"type", "", string}, 66 {"from", "", string}, 67 {"start", 0, integer}, 68 {"end", infinity, [integer, atom]}, 69 {"count", infinity, [integer, atom]}, 70 {"page", 1, integer}, 71 {"order", asc, atom}, 72 {"parent", "", string}]}}]. 73 74add(Domain, [], Params, Arg) -> 75 add(Domain, [""], Params, Arg); 76add(Domain, [Meeting], [Uid, Sid, Type, To, Parent, Metadata], _Arg) -> 77 {ok, true} = uce_presence:assert(Domain, Uid, Sid), 78 {ok, true} = uce_access:assert(Domain, Uid, Meeting, "event", "add", 79 [{"type", Type}, {"to", To}]), 80 case Type of 81 "internal."++ _Rest -> 82 throw({error, unauthorized}); 83 _OtherEvent -> 84 {ok, Id} = uce_event:add(Domain, 85 #uce_event{id=none, 86 location=Meeting, 87 from=Uid, 88 type=Type, 89 to=To, 90 parent=Parent, 91 metadata=json_helpers:to_struct(Metadata)}), 92 json_helpers:created(Domain, Id) 93 end. 94 95add2(Domain, [], [], Arg) -> 96 add2(Domain, [""], [], Arg); 97add2(Domain, [Meeting], [], Arg) -> 98 {struct, Json} = mochijson:decode(Arg#arg.clidata), 99 Uid = proplists:get_value("uid", Json), 100 Sid = proplists:get_value("sid", Json), 101 {ok, true} = uce_presence:assert(Domain, Uid, Sid), 102 103 Type = proplists:get_value("type", Json), 104 To = proplists:get_value("to", Json, ""), 105 {ok, true} = uce_access:assert(Domain, Uid, Meeting, "event", "add", 106 [{"type", Type}, {"to", To}]), 107 Parent = proplists:get_value("parent", Json, ""), 108 Metadata = proplists:get_value("metadata", Json, []), 109 {ok, Id} = uce_event:add(Domain, 110 #uce_event{id=none, 111 location=Meeting, 112 from=Uid, 113 type=Type, 114 to=To, 115 parent=Parent, 116 metadata=Metadata}), 117 json_helpers:created(Domain, Id). 118 119get(Domain, [_, {id, Id}], [Uid, Sid], _) -> 120 {ok, true} = uce_presence:assert(Domain, Uid, Sid), 121 {ok, true} = uce_access:assert(Domain, Uid, "", "event", "get", [{"id", Id}]), 122 {ok, #uce_event{to=To} = Event} = uce_event:get(Domain, Id), 123 case To of 124 "" -> 125 json_helpers:json(Domain, Event); 126 Uid -> 127 json_helpers:json(Domain, Event); 128 _ -> 129 throw({error, unauthorized}) 130 end. 131 132list(Domain, [], Params, Arg) -> 133 list(Domain, [""], Params, Arg); 134list(Domain, [Meeting], 135 [Uid, Sid, Search, Type, From, DateStart, DateEnd, Count, Page, Order, Parent], _Arg) -> 136 137 {ok, true} = uce_presence:assert(Domain, Uid, Sid), 138 {ok, true} = uce_access:assert(Domain, Uid, Meeting, "event", "list", [{"from", From}]), 139 140 Keywords = string:tokens(Search, ","), 141 Types = string:tokens(Type, ","), 142 143 Start = uce_paginate:index(Count, 0, Page), 144 {ok, Events} = uce_event:list(Domain, 145 Meeting, 146 Keywords, 147 From, 148 Types, 149 Uid, 150 DateStart, 151 DateEnd, 152 Parent, 153 Start, 154 Count, 155 Order), 156 json_helpers:json(Domain, Events). 157 158live(Domain, [], Params, Arg) -> 159 live(Domain, [""], Params, Arg); 160live(Domain, [Meeting], 161 [Uid, Sid, Search, Type, From, UserStart, Parent, Mode], Arg) -> 162 163 {ok, true} = uce_presence:assert(Domain, Uid, Sid), 164 {ok, true} = uce_access:assert(Domain, Uid, Meeting, "event", "list", [{"from", From}]), 165 166 Start = case get_last_event_id(Arg) of 167 undefined -> 168 UserStart; 169 Value -> 170 Value + 1 171 end, 172 173 Keywords = string:tokens(Search, ","), 174 Types = string:tokens(Type, ","), 175 176 {ok, PreviousEvents} = uce_event:list(Domain, 177 Meeting, 178 Uid, 179 Keywords, 180 From, 181 Types, 182 Start, 183 Parent), 184 case Mode of 185 "longpolling" -> 186 uce_async_lp:wait(Domain, 187 Uid, 188 Meeting, 189 Keywords, 190 From, 191 Types, 192 Parent, 193 PreviousEvents); 194 "eventsource" -> 195 uce_async_stream:wait(Domain, 196 Uid, 197 Meeting, 198 Keywords, 199 From, 200 Types, 201 Parent, 202 Sid, 203 PreviousEvents); 204 _ -> 205 {error, bad_parameters} 206 end. 207 208get_last_event_id(Arg) -> 209 Header = "Last-Event-Id", 210 case lists:keyfind(Header, 3, Arg#arg.headers#headers.other) of 211 false -> 212 undefined; 213 {http_header, _, Header, _, ""} -> 214 undefined; 215 {http_header, _, Header, _, Value} -> 216 list_to_integer(Value) 217 end.