Class: UserNotifications

Inherits:
ActionMailer::Base
  • Object
show all
Includes:
ApplicationHelper, Email::BuildEmailHelper, UserNotificationsHelper
Defined in:
app/mailers/user_notifications.rb

Instance Method Summary collapse

Methods included from Email::BuildEmailHelper

#build_email

Methods included from ApplicationHelper

#add_resource_preload_list, #admin?, #age_words, all_connectors, #allow_plugins?, #allow_third_party_plugins?, #anonymous_top_menu_items, #application_logo_dark_url, #application_logo_url, #authentication_data, #body_classes, #build_plugin_html, #can_sign_up?, #category_badge, #client_side_setup_data, #crawlable_meta_data, #crawler_layout?, #csp_nonce_placeholder, #current_homepage, #customization_disabled?, #dark_color_scheme?, #dark_scheme_id, #discourse_color_scheme_meta_tag, #discourse_color_scheme_stylesheets, #discourse_config_environment, #discourse_csrf_tags, #discourse_preload_color_scheme_stylesheets, #discourse_stylesheet_link_tag, #discourse_stylesheet_preload_tag, #discourse_theme_color_meta_tags, #escape_noscript, #escape_unicode, extra_body_classes, #format_topic_title, #ga_universal_json, #get_absolute_image_url, #google_tag_manager_json, #google_universal_analytics_json, #gsub_emoji_to_unicode, #guardian, #html_classes, #html_lang, #include_crawler_content?, #include_ios_native_app_banner?, #include_splash_screen?, #ios_app_argument, #ios_device?, #is_brotli_req?, #is_gzip_req?, #loading_admin?, #login_path, #manifest_url, #mobile_device?, #mobile_view?, #moderator?, #modern_mobile_device?, #normalized_safe_mode, #preload_script, #preload_script_url, #preloaded_json, #render_sitelinks_search_tag, #replace_plugin_html, #rss_creator, #rtl?, #scheme_id, #script_asset_path, #server_plugin_outlet, #shared_session_key, #short_date, #staff?, #stylesheet_manager, #text_size_class, #theme_id, #theme_js_lookup, #theme_lookup, #theme_translations_lookup, #topic_featured_link_domain, #waving_hand_url, #with_format

Methods included from GlobalPath

#cdn_path, #cdn_relative_path, #full_cdn_url, #path, #upload_cdn_path

Methods included from ConfigurableUrls

#faq_path, #privacy_policy_url, #tos_url

Methods included from CanonicalURL::Helpers

#canonical_link_tag

Methods included from CurrentUser

#clear_current_user, #current_user, has_auth_cookie?, #is_api?, #is_user_api?, #log_off_user, #log_on_user, lookup_from_env, #refresh_session

Methods included from UserNotificationsHelper

#correct_top_margin, #digest_custom, #digest_custom_html, #digest_custom_text, #email_excerpt, #email_image_url, #first_paragraphs_from, #format_for_email, #html_site_link, #indent, #logo_url, #normalize_name, #show_image_with_url, #show_name_on_post, #show_username_on_post, #url_for_email

Instance Method Details

#account_created(user, opts = {}) ⇒ Object



149
150
151
152
153
154
155
# File 'app/mailers/user_notifications.rb', line 149

def (user, opts = {})
  build_user_email_token_by_template(
    "user_notifications.account_created",
    user,
    opts[:email_token],
  )
end

#account_exists(user, opts = {}) ⇒ Object



205
206
207
208
209
210
211
212
# File 'app/mailers/user_notifications.rb', line 205

def (user, opts = {})
  build_email(
    user.email,
    template: "user_notifications.account_exists",
    locale: user_locale(user),
    email: user.email,
  )
end

#account_second_factor_disabled(user, opts = {}) ⇒ Object



214
215
216
217
218
219
220
221
# File 'app/mailers/user_notifications.rb', line 214

