PageRenderTime 193ms CodeModel.GetById 90ms app.highlight 10ms RepoModel.GetById 91ms 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
20-export([init/0, delete/4, get/4, add/4]).
21
22-include("uce.hrl").
23
24init() ->
25    [#uce_route{method='POST',
26                path=["presence"],
27                callback={?MODULE, add,
28                          [{"name", required, string},
29                           {"credential", "", string},
30                           {"timeout", 0, integer}]}},
31
32     #uce_route{method='GET',
33                path=["presence", sid],
34                callback={?MODULE, get, []}},
35
36     #uce_route{method='DELETE',
37                path=["presence", sid],
38                callback={?MODULE, delete,
39                          [{"uid", required, string},
40                           {"sid", required, string}]}}].
41
42add(Domain, [], [Name, Credential, Timeout], _) ->
43    {ok, #uce_user{id = Uid} = User} = uce_user:get_by_name(Domain, Name),
44    {ok, true} = uce_access:assert(Domain, Uid, "", "presence", "add"),
45    {ok, true} = ?AUTH_MODULE(User#uce_user.auth):assert(User, Credential),
46    {ok, Sid} = uce_presence:add(Domain,
47                                 #uce_presence{id=none,
48                                               user=Uid,
49                                               timeout=Timeout,
50                                               auth=User#uce_user.auth}),
51    {ok, _} = uce_event:add(Domain,
52                            #uce_event{id=none,
53                                       from=User#uce_user.id,
54                                       location="",
55                                       type="internal.presence.add"}),
56    json_helpers:json(Domain, 201, {struct, [{uid, Uid}, {sid, Sid}]}).
57
58get(Domain, [{sid, Sid}], [], _) ->
59    case uce_presence:get(Domain, Sid) of
60        {ok, Presence} ->
61            json_helpers:json(Domain, Presence);
62        {error, not_found} ->
63            json_helpers:error(Domain, not_found)
64    end.
65
66delete(Domain, [{sid, Sid2}], [Uid, Sid], _) ->
67    {ok, true} = uce_presence:assert(Domain, Uid, Sid),
68    case uce_presence:get(Domain, Sid2) of
69        {ok, #uce_presence{id=Id}} ->
70            {ok, true} = uce_access:assert(Domain, Uid, "", "presence", "delete",
71                                           [{"id", Id}]),
72            {ok, deleted} = uce_presence:delete(Domain, Sid2),
73            json_helpers:ok(Domain);
74        {error, not_found} ->
75            json_helpers:error(Domain, not_found)
76    end.