mirror of
https://git.kescher.at/CatCatNya/catstodon.git
synced 2024-11-26 00:51:37 +01:00
Change grouped notifications API shape (take 2) (#31214)
This commit is contained in:
parent
288961bbb9
commit
549ab089ee
9 changed files with 70 additions and 20 deletions
|
@ -33,7 +33,8 @@ class Api::V2Alpha::NotificationsController < Api::BaseController
|
||||||
'app.notification_grouping.status.unique_count' => statuses.uniq.size
|
'app.notification_grouping.status.unique_count' => statuses.uniq.size
|
||||||
)
|
)
|
||||||
|
|
||||||
render json: @grouped_notifications, each_serializer: REST::NotificationGroupSerializer, relationships: @relationships, group_metadata: @group_metadata
|
presenter = GroupedNotificationsPresenter.new(@grouped_notifications)
|
||||||
|
render json: presenter, serializer: REST::DedupNotificationGroupSerializer, relationships: @relationships, group_metadata: @group_metadata
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -47,7 +48,8 @@ class Api::V2Alpha::NotificationsController < Api::BaseController
|
||||||
|
|
||||||
def show
|
def show
|
||||||
@notification = current_account.notifications.without_suspended.find_by!(group_key: params[:id])
|
@notification = current_account.notifications.without_suspended.find_by!(group_key: params[:id])
|
||||||
render json: NotificationGroup.from_notification(@notification), serializer: REST::NotificationGroupSerializer
|
presenter = GroupedNotificationsPresenter.new([NotificationGroup.from_notification(@notification)])
|
||||||
|
render json: presenter, serializer: REST::DedupNotificationGroupSerializer
|
||||||
end
|
end
|
||||||
|
|
||||||
def clear
|
def clear
|
||||||
|
|
|
@ -38,10 +38,6 @@ function dispatchAssociatedRecords(
|
||||||
const fetchedStatuses: ApiStatusJSON[] = [];
|
const fetchedStatuses: ApiStatusJSON[] = [];
|
||||||
|
|
||||||
notifications.forEach((notification) => {
|
notifications.forEach((notification) => {
|
||||||
if ('sample_accounts' in notification) {
|
|
||||||
fetchedAccounts.push(...notification.sample_accounts);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (notification.type === 'admin.report') {
|
if (notification.type === 'admin.report') {
|
||||||
fetchedAccounts.push(notification.report.target_account);
|
fetchedAccounts.push(notification.report.target_account);
|
||||||
}
|
}
|
||||||
|
@ -75,7 +71,9 @@ export const fetchNotifications = createDataLoadingThunk(
|
||||||
: excludeAllTypesExcept(activeFilter),
|
: excludeAllTypesExcept(activeFilter),
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
({ notifications }, { dispatch }) => {
|
({ notifications, accounts, statuses }, { dispatch }) => {
|
||||||
|
dispatch(importFetchedAccounts(accounts));
|
||||||
|
dispatch(importFetchedStatuses(statuses));
|
||||||
dispatchAssociatedRecords(dispatch, notifications);
|
dispatchAssociatedRecords(dispatch, notifications);
|
||||||
const payload: (ApiNotificationGroupJSON | NotificationGap)[] =
|
const payload: (ApiNotificationGroupJSON | NotificationGap)[] =
|
||||||
notifications;
|
notifications;
|
||||||
|
@ -95,7 +93,9 @@ export const fetchNotificationsGap = createDataLoadingThunk(
|
||||||
async (params: { gap: NotificationGap }) =>
|
async (params: { gap: NotificationGap }) =>
|
||||||
apiFetchNotifications({ max_id: params.gap.maxId }),
|
apiFetchNotifications({ max_id: params.gap.maxId }),
|
||||||
|
|
||||||
({ notifications }, { dispatch }) => {
|
({ notifications, accounts, statuses }, { dispatch }) => {
|
||||||
|
dispatch(importFetchedAccounts(accounts));
|
||||||
|
dispatch(importFetchedStatuses(statuses));
|
||||||
dispatchAssociatedRecords(dispatch, notifications);
|
dispatchAssociatedRecords(dispatch, notifications);
|
||||||
|
|
||||||
return { notifications };
|
return { notifications };
|
||||||
|
|
|
@ -1,17 +1,24 @@
|
||||||
import api, { apiRequest, getLinks } from 'mastodon/api';
|
import api, { apiRequest, getLinks } from 'mastodon/api';
|
||||||
import type { ApiNotificationGroupJSON } from 'mastodon/api_types/notifications';
|
import type { ApiNotificationGroupsResultJSON } from 'mastodon/api_types/notifications';
|
||||||
|
|
||||||
export const apiFetchNotifications = async (params?: {
|
export const apiFetchNotifications = async (params?: {
|
||||||
exclude_types?: string[];
|
exclude_types?: string[];
|
||||||
max_id?: string;
|
max_id?: string;
|
||||||
}) => {
|
}) => {
|
||||||
const response = await api().request<ApiNotificationGroupJSON[]>({
|
const response = await api().request<ApiNotificationGroupsResultJSON>({
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
url: '/api/v2_alpha/notifications',
|
url: '/api/v2_alpha/notifications',
|
||||||
params,
|
params,
|
||||||
});
|
});
|
||||||
|
|
||||||
return { notifications: response.data, links: getLinks(response) };
|
const { statuses, accounts, notification_groups } = response.data;
|
||||||
|
|
||||||
|
return {
|
||||||
|
statuses,
|
||||||
|
accounts,
|
||||||
|
notifications: notification_groups,
|
||||||
|
links: getLinks(response),
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export const apiClearNotifications = () =>
|
export const apiClearNotifications = () =>
|
||||||
|
|
|
@ -51,7 +51,7 @@ export interface BaseNotificationGroupJSON {
|
||||||
group_key: string;
|
group_key: string;
|
||||||
notifications_count: number;
|
notifications_count: number;
|
||||||
type: NotificationType;
|
type: NotificationType;
|
||||||
sample_accounts: ApiAccountJSON[];
|
sample_account_ids: string[];
|
||||||
latest_page_notification_at: string; // FIXME: This will only be present if the notification group is returned in a paginated list, not requested directly
|
latest_page_notification_at: string; // FIXME: This will only be present if the notification group is returned in a paginated list, not requested directly
|
||||||
most_recent_notification_id: string;
|
most_recent_notification_id: string;
|
||||||
page_min_id?: string;
|
page_min_id?: string;
|
||||||
|
@ -143,3 +143,9 @@ export type ApiNotificationGroupJSON =
|
||||||
| AccountRelationshipSeveranceNotificationGroupJSON
|
| AccountRelationshipSeveranceNotificationGroupJSON
|
||||||
| NotificationGroupWithStatusJSON
|
| NotificationGroupWithStatusJSON
|
||||||
| ModerationWarningNotificationGroupJSON;
|
| ModerationWarningNotificationGroupJSON;
|
||||||
|
|
||||||
|
export interface ApiNotificationGroupsResultJSON {
|
||||||
|
accounts: ApiAccountJSON[];
|
||||||
|
statuses: ApiStatusJSON[];
|
||||||
|
notification_groups: ApiNotificationGroupJSON[];
|
||||||
|
}
|
||||||
|
|
|
@ -14,7 +14,7 @@ import type { ApiReportJSON } from 'mastodon/api_types/reports';
|
||||||
export const NOTIFICATIONS_GROUP_MAX_AVATARS = 8;
|
export const NOTIFICATIONS_GROUP_MAX_AVATARS = 8;
|
||||||
|
|
||||||
interface BaseNotificationGroup
|
interface BaseNotificationGroup
|
||||||
extends Omit<BaseNotificationGroupJSON, 'sample_accounts'> {
|
extends Omit<BaseNotificationGroupJSON, 'sample_account_ids'> {
|
||||||
sampleAccountIds: string[];
|
sampleAccountIds: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -115,8 +115,7 @@ function createAccountRelationshipSeveranceEventFromJSON(
|
||||||
export function createNotificationGroupFromJSON(
|
export function createNotificationGroupFromJSON(
|
||||||
groupJson: ApiNotificationGroupJSON,
|
groupJson: ApiNotificationGroupJSON,
|
||||||
): NotificationGroup {
|
): NotificationGroup {
|
||||||
const { sample_accounts, ...group } = groupJson;
|
const { sample_account_ids: sampleAccountIds, ...group } = groupJson;
|
||||||
const sampleAccountIds = sample_accounts.map((account) => account.id);
|
|
||||||
|
|
||||||
switch (group.type) {
|
switch (group.type) {
|
||||||
case 'favourite':
|
case 'favourite':
|
||||||
|
|
21
app/presenters/grouped_notifications_presenter.rb
Normal file
21
app/presenters/grouped_notifications_presenter.rb
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class GroupedNotificationsPresenter < ActiveModelSerializers::Model
|
||||||
|
def initialize(grouped_notifications)
|
||||||
|
super()
|
||||||
|
|
||||||
|
@grouped_notifications = grouped_notifications
|
||||||
|
end
|
||||||
|
|
||||||
|
def notification_groups
|
||||||
|
@grouped_notifications
|
||||||
|
end
|
||||||
|
|
||||||
|
def statuses
|
||||||
|
@grouped_notifications.filter_map(&:target_status).uniq(&:id)
|
||||||
|
end
|
||||||
|
|
||||||
|
def accounts
|
||||||
|
@grouped_notifications.flat_map(&:sample_accounts).uniq(&:id)
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,7 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class REST::DedupNotificationGroupSerializer < ActiveModel::Serializer
|
||||||
|
has_many :accounts, serializer: REST::AccountSerializer
|
||||||
|
has_many :statuses, serializer: REST::StatusSerializer
|
||||||
|
has_many :notification_groups, serializer: REST::NotificationGroupSerializer
|
||||||
|
end
|
|
@ -8,12 +8,20 @@ class REST::NotificationGroupSerializer < ActiveModel::Serializer
|
||||||
attribute :page_max_id, if: :paginated?
|
attribute :page_max_id, if: :paginated?
|
||||||
attribute :latest_page_notification_at, if: :paginated?
|
attribute :latest_page_notification_at, if: :paginated?
|
||||||
|
|
||||||
has_many :sample_accounts, serializer: REST::AccountSerializer
|
attribute :sample_account_ids
|
||||||
belongs_to :target_status, key: :status, if: :status_type?, serializer: REST::StatusSerializer
|
attribute :status_id, if: :status_type?
|
||||||
belongs_to :report, if: :report_type?, serializer: REST::ReportSerializer
|
belongs_to :report, if: :report_type?, serializer: REST::ReportSerializer
|
||||||
belongs_to :account_relationship_severance_event, key: :event, if: :relationship_severance_event?, serializer: REST::AccountRelationshipSeveranceEventSerializer
|
belongs_to :account_relationship_severance_event, key: :event, if: :relationship_severance_event?, serializer: REST::AccountRelationshipSeveranceEventSerializer
|
||||||
belongs_to :account_warning, key: :moderation_warning, if: :moderation_warning_event?, serializer: REST::AccountWarningSerializer
|
belongs_to :account_warning, key: :moderation_warning, if: :moderation_warning_event?, serializer: REST::AccountWarningSerializer
|
||||||
|
|
||||||
|
def sample_account_ids
|
||||||
|
object.sample_accounts.pluck(:id).map(&:to_s)
|
||||||
|
end
|
||||||
|
|
||||||
|
def status_id
|
||||||
|
object.target_status&.id&.to_s
|
||||||
|
end
|
||||||
|
|
||||||
def status_type?
|
def status_type?
|
||||||
[:favourite, :reblog, :status, :mention, :poll, :update].include?(object.type)
|
[:favourite, :reblog, :status, :mention, :poll, :update].include?(object.type)
|
||||||
end
|
end
|
||||||
|
|
|
@ -135,7 +135,7 @@ RSpec.describe 'Notifications' do
|
||||||
|
|
||||||
expect(response).to have_http_status(200)
|
expect(response).to have_http_status(200)
|
||||||
expect(body_json_types.uniq).to eq ['mention']
|
expect(body_json_types.uniq).to eq ['mention']
|
||||||
expect(body_as_json[0][:page_min_id]).to_not be_nil
|
expect(body_as_json.dig(:notification_groups, 0, :page_min_id)).to_not be_nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -147,7 +147,7 @@ RSpec.describe 'Notifications' do
|
||||||
|
|
||||||
notifications = user.account.notifications
|
notifications = user.account.notifications
|
||||||
|
|
||||||
expect(body_as_json.size)
|
expect(body_as_json[:notification_groups].size)
|
||||||
.to eq(params[:limit])
|
.to eq(params[:limit])
|
||||||
|
|
||||||
expect(response)
|
expect(response)
|
||||||
|
@ -161,7 +161,7 @@ RSpec.describe 'Notifications' do
|
||||||
end
|
end
|
||||||
|
|
||||||
def body_json_types
|
def body_json_types
|
||||||
body_as_json.pluck(:type)
|
body_as_json[:notification_groups].pluck(:type)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue