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

refs #1717 Fix custom emoji for Misskey #1752

Merged
merged 2 commits into from
Jun 3, 2023
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
18 changes: 18 additions & 0 deletions example/typescript/src/misskey/emojis.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import generator, { MegalodonInterface } from 'megalodon'

declare var process: {
env: {
MISSKEY_ACCESS_TOKEN: string
}
}

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

const access_token: string = process.env.MISSKEY_ACCESS_TOKEN

const client: MegalodonInterface = generator('misskey', BASE_URL, access_token)

client
.getInstanceCustomEmojis()
.then(res => console.log(res.data))
.catch(err => console.error(err))
18 changes: 18 additions & 0 deletions example/typescript/src/misskey/note.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import generator, { MegalodonInterface } from 'megalodon'

declare var process: {
env: {
MISSKEY_ACCESS_TOKEN: string
}
}

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

const access_token: string = process.env.MISSKEY_ACCESS_TOKEN

const client: MegalodonInterface = generator('misskey', BASE_URL, access_token)

client
.getStatus('9fjp2aknrm')
.then(res => console.log(res.data))
.catch(err => console.error(err))
2 changes: 1 addition & 1 deletion example/typescript/src/misskey/timeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const access_token: string = process.env.MISSKEY_ACCESS_TOKEN
const client: MegalodonInterface = generator('misskey', BASE_URL, access_token)

client
.getHomeTimeline()
.getLocalTimeline()
.then((resp: Response<Array<Entity.Status>>) => {
console.log(resp.data)
})
Expand Down
4 changes: 2 additions & 2 deletions megalodon/src/misskey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2104,11 +2104,11 @@ export default class Misskey implements MegalodonInterface {
// instance/custom_emojis
// ======================================
/**
* POST /api/meta
* GET /api/emojis
*/
public async getInstanceCustomEmojis(): Promise<Response<Array<Entity.Emoji>>> {
return this.client
.post<MisskeyAPI.Entity.Meta>('/api/meta')
.get<{ emojis: Array<MisskeyAPI.Entity.Emoji> }>('/api/emojis')
.then(res => ({ ...res, data: res.data.emojis.map(e => MisskeyAPI.Converter.emoji(e)) }))
}

Expand Down
44 changes: 41 additions & 3 deletions megalodon/src/misskey/api_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ namespace MisskeyAPI {
static_url: e.url,
url: e.url,
visible_in_picker: true,
category: ''
category: e.category
}
}

Expand Down Expand Up @@ -253,7 +253,7 @@ namespace MisskeyAPI {
: '',
plain_content: n.text ? n.text : null,
created_at: n.createdAt,
emojis: Array.isArray(n.emojis) ? n.emojis.map(e => emoji(e)) : [],
emojis: (Array.isArray(n.emojis) ? n.emojis.map(e => emoji(e)) : []).concat(mapReactionEmojis(n.reactionEmojis)),
replies_count: n.repliesCount,
reblogs_count: n.renoteCount,
favourites_count: 0,
Expand Down Expand Up @@ -294,6 +294,16 @@ namespace MisskeyAPI {
})
}

const mapReactionEmojis = (r: { [key: string]: string }): Array<MegalodonEntity.Emoji> => {
return Object.keys(r).map(key => ({
shortcode: key,
static_url: r[key],
url: r[key],
visible_in_picker: true,
category: ''
}))
}

export const reactions = (r: Array<Entity.Reaction>): Array<MegalodonEntity.Reaction> => {
const result: Array<MegalodonEntity.Reaction> = []
r.map(e => {
Expand Down Expand Up @@ -460,6 +470,7 @@ namespace MisskeyAPI {
* Interface
*/
export interface Interface {
get<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
post<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
cancel(): void
socket(channel: 'user' | 'localTimeline' | 'hybridTimeline' | 'globalTimeline' | 'conversation' | 'list', listId?: string): WebSocket
Expand Down Expand Up @@ -493,7 +504,34 @@ namespace MisskeyAPI {
}

/**
* POST request to mastodon REST API.
* GET request to misskey API.
**/
public async get<T>(path: string, params: any = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
let options: AxiosRequestConfig = {
params: params,
headers: headers,
maxContentLength: Infinity,
maxBodyLength: Infinity
}
if (this.proxyConfig) {
options = Object.assign(options, {
httpAgent: proxyAgent(this.proxyConfig),
httpsAgent: proxyAgent(this.proxyConfig)
})
}
return axios.get<T>(this.baseUrl + path, options).then((resp: AxiosResponse<T>) => {
const res: Response<T> = {
data: resp.data,
status: resp.status,
statusText: resp.statusText,
headers: resp.headers
}
return res
})
}

/**
* POST request to misskey REST API.
* @param path relative path from baseUrl
* @param params Form data
* @param headers Request header object
Expand Down
2 changes: 1 addition & 1 deletion megalodon/src/misskey/entities/emoji.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace MisskeyEntity {
export type Emoji = {
name: string
host: string | null
url: string
aliases: Array<string>
category: string
}
}
2 changes: 2 additions & 0 deletions megalodon/src/misskey/entities/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ namespace MisskeyEntity {
renoteCount: number
repliesCount: number
reactions: { [key: string]: number }
// This field includes only remote emojis
reactionEmojis: { [key: string]: string }
emojis: Array<Emoji>
fileIds: Array<string>
files: Array<File>
Expand Down
1 change: 1 addition & 0 deletions megalodon/test/integration/misskey.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const note: MisskeyEntity.Note = {
renoteCount: 0,
repliesCount: 0,
reactions: {},
reactionEmojis: {},
emojis: [],
fileIds: [],
files: [],
Expand Down
52 changes: 52 additions & 0 deletions megalodon/test/unit/misskey/api_client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ describe('api_client', () => {
renoteCount: 0,
repliesCount: 0,
reactions: {},
reactionEmojis: {},
emojis: [],
fileIds: [],
files: [],
Expand All @@ -216,6 +217,7 @@ describe('api_client', () => {
renoteCount: 0,
repliesCount: 0,
reactions: {},
reactionEmojis: {},
emojis: [],
fileIds: [],
files: [],
Expand All @@ -227,5 +229,55 @@ describe('api_client', () => {
expect(megalodonStatus.content).toEqual(content)
})
})
describe('emoji reaction', () => {
it('reactionEmojis should be parsed', () => {
const plainContent = 'hoge\nfuga\nfuga'
const note: MisskeyEntity.Note = {
id: '1',
createdAt: '2021-02-01T01:49:29',
userId: '1',
user: user,
text: plainContent,
cw: null,
visibility: 'public',
renoteCount: 0,
repliesCount: 0,
reactions: {
':example1@.:': 1,
':example2@example.com:': 2
},
reactionEmojis: {
'example2@example.com': 'https://example.com/emoji.png'
},
emojis: [],
fileIds: [],
files: [],
replyId: null,
renoteId: null
}
const megalodonStatus = MisskeyAPI.Converter.note(note)
expect(megalodonStatus.emojis).toEqual([
{
shortcode: 'example2@example.com',
static_url: 'https://example.com/emoji.png',
url: 'https://example.com/emoji.png',
visible_in_picker: true,
category: ''
}
])
expect(megalodonStatus.emoji_reactions).toEqual([
{
count: 1,
me: false,
name: ':example1@.:'
},
{
count: 2,
me: false,
name: ':example2@example.com:'
}
])
})
})
})
})