Skip to content

Commit

Permalink
Fix API response of pleroma read notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
h3poteto committed Oct 21, 2024
1 parent af8af0b commit 1587760
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 36 deletions.
2 changes: 1 addition & 1 deletion example/typescript/src/pleroma/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ const access_token: string = process.env.PLEROMA_ACCESS_TOKEN

const client = generator('pleroma', BASE_URL, access_token)

client.getNotifications({ exclude_types: [NotificationType.Favourite, NotificationType.Reblog] }).then(res => console.log(res.data))
client.getNotifications({ exclude_types: [NotificationType.Reblog] }).then(res => console.log(res.data))
33 changes: 33 additions & 0 deletions example/typescript/src/pleroma/read_notifications.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as readline from 'readline'
import generator, { Entity, Response } from 'megalodon'

const rl: readline.ReadLine = readline.createInterface({
input: process.stdin,
output: process.stdout
})

const BASE_URL: string = 'https://pleroma.io'

const access_token: string = process.env.PLEROMA_ACCESS_TOKEN as string

const client = generator('pleroma', BASE_URL, access_token)

new Promise(resolve => {
rl.question('Max notification ID: ', max_id => {
client
.readNotifications({ max_id: max_id })
.then(res => {
console.log(res)
rl.close()
client.getMarkers(['home', 'notifications']).then((res: Response<Entity.Marker | Record<never, never>>) => {
console.log(res.data)
})

resolve(res)
})
.catch(err => {
console.error(err)
rl.close()
})
})
})
5 changes: 1 addition & 4 deletions megalodon/src/firefish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1926,10 +1926,7 @@ export default class Firefish implements MegalodonInterface {
})
}

public async readNotifications(_options: {
id?: string
max_id?: string
}): Promise<Response<Entity.Notification | Array<Entity.Notification>>> {
public async readNotifications(_options: { id?: string; max_id?: string }): Promise<Response<{}>> {
return new Promise((_, reject) => {
const err = new NotImplementedError('mastodon does not support')
reject(err)
Expand Down
5 changes: 1 addition & 4 deletions megalodon/src/friendica.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2466,10 +2466,7 @@ export default class Friendica implements MegalodonInterface {
return this.client.post<Record<string, unknown>>(`/api/v1/notifications/${id}/dismiss`)
}

public readNotifications(_options: {
id?: string
max_id?: string
}): Promise<Response<Entity.Notification | Array<Entity.Notification>>> {
public readNotifications(_options: { id?: string; max_id?: string }): Promise<Response<{}>> {
return new Promise((_, reject) => {
const err = new NotImplementedError('Friendica does not support this method')
reject(err)
Expand Down
5 changes: 1 addition & 4 deletions megalodon/src/gotosocial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2431,10 +2431,7 @@ export default class Gotosocial implements MegalodonInterface {
})
}

public readNotifications(_options: {
id?: string
max_id?: string
}): Promise<Response<Entity.Notification | Array<Entity.Notification>>> {
public readNotifications(_options: { id?: string; max_id?: string }): Promise<Response<{}>> {
return new Promise((_, reject) => {
const err = new NotImplementedError('Gotosocial does not support this method')
reject(err)
Expand Down
5 changes: 1 addition & 4 deletions megalodon/src/mastodon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2803,10 +2803,7 @@ export default class Mastodon implements MegalodonInterface {
return this.client.post<{}>(`/api/v1/notifications/${id}/dismiss`)
}

public readNotifications(_options: {
id?: string
max_id?: string
}): Promise<Response<Entity.Notification | Array<Entity.Notification>>> {
public readNotifications(_options: { id?: string; max_id?: string }): Promise<Response<{}>> {
return new Promise((_, reject) => {
const err = new NotImplementedError('Mastodon does not support this method')
reject(err)
Expand Down
3 changes: 1 addition & 2 deletions megalodon/src/megalodon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1183,9 +1183,8 @@ export interface MegalodonInterface {
*
* @param id A single notification ID to read
* @param max_id Read all notifications up to this ID
* @return Array of notifications
*/
readNotifications(options: { id?: string; max_id?: string }): Promise<Response<Entity.Notification | Array<Entity.Notification>>>
readNotifications(options: { id?: string; max_id?: string }): Promise<Response<{}>>
// ======================================
// notifications/push
// ======================================
Expand Down
22 changes: 5 additions & 17 deletions megalodon/src/pleroma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2781,29 +2781,17 @@ export default class Pleroma implements MegalodonInterface {
* @param max_id Read all notifications up to this ID
* @return Array of notifications
*/
public async readNotifications(options: {
id?: string
max_id?: string
}): Promise<Response<Entity.Notification | Array<Entity.Notification>>> {
public async readNotifications(options: { id?: string; max_id?: string }): Promise<Response<{}>> {
if (options.id) {
const res = await this.client.post<PleromaAPI.Entity.Notification>('/api/v1/pleroma/notifications/read', {
const res = await this.client.post<{}>('/api/v1/pleroma/notifications/read', {
id: options.id
})
const notify = PleromaAPI.Converter.notification(res.data)
if (notify instanceof UnknownNotificationTypeError) return { ...res, data: [] }
return { ...res, data: notify }
return res
} else if (options.max_id) {
const res = await this.client.post<Array<PleromaAPI.Entity.Notification>>('/api/v1/pleroma/notifications/read', {
const res = await this.client.post<{}>('/api/v1/pleroma/notifications/read', {
max_id: options.max_id
})
return {
...res,
data: res.data.flatMap(n => {
const notify = PleromaAPI.Converter.notification(n)
if (notify instanceof UnknownNotificationTypeError) return []
return notify
})
}
return res
} else {
return new Promise((_, reject) => {
const err = new ArgumentError('id or max_id is required')
Expand Down

0 comments on commit 1587760

Please sign in to comment.