Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse pleroma custom emoji reactions in notifications #2147

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/typescript/src/pleroma/mentions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const excludes: Array<string> = [
NotificationType.Reblog,
NotificationType.PollVote,
NotificationType.PollExpired,
NotificationType.EmojiReaction
NotificationType.Reaction
]

client.getNotifications({ min_id: '9318', limit: 30, exclude_types: excludes }).then(res => console.log(res.data))
1 change: 0 additions & 1 deletion megalodon/src/entities/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export type Notification = {
created_at: string
id: string
status?: Status
emoji?: string
reaction?: Reaction
type: NotificationType
target?: Account
Expand Down
1 change: 1 addition & 0 deletions megalodon/src/entities/reaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export type Reaction = {
url?: string
static_url?: string
accounts?: Array<Account>
account_ids?: Array<string>
}
1 change: 0 additions & 1 deletion megalodon/src/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ namespace NotificationType {
export const Reblog: Entity.NotificationType = 'reblog'
export const Mention: Entity.NotificationType = 'mention'
export const Reaction: Entity.NotificationType = 'reaction'
export const EmojiReaction: Entity.NotificationType = 'emoji_reaction'
export const FollowRequest: Entity.NotificationType = 'follow_request'
export const Status: Entity.NotificationType = 'status'
export const PollVote: Entity.NotificationType = 'poll_vote'
Expand Down
39 changes: 34 additions & 5 deletions megalodon/src/pleroma/api_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ namespace PleromaAPI {
case PleromaNotificationType.Poll:
return NotificationType.PollExpired
case PleromaNotificationType.PleromaEmojiReaction:
return NotificationType.EmojiReaction
return NotificationType.Reaction
case PleromaNotificationType.FollowRequest:
return NotificationType.FollowRequest
case PleromaNotificationType.Update:
Expand All @@ -92,7 +92,7 @@ namespace PleromaAPI {
return PleromaNotificationType.Mention
case NotificationType.PollExpired:
return PleromaNotificationType.Poll
case NotificationType.EmojiReaction:
case NotificationType.Reaction:
return PleromaNotificationType.PleromaEmojiReaction
case NotificationType.FollowRequest:
return PleromaNotificationType.FollowRequest
Expand Down Expand Up @@ -257,12 +257,13 @@ namespace PleromaAPI {
const notificationType = decodeNotificationType(n.type)
if (notificationType instanceof UnknownNotificationTypeError) return notificationType
if (n.status && n.emoji) {
const r = mapReaction(n)
return {
id: n.id,
account: account(n.account),
created_at: n.created_at,
status: status(n.status),
emoji: n.emoji,
reaction: r ? reaction(r) : undefined,
type: notificationType
}
} else if (n.status) {
Expand Down Expand Up @@ -290,21 +291,49 @@ namespace PleromaAPI {
}
}
}

export const mapReaction = (n: Entity.Notification): Entity.Reaction | undefined => {
if (!n.emoji) return undefined
const name = n.emoji.replace(/:/g, '')
let r = {
count: 1,
me: false,
name: name
}
if (n.emoji_url) {
r = Object.assign({}, r, {
url: n.emoji_url
})
}
return r
}

export const poll = (p: Entity.Poll): MegalodonEntity.Poll => p
export const pollOption = (p: Entity.PollOption): MegalodonEntity.PollOption => p
export const preferences = (p: Entity.Preferences): MegalodonEntity.Preferences => p
export const push_subscription = (p: Entity.PushSubscription): MegalodonEntity.PushSubscription => p
export const reaction = (r: Entity.Reaction): MegalodonEntity.Reaction => {
const p = {
let p = {
count: r.count,
me: r.me,
name: r.name
}
if (r.url) {
p = Object.assign({}, p, {
url: r.url,
static_url: r.url
})
}
if (r.accounts) {
return Object.assign({}, p, {
p = Object.assign({}, p, {
accounts: r.accounts.map(a => account(a))
})
}
if (r.account_ids) {
p = Object.assign({}, p, {
accounts: r.account_ids
})
}
return p
}
export const relationship = (r: Entity.Relationship): MegalodonEntity.Relationship => ({
Expand Down
1 change: 1 addition & 0 deletions megalodon/src/pleroma/entities/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type Notification = {
id: string
status?: Status
emoji?: string
emoji_url?: string
type: NotificationType
target?: Account
}
Expand Down
2 changes: 2 additions & 0 deletions megalodon/src/pleroma/entities/reaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ export type Reaction = {
me: boolean
name: string
accounts?: Array<Account>
account_ids?: Array<string>
url?: string
}
2 changes: 1 addition & 1 deletion megalodon/test/integration/pleroma.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ describe('getNotifications', () => {
},
{
event: emojiReaction,
expected: MegalodonNotificationType.EmojiReaction,
expected: MegalodonNotificationType.Reaction,
title: 'emojiReaction'
},
{
Expand Down
4 changes: 2 additions & 2 deletions megalodon/test/unit/pleroma/api_client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe('api_client', () => {
dist: PleromaNotificationType.Poll
},
{
src: MegalodonNotificationType.EmojiReaction,
src: MegalodonNotificationType.Reaction,
dist: PleromaNotificationType.PleromaEmojiReaction
},
{
Expand Down Expand Up @@ -108,7 +108,7 @@ describe('api_client', () => {
},
{
src: PleromaNotificationType.PleromaEmojiReaction,
dist: MegalodonNotificationType.EmojiReaction
dist: MegalodonNotificationType.Reaction
},
{
src: PleromaNotificationType.FollowRequest,
Expand Down