./ct_report/coverage/mod_smart_markers_rdbms_async.COVER.html

1 %%% @doc
2 %%% RDBMS backend for mod_smart_markers
3 %%% @end
4 %%% @copyright (C) 2022, Erlang Solutions Ltd.
5 -module(mod_smart_markers_rdbms_async).
6 -behavior(mod_smart_markers_backend).
7 -behaviour(mongoose_aggregator_worker).
8
9 -include("jlib.hrl").
10
11 -export([init/2, stop/1, update_chat_marker/2, get_chat_markers/4, get_conv_chat_marker/6]).
12 -export([remove_domain/2, remove_user/2, remove_to/2, remove_to_for_user/3]).
13
14 %% Worker callbacks
15 -export([request/2, aggregate/3, verify/3]).
16
17 %%--------------------------------------------------------------------
18 %% API
19 %%--------------------------------------------------------------------
20 -spec init(mongooseim:host_type(), gen_mod:module_opts()) -> ok.
21 init(HostType, Opts) ->
22
:-(
AsyncOpts = prepare_pool_opts(Opts),
23
:-(
mod_smart_markers_rdbms:init(HostType, Opts),
24
:-(
start_pool(HostType, AsyncOpts),
25
:-(
ok.
26
27 stop(HostType) ->
28
:-(
mongoose_async_pools:stop_pool(HostType, smart_markers).
29
30 prepare_pool_opts(#{async_writer := AsyncOpts}) ->
31
:-(
AsyncOpts#{pool_type => aggregate,
32 request_callback => fun ?MODULE:request/2,
33 aggregate_callback => fun ?MODULE:aggregate/3,
34 verify_callback => fun ?MODULE:verify/3}.
35
36 -spec start_pool(mongooseim:host_type(), mongoose_async_pools:pool_opts()) -> term().
37 start_pool(HostType, Opts) ->
38
:-(
mongoose_async_pools:start_pool(HostType, smart_markers, Opts).
39
40 %%% @doc
41 %%% 'from', 'to', 'thread' and 'type' keys of the ChatMarker map serve
42 %%% as a composite database key. If key is not available in the database,
43 %%% then chat marker must be added. Otherwise this function must update
44 %%% chat marker record for that composite key.
45 %%% @end
46 -spec update_chat_marker(mongooseim:host_type(),
47 mod_smart_markers:chat_marker()) -> ok.
48 update_chat_marker(HostType, #{from := #jid{luser = LU, lserver = LS},
49 to := To, thread := Thread, type := Type} = Marker) ->
50
:-(
Key = {LU, LS, To, Thread, Type},
51
:-(
mongoose_async_pools:put_task(HostType, smart_markers, Key, Marker).
52
53 %% Synchronous calls
54 -spec get_conv_chat_marker(HostType :: mongooseim:host_type(),
55 From :: jid:jid(),
56 To :: jid:jid(),
57 Thread :: mod_smart_markers:maybe_thread(),
58 Timestamp :: integer(),
59 Private :: boolean()) -> [mod_smart_markers:chat_marker()].
60 get_conv_chat_marker(HostType, From, To, Thread, TS, Private) ->
61
:-(
mod_smart_markers_rdbms:get_conv_chat_marker(HostType, From, To, Thread, TS, Private).
62
63 -spec get_chat_markers(HostType :: mongooseim:host_type(),
64 To :: jid:jid(),
65 Thread :: mod_smart_markers:maybe_thread(),
66 Timestamp :: integer()) -> [mod_smart_markers:chat_marker()].
67 get_chat_markers(HostType, To, Thread, TS) ->
68
:-(
mod_smart_markers_rdbms:get_chat_markers(HostType, To, Thread, TS).
69
70 -spec remove_domain(mongooseim:host_type(), jid:lserver()) -> mongoose_rdbms:query_result().
71 remove_domain(HostType, Domain) ->
72
:-(
mod_smart_markers_rdbms:remove_domain(HostType, Domain).
73
74 -spec remove_user(mongooseim:host_type(), jid:jid()) -> mongoose_rdbms:query_result().
75 remove_user(HostType, User) ->
76
:-(
mod_smart_markers_rdbms:remove_user(HostType, User).
77
78 -spec remove_to(mongooseim:host_type(), jid:jid()) -> mongoose_rdbms:query_result().
79 remove_to(HostType, To) ->
80
:-(
mod_smart_markers_rdbms:remove_to(HostType, To).
81
82 -spec remove_to_for_user(mongooseim:host_type(), From :: jid:jid(), To :: jid:jid()) ->
83 mongoose_rdbms:query_result().
84 remove_to_for_user(HostType, From, To) ->
85
:-(
mod_smart_markers_rdbms:remove_to_for_user(HostType, From, To).
86
87 %% callbacks
88 -spec aggregate(mod_smart_markers:chat_marker(),
89 mod_smart_markers:chat_marker(),
90 mongoose_async_pools:pool_extra()) -> {ok, mod_smart_markers:chat_marker()}.
91 aggregate(_, NewTask, _Extra) ->
92
:-(
{ok, NewTask}.
93
94 -spec request(mod_smart_markers:chat_marker(),
95 mongoose_async_pools:pool_extra()) -> gen_server:request_id().
96 request(#{from := #jid{luser = LU, lserver = LS}, to := To, thread := Thread,
97 type := Type, timestamp := TS, id := Id}, #{host_type := HostType}) ->
98
:-(
ToEncoded = mod_smart_markers_rdbms:encode_jid(To),
99
:-(
ThreadEncoded = mod_smart_markers_rdbms:encode_thread(Thread),
100
:-(
TypeEncoded = mod_smart_markers_rdbms:encode_type(Type),
101
:-(
KeyValues = [LS, LU, ToEncoded, ThreadEncoded, TypeEncoded],
102
:-(
UpdateValues = [Id, TS],
103
:-(
InsertValues = KeyValues ++ UpdateValues,
104
:-(
rdbms_queries:request_upsert(HostType, smart_markers_upsert,
105 InsertValues, UpdateValues, KeyValues).
106
107 -spec verify(term(), mod_smart_markers:chat_marker(), mongoose_async_pools:pool_extra()) -> ok.
108 verify(Answer, Marker, _Extra) ->
109
:-(
mod_smart_markers_rdbms:verify(Answer, Marker).
Line Hits Source