From 822e7e228741281e57604a047e98f9193d80c3e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Mon, 28 Oct 2024 00:10:25 +0100 Subject: [PATCH] pl-api: Add some newer Mastodon methods MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- packages/pl-api/lib/client.ts | 24 +++++++++++-- .../lib/entities/notification-policy.ts | 11 +++--- packages/pl-api/lib/features.ts | 11 ++++++ packages/pl-api/lib/params/notifications.ts | 36 +++++++++++++------ 4 files changed, 65 insertions(+), 17 deletions(-) diff --git a/packages/pl-api/lib/client.ts b/packages/pl-api/lib/client.ts index 22f9413d5..000ef0cb0 100644 --- a/packages/pl-api/lib/client.ts +++ b/packages/pl-api/lib/client.ts @@ -169,6 +169,7 @@ import type { GetTrendingLinks, GetTrendingStatuses, GetTrendingTags, + GetUnreadNotificationCountParams, GroupTimelineParams, HashtagTimelineParams, HomeTimelineParams, @@ -2524,13 +2525,30 @@ class PlApiClient { return response.json as {}; }, + /** + * Get the number of unread notification + * Get the (capped) number of unread notifications for the current user. + * + * Requires features{@link Features['notificationsGetUnreadCount']}. + * @see {@link https://docs.joinmastodon.org/methods/notifications/#unread-count} + */ + getUnreadNotificationCount: async (params?: GetUnreadNotificationCountParams) => { + const response = await this.request('/api/v1/notifications/unread_count', { params }); + + return v.parse(v.object({ + count: v.number(), + }), response.json); + }, + /** * Get the filtering policy for notifications * Notifications filtering policy for the user. + * + * Requires features{@link Features['notificationsPolicy']}. * @see {@link https://docs.joinmastodon.org/methods/notifications/#get-policy} */ getNotificationPolicy: async () => { - const response = await this.request('/api/v1/notifications/policy'); + const response = await this.request('/api/v2/notifications/policy'); return v.parse(notificationPolicySchema, response.json); }, @@ -2538,10 +2556,12 @@ class PlApiClient { /** * Update the filtering policy for notifications * Update the user’s notifications filtering policy. + * + * Requires features{@link Features['notificationsPolicy']}. * @see {@link https://docs.joinmastodon.org/methods/notifications/#update-the-filtering-policy-for-notifications} */ updateNotificationPolicy: async (params: UpdateNotificationPolicyRequest) => { - const response = await this.request('/api/v1/notifications/policy', { method: 'POST', body: params }); + const response = await this.request('/api/v2/notifications/policy', { method: 'PATCH', body: params }); return v.parse(notificationPolicySchema, response.json); }, diff --git a/packages/pl-api/lib/entities/notification-policy.ts b/packages/pl-api/lib/entities/notification-policy.ts index ed964d15d..2eb3660f0 100644 --- a/packages/pl-api/lib/entities/notification-policy.ts +++ b/packages/pl-api/lib/entities/notification-policy.ts @@ -1,11 +1,14 @@ import * as v from 'valibot'; +const notificationPolicyRuleSchema = v.picklist(['accept', 'filter', 'drop']); + /** @see {@link https://docs.joinmastodon.org/entities/NotificationPolicy} */ const notificationPolicySchema = v.object({ - filter_not_following: v.boolean(), - filter_not_followers: v.boolean(), - filter_new_accounts: v.boolean(), - filter_private_mentions: v.boolean(), + for_not_following: notificationPolicyRuleSchema, + for_not_followers: notificationPolicyRuleSchema, + for_new_accounts: notificationPolicyRuleSchema, + for_private_mentions: notificationPolicyRuleSchema, + for_limited_accounts: notificationPolicyRuleSchema, summary: v.object({ pending_requests_count: v.pipe(v.number(), v.integer()), pending_notifications_count: v.pipe(v.number(), v.integer()), diff --git a/packages/pl-api/lib/features.ts b/packages/pl-api/lib/features.ts index 31261f0cd..a4b0850a6 100644 --- a/packages/pl-api/lib/features.ts +++ b/packages/pl-api/lib/features.ts @@ -833,6 +833,11 @@ const getFeatures = (instance: Instance) => { */ notificationsExcludeVisibilities: v.software === PLEROMA, + /** + * @see GET /api/v1/notifications/unread_count + */ + notificationsGetUnreadCount: instance.api_versions.mastodon >= 1, + /** * Allows specifying notification types to include, rather than to exclude. * @see GET /api/v1/notifications @@ -846,6 +851,12 @@ const getFeatures = (instance: Instance) => { v.software === GOTOSOCIAL, ]), + /** + * @see GET /api/v2/notifications/policy + * @see PATCH /api/v2/notifications/policy + */ + notificationsPolicy: instance.api_versions.mastodon >= 1, + pleromaAdminAccounts: v.software === PLEROMA, /** diff --git a/packages/pl-api/lib/params/notifications.ts b/packages/pl-api/lib/params/notifications.ts index 488dd807c..52d8f9553 100644 --- a/packages/pl-api/lib/params/notifications.ts +++ b/packages/pl-api/lib/params/notifications.ts @@ -1,11 +1,11 @@ import type { PaginationParams } from './common'; interface GetNotificationParams extends PaginationParams { - /** Array of String. Types to include in the result. */ + /** Types to include in the result. */ types?: string[]; - /** Array of String. Types to exclude from the results. */ + /** Types to exclude from the results. */ exclude_types?: string[]; - /** String. Return only notifications received from the specified account. */ + /** Return only notifications received from the specified account. */ account_id?: string; /** * will exclude the notifications for activities with the given visibilities. The parameter accepts an array of visibility types (`public`, `unlisted`, `private`, `direct`). @@ -14,21 +14,35 @@ interface GetNotificationParams extends PaginationParams { exclude_visibilities?: string[]; } +interface GetUnreadNotificationCountParams { + /** Maximum number of results to return. Defaults to 100 notifications. Max 1000 notifications. */ + limit?: number; + /** Types of notifications that should count towards unread notifications. */ + types?: string[]; + /** Types of notifications that should not count towards unread notifications */ + exclude_types?: string[]; + /** Only count unread notifications received from the specified account. */ + account_id?: string; +} + interface UpdateNotificationPolicyRequest { - /** Boolean. Whether to filter notifications from accounts the user is not following. */ - filter_not_following?: boolean; - /** Boolean. Whether to filter notifications from accounts that are not following the user. */ - filter_not_followers?: boolean; - /** Boolean. Whether to filter notifications from accounts created in the past 30 days. */ - filter_new_accounts?: boolean; - /** Boolean. Whether to filter notifications from private mentions. Replies to private mentions initiated by the user, as well as accounts the user follows, are never filtered. */ - filter_private_mentions?: boolean; + /** Whether to `accept`, `filter` or `drop` notifications from accounts the user is not following. */ + for_not_following?: boolean; + /** Whether to `accept`, `filter` or `drop` notifications from accounts that are not following the user. */ + for_not_followers?: boolean; + /** Whether to `accept`, `filter` or `drop` notifications from accounts created in the past 30 days. */ + for_new_accounts?: boolean; + /** Whether to `accept`, `filter` or `drop` notifications from private mentions. drop will prevent creation of the notification object altogether (without preventing the underlying activity), */ + for_private_mentions?: boolean; + /** Whether to `accept`, `filter` or `drop` notifications from accounts that were limited by a moderator. */ + for_limited_accounts?: boolean; } type GetNotificationRequestsParams = PaginationParams; export type { GetNotificationParams, + GetUnreadNotificationCountParams, UpdateNotificationPolicyRequest, GetNotificationRequestsParams, };