我正在尝试在mod_admin_extra.erl
. 获取 2 个 JID 之间的消息。
我的命令将如下所示:-
ejabberdctl get_messages HOST FROM TO START_TIME END_TIME
SQL 查询将如下所示:-
select * from archive where (username = FROM and bare_peer = TO) OR (username=TO and bare_peer = FROM) where created_at BETWEEN START_TIME AND END_TIME;
我通过这个线程来了解 IQ 查询的工作原理,并希望通过命令和 API 构建类似的东西。
如何在上述函数中触发查询以获取 2 个 JID 的对话之间的消息?
我的回答是字典列表:-
[{from: jid1, to: jid2, body: Hello, created_at: T1}]
我会反过来对 POST API 使用相同的命令来获取消息。
更新
根据@Badlop 提供的建议,我更新了我的函数
% ----------------- Custom Command Get Message ----------------------
#ejabberd_commands{name = get_message, tags = [stanza],
desc = "Get messages from a local or remote bare of full JID",
longdesc = "Get messages of a specific JID sent to a JID",
module = ?MODULE, function = get_message,
args = [{host, binary}, {from, binary}, {to, binary},
{start_time, binary}, {end_time, binary}],
args_example = [<<"localhost">>, <<"admin">>, <<"user1">>,
<<"2015-07-00T00:00:00Z">>, <<"2015-07-029T13:23:54Z">>],
args_desc = ["Host", "From JID", "Receiver JID", "Start Time", "End Time"],
result = {result, {
tuple, [{messages, list, {message, {tuple,
[
{timestamp, string},
{xml, string},
{txt, string},
{peer, integer},
{kind, integer},
{nick, string}
]}}},
{status, string},
{count, integer}]}}
},
% ----------------- Custom Command Ends -----------------------------
这是我收到命令时调用的函数。
% ----------------- Custom Function Get Message ----------------------
get_message(Host, From, To, StartTime, EndTime) ->
mod_mam:select(
Host,
jid:make(From, Host),
jid:make(From, Host),
[{start, xmpp_util:decode_timestamp(StartTime)},
{'end', xmpp_util:decode_timestamp(EndTime)},
{with, jid:make(To, Host)}],
#rsm_set{},
chat,
all
).
% ----------------- Custom Function Get Message ----------------------
但是,它返回错误响应:-
Unhandled exception occurred executing the command:
** exception error: no function clause matching
ejabberd_ctl:format_result([],
{messages,list,
{message,
{tuple,
[{timestamp,string},
{xml,string},
{peer,integer},
{kind,integer},
{nick,string}]}}}) (src/ejabberd_ctl.erl, line 405)
in function ejabberd_ctl:format_result/2 (src/ejabberd_ctl.erl, line 461)
in call from ejabberd_ctl:try_call_command/4 (src/ejabberd_ctl.erl, line 321)
in call from ejabberd_ctl:process2/4 (src/ejabberd_ctl.erl, line 274)
in call from ejabberd_ctl:process/2 (src/ejabberd_ctl.erl, line 252)
in call from rpc:'-handle_call_call/6-fun-0-'/5 (rpc.erl, line 197)
日志中打印的查询如下:-
2020-04-24 21:57:13.717746+05:30 [debug] SQL: "SELECT timestamp, xml, peer, kind, nick FROM archive WHERE username=E'admin' and server_host=E'localhost' and bare_peer=E'test@localhost' and timestamp >= 1587692943312536 and timestamp <= 1587779343312536 ORDER BY timestamp ASC ;"
2020-04-24 21:57:13.726745+05:30 [debug] SQL: "SELECT COUNT(*) FROM archive WHERE username=E'admin' and server_host=E'localhost' and bare_peer=E'test@localhost' and timestamp >= 1587692943312536 and timestamp <= 1587779343312536;"