Class: DiscourseSubscriptionClient::NoticesController

Inherits:
AdminController
  • Object
show all
Defined in:
app/controllers/discourse_subscription_client/notices_controller.rb

Instance Method Summary collapse

Methods inherited from AdminController

#ensure_can_manage_subscriptions, #failed_json, #render_json_dump, #render_serialized, #serialize_data, #success_json

Instance Method Details

#dismissObject



50
51
52
53
54
55
56
# File 'app/controllers/discourse_subscription_client/notices_controller.rb', line 50

def dismiss
  if @notice.dismissable? && @notice.dismiss!
    render json: success_json.merge(dismissed_at: @notice.dismissed_at)
  else
    render json: failed_json
  end
end

#hideObject



66
67
68
69
70
71
72
# File 'app/controllers/discourse_subscription_client/notices_controller.rb', line 66

def hide
  if @notice.can_hide? && @notice.hide!
    render json: success_json.merge(hidden_at: @notice.hidden_at)
  else
    render json: failed_json
  end
end

#indexObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/controllers/discourse_subscription_client/notices_controller.rb', line 7

def index
  notice_type = params[:notice_type]
  notice_subject_type = params[:notice_subject_type]
  page = params[:page].to_i

  visible = ActiveRecord::Type::Boolean.new.cast(params[:visible])

  include_all = if current_user.admin
                  ActiveRecord::Type::Boolean.new.cast(params[:include_all])
                else
                  false
                end

  if notice_type
    notice_type = if notice_type.is_a?(Array)
                    notice_type.map { |t| SubscriptionClientNotice.types[t.to_sym] }
                  else
                    SubscriptionClientNotice.types[notice_type.to_sym]
                  end
  end

  if notice_subject_type
    notice_subject_type = if notice_subject_type.is_a?(Array)
                            notice_subject_type.map { |t| SubscriptionClientNotice.notice_subject_types[t.to_sym] }
                          else
                            SubscriptionClientNotice.notice_subject_types[notice_subject_type.to_sym]
                          end
  end

  notices = SubscriptionClientNotice.list(
    include_all: include_all,
    page: page,
    notice_type: notice_type,
    notice_subject_type: notice_subject_type,
    visible: visible
  )

  render_json_dump(
    notices: serialize_data(notices, NoticeSerializer),
    hidden_notice_count: SubscriptionClientNotice.hidden.count
  )
end

#showObject



58
59
60
61
62
63
64
# File 'app/controllers/discourse_subscription_client/notices_controller.rb', line 58

def show
  if @notice.hidden? && @notice.show!
    render json: success_json
  else
    render json: failed_json
  end
end