Class: ComposerMessagesFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/composer_messages_finder.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, details) ⇒ ComposerMessagesFinder

Returns a new instance of ComposerMessagesFinder.



4
5
6
7
8
# File 'lib/composer_messages_finder.rb', line 4

def initialize(user, details)
  @user = user
  @details = details
  @topic = Topic.find_by(id: details[:topic_id]) if details[:topic_id]
end

Class Method Details

.check_methodsObject



10
11
12
# File 'lib/composer_messages_finder.rb', line 10

def self.check_methods
  @check_methods ||= instance_methods.find_all { |m| m =~ /\Acheck\_/ }
end

.user_not_seen_in_a_while(usernames) ⇒ Object



283
284
285
286
287
288
289
# File 'lib/composer_messages_finder.rb', line 283

def self.user_not_seen_in_a_while(usernames)
  User
    .where(username_lower: usernames)
    .where("last_seen_at < ?", SiteSetting.pm_warn_user_last_seen_months_ago.months.ago)
    .pluck(:username)
    .sort
end

Instance Method Details

#check_avatar_notificationObject

Should a user be contacted to update their avatar?



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/composer_messages_finder.rb', line 78

def check_avatar_notification
  # A user has to be basic at least to be considered for an avatar notification
  return unless @user.has_trust_level?(TrustLevel[1])

  # We don't notify users who have avatars or who have been notified already.
  if @user.uploaded_avatar_id || UserHistory.exists_for_user?(@user, :notified_about_avatar)
    return
  end

  # Do not notify user if any of the following is true:
  # - "disable avatar education message" is enabled
  # - "sso overrides avatar" is enabled
  # - "allow uploaded avatars" is disabled
  if SiteSetting.disable_avatar_education_message ||
       SiteSetting.discourse_connect_overrides_avatar ||
       !TrustLevelAndStaffAndDisabledSetting.matches?(SiteSetting.allow_uploaded_avatars, @user)
    return
  end

  # If we got this far, log that we've nagged them about the avatar
  UserHistory.create!(
    action: UserHistory.actions[:notified_about_avatar],
    target_user_id: @user.id,
  )

  # Return the message
  {
    id: "avatar",
    templateName: "education",
    body:
      PrettyText.cook(
        I18n.t(
          "education.avatar",
          profile_path: "/u/#{@user.username_lower}/preferences/account#profile-picture",
        ),
      ),
  }
end

#check_dominating_topicObject



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/composer_messages_finder.rb', line 154

def check_dominating_topic
  return unless educate_reply?(:notified_about_dominating_topic)

  if @topic.blank? || @topic.user_id == @user.id ||
       @topic.posts_count < SiteSetting.summary_posts_required || @topic.private_message?
    return
  end

  posts_by_user = @user.posts.where(topic_id: @topic.id).count

  ratio = (posts_by_user.to_f / @topic.posts_count.to_f)
  return if ratio < (SiteSetting.dominating_topic_minimum_percent.to_f / 100.0)

  # Log the topic notification
  UserHistory.create!(
    action: UserHistory.actions[:notified_about_dominating_topic],
    target_user_id: @user.id,
    topic_id: @details[:topic_id],
  )

  {
    id: "dominating_topic",
    templateName: "dominating-topic",
    wait_for_typing: true,
    extraClass: "education-message dominating-topic-message",
    body: PrettyText.cook(I18n.t("education.dominating_topic")),
  }
end

#check_dont_feed_the_trollsObject



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/composer_messages_finder.rb', line 229

def check_dont_feed_the_trolls
  return if !replying?

  post =
    if @details[:post_id]
      Post.find_by(id: @details[:post_id])
    else
      @topic&.first_post
    end

  return if post.blank?

  flags = post.flags.active.group(:user_id).count
  flagged_by_replier = flags[@user.id].to_i > 0
  flagged_by_others = flags.values.sum >= SiteSetting.dont_feed_the_trolls_threshold

  return if !flagged_by_replier && !flagged_by_others

  {
    id: "dont_feed_the_trolls",
    templateName: "education",
    wait_for_typing: false,
    extraClass: "urgent",
    body: PrettyText.cook(I18n.t("education.dont_feed_the_trolls")),
  }
end

#check_education_messageObject

Determines whether to show the user education text



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/composer_messages_finder.rb', line 26

def check_education_message
  return if @topic&.private_message?

  if creating_topic?
    count = @user.created_topic_count
    education_key = "education.new-topic"
  else
    count = @user.post_count
    education_key = "education.new-reply"
  end

  if count < SiteSetting.educate_until_posts
    return(
      {
        id: "education",
        templateName: "education",
        wait_for_typing: true,
        body:
          PrettyText.cook(
            I18n.t(
              education_key,
              education_posts_text:
                I18n.t("education.until_posts", count: SiteSetting.educate_until_posts),
              site_name: SiteSetting.title,
              base_path: Discourse.base_path,
            ),
          ),
      }
    )
  end

  nil
end

#check_get_a_room(min_users_posted: 5) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/composer_messages_finder.rb', line 183

def check_get_a_room(min_users_posted: 5)
  return unless educate_reply?(:notified_about_get_a_room)
  return unless @details[:post_id].present?
  return if @topic.category&.read_restricted

  reply_to_user_id = Post.where(id: @details[:post_id]).pluck(:user_id)[0]

  # Users's last x posts in the topic
  last_x_replies =
    @topic
      .posts
      .where(user_id: @user.id)
      .order("created_at desc")
      .limit(SiteSetting.get_a_room_threshold)
      .pluck(:reply_to_user_id)
      .find_all { |uid| uid != @user.id && uid == reply_to_user_id }

  return if last_x_replies.size != SiteSetting.get_a_room_threshold
  return if @topic.posts.count("distinct user_id") < min_users_posted

  UserHistory.create!(
    action: UserHistory.actions[:notified_about_get_a_room],
    target_user_id: @user.id,
    topic_id: @details[:topic_id],
  )

  reply_username = User.where(id: last_x_replies[0]).pick(:username)

  {
    id: "get_a_room",
    templateName: "get-a-room",
    wait_for_typing: true,
    reply_username: reply_username,
    extraClass: "education-message get-a-room",
    body:
      PrettyText.cook(
        I18n.t(
          "education.get_a_room",
          count: SiteSetting.get_a_room_threshold,
          reply_username: reply_username,
          base_path: Discourse.base_path,
        ),
      ),
  }
end

#check_new_user_many_repliesObject

New users have a limited number of replies in a topic



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/composer_messages_finder.rb', line 61

def check_new_user_many_replies
  return unless replying? && @user.posted_too_much_in_topic?(@details[:topic_id])

  {
    id: "too_many_replies",
    templateName: "education",
    body:
      PrettyText.cook(
        I18n.t(
          "education.too_many_replies",
          newuser_max_replies_per_topic: SiteSetting.newuser_max_replies_per_topic,
        ),
      ),
  }
end

#check_reviving_old_topicObject



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/composer_messages_finder.rb', line 256

def check_reviving_old_topic
  return unless replying?
  if @topic.nil? || SiteSetting.warn_reviving_old_topic_age < 1 || @topic.last_posted_at.nil? ||
       @topic.last_posted_at > SiteSetting.warn_reviving_old_topic_age.days.ago
    return
  end

  {
    id: "reviving_old",
    templateName: "education",
    wait_for_typing: false,
    extraClass: "education-message",
    body:
      PrettyText.cook(
        I18n.t(
          "education.reviving_old_topic",
          time_ago:
            AgeWords.time_ago_in_words(
              @topic.last_posted_at,
              false,
              scope: :"datetime.distance_in_words_verbose",
            ),
        ),
      ),
  }
end

#check_sequential_repliesObject

Is a user replying too much in succession?



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/composer_messages_finder.rb', line 118

def check_sequential_replies
  return unless educate_reply?(:notified_about_sequential_replies)

  # Count the posts made by this user in the last day
  recent_posts_user_ids =
    Post
      .where(topic_id: @details[:topic_id])
      .where("created_at > ?", 1.day.ago)
      .where(post_type: Post.types[:regular])
      .order("created_at desc")
      .limit(SiteSetting.sequential_replies_threshold)
      .pluck(:user_id)

  # Did we get back as many posts as we asked for, and are they all by the current user?
  if recent_posts_user_ids.size != SiteSetting.sequential_replies_threshold ||
       recent_posts_user_ids.detect { |u| u != @user.id }
    return
  end

  # If we got this far, log that we've nagged them about the sequential replies
  UserHistory.create!(
    action: UserHistory.actions[:notified_about_sequential_replies],
    target_user_id: @user.id,
    topic_id: @details[:topic_id],
  )

  {
    id: "sequential_replies",
    templateName: "education",
    wait_for_typing: true,
    extraClass: "education-message",
    hide_if_whisper: true,
    body: PrettyText.cook(I18n.t("education.sequential_replies")),
  }
end

#findObject



14
15
16
17
18
19
20
21
22
23
# File 'lib/composer_messages_finder.rb', line 14

def find
  return if editing_post?

  self.class.check_methods.each do |m|
    msg = public_send(m)
    return msg if msg.present?
  end

  nil
end