def (user, opts = {})
  build_email(
    user.email,
    template: "user_notifications.account_second_factor_disabled",
    locale: user_locale(user),
    email: user.email,
  )
end

#account_silenced(user, opts = nil) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'app/mailers/user_notifications.rb', line 157

def (user, opts = nil)
  opts ||= {}

  return unless user_history = opts[:user_history]

  if user.silenced_forever?
    build_email(
      user.email,
      template: "user_notifications.account_silenced_forever",
      locale: user_locale(user),
      reason: user_history.details,
    )
  else
    silenced_till = user.silenced_till.in_time_zone(user.user_option.timezone.presence || "UTC")
    build_email(
      user.email,
      template: "user_notifications.account_silenced",
      locale: user_locale(user),
      reason: user_history.details,
      silenced_till: I18n.l(silenced_till, format: :long),
    )
  end
end

#account_suspended(user, opts = nil) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'app/mailers/user_notifications.rb', line 181

def (user, opts = nil)
  opts ||= {}

  return unless user_history = opts[:user_history]

  if user.suspended_forever?
    build_email(
      user.email,
      template: "user_notifications.account_suspended_forever",
      locale: user_locale(user),
      reason: user_history.details,
    )
  else
    suspended_till = user.suspended_till.in_time_zone(user.user_option.timezone.presence || "UTC")
    build_email(
      user.email,
      template: "user_notifications.account_suspended",
      locale: user_locale(user),
      reason: user_history.details,
      suspended_till: I18n.l(suspended_till, format: :long),
    )
  end
end

#activation_reminder(user, opts = {}) ⇒ Object



16
17
18
19
20
21
22
# File 'app/mailers/user_notifications.rb', line 16

def activation_reminder(user, opts = {})
  build_user_email_token_by_template(
    "user_notifications.activation_reminder",
    user,
    opts[:email_token],
  )
end

#admin_login(user, opts = {}) ⇒ Object



145
146
147
# File 'app/mailers/user_notifications.rb', line 145

def (user, opts = {})
  build_user_email_token_by_template("user_notifications.admin_login", user, opts[:email_token])
end

#confirm_new_email(user, opts = {}) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'app/mailers/user_notifications.rb', line 119

def confirm_new_email(user, opts = {})
  build_user_email_token_by_template(
    (
      if opts[:requested_by_admin]
        "user_notifications.confirm_new_email_via_admin"
      else
        "user_notifications.confirm_new_email"
      end
    ),
    user,
    opts[:email_token],
  )
end

#confirm_old_email(user, opts = {}) ⇒ Object



103
104
105
106
107
108
109
# File 'app/mailers/user_notifications.rb', line 103

def confirm_old_email(user, opts = {})
  build_user_email_token_by_template(
    "user_notifications.confirm_old_email",
    user,
    opts[:email_token],
  )
end

#confirm_old_email_add(user, opts = {}) ⇒ Object



111
112
113
114
115
116
117
# File 'app/mailers/user_notifications.rb', line 111

def confirm_old_email_add(user, opts = {})
  build_user_email_token_by_template(
    "user_notifications.confirm_old_email_add",
    user,
    opts[:email_token],
  )
end

#digest(user, opts = {}) ⇒ Object



223
224
225
226
227
228
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
255
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'app/mailers/user_notifications.rb', line 223

