group_serializer.rb 1.38 KB
Newer Older
Rob Colbert's avatar
Rob Colbert committed
1 2 3 4 5
# frozen_string_literal: true

class REST::GroupSerializer < ActiveModel::Serializer
  include RoutingHelper
  
mgabdev's avatar
mgabdev committed
6 7
  attributes :id, :title, :description, :description_html,
             :cover_image_url, :is_archived, :member_count,
8
             :created_at, :is_private, :is_visible, :slug, :url,
mgabdev's avatar
mgabdev committed
9
             :tags, :group_category, :password, :has_password
Rob Colbert's avatar
Rob Colbert committed
10 11 12 13 14

  def id
    object.id.to_s
  end

15
  def has_password
mgabdev's avatar
mgabdev committed
16
    object.has_password?
17 18 19
  end

  def password
20 21 22 23
    if !defined?(current_user) || current_user.nil?
      return nil
    end

24 25 26 27 28 29
    if instance_options[:individual_group]
      if object.group_accounts.where(account_id: current_user.account.id, role: :admin).exists?
        object.password
      else
        nil
      end
30 31 32 33 34
    else
      nil
    end
  end

35 36 37 38
  def group_category
    if object.group_categories
      object.group_categories
    end
39 40
  end

41 42 43 44 45 46 47 48
  def description
    object.description
  end

  def description_html
    Formatter.instance.formatGroupDescription(object.description).strip
  end

2458773093's avatar
2458773093 committed
49 50 51 52 53 54
  def clean_migrated_url
    object
      .cover_image_file_name
      .sub("gab://groups/", "https://gab.com/media/user/")
  end

Rob Colbert's avatar
Rob Colbert committed
55
  def cover_image_url
2458773093's avatar
2458773093 committed
56 57 58 59
    if object.cover_image_file_name and object.cover_image_file_name.start_with? "gab://groups/"
      return clean_migrated_url
    end

Rob Colbert's avatar
Rob Colbert committed
60 61
    full_asset_url(object.cover_image.url)
  end
62 63 64 65 66

  def url
    group_show_page_url(object)
  end
  
Rob Colbert's avatar
Rob Colbert committed
67
end