-
Notifications
You must be signed in to change notification settings - Fork 428
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an example for send_message callback
- Loading branch information
1 parent
50fb498
commit 2787246
Showing
6 changed files
with
203 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
-module(mam_send_message_SUITE). | ||
|
||
%% API | ||
-export([all/0, | ||
groups/0, | ||
init_per_suite/1, | ||
end_per_suite/1, | ||
init_per_group/2, | ||
end_per_group/2, | ||
init_per_testcase/2, | ||
end_per_testcase/2]). | ||
|
||
-export([mam_muc_send_message/1, | ||
mam_pm_send_message/1]). | ||
|
||
-import(mam_helper, | ||
[stanza_archive_request/2, | ||
wait_archive_respond/1, | ||
assert_respond_size/2, | ||
respond_messages/1, | ||
parse_forwarded_message/1]). | ||
|
||
-import(distributed_helper, [mim/0, | ||
require_rpc_nodes/1, | ||
rpc/4]). | ||
|
||
-include("mam_helper.hrl"). | ||
-include_lib("escalus/include/escalus.hrl"). | ||
-include_lib("escalus/include/escalus_xmlns.hrl"). | ||
-include_lib("common_test/include/ct.hrl"). | ||
-include_lib("exml/include/exml_stream.hrl"). | ||
|
||
all() -> | ||
[{group, send_message}]. | ||
|
||
groups() -> | ||
[ | ||
{send_message, [], [mam_pm_send_message, | ||
mam_muc_send_message]} | ||
]. | ||
|
||
domain() -> | ||
ct:get_config({hosts, mim, domain}). | ||
|
||
%%%=================================================================== | ||
%%% Overall setup/teardown | ||
%%%=================================================================== | ||
init_per_suite(Config) -> | ||
escalus:init_per_suite(Config). | ||
|
||
end_per_suite(Config) -> | ||
escalus:end_per_suite(Config). | ||
|
||
%%%=================================================================== | ||
%%% Group specific setup/teardown | ||
%%%=================================================================== | ||
init_per_group(Group, Config) -> | ||
case mongoose_helper:is_rdbms_enabled(domain()) of | ||
true -> | ||
load_custom_module(), | ||
Config2 = dynamic_modules:save_modules(domain(), Config), | ||
rpc(mim(), gen_mod_deps, start_modules, [domain(), group_to_modules(Group)]), | ||
Config2; | ||
false -> | ||
{skip, require_rdbms} | ||
end. | ||
|
||
end_per_group(_Groupname, Config) -> | ||
case mongoose_helper:is_rdbms_enabled(domain()) of | ||
true -> | ||
dynamic_modules:restore_modules(domain(), Config); | ||
false -> | ||
ok | ||
end, | ||
ok. | ||
|
||
group_to_modules(send_message) -> | ||
MH = muc_light_helper:muc_host(), | ||
[{mod_mam_meta, [{backend, rdbms}, {pm, []}, {muc, [{host, MH}]}, | ||
{send_message, mam_send_message_example}]}, | ||
{mod_muc_light, []}, | ||
{mam_send_message_example, []}]. | ||
|
||
load_custom_module() -> | ||
mam_send_message_example:module_info(), | ||
{Mod, Code, File} = code:get_object_code(mam_send_message_example), | ||
rpc(mim(), code, load_binary, [Mod, File, Code]). | ||
|
||
%%%=================================================================== | ||
%%% Testcase specific setup/teardown | ||
%%%=================================================================== | ||
|
||
init_per_testcase(TestCase, Config) -> | ||
escalus:init_per_testcase(TestCase, Config). | ||
|
||
end_per_testcase(TestCase, Config) -> | ||
escalus:end_per_testcase(TestCase, Config). | ||
|
||
%%%=================================================================== | ||
%%% Test Cases | ||
%%%=================================================================== | ||
|
||
mam_pm_send_message(Config) -> | ||
P = ?config(props, Config), | ||
F = fun(Alice, Bob) -> | ||
escalus:send(Alice, escalus_stanza:chat_to(Bob, <<"OH, HAI!">>)), | ||
escalus:wait_for_stanza(Bob), | ||
mam_helper:wait_for_archive_size(Alice, 1), | ||
mam_helper:wait_for_archive_size(Bob, 1), | ||
escalus:send(Alice, stanza_archive_request(P, <<"q1">>)), | ||
Res = wait_archive_respond(Alice), | ||
assert_respond_size(1, Res), | ||
[Msg] = respond_messages(Res), | ||
verify_has_some_hash(Msg) | ||
end, | ||
escalus_fresh:story(Config, [{alice, 1}, {bob, 1}], F). | ||
|
||
mam_muc_send_message(Config0) -> | ||
F = fun(Config, Alice) -> | ||
P = ?config(props, Config), | ||
Room = muc_helper:fresh_room_name(), | ||
MucHost = muc_light_helper:muc_host(), | ||
muc_light_helper:create_room(Room, MucHost, alice, | ||
[], Config, muc_light_helper:ver(1)), | ||
escalus_assert:has_no_stanzas(Alice), | ||
RoomAddr = <<Room/binary, "@", MucHost/binary>>, | ||
escalus:send(Alice, escalus_stanza:groupchat_to(RoomAddr, <<"text">>)), | ||
M = escalus:wait_for_stanza(Alice), | ||
escalus:assert(is_message, M), | ||
escalus_assert:has_no_stanzas(Alice), | ||
mam_helper:wait_for_room_archive_size(MucHost, Room, 1), | ||
escalus:send(Alice, escalus_stanza:to(stanza_archive_request(P, <<"q1">>), RoomAddr)), | ||
[Msg] = respond_messages(assert_respond_size(1, wait_archive_respond(Alice))), | ||
verify_has_some_hash(Msg) | ||
end, | ||
escalus_fresh:story_with_config(Config0, [{alice, 1}], F). | ||
|
||
verify_has_some_hash(Msg) -> | ||
Hash = exml_query:path(Msg, [{element, <<"result">>}, | ||
{element, <<"some_hash">>}, | ||
{attr, <<"value">>}]), | ||
binary_to_integer(Hash). %% is integer |
46 changes: 46 additions & 0 deletions
46
big_tests/tests/mam_send_message_SUITE_data/mam_send_message_example.erl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
%% Adds some_hash element to each extracted message result. | ||
%% | ||
%% An example module for extending MAM lookup results. | ||
%% Defines a callback for send_message callback. | ||
%% Handles lookup messages hooks to extend message rows with extra info. | ||
-module(mam_send_message_example). | ||
-behaviour(gen_mod). | ||
-behaviour(mongoose_module_metrics). | ||
-include_lib("exml/include/exml.hrl"). | ||
|
||
-export([start/2, | ||
stop/1, | ||
lookup_messages/3, | ||
send_message/4]). | ||
|
||
|
||
start(Host, _Opts) -> | ||
ejabberd_hooks:add(hooks(Host)). | ||
|
||
stop(Host) -> | ||
ejabberd_hooks:delete(hooks(Host)). | ||
|
||
hooks(Host) -> | ||
[{mam_lookup_messages, Host, ?MODULE, lookup_messages, 60}, | ||
{mam_muc_lookup_messages, Host, ?MODULE, lookup_messages, 60}]. | ||
|
||
lookup_messages({error, _Reason} = Result, _Host, _Params) -> | ||
Result; | ||
lookup_messages({ok, {TotalCount, Offset, MessageRows}}, | ||
Host, Params = #{owner_jid := ArcJID}) -> | ||
MessageRows2 = [extend_message(Host, ArcJID, Row) || Row <- MessageRows], | ||
{ok, {TotalCount, Offset, MessageRows2}}. | ||
|
||
extend_message(_Host, _ArcJID, Row = #{}) -> | ||
%% Extend a message with a new field | ||
%% Usually extracted from a DB | ||
Row#{some_hash => erlang:phash2(Row, 32)}. | ||
|
||
send_message(Row, From, To, Mess) -> | ||
Res = xml:get_subtag(Mess, <<"result">>), | ||
Res2 = xml:append_subtags(Res, [new_subelem(Row)]), | ||
Mess2 = xml:replace_subelement(Mess, Res2), | ||
mod_mam_utils:send_message(Row, From, To, Mess2). | ||
|
||
new_subelem(#{some_hash := SomeHash}) -> | ||
#xmlel{name = <<"some_hash">>, attrs = [{<<"value">>, integer_to_binary(SomeHash)}]}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters