Skip to content

Commit

Permalink
Rename /api/v2_alpha/notifications* to /api/v2/notifications* (ma…
Browse files Browse the repository at this point in the history
  • Loading branch information
ClearlyClaire authored Sep 16, 2024
1 parent efb0e2c commit e0648a9
Show file tree
Hide file tree
Showing 9 changed files with 391 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

class Api::V2Alpha::Notifications::AccountsController < Api::BaseController
class Api::V2::Notifications::AccountsController < Api::BaseController
before_action -> { doorkeeper_authorize! :read, :'read:notifications' }
before_action :require_user!
before_action :set_notifications!
Expand Down Expand Up @@ -33,11 +33,11 @@ def set_notifications!
end

def next_path
api_v2_alpha_notification_accounts_url pagination_params(max_id: pagination_max_id) if records_continue?
api_v2_notification_accounts_url pagination_params(max_id: pagination_max_id) if records_continue?
end

def prev_path
api_v2_alpha_notification_accounts_url pagination_params(min_id: pagination_since_id) unless @paginated_notifications.empty?
api_v2_notification_accounts_url pagination_params(min_id: pagination_since_id) unless @paginated_notifications.empty?
end

def pagination_collection
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

class Api::V2Alpha::NotificationsController < Api::BaseController
class Api::V2::NotificationsController < Api::BaseController
before_action -> { doorkeeper_authorize! :read, :'read:notifications' }, except: [:clear, :dismiss]
before_action -> { doorkeeper_authorize! :write, :'write:notifications' }, only: [:clear, :dismiss]
before_action :require_user!
Expand All @@ -21,7 +21,7 @@ def index
ActiveRecord::Associations::Preloader.new(records: @presenter.accounts, associations: [:account_stat, { user: :role }]).call
end

MastodonOTELTracer.in_span('Api::V2Alpha::NotificationsController#index rendering') do |span|
MastodonOTELTracer.in_span('Api::V2::NotificationsController#index rendering') do |span|
statuses = @grouped_notifications.filter_map { |group| group.target_status&.id }

span.add_attributes(
Expand Down Expand Up @@ -64,7 +64,7 @@ def dismiss
private

def load_notifications
MastodonOTELTracer.in_span('Api::V2Alpha::NotificationsController#load_notifications') do
MastodonOTELTracer.in_span('Api::V2::NotificationsController#load_notifications') do
notifications = browserable_account_notifications.includes(from_account: [:account_stat, :user]).to_a_grouped_paginated_by_id(
limit_param(DEFAULT_NOTIFICATIONS_LIMIT),
params.slice(:max_id, :since_id, :min_id, :grouped_types).permit(:max_id, :since_id, :min_id, grouped_types: [])
Expand All @@ -79,7 +79,7 @@ def load_notifications
def load_grouped_notifications
return [] if @notifications.empty?

MastodonOTELTracer.in_span('Api::V2Alpha::NotificationsController#load_grouped_notifications') do
MastodonOTELTracer.in_span('Api::V2::NotificationsController#load_grouped_notifications') do
NotificationGroup.from_notifications(@notifications, pagination_range: (@notifications.last.id)..(@notifications.first.id), grouped_types: params[:grouped_types])
end
end
Expand All @@ -101,11 +101,11 @@ def target_statuses_from_notifications
end

def next_path
api_v2_alpha_notifications_url pagination_params(max_id: pagination_max_id) unless @notifications.empty?
api_v2_notifications_url pagination_params(max_id: pagination_max_id) unless @notifications.empty?
end

def prev_path
api_v2_alpha_notifications_url pagination_params(min_id: pagination_since_id) unless @notifications.empty?
api_v2_notifications_url pagination_params(min_id: pagination_since_id) unless @notifications.empty?
end

def pagination_collection
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/mastodon/api/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const apiFetchNotificationGroups = async (params?: {
}) => {
const response = await api().request<ApiNotificationGroupsResultJSON>({
method: 'GET',
url: '/api/v2_alpha/notifications',
url: '/api/v2/notifications',
params,
});

Expand Down
4 changes: 1 addition & 3 deletions app/serializers/rest/instance_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,7 @@ def registrations
end

def api_versions
{
mastodon: 1,
}
Mastodon::Version.api_versions
end

private
Expand Down
32 changes: 19 additions & 13 deletions config/routes/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,21 @@
end
end

concern :grouped_notifications do
resources :notifications, param: :group_key, only: [:index, :show] do
collection do
post :clear
get :unread_count
end

member do
post :dismiss
end

resources :accounts, only: [:index], module: :notifications
end
end

namespace :v2 do
get '/search', to: 'search#index', as: :search

Expand All @@ -341,21 +356,12 @@
namespace :notifications do
resource :policy, only: [:show, :update]
end
end

namespace :v2_alpha do
resources :notifications, param: :group_key, only: [:index, :show] do
collection do
post :clear
get :unread_count
end

member do
post :dismiss
end
concerns :grouped_notifications
end

resources :accounts, only: [:index], module: :notifications
end
namespace :v2_alpha, module: 'v2' do
concerns :grouped_notifications
end

namespace :web do
Expand Down
6 changes: 6 additions & 0 deletions lib/mastodon/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ def gem_version
@gem_version ||= Gem::Version.new(to_s.split('+')[0])
end

def api_versions
{
mastodon: 2,
}
end

def repository
ENV.fetch('GITHUB_REPOSITORY', 'mastodon/mastodon')
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
let(:scopes) { 'read:notifications write:notifications' }
let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }

describe 'GET /api/v2_alpha/notifications/:group_key/accounts', :inline_jobs do
describe 'GET /api/v2/notifications/:group_key/accounts', :inline_jobs do
subject do
get "/api/v2_alpha/notifications/#{user.account.notifications.first.group_key}/accounts", headers: headers, params: params
get "/api/v2/notifications/#{user.account.notifications.first.group_key}/accounts", headers: headers, params: params
end

let(:params) { {} }
Expand Down Expand Up @@ -71,8 +71,8 @@

expect(response)
.to include_pagination_headers(
prev: api_v2_alpha_notification_accounts_url(limit: params[:limit], min_id: notifications.first.id),
next: api_v2_alpha_notification_accounts_url(limit: params[:limit], max_id: notifications.second.id)
prev: api_v2_notification_accounts_url(limit: params[:limit], min_id: notifications.first.id),
next: api_v2_notification_accounts_url(limit: params[:limit], max_id: notifications.second.id)
)
end
end
Expand Down
Loading

0 comments on commit e0648a9

Please sign in to comment.