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

[15.0][FIX] mail_show_follower: Use company from the document to render messages correctly #1464

Merged

Conversation

carlos-lopez-tecnativa
Copy link
Contributor

@carlos-lopez-tecnativa carlos-lopez-tecnativa commented Oct 10, 2024

In a multi-company environment, if one company uses the show_followers_partner_format by partner_name and another company uses partner_email, a user with permissions in both companies may experience issues. If a document belongs to Company A, but the current company is set to Company B, the message format is taken from the current company (Company B) instead of the document’s company (Company A). This fix ensures that the message format is rendered based on the document's company.

TT51022 @Tecnativa
@pedrobaeza @chienandalu could you please review it, thanks

@OCA-git-bot
Copy link
Contributor

Hi @yajo,
some modules you are maintaining are being modified, check this out!

@pedrobaeza pedrobaeza added this to the 15.0 milestone Oct 10, 2024
This module aims to show the recipients of an email in the email header, but the previous implementation was not suitable for internal notes.

When **logging an internal note** with tagged users and/or partners, those specific recipients should be displayed
as well as the document's followers subscribed to this message subtype.

When **sending an email**, the recipients should be the document's followers.

This is achieved by using the union of `mail.mail`'s `recipient_ids` and `notified_partner_ids`.
@carlos-lopez-tecnativa carlos-lopez-tecnativa force-pushed the 15.0-fix-mail_show_follower-multicompany branch from 666a2f5 to a8af850 Compare October 10, 2024 19:00
@carlos-lopez-tecnativa
Copy link
Contributor Author

Backport of #1391

Copy link
Member

@yajo yajo left a comment

Choose a reason for hiding this comment

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

Good job, thanks! Some concerns below.

# recipients from any Notification Type (i.e. email, inbox, etc.)
recipients = mail.notification_ids.res_partner_id
record = self.env[mail.model].browse(mail.res_id)
company = getattr(record, "company_id", self.env.company)
Copy link
Member

Choose a reason for hiding this comment

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

suggestion: This is not the recommended way to get a field that you don't know if exists, because that attribute could perfectly be a method (imagine def company_id():) and break your logic. Hardly probable, but possible.

The rule of thumb way to make sure it's really a field:

Suggested change
company = getattr(record, "company_id", self.env.company)
company = record["company_id"]

Of course there can be a KeyError which you can circumvent with a try-except block, or a if "company_id" in record guard.

mail_show_follower/models/mail_mail.py Outdated Show resolved Hide resolved
recipients = mail.notification_ids.res_partner_id
record = self.env[mail.model].browse(mail.res_id)
company = getattr(record, "company_id", self.env.company)
show_internal_users = company and company.show_internal_users_cc
Copy link
Member

Choose a reason for hiding this comment

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

suggestion: This guard is unnecessary:

Suggested change
show_internal_users = company and company.show_internal_users_cc
show_internal_users = company.show_internal_users_cc

# it is saved in the body_html field so that it does
# not appear in the odoo log
mail.body_html = final_cc + mail.body_html
group_user = self.env.ref("base.group_user")
Copy link
Member

Choose a reason for hiding this comment

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

issue: although the PR description explains a clear and concise problem, the code I see here is a medium refactor of a very important (and yes, spaghethic) piece of code.

For example, you are going away from testing portal group membership into testing backend user membership.

So, the PR goes beyond scope and gets harder to review. Could you split the fix and the refactor in 2 different PRs? Or could you explain a little bit more what's this refactor doing, so it's easier to review?

…sages correctly + proper lang handle

In a multi-company environment, if one company uses the
show_followers_partner_format by partner_name and another company
uses partner_email, a user with permissions in both companies may
experience issues. If a document belongs to Company A, but the current
company is set to Company B, the message format is taken from the
current company (Company B) instead of the document’s company
(Company A). This fix ensures that the message format is rendered based
on the document's company.

It also fixes the lang handling for cases with False values.
@pedrobaeza pedrobaeza force-pushed the 15.0-fix-mail_show_follower-multicompany branch from a8af850 to 1de314a Compare October 11, 2024 18:10
@pedrobaeza
Copy link
Member

@yajo this is a backport of #1391, with only an extra patch in a separated commit, so all your comments should go to the 16.0 PR when it was done, and it was you who approve it. We are not going to invest more time in that part, as we will finally get to the newest version with the refactoring already done.

The only patch from us applied right now is the company one, but we have changed also the lang part, which indeed can have some side effects (but this is not new). We will fw-port it to 16 if this PR is accepted.

Please accept it as it is to get both versions with the good refactoring, or we will let it die as a PR injected where needed.

Copy link
Member

@yajo yajo left a comment

Choose a reason for hiding this comment

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

Thanks! I didn't realize this was a backport and there were 2 commits. Also thanks for that extra fix. Happy to help! ❤️

/ocabot merge minor

@OCA-git-bot
Copy link
Contributor

What a great day to merge this nice PR. Let's do it!
Prepared branch 15.0-ocabot-merge-pr-1464-by-yajo-bump-minor, awaiting test results.

@OCA-git-bot OCA-git-bot merged commit d68c9f1 into OCA:15.0 Oct 15, 2024
5 of 7 checks passed
@OCA-git-bot
Copy link
Contributor

Congratulations, your PR was merged at b786332. Thanks a lot for contributing to OCA. ❤️

@pedrobaeza pedrobaeza deleted the 15.0-fix-mail_show_follower-multicompany branch November 1, 2024 08:57
@pedrobaeza
Copy link
Member

@carlos-lopez-tecnativa please fw-port the second patch to 16

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants