Skip to content

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

implementation/64104 Extend API V3 to cover Emoji reactions on work package comments (toggle) and extend documentation

Kabiru Mwenja07951c52729f
diff --git a/app/controllers/work_packages/activities_tab_controller.rb b/app/controllers/work_packages/activities_tab_controller.rb
index fb16ba99821..061397ee554 100644
--- a/app/controllers/work_packages/activities_tab_controller.rb
+++ b/app/controllers/work_packages/activities_tab_controller.rb
@@ -149,17 +149,10 @@ class WorkPackages::ActivitiesTabController < ApplicationController
end
def toggle_reaction # rubocop:disable Metrics/AbcSize
- emoji_reaction_service =
- if @journal.emoji_reactions.exists?(user: User.current, reaction: params[:reaction])
- EmojiReactions::DeleteService
- .new(user: User.current,
- model: @journal.emoji_reactions.find_by(user: User.current, reaction: params[:reaction]))
- .call
- else
- EmojiReactions::CreateService
- .new(user: User.current)
- .call(user: User.current, reactable: @journal, reaction: params[:reaction])
- end
+ emoji_reaction_service = EmojiReactions::ToggleEmojiReactionService
+ .call(user: User.current,
+ reactable: @journal,
+ reaction: params[:reaction])
emoji_reaction_service.on_success do
update_via_turbo_stream(
diff --git a/app/services/emoji_reactions/toggle_emoji_reaction_service.rb b/app/services/emoji_reactions/toggle_emoji_reaction_service.rb
new file mode 100644
index 00000000000..8178807d915
--- /dev/null
+++ b/app/services/emoji_reactions/toggle_emoji_reaction_service.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 EmojiReactions
+ module ToggleEmojiReactionService
+ def self.call(user:, reactable:, reaction:)
+ if reactable.emoji_reactions.exists?(user:, reaction:)
+ EmojiReactions::DeleteService
+ .new(user:,
+ model: reactable.emoji_reactions.find_by(user:, reaction:))
+ .call
+ else
+ EmojiReactions::CreateService
+ .new(user:)
+ .call(user:, reactable:, reaction:)
+ end
+ end
+ end
+end
diff --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.rb
index 3ebb4a1f2d8..6eef7addaa6 100644
--- 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.rb
@@ -49,6 +49,28 @@ module API
self_link: get_emoji_reactions_self_path,
current_user: User.current)
end
+
+ params do
+ requires :reaction, type: String, desc: "The emoji reaction to add/remove",
+ values: ::EmojiReaction::EMOJI_MAP.keys.map(&:to_s)
+ end
+
+ patch do
+ toggle_service = ::EmojiReactions::ToggleEmojiReactionService.call(
+ user: current_user,
+ reactable: reactable,
+ reaction: params[:reaction]
+ )
+
+ if toggle_service.success?
+ 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:)
+ else
+ fail ::API::Errors::ErrorBase.create_and_merge_errors(toggle_service.errors)
+ end
+ end
end
end
end
diff --git a/spec/requests/api/v3/emoji_reactions/emoji_reactions_by_activity_comment_api_spec.rb b/spec/requests/api/v3/emoji_reactions/emoji_reactions_by_activity_comment_api_spec.rb
index 162b34c77cd..c8041565c28 100644
--- a/spec/requests/api/v3/emoji_reactions/emoji_reactions_by_activity_comment_api_spec.rb
+++ b/spec/requests/api/v3/emoji_reactions/emoji_reactions_by_activity_comment_api_spec.rb
@@ -34,8 +34,8 @@ require "rack/test"
RSpec.describe API::V3::EmojiReactions::EmojiReactionsByActivityCommentAPI do
include API::V3::Utilities::PathHelper
- let(:project) { work_package.project }
- let(:work_package) { create(:work_package) }
+ let(:project) { create(:project, enabled_internal_comments: true) }
+ let(:work_package) { create(:work_package, project:) }
let(:current_user) do
create(:user, member_with_roles: { project => role })
end
@@ -104,11 +104,6 @@ RSpec.describe API::V3::EmojiReactions::EmojiReactionsByActivityCommentAPI do
end
let!(:internal_emoji_reaction) { create(:emoji_reaction, reactable: internal_comment, user: current_user) }
- before do
- project.enabled_internal_comments = true
- project.save!
- end
-
context "and user has permission to view internal comments" do
before do
get api_v3_paths.emoji_reactions_by_activity_comment(internal_comment.id)
@@ -147,4 +142,126 @@ RSpec.describe API::V3::EmojiReactions::EmojiReactionsByActivityCommentAPI do
end
end
end
+
+ describe "PATCH /api/v3/activities/:id/emoji_reactions" do
+ let(:path) { api_v3_paths.emoji_reactions_by_activity_comment(activity.id) }
+ let(:headers) { { "CONTENT_TYPE" => "application/json" } }
+ let(:reaction) { "heart" }
+
+ def make_request
+ patch path, { reaction: }.to_json, headers
+ end
+
+ def destroy_all_reactions
+ EmojiReaction.destroy_all
+ end
+
+ shared_examples "a successful reaction" do
+ before do
+ destroy_all_reactions
+ make_request
+ end
+
+ it "creates the reaction" do
+ expect(last_response).to have_http_status :ok
+
+ expect(last_response.body)
+ .to be_json_eql("#{activity.id}-#{reaction}".to_json)
+ .at_path("_embedded/elements/0/id")
+
+ expect(last_response.body)
+ .to be_json_eql(EmojiReaction.emoji(reaction).to_json)
+ .at_path("_embedded/elements/0/emoji")
+
+ expect(last_response.body)
+ .to be_json_eql([{ "href" => "/api/v3/users/#{current_user.id}", "title" => current_user.name }].to_json)
+ .at_path("_embedded/elements/0/_links/reactingUsers")
+ end
+ end
+
+ context "when user has permission to add work package comments" do
+ let(:permissions) { %i(view_work_packages add_work_package_comments) }
+
+ context "when adding a new reaction" do
+ let(:reaction) { "rocket" }
+
+ it_behaves_like "a successful reaction"
+ end
+
+ context "when removing an existing reaction" do
+ let(:reaction) { emoji_reaction.reaction }
+
+ it "succeeds" do
+ make_request
+
+ expect(last_response).to have_http_status :ok
+
+ expect(last_response.body)
+ .to be_json_eql([].to_json)
+ .at_path("_embedded/elements")
+ end
+ end
+
+ context "with an invalid reaction" do
+ let(:reaction) { "invalid_reaction" }
+
+ it "fails with HTTP Bad Request" do
+ make_request
+
+ expect(last_response).to have_http_status :bad_request
+
+ expect(last_response.body)
+ .to include_json("Bad request: reaction does not have a valid value".to_json)
+ .at_path("message")
+ end
+ end
+ end
+
+ context "when user does not have permission to add work package notes" do
+ let(:permissions) { %i(view_work_packages) }
+
+ it "fails with HTTP Not Found" do
+ make_request
+
+ expect(last_response).to have_http_status :forbidden
+
+ expect(last_response.body)
+ .to be_json_eql("You are not authorized to access this resource.".to_json)
+ .at_path("message")
+ end
+ end
+
+ context "when the activity is internal" do
+ let(:internal_comment) do
+ work_package.add_journal(user: current_user, notes: "Internal comment", internal: true)
+ work_package.save(validate: false)
+ work_package.journals.last
+ end
+ let(:path) { api_v3_paths.emoji_reactions_by_activity_comment(internal_comment.id) }
+ let(:reaction) { "thumbs_up" }
+
+ before do
+ project.enabled_internal_comments = true
+ project.save!
+ end
+
+ context "and user has permission to create internal comments" do
+ let(:reaction) { "rocket" }
+ let(:permissions) { %i(view_work_packages add_work_package_comments view_internal_comments add_internal_comments) }
+
+ it_behaves_like "a successful reaction" do
+ let(:activity) { internal_comment }
+ end
+ end
+
+ context "and user does not have permission to create internal comments" do
+ let(:permissions) { %i(view_work_packages view_internal_comments) }
+
+ it "fails with HTTP Forbidden" do
+ make_request
+ expect(last_response).to have_http_status :forbidden
+ end
+ end
+ end
+ end
end
diff --git a/spec/services/emoji_reactions/toggle_emoji_reaction_service_spec.rb b/spec/services/emoji_reactions/toggle_emoji_reaction_service_spec.rb
new file mode 100644
index 00000000000..376b05bdecb
--- /dev/null
+++ b/spec/services/emoji_reactions/toggle_emoji_reaction_service_spec.rb
@@ -0,0 +1,68 @@
+# 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.
+#++
+
+require "spec_helper"
+
+RSpec.describe EmojiReactions::ToggleEmojiReactionService do
+ shared_let(:user) { create(:user, admin: true) }
+ shared_let(:work_package) { create(:work_package) }
+ shared_let(:reactable) do
+ create(:work_package_journal, user: user, notes: "A Note", journable: work_package, version: 2)
+ end
+
+ let(:reaction) { "thumbs_up" }
+
+ before do
+ allow(EmojiReactions::CreateService).to receive(:new).and_call_original
+ allow(EmojiReactions::DeleteService).to receive(:new).and_call_original
+ end
+
+ describe ".call" do
+ it "toggles the reaction" do
+ aggregate_failures "creates if not exists" do
+ expect do
+ described_class.call(user:, reactable:, reaction:)
+ end.to change(EmojiReaction, :count).by(1)
+
+ expect(EmojiReactions::CreateService).to have_received(:new).with(user:)
+ end
+
+ emoji_reaction = EmojiReaction.last
+
+ aggregate_failures "deletes if exists" do
+ expect do
+ described_class.call(user:, reactable:, reaction:)
+ end.to change(EmojiReaction, :count).by(-1)
+
+ expect(EmojiReactions::DeleteService).to have_received(:new).with(user:, model: emoji_reaction)
+ end
+ end
+ end
+end