PageRenderTime 37ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/ucengine/src/controllers/event_controller.erl

http://github.com/AF83/ucengine
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. -export([init/0, get/4, list/4, add/4, add2/4, live/4]).
  20. -include("uce.hrl").
  21. -include_lib("yaws/include/yaws_api.hrl").
  22. init() ->
  23. [#uce_route{method='POST',
  24. path=["event", '...'],
  25. content_type="application/json",
  26. callback={?MODULE, add2, []}},
  27. #uce_route{method='POST',
  28. path=["event", '...'],
  29. callback={?MODULE, add,
  30. [{"uid", required, string},
  31. {"sid", required, string},
  32. {"type", required, string},
  33. {"to", "", string},
  34. {"parent", "", string},
  35. {"metadata", [], dictionary}]}},
  36. #uce_route{method='GET',
  37. path=["event", meeting, id],
  38. callback={?MODULE, get,
  39. [{"uid", required, string},
  40. {"sid", required, string}]}},
  41. #uce_route{method='GET',
  42. path=["live", '...'],
  43. callback={?MODULE, live,
  44. [{"uid", required, string},
  45. {"sid", required, string},
  46. {"search", "", string},
  47. {"type", "", string},
  48. {"from", "", string},
  49. {"start", 0, integer},
  50. {"parent", "", string},
  51. {"mode", "longpolling", string}]}},
  52. #uce_route{method='GET',
  53. path=["event", '...'],
  54. callback={?MODULE, list,
  55. [{"uid", required, string},
  56. {"sid", required, string},
  57. {"search", "", string},
  58. {"type", "", string},
  59. {"from", "", string},
  60. {"start", 0, integer},
  61. {"end", infinity, [integer, atom]},
  62. {"count", infinity, [integer, atom]},
  63. {"page", 1, integer},
  64. {"order", asc, atom},
  65. {"parent", "", string}]}}].
  66. add(Domain, [], Params, Arg) ->
  67. add(Domain, [""], Params, Arg);
  68. add(Domain, [Meeting], [Uid, Sid, Type, To, Parent, Metadata], _Arg) ->
  69. {ok, true} = uce_presence:assert(Domain, Uid, Sid),
  70. {ok, true} = uce_access:assert(Domain, Uid, Meeting, "event", "add",
  71. [{"type", Type}, {"to", To}]),
  72. case Type of
  73. "internal."++ _Rest ->
  74. throw({error, unauthorized});
  75. _OtherEvent ->
  76. {ok, Id} = uce_event:add(Domain,
  77. #uce_event{id=none,
  78. location=Meeting,
  79. from=Uid,
  80. type=Type,
  81. to=To,
  82. parent=Parent,
  83. metadata=json_helpers:to_struct(Metadata)}),
  84. json_helpers:created(Domain, Id)
  85. end.
  86. add2(Domain, [], [], Arg) ->
  87. add2(Domain, [""], [], Arg);
  88. add2(Domain, [Meeting], [], Arg) ->
  89. {struct, Json} = mochijson:decode(Arg#arg.clidata),
  90. Uid = proplists:get_value("uid", Json),
  91. Sid = proplists:get_value("sid", Json),
  92. {ok, true} = uce_presence:assert(Domain, Uid, Sid),
  93. Type = proplists:get_value("type", Json),
  94. To = proplists:get_value("to", Json, ""),
  95. {ok, true} = uce_access:assert(Domain, Uid, Meeting, "event", "add",
  96. [{"type", Type}, {"to", To}]),
  97. Parent = proplists:get_value("parent", Json, ""),
  98. Metadata = proplists:get_value("metadata", Json, []),
  99. {ok, Id} = uce_event:add(Domain,
  100. #uce_event{id=none,
  101. location=Meeting,
  102. from=Uid,
  103. type=Type,
  104. to=To,
  105. parent=Parent,
  106. metadata=Metadata}),
  107. json_helpers:created(Domain, Id).
  108. get(Domain, [_, {id, Id}], [Uid, Sid], _) ->
  109. {ok, true} = uce_presence:assert(Domain, Uid, Sid),
  110. {ok, true} = uce_access:assert(Domain, Uid, "", "event", "get", [{"id", Id}]),
  111. {ok, #uce_event{to=To} = Event} = uce_event:get(Domain, Id),
  112. case To of
  113. "" ->
  114. json_helpers:json(Domain, Event);
  115. Uid ->
  116. json_helpers:json(Domain, Event);
  117. _ ->
  118. throw({error, unauthorized})
  119. end.
  120. list(Domain, [], Params, Arg) ->
  121. list(Domain, [""], Params, Arg);
  122. list(Domain, [Meeting],
  123. [Uid, Sid, Search, Type, From, DateStart, DateEnd, Count, Page, Order, Parent], _Arg) ->
  124. {ok, true} = uce_presence:assert(Domain, Uid, Sid),
  125. {ok, true} = uce_access:assert(Domain, Uid, Meeting, "event", "list", [{"from", From}]),
  126. Keywords = string:tokens(Search, ","),
  127. Types = string:tokens(Type, ","),
  128. Start = uce_paginate:index(Count, 0, Page),
  129. {ok, Events} = uce_event:list(Domain,
  130. Meeting,
  131. Keywords,
  132. From,
  133. Types,
  134. Uid,
  135. DateStart,
  136. DateEnd,
  137. Parent,
  138. Start,
  139. Count,
  140. Order),
  141. json_helpers:json(Domain, Events).
  142. live(Domain, [], Params, Arg) ->
  143. live(Domain, [""], Params, Arg);
  144. live(Domain, [Meeting],
  145. [Uid, Sid, Search, Type, From, UserStart, Parent, Mode], Arg) ->
  146. {ok, true} = uce_presence:assert(Domain, Uid, Sid),
  147. {ok, true} = uce_access:assert(Domain, Uid, Meeting, "event", "list", [{"from", From}]),
  148. Start = case get_last_event_id(Arg) of
  149. undefined ->
  150. UserStart;
  151. Value ->
  152. Value + 1
  153. end,
  154. Keywords = string:tokens(Search, ","),
  155. Types = string:tokens(Type, ","),
  156. {ok, PreviousEvents} = uce_event:list(Domain,
  157. Meeting,
  158. Uid,
  159. Keywords,
  160. From,
  161. Types,
  162. Start,
  163. Parent),
  164. case Mode of
  165. "longpolling" ->
  166. uce_async_lp:wait(Domain,
  167. Uid,
  168. Meeting,
  169. Keywords,
  170. From,
  171. Types,
  172. Parent,
  173. PreviousEvents);
  174. "eventsource" ->
  175. uce_async_stream:wait(Domain,
  176. Uid,
  177. Meeting,
  178. Keywords,
  179. From,
  180. Types,
  181. Parent,
  182. Sid,
  183. PreviousEvents);
  184. _ ->
  185. {error, bad_parameters}
  186. end.
  187. get_last_event_id(Arg) ->
  188. Header = "Last-Event-Id",
  189. case lists:keyfind(Header, 3, Arg#arg.headers#headers.other) of
  190. false ->
  191. undefined;
  192. {http_header, _, Header, _, ""} ->
  193. undefined;
  194. {http_header, _, Header, _, Value} ->
  195. list_to_integer(Value)
  196. end.