2
0

Fix syntax.

This commit is contained in:
2025-06-30 20:14:32 +00:00
parent e62290e54f
commit 88116b22d3

212
plugin.rb
View File

@@ -9,127 +9,135 @@ after_initialize do
UserNotifications.class_eval do UserNotifications.class_eval do
def digest(user, opts = {}) def digest(user, opts = {})
build_summary_for(user) build_summary_for(user)
@unsubscribe_key = UnsubscribeKey.create_key_for(@user, UnsubscribeKey::DIGEST_TYPE) @unsubscribe_key = UnsubscribeKey.create_key_for(@user, UnsubscribeKey::DIGEST_TYPE)
@since = opts[:since].presence @since = opts[:since].presence
@since ||= [user.last_seen_at, user.user_stat&.digest_attempted_at, 1.month.ago].compact.max @since ||= [user.last_seen_at, user.user_stat&.digest_attempted_at, 1.month.ago].compact.max
# Fetch some topics and posts to show # Fetch some topics and posts to show
digest_opts = { digest_opts = {
limit: SiteSetting.digest_topics + SiteSetting.digest_other_topics, limit: SiteSetting.digest_topics + SiteSetting.digest_other_topics,
top_order: true, top_order: true,
} }
topics_for_digest = Topic.for_digest(user, @since, digest_opts) topics_for_digest = Topic.for_digest(user, @since, digest_opts)
if topics_for_digest.empty? && !user.user_option.try(:include_tl0_in_digests) 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 # Find some topics from new users that are at least 24 hours old
topics_for_digest = topics_for_digest =
Topic.for_digest(user, @since, digest_opts.merge(include_tl0: true)).where( Topic.for_digest(user, @since, digest_opts.merge(include_tl0: true)).where(
"topics.created_at < ?", "topics.created_at < ?",
24.hours.ago, 24.hours.ago,
) )
end end
@popular_topics = topics_for_digest[0, SiteSetting.digest_topics] @popular_topics = topics_for_digest[0, SiteSetting.digest_topics]
if @popular_topics.present? if @popular_topics.present?
@other_new_for_you = @other_new_for_you =
( (
if topics_for_digest.size > SiteSetting.digest_topics if topics_for_digest.size > SiteSetting.digest_topics
topics_for_digest[SiteSetting.digest_topics..-1] topics_for_digest[SiteSetting.digest_topics..-1]
else
[]
end
)
# Modified @popular_posts to remove score threshold
@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 > ?", 1)
.where("posts.created_at < ?", (SiteSetting.editing_grace_period || 0).seconds.ago)
.limit(SiteSetting.digest_posts)
else else
[] []
end end
)
@excerpts = {} @popular_posts =
if SiteSetting.digest_posts > 0
@popular_topics.each do |t| Post
next if t.first_post.blank? .order("posts.score DESC")
@excerpts[t.first_post.id] = email_excerpt(t.first_post.cooked, t.first_post) .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 end
# Try to find 3 interesting stats for the top of the digest @excerpts = {}
new_topics_count = Topic.for_digest(user, @since).count
new_topics_count = topics_for_digest.size if new_topics_count == 0
@counts = [ @popular_topics.each do |t|
{ next if t.first_post.blank?
id: "new_topics", @excerpts[t.first_post.id] = email_excerpt(t.first_post.cooked, t.first_post)
label_key: "user_notifications.digest.new_topics", end
value: new_topics_count,
href: "#{Discourse.base_url}/new",
},
]
value = user.unread_notifications + user.unread_high_priority_notifications # 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 if value > 0
@counts << { @counts << {
id: "unread_notifications", id: "likes_received",
label_key: "user_notifications.digest.unread_notifications", label_key: "user_notifications.digest.liked_received",
value: value, value: value,
href: "#{Discourse.base_url}/my/notifications", href: "#{Discourse.base_url}/my/notifications",
} }
end 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
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
@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
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
end end
end end