Bring back old notifications

SCI-4140
This commit is contained in:
Urban Rotnik 2021-02-25 07:43:04 +01:00
parent a93174f827
commit 347f208480
7 changed files with 151 additions and 28 deletions

View file

@ -5,7 +5,7 @@ module GlobalActivitiesHelper
include ActionView::Helpers::UrlHelper
include InputSanitizeHelper
def generate_activity_content(activity, no_links = false)
def generate_activity_content(activity, no_links = false, no_smart_annotation = false)
parameters = {}
activity.message_items.each do |key, value|
parameters[key] =
@ -17,10 +17,15 @@ module GlobalActivitiesHelper
no_links ? generate_name(value) : generate_link(value, activity)
end
end
custom_auto_link(
I18n.t("global_activities.content.#{activity.type_of}_html", parameters.symbolize_keys),
team: activity.team
)
if no_smart_annotation
I18n.t("global_activities.content.#{activity.type_of}_html", parameters.symbolize_keys)
else
custom_auto_link(
I18n.t("global_activities.content.#{activity.type_of}_html", parameters.symbolize_keys),
team: activity.team
)
end
rescue StandardError => e
Rails.logger.error(e.message)
Rails.logger.error(e.backtrace.join("\n"))

View file

@ -0,0 +1,9 @@
# frozen_string_literal: true
class CreateNotificationFromActivityJob < ApplicationJob
queue_as :high_priority
def perform(activity)
activity.generate_notification_from_activity
end
end

View file

@ -2,6 +2,7 @@
class Activity < ApplicationRecord
include ActivityValuesModel
include GenerateNotificationModel
enum type_of: Extends::ACTIVITY_TYPES

View file

@ -0,0 +1,110 @@
# frozen_string_literal: true
module GenerateNotificationModel
extend ActiveSupport::Concern
include GlobalActivitiesHelper
included do
after_create :generate_notification
end
def generate_notification_from_activity
return if notification_recipients.none?
message = generate_activity_content(self, true, true)
description = generate_notification_description_elements(subject).reverse.join(' | ')
notification = Notification.create(
type_of: :recent_changes,
title: sanitize_input(message, %w(strong a)),
message: sanitize_input(description, %w(strong a)),
generator_user_id: owner.id
)
notification_recipients.each do |user|
notification.create_user_notification(user)
end
end
protected
def notification_recipients
users = []
case subject
when Project
users = subject.users
when Experiment
users = subject.project.users
when MyModule
users = subject.users
when Protocol
users = subject.in_repository? ? [] : subject.my_module.users
when Result
users = subject.my_module.users
when Repository
users = subject.team.users
when Team
users = subject.users
when Report
users = subject.team.users
when ProjectFolder
users = subject.team.users
end
users - [owner]
end
def generate_notification_description_elements(object, elements = [])
case object
when Project
path = Rails.application.routes.url_helpers.project_path(object)
elements << "Project: <a href='#{path}'>#{object.name}</a>"
when Experiment
path = Rails.application.routes.url_helpers.canvas_experiment_path(object)
elements << "Experiment: <a href='#{path}'>#{object.name}</a>"
generate_notification_description_elements(object.project, elements)
when MyModule
path = if object.archived?
Rails.application.routes.url_helpers.module_archive_experiment_path(object.experiment)
else
Rails.application.routes.url_helpers.protocols_my_module_path(object)
end
elements << "Task: <a href='#{path}'>#{object.name}</a>"
generate_notification_description_elements(object.experiment, elements)
when Protocol
if object.in_repository?
path = Rails.application.routes.url_helpers.protocols_path(team: object.team.id)
elements << "Protocol: <a href='#{path}'>#{object.name}</a>"
generate_notification_description_elements(object.team, elements)
else
generate_notification_description_elements(object.my_module, elements)
end
when Result
generate_notification_description_elements(object.my_module, elements)
when Repository
path = Rails.application.routes.url_helpers.repository_path(object, team: object.team.id)
elements << "Repository: <a href='#{path}'>#{object.name}</a>"
generate_notification_description_elements(object.team, elements)
when Team
path = Rails.application.routes.url_helpers.projects_path(team: object.id)
elements << "Team: <a href='#{path}'>#{object.name}</a>"
when Report
path = Rails.application.routes.url_helpers.reports_path(team: object.team.id)
elements << "Report: <a href='#{path}'>#{object.name}</a>"
generate_notification_description_elements(object.team, elements)
when ProjectFolder
generate_notification_description_elements(object.team, elements)
end
elements
end
def generate_notification
CreateNotificationFromActivityJob.perform_later(self) if notifiable?
end
def notifiable?
# send notifications for all activity types for now
true
end
end

View file

@ -10,4 +10,10 @@ class Notification < ApplicationRecord
.pluck(:checked)
.first
end
def create_user_notification(user)
return if user == generator_user
user_notifications.create!(user: user) if user.enabled_notifications_for?(type_of.to_sym, :web)
end
end

View file

@ -556,6 +556,19 @@ class User < ApplicationRecord
end
end
def enabled_notifications_for?(notification_type, channel)
return true if notification_type == :deliver
case channel
when :web
notification_type == :recent_changes && recent_notification ||
notification_type == :assignment && assignments_notification
when :email
notification_type == :recent_changes && recent_email_notification ||
notification_type == :assignment && assignments_email_notification
end
end
def increase_daily_exports_counter!
range = Time.now.utc.beginning_of_day.to_i..Time.now.utc.end_of_day.to_i
last_export = export_vars[:last_export_timestamp] || 0

View file

@ -6,7 +6,7 @@ class UserNotification < ApplicationRecord
belongs_to :user, optional: true
belongs_to :notification, optional: true
after_save :send_email
after_create :send_email
def self.last_notifications(
user,
@ -37,27 +37,6 @@ class UserNotification < ApplicationRecord
end
def send_email
case notification.type_of
when 'system_message'
send_email_notification(
user,
notification
) if user.system_message_email_notification
when 'assignment'
send_email_notification(
user,
notification
) if user.assignments_email_notification
when 'recent_changes'
send_email_notification(
user,
notification
) if user.recent_email_notification
when 'deliver'
send_email_notification(
user,
notification
)
end
send_email_notification(user, notification) if user.enabled_notifications_for?(notification.type_of.to_sym, :email)
end
end