/ucengine/src/models/uce_event.erl
Erlang | 122 lines | 96 code | 9 blank | 17 comment | 3 complexity | d96f0eb5332aa197c232ec88ffeb67c9 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_event). 19 20-export([add/2, get/2, exists/2, list/8, list/12, search/12]). 21 22-include("uce.hrl"). 23 24-spec add(domain(), event()) -> {ok, event_id()} | erlang:throw({error, not_found}). 25add(Domain, #uce_event{id=none}=Event) -> 26 add(Domain, Event#uce_event{id=utils:random()}); 27add(Domain, #uce_event{datetime=undefined}=Event) -> 28 add(Domain, Event#uce_event{datetime=utils:now()}); 29add(Domain, #uce_event{location=Location, to=To, parent=Parent} = Event) -> 30 LocationExists = uce_meeting:exists(Domain, Location), 31 ToExists = uce_user:exists(Domain, To), 32 ParentExists = uce_event:exists(Domain, Parent), 33 34 case {LocationExists, ToExists, ParentExists} of 35 {true, true, true} -> 36 {ok, Id} = (db:get(?MODULE, Domain)):add(Domain, Event), 37 uce_meeting:publish(Domain, Event), 38 ?SEARCH_MODULE:add(Domain, Event), 39 ?COUNTER("event_add:" ++ Event#uce_event.type), 40 {ok, Id}; 41 _ ->% [TODO] throw the missing exist 42 throw({error, not_found}) 43 end. 44 45-spec get(domain(), event_id()) -> {ok, event()} | erlang:throw({error, bad_parameters}). 46get(Domain, Id) -> 47 (db:get(?MODULE, Domain)):get(Domain, Id). 48 49-spec exists(domain(), event_id()) -> boolean(). 50exists(_Domain, "") -> 51 true; 52exists(Domain, Id) -> 53 case catch get(Domain, Id) of 54 {error, not_found} -> 55 false; 56 {error, Reason} -> 57 throw({error, Reason}); 58 _ -> 59 true 60 end. 61 62-spec filter_private(list(event()), uid()) -> list(event()). 63filter_private(Events, Name) -> 64 lists:filter(fun(#uce_event{to=To, from=From}) -> 65 if 66 To == "" -> % all 67 true; 68 To == Name -> 69 true; 70 From == Name -> 71 true; 72 true -> 73 false 74 end 75 end, 76 Events). 77 78-spec search(Domain :: domain(), 79 Location :: meeting_id(), 80 Search :: string(), 81 From :: uid(), 82 Types :: string(), 83 Uid :: uid(), 84 DateStart :: timestamp(), 85 DateEnd :: timestamp(), 86 Parent :: event_id(), 87 Start :: integer(), 88 Max :: integer(), 89 Order :: string()) -> {ok, integer(), list(event())}. 90search(Domain, Location, Search, From, Types, Uid, DateStart, DateEnd, Parent, Start, Max, Order) -> 91 {ok, NumTotal, Events} = ?SEARCH_MODULE:list(Domain, 92 Location, 93 Search, 94 From, 95 Types, 96 DateStart, 97 DateEnd, 98 Parent, 99 Start, 100 Max, 101 Order), 102 ?COUNTER(event_search), 103 {ok, NumTotal, filter_private(Events, Uid)}. 104 105list(Domain, Location, Uid, Search, From, Types, Start, Parent) -> 106 list(Domain, Location, Search, From, Types, Uid, Start, infinity, Parent, 0, infinity, asc). 107list(Domain, Location, Search, From, Types, Uid, DateStart, DateEnd, Parent, Start, Max, Order) -> 108 N = now(), 109 {ok, _Num, Events} = uce_event_erlang_search:list(Domain, 110 Location, 111 Search, 112 From, 113 Types, 114 DateStart, 115 DateEnd, 116 Parent, 117 Start, 118 Max, 119 Order), 120 ?TIMER_APPEND(event_list, N), 121 ?COUNTER(event_list), 122 {ok, filter_private(Events, Uid)}.