def digest(user, opts = {})
  build_summary_for(user)
  @unsubscribe_key = UnsubscribeKey.create_key_for(@user, UnsubscribeKey::DIGEST_TYPE)

  @since = opts[:since].presence
  @since ||= [user.last_seen_at, user.user_stat&.digest_attempted_at, 1.month.ago].compact.max

  # Fetch some topics and posts to show
  digest_opts = {
    limit: SiteSetting.digest_topics + SiteSetting.digest_other_topics,
    top_order: true,
  }
  topics_for_digest = Topic.for_digest(user, @since, digest_opts)
  if topics_for_digest.empty? && !user.user_option.try(:include_tl0_in_digests)
    # Find some topics from new users that are at least 24 hours old
    topics_for_digest =
      Topic.for_digest(user, @since, digest_opts.merge(include_tl0: true)).where(
        "topics.created_at < ?",
        24.hours.ago,
      )
  end

  @popular_topics = topics_for_digest[0, SiteSetting.digest_topics]

  if @popular_topics.present?
    @other_new_for_you =
      (
        if topics_for_digest.size > SiteSetting.digest_topics
          topics_for_digest[SiteSetting.digest_topics..-1]
        else
          []
        end
      )

    @popular_posts =
      if SiteSetting.digest_posts > 0
        Post
          .order("posts.score DESC")
          .for_mailing_list(user, @since)
          .where("posts.post_type = ?", Post.types[:regular])
          .where(
            "posts.deleted_at IS NULL AND posts.hidden = false AND posts.user_deleted = false",
          )
          .where(
            "posts.post_number > ? AND posts.score > ?",
            1,
            ScoreCalculator.default_score_weights[:like_score] * 5.0,
          )
          .where("posts.created_at < ?", (SiteSetting.editing_grace_period || 0).seconds.ago)
          .limit(SiteSetting.digest_posts)
      else
        []
      end

    @excerpts = {}

    @popular_topics.each do |t|
      next if t.first_post.blank?
      @excerpts[t.first_post.id] = email_excerpt(t.first_post.cooked, t.first_post)
    end

    # Try to find 3 interesting stats for the top of the digest
    new_topics_count = Topic.for_digest(user, @since).count
    # We used topics from new users instead, so count should match
    new_topics_count = topics_for_digest.size if new_topics_count == 0

    @counts = [
      {
        id: "new_topics",
        label_key: "user_notifications.digest.new_topics",
        value: new_topics_count,
        href: "#{Discourse.base_url}/new",
      },
    ]

    # totalling unread notifications (which are low-priority only) and unread
    # PMs and bookmark reminder notifications, so the total is both unread low
    # and high priority PMs
    value = user.unread_notifications + user.unread_high_priority_notifications
    if value > 0
      @counts << {
        id: "unread_notifications",
        label_key: "user_notifications.digest.unread_notifications",
        value: value,
        href: "#{Discourse.base_url}/my/notifications",
      }
    end

    if @counts.size < 3
      value = user.unread_notifications_of_type(Notification.types[:liked], since: @since)
      if value > 0
        @counts << {
          id: "likes_received",
          label_key: "user_notifications.digest.liked_received",
          value: value,
          href: "#{Discourse.base_url}/my/notifications",
        }
      end
    end

    if @counts.size < 3 && user.user_option.digest_after_minutes.to_i >= 1440
      value = summary_new_users_count(@since)
      if value > 0
        @counts << {
          id: "new_users",
          label_key: "user_notifications.digest.new_users",
          value: value,
          href: "#{Discourse.base_url}/about",
        }
      end
    end

    @preheader_text = I18n.t("user_notifications.digest.preheader", since: @since)

    opts = {
      from_alias: I18n.t("user_notifications.digest.from", site_name: Email.site_title),
      subject:
        I18n.t(
          "user_notifications.digest.subject_template",
          email_prefix: @email_prefix,
          date: short_date(Time.now),
        ),
      add_unsubscribe_link: true,
      unsubscribe_url: "#{Discourse.base_url}/email/unsubscribe/#{@unsubscribe_key}",
      topic_ids: topics_for_digest.pluck(:id),
      post_ids:
        topics_for_digest.joins(:posts).where(posts: { post_number: 1 }).pluck("posts.id"),
    }

    build_email(user.email, opts)
  end
end

#email_login(user, opts = {}) ⇒ Object



141
142
143
# File 'app/mailers/user_notifications.rb', line 141

def (user, opts = {})
  build_user_email_token_by_template("user_notifications.email_login", user, opts[:email_token])
