Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add host_type to hook calls in core modules #3097

Merged
merged 24 commits into from
Apr 29, 2021

Conversation

chrzaszcz
Copy link
Member

@chrzaszcz chrzaszcz commented Apr 23, 2021

Make all hook calls not related to any extension modules work with host types.

The following rules were applied:

  • If Acc is passed, make sure its host_type is consistent with the LServer argument that was passed before and get the host type form the Acc inside mongoose_hooks.
  • Otherwise, make the caller determine the host type and provide it to the hook. Remove the LServer argument if possible.
  • For unused s2s hooks, make them global.
  • Do not rearrange the arguments when not necessary.
  • If a hook is also called by extension modules, add a host type to these calls as well to make them consistent.

More details in commit messages.

@codecov
Copy link

codecov bot commented Apr 23, 2021

Codecov Report

Merging #3097 (fe91830) into master (160ba44) will increase coverage by 0.06%.
The diff coverage is 98.07%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #3097      +/-   ##
==========================================
+ Coverage   79.06%   79.12%   +0.06%     
==========================================
  Files         386      386              
  Lines       31830    31820      -10     
==========================================
+ Hits        25165    25177      +12     
+ Misses       6665     6643      -22     
Impacted Files Coverage Δ
src/global_distrib/mod_global_distrib_mapping.erl 98.11% <ø> (ø)
src/metrics/mongoose_metrics_hooks.erl 97.05% <ø> (ø)
src/mod_ping.erl 95.31% <ø> (ø)
src/ejabberd_hooks.erl 86.15% <66.66%> (-0.95%) ⬇️
src/ejabberd_s2s_in.erl 77.11% <75.00%> (-0.24%) ⬇️
src/admin_extra/service_admin_extra_gdpr.erl 94.82% <100.00%> (+0.09%) ⬆️
src/admin_extra/service_admin_extra_roster.erl 87.16% <100.00%> (ø)
src/auth/ejabberd_auth.erl 75.80% <100.00%> (+0.39%) ⬆️
src/auth/ejabberd_auth_anonymous.erl 48.75% <100.00%> (ø)
src/ejabberd_c2s.erl 89.43% <100.00%> (ø)
... and 23 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 160ba44...fe91830. Read the comment docs.

@chrzaszcz chrzaszcz force-pushed the multi-tenancy-core-hooks branch 5 times, most recently from 7c57d27 to 438415b Compare April 26, 2021 13:00
- For binary: run as before
- For 'undefined': log an error and return Acc (no-op)
    This might happen if host type is taken from the Acc.
    We need to eradicate this case from the code later.
They have been deprecated for a long time
  and they conflict with new functions that will be introduced
  for host_type.
This required adding it to open_session,
  what in turn required adding it to mongoose_client_api.
Take it from Acc as the hook is always run for a local user.
Reason: The stanza is handled locally and host type
          can be used when running local hooks.
        Anyway, this clause is used only for sending MAM messages.
It is safe to take it from the acc created for the local user.
The Acc is constructed in ejabberd_c2s from the local state,
which contains the host type of the local user.
Hooks changed:
- roster_in_subscription
- sm_filter_offline_message
- privacy_get_user_list
- offline_groupchat_message_hook
Hooks changed:
  - offline_message_hook
  - failed_to_store_message
This change is in a separate commit for clarity.
The host_type argument is removed as it is always calculated
  from the server part of the JID.
Motivation:
- Simplicity.
- They were unused - no handlers in the code base.
- One was already global, now they are consistent.
- Host type can be introduced anytime when a need arises.
Also:
- Avoid duplicate calculation of host_type in ejabberd_auth:remove_user/1
- Provide host_type in sm_register_connection_hook
    to pass it in a call to register_user from ejabberd_auth_anonymous
@chrzaszcz chrzaszcz marked this pull request as ready for review April 28, 2021 11:41
Copy link
Contributor

@arcusfelis arcusfelis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good

case lists:any(fun(El) -> El == ok end, RemoveResult) of
true ->
Acc = mongoose_acc:new(#{ location => ?LOCATION,
host_type => HostType,
lserver => LServer,
element => undefined }),
mongoose_hooks:remove_user(LServer, Acc, LUser),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe it could be remove_user(Acc, LServer, LUser). Though backward compatibility would suffer.

Copy link
Member Author

@chrzaszcz chrzaszcz Apr 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather rearrange the hook arguments in a separate PR as these changes are quite tricky.

As I said in the PR description: Do not rearrange the arguments when not necessary.

@@ -153,13 +153,14 @@ remove_connection(SID, LUser, LServer) ->

%% @doc Register connection
-spec register_connection(Acc,
HostType :: binary(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have this type defined in mongooseim:host_type() since yesterday

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've seen it. I don't think it makes sense to amend this PR though. I'd rather do it later.

@@ -1098,7 +1098,7 @@ get_roster_old(DestServer, JID) ->
A = mongoose_acc:new(#{ location => ?LOCATION,
lserver => DestServer,
element => undefined }),
A2 = mongoose_hooks:roster_get(DestServer, A, JID),
A2 = mongoose_hooks:roster_get(A, JID),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be Acc instead of A, to be consistent.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, I just wanted to minimize changes in mod_* for now.

Copy link
Collaborator

@DenysGonchar DenysGonchar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks fine. a bit hard to follow, but we all knew it's gonna be like that :)

@DenysGonchar DenysGonchar merged commit 48039c1 into master Apr 29, 2021
@DenysGonchar DenysGonchar deleted the multi-tenancy-core-hooks branch April 29, 2021 08:00
@Premwoik Premwoik added this to the 5.0.0 milestone Oct 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants