PageRenderTime 19ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/ucengine/src/controllers/presence_controller.erl

http://github.com/AF83/ucengine
Erlang | 76 lines | 51 code | 8 blank | 17 comment | 0 complexity | 5eff7cdd11debbf9a22fe648f7718662 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(presence_controller).
  19. -export([init/0, delete/4, get/4, add/4]).
  20. -include("uce.hrl").
  21. init() ->
  22. [#uce_route{method='POST',
  23. path=["presence"],
  24. callback={?MODULE, add,
  25. [{"name", required, string},
  26. {"credential", "", string},
  27. {"timeout", 0, integer}]}},
  28. #uce_route{method='GET',
  29. path=["presence", sid],
  30. callback={?MODULE, get, []}},
  31. #uce_route{method='DELETE',
  32. path=["presence", sid],
  33. callback={?MODULE, delete,
  34. [{"uid", required, string},
  35. {"sid", required, string}]}}].
  36. add(Domain, [], [Name, Credential, Timeout], _) ->
  37. {ok, #uce_user{id = Uid} = User} = uce_user:get_by_name(Domain, Name),
  38. {ok, true} = uce_access:assert(Domain, Uid, "", "presence", "add"),
  39. {ok, true} = ?AUTH_MODULE(User#uce_user.auth):assert(User, Credential),
  40. {ok, Sid} = uce_presence:add(Domain,
  41. #uce_presence{id=none,
  42. user=Uid,
  43. timeout=Timeout,
  44. auth=User#uce_user.auth}),
  45. {ok, _} = uce_event:add(Domain,
  46. #uce_event{id=none,
  47. from=User#uce_user.id,
  48. location="",
  49. type="internal.presence.add"}),
  50. json_helpers:json(Domain, 201, {struct, [{uid, Uid}, {sid, Sid}]}).
  51. get(Domain, [{sid, Sid}], [], _) ->
  52. case uce_presence:get(Domain, Sid) of
  53. {ok, Presence} ->
  54. json_helpers:json(Domain, Presence);
  55. {error, not_found} ->
  56. json_helpers:error(Domain, not_found)
  57. end.
  58. delete(Domain, [{sid, Sid2}], [Uid, Sid], _) ->
  59. {ok, true} = uce_presence:assert(Domain, Uid, Sid),
  60. case uce_presence:get(Domain, Sid2) of
  61. {ok, #uce_presence{id=Id}} ->
  62. {ok, true} = uce_access:assert(Domain, Uid, "", "presence", "delete",
  63. [{"id", Id}]),
  64. {ok, deleted} = uce_presence:delete(Domain, Sid2),
  65. json_helpers:ok(Domain);
  66. {error, not_found} ->
  67. json_helpers:error(Domain, not_found)
  68. end.