-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod_inbox_sql.erl
316 lines (275 loc) · 12 KB
/
mod_inbox_sql.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
%%%-------------------------------------------------------------------
%%% @author dedaldino3D
%%% @copyright (C) 2020 dedaldino3D
%%% @doc
%%%
%%% @end
%%% Created : 30. Jan 2018 16:59
%%%-------------------------------------------------------------------
-module(mod_inbox_sql).
-author("dedaldino3D").
-include_lib("xmpp/include/xmpp.hrl").
-include("logger.hrl").
-include("ejabber_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
]).
%% ----------------------------------------------------------------------
%% Types:
%%%? from ejabberd_sql
%% ----------------------------------------------------------------------
-type sql_query_result() :: {updated,non_neg_integer()} |
{error, binary() | atom()} |
{selected, [binary()], [[binary()]]} |
{selected, [any()]} |
ok.
%%%%%%%%%%%%%%%%%%%% ----------------------------------------------------------------------
%% API
%%%%%%%%%%%%%%%%%%%%-----------------------------------------------------
init(_Host, _Opts) ->
% TODO: add...
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, Rest} ->
[decode_row(LServer, R) || R <- Res]
end.
% ? Be careful, maybe it doesnt work, why: using binary data in ?SQL as string
-spec get_inbox_rdbms(LUser :: jid:luser(),
LServer :: jid:lserver(),
Params :: mod_inbox:get_inbox_params()) ->
sql_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 = ?SQL("select @(remote_bare_jid)b, @(content)b, @(unread_count)d, @(timestamp)b from inbox "
"where luser=%(LUser)s and lserver=%(LServer)s "
"%(BeginSQL)b %(EndSQL)b %(HiddenSQL)b order by timestamp %(OrderSQL)b"),
ejabberd_sql:sql_query(LServer, Query).
get_inbox_unread(Username, Server, InterlocutorJID) ->
RemBareJIDBin = jid:encode(jid:remove_resource(InterlocutorJID)),
QuerySQL = ?SQL("select @(unread_count)d from inbox "
"where luser=%(Username)s and lserver=%(Server)s "
"and remote_bare_jid=%(RemBareJIDBin)b"),
Res = ejabberd_sql:sql_query(Server, QuerySQL),
{ok, Val} = check_result(Val),
%% 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),
% ? Original data
% *InsertParams = [Username, LServer, LToBareJid,
% * Content, Count, MsgId, Timestamp],
% *UpdateParams = [Content, Count, MsgId, Timestamp],
% *UniqueKeyValues = [LUsername, LServer, LToBareJid],
% ? changed version
% ! Probably timestamp in SQL_UPSERT need to be binary,
% ! using integer now
case ?SQL_UPSERT(
LServer,
"inbox",
["!luser=%(LUsername)s",
"!lserver=%(LServer)s",
"!remote_bare_jid=%(LToBareJid)s",
"content=%(Content)b",
"unread_count=%(Count)d",
"msg_id=%(MsgId)b",
"timestamp=%(Timestamp)d"
]) of
ok ->
ok;
Err ->
Err
end.
-spec remove_inbox(Username :: 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(LUsername :: jid:luser(),
LServer :: jid:lserver(),
ToBareJid :: binary()) -> sql_query_result().
remove_inbox_rdbms(LUsername, LServer, ToBareJid) ->
Query = ?SQL("delete from inbox where luser=%(LUsername)s "
"and lserver=%(LServer)s and remote_bare_jid=%(ToBareJid)b"),
ejabberd_sql:sql_query(LServer, Query).
-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),
% TODO: Res = Not implemented yet
%** Res = BackendModule:set_inbox_incr_unread(LUsername, LServer, LToBareJid,
% ? For postgreSQL only, for now
Res = set_inbox_incr_unread_pg(LUsername, LServer, LToBareJid,
Content, MsgId, Timestamp),
Content, MsgId, Timestamp),
%% psql will return {updated, {[UnreadCount]}}
%% mssql and mysql will return {selected, {[Val]}}
check_result(Res).
%% ----------------------------------------------------------------------
%% For PostgreSQL DB only
%% ----------------------------------------------------------------------
%%% ! Search about how make a sql query
-spec set_inbox_incr_unread_pg(Username :: jid:luser(),
Server :: jid:lserver(),
ToBareJid :: binary(),
Content :: binary(),
MsgId :: binary(),
Timestamp :: non_neg_integer()) -> sql_query_result().
set_inbox_incr_unread_pg(Username, Server, ToBareJid, Content, MsgId, Timestamp) ->
Query = ?SQL("insert into inbox(luser, lserver, remote_bare_jid,"
"content, unread_count, msg_id, timestamp)"
" values (%(Username)s, %(Server)s, %(ToBareJid), %(Content), 1"
" %(MsgId), %(Timestamp)s )"
" on conflict (@(luser)s, @(lserver)s, @(remote_bare_jid)s) do"
" update set content=%(Content)s,"
" unread_count=inbox.unread_count + 1,"
" msg_id=%(MsgId)s"
" timestamp=%(Timestamp)b, returning %(unread_count)d"),
ejabberd_sql:sql_query(Server, Query).
%% ----------------------------------------------------------------------
%% Finish
%% ----------------------------------------------------------------------
-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) -> sql_query_result().
reset_inbox_unread_rdbms(Username, Server, ToBareJid, undefined) ->
ejabberd_sql:sql_query(Server, ?SQL("update inbox set unread_count=0 "
"where luser=%(Username)s and lserver=%(Server)s "
"and remote_bare_jid=%(ToBareJid)b"));
reset_inbox_unread_rdbms(Username, Server, ToBareJid, MsgId) ->
ejabberd_sql:sql_query(Server, ?SQL("update inbox set unread-count=0 where luser=%(Username)s "
"and lserver=%(Server)s and remote_bare_jid=%(ToBareJid)s "
"and msg_id=%(MsgId)b")).
-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).
%% ----------------------------------------------------------------------
%% 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),
% ! maybe will throw an ERROR
[" and timestamp ", Operator, 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()) -> sql_query_result().
clear_inbox_rdbms(Username, Server) ->
ejabberd:sql_query(Server, ?SQ;("delete from inbox where luser=%(Username)s "
" and lserver=%(Server)s")).
-spec clear_inbox_rdbms(Server :: jid:lserver()) -> sql_query_result().
clear_inbox_rdbms(Server) ->
ejabberd_sql:sql_query(Server, ?SQL("delete from inbox")).
%%%
%%%
-spec result_to_integer(binary() | integer()) -> integer().
result_to_integer(Int) when is_integer(Int) ->
Int;
result_to_integer(Bin) when is_binary(Bin) ->
binary_to_integer(Bin).
-spec decode_row(host(), {username(), binary(), count_bin(), non_neg_integer() | binary()}) ->
inbox_res().
decode_row(LServer, {Username, Content, Count, Timestamp}) ->
Data = fxml_stream:parse_element(Content),
BCount = count_to_bin(Count),
NumericTimestamp = result_to_integer(Timestamp),
{Username, Data, BCount, usec:to_now(NumericTimestamp)}.
count_to_bin(Count) when is_integer(Count) -> integer_to_binary(Count);
count_to_bin(Count) when is_binary(Count) -> Count;
%%%% TODO: UNDERSTAND HOW THIS WORK
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}}.