Browse Source

Maintenance: remove unused notifications table

Mantas Masalskis 2 years ago
parent
commit
bc1b789d39

+ 61 - 51
app/models/application_model/can_assets.rb

@@ -58,62 +58,69 @@ get assets and record_ids of selector
 =end
 
   def assets_of_selector(selector, assets = {})
+    send(selector)
+      .each_with_object(assets) do |(item, content), memo|
+        assets_of_single_selector(item, content, memo)
+      end
+  end
 
-    # get assets of condition
-    models = Models.all
-    send(selector).each do |item, content|
-      attribute = item.split('.')
-      next if !attribute[1]
+  def assets_added_to?(data)
+    data.dig(self.class.to_app_model, id).present?
+  end
 
-      if attribute[0] == 'customer' || attribute[0] == 'session'
-        attribute[0] = 'user'
-      end
+  private
 
-      begin
-        attribute_class = attribute[0].to_classname.constantize
-      rescue => e
-        next if attribute[0] == 'article'
-        next if attribute[0] == 'execution_time'
+  def assets_of_single_selector(item, content, assets = {})
+    area, key = item.split('.')
+    return if !key
 
-        logger.error "Unable to get asset for '#{attribute[0]}': #{e.inspect}"
-        next
-      end
+    area = 'user' if %w[customer session].include? area
 
-      if attribute_class == ::Notification
-        next if content['recipient'].blank?
+    attribute_ref_class, item_ids = if area == 'notification'
+                                      notifications_assets_data(content)
+                                    else
+                                      non_notifications_assets_data(area, key, content)
+                                    end
 
-        attribute_ref_class = ::User
-        item_ids            = []
-        Array(content['recipient']).each do |identifier|
-          next if identifier !~ %r{\Auserid_(\d+)\z}
+    return if !attribute_ref_class
 
-          item_ids.push($1)
-        end
-      else
-        reflection = attribute[1].sub(%r{_id$}, '')
-        next if !models[attribute_class]
-        next if !models[attribute_class][:reflections]
-        next if !models[attribute_class][:reflections][reflection]
-        next if !models[attribute_class][:reflections][reflection].klass
-
-        attribute_ref_class = models[attribute_class][:reflections][reflection].klass
-        item_ids            = Array(content['value'])
-      end
+    items = item_ids
+      .compact_blank
+      .filter_map { |elem| attribute_ref_class.lookup(id: elem) }
 
-      item_ids.each do |item_id|
-        next if item_id.blank?
+    ApplicationModel::CanAssets.reduce items, assets
+  end
+
+  def notifications_assets_data(content)
+    return if content['recipient'].blank?
 
-        attribute_object = attribute_ref_class.lookup(id: item_id)
-        next if !attribute_object
+    item_ids = Array(content['recipient'])
+      .filter_map do |elem|
+        match = elem.match %r{\Auserid_(?<id>\d+)\z}
 
-        assets = attribute_object.assets(assets)
+        match[:id] if match
       end
-    end
-    assets
+
+    [::User, item_ids]
   end
 
-  def assets_added_to?(data)
-    data.dig(self.class.to_app_model, id).present?
+  def non_notifications_assets_data(area, key, content)
+    return if %w[article execution_time].include? area
+
+    begin
+      attribute_class = area.to_classname.constantize
+    rescue => e
+      logger.error "Unable to get asset for '#{area}': #{e.inspect}"
+      return
+    end
+
+    reflection = key.delete_suffix '_id'
+
+    klass = Models.all.dig(attribute_class, :reflections, reflection)&.klass
+
+    return if !klass
+
+    [klass, Array(content['value'])]
   end
 
   # methods defined here are going to extend the class, not the instance of it
@@ -178,6 +185,8 @@ get assets of object list
     end
   end
 
+  class << self
+
 =begin
 
 Compiles an assets hash for given items
@@ -193,15 +202,16 @@ Compiles an assets hash for given items
 
 =end
 
-  def self.reduce(items, data = {}, suffix = nil)
-    items.reduce(data) do |memo, elem|
-      method_name = if suffix.present? && elem.respond_to?("assets_#{suffix}")
-                      "assets_#{suffix}"
-                    else
-                      :assets
-                    end
+    def reduce(items, data = {}, suffix = nil)
+      items.reduce(data) do |memo, elem|
+        method_name = if suffix.present? && elem.respond_to?("assets_#{suffix}")
+                        "assets_#{suffix}"
+                      else
+                        :assets
+                      end
 
-      elem.send method_name, memo
+        elem.send method_name, memo
+      end
     end
   end
 end

+ 0 - 7
app/models/notification.rb

@@ -1,7 +0,0 @@
-# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
-
-class Notification < ApplicationModel
-  include ChecksHtmlSanitized
-
-  sanitized_html :note
-end

+ 0 - 10
db/migrate/20120101000010_create_ticket.rb

@@ -328,15 +328,6 @@ class CreateTicket < ActiveRecord::Migration[4.2]
     add_foreign_key :jobs, :users, column: :created_by_id
     add_foreign_key :jobs, :users, column: :updated_by_id
 
-    create_table :notifications do |t|
-      t.column :subject,      :string, limit: 250,   null: false
-      t.column :body,         :string, limit: 8000,  null: false
-      t.column :content_type, :string, limit: 250,   null: false
-      t.column :active,       :boolean,              null: false, default: true
-      t.column :note,         :string, limit: 250,   null: true
-      t.timestamps limit: 3, null: false
-    end
-
     create_table :link_types do |t|
       t.column :name,         :string, limit: 250,   null: false
       t.column :note,         :string, limit: 250,   null: true
@@ -625,7 +616,6 @@ class CreateTicket < ActiveRecord::Migration[4.2]
     drop_table :text_modules_groups
     drop_table :text_modules
     drop_table :postmaster_filters
-    drop_table :notifications
     drop_table :triggers
     drop_table :links
     drop_table :link_types

+ 10 - 0
db/migrate/20220607113229_drop_notifications_table.rb

@@ -0,0 +1,10 @@
+# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
+
+class DropNotificationsTable < ActiveRecord::Migration[6.1]
+  def up
+    # return if it's a new setup
+    return if !Setting.exists?(name: 'system_init_done')
+
+    drop_table :notifications
+  end
+end