Merge pull request #621 from h3poteto/iss-587

closes #587 Define notification types
This commit is contained in:
AkiraFukushima 2021-02-07 21:27:51 +09:00 committed by GitHub
commit 5fe522e4e1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
22 changed files with 964 additions and 69 deletions

View file

@ -11,5 +11,5 @@ namespace Entity {
type: NotificationType
}
export type NotificationType = 'mention' | 'reblog' | 'favourite' | 'follow' | 'poll' | 'emoji_reaction' | 'follow_request'
export type NotificationType = string
}

View file

@ -2,11 +2,12 @@ import Response from './response'
import OAuth from './oauth'
import { isCancel, RequestCanceledError } from './cancel'
import { ProxyConfig } from './proxy_config'
import generator, { detector, MegalodonInterface, WebSocketInterface, StreamListenerInterface, NotificationType } from './megalodon'
import generator, { detector, MegalodonInterface, WebSocketInterface, StreamListenerInterface } from './megalodon'
import Mastodon from './mastodon'
import Pleroma from './pleroma'
import Misskey from './misskey'
import Entity from './entity'
import NotificationType from './notification'
export {
Response,

View file

@ -2,7 +2,7 @@ import { OAuth2 } from 'oauth'
import MastodonAPI from './mastodon/api_client'
import WebSocket from './mastodon/web_socket'
import { MegalodonInterface, StreamListenerInterface, NoImplementedError, NotificationType } from './megalodon'
import { MegalodonInterface, StreamListenerInterface, NoImplementedError } from './megalodon'
import Response from './response'
import Entity from './entity'
import { NO_REDIRECT, DEFAULT_SCOPE, DEFAULT_UA } from './default'
@ -1803,7 +1803,7 @@ export default class Mastodon implements MegalodonInterface {
max_id?: string
since_id?: string
min_id?: string
exclude_types?: Array<NotificationType>
exclude_types?: Array<Entity.NotificationType>
account_id?: string
}): Promise<Response<Array<Entity.Notification>>> {
let params = {}
@ -1830,7 +1830,7 @@ export default class Mastodon implements MegalodonInterface {
}
if (options.exclude_types) {
params = Object.assign(params, {
exclude_types: options.exclude_types
exclude_types: options.exclude_types.map(e => MastodonAPI.Converter.encodeNotificationType(e))
})
}
if (options.account_id) {

View file

@ -8,6 +8,8 @@ import proxyAgent, { ProxyConfig } from '../proxy_config'
import { NO_REDIRECT, DEFAULT_SCOPE, DEFAULT_UA } from '../default'
import MastodonEntity from './entity'
import MegalodonEntity from '../entity'
import NotificationType from '../notification'
import MastodonNotificationType from './notification'
namespace MastodonAPI {
/**
@ -354,6 +356,44 @@ namespace MastodonAPI {
}
export namespace Converter {
export const encodeNotificationType = (t: MegalodonEntity.NotificationType): MastodonEntity.NotificationType => {
switch (t) {
case NotificationType.Follow:
return MastodonNotificationType.Follow
case NotificationType.Favourite:
return MastodonNotificationType.Favourite
case NotificationType.Reblog:
return MastodonNotificationType.Reblog
case NotificationType.Mention:
return MastodonNotificationType.Mention
case NotificationType.Poll:
return MastodonNotificationType.Poll
case NotificationType.FollowRequest:
return MastodonNotificationType.FollowRequest
default:
return t
}
}
export const decodeNotificationType = (t: MastodonEntity.NotificationType): MegalodonEntity.NotificationType => {
switch (t) {
case MastodonNotificationType.Follow:
return NotificationType.Follow
case MastodonNotificationType.Favourite:
return NotificationType.Favourite
case MastodonNotificationType.Mention:
return NotificationType.Mention
case MastodonNotificationType.Reblog:
return NotificationType.Reblog
case MastodonNotificationType.Poll:
return NotificationType.Poll
case MastodonNotificationType.FollowRequest:
return NotificationType.FollowRequest
default:
return t
}
}
export const account = (a: Entity.Account): MegalodonEntity.Account => a
export const activity = (a: Entity.Activity): MegalodonEntity.Activity => a
export const application = (a: Entity.Application): MegalodonEntity.Application => a
@ -386,14 +426,14 @@ namespace MastodonAPI {
created_at: n.created_at,
id: n.id,
status: status(n.status),
type: n.type
type: decodeNotificationType(n.type)
}
} else {
return {
account: account(n.account),
created_at: n.created_at,
id: n.id,
type: n.type
type: decodeNotificationType(n.type)
}
}
}

View file

@ -10,5 +10,5 @@ namespace MastodonEntity {
type: NotificationType
}
export type NotificationType = 'mention' | 'reblog' | 'favourite' | 'follow' | 'poll' | 'follow_request'
export type NotificationType = string
}

View file

@ -0,0 +1,12 @@
import MastodonEntity from './entity'
namespace MastodonNotificationType {
export const Mention: MastodonEntity.NotificationType = 'mention'
export const Reblog: MastodonEntity.NotificationType = 'reblog'
export const Favourite: MastodonEntity.NotificationType = 'favourite'
export const Follow: MastodonEntity.NotificationType = 'follow'
export const Poll: MastodonEntity.NotificationType = 'poll'
export const FollowRequest: MastodonEntity.NotificationType = 'follow_request'
}
export default MastodonNotificationType

View file

@ -1047,7 +1047,7 @@ export interface MegalodonInterface {
max_id?: string
since_id?: string
min_id?: string
exclude_types?: Array<NotificationType>
exclude_types?: Array<Entity.NotificationType>
account_id?: string
}): Promise<Response<Array<Entity.Notification>>>
/**
@ -1325,13 +1325,4 @@ const generator = (
}
}
export enum NotificationType {
Follow = 'follow',
Favourite = 'favourite',
Reblog = 'reblog',
Mention = 'mention',
Poll = 'poll',
EmojiReaction = 'emoji_reaction'
}
export default generator

View file

@ -9,8 +9,7 @@ import {
WebSocketInterface,
NoImplementedError,
ArgumentError,
UnexpectedError,
NotificationType
UnexpectedError
} from './megalodon'
export default class Misskey implements MegalodonInterface {
@ -1735,7 +1734,7 @@ export default class Misskey implements MegalodonInterface {
max_id?: string
since_id?: string
min_id?: string
exclude_type?: Array<NotificationType>
exclude_type?: Array<Entity.NotificationType>
account_id?: string
}): Promise<Response<Array<Entity.Notification>>> {
let params = {}

View file

@ -6,6 +6,8 @@ import Response from '../response'
import MisskeyEntity from './entity'
import MegalodonEntity from '../entity'
import WebSocket from './web_socket'
import MisskeyNotificationType from './notification'
import NotificationType from '../notification'
namespace MisskeyAPI {
export namespace Entity {
@ -287,42 +289,44 @@ namespace MisskeyAPI {
export const encodeNotificationType = (e: MegalodonEntity.NotificationType): MisskeyEntity.NotificationType => {
switch (e) {
case 'follow':
return 'follow'
case 'mention':
return 'reply'
case 'favourite':
case 'emoji_reaction':
return 'reaction'
case 'reblog':
return 'renote'
case 'poll':
return 'pollVote'
case 'follow_request':
return 'receiveFollowRequest'
case NotificationType.Follow:
return MisskeyNotificationType.Follow
case NotificationType.Mention:
return MisskeyNotificationType.Reply
case NotificationType.Favourite:
case NotificationType.EmojiReaction:
return MisskeyNotificationType.Reaction
case NotificationType.Reblog:
return MisskeyNotificationType.Renote
case NotificationType.Poll:
return MisskeyNotificationType.PollVote
case NotificationType.FollowRequest:
return MisskeyNotificationType.ReceiveFollowRequest
default:
return e
}
}
export const decodeNotificationType = (e: MisskeyEntity.NotificationType): MegalodonEntity.NotificationType => {
switch (e) {
case 'follow':
return 'follow'
case 'mention':
case 'reply':
return 'mention'
case 'renote':
case 'quote':
return 'reblog'
case 'reaction':
return 'emoji_reaction'
case 'pollVote':
return 'poll'
case 'receiveFollowRequest':
return 'follow_request'
case 'followRequestAccepted':
return 'follow'
case MisskeyNotificationType.Follow:
return NotificationType.Follow
case MisskeyNotificationType.Mention:
case MisskeyNotificationType.Reply:
return NotificationType.Mention
case MisskeyNotificationType.Renote:
case MisskeyNotificationType.Quote:
return NotificationType.Reblog
case MisskeyNotificationType.Reaction:
return NotificationType.EmojiReaction
case MisskeyNotificationType.PollVote:
return NotificationType.Poll
case MisskeyNotificationType.ReceiveFollowRequest:
return NotificationType.FollowRequest
case MisskeyNotificationType.FollowRequestAccepted:
return NotificationType.Follow
default:
return 'follow'
return e
}
}

View file

@ -13,15 +13,5 @@ namespace MisskeyEntity {
reaction?: string
}
export type NotificationType =
| 'follow'
| 'mention'
| 'reply'
| 'renote'
| 'quote'
| 'reaction'
| 'pollVote'
| 'receiveFollowRequest'
| 'followRequestAccepted'
| 'groupInvited'
export type NotificationType = string
}

View file

@ -0,0 +1,16 @@
import MisskeyEntity from './entity'
namespace MisskeyNotificationType {
export const Follow: MisskeyEntity.NotificationType = 'follow'
export const Mention: MisskeyEntity.NotificationType = 'mention'
export const Reply: MisskeyEntity.NotificationType = 'reply'
export const Renote: MisskeyEntity.NotificationType = 'renote'
export const Quote: MisskeyEntity.NotificationType = 'quote'
export const Reaction: MisskeyEntity.NotificationType = 'reaction'
export const PollVote: MisskeyEntity.NotificationType = 'pollVote'
export const ReceiveFollowRequest: MisskeyEntity.NotificationType = 'receiveFollowRequest'
export const FollowRequestAccepted: MisskeyEntity.NotificationType = 'followRequestAccepted'
export const GroupInvited: MisskeyEntity.NotificationType = 'groupInvited'
}
export default MisskeyNotificationType

13
src/notification.ts Normal file
View file

@ -0,0 +1,13 @@
import Entity from './entity'
namespace NotificationType {
export const Follow: Entity.NotificationType = 'follow'
export const Favourite: Entity.NotificationType = 'favourite'
export const Reblog: Entity.NotificationType = 'reblog'
export const Mention: Entity.NotificationType = 'mention'
export const Poll: Entity.NotificationType = 'poll'
export const EmojiReaction: Entity.NotificationType = 'emoji_reaction'
export const FollowRequest: Entity.NotificationType = 'follow_request'
}
export default NotificationType

View file

@ -2,7 +2,7 @@ import { OAuth2 } from 'oauth'
import PleromaAPI from './pleroma/api_client'
import WebSocket from './pleroma/web_socket'
import { MegalodonInterface, StreamListenerInterface, NoImplementedError, NotificationType } from './megalodon'
import { MegalodonInterface, StreamListenerInterface, NoImplementedError } from './megalodon'
import Response from './response'
import Entity from './entity'
import { NO_REDIRECT, DEFAULT_SCOPE, DEFAULT_UA } from './default'
@ -1818,7 +1818,7 @@ export default class Pleroma implements MegalodonInterface {
max_id?: string
since_id?: string
min_id?: string
exclude_types?: Array<NotificationType>
exclude_types?: Array<Entity.NotificationType>
account_id?: string
}): Promise<Response<Array<Entity.Notification>>> {
let params = {}

View file

@ -7,6 +7,8 @@ import { RequestCanceledError } from '../cancel'
import proxyAgent, { ProxyConfig } from '../proxy_config'
import { NO_REDIRECT, DEFAULT_SCOPE, DEFAULT_UA } from '../default'
import WebSocket from './web_socket'
import NotificationType from '../notification'
import PleromaNotificationType from './notification'
namespace PleromaAPI {
export namespace Entity {
@ -49,16 +51,40 @@ namespace PleromaAPI {
export namespace Converter {
export const decodeNotificationType = (t: PleromaEntity.NotificationType): MegalodonEntity.NotificationType => {
switch (t) {
case 'pleroma:emoji_reaction':
return 'emoji_reaction'
case PleromaNotificationType.Mention:
return NotificationType.Mention
case PleromaNotificationType.Reblog:
return NotificationType.Reblog
case PleromaNotificationType.Favourite:
return NotificationType.Favourite
case PleromaNotificationType.Follow:
return NotificationType.Follow
case PleromaNotificationType.Poll:
return NotificationType.Poll
case PleromaNotificationType.PleromaEmojiReaction:
return NotificationType.EmojiReaction
case PleromaNotificationType.FollowRequest:
return NotificationType.FollowRequest
default:
return t
}
}
export const encodeNotificationType = (t: MegalodonEntity.NotificationType): PleromaEntity.NotificationType => {
switch (t) {
case 'emoji_reaction':
return 'pleroma:emoji_reaction'
case NotificationType.Follow:
return PleromaNotificationType.Follow
case NotificationType.Favourite:
return PleromaNotificationType.Favourite
case NotificationType.Reblog:
return PleromaNotificationType.Reblog
case NotificationType.Mention:
return PleromaNotificationType.Mention
case NotificationType.Poll:
return PleromaNotificationType.Poll
case NotificationType.EmojiReaction:
return PleromaNotificationType.PleromaEmojiReaction
case NotificationType.FollowRequest:
return PleromaNotificationType.FollowRequest
default:
return t
}

View file

@ -11,5 +11,5 @@ namespace PleromaEntity {
type: NotificationType
}
export type NotificationType = 'mention' | 'reblog' | 'favourite' | 'follow' | 'poll' | 'pleroma:emoji_reaction' | 'follow_request'
export type NotificationType = string
}

View file

@ -0,0 +1,13 @@
import PleromaEntity from './entity'
namespace PleromaNotificationType {
export const Mention: PleromaEntity.NotificationType = 'mention'
export const Reblog: PleromaEntity.NotificationType = 'reblog'
export const Favourite: PleromaEntity.NotificationType = 'favourite'
export const Follow: PleromaEntity.NotificationType = 'follow'
export const Poll: PleromaEntity.NotificationType = 'poll'
export const PleromaEmojiReaction: PleromaEntity.NotificationType = 'pleroma:emoji_reaction'
export const FollowRequest: PleromaEntity.NotificationType = 'follow_request'
}
export default PleromaNotificationType

View file

@ -0,0 +1,163 @@
import MastodonEntity from '@/mastodon/entity'
import MastodonNotificationType from '@/mastodon/notification'
import Mastodon from '@/mastodon'
import MegalodonNotificationType from '@/notification'
import axios, { AxiosResponse } from 'axios'
jest.mock('axios')
const account: MastodonEntity.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: null,
bot: false
}
const status: MastodonEntity.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',
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 MastodonEntity.Application,
language: null,
pinned: null,
bookmarked: false
}
const follow: MastodonEntity.Notification = {
account: account,
created_at: '2021-01-31T23:33:26',
id: '1',
type: MastodonNotificationType.Follow
}
const favourite: MastodonEntity.Notification = {
account: account,
created_at: '2021-01-31T23:33:26',
id: '2',
status: status,
type: MastodonNotificationType.Favourite
}
const mention: MastodonEntity.Notification = {
account: account,
created_at: '2021-01-31T23:33:26',
id: '3',
status: status,
type: MastodonNotificationType.Mention
}
const reblog: MastodonEntity.Notification = {
account: account,
created_at: '2021-01-31T23:33:26',
id: '4',
status: status,
type: MastodonNotificationType.Reblog
}
const poll: MastodonEntity.Notification = {
account: account,
created_at: '2021-01-31T23:33:26',
id: '5',
type: MastodonNotificationType.Poll
}
const followRequest: MastodonEntity.Notification = {
account: account,
created_at: '2021-01-31T23:33:26',
id: '6',
type: MastodonNotificationType.FollowRequest
}
;(axios.CancelToken.source as any).mockImplementation(() => {
return {
token: 'cancelToken'
}
})
describe('getNotifications', () => {
const client = new Mastodon('http://localhost', 'sample token')
const cases: Array<{ event: MastodonEntity.Notification; expected: Entity.NotificationType; title: string }> = [
{
event: follow,
expected: MegalodonNotificationType.Follow,
title: 'follow'
},
{
event: favourite,
expected: MegalodonNotificationType.Favourite,
title: 'favourite'
},
{
event: mention,
expected: MegalodonNotificationType.Mention,
title: 'mention'
},
{
event: reblog,
expected: MegalodonNotificationType.Reblog,
title: 'reblog'
},
{
event: poll,
expected: MegalodonNotificationType.Poll,
title: 'poll'
},
{
event: followRequest,
expected: MegalodonNotificationType.FollowRequest,
title: 'followRequest'
}
]
cases.forEach(c => {
it(`should be ${c.title} event`, async () => {
const mockResponse: AxiosResponse<Array<MastodonEntity.Notification>> = {
data: [c.event],
status: 200,
statusText: '200OK',
headers: [],
config: {}
}
;(axios.get as any).mockResolvedValue(mockResponse)
const res = await client.getNotifications()
expect(res.data[0].type).toEqual(c.expected)
})
})
})

View file

@ -0,0 +1,198 @@
import MisskeyEntity from '@/misskey/entity'
import MisskeyNotificationType from '@/misskey/notification'
import Misskey from '@/misskey'
import MegalodonNotificationType from '@/notification'
import axios, { AxiosResponse } from 'axios'
jest.mock('axios')
const user: MisskeyEntity.User = {
id: '1',
name: 'test_user',
username: 'TestUser',
host: 'misskey.io',
avatarUrl: 'https://example.com/icon.png',
avatarColor: '#000000',
emojis: []
}
const note: MisskeyEntity.Note = {
id: '1',
createdAt: '2021-02-01T01:49:29',
userId: '1',
user: user,
text: 'hogehoge',
cw: null,
visibility: 'public',
renoteCount: 0,
repliesCount: 0,
reactions: {},
emojis: [],
fileIds: [],
files: [],
replyId: null,
renoteId: null
}
const follow: MisskeyEntity.Notification = {
id: '1',
createdAt: '2021-02-01T01:49:29',
userId: user.id,
user: user,
type: MisskeyNotificationType.Follow
}
const mention: MisskeyEntity.Notification = {
id: '1',
createdAt: '2021-02-01T01:49:29',
userId: user.id,
user: user,
type: MisskeyNotificationType.Mention,
note: note
}
const reply: MisskeyEntity.Notification = {
id: '1',
createdAt: '2021-02-01T01:49:29',
userId: user.id,
user: user,
type: MisskeyNotificationType.Reply,
note: note
}
const renote: MisskeyEntity.Notification = {
id: '1',
createdAt: '2021-02-01T01:49:29',
userId: user.id,
user: user,
type: MisskeyNotificationType.Renote,
note: note
}
const quote: MisskeyEntity.Notification = {
id: '1',
createdAt: '2021-02-01T01:49:29',
userId: user.id,
user: user,
type: MisskeyNotificationType.Quote,
note: note
}
const reaction: MisskeyEntity.Notification = {
id: '1',
createdAt: '2021-02-01T01:49:29',
userId: user.id,
user: user,
type: MisskeyNotificationType.Reaction,
note: note,
reaction: '♥'
}
const pollVote: MisskeyEntity.Notification = {
id: '1',
createdAt: '2021-02-01T01:49:29',
userId: user.id,
user: user,
type: MisskeyNotificationType.PollVote,
note: note
}
const receiveFollowRequest: MisskeyEntity.Notification = {
id: '1',
createdAt: '2021-02-01T01:49:29',
userId: user.id,
user: user,
type: MisskeyNotificationType.ReceiveFollowRequest
}
const followRequestAccepted: MisskeyEntity.Notification = {
id: '1',
createdAt: '2021-02-01T01:49:29',
userId: user.id,
user: user,
type: MisskeyNotificationType.FollowRequestAccepted
}
const groupInvited: MisskeyEntity.Notification = {
id: '1',
createdAt: '2021-02-01T01:49:29',
userId: user.id,
user: user,
type: MisskeyNotificationType.GroupInvited
}
;(axios.CancelToken.source as any).mockImplementation(() => {
return {
token: 'cancelToken'
}
})
describe('getNotifications', () => {
const client = new Misskey('http://localhost', 'sample token')
const cases: Array<{ event: MisskeyEntity.Notification; expected: Entity.NotificationType; title: string }> = [
{
event: follow,
expected: MegalodonNotificationType.Follow,
title: 'follow'
},
{
event: mention,
expected: MegalodonNotificationType.Mention,
title: 'mention'
},
{
event: reply,
expected: MegalodonNotificationType.Mention,
title: 'reply'
},
{
event: renote,
expected: MegalodonNotificationType.Reblog,
title: 'renote'
},
{
event: quote,
expected: MegalodonNotificationType.Reblog,
title: 'quote'
},
{
event: reaction,
expected: MegalodonNotificationType.EmojiReaction,
title: 'reaction'
},
{
event: pollVote,
expected: MegalodonNotificationType.Poll,
title: 'pollVote'
},
{
event: receiveFollowRequest,
expected: MegalodonNotificationType.FollowRequest,
title: 'receiveFollowRequest'
},
{
event: followRequestAccepted,
expected: MegalodonNotificationType.Follow,
title: 'followRequestAccepted'
},
{
event: groupInvited,
expected: MisskeyNotificationType.GroupInvited,
title: 'groupInvited'
}
]
cases.forEach(c => {
it(`should be ${c.title} event`, async () => {
const mockResponse: AxiosResponse<Array<MisskeyEntity.Notification>> = {
data: [c.event],
status: 200,
statusText: '200OK',
headers: [],
config: {}
}
;(axios.post as any).mockResolvedValue(mockResponse)
const res = await client.getNotifications()
expect(res.data[0].type).toEqual(c.expected)
})
})
})

View file

@ -0,0 +1,181 @@
import PleromaEntity from '@/pleroma/entity'
import Pleroma from '@/pleroma'
import MegalodonNotificationType from '@/notification'
import PleromaNotificationType from '@/pleroma/notification'
import axios, { AxiosResponse } from 'axios'
jest.mock('axios')
const account: PleromaEntity.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: null,
bot: false
}
const status: PleromaEntity.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',
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 MastodonEntity.Application,
language: null,
pinned: null,
bookmarked: false,
pleroma: {
local: false
}
}
const follow: PleromaEntity.Notification = {
account: account,
created_at: '2021-01-31T23:33:26',
id: '1',
type: PleromaNotificationType.Follow
}
const favourite: PleromaEntity.Notification = {
account: account,
created_at: '2021-01-31T23:33:26',
id: '2',
type: PleromaNotificationType.Favourite,
status: status
}
const mention: PleromaEntity.Notification = {
account: account,
created_at: '2021-01-31T23:33:26',
id: '3',
type: PleromaNotificationType.Mention,
status: status
}
const reblog: PleromaEntity.Notification = {
account: account,
created_at: '2021-01-31T23:33:26',
id: '4',
type: PleromaNotificationType.Reblog,
status: status
}
const poll: PleromaEntity.Notification = {
account: account,
created_at: '2021-01-31T23:33:26',
id: '5',
type: PleromaNotificationType.Poll,
status: status
}
const emojiReaction: PleromaEntity.Notification = {
account: account,
created_at: '2021-01-31T23:33:26',
id: '6',
type: PleromaNotificationType.PleromaEmojiReaction,
status: status,
emoji: '♥'
}
const followRequest: PleromaEntity.Notification = {
account: account,
created_at: '2021-01-31T23:33:26',
id: '7',
type: PleromaNotificationType.FollowRequest
}
;(axios.CancelToken.source as any).mockImplementation(() => {
return {
token: 'cancelToken'
}
})
describe('getNotifications', () => {
const client = new Pleroma('http://localhost', 'sample token')
const cases: Array<{ event: PleromaEntity.Notification; expected: Entity.NotificationType; title: string }> = [
{
event: follow,
expected: MegalodonNotificationType.Follow,
title: 'follow'
},
{
event: favourite,
expected: MegalodonNotificationType.Favourite,
title: 'favourite'
},
{
event: mention,
expected: MegalodonNotificationType.Mention,
title: 'mention'
},
{
event: reblog,
expected: MegalodonNotificationType.Reblog,
title: 'reblog'
},
{
event: poll,
expected: MegalodonNotificationType.Poll,
title: 'poll'
},
{
event: emojiReaction,
expected: MegalodonNotificationType.EmojiReaction,
title: 'emojiReaction'
},
{
event: followRequest,
expected: MegalodonNotificationType.FollowRequest,
title: 'followRequest'
}
]
cases.forEach(c => {
it(`should be ${c.title} event`, async () => {
const mockResponse: AxiosResponse<Array<PleromaEntity.Notification>> = {
data: [c.event],
status: 200,
statusText: '200OK',
headers: [],
config: {}
}
;(axios.get as any).mockResolvedValue(mockResponse)
const res = await client.getNotifications()
expect(res.data[0].type).toEqual(c.expected)
})
})
})

View file

@ -0,0 +1,76 @@
import MastodonAPI from '@/mastodon/api_client'
import MegalodonEntity from '@/entity'
import MastodonEntity from '@/mastodon/entity'
import MegalodonNotificationType from '@/notification'
import MastodonNotificationType from '@/mastodon/notification'
describe('api_client', () => {
describe('notification', () => {
describe('encode', () => {
it('megalodon notification type should be encoded to mastodon notification type', () => {
const cases: Array<{ src: MegalodonEntity.NotificationType; dist: MastodonEntity.NotificationType }> = [
{
src: MegalodonNotificationType.Follow,
dist: MastodonNotificationType.Follow
},
{
src: MegalodonNotificationType.Favourite,
dist: MastodonNotificationType.Favourite
},
{
src: MegalodonNotificationType.Reblog,
dist: MastodonNotificationType.Reblog
},
{
src: MegalodonNotificationType.Mention,
dist: MastodonNotificationType.Mention
},
{
src: MegalodonNotificationType.Poll,
dist: MastodonNotificationType.Poll
},
{
src: MegalodonNotificationType.FollowRequest,
dist: MastodonNotificationType.FollowRequest
}
]
cases.forEach(c => {
expect(MastodonAPI.Converter.encodeNotificationType(c.src)).toEqual(c.dist)
})
})
})
describe('decode', () => {
it('mastodon notification type should be decoded to megalodon notification type', () => {
const cases: Array<{ src: MastodonEntity.NotificationType; dist: MegalodonEntity.NotificationType }> = [
{
src: MastodonNotificationType.Follow,
dist: MegalodonNotificationType.Follow
},
{
src: MastodonNotificationType.Favourite,
dist: MegalodonNotificationType.Favourite
},
{
src: MastodonNotificationType.Mention,
dist: MegalodonNotificationType.Mention
},
{
src: MastodonNotificationType.Reblog,
dist: MegalodonNotificationType.Reblog
},
{
src: MastodonNotificationType.Poll,
dist: MegalodonNotificationType.Poll
},
{
src: MastodonNotificationType.FollowRequest,
dist: MegalodonNotificationType.FollowRequest
}
]
cases.forEach(c => {
expect(MastodonAPI.Converter.decodeNotificationType(c.src)).toEqual(c.dist)
})
})
})
})
})

View file

@ -1,6 +1,94 @@
import MisskeyAPI from '@/misskey/api_client'
import MegalodonEntity from '@/entity'
import MisskeyEntity from '@/misskey/entity'
import MegalodonNotificationType from '@/notification'
import MisskeyNotificationType from '@/misskey/notification'
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.EmojiReaction,
dist: MisskeyNotificationType.Reaction
},
{
src: MegalodonNotificationType.Reblog,
dist: MisskeyNotificationType.Renote
},
{
src: MegalodonNotificationType.Poll,
dist: MisskeyNotificationType.PollVote
},
{
src: MegalodonNotificationType.FollowRequest,
dist: MisskeyNotificationType.ReceiveFollowRequest
}
]
cases.forEach(c => {
expect(MisskeyAPI.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.EmojiReaction
},
{
src: MisskeyNotificationType.PollVote,
dist: MegalodonNotificationType.Poll
},
{
src: MisskeyNotificationType.ReceiveFollowRequest,
dist: MegalodonNotificationType.FollowRequest
},
{
src: MisskeyNotificationType.FollowRequestAccepted,
dist: MegalodonNotificationType.Follow
}
]
cases.forEach(c => {
expect(MisskeyAPI.Converter.decodeNotificationType(c.src)).toEqual(c.dist)
})
})
})
})
describe('reactions', () => {
it('should be mapped', () => {
const misskeyReactions = [

View file

@ -0,0 +1,84 @@
import PleromaAPI from '@/pleroma/api_client'
import MegalodonEntity from '@/entity'
import PleromaEntity from '@/pleroma/entity'
import MegalodonNotificationType from '@/notification'
import PleromaNotificationType from '@/pleroma/notification'
describe('api_client', () => {
describe('notification', () => {
describe('encode', () => {
it('megalodon notification type should be encoded to pleroma notification type', () => {
const cases: Array<{ src: MegalodonEntity.NotificationType; dist: PleromaEntity.NotificationType }> = [
{
src: MegalodonNotificationType.Follow,
dist: PleromaNotificationType.Follow
},
{
src: MegalodonNotificationType.Favourite,
dist: PleromaNotificationType.Favourite
},
{
src: MegalodonNotificationType.Reblog,
dist: PleromaNotificationType.Reblog
},
{
src: MegalodonNotificationType.Mention,
dist: PleromaNotificationType.Mention
},
{
src: MegalodonNotificationType.Poll,
dist: PleromaNotificationType.Poll
},
{
src: MegalodonNotificationType.EmojiReaction,
dist: PleromaNotificationType.PleromaEmojiReaction
},
{
src: MegalodonNotificationType.FollowRequest,
dist: PleromaNotificationType.FollowRequest
}
]
cases.forEach(c => {
expect(PleromaAPI.Converter.encodeNotificationType(c.src)).toEqual(c.dist)
})
})
})
describe('decode', () => {
it('pleroma notification type should be decoded to megalodon notification type', () => {
const cases: Array<{ src: PleromaEntity.NotificationType; dist: MegalodonEntity.NotificationType }> = [
{
src: PleromaNotificationType.Follow,
dist: MegalodonNotificationType.Follow
},
{
src: PleromaNotificationType.Favourite,
dist: MegalodonNotificationType.Favourite
},
{
src: PleromaNotificationType.Mention,
dist: MegalodonNotificationType.Mention
},
{
src: PleromaNotificationType.Reblog,
dist: MegalodonNotificationType.Reblog
},
{
src: PleromaNotificationType.Poll,
dist: MegalodonNotificationType.Poll
},
{
src: PleromaNotificationType.PleromaEmojiReaction,
dist: MegalodonNotificationType.EmojiReaction
},
{
src: PleromaNotificationType.FollowRequest,
dist: MegalodonNotificationType.FollowRequest
}
]
cases.forEach(c => {
expect(PleromaAPI.Converter.decodeNotificationType(c.src)).toEqual(c.dist)
})
})
})
})
})