PageRenderTime 74ms CodeModel.GetById 40ms app.highlight 18ms RepoModel.GetById 13ms app.codeStats 0ms

/ucengine/src/models/uce_meeting.erl

http://github.com/AF83/ucengine
Erlang | 128 lines | 95 code | 16 blank | 17 comment | 0 complexity | c9da88dd239e31cd475df1ca73743566 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(uce_meeting).
 19
 20-export([add/2,
 21         delete/2,
 22         update/2,
 23         get/2,
 24         list/1,
 25         join/3,
 26         leave/3,
 27         roster/2,
 28         exists/2,
 29         publish/2,
 30         subscribe/7,
 31         unsubscribe/1]).
 32
 33-include("uce.hrl").
 34
 35
 36-spec add(domain(), meeting()) -> {ok, created} | erlang:throw({error, conflict}).
 37add(Domain, #uce_meeting{id=Id} = Meeting) ->
 38    case exists(Domain, Id) of
 39        true ->
 40            throw({error, conflict});
 41        false ->
 42            (db:get(?MODULE, Domain)):add(Domain, Meeting)
 43    end.
 44
 45-spec delete(domain(), meeting_id()) -> {ok, deleted} | erlang:throw({error, not_found}).
 46delete(Domain, Id) ->
 47    case exists(Domain, Id) of
 48        false ->
 49            throw({error, not_found});
 50        true ->
 51            (db:get(?MODULE, Domain)):delete(Domain, Id)
 52    end.
 53
 54-spec get(domain(), meeting_id()) -> {ok, meeting()} | erlang:throw({error, not_found}).
 55get(Domain, Id) ->
 56    (db:get(?MODULE, Domain)):get(Domain, Id).
 57
 58-spec update(domain(), meeting()) -> {ok, updated} | erlang:throw({error, not_found}).
 59update(Domain, #uce_meeting{id=Id} = Meeting) ->
 60    case exists(Domain, Id) of
 61        false ->
 62            throw({error, not_found});
 63        true ->
 64            (db:get(?MODULE, Domain)):update(Domain, Meeting)
 65    end.
 66
 67-spec list(domain()) -> {ok, list(meeting)} | erlang:throw({error, bad_parameters}).
 68list(Domain) ->
 69    (db:get(?MODULE, Domain)):list(Domain).
 70
 71-spec exists(domain(), meeting_id()) -> boolean().
 72exists(_Domain, "") ->
 73    true; % root
 74exists(Domain, Id) ->
 75    case catch get(Domain, Id) of
 76        {error, not_found} ->
 77            false;
 78        {error, Reason} ->
 79            throw({error, Reason});
 80        {ok, _} ->
 81            true
 82    end.
 83
 84-spec join(domain(), meeting_id(), user()) -> {ok, updated} | erlang:throw({error, not_found}).
 85join(Domain, Id, User) ->
 86    case uce_user:exists(Domain, User) of
 87        false ->
 88            throw({error, not_found});
 89        true ->
 90            {ok, Meeting} = get(Domain, Id),
 91            case lists:member(User, Meeting#uce_meeting.roster) of
 92                false ->
 93                    update(Domain, Meeting#uce_meeting{roster=Meeting#uce_meeting.roster ++ [User]});
 94                true ->
 95                    {ok, updated}
 96            end
 97    end.
 98
 99-spec leave(domain(), meeting_id(), user()) -> {ok, updated} | erlang:throw({error, not_found}).
100leave(Domain, Id, User) ->
101    case uce_user:exists(Domain, User) of
102        false ->
103            throw({error, not_found});
104        true ->
105            {ok, Meeting} = get(Domain, Id),
106            case lists:member(User, Meeting#uce_meeting.roster) of
107                false ->
108                    throw({error, not_found});
109                true ->
110                    Roster = lists:subtract(Meeting#uce_meeting.roster, [User]),
111                    update(Domain, Meeting#uce_meeting{roster=Roster})
112            end
113    end.
114
115-spec roster(domain(), meeting_id()) -> list(user()) | erlang:throw({error, not_found}).
116roster(Domain, Id) ->
117    {ok, Meeting} = get(Domain, Id),
118    {ok, Meeting#uce_meeting.roster}.
119
120
121publish(Domain, Event) ->
122    ?PUBSUB_MODULE:publish(Domain, Event).
123
124subscribe(Pid, Domain, Uid, Location, From, Types, Parent) ->
125    ?PUBSUB_MODULE:subscribe(Pid, Domain, Uid, Location, From, Types, Parent).
126
127unsubscribe(Pid) ->
128    ?PUBSUB_MODULE:unsubscribe(Pid).