add: Megalodon, initial mastodon api

This commit is contained in:
Mar0xy 2023-09-23 18:49:47 +02:00
parent 240d76a987
commit 2375d043d1
No known key found for this signature in database
GPG key ID: 56569BBE47D2C828
103 changed files with 9492 additions and 82 deletions

View file

@ -0,0 +1,233 @@
import MisskeyAPI from '@/misskey/api_client'
import MegalodonEntity from '@/entity'
import MisskeyEntity from '@/misskey/entity'
import MegalodonNotificationType from '@/notification'
import MisskeyNotificationType from '@/misskey/notification'
const user: MisskeyEntity.User = {
id: '1',
name: 'test_user',
username: 'TestUser',
host: 'misskey.io',
avatarUrl: 'https://example.com/icon.png',
avatarColor: '#000000',
emojis: []
}
const converter: MisskeyAPI.Converter = new MisskeyAPI.Converter("https://example.com")
describe('api_client', () => {
describe('notification', () => {
describe('encode', () => {
it('megalodon notification type should be encoded to misskey notification type', () => {
const cases: Array<{ src: MegalodonEntity.NotificationType; dist: MisskeyEntity.NotificationType }> = [
{
src: MegalodonNotificationType.Follow,
dist: MisskeyNotificationType.Follow
},
{
src: MegalodonNotificationType.Mention,
dist: MisskeyNotificationType.Reply
},
{
src: MegalodonNotificationType.Favourite,
dist: MisskeyNotificationType.Reaction
},
{
src: MegalodonNotificationType.Reaction,
dist: MisskeyNotificationType.Reaction
},
{
src: MegalodonNotificationType.Reblog,
dist: MisskeyNotificationType.Renote
},
{
src: MegalodonNotificationType.Poll,
dist: MisskeyNotificationType.PollEnded
},
{
src: MegalodonNotificationType.FollowRequest,
dist: MisskeyNotificationType.ReceiveFollowRequest
}
]
cases.forEach(c => {
expect(converter.encodeNotificationType(c.src)).toEqual(c.dist)
})
})
})
describe('decode', () => {
it('misskey notification type should be decoded to megalodon notification type', () => {
const cases: Array<{ src: MisskeyEntity.NotificationType; dist: MegalodonEntity.NotificationType }> = [
{
src: MisskeyNotificationType.Follow,
dist: MegalodonNotificationType.Follow
},
{
src: MisskeyNotificationType.Mention,
dist: MegalodonNotificationType.Mention
},
{
src: MisskeyNotificationType.Reply,
dist: MegalodonNotificationType.Mention
},
{
src: MisskeyNotificationType.Renote,
dist: MegalodonNotificationType.Reblog
},
{
src: MisskeyNotificationType.Quote,
dist: MegalodonNotificationType.Reblog
},
{
src: MisskeyNotificationType.Reaction,
dist: MegalodonNotificationType.Reaction
},
{
src: MisskeyNotificationType.PollEnded,
dist: MegalodonNotificationType.Poll
},
{
src: MisskeyNotificationType.ReceiveFollowRequest,
dist: MegalodonNotificationType.FollowRequest
},
{
src: MisskeyNotificationType.FollowRequestAccepted,
dist: MegalodonNotificationType.Follow
}
]
cases.forEach(c => {
expect(converter.decodeNotificationType(c.src)).toEqual(c.dist)
})
})
})
})
describe('reactions', () => {
it('should be mapped', () => {
const misskeyReactions = [
{
id: '1',
createdAt: '2020-04-21T13:04:13.968Z',
user: {
id: '81u70uwsja',
name: 'h3poteto',
username: 'h3poteto',
host: null,
avatarUrl: 'https://s3.arkjp.net/misskey/thumbnail-63807d97-20ca-40ba-9493-179aa48065c1.png',
avatarColor: 'rgb(146,189,195)',
emojis: []
},
type: '❤'
},
{
id: '2',
createdAt: '2020-04-21T13:04:13.968Z',
user: {
id: '81u70uwsja',
name: 'h3poteto',
username: 'h3poteto',
host: null,
avatarUrl: 'https://s3.arkjp.net/misskey/thumbnail-63807d97-20ca-40ba-9493-179aa48065c1.png',
avatarColor: 'rgb(146,189,195)',
emojis: []
},
type: '❤'
},
{
id: '3',
createdAt: '2020-04-21T13:04:13.968Z',
user: {
id: '81u70uwsja',
name: 'h3poteto',
username: 'h3poteto',
host: null,
avatarUrl: 'https://s3.arkjp.net/misskey/thumbnail-63807d97-20ca-40ba-9493-179aa48065c1.png',
avatarColor: 'rgb(146,189,195)',
emojis: []
},
type: '☺'
},
{
id: '4',
createdAt: '2020-04-21T13:04:13.968Z',
user: {
id: '81u70uwsja',
name: 'h3poteto',
username: 'h3poteto',
host: null,
avatarUrl: 'https://s3.arkjp.net/misskey/thumbnail-63807d97-20ca-40ba-9493-179aa48065c1.png',
avatarColor: 'rgb(146,189,195)',
emojis: []
},
type: '❤'
}
]
const reactions = converter.reactions(misskeyReactions)
expect(reactions).toEqual([
{
count: 3,
me: false,
name: '❤'
},
{
count: 1,
me: false,
name: '☺'
}
])
})
})
describe('status', () => {
describe('plain content', () => {
it('should be exported plain content and html content', () => {
const plainContent = 'hoge\nfuga\nfuga'
const content = 'hoge<br>fuga<br>fuga'
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: {},
emojis: [],
fileIds: [],
files: [],
replyId: null,
renoteId: null
}
const megalodonStatus = converter.note(note, user.host || 'misskey.io')
expect(megalodonStatus.plain_content).toEqual(plainContent)
expect(megalodonStatus.content).toEqual(content)
})
it('html tags should be escaped', () => {
const plainContent = '<p>hoge\nfuga\nfuga<p>'
const content = '&lt;p&gt;hoge<br>fuga<br>fuga&lt;p&gt;'
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: {},
emojis: [],
fileIds: [],
files: [],
replyId: null,
renoteId: null
}
const megalodonStatus = converter.note(note, user.host || 'misskey.io')
expect(megalodonStatus.plain_content).toEqual(plainContent)
expect(megalodonStatus.content).toEqual(content)
})
})
})
})

