PageRenderTime 183ms CodeModel.GetById 80ms app.highlight 35ms RepoModel.GetById 66ms app.codeStats 0ms

/ucengine/src/controllers/meeting_controller.erl

http://github.com/AF83/ucengine
Erlang | 144 lines | 109 code | 17 blank | 18 comment | 0 complexity | a1e949dad9dc793383aacaca04185e3e 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(meeting_controller).
 19
 20-export([init/0, add/4, list/4, get/4, update/4, leave/4, join/4, roster/4]).
 21
 22-include("uce.hrl").
 23
 24init() ->
 25    [#uce_route{method='POST',
 26                path=["meeting"],
 27                callback={?MODULE, add,
 28                          [{"uid", required, string},
 29                           {"sid", required, string},
 30                           {"name", required, string},
 31                           {"metadata", [], dictionary}]}},
 32
 33     #uce_route{method='GET',
 34                path=["meeting"],
 35                callback={?MODULE, list,
 36                          [{"uid", required, string},
 37                           {"sid", required, string}]}},
 38
 39
 40     #uce_route{method='GET',
 41                path=["meeting", meeting],
 42                callback={?MODULE, get,
 43                          [{"uid", required, string},
 44                           {"sid", required, string}]}},
 45
 46     #uce_route{method='PUT',
 47                path=["meeting", meeting],
 48                callback={?MODULE, update,
 49                          [{"uid", required, string},
 50                           {"sid", required, string},
 51                           {"metadata", [], dictionary}]}},
 52
 53     #uce_route{method='POST',
 54                path=["meeting", meeting, "roster"],
 55                callback={?MODULE, join,
 56                          [{"uid", required, string},
 57                           {"sid", required, string},
 58                           {"metadata", [], dictionary}]}},
 59
 60     #uce_route{method='DELETE',
 61                path=["meeting", meeting, "roster", uid],
 62                callback={?MODULE, leave,
 63                          [{"uid", required, string},
 64                           {"sid", required, string}]}},
 65
 66     #uce_route{method='GET',
 67                path=["meeting", meeting, "roster"],
 68                callback={?MODULE, roster,
 69                          [{"uid", required, string},
 70                           {"sid", required, string}]}}].
 71
 72add(Domain, [], [Uid, Sid, Name, Metadata], _) ->
 73    {ok, true} = uce_presence:assert(Domain, Uid, Sid),
 74    {ok, true} = uce_access:assert(Domain, Uid, "", "meeting", "add"),
 75    {ok, created} = uce_meeting:add(Domain,
 76                                    #uce_meeting{id=Name,
 77                                                 metadata=json_helpers:to_struct(Metadata)}),
 78    {ok, _} = uce_event:add(Domain, #uce_event{id=none,
 79                                               from=Uid,
 80                                               location=Name,
 81                                               type="internal.meeting.add"}),
 82    json_helpers:created(Domain).
 83
 84list(Domain, [], [Uid, Sid], _) ->
 85    {ok, true} = uce_presence:assert(Domain, Uid, Sid),
 86    {ok, true} = uce_access:assert(Domain, Uid, "", "meeting", "list"),
 87    {ok, Meetings} = uce_meeting:list(Domain),
 88    json_helpers:json(Domain, Meetings).
 89
 90get(Domain, [{meeting, Name}], [Uid, Sid], _) ->
 91    {ok, true} = uce_presence:assert(Domain, Uid, Sid),
 92    {ok, true} = uce_access:assert(Domain, Uid, "", "meeting", "get"),
 93    {ok, Meeting} = uce_meeting:get(Domain, Name),
 94    json_helpers:json(Domain, Meeting).
 95
 96update(Domain, [{meeting, Name}], [Uid, Sid, Metadata], _) ->
 97    {ok, true} = uce_presence:assert(Domain, Uid, Sid),
 98    {ok, true} = uce_access:assert(Domain, Uid, Name, "meeting", "update"),
 99    {ok, Meeting} = uce_meeting:get(Domain, Name),
100    {ok, updated} = uce_meeting:update(Domain,
101                                       #uce_meeting{id=Name,
102                                                    roster=Meeting#uce_meeting.roster,
103                                                    metadata=json_helpers:to_struct(Metadata)}),
104    {ok, _} = uce_event:add(Domain, #uce_event{id=none,
105                                               from=Uid,
106                                               location=Name,
107                                               type="internal.meeting.update"}),
108    json_helpers:ok(Domain).
109
110join(Domain, [{meeting, Name}], [Uid, Sid, Metadata], _) ->
111    {ok, true} = uce_presence:assert(Domain, Uid, Sid),
112    {ok, true} = uce_access:assert(Domain, Uid, Name, "roster", "add"),
113    {ok, updated} = uce_meeting:join(Domain, Name, Uid),
114    uce_presence:join(Domain, Sid, Name),
115    {ok, _} = uce_event:add(Domain,
116                            #uce_event{id=none,
117                                       type="internal.roster.add",
118                                       location=Name,
119                                       from=Uid,
120                                       metadata=json_helpers:to_struct(Metadata)}),
121    json_helpers:ok(Domain).
122
123%% TODO : Incomplete Sid must be ToSid
124leave(Domain, [{meeting, Name}, {uid, User}], [Uid, Sid], _) ->
125    {ok, true} = uce_presence:assert(Domain, Uid, Sid),
126    {ok, true} = uce_access:assert(Domain, Uid, Name, "roster", "delete"),
127    {ok, updated} = uce_meeting:leave(Domain, Name, User),
128    uce_presence:leave(Domain, Sid, Name),
129    {ok, _} = uce_event:add(Domain,
130                            #uce_event{id=none,
131                                       type="internal.roster.delete",
132                                       location=Name,
133                                       from=User}),
134    json_helpers:ok(Domain).
135
136roster(Domain, [{meeting, Name}], [Uid, Sid], _) ->
137    {ok, true} = uce_presence:assert(Domain, Uid, Sid),
138    {ok, true} = uce_access:assert(Domain, Uid, Name, "roster", "list"),
139    {ok, Roster} = uce_meeting:roster(Domain, Name),
140    json_helpers:json(Domain, lists:map(fun(Member) ->
141                                                {ok, User} = uce_user:get(Domain, Member),
142                                                User
143                                        end,
144                                        Roster)).