PageRenderTime 31ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/ucengine/src/models/uce_event.erl

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