end

#forgot_password(user, opts = {}) ⇒ Object



133
134
135
136
137
138
139
# File 'app/mailers/user_notifications.rb', line 133

def forgot_password(user, opts = {})
  build_user_email_token_by_template(
    user.has_password? ? "user_notifications.forgot_password" : "user_notifications.set_password",
    user,
    opts[:email_token],
  )
end

#group_mentioned(user, opts) ⇒ Object



388
389
390
391
392
393
394
# File 'app/mailers/user_notifications.rb', line 388

def group_mentioned(user, opts)
  opts[:allow_reply_by_email] = true
  opts[:use_site_subject] = true
  opts[:show_category_in_subject] = true
  opts[:show_tags_in_subject] = true
  notification_email(user, opts)
end

#mailing_list_notify(user, post) ⇒ Object



437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
# File 'app/mailers/user_notifications.rb', line 437

def mailing_list_notify(user, post)
  opts = {
    post: post,
    allow_reply_by_email: true,
    use_site_subject: true,
    add_re_to_subject: true,
    show_category_in_subject: true,
    show_tags_in_subject: true,
    notification_type: "posted",
    notification_data_hash: {
      original_username: post.user.username,
      topic_title: post.topic.title,
    },
  }
  notification_email(user, opts)
end

#notify_old_email(user, opts = {}) ⇒ Object



85
86
87
88
89
90
91
92
# File 'app/mailers/user_notifications.rb', line 85

def notify_old_email(user, opts = {})
  build_email(
    user.email,
    template: "user_notifications.notify_old_email",
    locale: user_locale(user),
    new_email: opts[:new_email],
  )
end

#notify_old_email_add(user, opts = {}) ⇒ Object



94
95
96
97
98
99
100
101
# File 'app/mailers/user_notifications.rb', line 94

def notify_old_email_add(user, opts = {})
  build_email(
    user.email,
    template: "user_notifications.notify_old_email_add",
    locale: user_locale(user),
    new_email: opts[:new_email],
  )
end

#post_approved(user, opts = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/mailers/user_notifications.rb', line 41

def post_approved(user, opts = {})
  post_url = opts.dig(:notification_data_hash, :post_url)

  return if post_url.nil?

  locale = user_locale(user)
  build_email(
    user.email,
    template: "user_notifications.post_approved",
    locale: locale,
    base_url: Discourse.base_url,
    post_url: post_url,
  )
end

#signup(user, opts = {}) ⇒ Object



12
13
14
# File 'app/mailers/user_notifications.rb', line 12

def (user, opts = {})
  build_user_email_token_by_template("user_notifications.signup", user, opts[:email_token])
end

#signup_after_approval(user, opts = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/mailers/user_notifications.rb', line 24

def (user, opts = {})
  locale = user_locale(user)
  tips =
    I18n.t(
      "system_messages.usage_tips.text_body_template",
      base_url: Discourse.base_url,
      locale: locale,
    )

  build_email(
    user.email,
    template: "user_notifications.signup_after_approval",
    locale: locale,
    new_user_tips: tips,
  )
end

#signup_after_reject(user, opts = {}) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'app/mailers/user_notifications.rb', line 56

def (user, opts = {})
  locale = user_locale(user)
  build_email(
    user.email,
    template: "user_notifications.signup_after_reject",
    locale: locale,
    reject_reason: opts[:reject_reason],
  )
end

#suspicious_login(user, opts = {}) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/mailers/user_notifications.rb', line 66

def (user, opts = {})
  ipinfo = DiscourseIpInfo.get(opts[:client_ip])
  location = ipinfo[:location]
  browser = BrowserDetection.browser(opts[:user_agent])
  device = BrowserDetection.device(opts[:user_agent])
  os = BrowserDetection.os(opts[:user_agent])

  build_email(
    user.email,
    template: "user_notifications.suspicious_login",
    locale: user_locale(user),
    client_ip: opts[:client_ip],
    location: location.present? ? location : I18n.t("staff_action_logs.unknown"),
    browser: I18n.t("user_auth_tokens.browser.#{browser}"),
    device: I18n.t("user_auth_tokens.device.#{device}"),
    os: I18n.t("user_auth_tokens.os.#{os}"),
  )
