1 |
|
-module(mam_encoder). |
2 |
|
-export([encode_message/3]). |
3 |
|
-export([encode_jid/2]). |
4 |
|
-export([encode_direction/1]). |
5 |
|
-export([encode_packet/2]). |
6 |
|
-export([extend_lookup_params/2]). |
7 |
|
|
8 |
|
-include("jlib.hrl"). |
9 |
|
-include("mongoose_mam.hrl"). |
10 |
|
|
11 |
|
-type value_type() :: int | maybe_string | direction | bare_jid | jid | jid_resource | xml | search. |
12 |
|
-type env_vars() :: mod_mam_rdbms_arch:env_vars(). |
13 |
|
-type db_mapping() :: #db_mapping{}. |
14 |
|
-type encoded_field_value() :: term(). |
15 |
|
|
16 |
|
-spec extend_lookup_params(mam_iq:lookup_params(), env_vars()) -> mam_iq:lookup_params(). |
17 |
|
extend_lookup_params(#{start_ts := Start, end_ts := End, with_jid := WithJID, |
18 |
|
borders := Borders, search_text := SearchText} = Params, Env) -> |
19 |
1548 |
Params#{norm_search_text => mod_mam_utils:normalize_search_text(SearchText), |
20 |
|
start_id => make_start_id(Start, Borders), |
21 |
|
end_id => make_end_id(End, Borders), |
22 |
|
remote_bare_jid => maybe_encode_bare_jid(WithJID, Env), |
23 |
|
remote_resource => jid_to_non_empty_resource(WithJID)}. |
24 |
|
|
25 |
|
-spec encode_message(mod_mam:archive_message_params(), env_vars(), list(db_mapping())) -> |
26 |
|
[encoded_field_value()]. |
27 |
|
encode_message(Params, Env, Mappings) -> |
28 |
10257 |
[encode_value_using_mapping(Params, Env, Mapping) || Mapping <- Mappings]. |
29 |
|
|
30 |
|
encode_value_using_mapping(Params, Env, #db_mapping{param = Param, format = Format}) -> |
31 |
95004 |
Value = maps:get(Param, Params), |
32 |
95004 |
encode_value(Format, Value, Env). |
33 |
|
|
34 |
|
-spec encode_value(value_type(), term(), env_vars()) -> encoded_field_value(). |
35 |
|
encode_value(int, Value, _Env) when is_integer(Value) -> |
36 |
23036 |
Value; |
37 |
|
encode_value(maybe_string, none, _Env) -> |
38 |
10015 |
null; |
39 |
|
encode_value(maybe_string, Value, _Env) when is_binary(Value) -> |
40 |
242 |
Value; |
41 |
|
encode_value(direction, Value, _Env) -> |
42 |
7735 |
encode_direction(Value); |
43 |
|
encode_value(bare_jid, Value, Env) -> |
44 |
7735 |
encode_jid(jid:to_bare(Value), Env); |
45 |
|
encode_value(jid, Value, Env) -> |
46 |
7735 |
encode_jid(Value, Env); |
47 |
|
encode_value(jid_resource, #jid{lresource = Res}, _Env) -> |
48 |
10257 |
Res; |
49 |
|
encode_value(xml, Value, Env) -> |
50 |
10257 |
encode_packet(Value, Env); |
51 |
|
encode_value(search, Value, Env) -> |
52 |
10257 |
encode_search_body(Value, Env); |
53 |
|
encode_value(bool, Value, _Env) -> |
54 |
7735 |
encode_boolean(Value). |
55 |
|
|
56 |
3950 |
encode_direction(incoming) -> <<"I">>; |
57 |
3827 |
encode_direction(outgoing) -> <<"O">>. |
58 |
|
|
59 |
|
make_start_id(Start, Borders) -> |
60 |
1548 |
StartID = maybe_encode_compact_uuid(Start, 0), |
61 |
1548 |
mod_mam_utils:apply_start_border(Borders, StartID). |
62 |
|
|
63 |
|
make_end_id(End, Borders) -> |
64 |
1548 |
EndID = maybe_encode_compact_uuid(End, 255), |
65 |
1548 |
mod_mam_utils:apply_end_border(Borders, EndID). |
66 |
|
|
67 |
|
maybe_encode_compact_uuid(undefined, _) -> |
68 |
2979 |
undefined; |
69 |
|
maybe_encode_compact_uuid(Microseconds, NodeID) -> |
70 |
117 |
mod_mam_utils:encode_compact_uuid(Microseconds, NodeID). |
71 |
|
|
72 |
1501 |
jid_to_non_empty_resource(undefined) -> undefined; |
73 |
33 |
jid_to_non_empty_resource(#jid{lresource = <<>>}) -> undefined; |
74 |
14 |
jid_to_non_empty_resource(#jid{lresource = Res}) -> Res. |
75 |
|
|
76 |
|
-spec encode_jid(jid:jid(), env_vars()) -> binary(). |
77 |
|
encode_jid(JID, #{db_jid_codec := Codec, archive_jid := ArcJID}) -> |
78 |
15559 |
mam_jid:encode(Codec, ArcJID, JID). |
79 |
|
|
80 |
1501 |
maybe_encode_bare_jid(undefined, _Env) -> undefined; |
81 |
47 |
maybe_encode_bare_jid(JID, Env) -> encode_jid(jid:to_bare(JID), Env). |
82 |
|
|
83 |
|
-spec encode_packet(exml:element(), env_vars()) -> binary(). |
84 |
|
encode_packet(Packet, #{db_message_codec := Codec}) -> |
85 |
10299 |
mam_message:encode(Codec, Packet). |
86 |
|
|
87 |
|
-spec encode_search_body(exml:element(), env_vars()) -> binary(). |
88 |
|
encode_search_body(Packet, #{has_full_text_search := SearchEnabled}) -> |
89 |
10257 |
mod_mam_utils:packet_to_search_body(SearchEnabled, Packet). |
90 |
|
|
91 |
259 |
encode_boolean(true) -> 1; |
92 |
7476 |
encode_boolean(false) -> 0. |