Skip to content

Commit

Permalink
Add an endpoint to get reactions of the note in Misskey
Browse files Browse the repository at this point in the history
  • Loading branch information
h3poteto committed Apr 21, 2020
1 parent 074e269 commit 6cd5900
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 12 deletions.
14 changes: 9 additions & 5 deletions src/misskey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2000,11 +2000,15 @@ export default class Misskey implements MegalodonInterface {
.then(res => ({ ...res, data: MisskeyAPI.Converter.note(res.data) }))
}

public async getEmojiReactions(_id: string): Promise<Response<Array<Entity.Reaction>>> {
return new Promise((_, reject) => {
const err = new NoImplementedError('misskey does not support')
reject(err)
})
public async getEmojiReactions(id: string): Promise<Response<Array<Entity.Reaction>>> {
return this.client
.post<Array<MisskeyAPI.Entity.Reaction>>('/api/notes/reactions', {
noteId: id
})
.then(res => ({
...res,
data: MisskeyAPI.Converter.reactions(res.data)
}))
}

public async getEmojiReaction(_id: string, _emoji: string): Promise<Response<Entity.Reaction>> {
Expand Down
41 changes: 34 additions & 7 deletions src/misskey/api_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace MisskeyAPI {
export type Note = MisskeyEntity.Note
export type Notification = MisskeyEntity.Notification
export type Poll = MisskeyEntity.Poll
export type Reaction = MisskeyEntity.Reaction
export type Relation = MisskeyEntity.Relation
export type User = MisskeyEntity.User
export type UserDetail = MisskeyEntity.UserDetail
Expand Down Expand Up @@ -226,16 +227,42 @@ namespace MisskeyAPI {
application: null,
language: null,
pinned: null,
emoji_reactions: reactions(n.reactions)
emoji_reactions: mapReactions(n.reactions, n.myReaction)
}
}

export const reactions = (r: { [key: string]: number }): Array<MegalodonEntity.Reaction> => {
return Object.keys(r).map(key => ({
count: r[key],
me: false,
name: key
}))
export const mapReactions = (r: { [key: string]: number }, myReaction?: string): Array<MegalodonEntity.Reaction> => {
return Object.keys(r).map(key => {
if (myReaction && key === myReaction) {
return {
count: r[key],
me: true,
name: key
}
}
return {
count: r[key],
me: false,
name: key
}
})
}

export const reactions = (r: Array<Entity.Reaction>): Array<MegalodonEntity.Reaction> => {
let result: Array<MegalodonEntity.Reaction> = []
r.map(e => {
const i = result.findIndex(res => res.name === e.type)
if (i) {
result[i].count++
} else {
result.push({
count: 1,
me: false,
name: e.type
})
}
})
return result
}

export const noteToConversation = (n: Entity.Note): MegalodonEntity.Conversation => {
Expand Down
1 change: 1 addition & 0 deletions src/misskey/entities/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ namespace MisskeyEntity {
tags?: Array<string>
poll?: Poll
mentions?: Array<string>
myReaction?: string
}
}
10 changes: 10 additions & 0 deletions src/misskey/entities/reaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/// <reference path="user.ts" />

namespace MisskeyEntity {
export type Reaction = {
id: string
createdAt: string
user: User
type: string
}
}
1 change: 1 addition & 0 deletions src/misskey/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
/// <reference path="entities/note.ts" />
/// <reference path="entities/notification.ts" />
/// <reference path="entities/poll.ts" />
/// <reference path="entities/reaction.ts" />
/// <reference path="entities/relation.ts" />
/// <reference path="entities/user.ts" />
/// <reference path="entities/userDetail.ts" />
Expand Down

0 comments on commit 6cd5900

Please sign in to comment.