1 |
|
-module(mod_smart_markers_backend). |
2 |
|
|
3 |
|
-define(MAIN_MODULE, mod_smart_markers). |
4 |
|
|
5 |
|
-export([init/2]). |
6 |
|
-export([update_chat_marker/2]). |
7 |
|
-export([get_chat_markers/4]). |
8 |
|
|
9 |
|
%%-------------------------------------------------------------------- |
10 |
|
%% DB backend behaviour definition |
11 |
|
%%-------------------------------------------------------------------- |
12 |
|
-callback init(mongooseim:host_type(), gen_mod:module_opts()) -> ok. |
13 |
|
|
14 |
|
%%% 'from', 'to', 'thread' and 'type' keys of the ChatMarker map serve |
15 |
|
%%% as a composite database key. If key is not available in the database, |
16 |
|
%%% then chat marker must be added. Otherwise this function must update |
17 |
|
%%% chat marker record for that composite key. |
18 |
|
-callback update_chat_marker(mongooseim:host_type(), mod_smart_markers:chat_marker()) -> ok. |
19 |
|
|
20 |
|
%%% This function must return the latest chat markers sent to the |
21 |
|
%%% user/room (with or w/o thread) later than provided timestamp. |
22 |
|
-callback get_chat_markers(HostType :: mongooseim:host_type(), |
23 |
|
To :: jid:jid(), |
24 |
|
Thread :: mod_smart_markers:maybe_thread(), |
25 |
|
Timestamp :: integer()) -> |
26 |
|
[mod_smart_markers:chat_marker()]. |
27 |
|
|
28 |
|
|
29 |
|
-spec init(mongooseim:host_type(), gen_mod:module_opts()) -> ok. |
30 |
|
init(HostType, Opts) -> |
31 |
1 |
FOpts = add_default_backend(Opts), |
32 |
1 |
TrackedFuns = [get_chat_markers, update_chat_marker], |
33 |
1 |
mongoose_backend:init(HostType, ?MAIN_MODULE, TrackedFuns, FOpts), |
34 |
1 |
Args = [HostType, Opts], |
35 |
1 |
mongoose_backend:call(HostType, ?MAIN_MODULE, ?FUNCTION_NAME, Args). |
36 |
|
|
37 |
|
-spec update_chat_marker(mongooseim:host_type(), |
38 |
|
mod_smart_markers:chat_marker()) -> ok. |
39 |
|
update_chat_marker(HostType, ChatMarker) -> |
40 |
10 |
Args = [HostType, ChatMarker], |
41 |
10 |
mongoose_backend:call_tracked(HostType, ?MAIN_MODULE, ?FUNCTION_NAME, Args). |
42 |
|
|
43 |
|
-spec get_chat_markers(HostType :: mongooseim:host_type(), |
44 |
|
To :: jid:jid(), |
45 |
|
Thread :: mod_smart_markers:maybe_thread(), |
46 |
|
Timestamp :: integer()) -> [mod_smart_markers:chat_marker()]. |
47 |
|
get_chat_markers(HostType, To, Thread, TS) -> |
48 |
4 |
Args = [HostType, To, Thread, TS], |
49 |
4 |
mongoose_backend:call_tracked(HostType, ?MAIN_MODULE, ?FUNCTION_NAME, Args). |
50 |
|
|
51 |
|
add_default_backend(Opts) -> |
52 |
1 |
case lists:keyfind(backend, 2, Opts) of |
53 |
1 |
false -> [{backend, rdbms} | Opts]; |
54 |
:-( |
_ -> Opts |
55 |
|
end. |