diff --git a/app/controllers/api/v1/accounts/notifications_controller.rb b/app/controllers/api/v1/accounts/notifications_controller.rb index fb23370c5..0d8cf6a47 100644 --- a/app/controllers/api/v1/accounts/notifications_controller.rb +++ b/app/controllers/api/v1/accounts/notifications_controller.rb @@ -1,7 +1,8 @@ class Api::V1::Accounts::NotificationsController < Api::V1::Accounts::BaseController RESULTS_PER_PAGE = 15 + include DateRangeHelper - before_action :fetch_notification, only: [:update, :destroy] + before_action :fetch_notification, only: [:update, :destroy, :snooze] before_action :set_primary_actor, only: [:read_all] before_action :set_current_page, only: [:index] @@ -38,6 +39,11 @@ class Api::V1::Accounts::NotificationsController < Api::V1::Accounts::BaseContro render json: @unread_count end + def snooze + @notification.update(snoozed_until: parse_date_time(params[:snoozed_until].to_s)) if params[:snoozed_until] + render json: @notification + end + private def set_primary_actor diff --git a/app/models/notification.rb b/app/models/notification.rb index 30c93c763..de6d6790c 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -7,6 +7,7 @@ # primary_actor_type :string not null # read_at :datetime # secondary_actor_type :string +# snoozed_until :datetime # created_at :datetime not null # updated_at :datetime not null # account_id :bigint not null diff --git a/config/routes.rb b/config/routes.rb index fc45c895c..fb071c91b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -171,6 +171,9 @@ Rails.application.routes.draw do post :read_all get :unread_count end + member do + post :snooze + end end resource :notification_settings, only: [:show, :update] diff --git a/db/migrate/20231129091149_add_snoozed_until_to_notifications.rb b/db/migrate/20231129091149_add_snoozed_until_to_notifications.rb new file mode 100644 index 000000000..14a52a9a7 --- /dev/null +++ b/db/migrate/20231129091149_add_snoozed_until_to_notifications.rb @@ -0,0 +1,5 @@ +class AddSnoozedUntilToNotifications < ActiveRecord::Migration[7.0] + def change + add_column :notifications, :snoozed_until, :datetime + end +end diff --git a/db/schema.rb b/db/schema.rb index 081030b11..0e4ba980c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2023_11_14_111614) do +ActiveRecord::Schema[7.0].define(version: 2023_11_29_091149) do # These are extensions that must be enabled in order to support this database enable_extension "pg_stat_statements" enable_extension "pg_trgm" @@ -729,6 +729,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_11_14_111614) do t.datetime "read_at", precision: nil t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.datetime "snoozed_until" t.index ["account_id"], name: "index_notifications_on_account_id" t.index ["primary_actor_type", "primary_actor_id"], name: "uniq_primary_actor_per_account_notifications" t.index ["secondary_actor_type", "secondary_actor_id"], name: "uniq_secondary_actor_per_account_notifications" diff --git a/spec/controllers/api/v1/accounts/notifications_controller_spec.rb b/spec/controllers/api/v1/accounts/notifications_controller_spec.rb index e922d613a..cadc212d4 100644 --- a/spec/controllers/api/v1/accounts/notifications_controller_spec.rb +++ b/spec/controllers/api/v1/accounts/notifications_controller_spec.rb @@ -153,4 +153,32 @@ RSpec.describe 'Notifications API', type: :request do end end end + + describe 'PATCH /api/v1/accounts/{account.id}/notifications/:id/snooze' do + let(:admin) { create(:user, account: account, role: :administrator) } + let!(:notification) { create(:notification, account: account, user: admin) } + + context 'when it is an unauthenticated user' do + it 'returns unauthorized' do + post "/api/v1/accounts/#{account.id}/notifications/#{notification.id}/snooze", + params: { snoozed_until: DateTime.now.utc + 1.day } + + expect(response).to have_http_status(:unauthorized) + end + end + + context 'when it is an authenticated user' do + let(:admin) { create(:user, account: account, role: :administrator) } + + it 'updates the notification snoozed until' do + post "/api/v1/accounts/#{account.id}/notifications/#{notification.id}/snooze", + headers: admin.create_new_auth_token, + params: { snoozed_until: DateTime.now.utc + 1.day }, + as: :json + + expect(response).to have_http_status(:success) + expect(notification.reload.snoozed_until).not_to eq('') + end + end + end end