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

Favorite projects API

Oliver Güntherdd35273d82a1
diff --git a/docs/api/apiv3/openapi-spec.yml b/docs/api/apiv3/openapi-spec.yml
index 51cfaefaf56..014050c9434 100644
--- a/docs/api/apiv3/openapi-spec.yml
+++ b/docs/api/apiv3/openapi-spec.yml
@@ -350,6 +350,8 @@ paths:
"$ref": "./paths/project_types.yml"
"/api/v3/projects/{id}/versions":
"$ref": "./paths/project_versions.yml"
+ "/api/v3/projects/{id}/favorite":
+ "$ref": "./paths/project_favorite.yml"
"/api/v3/queries":
"$ref": "./paths/queries.yml"
"/api/v3/queries/available_projects":
diff --git a/docs/api/apiv3/paths/project_favorite.yml b/docs/api/apiv3/paths/project_favorite.yml
new file mode 100644
index 00000000000..209d1f0ba13
--- /dev/null
+++ b/docs/api/apiv3/paths/project_favorite.yml
@@ -0,0 +1,104 @@
+# /api/v3/projects/{id}/favorite
+---
+delete:
+ parameters:
+ - description: Project id
+ example: '1'
+ in: path
+ name: id
+ required: true
+ schema:
+ type: integer
+ responses:
+ '204':
+ description: |-
+ Returned if the project was successfully removed from favorites.
+ '404':
+ content:
+ application/hal+json:
+ schema:
+ $ref: "../components/schemas/error_response.yml"
+ examples:
+ response:
+ value:
+ _type: Error
+ errorIdentifier: urn:openproject-org:api:v3:errors:NotFound
+ message: The requested resource could not be found.
+ description: |-
+ Returned if the project does not exist or the client does not have sufficient permissions
+ to see it.
+
+ **Required permission:** view project
+ '403':
+ content:
+ application/hal+json:
+ schema:
+ $ref: '../components/schemas/error_response.yml'
+ examples:
+ response:
+ value:
+ _type: Error
+ errorIdentifier: urn:openproject-org:api:v3:errors:MissingPermission
+ message: You are not authorized to access this resource.
+ description: |-
+ Returned if the client does not have sufficient permissions.
+
+ **Required permission:** logged in
+ tags:
+ - Projects
+ - Favorites
+ description: |-
+ Removes the project from the current user's favorites.
+ operationId: Unfavorite_Project
+ summary: Unfavorite Project
+post:
+ parameters:
+ - description: Project id
+ example: '1'
+ in: path
+ name: id
+ required: true
+ schema:
+ type: integer
+ responses:
+ '204':
+ description: |-
+ Returned if the project was successfully added to favorites.
+ '404':
+ content:
+ application/hal+json:
+ schema:
+ $ref: "../components/schemas/error_response.yml"
+ examples:
+ response:
+ value:
+ _type: Error
+ errorIdentifier: urn:openproject-org:api:v3:errors:NotFound
+ message: The requested resource could not be found.
+ description: |-
+ Returned if the project does not exist or the client does not have sufficient permissions
+ to see it.
+
+ **Required permission:** view project
+ '403':
+ content:
+ application/hal+json:
+ schema:
+ $ref: '../components/schemas/error_response.yml'
+ examples:
+ response:
+ value:
+ _type: Error
+ errorIdentifier: urn:openproject-org:api:v3:errors:MissingPermission
+ message: You are not authorized to access this resource.
+ description: |-
+ Returned if the client does not have sufficient permissions.
+
+ **Required permission:** logged in
+ tags:
+ - Projects
+ - Favorites
+ description: |-
+ Adds the project to the current user's favorites.
+ operationId: Favorite_Project
+ summary: Favorite Project
diff --git a/lib/api/v3/favorites/favorite_actions_api.rb b/lib/api/v3/favorites/favorite_actions_api.rb
new file mode 100644
index 00000000000..e780d9a0bc3
--- /dev/null
+++ b/lib/api/v3/favorites/favorite_actions_api.rb
@@ -0,0 +1,64 @@
+# 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 Favorites
+ class FavoriteActionsAPI < ::API::OpenProjectAPI
+ namespace :favorite do
+ after_validation do
+ getter = configuration.fetch(:favorite_object_getter)
+ @favorite_object = instance_eval(&getter)
+
+ unless @favorite_object&.visible?(current_user)
+ raise API::Errors::NotFound
+ end
+
+ unless current_user.logged?
+ raise API::Errors::Unauthorized
+ end
+ end
+
+ desc "Mark as favorite"
+ post do
+ @favorite_object.set_favored(current_user, favored: true)
+ status 204
+ end
+
+ desc "Unmark as favorite"
+ delete do
+ @favorite_object.set_favored(current_user, favored: false)
+ status 204
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/lib/api/v3/projects/projects_api.rb b/lib/api/v3/projects/projects_api.rb
index cb1ab660c59..f30268b0946 100644
--- a/lib/api/v3/projects/projects_api.rb
+++ b/lib/api/v3/projects/projects_api.rb
@@ -79,6 +79,7 @@ module API
mount API::V3::Versions::VersionsByProjectAPI
mount API::V3::Types::TypesByProjectAPI
mount API::V3::Queries::QueriesByProjectAPI
+ mount API::V3::Favorites::FavoriteActionsAPI, with: { favorite_object_getter: ->(*) { @project } }
end
end
end
diff --git a/spec/requests/api/v3/projects/favorite_resource_spec.rb b/spec/requests/api/v3/projects/favorite_resource_spec.rb
new file mode 100644
index 00000000000..fa7013d11d4
--- /dev/null
+++ b/spec/requests/api/v3/projects/favorite_resource_spec.rb
@@ -0,0 +1,138 @@
+# 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"
+require "rack/test"
+
+RSpec.describe "API v3 Project favorite resource", content_type: :json do
+ include Rack::Test::Methods
+ include API::V3::Utilities::PathHelper
+
+ shared_let(:project) { create(:project) }
+ shared_let(:user) { create(:user, member_with_permissions: { project => %i[view_project] }) }
+ let(:favorite_path) { "/api/v3/projects/#{project.id}/favorite" }
+
+ before do
+ login_as(user)
+ end
+
+ describe "POST /api/v3/projects/:id/favorite" do
+ before do
+ post favorite_path
+ end
+
+ it "responds with 204 No Content and marks as favorite", :aggregate_failures do
+ expect(last_response).to have_http_status(204)
+ expect(project.favored_by?(user)).to be true
+ end
+
+ context "when project is already favorited" do
+ before do
+ project.set_favored(user, favored: true)
+ post favorite_path
+ end
+
+ it "responds with 204 No Content and keeps project as is", :aggregate_failures do
+ expect(last_response).to have_http_status(204)
+ expect(project.favored_by?(user)).to be true
+ end
+ end
+
+ context "when user lacks permissions" do
+ let(:other_project) { create(:project, public: false) }
+ let(:favorite_path) { "/api/v3/projects/#{other_project.id}/favorite" }
+
+ it "responds with 404 Not Found" do
+ expect(last_response).to have_http_status(404)
+ end
+ end
+
+ context "when user is anonymous and login not required",
+ with_settings: { login_required: false } do
+ let(:user) { User.anonymous }
+
+ it "responds with 404 Not found" do
+ expect(last_response).to have_http_status(404)
+ end
+
+ context "when project is public" do
+ before do
+ project.update!(public: true)
+ end
+
+ it "responds with 403 Forbidden" do
+ expect(last_response).to have_http_status(404)
+ end
+ end
+ end
+
+ context "when user is anonymous and login required",
+ with_settings: { login_required: true } do
+ let(:user) { User.anonymous }
+
+ it "responds with 401 Unauthorized" do
+ expect(last_response).to have_http_status(401)
+ end
+ end
+ end
+
+ describe "DELETE /api/v3/projects/:id/favorite" do
+ before do
+ project.set_favored(user, favored: true)
+ delete favorite_path
+ end
+
+ it "responds with 204 No Content and removes favorite", :aggregate_failures do
+ expect(last_response).to have_http_status(204)
+ expect(project.favored_by?(user)).to be false
+ end
+
+ context "when project is not favorited" do
+ before do
+ project.set_favored(user, favored: false)
+ delete favorite_path
+ end
+
+ it "responds with 204 No Content, and keeps the project as is", :aggregate_failures do
+ expect(last_response).to have_http_status(204)
+ expect(project.favored_by?(user)).to be false
+ end
+ end
+
+ context "when user lacks permissions" do
+ let(:other_project) { create(:project, public: false) }
+ let(:favorite_path) { "/api/v3/projects/#{other_project.id}/favorite" }
+
+ it "responds with 404 Not Found" do
+ expect(last_response).to have_http_status(404)
+ end
+ end
+ end
+end