2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-11-25 02:26:49 -05:00
|
|
|
class GroupShowSerializer < BasicGroupSerializer
|
2020-05-25 01:38:47 -04:00
|
|
|
attributes :is_group_user,
|
|
|
|
:is_group_owner,
|
|
|
|
:is_group_owner_display,
|
|
|
|
:mentionable,
|
|
|
|
:messageable,
|
|
|
|
:flair_icon,
|
|
|
|
:flair_type
|
2016-11-25 02:26:49 -05:00
|
|
|
|
2020-09-16 14:58:52 -04:00
|
|
|
def self.admin_attributes(*attrs)
|
|
|
|
attributes(*attrs)
|
|
|
|
attrs.each do |attr|
|
|
|
|
define_method "include_#{attr}?" do
|
|
|
|
scope.is_admin?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
FEATURE: Improve group email settings UI (#13083)
This overhauls the user interface for the group email settings management, aiming to make it a lot easier to test the settings entered and confirm they are correct before proceeding. We do this by forcing the user to test the settings before they can be saved to the database. It also includes some quality of life improvements around setting up IMAP and SMTP for our first supported provider, GMail. This PR does not remove the old group email config, that will come in a subsequent PR. This is related to https://meta.discourse.org/t/imap-support-for-group-inboxes/160588 so read that if you would like more backstory.
### UI
Both site settings of `enable_imap` and `enable_smtp` must be true to test this. You must enable SMTP first to enable IMAP.
You can prefill the SMTP settings with GMail configuration. To proceed with saving these settings you must test them, which is handled by the EmailSettingsValidator.
If there is an issue with the configuration or credentials a meaningful error message should be shown.
IMAP settings must also be validated when IMAP is enabled, before saving.
When saving IMAP, we fetch the mailboxes for that account and populate them. This mailbox must be selected and saved for IMAP to work (the feature acts as though it is disabled until the mailbox is selected and saved):
### Database & Backend
This adds several columns to the Groups table. The purpose of this change is to make it much more explicit that SMTP/IMAP is enabled for a group, rather than relying on settings not being null. Also included is an UPDATE query to backfill these columns. These columns are automatically filled when updating the group.
For GMail, we now filter the mailboxes returned. This is so users cannot use a mailbox like Sent or Trash for syncing, which would generally be disastrous.
There is a new group endpoint for testing email settings. This may be useful in the future for other places in our UI, at which point it can be extracted to a more generic endpoint or module to be included.
2021-05-27 19:28:18 -04:00
|
|
|
has_one :smtp_updated_by, embed: :object, serializer: BasicUserSerializer
|
|
|
|
has_one :imap_updated_by, embed: :object, serializer: BasicUserSerializer
|
|
|
|
|
2020-09-16 14:58:52 -04:00
|
|
|
admin_attributes :automatic_membership_email_domains,
|
|
|
|
:smtp_server,
|
|
|
|
:smtp_port,
|
2024-07-17 20:33:14 -04:00
|
|
|
:smtp_ssl_mode,
|
FEATURE: Improve group email settings UI (#13083)
This overhauls the user interface for the group email settings management, aiming to make it a lot easier to test the settings entered and confirm they are correct before proceeding. We do this by forcing the user to test the settings before they can be saved to the database. It also includes some quality of life improvements around setting up IMAP and SMTP for our first supported provider, GMail. This PR does not remove the old group email config, that will come in a subsequent PR. This is related to https://meta.discourse.org/t/imap-support-for-group-inboxes/160588 so read that if you would like more backstory.
### UI
Both site settings of `enable_imap` and `enable_smtp` must be true to test this. You must enable SMTP first to enable IMAP.
You can prefill the SMTP settings with GMail configuration. To proceed with saving these settings you must test them, which is handled by the EmailSettingsValidator.
If there is an issue with the configuration or credentials a meaningful error message should be shown.
IMAP settings must also be validated when IMAP is enabled, before saving.
When saving IMAP, we fetch the mailboxes for that account and populate them. This mailbox must be selected and saved for IMAP to work (the feature acts as though it is disabled until the mailbox is selected and saved):
### Database & Backend
This adds several columns to the Groups table. The purpose of this change is to make it much more explicit that SMTP/IMAP is enabled for a group, rather than relying on settings not being null. Also included is an UPDATE query to backfill these columns. These columns are automatically filled when updating the group.
For GMail, we now filter the mailboxes returned. This is so users cannot use a mailbox like Sent or Trash for syncing, which would generally be disastrous.
There is a new group endpoint for testing email settings. This may be useful in the future for other places in our UI, at which point it can be extracted to a more generic endpoint or module to be included.
2021-05-27 19:28:18 -04:00
|
|
|
:smtp_enabled,
|
|
|
|
:smtp_updated_at,
|
|
|
|
:smtp_updated_by,
|
2020-09-16 14:58:52 -04:00
|
|
|
:imap_server,
|
|
|
|
:imap_port,
|
|
|
|
:imap_ssl,
|
|
|
|
:imap_mailbox_name,
|
|
|
|
:imap_mailboxes,
|
FEATURE: Improve group email settings UI (#13083)
This overhauls the user interface for the group email settings management, aiming to make it a lot easier to test the settings entered and confirm they are correct before proceeding. We do this by forcing the user to test the settings before they can be saved to the database. It also includes some quality of life improvements around setting up IMAP and SMTP for our first supported provider, GMail. This PR does not remove the old group email config, that will come in a subsequent PR. This is related to https://meta.discourse.org/t/imap-support-for-group-inboxes/160588 so read that if you would like more backstory.
### UI
Both site settings of `enable_imap` and `enable_smtp` must be true to test this. You must enable SMTP first to enable IMAP.
You can prefill the SMTP settings with GMail configuration. To proceed with saving these settings you must test them, which is handled by the EmailSettingsValidator.
If there is an issue with the configuration or credentials a meaningful error message should be shown.
IMAP settings must also be validated when IMAP is enabled, before saving.
When saving IMAP, we fetch the mailboxes for that account and populate them. This mailbox must be selected and saved for IMAP to work (the feature acts as though it is disabled until the mailbox is selected and saved):
### Database & Backend
This adds several columns to the Groups table. The purpose of this change is to make it much more explicit that SMTP/IMAP is enabled for a group, rather than relying on settings not being null. Also included is an UPDATE query to backfill these columns. These columns are automatically filled when updating the group.
For GMail, we now filter the mailboxes returned. This is so users cannot use a mailbox like Sent or Trash for syncing, which would generally be disastrous.
There is a new group endpoint for testing email settings. This may be useful in the future for other places in our UI, at which point it can be extracted to a more generic endpoint or module to be included.
2021-05-27 19:28:18 -04:00
|
|
|
:imap_enabled,
|
|
|
|
:imap_updated_at,
|
|
|
|
:imap_updated_by,
|
2020-09-16 14:58:52 -04:00
|
|
|
:email_username,
|
|
|
|
:email_password,
|
2022-02-06 22:52:01 -05:00
|
|
|
:email_from_alias,
|
2020-09-16 14:58:52 -04:00
|
|
|
:imap_last_error,
|
|
|
|
:imap_old_emails,
|
2021-01-21 16:59:34 -05:00
|
|
|
:imap_new_emails,
|
2021-01-28 18:59:10 -05:00
|
|
|
:message_count,
|
2021-12-09 07:30:27 -05:00
|
|
|
:allow_unknown_sender_topic_replies,
|
|
|
|
:associated_group_ids
|
2020-09-16 14:58:52 -04:00
|
|
|
|
|
|
|
def self.admin_or_owner_attributes(*attrs)
|
|
|
|
attributes(*attrs)
|
|
|
|
attrs.each do |attr|
|
|
|
|
define_method "include_#{attr}?" do
|
|
|
|
scope.is_admin? || (include_is_group_owner? && is_group_owner)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
admin_or_owner_attributes :watching_category_ids,
|
|
|
|
:tracking_category_ids,
|
|
|
|
:watching_first_post_category_ids,
|
|
|
|
:regular_category_ids,
|
|
|
|
:muted_category_ids,
|
|
|
|
:watching_tags,
|
|
|
|
:watching_first_post_tags,
|
|
|
|
:tracking_tags,
|
|
|
|
:regular_tags,
|
|
|
|
:muted_tags
|
|
|
|
|
2016-11-25 02:26:49 -05:00
|
|
|
def include_is_group_user?
|
2017-07-27 04:51:25 -04:00
|
|
|
authenticated?
|
2016-11-25 02:26:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def is_group_user
|
2016-11-29 03:25:02 -05:00
|
|
|
!!fetch_group_user
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_is_group_owner?
|
2020-08-18 12:30:08 -04:00
|
|
|
authenticated? && fetch_group_user&.owner
|
2016-11-29 03:25:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def is_group_owner
|
2020-08-18 12:30:08 -04:00
|
|
|
true
|
2016-11-29 03:25:02 -05:00
|
|
|
end
|
|
|
|
|
2018-06-12 17:57:26 -04:00
|
|
|
def include_is_group_owner_display?
|
|
|
|
authenticated?
|
|
|
|
end
|
|
|
|
|
|
|
|
def is_group_owner_display
|
|
|
|
!!fetch_group_user&.owner
|
|
|
|
end
|
|
|
|
|
2017-07-27 04:51:25 -04:00
|
|
|
def include_mentionable?
|
|
|
|
authenticated?
|
|
|
|
end
|
|
|
|
|
2017-10-02 04:50:27 -04:00
|
|
|
def include_messageable?
|
|
|
|
authenticated?
|
|
|
|
end
|
|
|
|
|
2017-07-27 04:51:25 -04:00
|
|
|
def mentionable
|
|
|
|
Group.mentionable(scope.user).exists?(id: object.id)
|
|
|
|
end
|
|
|
|
|
2017-08-28 12:32:08 -04:00
|
|
|
def messageable
|
2021-08-30 01:08:33 -04:00
|
|
|
scope.can_send_private_message?(object)
|
2017-08-28 12:32:08 -04:00
|
|
|
end
|
|
|
|
|
2020-05-25 01:38:47 -04:00
|
|
|
def include_flair_icon?
|
2020-08-04 12:41:14 -04:00
|
|
|
flair_icon.present? && (is_group_owner || scope.is_admin?)
|
2020-05-25 01:38:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def include_flair_type?
|
2020-08-04 12:41:14 -04:00
|
|
|
flair_type.present? && (is_group_owner || scope.is_admin?)
|
2020-05-25 01:38:47 -04:00
|
|
|
end
|
|
|
|
|
2020-09-16 14:58:52 -04:00
|
|
|
%i[watching regular tracking watching_first_post muted].each do |level|
|
|
|
|
define_method("#{level}_category_ids") do
|
|
|
|
group_category_notifications[NotificationLevels.all[level]] || []
|
|
|
|
end
|
|
|
|
|
2020-09-16 15:39:57 -04:00
|
|
|
define_method("include_#{level}_tags?") do
|
2020-09-16 16:02:28 -04:00
|
|
|
SiteSetting.tagging_enabled? && scope.is_admin? || (include_is_group_owner? && is_group_owner)
|
2020-09-16 15:39:57 -04:00
|
|
|
end
|
|
|
|
|
2020-09-16 14:58:52 -04:00
|
|
|
define_method("#{level}_tags") { group_tag_notifications[NotificationLevels.all[level]] || [] }
|
|
|
|
end
|
|
|
|
|
2021-12-09 07:30:27 -05:00
|
|
|
def associated_group_ids
|
|
|
|
object.associated_groups.map(&:id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_associated_group_ids?
|
|
|
|
scope.can_associate_groups?
|
|
|
|
end
|
|
|
|
|
2016-11-29 03:25:02 -05:00
|
|
|
private
|
|
|
|
|
2017-07-27 04:51:25 -04:00
|
|
|
def authenticated?
|
|
|
|
scope.authenticated?
|
|
|
|
end
|
|
|
|
|
2016-11-29 03:25:02 -05:00
|
|
|
def fetch_group_user
|
2020-09-10 22:08:06 -04:00
|
|
|
return @group_user if defined?(@group_user)
|
|
|
|
@group_user = object.group_users.find_by(user: scope.user)
|
2016-11-25 02:26:49 -05:00
|
|
|
end
|
2020-09-16 14:58:52 -04:00
|
|
|
|
|
|
|
def group_category_notifications
|
|
|
|
@group_category_notification_defaults ||=
|
|
|
|
GroupCategoryNotificationDefault
|
|
|
|
.where(group_id: object.id)
|
|
|
|
.pluck(:notification_level, :category_id)
|
|
|
|
.inject({}) do |h, arr|
|
|
|
|
h[arr[0]] ||= []
|
|
|
|
h[arr[0]] << arr[1]
|
|
|
|
h
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def group_tag_notifications
|
|
|
|
@group_tag_notification_defaults ||=
|
|
|
|
GroupTagNotificationDefault
|
|
|
|
.where(group_id: object.id)
|
|
|
|
.joins(:tag)
|
|
|
|
.pluck(:notification_level, :name)
|
|
|
|
.inject({}) do |h, arr|
|
|
|
|
h[arr[0]] ||= []
|
|
|
|
h[arr[0]] << arr[1]
|
|
|
|
h
|
|
|
|
end
|
|
|
|
end
|
2016-11-25 02:26:49 -05:00
|
|
|
end
|