1 |
|
%%% @doc Encoder and decoder for MAM messages |
2 |
|
%%% |
3 |
|
%%% Default implementations are: |
4 |
|
%%% - mam_message_xml |
5 |
|
%%% - mam_message_eterm |
6 |
|
%%% - mam_message_compressed_eterm |
7 |
|
-module(mam_message). |
8 |
|
|
9 |
|
-ignore_xref([behaviour_info/1]). |
10 |
|
|
11 |
|
-callback encode(exml:element()) -> binary(). |
12 |
|
-callback decode(binary()) -> exml:element(). |
13 |
|
|
14 |
|
-export([encode/2, decode/2]). |
15 |
|
|
16 |
|
-include_lib("kernel/include/logger.hrl"). |
17 |
|
-include_lib("exml/include/exml.hrl"). |
18 |
|
|
19 |
|
-spec encode(module(), exml:element()) -> binary(). |
20 |
8165 |
encode(Mod, Packet) -> Mod:encode(Packet). |
21 |
|
|
22 |
|
-spec decode(module(), binary()) -> exml:element(). |
23 |
|
decode(Mod, Bin) -> |
24 |
4977 |
try |
25 |
4977 |
Mod:decode(Bin) |
26 |
|
catch Class:Reason:Stacktrace -> |
27 |
21 |
?LOG_ERROR(#{what => mam_failed_to_decode_message, |
28 |
|
encoded_message => Bin, |
29 |
:-( |
class => Class, reason => Reason, stacktrace => Stacktrace}), |
30 |
21 |
error_stanza() |
31 |
|
end. |
32 |
|
|
33 |
|
error_stanza() -> |
34 |
21 |
Text = <<"Failed to decode message in database">>, |
35 |
21 |
Err = mongoose_xmpp_errors:internal_server_error(<<"en">>, Text), |
36 |
21 |
Body = #xmlel{name = <<"body">>, children = [#xmlcdata{content = Text}]}, |
37 |
21 |
#xmlel{name = <<"message">>, |
38 |
|
attrs = [{<<"type">>, <<"error">>}], |
39 |
|
children = [Err, Body]}. |