Skip to content

Commit

Permalink
Move exported MAM types to mod_mam
Browse files Browse the repository at this point in the history
  • Loading branch information
chrzaszcz committed Jun 15, 2022
1 parent 4ab1112 commit 91a3ac5
Show file tree
Hide file tree
Showing 29 changed files with 238 additions and 237 deletions.
6 changes: 3 additions & 3 deletions src/mam/ejabberd_gen_mam_archive.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
-ignore_xref([behaviour_info/1]).

-callback archive_size(Size :: integer(), Host :: jid:server(),
ArchiveID :: mod_mam_pm:archive_id(), ArchiveJID :: jid:jid())
ArchiveID :: mod_mam:archive_id(), ArchiveJID :: jid:jid())
-> integer().

-callback archive_message(Result :: any(), Host :: jid:server(),
Params :: mod_mam_pm:archive_message_params()) -> ok | {error, timeout}.
Params :: mod_mam:archive_message_params()) -> ok | {error, timeout}.

-callback lookup_messages(Result :: any(), Host :: jid:server(),
Params :: map()) -> Result when
Result :: {ok, mod_mam_pm:lookup_result()} | {error, 'policy-violation'}.
Result :: {ok, mod_mam:lookup_result()} | {error, 'policy-violation'}.

-callback get_mam_pm_gdpr_data(mam_pm_gdpr_data(), mongooseim:host_type(), jid:jid()) -> mam_pm_gdpr_data().

Expand Down
14 changes: 7 additions & 7 deletions src/mam/ejabberd_gen_mam_prefs.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

-ignore_xref([behaviour_info/1]).