View file

@ -0,0 +1,152 @@
import { Parser } from '@/parser'
import Entity from '@/entity'
const account: Entity.Account = {
id: '1',
username: 'h3poteto',
acct: 'h3poteto@pleroma.io',
display_name: 'h3poteto',
locked: false,
created_at: '2019-03-26T21:30:32',
followers_count: 10,
following_count: 10,
statuses_count: 100,
note: 'engineer',
url: 'https://pleroma.io',
avatar: '',
avatar_static: '',
header: '',
header_static: '',
emojis: [],
moved: null,
fields: [],
bot: false
}
const status: Entity.Status = {
id: '1',
uri: 'http://example.com',
url: 'http://example.com',
account: account,
in_reply_to_id: null,
in_reply_to_account_id: null,
reblog: null,
content: 'hoge',
plain_content: 'hoge',
created_at: '2019-03-26T21:40:32',
emojis: [],
replies_count: 0,
reblogs_count: 0,
favourites_count: 0,
reblogged: null,
favourited: null,
muted: null,
sensitive: false,
spoiler_text: '',
visibility: 'public',
media_attachments: [],
mentions: [],
tags: [],
card: null,
poll: null,
application: {
name: 'Web'
} as Entity.Application,
language: null,
pinned: null,
reactions: [],
bookmarked: false,
quote: null
}
const notification: Entity.Notification = {
id: '1',
account: account,
status: status,
type: 'favourite',
created_at: '2019-04-01T17:01:32'
}
const conversation: Entity.Conversation = {
id: '1',
accounts: [account],
last_status: status,
unread: true
}
describe('Parser', () => {
let parser: Parser
beforeEach(() => {
parser = new Parser()
})
describe('parse', () => {
describe('message is heartbeat', () => {
const message: string = ':thump\n'
it('should be called', () => {
const spy = jest.fn()
parser.on('heartbeat', spy)
parser.parse(message)
expect(spy).toHaveBeenLastCalledWith({})
})
})
describe('message is not json', () => {
describe('event is delete', () => {
const message = `event: delete\ndata: 12asdf34\n\n`
it('should be called', () => {
const spy = jest.fn()
parser.once('delete', spy)
parser.parse(message)
expect(spy).toHaveBeenCalledWith('12asdf34')
})
})
describe('event is not delete', () => {
const message = `event: event\ndata: 12asdf34\n\n`
it('should be error', () => {
const error = jest.fn()
const deleted = jest.fn()
parser.once('error', error)
parser.once('delete', deleted)
parser.parse(message)
expect(error).toHaveBeenCalled()
expect(deleted).not.toHaveBeenCalled()
})
})
})
describe('message is json', () => {
describe('event is update', () => {
const message = `event: update\ndata: ${JSON.stringify(status)}\n\n`
it('should be called', () => {
const spy = jest.fn()
parser.once('update', spy)
parser.parse(message)
expect(spy).toHaveBeenCalledWith(status)
})
})
describe('event is notification', () => {
const message = `event: notification\ndata: ${JSON.stringify(notification)}\n\n`
it('should be called', () => {
const spy = jest.fn()
parser.once('notification', spy)
parser.parse(message)
expect(spy).toHaveBeenCalledWith(notification)
})
})
describe('event is conversation', () => {
const message = `event: conversation\ndata: ${JSON.stringify(conversation)}\n\n`
it('should be called', () => {
const spy = jest.fn()
parser.once('conversation', spy)
parser.parse(message)
expect(spy).toHaveBeenCalledWith(conversation)
})
})
})
})
})