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
implementation/64103 Extend API V3 to cover Emoji reactions on work package comments (read) and extend documentation
Kabiru Mwenja4447fcae2fc7
diff --git a/app/models/concerns/reactable.rb b/app/models/concerns/reactable.rbindex 7b3bd85e2b7..5f2392dbcf1 100644--- a/app/models/concerns/reactable.rb+++ b/app/models/concerns/reactable.rb@@ -57,7 +57,7 @@ module Reactable.select(emoji_reactions_group_selection_sql).joins(:user).where(reactable_id:, reactable_type:)- .group("emoji_reactions.reactable_id, emoji_reactions.reaction")+ .group("emoji_reactions.reactable_type, emoji_reactions.reactable_id, emoji_reactions.reaction").order("first_created_at ASC")end@@ -65,7 +65,7 @@ module Reactabledef emoji_reactions_group_selection_sql<<~SQL.squish- emoji_reactions.reactable_id, emoji_reactions.reaction,+ emoji_reactions.reactable_id, emoji_reactions.reactable_type, emoji_reactions.reaction,COUNT(emoji_reactions.id) as reactions_count,json_agg(json_build_array(users.id, #{user_name_concat_format_sql})diff --git a/lib/api/v3/activities/activities_api.rb b/lib/api/v3/activities/activities_api.rbindex 54b802a23ad..305f98ca9c1 100644--- a/lib/api/v3/activities/activities_api.rb+++ b/lib/api/v3/activities/activities_api.rb@@ -58,6 +58,7 @@ module API.mountmount ::API::V3::Attachments::AttachmentsByActivityCommentAPI+ mount ::API::V3::EmojiReactions::EmojiReactionsByActivityCommentAPIendendenddiff --git a/lib/api/v3/activities/activity_representer.rb b/lib/api/v3/activities/activity_representer.rbindex f20037bcc29..f3669241413 100644--- a/lib/api/v3/activities/activity_representer.rb+++ b/lib/api/v3/activities/activity_representer.rb@@ -82,6 +82,12 @@ module APIif: ->(*) { embed_links },uncacheable: true+ property :emoji_reactions,+ embedded: true,+ exec_context: :decorator,+ if: ->(*) { embed_links },+ uncacheable: true+date_time_property :created_atdate_time_property :updated_at@@ -102,6 +108,16 @@ module APIembed_links: false)end+ def emoji_reactions+ return unless represented.journable.is_a?(WorkPackage)++ emoji_reactions = Journal.grouped_emoji_reactions(reactable_id: represented.id, reactable_type: "Journal")+ API::V3::EmojiReactions::EmojiReactionCollectionRepresenter+ .new(emoji_reactions,+ self_link: api_v3_paths.emoji_reactions_by_activity_comment(represented.id),+ current_user:)+ end+privatedef current_user_allowed_to_edit?diff --git a/lib/api/v3/emoji_reactions/emoji_reaction_collection_representer.rb b/lib/api/v3/emoji_reactions/emoji_reaction_collection_representer.rbnew file mode 100644index 00000000000..954dd50a284--- /dev/null+++ b/lib/api/v3/emoji_reactions/emoji_reaction_collection_representer.rb@@ -0,0 +1,46 @@+# frozen_string_literal: true++#-- 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 API+ module V3+ module EmojiReactions+ class EmojiReactionCollectionRepresenter < ::API::Decorators::UnpaginatedCollection+ def model_count(models)+ # Since models is a grouped ActiveRecord relation, we count resulting collection+ models.to_a.size+ end++ property :count,+ getter: ->(*) { model_count(represented) },+ exec_context: :decorator+ end+ end+ end+enddiff --git a/lib/api/v3/emoji_reactions/emoji_reaction_representer.rb b/lib/api/v3/emoji_reactions/emoji_reaction_representer.rbnew file mode 100644index 00000000000..044fb3540c5--- /dev/null+++ b/lib/api/v3/emoji_reactions/emoji_reaction_representer.rb@@ -0,0 +1,118 @@+# frozen_string_literal: true++#-- 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 API+ module V3+ module EmojiReactions+ class EmojiReactionRepresenter < ::API::Decorators::Single+ include API::Decorators::DateProperty+ include API::Decorators::LinkedResource+ include API::Caching::CachedRepresenter++ def self.associated_reactable_getter+ ->(*) {+ next unless embed_links && reactable_representer++ reactable_representer+ .create(represented.reactable, current_user:)+ }+ end++ def self.associated_reactable_link+ ->(*) {+ return nil unless v3_reactable_name == "nil_class" || api_v3_paths.respond_to?(v3_reactable_name)++ ::API::Decorators::LinkObject+ .new(represented,+ path: v3_reactable_name,+ property_name: :reactable)+ .to_hash+ }+ end++ def initialize(model, current_user:, embed_links: false)+ raise "no represented object passed" if model_required? && model.nil?++ # EmojiReaction#updated_at is set to the first created_at timestamp of the reaction.+ #+ # This is done to ensure that the updated_at timestamp is always present for caching purposes.+ model.updated_at = model.first_created_at++ super+ end++ cached_representer key_parts: %i[reactable]++ links :reactingUsers,+ uncacheable: true do+ represented.reacting_users.map do |(id, name)|+ {+ href: api_v3_paths.user(id),+ title: name+ }+ end+ end++ property :id,+ exec_context: :decorator,+ getter: ->(*) { "#{represented.reactable_id}-#{represented.reaction}" }+ property :reaction+ property :emoji,+ exec_context: :decorator,+ getter: ->(*) { ::EmojiReaction.emoji(represented.reaction) }+ property :reactions_count++ date_time_property :first_created_at, as: :firstReactionAt++ associated_resource :reactable,+ getter: associated_reactable_getter,+ link: associated_reactable_link++ def _type+ "EmojiReaction"+ end++ def reactable_representer+ name = v3_reactable_name.camelcase++ "::API::V3::#{name.pluralize}::#{name}Representer".constantize+ rescue NameError+ nil+ end++ def v3_reactable_name+ ar_name = represented.reactable_type.underscore++ ::API::Utilities::PropertyNameConverter.from_ar_name_with_aliases(ar_name, "journal" => "activity").underscore+ end+ end+ end+ end+enddiff --git a/lib/api/v3/emoji_reactions/emoji_reactions_by_activity_comment_api.rb b/lib/api/v3/emoji_reactions/emoji_reactions_by_activity_comment_api.rbnew file mode 100644index 00000000000..3ebb4a1f2d8--- /dev/null+++ b/lib/api/v3/emoji_reactions/emoji_reactions_by_activity_comment_api.rb@@ -0,0 +1,56 @@+# frozen_string_literal: true++#-- 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 API+ module V3+ module EmojiReactions+ class EmojiReactionsByActivityCommentAPI < ::API::OpenProjectAPI+ resource :emoji_reactions do+ helpers do+ def reactable+ @activity+ end++ def get_emoji_reactions_self_path+ api_v3_paths.emoji_reactions_by_activity_comment(reactable.id)+ end+ end++ get do+ emoji_reactions = Journal.grouped_emoji_reactions(reactable_id: reactable.id, reactable_type: "Journal")+ EmojiReactionCollectionRepresenter.new(emoji_reactions,+ self_link: get_emoji_reactions_self_path,+ current_user: User.current)+ end+ end+ end+ end+ end+enddiff --git a/lib/api/v3/emoji_reactions/emoji_reactions_by_work_package_comments_api.rb b/lib/api/v3/emoji_reactions/emoji_reactions_by_work_package_comments_api.rbnew file mode 100644index 00000000000..179af78babf--- /dev/null+++ b/lib/api/v3/emoji_reactions/emoji_reactions_by_work_package_comments_api.rb@@ -0,0 +1,59 @@+# frozen_string_literal: true++#-- 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 API+ module V3+ module EmojiReactions+ class EmojiReactionsByWorkPackageCommentsAPI < ::API::OpenProjectAPI+ resource :activities_emoji_reactions do+ helpers do+ def reactable+ @work_package+ end++ def get_emoji_reactions_self_path+ api_v3_paths.emoji_reactions_by_work_package_comments(reactable.id)+ end+ end++ get do+ emoji_reactions = Journal.grouped_emoji_reactions(+ reactable_id: reactable.journals.internal_visible.select(:id).pluck(:id),+ reactable_type: "Journal"+ )+ EmojiReactionCollectionRepresenter.new(emoji_reactions,+ self_link: get_emoji_reactions_self_path,+ current_user: User.current)+ end+ end+ end+ end+ end+enddiff --git a/lib/api/v3/utilities/path_helper.rb b/lib/api/v3/utilities/path_helper.rbindex 719dee43a80..8060602941e 100644--- a/lib/api/v3/utilities/path_helper.rb+++ b/lib/api/v3/utilities/path_helper.rb@@ -162,6 +162,14 @@ module API"#{root}/attachments/#{attachment_id}/uploaded"end+ def self.emoji_reactions_by_activity_comment(id)+ "#{root}/activities/#{id}/emoji_reactions"+ end++ def self.emoji_reactions_by_work_package_comments(work_package_id)+ "#{work_package(work_package_id)}/activities_emoji_reactions"+ end+def self.available_assignees_in_project(project_id)"#{project(project_id)}/available_assignees"end@@ -572,6 +580,10 @@ module API"#{root}/work_packages/schemas/#{project_id}-#{type_id}"end+ def self.work_package_activities_emoji_reactions(id)+ "#{work_package_activities(id)}/emoji_reactions"+ end+def self.work_package_activities(id)"#{work_package(id)}/activities"enddiff --git a/lib/api/v3/work_packages/work_packages_api.rb b/lib/api/v3/work_packages/work_packages_api.rbindex b13b19cdb56..11797ad7754 100644--- a/lib/api/v3/work_packages/work_packages_api.rb+++ b/lib/api/v3/work_packages/work_packages_api.rb@@ -99,6 +99,7 @@ module APImount ::API::V3::WorkPackages::AvailableRelationCandidatesAPImount ::API::V3::WorkPackages::WorkPackageRelationsAPImount ::API::V3::Reminders::RemindersAPI+ mount ::API::V3::EmojiReactions::EmojiReactionsByWorkPackageCommentsAPIendmount ::API::V3::WorkPackages::CreateFormAPI