-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod_inbox_rdbms.erl
277 lines (243 loc) · 11.1 KB
/
mod_inbox_rdbms.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
%%%-------------------------------------------------------------------
%%% @author dedaldino3D
%%% @copyright (C) 2020 dedaldino3D
%%% @doc
%%%
%%% @end
%%% Created : 30. Jan 2018 16:59
%%%-------------------------------------------------------------------
-module(mod_inbox_rdbms).
-author("dedaldino3D").
% * Removed, original
% -include("jlib.hrl").
% -include("mongoose.hrl").
% -include("mod_inbox.hrl").
% * Changed for
-include_lib("xmpp/include/xmpp.hrl").
-include("logger.hrl").
-include("ejabberd_sql_pt.hrl").
-include("mod_inbox.hrl").
-behaviour(mod_inbox).
%% API
-export([get_inbox/3,
init/2,
set_inbox/7,
set_inbox_incr_unread/6,
reset_unread/4,
remove_inbox/3,
clear_inbox/1,
clear_inbox/2,
get_inbox_unread/3]).
%% For specific backends
-export([esc_string/1, esc_int/1]).
%% ----------------------------------------------------------------------
%% API
%% ----------------------------------------------------------------------
init(VHost, _Options) ->
rdbms_queries:prepare_upsert(VHost, inbox_upsert, inbox,
[<<"luser">>, <<"lserver">>, <<"remote_bare_jid">>,
<<"content">>, <<"unread_count">>, <<"msg_id">>, <<"timestamp">>],
[<<"content">>, <<"unread_count">>, <<"msg_id">>, <<"timestamp">>],
[<<"luser">>, <<"lserver">>, <<"remote_bare_jid">>]),
ok.
-spec get_inbox(LUsername :: jid:luser(),
LServer :: jid:lserver(),
Params :: mod_inbox:get_inbox_params()) -> get_inbox_res().
get_inbox(LUsername, LServer, Params) ->
case get_inbox_rdbms(LUsername, LServer, Params) of
{selected, []} ->
[];
{selected, Res} ->
[decode_row(LServer, R) || R <- Res]
end.
-spec get_inbox_rdbms(LUser :: jid:luser(),
LServer :: jid:lserver(),
Params :: mod_inbox:get_inbox_params()) ->
mongoose_rdbms:query_result().
get_inbox_rdbms(LUser, LServer, #{ order := Order } = Params) ->
OrderSQL = order_to_sql(Order),
BeginSQL = sql_and_where_timestamp(">=", maps:get(start, Params, undefined)),
EndSQL = sql_and_where_timestamp("<=", maps:get('end', Params, undefined)),
HiddenSQL = sql_and_where_unread_count(maps:get(hidden_read, Params, false)),
Query = ["SELECT remote_bare_jid, content, unread_count, timestamp FROM inbox "
"WHERE luser=", esc_string(LUser),
" AND lserver=", esc_string(LServer),
BeginSQL, EndSQL, HiddenSQL,
" ORDER BY timestamp ", OrderSQL, ";"],
mongoose_rdbms:sql_query(LServer, Query).
get_inbox_unread(Username, Server, InterlocutorJID) ->
RemBareJIDBin = jid:to_binary(jid:to_lus(InterlocutorJID)),
Res = mongoose_rdbms:sql_query(Server,
["select unread_count from inbox "
"WHERE luser=", esc_string(Username),
"AND lserver=", esc_string(Server),
"AND remote_bare_jid=", esc_string(RemBareJIDBin),
";"]),
{ok, Val} = check_result(Res),
%% We read unread_count value when the message is sent and is not yet in receiver inbox
%% so we have to add +1
{ok, Val + 1}.
-spec set_inbox(Username, Server, ToBareJid, Content,
Count, MsgId, Timestamp) -> inbox_write_res() when
Username :: jid:luser(),
Server :: jid:lserver(),
ToBareJid :: binary(),
Content :: binary(),
Count :: integer(),
MsgId :: binary(),
Timestamp :: integer().
set_inbox(Username, Server, ToBareJid, Content, Count, MsgId, Timestamp) ->
LUsername = jid:nodeprep(Username),
LServer = jid:nameprep(Server),
LToBareJid = jid:nameprep(ToBareJid),
InsertParams = [LUsername, LServer, LToBareJid,
Content, Count, MsgId, Timestamp],
UpdateParams = [Content, Count, MsgId, Timestamp],
UniqueKeyValues = [LUsername, LServer, LToBareJid],
Res = rdbms_queries:execute_upsert(Server, inbox_upsert,
InsertParams, UpdateParams, UniqueKeyValues),
%% MySQL returns 1 when an upsert is an insert
%% and 2, when an upsert acts as update
ok = check_result(Res, [1, 2]).
-spec remove_inbox(User :: binary(),
Server :: binary(),
ToBareJid :: binary()) -> ok.
remove_inbox(Username, Server, ToBareJid) ->
LUsername = jid:nodeprep(Username),
LServer = jid:nameprep(Server),
LToBareJid = jid:nameprep(ToBareJid),
Res = remove_inbox_rdbms(LUsername, LServer, LToBareJid),
check_result(Res).
-spec remove_inbox_rdbms(Username :: jid:luser(),
Server :: jid:lserver(),
ToBareJid :: binary()) -> mongoose_rdbms:query_result().
remove_inbox_rdbms(Username, Server, ToBareJid) ->
mongoose_rdbms:sql_query(Server, ["delete from inbox where luser=",
esc_string(Username), " and lserver=", esc_string(Server),
" and remote_bare_jid=",
esc_string(ToBareJid), ";"]).
%% This function was not refatorected to use the generic upsert helper
%% becase this helper doesn't support parametrized queries for incremental change
-spec set_inbox_incr_unread(Username :: binary(),
Server :: binary(),
ToBareJid :: binary(),
Content :: binary(),
MsgId :: binary(),
Timestamp :: erlang:timestamp()) -> ok | {ok, integer()}.
set_inbox_incr_unread(Username, Server, ToBareJid, Content, MsgId, Timestamp) ->
LUsername = jid:nodeprep(Username),
LServer = jid:nameprep(Server),
LToBareJid = jid:nameprep(ToBareJid),
BackendModule = rdbms_specific_backend(Server),
Res = BackendModule:set_inbox_incr_unread(LUsername, LServer, LToBareJid,
Content, MsgId, Timestamp),
%% psql will return {updated, {[UnreadCount]}}
%% mssql and mysql will return {selected, {[Val]}}
check_result(Res).
-spec reset_unread(User :: binary(),
Server :: binary(),
BareJid :: binary(),
MsgId :: binary() | undefined) -> ok.
reset_unread(Username, Server, ToBareJid, MsgId) ->
LUsername = jid:nodeprep(Username),
LServer = jid:nameprep(Server),
LToBareJid = jid:nameprep(ToBareJid),
Res = reset_inbox_unread_rdbms(LUsername, LServer, LToBareJid, MsgId),
check_result(Res).
-spec reset_inbox_unread_rdbms(Username :: jid:luser(),
Server :: jid:lserver(),
ToBareJid :: binary(),
MsgId :: binary() | undefined) -> mongoose_rdbms:query_result().
reset_inbox_unread_rdbms(Username, Server, ToBareJid, undefined) ->
mongoose_rdbms:sql_query(Server, ["update inbox set unread_count=0",
" where luser=", esc_string(Username),
" and lserver=", esc_string(Server),
" and remote_bare_jid=", esc_string(ToBareJid), ";"]);
reset_inbox_unread_rdbms(Username, Server, ToBareJid, MsgId) ->
mongoose_rdbms:sql_query(Server, ["update inbox set unread_count=0 where luser=",
esc_string(Username), " and lserver=", esc_string(Server), " and remote_bare_jid=",
esc_string(ToBareJid), " and msg_id=", esc_string(MsgId), ";"]).
-spec clear_inbox(Username :: binary(), Server :: binary()) -> inbox_write_res().
clear_inbox(Username, Server) ->
LUsername = jid:nodeprep(Username),
LServer = jid:nameprep(Server),
Res = clear_inbox_rdbms(LUsername, LServer),
check_result(Res).
-spec clear_inbox(Server :: binary()) -> inbox_write_res().
clear_inbox( Server) ->
LServer = jid:nameprep(Server),
Res = clear_inbox_rdbms(LServer),
check_result(Res).
-spec esc_string(binary() | string()) -> mongoose_rdbms:sql_query_part().
esc_string(String) ->
mongoose_rdbms:use_escaped_string(mongoose_rdbms:escape_string(String)).
-spec esc_int(integer()) -> mongoose_rdbms:sql_query_part().
esc_int(Integer) ->
mongoose_rdbms:use_escaped_integer(mongoose_rdbms:escape_integer(Integer)).
%% ----------------------------------------------------------------------
%% Internal functions
%% ----------------------------------------------------------------------
-spec order_to_sql(Order :: asc | desc) -> binary().
order_to_sql(asc) -> <<"ASC">>;
order_to_sql(desc) -> <<"DESC">>.
-spec sql_and_where_timestamp(Operator :: string(), Timestamp :: erlang:timestamp()) -> iolist().
sql_and_where_timestamp(_Operator, undefined) ->
[];
sql_and_where_timestamp(Operator, Timestamp) ->
NumericTimestamp = usec:from_now(Timestamp),
[" AND timestamp ", Operator, esc_int(NumericTimestamp)].
-spec sql_and_where_unread_count(HiddenRead :: boolean()) -> iolist().
sql_and_where_unread_count(true) ->
[" AND unread_count ", " > ", <<"0">>];
sql_and_where_unread_count(_) ->
[].
-spec clear_inbox_rdbms(Username :: jid:luser(), Server :: jid:lserver()) -> mongoose_rdbms:query_result().
clear_inbox_rdbms(Username, Server) ->
mongoose_rdbms:sql_query(Server, ["delete from inbox where luser=",
esc_string(Username), " and lserver=", esc_string(Server), ";"]).
-spec clear_inbox_rdbms(Server :: jid:lserver()) -> mongoose_rdbms:query_result().
clear_inbox_rdbms(Server) ->
mongoose_rdbms:sql_query(Server, ["delete from inbox;"]).
-spec decode_row(host(), {username(), binary(), count_bin(), non_neg_integer() | binary()}) ->
inbox_res().
decode_row(LServer, {Username, Content, Count, Timestamp}) ->
Data = mongoose_rdbms:unescape_binary(LServer, Content),
BCount = count_to_bin(Count),
NumericTimestamp = mongoose_rdbms:result_to_integer(Timestamp),
{Username, Data, BCount, usec:to_now(NumericTimestamp)}.
rdbms_specific_backend(Host) ->
case {mongoose_rdbms:db_engine(Host), mongoose_rdbms_type:get()} of
{mysql, _} -> mod_inbox_rdbms_mysql;
{pgsql, _} -> mod_inbox_rdbms_pgsql;
{odbc, mssql} -> mod_inbox_rdbms_mssql;
NotSupported -> erlang:error({rdbms_not_supported, NotSupported})
end.
count_to_bin(Count) when is_integer(Count) -> integer_to_binary(Count);
count_to_bin(Count) when is_binary(Count) -> Count.
check_result({updated, Val}, ValList) when is_list(ValList) ->
case lists:member(Val, ValList) of
true ->
ok;
_ ->
{error, {expected_does_not_match, Val, ValList}}
end;
check_result(Result, _) ->
{error, {bad_result, Result}}.
check_result({selected, []}) ->
{ok, 0};
check_result({selected, [{Val}]}) ->
parse_result(Val);
check_result({updated, _, [{Val}]}) ->
parse_result(Val);
check_result({updated, _}) ->
ok;
check_result(Result) ->
{error, {bad_result, Result}}.
parse_result(Value) when is_integer(Value) ->
{ok, Value};
parse_result(Value) when is_binary(Value) ->
{ok, binary_to_integer(Value)};
parse_result(null) ->
{ok, 0};
parse_result(Value) ->
{error, {unknown_result_value_type, Value}}.