Class: SubscriptionClientNotice

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/subscription_client_notice.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.dismiss_allObject

[View source]

182
183
184
185
186
187
188
# File 'app/models/subscription_client_notice.rb', line 182

def self.dismiss_all
  where("
    notice_type = #{types[:info]} AND
    expired_at IS NULL AND
    dismissed_at IS NULL
  ").update_all("dismissed_at = now()")
end

.error_typesObject

[View source]

24
25
26
27
28
29
# File 'app/models/subscription_client_notice.rb', line 24

def self.error_types
  @error_types ||= [
    types[:connection_error],
    types[:warning]
  ]
end

.expire_all(notice_type, notice_subject_type, notice_subject_id) ⇒ Object

[View source]

190
191
192
193
194
195
196
197
# File 'app/models/subscription_client_notice.rb', line 190

def self.expire_all(notice_type, notice_subject_type, notice_subject_id)
  where("
    notice_type = #{notice_type} AND
    notice_subject_type = '#{notice_subject_type}' AND
    notice_subject_id = #{notice_subject_id} AND
    expired_at IS NULL
  ").update_all("expired_at = now()")
end

.expire_connection_error(notice_subject_type_key, notice_subject_id) ⇒ Object

[View source]

176
177
178
179
180
# File 'app/models/subscription_client_notice.rb', line 176

def self.expire_connection_error(notice_subject_type_key, notice_subject_id)
  expired_count = expire_all(types[:connection_error], notice_subject_types[notice_subject_type_key.to_sym],
                             notice_subject_id)
  publish_notice_count if expired_count.to_i.positive?
end

.list(notice_type: nil, notice_subject_type: nil, notice_subject_id: nil, title: nil, include_all: false, visible: false, page: nil, page_limit: 30) ⇒ Object

[View source]

129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'app/models/subscription_client_notice.rb', line 129

def self.list(notice_type: nil, notice_subject_type: nil, notice_subject_id: nil, title: nil, include_all: false, visible: false, page: nil, page_limit: 30)
  query = SubscriptionClientNotice.all
  query = query.where("hidden_at IS NULL") if visible && !include_all
  query = query.where("dismissed_at IS NULL") unless include_all
  query = query.where("expired_at IS NULL") unless include_all
  query = query.where("notice_subject_type = ?", notice_subject_type.to_s) if notice_subject_type
  query = query.where("notice_subject_id = ?", notice_subject_id.to_i) if notice_subject_id
  if notice_type
    type_query_str = notice_type.is_a?(Array) ? "notice_type IN (?)" : "notice_type = ?"
    query = query.where(type_query_str, notice_type)
  end
  query = query.where("title = ?", title) if title
  query = query.limit(page_limit).offset(page.to_i * page_limit) unless page.nil?
  query.order("expired_at DESC, updated_at DESC, dismissed_at DESC, created_at DESC")
end

.notice_subject_typesObject

[View source]

17
18
19
20
21
22
# File 'app/models/subscription_client_notice.rb', line 17

def self.notice_subject_types
  @notice_subject_types ||= {
    resource: "SubscriptionClientResource",
    supplier: "SubscriptionClientSupplier"
  }
end

.notify_connection_error(notice_subject_type_key, notice_subject_id) ⇒ Object

[View source]

145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'app/models/subscription_client_notice.rb', line 145

def self.notify_connection_error(notice_subject_type_key, notice_subject_id)
  notice_subject_type = notice_subject_types[notice_subject_type_key.to_sym]
  return false unless notice_subject_type

  notices = list(
    notice_type: types[:connection_error],
    notice_subject_type: notice_subject_type,
    notice_subject_id: notice_subject_id
  )
  if notices.any?
    notice = notices.first
    notice.updated_at = DateTime.now.iso8601(3)
    notice.save
  else
    opts = {}
    if notice_subject_type === notice_subject_types[:supplier]
      supplier = SubscriptionClientSupplier.find(notice_subject_id)
      opts[:supplier] = supplier.name
    end
    create!(
      title: I18n.t("subscription_client.notices.#{notice_subject_type_key}.connection_error.title", **opts),
      message: I18n.t("subscription_client.notices.#{notice_subject_type_key}.connection_error.message", **opts),
      notice_subject_type: notice_subject_type,
      notice_subject_id: notice_subject_id,
      notice_type: types[:connection_error],
      created_at: DateTime.now.iso8601(3),
      updated_at: DateTime.now.iso8601(3)
    )
  end
end

.publish_notice_countObject

[View source]

121
122
123
124
125
126
127
# File 'app/models/subscription_client_notice.rb', line 121

def self.publish_notice_count
  payload = {
    visible_notice_count: list(visible: true).count
  }
  group_id_key = SiteSetting.subscription_client_allow_moderator_subscription_management ? :staff : :admins
  MessageBus.publish("/subscription_client_user", payload, group_ids: [Group::AUTO_GROUPS[group_id_key.to_sym]])
end

.typesObject

[View source]

9
10
11
12
13
14
15
# File 'app/models/subscription_client_notice.rb', line 9

def self.types
  @types ||= {
    info: 0,
    warning: 1,
    connection_error: 2
  }
end

Instance Method Details

#can_hide?Boolean

Returns:

  • (Boolean)
[View source]

99
100
101
102
103
# File 'app/models/subscription_client_notice.rb', line 99

def can_hide?
  !hidden? && self.class.error_types.include?(notice_type) && (
    notice_subject_type === self.class.notice_subject_types[:resource]
  )
end

#dismiss!Object

[View source]

57
58
59
60
61
62
# File 'app/models/subscription_client_notice.rb', line 57

def dismiss!
  return unless dismissable?

  self.dismissed_at = DateTime.now.iso8601(3)
  save_and_publish
end

#dismissable?Boolean

Returns:

  • (Boolean)
[View source]

113
114
115
# File 'app/models/subscription_client_notice.rb', line 113

def dismissable?
  !expired? && !dismissed? && notice_type === self.class.types[:info]
end

#dismissed?Boolean

Returns:

  • (Boolean)
[View source]

109
110
111
# File 'app/models/subscription_client_notice.rb', line 109

def dismissed?
  dismissed_at.present?
end

#expire!Object

[View source]

82
83
84
85
86
87
88
89
# File 'app/models/subscription_client_notice.rb', line 82

def expire!
  if !expired?
    self.expired_at = DateTime.now.iso8601(3)
    save_and_publish
  else
    false
  end
end

#expired?Boolean

Returns:

  • (Boolean)
[View source]

105
106
107
# File 'app/models/subscription_client_notice.rb', line 105

def expired?
  expired_at.present?
end

#hidden?Boolean

Returns:

  • (Boolean)
[View source]

117
118
119
# File 'app/models/subscription_client_notice.rb', line 117

def hidden?
  hidden_at.present?
end

#hide!Object

[View source]

64
65
66
67
68
69
70
71
# File 'app/models/subscription_client_notice.rb', line 64

def hide!
  if can_hide?
    self.hidden_at = DateTime.now.iso8601(3)
    save_and_publish
  else
    false
  end
end

#plugin_status_resource?Boolean

Returns:

  • (Boolean)
[View source]

53
54
55
# File 'app/models/subscription_client_notice.rb', line 53

def plugin_status_resource?
  DiscourseSubscriptionClient::Notices::PLUGIN_STATUS_RESOURCE_ID === notice_subject_id
end

#resourceObject

[View source]

39
40
41
42
43
# File 'app/models/subscription_client_notice.rb', line 39

def resource
  return unless resource?

  notice_subject
end

#resource?Boolean

Returns:

  • (Boolean)
[View source]

49
50
51
# File 'app/models/subscription_client_notice.rb', line 49

def resource?
  notice_subject_type === self.class.notice_subject_types[:resource] && !plugin_status_resource?
end

#save_and_publishObject

[View source]

91
92
93
94
95
96
97
# File 'app/models/subscription_client_notice.rb', line 91

def save_and_publish
  if save
    self.class.publish_notice_count
  else
    false
  end
end

#show!Object

[View source]

73
74
75
76
77
78
79
80
# File 'app/models/subscription_client_notice.rb', line 73

def show!
  if hidden?
    self.hidden_at = nil
    save_and_publish
  else
    false
  end
end

#supplierObject

[View source]

31
32
33
34
35
36
37
# File 'app/models/subscription_client_notice.rb', line 31

def supplier
  if supplier?
    notice_subject
  elsif resource?
    notice_subject.supplier
  end
end

#supplier?Boolean

Returns:

  • (Boolean)
[View source]

45
46
47
# File 'app/models/subscription_client_notice.rb', line 45

def supplier?
  notice_subject_type === self.class.notice_subject_types[:supplier]
end