2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-11-25 01:26:49 -06:00
|
|
|
class GroupShowSerializer < BasicGroupSerializer
|
2020-05-25 00:38:47 -05:00
|
|
|
attributes :is_group_user, :is_group_owner, :is_group_owner_display, :mentionable, :messageable, :flair_icon, :flair_type
|
2016-11-25 01:26:49 -06:00
|
|
|
|
2020-09-16 13:58:52 -05:00
|
|
|
def self.admin_attributes(*attrs)
|
|
|
|
attributes(*attrs)
|
|
|
|
attrs.each do |attr|
|
|
|
|
define_method "include_#{attr}?" do
|
|
|
|
scope.is_admin?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
admin_attributes :automatic_membership_email_domains,
|
|
|
|
:smtp_server,
|
|
|
|
:smtp_port,
|
|
|
|
:smtp_ssl,
|
|
|
|
:imap_server,
|
|
|
|
:imap_port,
|
|
|
|
:imap_ssl,
|
|
|
|
:imap_mailbox_name,
|
|
|
|
:imap_mailboxes,
|
|
|
|
:email_username,
|
|
|
|
:email_password,
|
|
|
|
:imap_last_error,
|
|
|
|
:imap_old_emails,
|
|
|
|
:imap_new_emails
|
|
|
|
|
|
|
|
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 01:26:49 -06:00
|
|
|
def include_is_group_user?
|
2017-07-27 03:51:25 -05:00
|
|
|
authenticated?
|
2016-11-25 01:26:49 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def is_group_user
|
2016-11-29 02:25:02 -06:00
|
|
|
!!fetch_group_user
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_is_group_owner?
|
2020-08-18 11:30:08 -05:00
|
|
|
authenticated? && fetch_group_user&.owner
|
2016-11-29 02:25:02 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def is_group_owner
|
2020-08-18 11:30:08 -05:00
|
|
|
true
|
2016-11-29 02:25:02 -06:00
|
|
|
end
|
|
|
|
|
2018-06-12 16:57:26 -05:00
|
|
|
def include_is_group_owner_display?
|
|
|
|
authenticated?
|
|
|
|
end
|
|
|
|
|
|
|
|
def is_group_owner_display
|
|
|
|
!!fetch_group_user&.owner
|
|
|
|
end
|
|
|
|
|
2017-07-27 03:51:25 -05:00
|
|
|
def include_mentionable?
|
|
|
|
authenticated?
|
|
|
|
end
|
|
|
|
|
2017-10-02 03:50:27 -05:00
|
|
|
def include_messageable?
|
|
|
|
authenticated?
|
|
|
|
end
|
|
|
|
|
2017-07-27 03:51:25 -05:00
|
|
|
def mentionable
|
|
|
|
Group.mentionable(scope.user).exists?(id: object.id)
|
|
|
|
end
|
|
|
|
|
2017-08-28 11:32:08 -05:00
|
|
|
def messageable
|
|
|
|
Group.messageable(scope.user).exists?(id: object.id)
|
|
|
|
end
|
|
|
|
|
2020-05-25 00:38:47 -05:00
|
|
|
def include_flair_icon?
|
2020-08-04 11:41:14 -05:00
|
|
|
flair_icon.present? && (is_group_owner || scope.is_admin?)
|
2020-05-25 00:38:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def include_flair_type?
|
2020-08-04 11:41:14 -05:00
|
|
|
flair_type.present? && (is_group_owner || scope.is_admin?)
|
2020-05-25 00:38:47 -05:00
|
|
|
end
|
|
|
|
|
2020-09-16 13:58:52 -05:00
|
|
|
[:watching, :regular, :tracking, :watching_first_post, :muted].each do |level|
|
|
|
|
define_method("#{level}_category_ids") do
|
|
|
|
group_category_notifications[NotificationLevels.all[level]] || []
|
|
|
|
end
|
|
|
|
|
|
|
|
define_method("#{level}_tags") do
|
|
|
|
group_tag_notifications[NotificationLevels.all[level]] || []
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-29 02:25:02 -06:00
|
|
|
private
|
|
|
|
|
2017-07-27 03:51:25 -05:00
|
|
|
def authenticated?
|
|
|
|
scope.authenticated?
|
|
|
|
end
|
|
|
|
|
2016-11-29 02:25:02 -06:00
|
|
|
def fetch_group_user
|
2020-09-10 21:08:06 -05:00
|
|
|
return @group_user if defined?(@group_user)
|
|
|
|
@group_user = object.group_users.find_by(user: scope.user)
|
2016-11-25 01:26:49 -06:00
|
|
|
end
|
2020-09-16 13:58:52 -05: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 01:26:49 -06:00
|
|
|
end
|