# name: custom-digest-posts # about: Removes score threshold for popular posts in Discourse digest emails # version: 0.1 # authors: David Szekeres # url: https://git.stonecarterstudios.com/StoneCarterStudios/custom-digest-posts after_initialize do require_dependency 'user_notifications' UserNotifications.class_eval do 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) .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 end end