Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEV: Add notification_types table #8085

Closed
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

DEV: Add notification_types table

This table stores the ids of the notfication types so that notification
types can be added by plugins.
  • Loading branch information...
danielwaterworth committed Sep 11, 2019
commit f9db0d371989c903c44ac76a9f3796e06d9adb02
@@ -1,6 +1,5 @@
# frozen_string_literal: true

require_dependency 'enum'
require_dependency 'notification_emailer'

class Notification < ActiveRecord::Base
@@ -48,29 +47,7 @@ def self.ensure_consistency!
end

def self.types
@types ||= Enum.new(mentioned: 1,
replied: 2,
quoted: 3,
edited: 4,
liked: 5,
private_message: 6,
invited_to_private_message: 7,
invitee_accepted: 8,
posted: 9,
moved_post: 10,
linked: 11,
granted_badge: 12,
invited_to_topic: 13,
custom: 14,
group_mentioned: 15,
group_message_summary: 16,
watching_first_post: 17,
topic_reminder: 18,
liked_consolidated: 19,
post_approved: 20,
code_review_commit_approved: 21,
membership_request_accepted: 22
)
NotificationType.enum
end

def self.mark_posts_read(user, topic_id, post_numbers)
@@ -0,0 +1,67 @@
# frozen_string_literal: true

require_dependency 'enum'

class NotificationType < ActiveRecord::Base
# Types added before the notification_types table was added
@old_types = {
mentioned: 1,
replied: 2,
quoted: 3,
edited: 4,
liked: 5,
private_message: 6,
invited_to_private_message: 7,
invitee_accepted: 8,
posted: 9,
moved_post: 10,
linked: 11,
granted_badge: 12,
invited_to_topic: 13,
custom: 14,
group_mentioned: 15,
group_message_summary: 16,
watching_first_post: 17,
topic_reminder: 18,
liked_consolidated: 19,
post_approved: 20,
code_review_commit_approved: 21,
membership_request_accepted: 22
}

@new_types = []

class << self
attr_reader :old_types
attr_reader :new_types
end

# For use by plugins
def self.add_notification_type(name)
@new_types << name.to_sym
end

def self.enum
@cache.getset(RailsMultisite::ConnectionManagement.current_db) do
Enum.new(Hash[pluck(:name, :id)].symbolize_keys)
end
end

def self.clear_cache!
@cache = LruRedux::ThreadSafeCache.new(1000)
end

clear_cache!
end

# == Schema Information
#
# Table name: notification_types
#
# id :bigint not null, primary key
# name :string
#
# Indexes
#
# index_notification_types_on_name (name) UNIQUE
#
@@ -4,6 +4,8 @@
require_dependency 'user_action_manager'

class PostAlerter
@mutex = Mutex.new

def self.post_created(post, opts = {})
PostAlerter.new(opts).after_save_post(post, true)
post
@@ -195,9 +197,22 @@ def destroy_notifications(user, types, topic)
end
end

NOTIFIABLE_TYPES = [:mentioned, :replied, :quoted, :posted, :linked, :private_message, :group_mentioned, :watching_first_post].map { |t|
Notification.types[t]
}
def self.notifiable_types
return @notifiable_types if @notifiable_types

@mutex.synchronize {
@notifiable_types ||= [
:mentioned,
:replied,
:quoted,
:posted,
:linked,
:private_message,
:group_mentioned,
:watching_first_post
].map { |t| Notification.types[t] }
}
end

def group_stats(topic)
sql = <<~SQL
@@ -269,11 +284,17 @@ def should_notify_previous?(user, notification, opts)
end
end

COLLAPSED_NOTIFICATION_TYPES ||= [
Notification.types[:replied],
Notification.types[:posted],
Notification.types[:private_message],
]
def self.collapsed_notification_types
return @collapsed_notification_types if @collapsed_notification_types

@mutex.synchronize {
@collapsed_notification_types ||= [
Notification.types[:replied],
Notification.types[:posted],
Notification.types[:private_message]
]
}
end

def create_notification(user, type, post, opts = {})
opts = @default_opts.merge(opts)
@@ -360,8 +381,8 @@ def create_notification(user, type, post, opts = {})

collapsed = false

if COLLAPSED_NOTIFICATION_TYPES.include?(type)
destroy_notifications(user, COLLAPSED_NOTIFICATION_TYPES, post.topic)
if self.class.collapsed_notification_types.include?(type)
destroy_notifications(user, self.class.collapsed_notification_types, post.topic)
collapsed = true
end

@@ -417,7 +438,7 @@ def create_notification(user, type, post, opts = {})
skip_send_email: skip_send_email
)

if created.id && existing_notifications.empty? && NOTIFIABLE_TYPES.include?(type) && !user.suspended?
if created.id && existing_notifications.empty? && self.class.notifiable_types.include?(type) && !user.suspended?
create_notification_alert(user: user, post: original_post, notification_type: type, username: original_username)
end

@@ -0,0 +1,14 @@
# frozen_string_literal: true

NotificationType.old_types.each do |name, id|
NotificationType.seed(:name) do |notification_type|
notification_type.name = name
notification_type.id = id
end
end

NotificationType.new_types.each do |name|
NotificationType.seed(:name) do |notification_type|
notification_type.name = name
end
end
@@ -0,0 +1,9 @@
# frozen_string_literal: true

class CreateNotificationType < ActiveRecord::Migration[5.2]
def change
create_table :notification_types do |t|
t.string :name, index: { unique: true }
end
end
end
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.