OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
prototypical implementation of the toggle singel notifications feature, specs and refactoring required
Jonas Jabari66aa22978ba5
diff --git a/app/components/work_packages/activities_tab/journals/item_component.html.erb b/app/components/work_packages/activities_tab/journals/item_component.html.erbindex c1c0f60d7db..8707ca13da8 100644--- a/app/components/work_packages/activities_tab/journals/item_component.html.erb+++ b/app/components/work_packages/activities_tab/journals/item_component.html.erb@@ -32,11 +32,38 @@header_container.with_column(flex_layout: true, align_items: :center) do |header_end_container|if has_unread_notifications?header_end_container.with_column(mr: 2, pt: 1) do- render(Primer::Beta::Octicon.new(- :"dot-fill", # color is set via CSS as requested by UI/UX Team+ render(Primer::Beta::IconButton.new(+ scheme: :invisible, # color is set via CSS as requested by UI/UX Teamclasses: "work-packages-activities-tab-journals-item-component--notification-dot-icon",- size: :medium,- data: { test_selector: "op-journal-unread-notification" }+ icon: "dot-fill",+ "aria-label": "mark notification as read",+ show_tooltip: true,+ href: toggle_notification_read_status_work_package_activity_path(work_package_id: journal.journable_id, id: journal.id ),+ tag: :a,+ data: {+ test_selector: "op-journal-unread-notification",+ turbo: true,+ turbo_stream: true,+ turbo_method: :put+ }+ ))+ end+ end+ if has_read_notifications?+ header_end_container.with_column(mr: 2, pt: 1) do+ render(Primer::Beta::IconButton.new(+ scheme: :invisible, # color is set via CSS as requested by UI/UX Team+ icon: "dot-fill",+ "aria-label": "mark notification as unread",+ show_tooltip: true,+ href: toggle_notification_read_status_work_package_activity_path(work_package_id: journal.journable_id, id: journal.id ),+ tag: :a,+ data: {+ test_selector: "op-journal-read-notification",+ turbo: true,+ turbo_stream: true,+ turbo_method: :put+ }))endend@@ -87,7 +114,13 @@endendjournal_container.with_row do- render(WorkPackages::ActivitiesTab::Journals::ItemComponent::Details.new(journal:, has_unread_notifications: notification_on_details?, filter:))+ render(WorkPackages::ActivitiesTab::Journals::ItemComponent::Details.new(+ journal:,+ show_notifications: show_notification_on_details?,+ has_unread_notifications: has_unread_notifications?,+ has_read_notifications: has_read_notifications?,+ filter:+ ))endendenddiff --git a/app/components/work_packages/activities_tab/journals/item_component.rb b/app/components/work_packages/activities_tab/journals/item_component.rbindex e79ecc2c766..e9366749cf5 100644--- a/app/components/work_packages/activities_tab/journals/item_component.rb+++ b/app/components/work_packages/activities_tab/journals/item_component.rb@@ -85,8 +85,12 @@ module WorkPackagesjournal.notifications.where(read_ian: false, recipient_id: User.current.id).any?end- def notification_on_details?- has_unread_notifications? && journal.notes.blank?+ def has_read_notifications?+ journal.notifications.where(read_ian: true, recipient_id: User.current.id).any?+ end++ def show_notification_on_details?+ journal.notes.blank?enddef allowed_to_edit?diff --git a/app/components/work_packages/activities_tab/journals/item_component.sass b/app/components/work_packages/activities_tab/journals/item_component.sassindex 31c09142304..f1568cdd5a4 100644--- a/app/components/work_packages/activities_tab/journals/item_component.sass+++ b/app/components/work_packages/activities_tab/journals/item_component.sass@@ -8,7 +8,8 @@@media screen and (min-width: $breakpoint-sm)max-width: 40%&--notification-dot-icon- color: var(--bgColor-accent-emphasis)+ svg+ color: var(--bgColor-accent-emphasis)!important&--header-start-containerflex-grow: 1diff --git a/app/components/work_packages/activities_tab/journals/item_component/details.rb b/app/components/work_packages/activities_tab/journals/item_component/details.rbindex 4fd23af862b..a9d7206475b 100644--- a/app/components/work_packages/activities_tab/journals/item_component/details.rb+++ b/app/components/work_packages/activities_tab/journals/item_component/details.rb@@ -37,17 +37,20 @@ module WorkPackagesinclude OpPrimer::ComponentHelpersinclude OpTurbo::Streamable- def initialize(journal:, filter:, has_unread_notifications: false)+ def initialize(journal:, filter:, show_notifications: false, has_unread_notifications: false,+ has_read_notifications: false)super@journal = journal+ @show_notifications = show_notifications@has_unread_notifications = has_unread_notifications+ @has_read_notifications = has_read_notifications@filter = filterendprivate- attr_reader :journal, :has_unread_notifications, :filter+ attr_reader :journal, :show_notifications, :has_unread_notifications, :has_read_notifications, :filterdef wrapper_uniq_byjournal.id@@ -144,19 +147,52 @@ module WorkPackagesdef render_header_end(header_container)header_container.with_column(flex_layout: true) do |header_end_container|- render_notification_bubble(header_end_container) if has_unread_notifications+ render_notification_bubble(header_end_container)render_activity_link(header_end_container)endenddef render_notification_bubble(container)- container.with_column(mr: 2) do- render(Primer::Beta::Octicon.new(- :"dot-fill", # color is set via CSS as requested by UI/UX Team- classes: "work-packages-activities-tab-journals-item-component-details--notification-dot-icon",- size: :medium,- data: { test_selector: "op-journal-unread-notification" }- ))+ return unless show_notifications++ if has_unread_notifications+ container.with_column(mr: 2) do+ render(Primer::Beta::IconButton.new(+ scheme: :invisible, # color is set via CSS as requested by UI/UX Team+ classes: "work-packages-activities-tab-journals-item-component--notification-dot-icon",+ icon: "dot-fill",+ "aria-label": "mark notification as read",+ show_tooltip: true,+ href: toggle_notification_read_status_work_package_activity_path(work_package_id: journal.journable_id,+ id: journal.id),+ tag: :a,+ data: {+ test_selector: "op-journal-unread-notification",+ turbo: true,+ turbo_stream: true,+ turbo_method: :put+ }+ ))+ end+ end+ if has_read_notifications+ container.with_column(mr: 2) do+ render(Primer::Beta::IconButton.new(+ scheme: :invisible, # color is set via CSS as requested by UI/UX Team+ icon: "dot-fill",+ "aria-label": "mark notification as unread",+ show_tooltip: true,+ href: toggle_notification_read_status_work_package_activity_path(work_package_id: journal.journable_id,+ id: journal.id),+ tag: :a,+ data: {+ test_selector: "op-journal-read-notification",+ turbo: true,+ turbo_stream: true,+ turbo_method: :put+ }+ ))+ endendenddiff --git a/app/contracts/notifications/update_contract.rb b/app/contracts/notifications/update_contract.rbnew file mode 100644index 00000000000..921b3cfee48--- /dev/null+++ b/app/contracts/notifications/update_contract.rb@@ -0,0 +1,33 @@+#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++module Notifications+ class UpdateContract < ::ModelContract+ attribute :read_ian+ end+enddiff --git a/app/controllers/work_packages/activities_tab_controller.rb b/app/controllers/work_packages/activities_tab_controller.rbindex 6aec4724d5f..11087e726b3 100644--- a/app/controllers/work_packages/activities_tab_controller.rb+++ b/app/controllers/work_packages/activities_tab_controller.rb@@ -33,7 +33,7 @@ class WorkPackages::ActivitiesTabController < ApplicationControllerbefore_action :find_work_packagebefore_action :find_project- before_action :find_journal, only: %i[edit cancel_edit update]+ before_action :find_journal, only: %i[edit cancel_edit update toggle_notification_read_status]before_action :set_filterbefore_action :authorize@@ -153,6 +153,23 @@ class WorkPackages::ActivitiesTabController < ApplicationControllerrespond_with_turbo_streamsend+ def toggle_notification_read_status+ notification = Notification.find_by(recipient: User.current, journal: @journal)++ call = Notifications::UpdateService.new(model: notification, user: User.current).call(+ read_ian: !notification.read_ian+ )++ if call.success?+ update_item_component(@journal.reload, state: :show)+ update_activity_counter+ else+ handle_failed_update_call(call)+ end++ respond_with_turbo_streams+ end+privatedef respond_with_error(error_message)@@ -293,27 +310,17 @@ class WorkPackages::ActivitiesTabController < ApplicationControllerenddef generate_time_based_update_streams(last_update_timestamp)- # TODO: prototypical implementationjournals = @work_package.journalsif @filter == :only_commentsjournals = journals.where.not(notes: "")end- journals.where("updated_at > ?", last_update_timestamp).find_each do |journal|- update_via_turbo_stream(- # we need to update the whole component as the show part is not rendered for journals which originally have no notes- component: WorkPackages::ActivitiesTab::Journals::ItemComponent.new(- journal:,- filter: @filter- )- )- # TODO: is it possible to loose an edit state this way?- end+ rerender_updated_journals(journals, last_update_timestamp)- journals.where("created_at > ?", last_update_timestamp).find_each do |journal|- append_or_prepend_latest_journal_via_turbo_stream(journal)- end+ rerender_journals_with_updated_notification(journals, last_update_timestamp)++ append_or_prepend_new_journals(journals, last_update_timestamp)if journals.any?remove_potential_empty_state@@ -321,6 +328,28 @@ class WorkPackages::ActivitiesTabController < ApplicationControllerendend+ def rerender_updated_journals(journals, last_update_timestamp)+ journals.where("updated_at > ?", last_update_timestamp).find_each do |journal|+ # we need to update the whole component as the show part is not rendered for journals which originally have no notes+ update_journal_item(journal:)+ end+ end++ def rerender_journals_with_updated_notification(journals, last_update_timestamp)+ journals+ .joins(:notifications)+ .where("notifications.updated_at > ?", last_update_timestamp)+ .find_each do |journal|+ update_journal_item(journal:)+ end+ end++ def append_or_prepend_new_journals(journals, last_update_timestamp)+ journals.where("created_at > ?", last_update_timestamp).find_each do |journal|+ append_or_prepend_latest_journal_via_turbo_stream(journal)+ end+ end+def append_or_prepend_latest_journal_via_turbo_stream(journal)target_component = WorkPackages::ActivitiesTab::Journals::IndexComponent.new(work_package: @work_package,@@ -357,6 +386,16 @@ class WorkPackages::ActivitiesTabController < ApplicationController)end+ def update_journal_item(journal:, filter: @filter, state: :show)+ update_via_turbo_stream(+ component: WorkPackages::ActivitiesTab::Journals::ItemComponent.new(+ journal:,+ filter:,+ state:+ )+ )+ end+def allowed_to_edit?(journal)journal.editable_by?(User.current)enddiff --git a/config/initializers/permissions.rb b/config/initializers/permissions.rbindex 4860440a10d..8afa0bc5e18 100644--- a/config/initializers/permissions.rb+++ b/config/initializers/permissions.rb@@ -263,7 +263,7 @@ Rails.application.reloader.to_prepare dowpt.permission :edit_work_package_notes,{- "work_packages/activities_tab": %i[edit cancel_edit update]+ "work_packages/activities_tab": %i[edit cancel_edit update toggle_notification_read_status]},permissible_on: :project,require: :loggedin,@@ -271,7 +271,7 @@ Rails.application.reloader.to_prepare dowpt.permission :edit_own_work_package_notes,{- "work_packages/activities_tab": %i[edit cancel_edit update]+ "work_packages/activities_tab": %i[edit cancel_edit update toggle_notification_read_status]},permissible_on: %i[work_package project],require: :loggedin,diff --git a/config/routes.rb b/config/routes.rbindex dff7f9c1449..aeba8365da0 100644--- a/config/routes.rb+++ b/config/routes.rb@@ -577,9 +577,11 @@ Rails.application.routes.draw do# states managed by client-side routing on work_package#indexget "details/*state" => "work_packages#index", on: :collection, as: :details- resources :activities, controller: "work_packages/activities_tab", only: %i[index create edit update] do+ resources :activities, controller: "work_packages/activities_tab",+ only: %i[index create edit update] domember doget :cancel_edit+ put :toggle_notification_read_statusendcollection doget :update_streams