-callback get_behaviour(Default :: mod_mam_pm:archive_behaviour(),
Host :: jid:server(), ArcID :: mod_mam_pm:archive_id(),
-callback get_behaviour(Default :: mod_mam:archive_behaviour(),
Host :: jid:server(), ArcID :: mod_mam:archive_id(),
LocJID :: jid:jid(), RemJID :: jid:jid()) -> any().

-callback set_prefs(Result :: any(), Host :: jid:server(),
ArcID :: mod_mam_pm:archive_id(), ArcJID :: jid:jid(),
DefaultMode :: mod_mam_pm:archive_behaviour(),
ArcID :: mod_mam:archive_id(), ArcJID :: jid:jid(),
DefaultMode :: mod_mam:archive_behaviour(),
AlwaysJIDs :: [jid:literal_jid()],
NeverJIDs :: [jid:literal_jid()]) -> any().

-callback get_prefs(mod_mam_pm:preference(), _Host :: jid:server(),
ArcId :: mod_mam_pm:archive_id(), ArcJID :: jid:jid())
-> mod_mam_pm:preference().
-callback get_prefs(mod_mam:preference(), _Host :: jid:server(),
ArcId :: mod_mam:archive_id(), ArcJID :: jid:jid())
-> mod_mam:preference().

6 changes: 3 additions & 3 deletions src/mam/mam_decoder.erl
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
-type db_muc_gdpr_row() :: {ext_mess_id(), ExtData :: binary()}.
-type decoded_muc_gdpr_row() :: {ext_mess_id(), exml:element()}.

-spec decode_row(db_row(), env_vars()) -> mod_mam_pm:message_row().
-spec decode_row(db_row(), env_vars()) -> mod_mam:message_row().
decode_row({ExtMessID, ExtSrcJID, ExtData}, Env) ->
MessID = mongoose_rdbms:result_to_integer(ExtMessID),
SrcJID = decode_jid(ExtSrcJID, Env),
Packet = decode_packet(ExtData, Env),
#{id => MessID, jid => SrcJID, packet => Packet}.

-spec decode_muc_row(db_muc_row(), env_vars()) -> mod_mam_pm:message_row().
-spec decode_muc_row(db_muc_row(), env_vars()) -> mod_mam:message_row().
decode_muc_row({ExtMessID, Nick, ExtData}, Env = #{archive_jid := RoomJID}) ->
MessID = mongoose_rdbms:result_to_integer(ExtMessID),
SrcJID = jid:replace_resource(RoomJID, Nick),
Expand All @@ -31,7 +31,7 @@ decode_muc_gdpr_row({ExtMessID, ExtData}, Env) ->
{ExtMessID, Packet}.

-spec decode_retraction_info(env_vars(),
[{binary(), mod_mam_pm:message_id(), binary()}],
[{binary(), mod_mam:message_id(), binary()}],
mod_mam_utils:retraction_id()) ->
skip | mod_mam_utils:retraction_info().
decode_retraction_info(_Env, [], _) -> skip;
Expand Down
2 changes: 1 addition & 1 deletion src/mam/mam_encoder.erl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ extend_lookup_params(#{start_ts := Start, end_ts := End, with_jid := WithJID,
remote_bare_jid => maybe_encode_bare_jid(WithJID, Env),
remote_resource => jid_to_non_empty_resource(WithJID)}.

-spec encode_message(mod_mam_pm:archive_message_params(), env_vars(), list(db_mapping())) ->
-spec encode_message(mod_mam:archive_message_params(), env_vars(), list(db_mapping())) ->
[encoded_field_value()].
encode_message(Params, Env, Mappings) ->
[encode_value_using_mapping(Params, Env, Mapping) || Mapping <- Mappings].
Expand Down
10 changes: 5 additions & 5 deletions src/mam/mam_iq.erl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
| 'mam_get_message_form'.

-type lookup_params() :: #{
archive_id => mod_mam_pm:archive_id(),
archive_id => mod_mam:archive_id(),
owner_jid => jid:jid(),
caller_jid => jid:jid(),
rsm => jlib:rsm_in() | undefined,
Expand All @@ -47,16 +47,16 @@
page_size => non_neg_integer(),
ordering_direction => backward | forward,
%% unix_timestamp() is in microseconds
now => mod_mam_pm:unix_timestamp(),
now => mod_mam:unix_timestamp(),
%% Filtering by date
start_ts => mod_mam_pm:unix_timestamp() | undefined,
end_ts => mod_mam_pm:unix_timestamp() | undefined,
start_ts => mod_mam:unix_timestamp() | undefined,
end_ts => mod_mam:unix_timestamp() | undefined,
%% Filtering by contact
with_jid => jid:jid() | undefined,
%% Filtering by body text
search_text => binary() | undefined,
%% Filtering Result Set based on message ids
borders => mod_mam_pm:borders() | undefined,
borders => mod_mam:borders() | undefined,
%% Affects 'policy-violation' for a case when:
%% - user does not use forms to query archive
%% - user does not provide "set" element
Expand Down
8 changes: 4 additions & 4 deletions src/mam/mam_lookup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
-type filter() :: mam_filter:filter().
-type env_vars() :: mod_mam_rdbms_arch:env_vars().
-type params() :: map().
-type message_id() :: mod_mam_pm:message_id().
-type message_id() :: mod_mam:message_id().
-type maybe_rsm() :: #rsm_in{} | undefined.
-type opt_count_type() :: last_page | by_offset | none.

Expand All @@ -18,7 +18,7 @@
%% - lookup_fn
%% - decode_row_fn
-spec lookup(env_vars(), filter(), params()) ->
{ok, mod_mam_pm:lookup_result()} | {error, item_not_found}.
{ok, mod_mam:lookup_result()} | {error, item_not_found}.
lookup(Env = #{}, Filter, Params = #{rsm := RSM}) when is_list(Filter) ->
OptParams = Params#{opt_count_type => opt_count_type(RSM)},
choose_lookup_messages_strategy(Env, Filter, OptParams).
Expand Down Expand Up @@ -155,12 +155,12 @@ rsm_to_regular_lookup_vars(RSM, Filter, Offset, PageSize) ->
decode_rows(MessageRows, Env) ->
[decode_row(Row, Env) || Row <- MessageRows].

-spec decoded_row_to_message_id(mod_mam_pm:message_row()) -> mod_mam_pm:message_id().
-spec decoded_row_to_message_id(mod_mam:message_row()) -> mod_mam:message_id().
decoded_row_to_message_id(#{id := MessId}) -> MessId.

-spec extract_messages(Env :: env_vars(),
Filter :: filter(), Offset :: non_neg_integer(), Max :: pos_integer(),
Order :: asc | desc) -> [mod_mam_pm:message_row()].
Order :: asc | desc) -> [mod_mam:message_row()].
extract_messages(_Env, _Filter, _Offset, 0 = _Max, _Order) ->
[];
extract_messages(Env, Filter, Offset, Max, Order) ->
Expand Down
4 changes: 2 additions & 2 deletions src/mam/mam_send_message.erl
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
-ignore_xref([behaviour_info/1]).

-callback send_message(
Row :: mod_mam_pm:message_row(),
Row :: mod_mam:message_row(),
ArcJID :: jid:jid(),
From :: jid:jid(),
Packet :: exml:element()) -> Acc :: mongoose_acc:t().

-spec call_send_message(
SendModule :: module(),
Row :: mod_mam_pm:message_row(),
Row :: mod_mam:message_row(),
ArcJID :: jid:jid(),
From :: jid:jid(),
Packet :: exml:element()) -> Acc :: mongoose_acc:t().
Expand Down
60 changes: 58 additions & 2 deletions src/mam/mod_mam.erl
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,71 @@
-behaviour(gen_mod).
-behaviour(mongoose_module_metrics).

-include("mod_mam.hrl").
-include("mongoose_config_spec.hrl").

-type module_opts() :: gen_mod:module_opts().
-type module_map() :: gen_mod_deps:module_map().

%% ----------------------------------------------------------------------
%% Datetime types
%% Microseconds from 01.01.1970
-type unix_timestamp() :: non_neg_integer().

%% ----------------------------------------------------------------------
%% Other types
-type archive_behaviour() :: roster | always | never.
-type message_id() :: non_neg_integer().

-type archive_id() :: non_neg_integer().

-type borders() :: #mam_borders{}.

-type message_row() :: #{id := message_id(), jid := jid:jid(), packet := exml:element()}.
-type lookup_result() :: {TotalCount :: non_neg_integer() | undefined,
Offset :: non_neg_integer() | undefined,
MessageRows :: [message_row()]}.

%% Internal types
-type iterator_fun() :: fun(() -> {'ok', {_, _}}).
-type rewriter_fun() :: fun((JID :: jid:literal_jid())
-> jid:literal_jid()).
-type restore_option() :: {rewrite_jids, rewriter_fun() | [{binary(), binary()}]}
| new_message_ids.

-type preference() :: {DefaultMode :: archive_behaviour(),
AlwaysJIDs :: [jid:literal_jid()],
NeverJIDs :: [jid:literal_jid()]}.

-type archive_message_params() :: #{message_id := message_id(),
archive_id := archive_id(),
local_jid := jid:jid(),
remote_jid := jid:jid(),
source_jid := jid:jid(),
origin_id := binary() | none,
direction := atom(),
packet := exml:element(),
%% Only in mod_mam_muc_rdbms_arch:retract_message/2
sender_id => archive_id()}.

-export_type([rewriter_fun/0,
borders/0,
preference/0,
archive_behaviour/0,
iterator_fun/0,
unix_timestamp/0,
archive_id/0,
lookup_result/0,
message_row/0,
message_id/0,
restore_option/0,
archive_message_params/0
]).

-export([start/2, stop/1, config_spec/0, supported_features/0, deps/2, config_metrics/1]).

-export([remove_unused_backend_opts/1]).

-include("mongoose_config_spec.hrl").

%%--------------------------------------------------------------------
%% API
%%--------------------------------------------------------------------
Expand Down
8 changes: 4 additions & 4 deletions src/mam/mod_mam_cache_user.erl
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ muc_hooks(HostType) ->
%% API
%%====================================================================
-spec cached_archive_id(undefined, mongooseim:host_type(), jid:jid()) ->
mod_mam_pm:archive_id().
mod_mam:archive_id().
cached_archive_id(undefined, HostType, ArcJid) ->
case mongoose_user_cache:get_entry(HostType, ?MODULE, ArcJid) of
#{id := ArchId} -> ArchId;
Expand All @@ -84,8 +84,8 @@ cached_archive_id(undefined, HostType, ArcJid) ->
undefined
end.

-spec store_archive_id(mod_mam_pm:archive_id(), mongooseim:host_type(), jid:jid())
-> mod_mam_pm:archive_id().
-spec store_archive_id(mod_mam:archive_id(), mongooseim:host_type(), jid:jid())
-> mod_mam:archive_id().
store_archive_id(ArchId, HostType, ArcJid) ->
case erase(mam_not_cached_flag) of
undefined ->
Expand All @@ -96,7 +96,7 @@ store_archive_id(ArchId, HostType, ArcJid) ->
end.

-spec remove_archive(Acc :: map(), HostType :: mongooseim:host_type(),
ArchId :: mod_mam_pm:archive_id(), ArcJid :: jid:jid()) -> map().
ArchId :: mod_mam:archive_id(), ArcJid :: jid:jid()) -> map().
remove_archive(Acc, HostType, _UserID, ArcJid) ->
mongoose_user_cache:delete_user(HostType, ?MODULE, ArcJid),
Acc.
Expand Down
2 changes: 1 addition & 1 deletion src/mam/mod_mam_cassandra_arch.erl
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ remove_archive(HostType, UserJID) ->
%% SELECT MESSAGES

-spec lookup_messages(Result :: any(), HostType :: host_type(), Params :: map()) ->
{ok, mod_mam_pm:lookup_result()}.
{ok, mod_mam:lookup_result()}.
lookup_messages({error, _Reason} = Result, _HostType, _Params) ->
Result;
lookup_messages(_Result, _HostType, #{search_text := <<_/binary>>}) ->
Expand Down
18 changes: 9 additions & 9 deletions src/mam/mod_mam_cassandra_prefs.erl
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ prepared_queries() ->
%% ----------------------------------------------------------------------
%% Internal functions and callbacks

-spec get_behaviour(Default :: mod_mam_pm:archive_behaviour(),
HostType :: host_type(), ArchiveID :: mod_mam_pm:archive_id(),
-spec get_behaviour(Default :: mod_mam:archive_behaviour(),
HostType :: host_type(), ArchiveID :: mod_mam:archive_id(),
LocJID :: jid:jid(), RemJID :: jid:jid()) -> any().
get_behaviour(DefaultBehaviour, HostType, _UserID, LocJID, RemJID) ->
BUserJID = mod_mam_utils:bare_jid(LocJID),
Expand All @@ -105,8 +105,8 @@ get_behaviour(DefaultBehaviour, HostType, _UserID, LocJID, RemJID) ->


-spec set_prefs(Result :: any(), HostType :: host_type(),
ArchiveID :: mod_mam_pm:archive_id(), ArchiveJID :: jid:jid(),
DefaultMode :: mod_mam_pm:archive_behaviour(),
ArchiveID :: mod_mam:archive_id(), ArchiveJID :: jid:jid(),
DefaultMode :: mod_mam:archive_behaviour(),
AlwaysJIDs :: [jid:literal_jid()],
NeverJIDs :: [jid:literal_jid()]) -> any().
set_prefs(_Result, HostType, _UserID, UserJID, DefaultMode, AlwaysJIDs, NeverJIDs) ->
Expand Down Expand Up @@ -144,9 +144,9 @@ encode_row(BUserJID, BRemoteJID, Behaviour, Timestamp) ->
behaviour => Behaviour, '[timestamp]' => Timestamp}.


-spec get_prefs(mod_mam_pm:preference(), _HostType :: host_type(),
ArchiveID :: mod_mam_pm:archive_id(), ArchiveJID :: jid:jid())
-> mod_mam_pm:preference().
-spec get_prefs(mod_mam:preference(), _HostType :: host_type(),
ArchiveID :: mod_mam:archive_id(), ArchiveJID :: jid:jid())
-> mod_mam:preference().
get_prefs({GlobalDefaultMode, _, _}, HostType, _UserID, UserJID) ->
BUserJID = mod_mam_utils:bare_jid(UserJID),
Params = #{user_jid => BUserJID},
Expand All @@ -155,7 +155,7 @@ get_prefs({GlobalDefaultMode, _, _}, HostType, _UserID, UserJID) ->
decode_prefs_rows(Rows, GlobalDefaultMode, [], []).


-spec remove_archive(mongoose_acc:t(), host_type(), mod_mam_pm:archive_id(), jid:jid()) ->
-spec remove_archive(mongoose_acc:t(), host_type(), mod_mam:archive_id(), jid:jid()) ->
mongoose_acc:t().
remove_archive(Acc, HostType, _UserID, UserJID) ->
remove_archive(HostType, UserJID),
Expand Down Expand Up @@ -197,7 +197,7 @@ decode_behaviour(<<"N">>) -> never.

-spec decode_prefs_rows([[term()]], DefaultMode, AlwaysJIDs, NeverJIDs) ->
{DefaultMode, AlwaysJIDs, NeverJIDs} when
DefaultMode :: mod_mam_pm:archive_behaviour(),
DefaultMode :: mod_mam:archive_behaviour(),
AlwaysJIDs :: [jid:literal_jid()],
NeverJIDs :: [jid:literal_jid()].
decode_prefs_rows([], DefaultMode, AlwaysJIDs, NeverJIDs) ->
Expand Down
14 changes: 7 additions & 7 deletions src/mam/mod_mam_elasticsearch_arch.erl
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ archive_size(_Size, _Host, _ArchiveId, OwnerJid) ->

-spec remove_archive(Acc :: mongoose_acc:t(),
Host :: jid:server(),
ArchiveId :: mod_mam_pm:archive_id(),
ArchiveId :: mod_mam:archive_id(),
OwnerJid :: jid:jid()) -> Acc when Acc :: map().
remove_archive(Acc, Host, _ArchiveId, OwnerJid) ->
remove_archive(Host, OwnerJid),
Expand Down Expand Up @@ -169,11 +169,11 @@ hooks(Host) ->
{mam_remove_archive, Host, ?MODULE, remove_archive, 50},
{get_mam_pm_gdpr_data, Host, ?MODULE, get_mam_pm_gdpr_data, 50}].

-spec make_document_id(binary(), mod_mam_pm:message_id()) -> binary().
-spec make_document_id(binary(), mod_mam:message_id()) -> binary().
make_document_id(Owner, MessageId) ->
<<Owner/binary, $$, (integer_to_binary(MessageId))/binary>>.

-spec make_document(mod_mam_pm:message_id(), binary(), binary(), binary(), exml:element()) ->
-spec make_document(mod_mam:message_id(), binary(), binary(), binary(), exml:element()) ->
map().
make_document(MessageId, Owner, Remote, SourceBinJid, Packet) ->
#{mam_id => MessageId,
Expand Down Expand Up @@ -225,13 +225,13 @@ range_filter(#{end_ts := End, start_ts := Start, borders := Borders, rsm := RSM}
range_filter(_) ->
[].

-spec maybe_add_end_filter(undefined | mod_mam_pm:message_id(), map()) -> map().
-spec maybe_add_end_filter(undefined | mod_mam:message_id(), map()) -> map().
maybe_add_end_filter(undefined, RangeMap) ->
RangeMap;
maybe_add_end_filter(Value, RangeMap) ->
RangeMap#{le => Value}.

-spec maybe_add_start_filter(undefined | mod_mam_pm:message_id(), map()) -> map().
-spec maybe_add_start_filter(undefined | mod_mam:message_id(), map()) -> map().
maybe_add_start_filter(undefined, RangeMap) ->
RangeMap;
maybe_add_start_filter(Value, RangeMap) ->
Expand All @@ -258,7 +258,7 @@ maybe_add_from_constraint(Query, #{rsm := #rsm_in{index = Offset}}) when is_inte
maybe_add_from_constraint(Query, _) ->
Query.

-spec search_result_to_mam_lookup_result(map(), map()) -> mod_mam_pm:lookup_result().
-spec search_result_to_mam_lookup_result(map(), map()) -> mod_mam:lookup_result().
search_result_to_mam_lookup_result(Result, Params) ->
#{<<"hits">> :=
#{<<"hits">> := Hits,
Expand All @@ -277,7 +277,7 @@ search_result_to_mam_lookup_result(Result, Params) ->
{CorrectedTotalCount, Offset, Messages}
end.

-spec hit_to_mam_message(map()) -> mod_mam_pm:message_row().
-spec hit_to_mam_message(map()) -> mod_mam:message_row().
hit_to_mam_message(#{<<"_source">> := JSON}) ->
MessageId = maps:get(<<"mam_id">>, JSON),
Packet = maps:get(<<"message">>, JSON),
Expand Down
Loading

0 comments on commit 91a3ac5

Please sign in to comment.