end

#user_invited_to_private_message(user, opts) ⇒ Object



419
420
421
422
423
# File 'app/mailers/user_notifications.rb', line 419

def user_invited_to_private_message(user, opts)
  opts[:allow_reply_by_email] = false
  opts[:use_invite_template] = true
  notification_email(user, opts)
end

#user_invited_to_topic(user, opts) ⇒ Object



425
426
427
428
429
430
431
# File 'app/mailers/user_notifications.rb', line 425

def user_invited_to_topic(user, opts)
  opts[:allow_reply_by_email] = false
  opts[:use_invite_template] = true
  opts[:show_category_in_subject] = true
  opts[:show_tags_in_subject] = true
  notification_email(user, opts)
end

#user_linked(user, opts) ⇒ Object



372
373
374
375
376
377
378
# File 'app/mailers/user_notifications.rb', line 372

def user_linked(user, opts)
  opts[:allow_reply_by_email] = true
  opts[:use_site_subject] = true
  opts[:show_category_in_subject] = true
  opts[:show_tags_in_subject] = true
  notification_email(user, opts)
end

#user_mentioned(user, opts) ⇒ Object



380
381
382
383
384
385
386
# File 'app/mailers/user_notifications.rb', line 380

def user_mentioned(user, opts)
  opts[:allow_reply_by_email] = true
  opts[:use_site_subject] = true
  opts[:show_category_in_subject] = true
  opts[:show_tags_in_subject] = true
  notification_email(user, opts)
end

#user_posted(user, opts) ⇒ Object



396
397
398
399
400
401
402
403
# File 'app/mailers/user_notifications.rb', line 396

def user_posted(user, opts)
  opts[:allow_reply_by_email] = true
  opts[:use_site_subject] = true
  opts[:add_re_to_subject] = true
  opts[:show_category_in_subject] = true
  opts[:show_tags_in_subject] = true
  notification_email(user, opts)
end

#user_private_message(user, opts) ⇒ Object



405
406
407
408
409
410
411
412
413
414
415
416
417
# File 'app/mailers/user_notifications.rb', line 405

def user_private_message(user, opts)
  opts[:allow_reply_by_email] = true
  opts[:use_site_subject] = true
  opts[:add_re_to_subject] = true
  opts[:show_category_in_subject] = false
  opts[:show_tags_in_subject] = false
  opts[:show_group_in_subject] = true if SiteSetting.group_in_subject

  # We use the 'user_posted' event when you are emailed a post in a PM.
  opts[:notification_type] = "posted"

  notification_email(user, opts)
end

#user_quoted(user, opts) ⇒ Object



364
365
366
367
368
369
370
# File 'app/mailers/user_notifications.rb', line 364

def user_quoted(user, opts)
  opts[:allow_reply_by_email] = true
  opts[:use_site_subject] = true
  opts[:show_category_in_subject] = true
  opts[:show_tags_in_subject] = true
  notification_email(user, opts)
end

#user_replied(user, opts) ⇒ Object



356
357
358
359
360
361
362
# File 'app/mailers/user_notifications.rb', line 356

def user_replied(user, opts)
  opts[:allow_reply_by_email] = true
  opts[:use_site_subject] = true
  opts[:show_category_in_subject] = true
  opts[:show_tags_in_subject] = true
  notification_email(user, opts)
end

#user_watching_first_post(user, opts) ⇒ Object



433
434
435
# File 'app/mailers/user_notifications.rb', line 433

def user_watching_first_post(user, opts)
  user_posted(user, opts)
end