mirror of
https://codeberg.org/yeentown/barkey.git
synced 2025-07-07 20:44:34 +00:00
remove unused megalodon components
This commit is contained in:
parent
cb9079208a
commit
75b6c63f44
102 changed files with 30 additions and 14604 deletions
|
@ -3,7 +3,7 @@
|
|||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import { megalodon, MegalodonInterface } from 'megalodon';
|
||||
import { Misskey } from 'megalodon';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { MiLocalUser } from '@/models/User.js';
|
||||
import { AuthenticateService } from '@/server/api/AuthenticateService.js';
|
||||
|
@ -18,7 +18,7 @@ export class MastodonClientService {
|
|||
/**
|
||||
* Gets the authenticated user and API client for a request.
|
||||
*/
|
||||
public async getAuthClient(request: FastifyRequest, accessToken?: string | null): Promise<{ client: MegalodonInterface, me: MiLocalUser | null }> {
|
||||
public async getAuthClient(request: FastifyRequest, accessToken?: string | null): Promise<{ client: Misskey, me: MiLocalUser | null }> {
|
||||
const authorization = request.headers.authorization;
|
||||
accessToken = accessToken !== undefined ? accessToken : getAccessToken(authorization);
|
||||
|
||||
|
@ -41,14 +41,14 @@ export class MastodonClientService {
|
|||
/**
|
||||
* Creates an authenticated API client for a request.
|
||||
*/
|
||||
public getClient(request: FastifyRequest, accessToken?: string | null): MegalodonInterface {
|
||||
public getClient(request: FastifyRequest, accessToken?: string | null): Misskey {
|
||||
const authorization = request.headers.authorization;
|
||||
accessToken = accessToken !== undefined ? accessToken : getAccessToken(authorization);
|
||||
|
||||
// TODO pass agent?
|
||||
const baseUrl = this.getBaseUrl(request);
|
||||
const userAgent = request.headers['user-agent'];
|
||||
return megalodon('misskey', baseUrl, accessToken, userAgent);
|
||||
return new Misskey(baseUrl, accessToken, userAgent);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -10,7 +10,7 @@ namespace Entity {
|
|||
email: string
|
||||
version: string
|
||||
thumbnail: string | null
|
||||
urls: URLs | null
|
||||
urls: URLs
|
||||
stats: Stats
|
||||
languages: Array<string>
|
||||
registrations: boolean
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -1,769 +0,0 @@
|
|||
import axios, { AxiosResponse, AxiosRequestConfig } from 'axios'
|
||||
import objectAssignDeep from 'object-assign-deep'
|
||||
|
||||
import WebSocket from './web_socket'
|
||||
import Response from '../response'
|
||||
import { RequestCanceledError } from '../cancel'
|
||||
import proxyAgent, { ProxyConfig } from '../proxy_config'
|
||||
import { NO_REDIRECT, DEFAULT_SCOPE, DEFAULT_UA } from '../default'
|
||||
import FriendicaEntity from './entity'
|
||||
import MegalodonEntity from '../entity'
|
||||
import NotificationType, { UnknownNotificationTypeError } from '../notification'
|
||||
import FriendicaNotificationType from './notification'
|
||||
|
||||
namespace FriendicaAPI {
|
||||
/**
|
||||
* Interface
|
||||
*/
|
||||
export interface Interface {
|
||||
get<T = any>(path: string, params?: any, headers?: { [key: string]: string }, pathIsFullyQualified?: boolean): Promise<Response<T>>
|
||||
put<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
|
||||
putForm<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
|
||||
patch<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
|
||||
patchForm<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>>
|
||||
postForm<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
|
||||
del<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
|
||||
cancel(): void
|
||||
socket(path: string, stream: string, params?: string): WebSocket
|
||||
}
|
||||
|
||||
/**
|
||||
* Friendica API client.
|
||||
*
|
||||
* Using axios for request, you will handle promises.
|
||||
*/
|
||||
export class Client implements Interface {
|
||||
static DEFAULT_SCOPE = DEFAULT_SCOPE
|
||||
static DEFAULT_URL = 'https://mastodon.social'
|
||||
static NO_REDIRECT = NO_REDIRECT
|
||||
|
||||
private accessToken: string | null
|
||||
private baseUrl: string
|
||||
private userAgent: string
|
||||
private abortController: AbortController
|
||||
private proxyConfig: ProxyConfig | false = false
|
||||
|
||||
/**
|
||||
* @param baseUrl hostname or base URL
|
||||
* @param accessToken access token from OAuth2 authorization
|
||||
* @param userAgent UserAgent is specified in header on request.
|
||||
* @param proxyConfig Proxy setting, or set false if don't use proxy.
|
||||
*/
|
||||
constructor(
|
||||
baseUrl: string,
|
||||
accessToken: string | null = null,
|
||||
userAgent: string = DEFAULT_UA,
|
||||
proxyConfig: ProxyConfig | false = false
|
||||
) {
|
||||
this.accessToken = accessToken
|
||||
this.baseUrl = baseUrl
|
||||
this.userAgent = userAgent
|
||||
this.proxyConfig = proxyConfig
|
||||
this.abortController = new AbortController()
|
||||
axios.defaults.signal = this.abortController.signal
|
||||
}
|
||||
|
||||
/**
|
||||
* GET request to mastodon REST API.
|
||||
* @param path relative path from baseUrl
|
||||
* @param params Query parameters
|
||||
* @param headers Request header object
|
||||
*/
|
||||
public async get<T>(
|
||||
path: string,
|
||||
params = {},
|
||||
headers: { [key: string]: string } = {},
|
||||
pathIsFullyQualified = false
|
||||
): Promise<Response<T>> {
|
||||
let options: AxiosRequestConfig = {
|
||||
params: params,
|
||||
headers: headers,
|
||||
maxContentLength: Infinity,
|
||||
maxBodyLength: Infinity
|
||||
}
|
||||
if (this.accessToken) {
|
||||
options = objectAssignDeep({}, options, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.accessToken}`
|
||||
}
|
||||
})
|
||||
}
|
||||
if (this.proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
httpAgent: proxyAgent(this.proxyConfig),
|
||||
httpsAgent: proxyAgent(this.proxyConfig)
|
||||
})
|
||||
}
|
||||
return axios
|
||||
.get<T>((pathIsFullyQualified ? '' : this.baseUrl) + path, options)
|
||||
.catch((err: Error) => {
|
||||
if (axios.isCancel(err)) {
|
||||
throw new RequestCanceledError(err.message)
|
||||
} else {
|
||||
throw err
|
||||
}
|
||||
})
|
||||
.then((resp: AxiosResponse<T>) => {
|
||||
const res: Response<T> = {
|
||||
data: resp.data,
|
||||
status: resp.status,
|
||||
statusText: resp.statusText,
|
||||
headers: resp.headers
|
||||
}
|
||||
return res
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* PUT request to mastodon REST API.
|
||||
* @param path relative path from baseUrl
|
||||
* @param params Form data. If you want to post file, please use FormData()
|
||||
* @param headers Request header object
|
||||
*/
|
||||
public async put<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
|
||||
let options: AxiosRequestConfig = {
|
||||
headers: headers,
|
||||
maxContentLength: Infinity,
|
||||
maxBodyLength: Infinity
|
||||
}
|
||||
if (this.accessToken) {
|
||||
options = objectAssignDeep({}, options, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.accessToken}`
|
||||
}
|
||||
})
|
||||
}
|
||||
if (this.proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
httpAgent: proxyAgent(this.proxyConfig),
|
||||
httpsAgent: proxyAgent(this.proxyConfig)
|
||||
})
|
||||
}
|
||||
return axios
|
||||
.put<T>(this.baseUrl + path, params, options)
|
||||
.catch((err: Error) => {
|
||||
if (axios.isCancel(err)) {
|
||||
throw new RequestCanceledError(err.message)
|
||||
} else {
|
||||
throw err
|
||||
}
|
||||
})
|
||||
.then((resp: AxiosResponse<T>) => {
|
||||
const res: Response<T> = {
|
||||
data: resp.data,
|
||||
status: resp.status,
|
||||
statusText: resp.statusText,
|
||||
headers: resp.headers
|
||||
}
|
||||
return res
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* PUT request to mastodon REST API for multipart.
|
||||
* @param path relative path from baseUrl
|
||||
* @param params Form data. If you want to post file, please use FormData()
|
||||
* @param headers Request header object
|
||||
*/
|
||||
public async putForm<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
|
||||
let options: AxiosRequestConfig = {
|
||||
headers: headers,
|
||||
maxContentLength: Infinity,
|
||||
maxBodyLength: Infinity
|
||||
}
|
||||
if (this.accessToken) {
|
||||
options = objectAssignDeep({}, options, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.accessToken}`
|
||||
}
|
||||
})
|
||||
}
|
||||
if (this.proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
httpAgent: proxyAgent(this.proxyConfig),
|
||||
httpsAgent: proxyAgent(this.proxyConfig)
|
||||
})
|
||||
}
|
||||
return axios
|
||||
.putForm<T>(this.baseUrl + path, params, options)
|
||||
.catch((err: Error) => {
|
||||
if (axios.isCancel(err)) {
|
||||
throw new RequestCanceledError(err.message)
|
||||
} else {
|
||||
throw err
|
||||
}
|
||||
})
|
||||
.then((resp: AxiosResponse<T>) => {
|
||||
const res: Response<T> = {
|
||||
data: resp.data,
|
||||
status: resp.status,
|
||||
statusText: resp.statusText,
|
||||
headers: resp.headers
|
||||
}
|
||||
return res
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* PATCH request to mastodon REST API.
|
||||
* @param path relative path from baseUrl
|
||||
* @param params Form data. If you want to post file, please use FormData()
|
||||
* @param headers Request header object
|
||||
*/
|
||||
public async patch<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
|
||||
let options: AxiosRequestConfig = {
|
||||
headers: headers,
|
||||
maxContentLength: Infinity,
|
||||
maxBodyLength: Infinity
|
||||
}
|
||||
if (this.accessToken) {
|
||||
options = objectAssignDeep({}, options, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.accessToken}`
|
||||
}
|
||||
})
|
||||
}
|
||||
if (this.proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
httpAgent: proxyAgent(this.proxyConfig),
|
||||
httpsAgent: proxyAgent(this.proxyConfig)
|
||||
})
|
||||
}
|
||||
return axios
|
||||
.patch<T>(this.baseUrl + path, params, options)
|
||||
.catch((err: Error) => {
|
||||
if (axios.isCancel(err)) {
|
||||
throw new RequestCanceledError(err.message)
|
||||
} else {
|
||||
throw err
|
||||
}
|
||||
})
|
||||
.then((resp: AxiosResponse<T>) => {
|
||||
const res: Response<T> = {
|
||||
data: resp.data,
|
||||
status: resp.status,
|
||||
statusText: resp.statusText,
|
||||
headers: resp.headers
|
||||
}
|
||||
return res
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* PATCH request to mastodon REST API for multipart.
|
||||
* @param path relative path from baseUrl
|
||||
* @param params Form data. If you want to post file, please use FormData()
|
||||
* @param headers Request header object
|
||||
*/
|
||||
public async patchForm<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
|
||||
let options: AxiosRequestConfig = {
|
||||
headers: headers,
|
||||
maxContentLength: Infinity,
|
||||
maxBodyLength: Infinity
|
||||
}
|
||||
if (this.accessToken) {
|
||||
options = objectAssignDeep({}, options, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.accessToken}`
|
||||
}
|
||||
})
|
||||
}
|
||||
if (this.proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
httpAgent: proxyAgent(this.proxyConfig),
|
||||
httpsAgent: proxyAgent(this.proxyConfig)
|
||||
})
|
||||
}
|
||||
return axios
|
||||
.patchForm<T>(this.baseUrl + path, params, options)
|
||||
.catch((err: Error) => {
|
||||
if (axios.isCancel(err)) {
|
||||
throw new RequestCanceledError(err.message)
|
||||
} else {
|
||||
throw err
|
||||
}
|
||||
})
|
||||
.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 mastodon REST API.
|
||||
* @param path relative path from baseUrl
|
||||
* @param params Form data
|
||||
* @param headers Request header object
|
||||
*/
|
||||
public async post<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
|
||||
let options: AxiosRequestConfig = {
|
||||
headers: headers,
|
||||
maxContentLength: Infinity,
|
||||
maxBodyLength: Infinity
|
||||
}
|
||||
if (this.accessToken) {
|
||||
options = objectAssignDeep({}, options, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.accessToken}`
|
||||
}
|
||||
})
|
||||
}
|
||||
if (this.proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
httpAgent: proxyAgent(this.proxyConfig),
|
||||
httpsAgent: proxyAgent(this.proxyConfig)
|
||||
})
|
||||
}
|
||||
return axios.post<T>(this.baseUrl + path, params, 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 mastodon REST API for multipart.
|
||||
* @param path relative path from baseUrl
|
||||
* @param params Form data
|
||||
* @param headers Request header object
|
||||
*/
|
||||
public async postForm<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
|
||||
let options: AxiosRequestConfig = {
|
||||
headers: headers,
|
||||
maxContentLength: Infinity,
|
||||
maxBodyLength: Infinity
|
||||
}
|
||||
if (this.accessToken) {
|
||||
options = objectAssignDeep({}, options, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.accessToken}`
|
||||
}
|
||||
})
|
||||
}
|
||||
if (this.proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
httpAgent: proxyAgent(this.proxyConfig),
|
||||
httpsAgent: proxyAgent(this.proxyConfig)
|
||||
})
|
||||
}
|
||||
return axios.postForm<T>(this.baseUrl + path, params, options).then((resp: AxiosResponse<T>) => {
|
||||
const res: Response<T> = {
|
||||
data: resp.data,
|
||||
status: resp.status,
|
||||
statusText: resp.statusText,
|
||||
headers: resp.headers
|
||||
}
|
||||
return res
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* DELETE request to mastodon REST API.
|
||||
* @param path relative path from baseUrl
|
||||
* @param params Form data
|
||||
* @param headers Request header object
|
||||
*/
|
||||
public async del<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
|
||||
let options: AxiosRequestConfig = {
|
||||
data: params,
|
||||
headers: headers,
|
||||
maxContentLength: Infinity,
|
||||
maxBodyLength: Infinity
|
||||
}
|
||||
if (this.accessToken) {
|
||||
options = objectAssignDeep({}, options, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.accessToken}`
|
||||
}
|
||||
})
|
||||
}
|
||||
if (this.proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
httpAgent: proxyAgent(this.proxyConfig),
|
||||
httpsAgent: proxyAgent(this.proxyConfig)
|
||||
})
|
||||
}
|
||||
return axios
|
||||
.delete(this.baseUrl + path, options)
|
||||
.catch((err: Error) => {
|
||||
if (axios.isCancel(err)) {
|
||||
throw new RequestCanceledError(err.message)
|
||||
} else {
|
||||
throw err
|
||||
}
|
||||
})
|
||||
.then((resp: AxiosResponse) => {
|
||||
const res: Response<T> = {
|
||||
data: resp.data,
|
||||
status: resp.status,
|
||||
statusText: resp.statusText,
|
||||
headers: resp.headers
|
||||
}
|
||||
return res
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancel all requests in this instance.
|
||||
* @returns void
|
||||
*/
|
||||
public cancel(): void {
|
||||
return this.abortController.abort()
|
||||
}
|
||||
|
||||
/**
|
||||
* Get connection and receive websocket connection for Pleroma API.
|
||||
*
|
||||
* @param path relative path from baseUrl: normally it is `/streaming`.
|
||||
* @param stream Stream name, please refer: https://git.pleroma.social/pleroma/pleroma/blob/develop/lib/pleroma/web/mastodon_api/mastodon_socket.ex#L19-28
|
||||
* @returns WebSocket, which inherits from EventEmitter
|
||||
*/
|
||||
public socket(path: string, stream: string, params?: string): WebSocket {
|
||||
if (!this.accessToken) {
|
||||
throw new Error('accessToken is required')
|
||||
}
|
||||
const url = this.baseUrl + path
|
||||
const streaming = new WebSocket(url, stream, params, this.accessToken, this.userAgent, this.proxyConfig)
|
||||
process.nextTick(() => {
|
||||
streaming.start()
|
||||
})
|
||||
return streaming
|
||||
}
|
||||
}
|
||||
|
||||
export namespace Entity {
|
||||
export type Account = FriendicaEntity.Account
|
||||
export type Activity = FriendicaEntity.Activity
|
||||
export type Application = FriendicaEntity.Application
|
||||
export type AsyncAttachment = FriendicaEntity.AsyncAttachment
|
||||
export type Attachment = FriendicaEntity.Attachment
|
||||
export type Card = FriendicaEntity.Card
|
||||
export type Context = FriendicaEntity.Context
|
||||
export type Conversation = FriendicaEntity.Conversation
|
||||
export type Emoji = FriendicaEntity.Emoji
|
||||
export type FeaturedTag = FriendicaEntity.FeaturedTag
|
||||
export type Field = FriendicaEntity.Field
|
||||
export type Filter = FriendicaEntity.Filter
|
||||
export type FollowRequest = FriendicaEntity.FollowRequest
|
||||
export type History = FriendicaEntity.History
|
||||
export type IdentityProof = FriendicaEntity.IdentityProof
|
||||
export type Instance = FriendicaEntity.Instance
|
||||
export type List = FriendicaEntity.List
|
||||
export type Marker = FriendicaEntity.Marker
|
||||
export type Mention = FriendicaEntity.Mention
|
||||
export type Notification = FriendicaEntity.Notification
|
||||
export type Poll = FriendicaEntity.Poll
|
||||
export type PollOption = FriendicaEntity.PollOption
|
||||
export type Preferences = FriendicaEntity.Preferences
|
||||
export type PushSubscription = FriendicaEntity.PushSubscription
|
||||
export type Relationship = FriendicaEntity.Relationship
|
||||
export type Report = FriendicaEntity.Report
|
||||
export type Results = FriendicaEntity.Results
|
||||
export type ScheduledStatus = FriendicaEntity.ScheduledStatus
|
||||
export type Source = FriendicaEntity.Source
|
||||
export type Stats = FriendicaEntity.Stats
|
||||
export type Status = FriendicaEntity.Status
|
||||
export type StatusParams = FriendicaEntity.StatusParams
|
||||
export type StatusSource = FriendicaEntity.StatusSource
|
||||
export type Tag = FriendicaEntity.Tag
|
||||
export type Token = FriendicaEntity.Token
|
||||
export type URLs = FriendicaEntity.URLs
|
||||
}
|
||||
|
||||
export namespace Converter {
|
||||
export const encodeNotificationType = (
|
||||
t: MegalodonEntity.NotificationType
|
||||
): FriendicaEntity.NotificationType | UnknownNotificationTypeError => {
|
||||
switch (t) {
|
||||
case NotificationType.Follow:
|
||||
return FriendicaNotificationType.Follow
|
||||
case NotificationType.Favourite:
|
||||
return FriendicaNotificationType.Favourite
|
||||
case NotificationType.Reblog:
|
||||
return FriendicaNotificationType.Reblog
|
||||
case NotificationType.Mention:
|
||||
return FriendicaNotificationType.Mention
|
||||
case NotificationType.FollowRequest:
|
||||
return FriendicaNotificationType.FollowRequest
|
||||
case NotificationType.Status:
|
||||
return FriendicaNotificationType.Status
|
||||
case NotificationType.PollExpired:
|
||||
return FriendicaNotificationType.Poll
|
||||
case NotificationType.Update:
|
||||
return FriendicaNotificationType.Update
|
||||
default:
|
||||
return new UnknownNotificationTypeError()
|
||||
}
|
||||
}
|
||||
|
||||
export const decodeNotificationType = (
|
||||
t: FriendicaEntity.NotificationType
|
||||
): MegalodonEntity.NotificationType | UnknownNotificationTypeError => {
|
||||
switch (t) {
|
||||
case FriendicaNotificationType.Follow:
|
||||
return NotificationType.Follow
|
||||
case FriendicaNotificationType.Favourite:
|
||||
return NotificationType.Favourite
|
||||
case FriendicaNotificationType.Mention:
|
||||
return NotificationType.Mention
|
||||
case FriendicaNotificationType.Reblog:
|
||||
return NotificationType.Reblog
|
||||
case FriendicaNotificationType.FollowRequest:
|
||||
return NotificationType.FollowRequest
|
||||
case FriendicaNotificationType.Status:
|
||||
return NotificationType.Status
|
||||
case FriendicaNotificationType.Poll:
|
||||
return NotificationType.PollExpired
|
||||
case FriendicaNotificationType.Update:
|
||||
return NotificationType.Update
|
||||
default:
|
||||
return new UnknownNotificationTypeError()
|
||||
}
|
||||
}
|
||||
|
||||
export const account = (a: Entity.Account): MegalodonEntity.Account => ({
|
||||
id: a.id,
|
||||
username: a.username,
|
||||
acct: a.acct,
|
||||
display_name: a.display_name,
|
||||
locked: a.locked,
|
||||
discoverable: a.discoverable,
|
||||
group: a.group,
|
||||
noindex: null,
|
||||
suspended: null,
|
||||
limited: null,
|
||||
created_at: a.created_at,
|
||||
followers_count: a.followers_count,
|
||||
following_count: a.following_count,
|
||||
statuses_count: a.statuses_count,
|
||||
note: a.note,
|
||||
url: a.url,
|
||||
avatar: a.avatar,
|
||||
avatar_static: a.avatar_static,
|
||||
header: a.header,
|
||||
header_static: a.header_static,
|
||||
emojis: a.emojis.map(e => emoji(e)),
|
||||
moved: a.moved ? account(a.moved) : null,
|
||||
fields: a.fields.map(f => field(f)),
|
||||
bot: a.bot,
|
||||
source: a.source ? source(a.source) : undefined
|
||||
})
|
||||
export const activity = (a: Entity.Activity): MegalodonEntity.Activity => a
|
||||
export const application = (a: Entity.Application): MegalodonEntity.Application => a
|
||||
export const attachment = (a: Entity.Attachment): MegalodonEntity.Attachment => a
|
||||
export const async_attachment = (a: Entity.AsyncAttachment) => {
|
||||
if (a.url) {
|
||||
return {
|
||||
id: a.id,
|
||||
type: a.type,
|
||||
url: a.url,
|
||||
remote_url: a.remote_url,
|
||||
preview_url: a.preview_url,
|
||||
text_url: a.text_url,
|
||||
meta: a.meta,
|
||||
description: a.description,
|
||||
blurhash: a.blurhash
|
||||
} as MegalodonEntity.Attachment
|
||||
} else {
|
||||
return a as MegalodonEntity.AsyncAttachment
|
||||
}
|
||||
}
|
||||
export const card = (c: Entity.Card): MegalodonEntity.Card => ({
|
||||
url: c.url,
|
||||
title: c.title,
|
||||
description: c.description,
|
||||
type: c.type,
|
||||
image: c.image,
|
||||
author_name: c.author_name,
|
||||
author_url: c.author_url,
|
||||
provider_name: c.provider_name,
|
||||
provider_url: c.provider_url,
|
||||
html: c.html,
|
||||
width: c.width,
|
||||
height: c.height,
|
||||
embed_url: null,
|
||||
blurhash: c.blurhash
|
||||
})
|
||||
export const context = (c: Entity.Context): MegalodonEntity.Context => ({
|
||||
ancestors: Array.isArray(c.ancestors) ? c.ancestors.map(a => status(a)) : [],
|
||||
descendants: Array.isArray(c.descendants) ? c.descendants.map(d => status(d)) : []
|
||||
})
|
||||
export const conversation = (c: Entity.Conversation): MegalodonEntity.Conversation => ({
|
||||
id: c.id,
|
||||
accounts: Array.isArray(c.accounts) ? c.accounts.map(a => account(a)) : [],
|
||||
last_status: c.last_status ? status(c.last_status) : null,
|
||||
unread: c.unread
|
||||
})
|
||||
export const emoji = (e: Entity.Emoji): MegalodonEntity.Emoji => ({
|
||||
shortcode: e.shortcode,
|
||||
static_url: e.static_url,
|
||||
url: e.url,
|
||||
visible_in_picker: e.visible_in_picker
|
||||
})
|
||||
export const featured_tag = (e: Entity.FeaturedTag): MegalodonEntity.FeaturedTag => e
|
||||
export const field = (f: Entity.Field): MegalodonEntity.Field => f
|
||||
export const filter = (f: Entity.Filter): MegalodonEntity.Filter => f
|
||||
export const follow_request = (f: Entity.FollowRequest): MegalodonEntity.FollowRequest => ({
|
||||
id: f.id,
|
||||
username: f.username,
|
||||
acct: f.acct,
|
||||
display_name: f.display_name,
|
||||
locked: f.locked,
|
||||
bot: f.bot,
|
||||
discoverable: f.discoverable,
|
||||
group: f.group,
|
||||
created_at: f.created_at,
|
||||
note: f.note,
|
||||
url: f.url,
|
||||
avatar: f.avatar,
|
||||
avatar_static: f.avatar_static,
|
||||
header: f.header,
|
||||
header_static: f.header_static,
|
||||
followers_count: f.followers_count,
|
||||
following_count: f.following_count,
|
||||
statuses_count: f.statuses_count,
|
||||
emojis: f.emojis.map(e => emoji(e)),
|
||||
fields: f.fields.map(f => field(f))
|
||||
})
|
||||
export const history = (h: Entity.History): MegalodonEntity.History => h
|
||||
export const identity_proof = (i: Entity.IdentityProof): MegalodonEntity.IdentityProof => i
|
||||
export const instance = (i: Entity.Instance): MegalodonEntity.Instance => {
|
||||
return {
|
||||
uri: i.uri,
|
||||
title: i.title,
|
||||
description: i.description,
|
||||
email: i.email,
|
||||
version: i.version,
|
||||
thumbnail: i.thumbnail,
|
||||
urls: i.urls ? urls(i.urls) : null,
|
||||
stats: stats(i.stats),
|
||||
languages: i.languages,
|
||||
registrations: i.registrations,
|
||||
approval_required: i.approval_required,
|
||||
invites_enabled: i.invites_enabled,
|
||||
configuration: {
|
||||
statuses: {
|
||||
max_characters: i.max_toot_chars
|
||||
}
|
||||
},
|
||||
contact_account: account(i.contact_account),
|
||||
rules: i.rules
|
||||
}
|
||||
}
|
||||
export const list = (l: Entity.List): MegalodonEntity.List => l
|
||||
export const marker = (m: Entity.Marker): MegalodonEntity.Marker => m
|
||||
export const mention = (m: Entity.Mention): MegalodonEntity.Mention => m
|
||||
export const notification = (n: Entity.Notification): MegalodonEntity.Notification | UnknownNotificationTypeError => {
|
||||
const notificationType = decodeNotificationType(n.type)
|
||||
if (notificationType instanceof UnknownNotificationTypeError) return notificationType
|
||||
if (n.status) {
|
||||
return {
|
||||
account: account(n.account),
|
||||
created_at: n.created_at,
|
||||
id: n.id,
|
||||
status: status(n.status),
|
||||
type: notificationType
|
||||
}
|
||||
} else {
|
||||
return {
|
||||
account: account(n.account),
|
||||
created_at: n.created_at,
|
||||
id: n.id,
|
||||
type: notificationType
|
||||
}
|
||||
}
|
||||
}
|
||||
export const poll = (p: Entity.Poll): MegalodonEntity.Poll => p
|
||||
export const poll_option = (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 relationship = (r: Entity.Relationship): MegalodonEntity.Relationship => r
|
||||
export const report = (r: Entity.Report): MegalodonEntity.Report => ({
|
||||
id: r.id,
|
||||
action_taken: r.action_taken,
|
||||
action_taken_at: null,
|
||||
category: r.category,
|
||||
comment: r.comment,
|
||||
forwarded: r.forwarded,
|
||||
status_ids: r.status_ids,
|
||||
rule_ids: r.rule_ids,
|
||||
target_account: account(r.target_account)
|
||||
})
|
||||
export const results = (r: Entity.Results): MegalodonEntity.Results => ({
|
||||
accounts: Array.isArray(r.accounts) ? r.accounts.map(a => account(a)) : [],
|
||||
statuses: Array.isArray(r.statuses) ? r.statuses.map(s => status(s)) : [],
|
||||
hashtags: Array.isArray(r.hashtags) ? r.hashtags.map(h => tag(h)) : []
|
||||
})
|
||||
export const scheduled_status = (s: Entity.ScheduledStatus): MegalodonEntity.ScheduledStatus => {
|
||||
return {
|
||||
id: s.id,
|
||||
scheduled_at: s.scheduled_at,
|
||||
params: status_params(s.params),
|
||||
media_attachments: s.media_attachments ? s.media_attachments.map(a => attachment(a)) : null
|
||||
}
|
||||
}
|
||||
export const source = (s: Entity.Source): MegalodonEntity.Source => s
|
||||
export const stats = (s: Entity.Stats): MegalodonEntity.Stats => s
|
||||
export const status = (s: Entity.Status): MegalodonEntity.Status => ({
|
||||
id: s.id,
|
||||
uri: s.uri,
|
||||
url: s.url,
|
||||
account: account(s.account),
|
||||
in_reply_to_id: s.in_reply_to_id,
|
||||
in_reply_to_account_id: s.in_reply_to_account_id,
|
||||
reblog: s.reblog ? status(s.reblog) : s.quote ? status(s.quote) : null,
|
||||
content: s.content,
|
||||
plain_content: null,
|
||||
created_at: s.created_at,
|
||||
edited_at: s.edited_at || null,
|
||||
emojis: Array.isArray(s.emojis) ? s.emojis.map(e => emoji(e)) : [],
|
||||
replies_count: s.replies_count,
|
||||
reblogs_count: s.reblogs_count,
|
||||
favourites_count: s.favourites_count,
|
||||
reblogged: s.reblogged,
|
||||
favourited: s.favourited,
|
||||
muted: s.muted,
|
||||
sensitive: s.sensitive,
|
||||
spoiler_text: s.spoiler_text,
|
||||
visibility: s.visibility,
|
||||
media_attachments: Array.isArray(s.media_attachments) ? s.media_attachments.map(m => attachment(m)) : [],
|
||||
mentions: Array.isArray(s.mentions) ? s.mentions.map(m => mention(m)) : [],
|
||||
tags: s.tags,
|
||||
card: s.card ? card(s.card) : null,
|
||||
poll: s.poll ? poll(s.poll) : null,
|
||||
application: s.application ? application(s.application) : null,
|
||||
language: s.language,
|
||||
pinned: s.pinned,
|
||||
emoji_reactions: [],
|
||||
bookmarked: s.bookmarked ? s.bookmarked : false,
|
||||
quote: false
|
||||
})
|
||||
export const status_params = (s: Entity.StatusParams): MegalodonEntity.StatusParams => {
|
||||
return {
|
||||
text: s.text,
|
||||
in_reply_to_id: s.in_reply_to_id,
|
||||
media_ids: s.media_ids,
|
||||
sensitive: s.sensitive,
|
||||
spoiler_text: s.spoiler_text,
|
||||
visibility: s.visibility,
|
||||
scheduled_at: s.scheduled_at,
|
||||
application_id: parseInt(s.application_id)
|
||||
}
|
||||
}
|
||||
export const status_source = (s: Entity.StatusSource): MegalodonEntity.StatusSource => s
|
||||
export const tag = (t: Entity.Tag): MegalodonEntity.Tag => t
|
||||
export const token = (t: Entity.Token): MegalodonEntity.Token => t
|
||||
export const urls = (u: Entity.URLs): MegalodonEntity.URLs => u
|
||||
}
|
||||
}
|
||||
export default FriendicaAPI
|
|
@ -1,29 +0,0 @@
|
|||
/// <reference path="emoji.ts" />
|
||||
/// <reference path="source.ts" />
|
||||
/// <reference path="field.ts" />
|
||||
namespace FriendicaEntity {
|
||||
export type Account = {
|
||||
id: string
|
||||
username: string
|
||||
acct: string
|
||||
display_name: string
|
||||
locked: boolean
|
||||
discoverable?: boolean
|
||||
group: boolean | null
|
||||
created_at: string
|
||||
followers_count: number
|
||||
following_count: number
|
||||
statuses_count: number
|
||||
note: string
|
||||
url: string
|
||||
avatar: string
|
||||
avatar_static: string
|
||||
header: string
|
||||
header_static: string
|
||||
emojis: Array<Emoji>
|
||||
moved: Account | null
|
||||
fields: Array<Field>
|
||||
bot: boolean
|
||||
source?: Source
|
||||
}
|
||||
}
|
|
@ -1,8 +0,0 @@
|
|||
namespace FriendicaEntity {
|
||||
export type Activity = {
|
||||
week: string
|
||||
statuses: string
|
||||
logins: string
|
||||
registrations: string
|
||||
}
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
namespace FriendicaEntity {
|
||||
export type Application = {
|
||||
name: string
|
||||
website?: string | null
|
||||
vapid_key?: string | null
|
||||
}
|
||||
}
|
|
@ -1,14 +0,0 @@
|
|||
/// <reference path="attachment.ts" />
|
||||
namespace FriendicaEntity {
|
||||
export type AsyncAttachment = {
|
||||
id: string
|
||||
type: 'unknown' | 'image' | 'gifv' | 'video' | 'audio'
|
||||
url: string | null
|
||||
remote_url: string | null
|
||||
preview_url: string
|
||||
text_url: string | null
|
||||
meta: Meta | null
|
||||
description: string | null
|
||||
blurhash: string | null
|
||||
}
|
||||
}
|
|
@ -1,49 +0,0 @@
|
|||
namespace FriendicaEntity {
|
||||
export type Sub = {
|
||||
// For Image, Gifv, and Video
|
||||
width?: number
|
||||
height?: number
|
||||
size?: string
|
||||
aspect?: number
|
||||
|
||||
// For Gifv and Video
|
||||
frame_rate?: string
|
||||
|
||||
// For Audio, Gifv, and Video
|
||||
duration?: number
|
||||
bitrate?: number
|
||||
}
|
||||
|
||||
export type Focus = {
|
||||
x: number
|
||||
y: number
|
||||
}
|
||||
|
||||
export type Meta = {
|
||||
original?: Sub
|
||||
small?: Sub
|
||||
focus?: Focus
|
||||
length?: string
|
||||
duration?: number
|
||||
fps?: number
|
||||
size?: string
|
||||
width?: number
|
||||
height?: number
|
||||
aspect?: number
|
||||
audio_encode?: string
|
||||
audio_bitrate?: string
|
||||
audio_channel?: string
|
||||
}
|
||||
|
||||
export type Attachment = {
|
||||
id: string
|
||||
type: 'unknown' | 'image' | 'gifv' | 'video' | 'audio'
|
||||
url: string
|
||||
remote_url: string | null
|
||||
preview_url: string | null
|
||||
text_url: string | null
|
||||
meta: Meta | null
|
||||
description: string | null
|
||||
blurhash: string | null
|
||||
}
|
||||
}
|
|
@ -1,17 +0,0 @@
|
|||
namespace FriendicaEntity {
|
||||
export type Card = {
|
||||
url: string
|
||||
title: string
|
||||
description: string
|
||||
type: 'link' | 'photo' | 'video' | 'rich'
|
||||
image: string | null
|
||||
author_name: string
|
||||
author_url: string
|
||||
provider_name: string
|
||||
provider_url: string
|
||||
html: string
|
||||
width: number
|
||||
height: number
|
||||
blurhash: string | null
|
||||
}
|
||||
}
|
|
@ -1,8 +0,0 @@
|
|||
/// <reference path="status.ts" />
|
||||
|
||||
namespace FriendicaEntity {
|
||||
export type Context = {
|
||||
ancestors: Array<Status>
|
||||
descendants: Array<Status>
|
||||
}
|
||||
}
|
|
@ -1,11 +0,0 @@
|
|||
/// <reference path="account.ts" />
|
||||
/// <reference path="status.ts" />
|
||||
|
||||
namespace FriendicaEntity {
|
||||
export type Conversation = {
|
||||
id: string
|
||||
accounts: Array<Account>
|
||||
last_status: Status | null
|
||||
unread: boolean
|
||||
}
|
||||
}
|
|
@ -1,8 +0,0 @@
|
|||
namespace FriendicaEntity {
|
||||
export type Emoji = {
|
||||
shortcode: string
|
||||
static_url: string
|
||||
url: string
|
||||
visible_in_picker: boolean
|
||||
}
|
||||
}
|
|
@ -1,8 +0,0 @@
|
|||
namespace FriendicaEntity {
|
||||
export type FeaturedTag = {
|
||||
id: string
|
||||
name: string
|
||||
statuses_count: number
|
||||
last_status_at: string
|
||||
}
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
namespace FriendicaEntity {
|
||||
export type Field = {
|
||||
name: string
|
||||
value: string
|
||||
verified_at: string | null
|
||||
}
|
||||
}
|
|
@ -1,12 +0,0 @@
|
|||
namespace FriendicaEntity {
|
||||
export type Filter = {
|
||||
id: string
|
||||
phrase: string
|
||||
context: Array<FilterContext>
|
||||
expires_at: string | null
|
||||
irreversible: boolean
|
||||
whole_word: boolean
|
||||
}
|
||||
|
||||
export type FilterContext = string
|
||||
}
|
|
@ -1,27 +0,0 @@
|
|||
/// <reference path="emoji.ts" />
|
||||
/// <reference path="field.ts" />
|
||||
|
||||
namespace FriendicaEntity {
|
||||
export type FollowRequest = {
|
||||
id: number
|
||||
username: string
|
||||
acct: string
|
||||
display_name: string
|
||||
locked: boolean
|
||||
bot: boolean
|
||||
discoverable?: boolean
|
||||
group: boolean
|
||||
created_at: string
|
||||
note: string
|
||||
url: string
|
||||
avatar: string
|
||||
avatar_static: string
|
||||
header: string
|
||||
header_static: string
|
||||
followers_count: number
|
||||
following_count: number
|
||||
statuses_count: number
|
||||
emojis: Array<Emoji>
|
||||
fields: Array<Field>
|
||||
}
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
namespace FriendicaEntity {
|
||||
export type History = {
|
||||
day: string
|
||||
uses: number
|
||||
accounts: number
|
||||
}
|
||||
}
|
|
@ -1,9 +0,0 @@
|
|||
namespace FriendicaEntity {
|
||||
export type IdentityProof = {
|
||||
provider: string
|
||||
provider_username: string
|
||||
updated_at: string
|
||||
proof_url: string
|
||||
profile_url: string
|
||||
}
|
||||
}
|
|
@ -1,28 +0,0 @@
|
|||
/// <reference path="account.ts" />
|
||||
/// <reference path="urls.ts" />
|
||||
/// <reference path="stats.ts" />
|
||||
|
||||
namespace FriendicaEntity {
|
||||
export type Instance = {
|
||||
uri: string
|
||||
title: string
|
||||
description: string
|
||||
email: string
|
||||
version: string
|
||||
thumbnail: string | null
|
||||
urls: URLs | null
|
||||
stats: Stats
|
||||
languages: Array<string>
|
||||
registrations: boolean
|
||||
approval_required: boolean
|
||||
invites_enabled: boolean
|
||||
max_toot_chars: number
|
||||
contact_account: Account
|
||||
rules: Array<InstanceRule>
|
||||
}
|
||||
|
||||
export type InstanceRule = {
|
||||
id: string
|
||||
text: string
|
||||
}
|
||||
}
|
|
@ -1,9 +0,0 @@
|
|||
namespace FriendicaEntity {
|
||||
export type List = {
|
||||
id: string
|
||||
title: string
|
||||
replies_policy: RepliesPolicy
|
||||
}
|
||||
|
||||
export type RepliesPolicy = 'followed' | 'list' | 'none'
|
||||
}
|
|
@ -1,14 +0,0 @@
|
|||
namespace FriendicaEntity {
|
||||
export type Marker = {
|
||||
home: {
|
||||
last_read_id: string
|
||||
version: number
|
||||
updated_at: string
|
||||
}
|
||||
notifications: {
|
||||
last_read_id: string
|
||||
version: number
|
||||
updated_at: string
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,8 +0,0 @@
|
|||
namespace FriendicaEntity {
|
||||
export type Mention = {
|
||||
id: string
|
||||
username: string
|
||||
url: string
|
||||
acct: string
|
||||
}
|
||||
}
|
|
@ -1,14 +0,0 @@
|
|||
/// <reference path="account.ts" />
|
||||
/// <reference path="status.ts" />
|
||||
|
||||
namespace FriendicaEntity {
|
||||
export type Notification = {
|
||||
account: Account
|
||||
created_at: string
|
||||
id: string
|
||||
status?: Status
|
||||
type: NotificationType
|
||||
}
|
||||
|
||||
export type NotificationType = string
|
||||
}
|
|
@ -1,13 +0,0 @@
|
|||
/// <reference path="poll_option.ts" />
|
||||
|
||||
namespace FriendicaEntity {
|
||||
export type Poll = {
|
||||
id: string
|
||||
expires_at: string | null
|
||||
expired: boolean
|
||||
multiple: boolean
|
||||
votes_count: number
|
||||
options: Array<PollOption>
|
||||
voted: boolean
|
||||
}
|
||||
}
|
|
@ -1,6 +0,0 @@
|
|||
namespace FriendicaEntity {
|
||||
export type PollOption = {
|
||||
title: string
|
||||
votes_count: number | null
|
||||
}
|
||||
}
|
|
@ -1,9 +0,0 @@
|
|||
namespace FriendicaEntity {
|
||||
export type Preferences = {
|
||||
'posting:default:visibility': 'public' | 'unlisted' | 'private' | 'direct'
|
||||
'posting:default:sensitive': boolean
|
||||
'posting:default:language': string | null
|
||||
'reading:expand:media': 'default' | 'show_all' | 'hide_all'
|
||||
'reading:expand:spoilers': boolean
|
||||
}
|
||||
}
|
|
@ -1,16 +0,0 @@
|
|||
namespace FriendicaEntity {
|
||||
export type Alerts = {
|
||||
follow: boolean
|
||||
favourite: boolean
|
||||
mention: boolean
|
||||
reblog: boolean
|
||||
poll: boolean
|
||||
}
|
||||
|
||||
export type PushSubscription = {
|
||||
id: string
|
||||
endpoint: string
|
||||
server_key: string
|
||||
alerts: Alerts
|
||||
}
|
||||
}
|
|
@ -1,17 +0,0 @@
|
|||
namespace FriendicaEntity {
|
||||
export type Relationship = {
|
||||
id: string
|
||||
following: boolean
|
||||
followed_by: boolean
|
||||
blocking: boolean
|
||||
blocked_by: boolean
|
||||
muting: boolean
|
||||
muting_notifications: boolean
|
||||
requested: boolean
|
||||
domain_blocking: boolean
|
||||
showing_reblogs: boolean
|
||||
endorsed: boolean
|
||||
notifying: boolean
|
||||
note: string | null
|
||||
}
|
||||
}
|
|
@ -1,16 +0,0 @@
|
|||
/// <reference path="account.ts" />
|
||||
|
||||
namespace FriendicaEntity {
|
||||
export type Report = {
|
||||
id: string
|
||||
action_taken: boolean
|
||||
category: Category
|
||||
comment: string
|
||||
forwarded: boolean
|
||||
status_ids: Array<string> | null
|
||||
rule_ids: Array<string> | null
|
||||
target_account: Account
|
||||
}
|
||||
|
||||
export type Category = 'spam' | 'violation' | 'other'
|
||||
}
|
|
@ -1,11 +0,0 @@
|
|||
/// <reference path="account.ts" />
|
||||
/// <reference path="status.ts" />
|
||||
/// <reference path="tag.ts" />
|
||||
|
||||
namespace FriendicaEntity {
|
||||
export type Results = {
|
||||
accounts: Array<Account>
|
||||
statuses: Array<Status>
|
||||
hashtags: Array<Tag>
|
||||
}
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
/// <reference path="attachment.ts" />
|
||||
/// <reference path="status_params.ts" />
|
||||
namespace FriendicaEntity {
|
||||
export type ScheduledStatus = {
|
||||
id: string
|
||||
scheduled_at: string
|
||||
params: StatusParams
|
||||
media_attachments: Array<Attachment>
|
||||
}
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
/// <reference path="field.ts" />
|
||||
namespace FriendicaEntity {
|
||||
export type Source = {
|
||||
privacy: string | null
|
||||
sensitive: boolean | null
|
||||
language: string | null
|
||||
note: string
|
||||
fields: Array<Field>
|
||||
}
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
namespace FriendicaEntity {
|
||||
export type Stats = {
|
||||
user_count: number
|
||||
status_count: number
|
||||
domain_count: number
|
||||
}
|
||||
}
|
|
@ -1,49 +0,0 @@
|
|||
/// <reference path="account.ts" />
|
||||
/// <reference path="application.ts" />
|
||||
/// <reference path="mention.ts" />
|
||||
/// <reference path="attachment.ts" />
|
||||
/// <reference path="emoji.ts" />
|
||||
/// <reference path="card.ts" />
|
||||
/// <reference path="poll.ts" />
|
||||
|
||||
namespace FriendicaEntity {
|
||||
export type Status = {
|
||||
id: string
|
||||
uri: string
|
||||
url: string
|
||||
account: Account
|
||||
in_reply_to_id: string | null
|
||||
in_reply_to_account_id: string | null
|
||||
reblog: Status | null
|
||||
content: string
|
||||
created_at: string
|
||||
edited_at?: string | null
|
||||
emojis: Emoji[]
|
||||
replies_count: number
|
||||
reblogs_count: number
|
||||
favourites_count: number
|
||||
reblogged: boolean | null
|
||||
favourited: boolean | null
|
||||
muted: boolean | null
|
||||
sensitive: boolean
|
||||
spoiler_text: string
|
||||
visibility: 'public' | 'unlisted' | 'private' | 'direct'
|
||||
media_attachments: Array<Attachment>
|
||||
mentions: Array<Mention>
|
||||
tags: Array<StatusTag>
|
||||
card: Card | null
|
||||
poll: Poll | null
|
||||
application: Application | null
|
||||
language: string | null
|
||||
pinned: boolean | null
|
||||
bookmarked?: boolean
|
||||
// These parameters are unique parameters in fedibird.com for quote.
|
||||
quote_id?: string
|
||||
quote?: Status | null
|
||||
}
|
||||
|
||||
export type StatusTag = {
|
||||
name: string
|
||||
url: string
|
||||
}
|
||||
}
|
|
@ -1,12 +0,0 @@
|
|||
namespace FriendicaEntity {
|
||||
export type StatusParams = {
|
||||
text: string
|
||||
in_reply_to_id: string | null
|
||||
media_ids: Array<string> | null
|
||||
sensitive: boolean | null
|
||||
spoiler_text: string | null
|
||||
visibility: 'public' | 'unlisted' | 'private' | null
|
||||
scheduled_at: string | null
|
||||
application_id: string
|
||||
}
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
namespace FriendicaEntity {
|
||||
export type StatusSource = {
|
||||
id: string
|
||||
text: string
|
||||
spoiler_text: string
|
||||
}
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
/// <reference path="history.ts" />
|
||||
|
||||
namespace FriendicaEntity {
|
||||
export type Tag = {
|
||||
name: string
|
||||
url: string
|
||||
history: Array<History>
|
||||
following?: boolean
|
||||
}
|
||||
}
|
|
@ -1,8 +0,0 @@
|
|||
namespace FriendicaEntity {
|
||||
export type Token = {
|
||||
access_token: string
|
||||
token_type: string
|
||||
scope: string
|
||||
created_at: number
|
||||
}
|
||||
}
|
|
@ -1,5 +0,0 @@
|
|||
namespace FriendicaEntity {
|
||||
export type URLs = {
|
||||
streaming_api: string
|
||||
}
|
||||
}
|
|
@ -1,38 +0,0 @@
|
|||
/// <reference path="./entities/account.ts" />
|
||||
/// <reference path="./entities/activity.ts" />
|
||||
/// <reference path="./entities/application.ts" />
|
||||
/// <reference path="./entities/async_attachment.ts" />
|
||||
/// <reference path="./entities/attachment.ts" />
|
||||
/// <reference path="./entities/card.ts" />
|
||||
/// <reference path="./entities/context.ts" />
|
||||
/// <reference path="./entities/conversation.ts" />
|
||||
/// <reference path="./entities/emoji.ts" />
|
||||
/// <reference path="./entities/featured_tag.ts" />
|
||||
/// <reference path="./entities/field.ts" />
|
||||
/// <reference path="./entities/filter.ts" />
|
||||
/// <reference path="./entities/follow_request.ts" />
|
||||
/// <reference path="./entities/history.ts" />
|
||||
/// <reference path="./entities/identity_proof.ts" />
|
||||
/// <reference path="./entities/instance.ts" />
|
||||
/// <reference path="./entities/list.ts" />
|
||||
/// <reference path="./entities/marker.ts" />
|
||||
/// <reference path="./entities/mention.ts" />
|
||||
/// <reference path="./entities/notification.ts" />
|
||||
/// <reference path="./entities/poll.ts" />
|
||||
/// <reference path="./entities/poll_option.ts" />
|
||||
/// <reference path="./entities/preferences.ts" />
|
||||
/// <reference path="./entities/push_subscription.ts" />
|
||||
/// <reference path="./entities/relationship.ts" />
|
||||
/// <reference path="./entities/report.ts" />
|
||||
/// <reference path="./entities/results.ts" />
|
||||
/// <reference path="./entities/scheduled_status.ts" />
|
||||
/// <reference path="./entities/source.ts" />
|
||||
/// <reference path="./entities/stats.ts" />
|
||||
/// <reference path="./entities/status.ts" />
|
||||
/// <reference path="./entities/status_params.ts" />
|
||||
/// <reference path="./entities/status_source.ts" />
|
||||
/// <reference path="./entities/tag.ts" />
|
||||
/// <reference path="./entities/token.ts" />
|
||||
/// <reference path="./entities/urls.ts" />
|
||||
|
||||
export default FriendicaEntity
|
|
@ -1,14 +0,0 @@
|
|||
import FriendicaEntity from './entity'
|
||||
|
||||
namespace FriendicaNotificationType {
|
||||
export const Mention: FriendicaEntity.NotificationType = 'mention'
|
||||
export const Reblog: FriendicaEntity.NotificationType = 'reblog'
|
||||
export const Favourite: FriendicaEntity.NotificationType = 'favourite'
|
||||
export const Follow: FriendicaEntity.NotificationType = 'follow'
|
||||
export const Poll: FriendicaEntity.NotificationType = 'poll'
|
||||
export const FollowRequest: FriendicaEntity.NotificationType = 'follow_request'
|
||||
export const Status: FriendicaEntity.NotificationType = 'status'
|
||||
export const Update: FriendicaEntity.NotificationType = 'update'
|
||||
}
|
||||
|
||||
export default FriendicaNotificationType
|
|
@ -1,18 +0,0 @@
|
|||
import { WebSocketInterface } from '../megalodon'
|
||||
import { EventEmitter } from 'events'
|
||||
import { ProxyConfig } from '../proxy_config'
|
||||
|
||||
export default class WebSocket extends EventEmitter implements WebSocketInterface {
|
||||
constructor(
|
||||
_url: string,
|
||||
_stream: string,
|
||||
_params: string | undefined,
|
||||
_accessToken: string,
|
||||
_userAgent: string,
|
||||
_proxyConfig: ProxyConfig | false = false
|
||||
) {
|
||||
super()
|
||||
}
|
||||
public start() {}
|
||||
public stop() {}
|
||||
}
|
|
@ -2,15 +2,14 @@ import Response from './response'
|
|||
import OAuth from './oauth'
|
||||
import { isCancel, RequestCanceledError } from './cancel'
|
||||
import { ProxyConfig } from './proxy_config'
|
||||
import generator, { MegalodonInterface, WebSocketInterface } from './megalodon'
|
||||
import { MegalodonInterface, WebSocketInterface } from './megalodon'
|
||||
import { detector } from './detector'
|
||||
import Mastodon from './mastodon'
|
||||
import Pleroma from './pleroma'
|
||||
import Misskey from './misskey'
|
||||
import Entity from './entity'
|
||||
import NotificationType from './notification'
|
||||
import FilterContext from './filter_context'
|
||||
import Converter from './converter'
|
||||
import MastodonEntity from './mastodon/entity';
|
||||
|
||||
export {
|
||||
Response,
|
||||
|
@ -23,14 +22,8 @@ export {
|
|||
WebSocketInterface,
|
||||
NotificationType,
|
||||
FilterContext,
|
||||
Mastodon,
|
||||
Pleroma,
|
||||
Misskey,
|
||||
Entity,
|
||||
Converter,
|
||||
generator,
|
||||
MastodonEntity,
|
||||
}
|
||||
|
||||
export const megalodon = generator;
|
||||
|
||||
export default generator
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -1,662 +0,0 @@
|
|||
import axios, { AxiosResponse, AxiosRequestConfig } from 'axios'
|
||||
import objectAssignDeep from 'object-assign-deep'
|
||||
|
||||
import WebSocket from './web_socket'
|
||||
import Response from '../response'
|
||||
import { RequestCanceledError } from '../cancel'
|
||||
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, { UnknownNotificationTypeError } from '../notification'
|
||||
import MastodonNotificationType from './notification'
|
||||
|
||||
namespace MastodonAPI {
|
||||
/**
|
||||
* Interface
|
||||
*/
|
||||
export interface Interface {
|
||||
get<T = any>(path: string, params?: any, headers?: { [key: string]: string }, pathIsFullyQualified?: boolean): Promise<Response<T>>
|
||||
put<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
|
||||
putForm<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
|
||||
patch<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
|
||||
patchForm<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>>
|
||||
postForm<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
|
||||
del<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
|
||||
cancel(): void
|
||||
socket(path: string, stream: string, params?: string): WebSocket
|
||||
}
|
||||
|
||||
/**
|
||||
* Mastodon API client.
|
||||
*
|
||||
* Using axios for request, you will handle promises.
|
||||
*/
|
||||
export class Client implements Interface {
|
||||
static DEFAULT_SCOPE = DEFAULT_SCOPE
|
||||
static DEFAULT_URL = 'https://mastodon.social'
|
||||
static NO_REDIRECT = NO_REDIRECT
|
||||
|
||||
private accessToken: string | null
|
||||
private baseUrl: string
|
||||
private userAgent: string
|
||||
private abortController: AbortController
|
||||
private proxyConfig: ProxyConfig | false = false
|
||||
|
||||
/**
|
||||
* @param baseUrl hostname or base URL
|
||||
* @param accessToken access token from OAuth2 authorization
|
||||
* @param userAgent UserAgent is specified in header on request.
|
||||
* @param proxyConfig Proxy setting, or set false if don't use proxy.
|
||||
*/
|
||||
constructor(
|
||||
baseUrl: string,
|
||||
accessToken: string | null = null,
|
||||
userAgent: string = DEFAULT_UA,
|
||||
proxyConfig: ProxyConfig | false = false
|
||||
) {
|
||||
this.accessToken = accessToken
|
||||
this.baseUrl = baseUrl
|
||||
this.userAgent = userAgent
|
||||
this.proxyConfig = proxyConfig
|
||||
this.abortController = new AbortController()
|
||||
axios.defaults.signal = this.abortController.signal
|
||||
}
|
||||
|
||||
/**
|
||||
* GET request to mastodon REST API.
|
||||
* @param path relative path from baseUrl
|
||||
* @param params Query parameters
|
||||
* @param headers Request header object
|
||||
*/
|
||||
public async get<T>(
|
||||
path: string,
|
||||
params = {},
|
||||
headers: { [key: string]: string } = {},
|
||||
pathIsFullyQualified = false
|
||||
): Promise<Response<T>> {
|
||||
let options: AxiosRequestConfig = {
|
||||
params: params,
|
||||
headers: headers,
|
||||
maxContentLength: Infinity,
|
||||
maxBodyLength: Infinity
|
||||
}
|
||||
if (this.accessToken) {
|
||||
options = objectAssignDeep({}, options, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.accessToken}`
|
||||
}
|
||||
})
|
||||
}
|
||||
if (this.proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
httpAgent: proxyAgent(this.proxyConfig),
|
||||
httpsAgent: proxyAgent(this.proxyConfig)
|
||||
})
|
||||
}
|
||||
return axios
|
||||
.get<T>((pathIsFullyQualified ? '' : this.baseUrl) + path, options)
|
||||
.catch((err: Error) => {
|
||||
if (axios.isCancel(err)) {
|
||||
throw new RequestCanceledError(err.message)
|
||||
} else {
|
||||
throw err
|
||||
}
|
||||
})
|
||||
.then((resp: AxiosResponse<T>) => {
|
||||
const res: Response<T> = {
|
||||
data: resp.data,
|
||||
status: resp.status,
|
||||
statusText: resp.statusText,
|
||||
headers: resp.headers
|
||||
}
|
||||
return res
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* PUT request to mastodon REST API.
|
||||
* @param path relative path from baseUrl
|
||||
* @param params Form data. If you want to post file, please use FormData()
|
||||
* @param headers Request header object
|
||||
*/
|
||||
public async put<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
|
||||
let options: AxiosRequestConfig = {
|
||||
headers: headers,
|
||||
maxContentLength: Infinity,
|
||||
maxBodyLength: Infinity
|
||||
}
|
||||
if (this.accessToken) {
|
||||
options = objectAssignDeep({}, options, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.accessToken}`
|
||||
}
|
||||
})
|
||||
}
|
||||
if (this.proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
httpAgent: proxyAgent(this.proxyConfig),
|
||||
httpsAgent: proxyAgent(this.proxyConfig)
|
||||
})
|
||||
}
|
||||
return axios
|
||||
.put<T>(this.baseUrl + path, params, options)
|
||||
.catch((err: Error) => {
|
||||
if (axios.isCancel(err)) {
|
||||
throw new RequestCanceledError(err.message)
|
||||
} else {
|
||||
throw err
|
||||
}
|
||||
})
|
||||
.then((resp: AxiosResponse<T>) => {
|
||||
const res: Response<T> = {
|
||||
data: resp.data,
|
||||
status: resp.status,
|
||||
statusText: resp.statusText,
|
||||
headers: resp.headers
|
||||
}
|
||||
return res
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* PUT request to mastodon REST API for multipart.
|
||||
* @param path relative path from baseUrl
|
||||
* @param params Form data. If you want to post file, please use FormData()
|
||||
* @param headers Request header object
|
||||
*/
|
||||
public async putForm<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
|
||||
let options: AxiosRequestConfig = {
|
||||
headers: headers,
|
||||
maxContentLength: Infinity,
|
||||
maxBodyLength: Infinity
|
||||
}
|
||||
if (this.accessToken) {
|
||||
options = objectAssignDeep({}, options, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.accessToken}`
|
||||
}
|
||||
})
|
||||
}
|
||||
if (this.proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
httpAgent: proxyAgent(this.proxyConfig),
|
||||
httpsAgent: proxyAgent(this.proxyConfig)
|
||||
})
|
||||
}
|
||||
return axios
|
||||
.putForm<T>(this.baseUrl + path, params, options)
|
||||
.catch((err: Error) => {
|
||||
if (axios.isCancel(err)) {
|
||||
throw new RequestCanceledError(err.message)
|
||||
} else {
|
||||
throw err
|
||||
}
|
||||
})
|
||||
.then((resp: AxiosResponse<T>) => {
|
||||
const res: Response<T> = {
|
||||
data: resp.data,
|
||||
status: resp.status,
|
||||
statusText: resp.statusText,
|
||||
headers: resp.headers
|
||||
}
|
||||
return res
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* PATCH request to mastodon REST API.
|
||||
* @param path relative path from baseUrl
|
||||
* @param params Form data. If you want to post file, please use FormData()
|
||||
* @param headers Request header object
|
||||
*/
|
||||
public async patch<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
|
||||
let options: AxiosRequestConfig = {
|
||||
headers: headers,
|
||||
maxContentLength: Infinity,
|
||||
maxBodyLength: Infinity
|
||||
}
|
||||
if (this.accessToken) {
|
||||
options = objectAssignDeep({}, options, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.accessToken}`
|
||||
}
|
||||
})
|
||||
}
|
||||
if (this.proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
httpAgent: proxyAgent(this.proxyConfig),
|
||||
httpsAgent: proxyAgent(this.proxyConfig)
|
||||
})
|
||||
}
|
||||
return axios
|
||||
.patch<T>(this.baseUrl + path, params, options)
|
||||
.catch((err: Error) => {
|
||||
if (axios.isCancel(err)) {
|
||||
throw new RequestCanceledError(err.message)
|
||||
} else {
|
||||
throw err
|
||||
}
|
||||
})
|
||||
.then((resp: AxiosResponse<T>) => {
|
||||
const res: Response<T> = {
|
||||
data: resp.data,
|
||||
status: resp.status,
|
||||
statusText: resp.statusText,
|
||||
headers: resp.headers
|
||||
}
|
||||
return res
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* PATCH request to mastodon REST API for multipart.
|
||||
* @param path relative path from baseUrl
|
||||
* @param params Form data. If you want to post file, please use FormData()
|
||||
* @param headers Request header object
|
||||
*/
|
||||
public async patchForm<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
|
||||
let options: AxiosRequestConfig = {
|
||||
headers: headers,
|
||||
maxContentLength: Infinity,
|
||||
maxBodyLength: Infinity
|
||||
}
|
||||
if (this.accessToken) {
|
||||
options = objectAssignDeep({}, options, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.accessToken}`
|
||||
}
|
||||
})
|
||||
}
|
||||
if (this.proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
httpAgent: proxyAgent(this.proxyConfig),
|
||||
httpsAgent: proxyAgent(this.proxyConfig)
|
||||
})
|
||||
}
|
||||
return axios
|
||||
.patchForm<T>(this.baseUrl + path, params, options)
|
||||
.catch((err: Error) => {
|
||||
if (axios.isCancel(err)) {
|
||||
throw new RequestCanceledError(err.message)
|
||||
} else {
|
||||
throw err
|
||||
}
|
||||
})
|
||||
.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 mastodon REST API.
|
||||
* @param path relative path from baseUrl
|
||||
* @param params Form data
|
||||
* @param headers Request header object
|
||||
*/
|
||||
public async post<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
|
||||
let options: AxiosRequestConfig = {
|
||||
headers: headers,
|
||||
maxContentLength: Infinity,
|
||||
maxBodyLength: Infinity
|
||||
}
|
||||
if (this.accessToken) {
|
||||
options = objectAssignDeep({}, options, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.accessToken}`
|
||||
}
|
||||
})
|
||||
}
|
||||
if (this.proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
httpAgent: proxyAgent(this.proxyConfig),
|
||||
httpsAgent: proxyAgent(this.proxyConfig)
|
||||
})
|
||||
}
|
||||
return axios.post<T>(this.baseUrl + path, params, 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 mastodon REST API for multipart.
|
||||
* @param path relative path from baseUrl
|
||||
* @param params Form data
|
||||
* @param headers Request header object
|
||||
*/
|
||||
public async postForm<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
|
||||
let options: AxiosRequestConfig = {
|
||||
headers: headers,
|
||||
maxContentLength: Infinity,
|
||||
maxBodyLength: Infinity
|
||||
}
|
||||
if (this.accessToken) {
|
||||
options = objectAssignDeep({}, options, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.accessToken}`
|
||||
}
|
||||
})
|
||||
}
|
||||
if (this.proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
httpAgent: proxyAgent(this.proxyConfig),
|
||||
httpsAgent: proxyAgent(this.proxyConfig)
|
||||
})
|
||||
}
|
||||
return axios.postForm<T>(this.baseUrl + path, params, options).then((resp: AxiosResponse<T>) => {
|
||||
const res: Response<T> = {
|
||||
data: resp.data,
|
||||
status: resp.status,
|
||||
statusText: resp.statusText,
|
||||
headers: resp.headers
|
||||
}
|
||||
return res
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* DELETE request to mastodon REST API.
|
||||
* @param path relative path from baseUrl
|
||||
* @param params Form data
|
||||
* @param headers Request header object
|
||||
*/
|
||||
public async del<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
|
||||
let options: AxiosRequestConfig = {
|
||||
data: params,
|
||||
headers: headers,
|
||||
maxContentLength: Infinity,
|
||||
maxBodyLength: Infinity
|
||||
}
|
||||
if (this.accessToken) {
|
||||
options = objectAssignDeep({}, options, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.accessToken}`
|
||||
}
|
||||
})
|
||||
}
|
||||
if (this.proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
httpAgent: proxyAgent(this.proxyConfig),
|
||||
httpsAgent: proxyAgent(this.proxyConfig)
|
||||
})
|
||||
}
|
||||
return axios
|
||||
.delete(this.baseUrl + path, options)
|
||||
.catch((err: Error) => {
|
||||
if (axios.isCancel(err)) {
|
||||
throw new RequestCanceledError(err.message)
|
||||
} else {
|
||||
throw err
|
||||
}
|
||||
})
|
||||
.then((resp: AxiosResponse) => {
|
||||
const res: Response<T> = {
|
||||
data: resp.data,
|
||||
status: resp.status,
|
||||
statusText: resp.statusText,
|
||||
headers: resp.headers
|
||||
}
|
||||
return res
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancel all requests in this instance.
|
||||
* @returns void
|
||||
*/
|
||||
public cancel(): void {
|
||||
return this.abortController.abort()
|
||||
}
|
||||
|
||||
/**
|
||||
* Get connection and receive websocket connection for Pleroma API.
|
||||
*
|
||||
* @param path relative path from baseUrl: normally it is `/streaming`.
|
||||
* @param stream Stream name, please refer: https://git.pleroma.social/pleroma/pleroma/blob/develop/lib/pleroma/web/mastodon_api/mastodon_socket.ex#L19-28
|
||||
* @returns WebSocket, which inherits from EventEmitter
|
||||
*/
|
||||
public socket(path: string, stream: string, params?: string): WebSocket {
|
||||
if (!this.accessToken) {
|
||||
throw new Error('accessToken is required')
|
||||
}
|
||||
const url = this.baseUrl + path
|
||||
const streaming = new WebSocket(url, stream, params, this.accessToken, this.userAgent, this.proxyConfig)
|
||||
process.nextTick(() => {
|
||||
streaming.start()
|
||||
})
|
||||
return streaming
|
||||
}
|
||||
}
|
||||
|
||||
export namespace Entity {
|
||||
export type Account = MastodonEntity.Account
|
||||
export type Activity = MastodonEntity.Activity
|
||||
export type Announcement = MastodonEntity.Announcement
|
||||
export type Application = MastodonEntity.Application
|
||||
export type AsyncAttachment = MegalodonEntity.AsyncAttachment
|
||||
export type Attachment = MastodonEntity.Attachment
|
||||
export type Card = MastodonEntity.Card
|
||||
export type Context = MastodonEntity.Context
|
||||
export type Conversation = MastodonEntity.Conversation
|
||||
export type Emoji = MastodonEntity.Emoji
|
||||
export type FeaturedTag = MastodonEntity.FeaturedTag
|
||||
export type Field = MastodonEntity.Field
|
||||
export type Filter = MastodonEntity.Filter
|
||||
export type History = MastodonEntity.History
|
||||
export type IdentityProof = MastodonEntity.IdentityProof
|
||||
export type Instance = MastodonEntity.Instance
|
||||
export type List = MastodonEntity.List
|
||||
export type Marker = MastodonEntity.Marker
|
||||
export type Mention = MastodonEntity.Mention
|
||||
export type Notification = MastodonEntity.Notification
|
||||
export type Poll = MastodonEntity.Poll
|
||||
export type PollOption = MastodonEntity.PollOption
|
||||
export type Preferences = MastodonEntity.Preferences
|
||||
export type PushSubscription = MastodonEntity.PushSubscription
|
||||
export type Relationship = MastodonEntity.Relationship
|
||||
export type Report = MastodonEntity.Report
|
||||
export type Results = MastodonEntity.Results
|
||||
export type Role = MastodonEntity.Role
|
||||
export type ScheduledStatus = MastodonEntity.ScheduledStatus
|
||||
export type Source = MastodonEntity.Source
|
||||
export type Stats = MastodonEntity.Stats
|
||||
export type Status = MastodonEntity.Status
|
||||
export type StatusParams = MastodonEntity.StatusParams
|
||||
export type StatusSource = MastodonEntity.StatusSource
|
||||
export type Tag = MastodonEntity.Tag
|
||||
export type Token = MastodonEntity.Token
|
||||
export type URLs = MastodonEntity.URLs
|
||||
}
|
||||
|
||||
export namespace Converter {
|
||||
export const encodeNotificationType = (
|
||||
t: MegalodonEntity.NotificationType
|
||||
): MastodonEntity.NotificationType | UnknownNotificationTypeError => {
|
||||
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.FollowRequest:
|
||||
return MastodonNotificationType.FollowRequest
|
||||
case NotificationType.Status:
|
||||
return MastodonNotificationType.Status
|
||||
case NotificationType.PollExpired:
|
||||
return MastodonNotificationType.Poll
|
||||
case NotificationType.Update:
|
||||
return MastodonNotificationType.Update
|
||||
case NotificationType.AdminSignup:
|
||||
return MastodonNotificationType.AdminSignup
|
||||
case NotificationType.AdminReport:
|
||||
return MastodonNotificationType.AdminReport
|
||||
default:
|
||||
return new UnknownNotificationTypeError()
|
||||
}
|
||||
}
|
||||
|
||||
export const decodeNotificationType = (
|
||||
t: MastodonEntity.NotificationType
|
||||
): MegalodonEntity.NotificationType | UnknownNotificationTypeError => {
|
||||
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.FollowRequest:
|
||||
return NotificationType.FollowRequest
|
||||
case MastodonNotificationType.Status:
|
||||
return NotificationType.Status
|
||||
case MastodonNotificationType.Poll:
|
||||
return NotificationType.PollExpired
|
||||
case MastodonNotificationType.Update:
|
||||
return NotificationType.Update
|
||||
case MastodonNotificationType.AdminSignup:
|
||||
return NotificationType.AdminSignup
|
||||
case MastodonNotificationType.AdminReport:
|
||||
return NotificationType.AdminReport
|
||||
default:
|
||||
return new UnknownNotificationTypeError()
|
||||
}
|
||||
}
|
||||
|
||||
export const account = (a: Entity.Account): MegalodonEntity.Account => a
|
||||
export const activity = (a: Entity.Activity): MegalodonEntity.Activity => a
|
||||
export const announcement = (a: Entity.Announcement): MegalodonEntity.Announcement => a
|
||||
export const application = (a: Entity.Application): MegalodonEntity.Application => a
|
||||
export const attachment = (a: Entity.Attachment): MegalodonEntity.Attachment => a
|
||||
export const async_attachment = (a: Entity.AsyncAttachment) => {
|
||||
if (a.url) {
|
||||
return {
|
||||
id: a.id,
|
||||
type: a.type,
|
||||
url: a.url!,
|
||||
remote_url: a.remote_url,
|
||||
preview_url: a.preview_url,
|
||||
text_url: a.text_url,
|
||||
meta: a.meta,
|
||||
description: a.description,
|
||||
blurhash: a.blurhash
|
||||
} as MegalodonEntity.Attachment
|
||||
} else {
|
||||
return a as MegalodonEntity.AsyncAttachment
|
||||
}
|
||||
}
|
||||
export const card = (c: Entity.Card): MegalodonEntity.Card => c
|
||||
export const context = (c: Entity.Context): MegalodonEntity.Context => ({
|
||||
ancestors: Array.isArray(c.ancestors) ? c.ancestors.map(a => status(a)) : [],
|
||||
descendants: Array.isArray(c.descendants) ? c.descendants.map(d => status(d)) : []
|
||||
})
|
||||
export const conversation = (c: Entity.Conversation): MegalodonEntity.Conversation => ({
|
||||
id: c.id,
|
||||
accounts: Array.isArray(c.accounts) ? c.accounts.map(a => account(a)) : [],
|
||||
last_status: c.last_status ? status(c.last_status) : null,
|
||||
unread: c.unread
|
||||
})
|
||||
export const emoji = (e: Entity.Emoji): MegalodonEntity.Emoji => e
|
||||
export const featured_tag = (e: Entity.FeaturedTag): MegalodonEntity.FeaturedTag => e
|
||||
export const field = (f: Entity.Field): MegalodonEntity.Field => f
|
||||
export const filter = (f: Entity.Filter): MegalodonEntity.Filter => f
|
||||
export const history = (h: Entity.History): MegalodonEntity.History => h
|
||||
export const identity_proof = (i: Entity.IdentityProof): MegalodonEntity.IdentityProof => i
|
||||
export const instance = (i: Entity.Instance): MegalodonEntity.Instance => i
|
||||
export const list = (l: Entity.List): MegalodonEntity.List => l
|
||||
export const marker = (m: Entity.Marker | Record<never, never>): MegalodonEntity.Marker | Record<never, never> => m
|
||||
export const mention = (m: Entity.Mention): MegalodonEntity.Mention => m
|
||||
export const notification = (n: Entity.Notification): MegalodonEntity.Notification | UnknownNotificationTypeError => {
|
||||
const notificationType = decodeNotificationType(n.type)
|
||||
if (notificationType instanceof UnknownNotificationTypeError) return notificationType
|
||||
if (n.status) {
|
||||
return {
|
||||
account: account(n.account),
|
||||
created_at: n.created_at,
|
||||
id: n.id,
|
||||
status: status(n.status),
|
||||
type: notificationType
|
||||
}
|
||||
} else {
|
||||
return {
|
||||
account: account(n.account),
|
||||
created_at: n.created_at,
|
||||
id: n.id,
|
||||
type: notificationType
|
||||
}
|
||||
}
|
||||
}
|
||||
export const poll = (p: Entity.Poll): MegalodonEntity.Poll => p
|
||||
export const poll_option = (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 relationship = (r: Entity.Relationship): MegalodonEntity.Relationship => r
|
||||
export const report = (r: Entity.Report): MegalodonEntity.Report => r
|
||||
export const results = (r: Entity.Results): MegalodonEntity.Results => ({
|
||||
accounts: Array.isArray(r.accounts) ? r.accounts.map(a => account(a)) : [],
|
||||
statuses: Array.isArray(r.statuses) ? r.statuses.map(s => status(s)) : [],
|
||||
hashtags: Array.isArray(r.hashtags) ? r.hashtags.map(h => tag(h)) : []
|
||||
})
|
||||
export const scheduled_status = (s: Entity.ScheduledStatus): MegalodonEntity.ScheduledStatus => s
|
||||
export const source = (s: Entity.Source): MegalodonEntity.Source => s
|
||||
export const stats = (s: Entity.Stats): MegalodonEntity.Stats => s
|
||||
export const status = (s: Entity.Status): MegalodonEntity.Status => ({
|
||||
id: s.id,
|
||||
uri: s.uri,
|
||||
url: s.url,
|
||||
account: account(s.account),
|
||||
in_reply_to_id: s.in_reply_to_id,
|
||||
in_reply_to_account_id: s.in_reply_to_account_id,
|
||||
reblog: s.reblog ? status(s.reblog) : s.quote ? status(s.quote) : null,
|
||||
content: s.content,
|
||||
plain_content: null,
|
||||
created_at: s.created_at,
|
||||
edited_at: s.edited_at || null,
|
||||
emojis: Array.isArray(s.emojis) ? s.emojis.map(e => emoji(e)) : [],
|
||||
replies_count: s.replies_count,
|
||||
reblogs_count: s.reblogs_count,
|
||||
favourites_count: s.favourites_count,
|
||||
reblogged: s.reblogged,
|
||||
favourited: s.favourited,
|
||||
muted: s.muted,
|
||||
sensitive: s.sensitive,
|
||||
spoiler_text: s.spoiler_text,
|
||||
visibility: s.visibility,
|
||||
media_attachments: Array.isArray(s.media_attachments) ? s.media_attachments.map(m => attachment(m)) : [],
|
||||
mentions: Array.isArray(s.mentions) ? s.mentions.map(m => mention(m)) : [],
|
||||
tags: s.tags,
|
||||
card: s.card ? card(s.card) : null,
|
||||
poll: s.poll ? poll(s.poll) : null,
|
||||
application: s.application ? application(s.application) : null,
|
||||
language: s.language,
|
||||
pinned: s.pinned,
|
||||
emoji_reactions: [],
|
||||
bookmarked: s.bookmarked ? s.bookmarked : false,
|
||||
// Now quote is supported only fedibird.com.
|
||||
quote: s.quote !== undefined && s.quote !== null
|
||||
})
|
||||
export const status_params = (s: Entity.StatusParams): MegalodonEntity.StatusParams => s
|
||||
export const status_source = (s: Entity.StatusSource): MegalodonEntity.StatusSource => s
|
||||
export const tag = (t: Entity.Tag): MegalodonEntity.Tag => t
|
||||
export const token = (t: Entity.Token): MegalodonEntity.Token => t
|
||||
export const urls = (u: Entity.URLs): MegalodonEntity.URLs => u
|
||||
}
|
||||
}
|
||||
export default MastodonAPI
|
|
@ -37,8 +37,15 @@ namespace MastodonEntity {
|
|||
min_expiration: number
|
||||
max_expiration: number
|
||||
}
|
||||
accounts: {
|
||||
max_featured_tags: number;
|
||||
max_pinned_statuses: number;
|
||||
}
|
||||
contact_account: Account
|
||||
reactions: {
|
||||
max_reactions: number,
|
||||
}
|
||||
}
|
||||
contact_account: Account | null
|
||||
rules: Array<InstanceRule>
|
||||
}
|
||||
|
||||
|
|
|
@ -1,16 +0,0 @@
|
|||
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 const Status: MastodonEntity.NotificationType = 'status'
|
||||
export const Update: MastodonEntity.NotificationType = 'update'
|
||||
export const AdminSignup: MastodonEntity.NotificationType = 'admin.sign_up'
|
||||
export const AdminReport: MastodonEntity.NotificationType = 'admin.report'
|
||||
}
|
||||
|
||||
export default MastodonNotificationType
|
|
@ -1,348 +0,0 @@
|
|||
import WS from 'ws'
|
||||
import dayjs, { Dayjs } from 'dayjs'
|
||||
import { EventEmitter } from 'events'
|
||||
import proxyAgent, { ProxyConfig } from '../proxy_config'
|
||||
import { WebSocketInterface } from '../megalodon'
|
||||
import MastodonAPI from './api_client'
|
||||
import { UnknownNotificationTypeError } from '../notification'
|
||||
|
||||
/**
|
||||
* WebSocket
|
||||
* Pleroma is not support streaming. It is support websocket instead of streaming.
|
||||
* So this class connect to Phoenix websocket for Pleroma.
|
||||
*/
|
||||
export default class WebSocket extends EventEmitter implements WebSocketInterface {
|
||||
public url: string
|
||||
public stream: string
|
||||
public params: string | null
|
||||
public parser: Parser
|
||||
public headers: { [key: string]: string }
|
||||
public proxyConfig: ProxyConfig | false = false
|
||||
private _accessToken: string
|
||||
private _reconnectInterval: number
|
||||
private _reconnectMaxAttempts: number
|
||||
private _reconnectCurrentAttempts: number
|
||||
private _connectionClosed: boolean
|
||||
private _client: WS | null
|
||||
private _pongReceivedTimestamp: Dayjs
|
||||
private _heartbeatInterval: number = 60000
|
||||
private _pongWaiting: boolean = false
|
||||
|
||||
/**
|
||||
* @param url Full url of websocket: e.g. https://pleroma.io/api/v1/streaming
|
||||
* @param stream Stream name, please refer: https://git.pleroma.social/pleroma/pleroma/blob/develop/lib/pleroma/web/mastodon_api/mastodon_socket.ex#L19-28
|
||||
* @param accessToken The access token.
|
||||
* @param userAgent The specified User Agent.
|
||||
* @param proxyConfig Proxy setting, or set false if don't use proxy.
|
||||
*/
|
||||
constructor(
|
||||
url: string,
|
||||
stream: string,
|
||||
params: string | undefined,
|
||||
accessToken: string,
|
||||
userAgent: string,
|
||||
proxyConfig: ProxyConfig | false = false
|
||||
) {
|
||||
super()
|
||||
this.url = url
|
||||
this.stream = stream
|
||||
if (params === undefined) {
|
||||
this.params = null
|
||||
} else {
|
||||
this.params = params
|
||||
}
|
||||
this.parser = new Parser()
|
||||
this.headers = {
|
||||
'User-Agent': userAgent
|
||||
}
|
||||
this.proxyConfig = proxyConfig
|
||||
this._accessToken = accessToken
|
||||
this._reconnectInterval = 10000
|
||||
this._reconnectMaxAttempts = Infinity
|
||||
this._reconnectCurrentAttempts = 0
|
||||
this._connectionClosed = false
|
||||
this._client = null
|
||||
this._pongReceivedTimestamp = dayjs()
|
||||
}
|
||||
|
||||
/**
|
||||
* Start websocket connection.
|
||||
*/
|
||||
public start() {
|
||||
this._connectionClosed = false
|
||||
this._resetRetryParams()
|
||||
this._startWebSocketConnection()
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset connection and start new websocket connection.
|
||||
*/
|
||||
private _startWebSocketConnection() {
|
||||
this._resetConnection()
|
||||
this._setupParser()
|
||||
this._client = this._connect(this.url, this.stream, this.params, this._accessToken, this.headers, this.proxyConfig)
|
||||
this._bindSocket(this._client)
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop current connection.
|
||||
*/
|
||||
public stop() {
|
||||
this._connectionClosed = true
|
||||
this._resetConnection()
|
||||
this._resetRetryParams()
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean up current connection, and listeners.
|
||||
*/
|
||||
private _resetConnection() {
|
||||
if (this._client) {
|
||||
this._client.close(1000)
|
||||
this._client.removeAllListeners()
|
||||
this._client = null
|
||||
}
|
||||
|
||||
if (this.parser) {
|
||||
this.parser.removeAllListeners()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets the parameters used in reconnect.
|
||||
*/
|
||||
private _resetRetryParams() {
|
||||
this._reconnectCurrentAttempts = 0
|
||||
}
|
||||
|
||||
/**
|
||||
* Reconnects to the same endpoint.
|
||||
*/
|
||||
private _reconnect() {
|
||||
setTimeout(() => {
|
||||
// Skip reconnect when client is connecting.
|
||||
// https://github.com/websockets/ws/blob/7.2.1/lib/websocket.js#L365
|
||||
if (this._client && this._client.readyState === WS.CONNECTING) {
|
||||
return
|
||||
}
|
||||
|
||||
if (this._reconnectCurrentAttempts < this._reconnectMaxAttempts) {
|
||||
this._reconnectCurrentAttempts++
|
||||
this._clearBinding()
|
||||
if (this._client) {
|
||||
// In reconnect, we want to close the connection immediately,
|
||||
// because recoonect is necessary when some problems occur.
|
||||
this._client.terminate()
|
||||
}
|
||||
// Call connect methods
|
||||
console.log('Reconnecting')
|
||||
this._client = this._connect(this.url, this.stream, this.params, this._accessToken, this.headers, this.proxyConfig)
|
||||
this._bindSocket(this._client)
|
||||
}
|
||||
}, this._reconnectInterval)
|
||||
}
|
||||
|
||||
/**
|
||||
* @param url Base url of streaming endpoint.
|
||||
* @param stream The specified stream name.
|
||||
* @param accessToken Access token.
|
||||
* @param headers The specified headers.
|
||||
* @param proxyConfig Proxy setting, or set false if don't use proxy.
|
||||
* @return A WebSocket instance.
|
||||
*/
|
||||
private _connect(
|
||||
url: string,
|
||||
stream: string,
|
||||
params: string | null,
|
||||
accessToken: string,
|
||||
headers: { [key: string]: string },
|
||||
proxyConfig: ProxyConfig | false
|
||||
): WS {
|
||||
const parameter: Array<string> = [`stream=${stream}`]
|
||||
|
||||
if (params) {
|
||||
parameter.push(params)
|
||||
}
|
||||
|
||||
if (accessToken !== null) {
|
||||
parameter.push(`access_token=${accessToken}`)
|
||||
}
|
||||
const requestURL: string = `${url}/?${parameter.join('&')}`
|
||||
let options: WS.ClientOptions = {
|
||||
headers: headers
|
||||
}
|
||||
if (proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
agent: proxyAgent(proxyConfig)
|
||||
})
|
||||
}
|
||||
|
||||
const cli: WS = new WS(requestURL, options)
|
||||
return cli
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear binding event for web socket client.
|
||||
*/
|
||||
private _clearBinding() {
|
||||
if (this._client) {
|
||||
this._client.removeAllListeners('close')
|
||||
this._client.removeAllListeners('pong')
|
||||
this._client.removeAllListeners('open')
|
||||
this._client.removeAllListeners('message')
|
||||
this._client.removeAllListeners('error')
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Bind event for web socket client.
|
||||
* @param client A WebSocket instance.
|
||||
*/
|
||||
private _bindSocket(client: WS) {
|
||||
client.on('close', (code: number, _reason: Buffer) => {
|
||||
// Refer the code: https://tools.ietf.org/html/rfc6455#section-7.4
|
||||
if (code === 1000) {
|
||||
this.emit('close', {})
|
||||
} else {
|
||||
console.log(`Closed connection with ${code}`)
|
||||
// If already called close method, it does not retry.
|
||||
if (!this._connectionClosed) {
|
||||
this._reconnect()
|
||||
}
|
||||
}
|
||||
})
|
||||
client.on('pong', () => {
|
||||
this._pongWaiting = false
|
||||
this.emit('pong', {})
|
||||
this._pongReceivedTimestamp = dayjs()
|
||||
// It is required to anonymous function since get this scope in checkAlive.
|
||||
setTimeout(() => this._checkAlive(this._pongReceivedTimestamp), this._heartbeatInterval)
|
||||
})
|
||||
client.on('open', () => {
|
||||
this.emit('connect', {})
|
||||
// Call first ping event.
|
||||
setTimeout(() => {
|
||||
client.ping('')
|
||||
}, 10000)
|
||||
})
|
||||
client.on('message', (data: WS.Data, isBinary: boolean) => {
|
||||
this.parser.parse(data, isBinary)
|
||||
})
|
||||
client.on('error', (err: Error) => {
|
||||
this.emit('error', err)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up parser when receive message.
|
||||
*/
|
||||
private _setupParser() {
|
||||
this.parser.on('update', (status: MastodonAPI.Entity.Status) => {
|
||||
this.emit('update', MastodonAPI.Converter.status(status))
|
||||
})
|
||||
this.parser.on('notification', (notification: MastodonAPI.Entity.Notification) => {
|
||||
const n = MastodonAPI.Converter.notification(notification)
|
||||
if (n instanceof UnknownNotificationTypeError) {
|
||||
console.warn(`Unknown notification event has received: ${notification}`)
|
||||
} else {
|
||||
this.emit('notification', n)
|
||||
}
|
||||
})
|
||||
this.parser.on('delete', (id: string) => {
|
||||
this.emit('delete', id)
|
||||
})
|
||||
this.parser.on('conversation', (conversation: MastodonAPI.Entity.Conversation) => {
|
||||
this.emit('conversation', MastodonAPI.Converter.conversation(conversation))
|
||||
})
|
||||
this.parser.on('status_update', (status: MastodonAPI.Entity.Status) => {
|
||||
this.emit('status_update', MastodonAPI.Converter.status(status))
|
||||
})
|
||||
this.parser.on('error', (err: Error) => {
|
||||
this.emit('parser-error', err)
|
||||
})
|
||||
this.parser.on('heartbeat', _ => {
|
||||
this.emit('heartbeat', 'heartbeat')
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Call ping and wait to pong.
|
||||
*/
|
||||
private _checkAlive(timestamp: Dayjs) {
|
||||
const now: Dayjs = dayjs()
|
||||
// Block multiple calling, if multiple pong event occur.
|
||||
// It the duration is less than interval, through ping.
|
||||
if (now.diff(timestamp) > this._heartbeatInterval - 1000 && !this._connectionClosed) {
|
||||
// Skip ping when client is connecting.
|
||||
// https://github.com/websockets/ws/blob/7.2.1/lib/websocket.js#L289
|
||||
if (this._client && this._client.readyState !== WS.CONNECTING) {
|
||||
this._pongWaiting = true
|
||||
this._client.ping('')
|
||||
setTimeout(() => {
|
||||
if (this._pongWaiting) {
|
||||
this._pongWaiting = false
|
||||
this._reconnect()
|
||||
}
|
||||
}, 10000)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parser
|
||||
* This class provides parser for websocket message.
|
||||
*/
|
||||
export class Parser extends EventEmitter {
|
||||
/**
|
||||
* @param message Message body of websocket.
|
||||
*/
|
||||
public parse(data: WS.Data, isBinary: boolean) {
|
||||
const message = isBinary ? data : data.toString()
|
||||
if (typeof message !== 'string') {
|
||||
this.emit('heartbeat', {})
|
||||
return
|
||||
}
|
||||
|
||||
if (message === '') {
|
||||
this.emit('heartbeat', {})
|
||||
return
|
||||
}
|
||||
|
||||
let event = ''
|
||||
let payload = ''
|
||||
let mes = {}
|
||||
try {
|
||||
const obj = JSON.parse(message)
|
||||
event = obj.event
|
||||
payload = obj.payload
|
||||
mes = JSON.parse(payload)
|
||||
} catch (err) {
|
||||
// delete event does not have json object
|
||||
if (event !== 'delete') {
|
||||
this.emit('error', new Error(`Error parsing websocket reply: ${message}, error message: ${err}`))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
switch (event) {
|
||||
case 'update':
|
||||
this.emit('update', mes as MastodonAPI.Entity.Status)
|
||||
break
|
||||
case 'notification':
|
||||
this.emit('notification', mes as MastodonAPI.Entity.Notification)
|
||||
break
|
||||
case 'conversation':
|
||||
this.emit('conversation', mes as MastodonAPI.Entity.Conversation)
|
||||
break
|
||||
case 'delete':
|
||||
this.emit('delete', payload)
|
||||
break
|
||||
case 'status.update':
|
||||
this.emit('status_update', mes as MastodonAPI.Entity.Status)
|
||||
break
|
||||
default:
|
||||
this.emit('error', new Error(`Unknown event has received: ${message}`))
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,11 +1,6 @@
|
|||
import Response from './response'
|
||||
import OAuth from './oauth'
|
||||
import Pleroma from './pleroma'
|
||||
import { ProxyConfig } from './proxy_config'
|
||||
import Mastodon from './mastodon'
|
||||
import Entity from './entity'
|
||||
import Misskey from './misskey'
|
||||
import Friendica from './friendica'
|
||||
|
||||
export interface WebSocketInterface {
|
||||
start(): void
|
||||
|
@ -1413,42 +1408,3 @@ export class NodeinfoError extends Error {
|
|||
Object.setPrototypeOf(this, new.target.prototype)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get client for each SNS according to megalodon interface.
|
||||
*
|
||||
* @param sns Name of your SNS, `mastodon` or `pleroma`.
|
||||
* @param baseUrl hostname or base URL.
|
||||
* @param accessToken access token from OAuth2 authorization
|
||||
* @param userAgent UserAgent is specified in header on request.
|
||||
* @param proxyConfig Proxy setting, or set false if don't use proxy.
|
||||
* @return Client instance for each SNS you specified.
|
||||
*/
|
||||
const generator = (
|
||||
sns: 'mastodon' | 'pleroma' | 'misskey' | 'friendica',
|
||||
baseUrl: string,
|
||||
accessToken: string | null = null,
|
||||
userAgent: string | null = null,
|
||||
proxyConfig: ProxyConfig | false = false
|
||||
): MegalodonInterface => {
|
||||
switch (sns) {
|
||||
case 'pleroma': {
|
||||
const pleroma = new Pleroma(baseUrl, accessToken, userAgent, proxyConfig)
|
||||
return pleroma
|
||||
}
|
||||
case 'misskey': {
|
||||
const misskey = new Misskey(baseUrl, accessToken, userAgent, proxyConfig)
|
||||
return misskey
|
||||
}
|
||||
case 'friendica': {
|
||||
const friendica = new Friendica(baseUrl, accessToken, userAgent, proxyConfig)
|
||||
return friendica
|
||||
}
|
||||
case 'mastodon': {
|
||||
const mastodon = new Mastodon(baseUrl, accessToken, userAgent, proxyConfig)
|
||||
return mastodon
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default generator
|
||||
|
|
|
@ -303,8 +303,8 @@ export default class Misskey implements MegalodonInterface {
|
|||
max_id?: string
|
||||
since_id?: string
|
||||
pinned?: boolean
|
||||
exclude_replies: boolean
|
||||
exclude_reblogs: boolean
|
||||
exclude_replies?: boolean
|
||||
exclude_reblogs?: boolean
|
||||
only_media?: boolean
|
||||
}
|
||||
): Promise<Response<Array<Entity.Status>>> {
|
||||
|
@ -2352,6 +2352,18 @@ export default class Misskey implements MegalodonInterface {
|
|||
}
|
||||
}))
|
||||
}
|
||||
default: {
|
||||
return {
|
||||
status: 400,
|
||||
statusText: 'bad request',
|
||||
headers: {},
|
||||
data: {
|
||||
accounts: [],
|
||||
statuses: [],
|
||||
hashtags: [],
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -1,824 +0,0 @@
|
|||
import axios, { AxiosResponse, AxiosRequestConfig } from 'axios'
|
||||
import objectAssignDeep from 'object-assign-deep'
|
||||
|
||||
import MegalodonEntity from '../entity'
|
||||
import PleromaEntity from './entity'
|
||||
import Response from '../response'
|
||||
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, { UnknownNotificationTypeError } from '../notification'
|
||||
import PleromaNotificationType from './notification'
|
||||
|
||||
namespace PleromaAPI {
|
||||
export namespace Entity {
|
||||
export type Account = PleromaEntity.Account
|
||||
export type Activity = PleromaEntity.Activity
|
||||
export type Announcement = PleromaEntity.Announcement
|
||||
export type Application = PleromaEntity.Application
|
||||
export type AsyncAttachment = PleromaEntity.AsyncAttachment
|
||||
export type Attachment = PleromaEntity.Attachment
|
||||
export type Card = PleromaEntity.Card
|
||||
export type Context = PleromaEntity.Context
|
||||
export type Conversation = PleromaEntity.Conversation
|
||||
export type Emoji = PleromaEntity.Emoji
|
||||
export type FeaturedTag = PleromaEntity.FeaturedTag
|
||||
export type Field = PleromaEntity.Field
|
||||
export type Filter = PleromaEntity.Filter
|
||||
export type History = PleromaEntity.History
|
||||
export type IdentityProof = PleromaEntity.IdentityProof
|
||||
export type Instance = PleromaEntity.Instance
|
||||
export type List = PleromaEntity.List
|
||||
export type Marker = PleromaEntity.Marker
|
||||
export type Mention = PleromaEntity.Mention
|
||||
export type Notification = PleromaEntity.Notification
|
||||
export type Poll = PleromaEntity.Poll
|
||||
export type PollOption = PleromaEntity.PollOption
|
||||
export type Preferences = PleromaEntity.Preferences
|
||||
export type PushSubscription = PleromaEntity.PushSubscription
|
||||
export type Reaction = PleromaEntity.Reaction
|
||||
export type Relationship = PleromaEntity.Relationship
|
||||
export type Report = PleromaEntity.Report
|
||||
export type Results = PleromaEntity.Results
|
||||
export type ScheduledStatus = PleromaEntity.ScheduledStatus
|
||||
export type Source = PleromaEntity.Source
|
||||
export type Stats = PleromaEntity.Stats
|
||||
export type Status = PleromaEntity.Status
|
||||
export type StatusParams = PleromaEntity.StatusParams
|
||||
export type StatusSource = PleromaEntity.StatusSource
|
||||
export type Tag = PleromaEntity.Tag
|
||||
export type Token = PleromaEntity.Token
|
||||
export type URLs = PleromaEntity.URLs
|
||||
}
|
||||
|
||||
export namespace Converter {
|
||||
export const decodeNotificationType = (
|
||||
t: PleromaEntity.NotificationType
|
||||
): MegalodonEntity.NotificationType | UnknownNotificationTypeError => {
|
||||
switch (t) {
|
||||
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.PollExpired
|
||||
case PleromaNotificationType.PleromaEmojiReaction:
|
||||
return NotificationType.EmojiReaction
|
||||
case PleromaNotificationType.FollowRequest:
|
||||
return NotificationType.FollowRequest
|
||||
case PleromaNotificationType.Update:
|
||||
return NotificationType.Update
|
||||
case PleromaNotificationType.Move:
|
||||
return NotificationType.Move
|
||||
default:
|
||||
return new UnknownNotificationTypeError()
|
||||
}
|
||||
}
|
||||
export const encodeNotificationType = (
|
||||
t: MegalodonEntity.NotificationType
|
||||
): PleromaEntity.NotificationType | UnknownNotificationTypeError => {
|
||||
switch (t) {
|
||||
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.PollExpired:
|
||||
return PleromaNotificationType.Poll
|
||||
case NotificationType.EmojiReaction:
|
||||
return PleromaNotificationType.PleromaEmojiReaction
|
||||
case NotificationType.FollowRequest:
|
||||
return PleromaNotificationType.FollowRequest
|
||||
case NotificationType.Update:
|
||||
return PleromaNotificationType.Update
|
||||
case NotificationType.Move:
|
||||
return PleromaNotificationType.Move
|
||||
default:
|
||||
return new UnknownNotificationTypeError()
|
||||
}
|
||||
}
|
||||
|
||||
export const account = (a: Entity.Account): MegalodonEntity.Account => {
|
||||
return {
|
||||
id: a.id,
|
||||
username: a.username,
|
||||
acct: a.acct,
|
||||
display_name: a.display_name,
|
||||
locked: a.locked,
|
||||
discoverable: a.discoverable,
|
||||
group: null,
|
||||
noindex: a.noindex,
|
||||
suspended: a.suspended,
|
||||
limited: a.limited,
|
||||
created_at: a.created_at,
|
||||
followers_count: a.followers_count,
|
||||
following_count: a.following_count,
|
||||
statuses_count: a.statuses_count,
|
||||
note: a.note,
|
||||
url: a.url,
|
||||
avatar: a.avatar,
|
||||
avatar_static: a.avatar_static,
|
||||
header: a.header,
|
||||
header_static: a.header_static,
|
||||
emojis: a.emojis.map(e => emoji(e)),
|
||||
moved: a.moved ? account(a.moved) : null,
|
||||
fields: a.fields,
|
||||
bot: a.bot,
|
||||
source: a.source
|
||||
}
|
||||
}
|
||||
export const activity = (a: Entity.Activity): MegalodonEntity.Activity => a
|
||||
export const announcement = (a: Entity.Announcement): MegalodonEntity.Announcement => ({
|
||||
id: a.id,
|
||||
content: a.content,
|
||||
starts_at: a.starts_at,
|
||||
ends_at: a.ends_at,
|
||||
published: a.published,
|
||||
all_day: a.all_day,
|
||||
published_at: a.published_at,
|
||||
updated_at: a.updated_at,
|
||||
read: null,
|
||||
mentions: a.mentions,
|
||||
statuses: a.statuses,
|
||||
tags: a.tags,
|
||||
emojis: a.emojis,
|
||||
reactions: a.reactions
|
||||
})
|
||||
export const application = (a: Entity.Application): MegalodonEntity.Application => a
|
||||
export const attachment = (a: Entity.Attachment): MegalodonEntity.Attachment => a
|
||||
export const async_attachment = (a: Entity.AsyncAttachment) => {
|
||||
if (a.url) {
|
||||
return {
|
||||
id: a.id,
|
||||
type: a.type,
|
||||
url: a.url!,
|
||||
remote_url: a.remote_url,
|
||||
preview_url: a.preview_url,
|
||||
text_url: a.text_url,
|
||||
meta: a.meta,
|
||||
description: a.description,
|
||||
blurhash: a.blurhash
|
||||
} as MegalodonEntity.Attachment
|
||||
} else {
|
||||
return a as MegalodonEntity.AsyncAttachment
|
||||
}
|
||||
}
|
||||
export const card = (c: Entity.Card): MegalodonEntity.Card => ({
|
||||
url: c.url,
|
||||
title: c.title,
|
||||
description: c.description,
|
||||
type: c.type,
|
||||
image: c.image,
|
||||
author_name: null,
|
||||
author_url: null,
|
||||
provider_name: c.provider_name,
|
||||
provider_url: c.provider_url,
|
||||
html: null,
|
||||
width: null,
|
||||
height: null,
|
||||
embed_url: null,
|
||||
blurhash: null
|
||||
})
|
||||
export const context = (c: Entity.Context): MegalodonEntity.Context => ({
|
||||
ancestors: Array.isArray(c.ancestors) ? c.ancestors.map(a => status(a)) : [],
|
||||
descendants: Array.isArray(c.descendants) ? c.descendants.map(d => status(d)) : []
|
||||
})
|
||||
export const conversation = (c: Entity.Conversation): MegalodonEntity.Conversation => ({
|
||||
id: c.id,
|
||||
accounts: Array.isArray(c.accounts) ? c.accounts.map(a => account(a)) : [],
|
||||
last_status: c.last_status ? status(c.last_status) : null,
|
||||
unread: c.unread
|
||||
})
|
||||
export const emoji = (e: Entity.Emoji): MegalodonEntity.Emoji => ({
|
||||
shortcode: e.shortcode,
|
||||
static_url: e.static_url,
|
||||
url: e.url,
|
||||
visible_in_picker: e.visible_in_picker
|
||||
})
|
||||
export const featured_tag = (f: Entity.FeaturedTag): MegalodonEntity.FeaturedTag => f
|
||||
export const field = (f: Entity.Field): MegalodonEntity.Field => f
|
||||
export const filter = (f: Entity.Filter): MegalodonEntity.Filter => f
|
||||
export const history = (h: Entity.History): MegalodonEntity.History => h
|
||||
export const identity_proof = (i: Entity.IdentityProof): MegalodonEntity.IdentityProof => i
|
||||
export const instance = (i: Entity.Instance): MegalodonEntity.Instance => ({
|
||||
uri: i.uri,
|
||||
title: i.title,
|
||||
description: i.description,
|
||||
email: i.email,
|
||||
version: i.version,
|
||||
thumbnail: i.thumbnail,
|
||||
urls: urls(i.urls),
|
||||
stats: stats(i.stats),
|
||||
languages: i.languages,
|
||||
registrations: i.registrations,
|
||||
approval_required: i.approval_required,
|
||||
configuration: {
|
||||
statuses: {
|
||||
max_characters: i.max_toot_chars,
|
||||
max_media_attachments: i.max_media_attachments
|
||||
},
|
||||
polls: {
|
||||
max_options: i.poll_limits.max_options,
|
||||
max_characters_per_option: i.poll_limits.max_option_chars,
|
||||
min_expiration: i.poll_limits.min_expiration,
|
||||
max_expiration: i.poll_limits.max_expiration
|
||||
}
|
||||
}
|
||||
})
|
||||
export const list = (l: Entity.List): MegalodonEntity.List => ({
|
||||
id: l.id,
|
||||
title: l.title,
|
||||
replies_policy: null
|
||||
})
|
||||
export const marker = (m: Entity.Marker | Record<never, never>): MegalodonEntity.Marker | Record<never, never> => {
|
||||
if ((m as any).notifications) {
|
||||
const mm = m as Entity.Marker
|
||||
return {
|
||||
notifications: {
|
||||
last_read_id: mm.notifications.last_read_id,
|
||||
version: mm.notifications.version,
|
||||
updated_at: mm.notifications.updated_at,
|
||||
unread_count: mm.notifications.pleroma.unread_count
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return {}
|
||||
}
|
||||
}
|
||||
export const mention = (m: Entity.Mention): MegalodonEntity.Mention => m
|
||||
export const notification = (n: Entity.Notification): MegalodonEntity.Notification | UnknownNotificationTypeError => {
|
||||
const notificationType = decodeNotificationType(n.type)
|
||||
if (notificationType instanceof UnknownNotificationTypeError) return notificationType
|
||||
if (n.status && n.emoji) {
|
||||
return {
|
||||
id: n.id,
|
||||
account: account(n.account),
|
||||
created_at: n.created_at,
|
||||
status: status(n.status),
|
||||
emoji: n.emoji,
|
||||
type: notificationType
|
||||
}
|
||||
} else if (n.status) {
|
||||
return {
|
||||
id: n.id,
|
||||
account: account(n.account),
|
||||
created_at: n.created_at,
|
||||
status: status(n.status),
|
||||
type: notificationType
|
||||
}
|
||||
} else if (n.target) {
|
||||
return {
|
||||
id: n.id,
|
||||
account: account(n.account),
|
||||
created_at: n.created_at,
|
||||
target: account(n.target),
|
||||
type: notificationType
|
||||
}
|
||||
} else {
|
||||
return {
|
||||
id: n.id,
|
||||
account: account(n.account),
|
||||
created_at: n.created_at,
|
||||
type: notificationType
|
||||
}
|
||||
}
|
||||
}
|
||||
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 = {
|
||||
count: r.count,
|
||||
me: r.me,
|
||||
name: r.name
|
||||
}
|
||||
if (r.accounts) {
|
||||
return Object.assign({}, p, {
|
||||
accounts: r.accounts.map(a => account(a))
|
||||
})
|
||||
}
|
||||
return p
|
||||
}
|
||||
export const relationship = (r: Entity.Relationship): MegalodonEntity.Relationship => ({
|
||||
id: r.id,
|
||||
following: r.following,
|
||||
followed_by: r.followed_by,
|
||||
blocking: r.blocking,
|
||||
blocked_by: r.blocked_by,
|
||||
muting: r.muting,
|
||||
muting_notifications: r.muting_notifications,
|
||||
requested: r.requested,
|
||||
domain_blocking: r.domain_blocking,
|
||||
showing_reblogs: r.showing_reblogs,
|
||||
endorsed: r.endorsed,
|
||||
notifying: r.notifying,
|
||||
note: r.note
|
||||
})
|
||||
export const report = (r: Entity.Report): MegalodonEntity.Report => ({
|
||||
id: r.id,
|
||||
action_taken: r.action_taken,
|
||||
action_taken_at: null,
|
||||
category: null,
|
||||
comment: null,
|
||||
forwarded: null,
|
||||
status_ids: null,
|
||||
rule_ids: null
|
||||
})
|
||||
export const results = (r: Entity.Results): MegalodonEntity.Results => ({
|
||||
accounts: Array.isArray(r.accounts) ? r.accounts.map(a => account(a)) : [],
|
||||
statuses: Array.isArray(r.statuses) ? r.statuses.map(s => status(s)) : [],
|
||||
hashtags: Array.isArray(r.hashtags) ? r.hashtags.map(h => tag(h)) : []
|
||||
})
|
||||
export const scheduled_status = (s: Entity.ScheduledStatus): MegalodonEntity.ScheduledStatus => ({
|
||||
id: s.id,
|
||||
scheduled_at: s.scheduled_at,
|
||||
params: status_params(s.params),
|
||||
media_attachments: Array.isArray(s.media_attachments) ? s.media_attachments.map(m => attachment(m)) : null
|
||||
})
|
||||
export const source = (s: Entity.Source): MegalodonEntity.Source => s
|
||||
export const stats = (s: Entity.Stats): MegalodonEntity.Stats => s
|
||||
export const status = (s: Entity.Status): MegalodonEntity.Status => ({
|
||||
id: s.id,
|
||||
uri: s.uri,
|
||||
url: s.url,
|
||||
account: account(s.account),
|
||||
in_reply_to_id: s.in_reply_to_id,
|
||||
in_reply_to_account_id: s.in_reply_to_account_id,
|
||||
reblog: s.reblog ? status(s.reblog) : null,
|
||||
content: s.content,
|
||||
plain_content: s.pleroma.content?.['text/plain'] ? s.pleroma.content['text/plain'] : null,
|
||||
created_at: s.created_at,
|
||||
edited_at: s.edited_at || null,
|
||||
emojis: Array.isArray(s.emojis) ? s.emojis.map(e => emoji(e)) : [],
|
||||
replies_count: s.replies_count,
|
||||
reblogs_count: s.reblogs_count,
|
||||
favourites_count: s.favourites_count,
|
||||
reblogged: s.reblogged,
|
||||
favourited: s.favourited,
|
||||
muted: s.muted,
|
||||
sensitive: s.sensitive,
|
||||
spoiler_text: s.spoiler_text,
|
||||
visibility: s.visibility,
|
||||
media_attachments: Array.isArray(s.media_attachments) ? s.media_attachments.map(m => attachment(m)) : [],
|
||||
mentions: Array.isArray(s.mentions) ? s.mentions.map(m => mention(m)) : [],
|
||||
tags: s.tags,
|
||||
card: s.card ? card(s.card) : null,
|
||||
poll: s.poll ? poll(s.poll) : null,
|
||||
application: s.application ? application(s.application) : null,
|
||||
language: s.language,
|
||||
pinned: s.pinned,
|
||||
emoji_reactions: Array.isArray(s.pleroma.emoji_reactions) ? s.pleroma.emoji_reactions.map(r => reaction(r)) : [],
|
||||
bookmarked: s.bookmarked ? s.bookmarked : false,
|
||||
quote: s.reblog !== null && s.reblog.content !== s.content
|
||||
})
|
||||
export const status_params = (s: Entity.StatusParams): MegalodonEntity.StatusParams => {
|
||||
return {
|
||||
text: s.text,
|
||||
in_reply_to_id: s.in_reply_to_id,
|
||||
media_ids: Array.isArray(s.media_ids) ? s.media_ids : null,
|
||||
sensitive: s.sensitive,
|
||||
spoiler_text: s.spoiler_text,
|
||||
visibility: s.visibility,
|
||||
scheduled_at: s.scheduled_at,
|
||||
application_id: null
|
||||
}
|
||||
}
|
||||
export const status_source = (s: Entity.StatusSource): MegalodonEntity.StatusSource => s
|
||||
export const tag = (t: Entity.Tag): MegalodonEntity.Tag => t
|
||||
export const token = (t: Entity.Token): MegalodonEntity.Token => t
|
||||
export const urls = (u: Entity.URLs): MegalodonEntity.URLs => u
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface
|
||||
*/
|
||||
export interface Interface {
|
||||
get<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
|
||||
put<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
|
||||
putForm<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
|
||||
patch<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
|
||||
patchForm<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>>
|
||||
postForm<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
|
||||
del<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
|
||||
cancel(): void
|
||||
socket(path: string, stream: string, params?: string): WebSocket
|
||||
}
|
||||
|
||||
/**
|
||||
* Mastodon API client.
|
||||
*
|
||||
* Using axios for request, you will handle promises.
|
||||
*/
|
||||
export class Client implements Interface {
|
||||
static DEFAULT_SCOPE = DEFAULT_SCOPE
|
||||
static DEFAULT_URL = 'https://pleroma.io'
|
||||
static NO_REDIRECT = NO_REDIRECT
|
||||
|
||||
private accessToken: string | null
|
||||
private baseUrl: string
|
||||
private userAgent: string
|
||||
private abortController: AbortController
|
||||
private proxyConfig: ProxyConfig | false = false
|
||||
|
||||
/**
|
||||
* @param baseUrl hostname or base URL
|
||||
* @param accessToken access token from OAuth2 authorization
|
||||
* @param userAgent UserAgent is specified in header on request.
|
||||
* @param proxyConfig Proxy setting, or set false if don't use proxy.
|
||||
*/
|
||||
constructor(
|
||||
baseUrl: string,
|
||||
accessToken: string | null = null,
|
||||
userAgent: string = DEFAULT_UA,
|
||||
proxyConfig: ProxyConfig | false = false
|
||||
) {
|
||||
this.accessToken = accessToken
|
||||
this.baseUrl = baseUrl
|
||||
this.userAgent = userAgent
|
||||
this.proxyConfig = proxyConfig
|
||||
this.abortController = new AbortController()
|
||||
axios.defaults.signal = this.abortController.signal
|
||||
}
|
||||
|
||||
/**
|
||||
* GET request to mastodon REST API.
|
||||
* @param path relative path from baseUrl
|
||||
* @param params Query parameters
|
||||
* @param headers Request header object
|
||||
*/
|
||||
public async get<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
|
||||
let options: AxiosRequestConfig = {
|
||||
params: params,
|
||||
headers: headers
|
||||
}
|
||||
if (this.accessToken) {
|
||||
options = objectAssignDeep({}, options, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.accessToken}`
|
||||
}
|
||||
})
|
||||
}
|
||||
if (this.proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
httpAgent: proxyAgent(this.proxyConfig),
|
||||
httpsAgent: proxyAgent(this.proxyConfig)
|
||||
})
|
||||
}
|
||||
return axios
|
||||
.get<T>(this.baseUrl + path, options)
|
||||
.catch((err: Error) => {
|
||||
if (axios.isCancel(err)) {
|
||||
throw new RequestCanceledError(err.message)
|
||||
} else {
|
||||
throw err
|
||||
}
|
||||
})
|
||||
.then((resp: AxiosResponse<T>) => {
|
||||
const res: Response<T> = {
|
||||
data: resp.data,
|
||||
status: resp.status,
|
||||
statusText: resp.statusText,
|
||||
headers: resp.headers
|
||||
}
|
||||
return res
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* PUT request to mastodon REST API.
|
||||
* @param path relative path from baseUrl
|
||||
* @param params Form data. If you want to post file, please use FormData()
|
||||
* @param headers Request header object
|
||||
*/
|
||||
public async put<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
|
||||
let options: AxiosRequestConfig = {
|
||||
headers: headers,
|
||||
maxContentLength: Infinity,
|
||||
maxBodyLength: Infinity
|
||||
}
|
||||
if (this.accessToken) {
|
||||
options = objectAssignDeep({}, options, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.accessToken}`
|
||||
}
|
||||
})
|
||||
}
|
||||
if (this.proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
httpAgent: proxyAgent(this.proxyConfig),
|
||||
httpsAgent: proxyAgent(this.proxyConfig)
|
||||
})
|
||||
}
|
||||
return axios
|
||||
.put<T>(this.baseUrl + path, params, options)
|
||||
.catch((err: Error) => {
|
||||
if (axios.isCancel(err)) {
|
||||
throw new RequestCanceledError(err.message)
|
||||
} else {
|
||||
throw err
|
||||
}
|
||||
})
|
||||
.then((resp: AxiosResponse<T>) => {
|
||||
const res: Response<T> = {
|
||||
data: resp.data,
|
||||
status: resp.status,
|
||||
statusText: resp.statusText,
|
||||
headers: resp.headers
|
||||
}
|
||||
return res
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* PUT request to mastodon REST API for multipart.
|
||||
* @param path relative path from baseUrl
|
||||
* @param params Form data. If you want to post file, please use FormData()
|
||||
* @param headers Request header object
|
||||
*/
|
||||
public async putForm<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
|
||||
let options: AxiosRequestConfig = {
|
||||
headers: headers,
|
||||
maxContentLength: Infinity,
|
||||
maxBodyLength: Infinity
|
||||
}
|
||||
if (this.accessToken) {
|
||||
options = objectAssignDeep({}, options, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.accessToken}`
|
||||
}
|
||||
})
|
||||
}
|
||||
if (this.proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
httpAgent: proxyAgent(this.proxyConfig),
|
||||
httpsAgent: proxyAgent(this.proxyConfig)
|
||||
})
|
||||
}
|
||||
return axios
|
||||
.putForm<T>(this.baseUrl + path, params, options)
|
||||
.catch((err: Error) => {
|
||||
if (axios.isCancel(err)) {
|
||||
throw new RequestCanceledError(err.message)
|
||||
} else {
|
||||
throw err
|
||||
}
|
||||
})
|
||||
.then((resp: AxiosResponse<T>) => {
|
||||
const res: Response<T> = {
|
||||
data: resp.data,
|
||||
status: resp.status,
|
||||
statusText: resp.statusText,
|
||||
headers: resp.headers
|
||||
}
|
||||
return res
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* PATCH request to mastodon REST API.
|
||||
* @param path relative path from baseUrl
|
||||
* @param params Form data. If you want to post file, please use FormData()
|
||||
* @param headers Request header object
|
||||
*/
|
||||
public async patch<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
|
||||
let options: AxiosRequestConfig = {
|
||||
headers: headers,
|
||||
maxContentLength: Infinity,
|
||||
maxBodyLength: Infinity
|
||||
}
|
||||
if (this.accessToken) {
|
||||
options = objectAssignDeep({}, options, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.accessToken}`
|
||||
}
|
||||
})
|
||||
}
|
||||
if (this.proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
httpAgent: proxyAgent(this.proxyConfig),
|
||||
httpsAgent: proxyAgent(this.proxyConfig)
|
||||
})
|
||||
}
|
||||
return axios
|
||||
.patch<T>(this.baseUrl + path, params, options)
|
||||
.catch((err: Error) => {
|
||||
if (axios.isCancel(err)) {
|
||||
throw new RequestCanceledError(err.message)
|
||||
} else {
|
||||
throw err
|
||||
}
|
||||
})
|
||||
.then((resp: AxiosResponse<T>) => {
|
||||
const res: Response<T> = {
|
||||
data: resp.data,
|
||||
status: resp.status,
|
||||
statusText: resp.statusText,
|
||||
headers: resp.headers
|
||||
}
|
||||
return res
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* PATCH request to mastodon REST API for multipart.
|
||||
* @param path relative path from baseUrl
|
||||
* @param params Form data. If you want to post file, please use FormData()
|
||||
* @param headers Request header object
|
||||
*/
|
||||
public async patchForm<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
|
||||
let options: AxiosRequestConfig = {
|
||||
headers: headers,
|
||||
maxContentLength: Infinity,
|
||||
maxBodyLength: Infinity
|
||||
}
|
||||
if (this.accessToken) {
|
||||
options = objectAssignDeep({}, options, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.accessToken}`
|
||||
}
|
||||
})
|
||||
}
|
||||
if (this.proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
httpAgent: proxyAgent(this.proxyConfig),
|
||||
httpsAgent: proxyAgent(this.proxyConfig)
|
||||
})
|
||||
}
|
||||
return axios
|
||||
.patchForm<T>(this.baseUrl + path, params, options)
|
||||
.catch((err: Error) => {
|
||||
if (axios.isCancel(err)) {
|
||||
throw new RequestCanceledError(err.message)
|
||||
} else {
|
||||
throw err
|
||||
}
|
||||
})
|
||||
.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 mastodon REST API.
|
||||
* @param path relative path from baseUrl
|
||||
* @param params Form data
|
||||
* @param headers Request header object
|
||||
*/
|
||||
public async post<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
|
||||
let options: AxiosRequestConfig = {
|
||||
headers: headers,
|
||||
maxContentLength: Infinity,
|
||||
maxBodyLength: Infinity
|
||||
}
|
||||
if (this.accessToken) {
|
||||
options = objectAssignDeep({}, options, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.accessToken}`
|
||||
}
|
||||
})
|
||||
}
|
||||
if (this.proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
httpAgent: proxyAgent(this.proxyConfig),
|
||||
httpsAgent: proxyAgent(this.proxyConfig)
|
||||
})
|
||||
}
|
||||
return axios.post<T>(this.baseUrl + path, params, 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 mastodon REST API for multipart.
|
||||
* @param path relative path from baseUrl
|
||||
* @param params Form data
|
||||
* @param headers Request header object
|
||||
*/
|
||||
public async postForm<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
|
||||
let options: AxiosRequestConfig = {
|
||||
headers: headers,
|
||||
maxContentLength: Infinity,
|
||||
maxBodyLength: Infinity
|
||||
}
|
||||
if (this.accessToken) {
|
||||
options = objectAssignDeep({}, options, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.accessToken}`
|
||||
}
|
||||
})
|
||||
}
|
||||
if (this.proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
httpAgent: proxyAgent(this.proxyConfig),
|
||||
httpsAgent: proxyAgent(this.proxyConfig)
|
||||
})
|
||||
}
|
||||
return axios.postForm<T>(this.baseUrl + path, params, options).then((resp: AxiosResponse<T>) => {
|
||||
const res: Response<T> = {
|
||||
data: resp.data,
|
||||
status: resp.status,
|
||||
statusText: resp.statusText,
|
||||
headers: resp.headers
|
||||
}
|
||||
return res
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* DELETE request to mastodon REST API.
|
||||
* @param path relative path from baseUrl
|
||||
* @param params Form data
|
||||
* @param headers Request header object
|
||||
*/
|
||||
public async del<T>(path: string, params = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
|
||||
let options: AxiosRequestConfig = {
|
||||
data: params,
|
||||
headers: headers,
|
||||
maxContentLength: Infinity,
|
||||
maxBodyLength: Infinity
|
||||
}
|
||||
if (this.accessToken) {
|
||||
options = objectAssignDeep({}, options, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.accessToken}`
|
||||
}
|
||||
})
|
||||
}
|
||||
if (this.proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
httpAgent: proxyAgent(this.proxyConfig),
|
||||
httpsAgent: proxyAgent(this.proxyConfig)
|
||||
})
|
||||
}
|
||||
return axios
|
||||
.delete(this.baseUrl + path, options)
|
||||
.catch((err: Error) => {
|
||||
if (axios.isCancel(err)) {
|
||||
throw new RequestCanceledError(err.message)
|
||||
} else {
|
||||
throw err
|
||||
}
|
||||
})
|
||||
.then((resp: AxiosResponse) => {
|
||||
const res: Response<T> = {
|
||||
data: resp.data,
|
||||
status: resp.status,
|
||||
statusText: resp.statusText,
|
||||
headers: resp.headers
|
||||
}
|
||||
return res
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancel all requests in this instance.
|
||||
* @returns void
|
||||
*/
|
||||
public cancel(): void {
|
||||
return this.abortController.abort()
|
||||
}
|
||||
|
||||
/**
|
||||
* Get connection and receive websocket connection for Pleroma API.
|
||||
*
|
||||
* @param path relative path from baseUrl: normally it is `/streaming`.
|
||||
* @param stream Stream name, please refer: https://git.pleroma.social/pleroma/pleroma/blob/develop/lib/pleroma/web/mastodon_api/mastodon_socket.ex#L19-28
|
||||
* @returns WebSocket, which inherits from EventEmitter
|
||||
*/
|
||||
public socket(path: string, stream: string, params?: string): WebSocket {
|
||||
if (!this.accessToken) {
|
||||
throw new Error('accessToken is required')
|
||||
}
|
||||
const url = this.baseUrl + path
|
||||
const streaming = new WebSocket(url, stream, params, this.accessToken, this.userAgent, this.proxyConfig)
|
||||
process.nextTick(() => {
|
||||
streaming.start()
|
||||
})
|
||||
return streaming
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default PleromaAPI
|
|
@ -1,31 +0,0 @@
|
|||
/// <reference path="emoji.ts" />
|
||||
/// <reference path="source.ts" />
|
||||
/// <reference path="field.ts" />
|
||||
namespace PleromaEntity {
|
||||
export type Account = {
|
||||
id: string
|
||||
username: string
|
||||
acct: string
|
||||
display_name: string
|
||||
locked: boolean
|
||||
discoverable?: boolean
|
||||
noindex: boolean | null
|
||||
suspended: boolean | null
|
||||
limited: boolean | null
|
||||
created_at: string
|
||||
followers_count: number
|
||||
following_count: number
|
||||
statuses_count: number
|
||||
note: string
|
||||
url: string
|
||||
avatar: string
|
||||
avatar_static: string
|
||||
header: string
|
||||
header_static: string
|
||||
emojis: Array<Emoji>
|
||||
moved: Account | null
|
||||
fields: Array<Field>
|
||||
bot: boolean
|
||||
source?: Source
|
||||
}
|
||||
}
|
|
@ -1,8 +0,0 @@
|
|||
namespace PleromaEntity {
|
||||
export type Activity = {
|
||||
week: string
|
||||
statuses: string
|
||||
logins: string
|
||||
registrations: string
|
||||
}
|
||||
}
|
|
@ -1,39 +0,0 @@
|
|||
/// <reference path="emoji.ts" />
|
||||
|
||||
namespace PleromaEntity {
|
||||
export type Announcement = {
|
||||
id: string
|
||||
content: string
|
||||
starts_at: string | null
|
||||
ends_at: string | null
|
||||
published: boolean
|
||||
all_day: boolean
|
||||
published_at: string
|
||||
updated_at: string
|
||||
mentions: Array<AnnouncementAccount>
|
||||
statuses: Array<AnnouncementStatus>
|
||||
tags: Array<StatusTag>
|
||||
emojis: Array<Emoji>
|
||||
reactions: Array<AnnouncementReaction>
|
||||
}
|
||||
|
||||
export type AnnouncementAccount = {
|
||||
id: string
|
||||
username: string
|
||||
url: string
|
||||
acct: string
|
||||
}
|
||||
|
||||
export type AnnouncementStatus = {
|
||||
id: string
|
||||
url: string
|
||||
}
|
||||
|
||||
export type AnnouncementReaction = {
|
||||
name: string
|
||||
count: number
|
||||
me: boolean | null
|
||||
url: string | null
|
||||
static_url: string | null
|
||||
}
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
namespace PleromaEntity {
|
||||
export type Application = {
|
||||
name: string
|
||||
website?: string | null
|
||||
vapid_key?: string | null
|
||||
}
|
||||
}
|
|
@ -1,14 +0,0 @@
|
|||
/// <reference path="attachment.ts" />
|
||||
namespace PleromaEntity {
|
||||
export type AsyncAttachment = {
|
||||
id: string
|
||||
type: 'unknown' | 'image' | 'gifv' | 'video' | 'audio'
|
||||
url: string | null
|
||||
remote_url: string | null
|
||||
preview_url: string
|
||||
text_url: string | null
|
||||
meta: Meta | null
|
||||
description: string | null
|
||||
blurhash: string | null
|
||||
}
|
||||
}
|
|
@ -1,49 +0,0 @@
|
|||
namespace PleromaEntity {
|
||||
export type Sub = {
|
||||
// For Image, Gifv, and Video
|
||||
width?: number
|
||||
height?: number
|
||||
size?: string
|
||||
aspect?: number
|
||||
|
||||
// For Gifv and Video
|
||||
frame_rate?: string
|
||||
|
||||
// For Audio, Gifv, and Video
|
||||
duration?: number
|
||||
bitrate?: number
|
||||
}
|
||||
|
||||
export type Focus = {
|
||||
x: number
|
||||
y: number
|
||||
}
|
||||
|
||||
export type Meta = {
|
||||
original?: Sub
|
||||
small?: Sub
|
||||
focus?: Focus
|
||||
length?: string
|
||||
duration?: number
|
||||
fps?: number
|
||||
size?: string
|
||||
width?: number
|
||||
height?: number
|
||||
aspect?: number
|
||||
audio_encode?: string
|
||||
audio_bitrate?: string
|
||||
audio_channel?: string
|
||||
}
|
||||
|
||||
export type Attachment = {
|
||||
id: string
|
||||
type: 'unknown' | 'image' | 'gifv' | 'video' | 'audio'
|
||||
url: string
|
||||
remote_url: string | null
|
||||
preview_url: string | null
|
||||
text_url: string | null
|
||||
meta: Meta | null
|
||||
description: string | null
|
||||
blurhash: string | null
|
||||
}
|
||||
}
|
|
@ -1,11 +0,0 @@
|
|||
namespace PleromaEntity {
|
||||
export type Card = {
|
||||
url: string
|
||||
title: string
|
||||
description: string
|
||||
type: 'link' | 'photo' | 'video' | 'rich'
|
||||
image: string | null
|
||||
provider_name: string
|
||||
provider_url: string
|
||||
}
|
||||
}
|
|
@ -1,8 +0,0 @@
|
|||
/// <reference path="status.ts" />
|
||||
|
||||
namespace PleromaEntity {
|
||||
export type Context = {
|
||||
ancestors: Array<Status>
|
||||
descendants: Array<Status>
|
||||
}
|
||||
}
|
|
@ -1,11 +0,0 @@
|
|||
/// <reference path="account.ts" />
|
||||
/// <reference path="status.ts" />
|
||||
|
||||
namespace PleromaEntity {
|
||||
export type Conversation = {
|
||||
id: string
|
||||
accounts: Array<Account>
|
||||
last_status: Status | null
|
||||
unread: boolean
|
||||
}
|
||||
}
|
|
@ -1,8 +0,0 @@
|
|||
namespace PleromaEntity {
|
||||
export type Emoji = {
|
||||
shortcode: string
|
||||
static_url: string
|
||||
url: string
|
||||
visible_in_picker: boolean
|
||||
}
|
||||
}
|
|
@ -1,8 +0,0 @@
|
|||
namespace PleromaEntity {
|
||||
export type FeaturedTag = {
|
||||
id: string
|
||||
name: string
|
||||
statuses_count: number
|
||||
last_status_at: string
|
||||
}
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
namespace PleromaEntity {
|
||||
export type Field = {
|
||||
name: string
|
||||
value: string
|
||||
verified_at: string | null
|
||||
}
|
||||
}
|
|
@ -1,12 +0,0 @@
|
|||
namespace PleromaEntity {
|
||||
export type Filter = {
|
||||
id: string
|
||||
phrase: string
|
||||
context: Array<FilterContext>
|
||||
expires_at: string | null
|
||||
irreversible: boolean
|
||||
whole_word: boolean
|
||||
}
|
||||
|
||||
export type FilterContext = string
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
namespace PleromaEntity {
|
||||
export type History = {
|
||||
day: string
|
||||
uses: number
|
||||
accounts: number
|
||||
}
|
||||
}
|
|
@ -1,9 +0,0 @@
|
|||
namespace PleromaEntity {
|
||||
export type IdentityProof = {
|
||||
provider: string
|
||||
provider_username: string
|
||||
updated_at: string
|
||||
proof_url: string
|
||||
profile_url: string
|
||||
}
|
||||
}
|
|
@ -1,46 +0,0 @@
|
|||
/// <reference path="account.ts" />
|
||||
/// <reference path="urls.ts" />
|
||||
/// <reference path="stats.ts" />
|
||||
|
||||
namespace PleromaEntity {
|
||||
export type Instance = {
|
||||
uri: string
|
||||
title: string
|
||||
description: string
|
||||
email: string
|
||||
version: string
|
||||
thumbnail: string | null
|
||||
urls: URLs
|
||||
stats: Stats
|
||||
languages: Array<string>
|
||||
registrations: boolean
|
||||
approval_required: boolean
|
||||
max_toot_chars: number
|
||||
max_media_attachments?: number
|
||||
pleroma: {
|
||||
metadata: {
|
||||
account_activation_required: boolean
|
||||
birthday_min_age: number
|
||||
birthday_required: boolean
|
||||
features: Array<string>
|
||||
federation: {
|
||||
enabled: boolean
|
||||
exclusions: boolean
|
||||
}
|
||||
fields_limits: {
|
||||
max_fields: number
|
||||
max_remote_fields: number
|
||||
name_length: number
|
||||
value_length: number
|
||||
}
|
||||
post_formats: Array<string>
|
||||
}
|
||||
}
|
||||
poll_limits: {
|
||||
max_expiration: number
|
||||
min_expiration: number
|
||||
max_option_chars: number
|
||||
max_options: number
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,6 +0,0 @@
|
|||
namespace PleromaEntity {
|
||||
export type List = {
|
||||
id: string
|
||||
title: string
|
||||
}
|
||||
}
|
|
@ -1,12 +0,0 @@
|
|||
namespace PleromaEntity {
|
||||
export type Marker = {
|
||||
notifications: {
|
||||
last_read_id: string
|
||||
version: number
|
||||
updated_at: string
|
||||
pleroma: {
|
||||
unread_count: number
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,8 +0,0 @@
|
|||
namespace PleromaEntity {
|
||||
export type Mention = {
|
||||
id: string
|
||||
username: string
|
||||
url: string
|
||||
acct: string
|
||||
}
|
||||
}
|
|
@ -1,16 +0,0 @@
|
|||
/// <reference path="account.ts" />
|
||||
/// <reference path="status.ts" />
|
||||
|
||||
namespace PleromaEntity {
|
||||
export type Notification = {
|
||||
account: Account
|
||||
created_at: string
|
||||
id: string
|
||||
status?: Status
|
||||
emoji?: string
|
||||
type: NotificationType
|
||||
target?: Account
|
||||
}
|
||||
|
||||
export type NotificationType = string
|
||||
}
|
|
@ -1,13 +0,0 @@
|
|||
/// <reference path="poll_option.ts" />
|
||||
|
||||
namespace PleromaEntity {
|
||||
export type Poll = {
|
||||
id: string
|
||||
expires_at: string | null
|
||||
expired: boolean
|
||||
multiple: boolean
|
||||
votes_count: number
|
||||
options: Array<PollOption>
|
||||
voted: boolean
|
||||
}
|
||||
}
|
|
@ -1,6 +0,0 @@
|
|||
namespace PleromaEntity {
|
||||
export type PollOption = {
|
||||
title: string
|
||||
votes_count: number | null
|
||||
}
|
||||
}
|
|
@ -1,9 +0,0 @@
|
|||
namespace PleromaEntity {
|
||||
export type Preferences = {
|
||||
'posting:default:visibility': 'public' | 'unlisted' | 'private' | 'direct'
|
||||
'posting:default:sensitive': boolean
|
||||
'posting:default:language': string | null
|
||||
'reading:expand:media': 'default' | 'show_all' | 'hide_all'
|
||||
'reading:expand:spoilers': boolean
|
||||
}
|
||||
}
|
|
@ -1,16 +0,0 @@
|
|||
namespace PleromaEntity {
|
||||
export type Alerts = {
|
||||
follow: boolean
|
||||
favourite: boolean
|
||||
mention: boolean
|
||||
reblog: boolean
|
||||
poll: boolean
|
||||
}
|
||||
|
||||
export type PushSubscription = {
|
||||
id: string
|
||||
endpoint: string
|
||||
server_key: string
|
||||
alerts: Alerts
|
||||
}
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
/// <reference path="./account.ts" />
|
||||
|
||||
namespace PleromaEntity {
|
||||
export type Reaction = {
|
||||
count: number
|
||||
me: boolean
|
||||
name: string
|
||||
accounts?: Array<Account>
|
||||
}
|
||||
}
|
|
@ -1,18 +0,0 @@
|
|||
namespace PleromaEntity {
|
||||
export type Relationship = {
|
||||
id: string
|
||||
following: boolean
|
||||
followed_by: boolean
|
||||
blocking: boolean
|
||||
blocked_by: boolean
|
||||
muting: boolean
|
||||
muting_notifications: boolean
|
||||
requested: boolean
|
||||
domain_blocking: boolean
|
||||
showing_reblogs: boolean
|
||||
endorsed: boolean
|
||||
subscribing: boolean
|
||||
notifying: boolean
|
||||
note: string
|
||||
}
|
||||
}
|
|
@ -1,6 +0,0 @@
|
|||
namespace PleromaEntity {
|
||||
export type Report = {
|
||||
id: string
|
||||
action_taken: boolean
|
||||
}
|
||||
}
|
|
@ -1,11 +0,0 @@
|
|||
/// <reference path="account.ts" />
|
||||
/// <reference path="status.ts" />
|
||||
/// <reference path="tag.ts" />
|
||||
|
||||
namespace PleromaEntity {
|
||||
export type Results = {
|
||||
accounts: Array<Account>
|
||||
statuses: Array<Status>
|
||||
hashtags: Array<Tag>
|
||||
}
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
/// <reference path="attachment.ts" />
|
||||
/// <reference path="status_params.ts" />
|
||||
namespace PleromaEntity {
|
||||
export type ScheduledStatus = {
|
||||
id: string
|
||||
scheduled_at: string
|
||||
params: StatusParams
|
||||
media_attachments: Array<Attachment> | null
|
||||
}
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
/// <reference path="field.ts" />
|
||||
namespace PleromaEntity {
|
||||
export type Source = {
|
||||
privacy: string | null
|
||||
sensitive: boolean | null
|
||||
language: string | null
|
||||
note: string
|
||||
fields: Array<Field>
|
||||
}
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
namespace PleromaEntity {
|
||||
export type Stats = {
|
||||
user_count: number
|
||||
status_count: number
|
||||
domain_count: number
|
||||
}
|
||||
}
|
|
@ -1,65 +0,0 @@
|
|||
/// <reference path="account.ts" />
|
||||
/// <reference path="application.ts" />
|
||||
/// <reference path="mention.ts" />
|
||||
/// <reference path="attachment.ts" />
|
||||
/// <reference path="emoji.ts" />
|
||||
/// <reference path="card.ts" />
|
||||
/// <reference path="poll.ts" />
|
||||
/// <reference path="reaction.ts" />
|
||||
|
||||
namespace PleromaEntity {
|
||||
export type Status = {
|
||||
id: string
|
||||
uri: string
|
||||
url: string
|
||||
account: Account
|
||||
in_reply_to_id: string | null
|
||||
in_reply_to_account_id: string | null
|
||||
reblog: Status | null
|
||||
content: string
|
||||
created_at: string
|
||||
edited_at?: string | null
|
||||
emojis: Emoji[]
|
||||
replies_count: number
|
||||
reblogs_count: number
|
||||
favourites_count: number
|
||||
reblogged: boolean | null
|
||||
favourited: boolean | null
|
||||
muted: boolean | null
|
||||
sensitive: boolean
|
||||
spoiler_text: string
|
||||
visibility: 'public' | 'unlisted' | 'private' | 'direct'
|
||||
media_attachments: Array<Attachment>
|
||||
mentions: Array<Mention>
|
||||
tags: Array<StatusTag>
|
||||
card: Card | null
|
||||
poll: Poll | null
|
||||
application: Application | null
|
||||
language: string | null
|
||||
pinned: boolean | null
|
||||
bookmarked?: boolean
|
||||
// Reblogged status contains only local parameter.
|
||||
pleroma: {
|
||||
content?: {
|
||||
'text/plain': string
|
||||
}
|
||||
spoiler_text?: {
|
||||
'text/plain': string
|
||||
}
|
||||
conversation_id?: number
|
||||
direct_conversation_id?: number | null
|
||||
emoji_reactions?: Array<Reaction>
|
||||
expires_at?: string
|
||||
in_reply_to_account_acct?: string
|
||||
local: boolean
|
||||
parent_visible?: boolean
|
||||
pinned_at?: string
|
||||
thread_muted?: boolean
|
||||
}
|
||||
}
|
||||
|
||||
export type StatusTag = {
|
||||
name: string
|
||||
url: string
|
||||
}
|
||||
}
|
|
@ -1,11 +0,0 @@
|
|||
namespace PleromaEntity {
|
||||
export type StatusParams = {
|
||||
text: string
|
||||
in_reply_to_id: string | null
|
||||
media_ids?: Array<string> | null
|
||||
sensitive: boolean | null
|
||||
spoiler_text: string | null
|
||||
visibility: 'public' | 'unlisted' | 'private' | 'direct' | null
|
||||
scheduled_at: string | null
|
||||
}
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
namespace PleromaEntity {
|
||||
export type StatusSource = {
|
||||
id: string
|
||||
text: string
|
||||
spoiler_text: string
|
||||
}
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
/// <reference path="history.ts" />
|
||||
|
||||
namespace PleromaEntity {
|
||||
export type Tag = {
|
||||
name: string
|
||||
url: string
|
||||
history: Array<History>
|
||||
following?: boolean
|
||||
}
|
||||
}
|
|
@ -1,8 +0,0 @@
|
|||
namespace PleromaEntity {
|
||||
export type Token = {
|
||||
access_token: string
|
||||
token_type: string
|
||||
scope: string
|
||||
created_at: number
|
||||
}
|
||||
}
|
|
@ -1,5 +0,0 @@
|
|||
namespace PleromaEntity {
|
||||
export type URLs = {
|
||||
streaming_api: string
|
||||
}
|
||||
}
|
|
@ -1,39 +0,0 @@
|
|||
/// <reference path="./entities/account.ts" />
|
||||
/// <reference path="./entities/activity.ts" />
|
||||
/// <reference path="./entities/announcement.ts" />
|
||||
/// <reference path="./entities/application.ts" />
|
||||
/// <reference path="./entities/async_attachment.ts" />
|
||||
/// <reference path="./entities/attachment.ts" />
|
||||
/// <reference path="./entities/card.ts" />
|
||||
/// <reference path="./entities/context.ts" />
|
||||
/// <reference path="./entities/conversation.ts" />
|
||||
/// <reference path="./entities/emoji.ts" />
|
||||
/// <reference path="./entities/featured_tag.ts" />
|
||||
/// <reference path="./entities/field.ts" />
|
||||
/// <reference path="./entities/filter.ts" />
|
||||
/// <reference path="./entities/history.ts" />>
|
||||
/// <reference path="./entities/identity_proof.ts" />
|
||||
/// <reference path="./entities/instance.ts" />
|
||||
/// <reference path="./entities/list.ts" />
|
||||
/// <reference path="./entities/marker.ts" />
|
||||
/// <reference path="./entities/mention.ts" />
|
||||
/// <reference path="./entities/notification.ts" />
|
||||
/// <reference path="./entities/poll.ts" />
|
||||
/// <reference path="./entities/poll_option.ts" />
|
||||
/// <reference path="./entities/preferences.ts" />
|
||||
/// <reference path="./entities/push_subscription.ts" />
|
||||
/// <reference path="./entities/reaction.ts" />
|
||||
/// <reference path="./entities/relationship.ts" />
|
||||
/// <reference path="./entities/report.ts" />
|
||||
/// <reference path="./entities/results.ts" />
|
||||
/// <reference path="./entities/scheduled_status.ts" />
|
||||
/// <reference path="./entities/source.ts" />
|
||||
/// <reference path="./entities/stats.ts" />
|
||||
/// <reference path="./entities/status.ts" />
|
||||
/// <reference path="./entities/status_params.ts" />
|
||||
/// <reference path="./entities/status_source.ts" />
|
||||
/// <reference path="./entities/tag.ts" />
|
||||
/// <reference path="./entities/token.ts" />
|
||||
/// <reference path="./entities/urls.ts" />
|
||||
|
||||
export default PleromaEntity
|
|
@ -1,15 +0,0 @@
|
|||
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 const Update: PleromaEntity.NotificationType = 'update'
|
||||
export const Move: PleromaEntity.NotificationType = 'move'
|
||||
}
|
||||
|
||||
export default PleromaNotificationType
|
|
@ -1,349 +0,0 @@
|
|||
import WS from 'ws'
|
||||
import dayjs, { Dayjs } from 'dayjs'
|
||||
import { EventEmitter } from 'events'
|
||||
|
||||
import proxyAgent, { ProxyConfig } from '../proxy_config'
|
||||
import { WebSocketInterface } from '../megalodon'
|
||||
import PleromaAPI from './api_client'
|
||||
import { UnknownNotificationTypeError } from '../notification'
|
||||
|
||||
/**
|
||||
* WebSocket
|
||||
* Pleroma is not support streaming. It is support websocket instead of streaming.
|
||||
* So this class connect to Phoenix websocket for Pleroma.
|
||||
*/
|
||||
export default class WebSocket extends EventEmitter implements WebSocketInterface {
|
||||
public url: string
|
||||
public stream: string
|
||||
public params: string | null
|
||||
public parser: Parser
|
||||
public headers: { [key: string]: string }
|
||||
public proxyConfig: ProxyConfig | false = false
|
||||
private _accessToken: string
|
||||
private _reconnectInterval: number
|
||||
private _reconnectMaxAttempts: number
|
||||
private _reconnectCurrentAttempts: number
|
||||
private _connectionClosed: boolean
|
||||
private _client: WS | null
|
||||
private _pongReceivedTimestamp: Dayjs
|
||||
private _heartbeatInterval: number = 60000
|
||||
private _pongWaiting: boolean = false
|
||||
|
||||
/**
|
||||
* @param url Full url of websocket: e.g. https://pleroma.io/api/v1/streaming
|
||||
* @param stream Stream name, please refer: https://git.pleroma.social/pleroma/pleroma/blob/develop/lib/pleroma/web/mastodon_api/mastodon_socket.ex#L19-28
|
||||
* @param accessToken The access token.
|
||||
* @param userAgent The specified User Agent.
|
||||
* @param proxyConfig Proxy setting, or set false if don't use proxy.
|
||||
*/
|
||||
constructor(
|
||||
url: string,
|
||||
stream: string,
|
||||
params: string | undefined,
|
||||
accessToken: string,
|
||||
userAgent: string,
|
||||
proxyConfig: ProxyConfig | false = false
|
||||
) {
|
||||
super()
|
||||
this.url = url
|
||||
this.stream = stream
|
||||
if (params === undefined) {
|
||||
this.params = null
|
||||
} else {
|
||||
this.params = params
|
||||
}
|
||||
this.parser = new Parser()
|
||||
this.headers = {
|
||||
'User-Agent': userAgent
|
||||
}
|
||||
this.proxyConfig = proxyConfig
|
||||
this._accessToken = accessToken
|
||||
this._reconnectInterval = 10000
|
||||
this._reconnectMaxAttempts = Infinity
|
||||
this._reconnectCurrentAttempts = 0
|
||||
this._connectionClosed = false
|
||||
this._client = null
|
||||
this._pongReceivedTimestamp = dayjs()
|
||||
}
|
||||
|
||||
/**
|
||||
* Start websocket connection.
|
||||
*/
|
||||
public start() {
|
||||
this._connectionClosed = false
|
||||
this._resetRetryParams()
|
||||
this._startWebSocketConnection()
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset connection and start new websocket connection.
|
||||
*/
|
||||
private _startWebSocketConnection() {
|
||||
this._resetConnection()
|
||||
this._setupParser()
|
||||
this._client = this._connect(this.url, this.stream, this.params, this._accessToken, this.headers, this.proxyConfig)
|
||||
this._bindSocket(this._client)
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop current connection.
|
||||
*/
|
||||
public stop() {
|
||||
this._connectionClosed = true
|
||||
this._resetConnection()
|
||||
this._resetRetryParams()
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean up current connection, and listeners.
|
||||
*/
|
||||
private _resetConnection() {
|
||||
if (this._client) {
|
||||
this._client.close(1000)
|
||||
this._client.removeAllListeners()
|
||||
this._client = null
|
||||
}
|
||||
|
||||
if (this.parser) {
|
||||
this.parser.removeAllListeners()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets the parameters used in reconnect.
|
||||
*/
|
||||
private _resetRetryParams() {
|
||||
this._reconnectCurrentAttempts = 0
|
||||
}
|
||||
|
||||
/**
|
||||
* Reconnects to the same endpoint.
|
||||
*/
|
||||
private _reconnect() {
|
||||
setTimeout(() => {
|
||||
// Skip reconnect when client is connecting.
|
||||
// https://github.com/websockets/ws/blob/7.2.1/lib/websocket.js#L365
|
||||
if (this._client && this._client.readyState === WS.CONNECTING) {
|
||||
return
|
||||
}
|
||||
|
||||
if (this._reconnectCurrentAttempts < this._reconnectMaxAttempts) {
|
||||
this._reconnectCurrentAttempts++
|
||||
this._clearBinding()
|
||||
if (this._client) {
|
||||
// In reconnect, we want to close the connection immediately,
|
||||
// because recoonect is necessary when some problems occur.
|
||||
this._client.terminate()
|
||||
}
|
||||
// Call connect methods
|
||||
console.log('Reconnecting')
|
||||
this._client = this._connect(this.url, this.stream, this.params, this._accessToken, this.headers, this.proxyConfig)
|
||||
this._bindSocket(this._client)
|
||||
}
|
||||
}, this._reconnectInterval)
|
||||
}
|
||||
|
||||
/**
|
||||
* @param url Base url of streaming endpoint.
|
||||
* @param stream The specified stream name.
|
||||
* @param accessToken Access token.
|
||||
* @param headers The specified headers.
|
||||
* @param proxyConfig Proxy setting, or set false if don't use proxy.
|
||||
* @return A WebSocket instance.
|
||||
*/
|
||||
private _connect(
|
||||
url: string,
|
||||
stream: string,
|
||||
params: string | null,
|
||||
accessToken: string,
|
||||
headers: { [key: string]: string },
|
||||
proxyConfig: ProxyConfig | false
|
||||
): WS {
|
||||
const parameter: Array<string> = [`stream=${stream}`]
|
||||
|
||||
if (params) {
|
||||
parameter.push(params)
|
||||
}
|
||||
|
||||
if (accessToken !== null) {
|
||||
parameter.push(`access_token=${accessToken}`)
|
||||
}
|
||||
const requestURL: string = `${url}/?${parameter.join('&')}`
|
||||
let options: WS.ClientOptions = {
|
||||
headers: headers
|
||||
}
|
||||
if (proxyConfig) {
|
||||
options = Object.assign(options, {
|
||||
agent: proxyAgent(proxyConfig)
|
||||
})
|
||||
}
|
||||
|
||||
const cli: WS = new WS(requestURL, options)
|
||||
return cli
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear binding event for web socket client.
|
||||
*/
|
||||
private _clearBinding() {
|
||||
if (this._client) {
|
||||
this._client.removeAllListeners('close')
|
||||
this._client.removeAllListeners('pong')
|
||||
this._client.removeAllListeners('open')
|
||||
this._client.removeAllListeners('message')
|
||||
this._client.removeAllListeners('error')
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Bind event for web socket client.
|
||||
* @param client A WebSocket instance.
|
||||
*/
|
||||
private _bindSocket(client: WS) {
|
||||
client.on('close', (code: number, _reason: Buffer) => {
|
||||
// Refer the code: https://tools.ietf.org/html/rfc6455#section-7.4
|
||||
if (code === 1000) {
|
||||
this.emit('close', {})
|
||||
} else {
|
||||
console.log(`Closed connection with ${code}`)
|
||||
// If already called close method, it does not retry.
|
||||
if (!this._connectionClosed) {
|
||||
this._reconnect()
|
||||
}
|
||||
}
|
||||
})
|
||||
client.on('pong', () => {
|
||||
this._pongWaiting = false
|
||||
this.emit('pong', {})
|
||||
this._pongReceivedTimestamp = dayjs()
|
||||
// It is required to anonymous function since get this scope in checkAlive.
|
||||
setTimeout(() => this._checkAlive(this._pongReceivedTimestamp), this._heartbeatInterval)
|
||||
})
|
||||
client.on('open', () => {
|
||||
this.emit('connect', {})
|
||||
// Call first ping event.
|
||||
setTimeout(() => {
|
||||
client.ping('')
|
||||
}, 10000)
|
||||
})
|
||||
client.on('message', (data: WS.Data, isBinary: boolean) => {
|
||||
this.parser.parse(data, isBinary)
|
||||
})
|
||||
client.on('error', (err: Error) => {
|
||||
this.emit('error', err)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up parser when receive message.
|
||||
*/
|
||||
private _setupParser() {
|
||||
this.parser.on('update', (status: PleromaAPI.Entity.Status) => {
|
||||
this.emit('update', PleromaAPI.Converter.status(status))
|
||||
})
|
||||
this.parser.on('notification', (notification: PleromaAPI.Entity.Notification) => {
|
||||
const n = PleromaAPI.Converter.notification(notification)
|
||||
if (n instanceof UnknownNotificationTypeError) {
|
||||
console.warn(`Unknown notification event has received: ${notification}`)
|
||||
} else {
|
||||
this.emit('notification', n)
|
||||
}
|
||||
})
|
||||
this.parser.on('delete', (id: string) => {
|
||||
this.emit('delete', id)
|
||||
})
|
||||
this.parser.on('conversation', (conversation: PleromaAPI.Entity.Conversation) => {
|
||||
this.emit('conversation', PleromaAPI.Converter.conversation(conversation))
|
||||
})
|
||||
this.parser.on('status_update', (status: PleromaAPI.Entity.Status) => {
|
||||
this.emit('status_update', PleromaAPI.Converter.status(status))
|
||||
})
|
||||
this.parser.on('error', (err: Error) => {
|
||||
this.emit('parser-error', err)
|
||||
})
|
||||
this.parser.on('heartbeat', _ => {
|
||||
this.emit('heartbeat', 'heartbeat')
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Call ping and wait to pong.
|
||||
*/
|
||||
private _checkAlive(timestamp: Dayjs) {
|
||||
const now: Dayjs = dayjs()
|
||||
// Block multiple calling, if multiple pong event occur.
|
||||
// It the duration is less than interval, through ping.
|
||||
if (now.diff(timestamp) > this._heartbeatInterval - 1000 && !this._connectionClosed) {
|
||||
// Skip ping when client is connecting.
|
||||
// https://github.com/websockets/ws/blob/7.2.1/lib/websocket.js#L289
|
||||
if (this._client && this._client.readyState !== WS.CONNECTING) {
|
||||
this._pongWaiting = true
|
||||
this._client.ping('')
|
||||
setTimeout(() => {
|
||||
if (this._pongWaiting) {
|
||||
this._pongWaiting = false
|
||||
this._reconnect()
|
||||
}
|
||||
}, 10000)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parser
|
||||
* This class provides parser for websocket message.
|
||||
*/
|
||||
export class Parser extends EventEmitter {
|
||||
/**
|
||||
* @param message Message body of websocket.
|
||||
*/
|
||||
public parse(data: WS.Data, isBinary: boolean) {
|
||||
const message = isBinary ? data : data.toString()
|
||||
if (typeof message !== 'string') {
|
||||
this.emit('heartbeat', {})
|
||||
return
|
||||
}
|
||||
|
||||
if (message === '') {
|
||||
this.emit('heartbeat', {})
|
||||
return
|
||||
}
|
||||
|
||||
let event = ''
|
||||
let payload = ''
|
||||
let mes = {}
|
||||
try {
|
||||
const obj = JSON.parse(message)
|
||||
event = obj.event
|
||||
payload = obj.payload
|
||||
mes = JSON.parse(payload)
|
||||
} catch (err) {
|
||||
// delete event does not have json object
|
||||
if (event !== 'delete') {
|
||||
this.emit('error', new Error(`Error parsing websocket reply: ${message}, error message: ${err}`))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
switch (event) {
|
||||
case 'update':
|
||||
this.emit('update', mes as PleromaAPI.Entity.Status)
|
||||
break
|
||||
case 'notification':
|
||||
this.emit('notification', mes as PleromaAPI.Entity.Notification)
|
||||
break
|
||||
case 'conversation':
|
||||
this.emit('conversation', mes as PleromaAPI.Entity.Conversation)
|
||||
break
|
||||
case 'delete':
|
||||
this.emit('delete', payload)
|
||||
break
|
||||
case 'status.update':
|
||||
this.emit('status_update', mes as PleromaAPI.Entity.Status)
|
||||
break
|
||||
default:
|
||||
this.emit('error', new Error(`Unknown event has received: ${message}`))
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,38 +0,0 @@
|
|||
import MastodonAPI from '@/mastodon/api_client'
|
||||
import { Worker } from 'jest-worker'
|
||||
|
||||
jest.mock('axios', () => {
|
||||
const mockAxios = jest.requireActual('axios')
|
||||
mockAxios.get = (_path: string) => {
|
||||
return new Promise(resolve => {
|
||||
setTimeout(() => {
|
||||
console.log('hoge')
|
||||
resolve({
|
||||
data: 'hoge',
|
||||
status: 200,
|
||||
statusText: '200OK',
|
||||
headers: [],
|
||||
config: {}
|
||||
})
|
||||
}, 5000)
|
||||
})
|
||||
}
|
||||
return mockAxios
|
||||
})
|
||||
|
||||
const worker = async (client: MastodonAPI.Client) => {
|
||||
const w: any = new Worker(require.resolve('./cancelWorker.ts'))
|
||||
await w.cancel(client)
|
||||
}
|
||||
|
||||
// Could not use jest-worker under typescript.
|
||||
// I'm waiting for resolve this issue.
|
||||
// https://github.com/facebook/jest/issues/8872
|
||||
describe.skip('cancel', () => {
|
||||
const client = new MastodonAPI.Client('testToken', 'https://pleroma.io/api/v1')
|
||||
it('should be raised', async () => {
|
||||
const getPromise = client.get<{}>('/timelines/home')
|
||||
worker(client)
|
||||
await expect(getPromise).rejects.toThrow()
|
||||
})
|
||||
})
|
|
@ -1,5 +0,0 @@
|
|||
import MastodonAPI from '@/mastodon/api_client'
|
||||
|
||||
export function cancel(client: MastodonAPI.Client) {
|
||||
return client.cancel()
|
||||
}
|
|
@ -1,218 +0,0 @@
|
|||
import MastodonEntity from '@/mastodon/entity'
|
||||
import MastodonNotificationType from '@/mastodon/notification'
|
||||
import Mastodon from '@/mastodon'
|
||||
import MegalodonNotificationType from '@/notification'
|
||||
import axios, { AxiosResponse, InternalAxiosRequestConfig, AxiosHeaders } from 'axios'
|
||||
|
||||
jest.mock('axios')
|
||||
|
||||
const account: MastodonEntity.Account = {
|
||||
id: '1',
|
||||
username: 'h3poteto',
|
||||
acct: 'h3poteto@pleroma.io',
|
||||
display_name: 'h3poteto',
|
||||
locked: false,
|
||||
group: false,
|
||||
noindex: false,
|
||||
suspended: false,
|
||||
limited: 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,
|
||||
source: {
|
||||
privacy: null,
|
||||
sensitive: false,
|
||||
language: null,
|
||||
note: 'test',
|
||||
fields: []
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
const toot: MastodonEntity.Notification = {
|
||||
account: account,
|
||||
created_at: '2021-01-31T23:33:26',
|
||||
id: '7',
|
||||
status: status,
|
||||
type: MastodonNotificationType.Status
|
||||
}
|
||||
|
||||
const unknownEvent: MastodonEntity.Notification = {
|
||||
account: account,
|
||||
created_at: '2021-01-31T23:33:26',
|
||||
id: '8',
|
||||
type: 'unknown'
|
||||
}
|
||||
|
||||
;(axios.CancelToken.source as any).mockImplementation(() => {
|
||||
return {
|
||||
token: {
|
||||
throwIfRequested: () => {},
|
||||
promise: {
|
||||
then: () => {},
|
||||
catch: () => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
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.PollExpired,
|
||||
title: 'poll'
|
||||
},
|
||||
{
|
||||
event: followRequest,
|
||||
expected: MegalodonNotificationType.FollowRequest,
|
||||
title: 'followRequest'
|
||||
},
|
||||
{
|
||||
event: toot,
|
||||
expected: MegalodonNotificationType.Status,
|
||||
title: 'status'
|
||||
}
|
||||
]
|
||||
cases.forEach(c => {
|
||||
it(`should be ${c.title} event`, async () => {
|
||||
const config: InternalAxiosRequestConfig<any> = {
|
||||
headers: new AxiosHeaders()
|
||||
}
|
||||
const mockResponse: AxiosResponse<Array<MastodonEntity.Notification>> = {
|
||||
data: [c.event],
|
||||
status: 200,
|
||||
statusText: '200OK',
|
||||
headers: {},
|
||||
config: config
|
||||
}
|
||||
;(axios.get as any).mockResolvedValue(mockResponse)
|
||||
const res = await client.getNotifications()
|
||||
expect(res.data[0].type).toEqual(c.expected)
|
||||
})
|
||||
})
|
||||
it('UnknownEvent should be ignored', async () => {
|
||||
const config: InternalAxiosRequestConfig<any> = {
|
||||
headers: new AxiosHeaders()
|
||||
}
|
||||
const mockResponse: AxiosResponse<Array<MastodonEntity.Notification>> = {
|
||||
data: [unknownEvent],
|
||||
status: 200,
|
||||
statusText: '200OK',
|
||||
headers: {},
|
||||
config: config
|
||||
}
|
||||
;(axios.get as any).mockResolvedValue(mockResponse)
|
||||
const res = await client.getNotifications()
|
||||
expect(res.data).toEqual([])
|
||||
})
|
||||
})
|
|
@ -1,177 +0,0 @@
|
|||
import MastodonAPI from '@/mastodon/api_client'
|
||||
import Entity from '@/entity'
|
||||
import Response from '@/response'
|
||||
import axios, { AxiosResponse, InternalAxiosRequestConfig, AxiosHeaders } from 'axios'
|
||||
|
||||
jest.mock('axios')
|
||||
|
||||
const account: Entity.Account = {
|
||||
id: '1',
|
||||
username: 'h3poteto',
|
||||
acct: 'h3poteto@pleroma.io',
|
||||
display_name: 'h3poteto',
|
||||
locked: false,
|
||||
group: false,
|
||||
noindex: false,
|
||||
suspended: false,
|
||||
limited: 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,
|
||||
source: {
|
||||
privacy: null,
|
||||
sensitive: false,
|
||||
language: null,
|
||||
note: 'test',
|
||||
fields: []
|
||||
}
|
||||
}
|
||||
|
||||
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: null,
|
||||
created_at: '2019-03-26T21:40:32',
|
||||
edited_at: null,
|
||||
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,
|
||||
emoji_reactions: [],
|
||||
bookmarked: false,
|
||||
quote: false
|
||||
}
|
||||
;(axios.CancelToken.source as any).mockImplementation(() => {
|
||||
return {
|
||||
token: {
|
||||
throwIfRequested: () => {},
|
||||
promise: {
|
||||
then: () => {},
|
||||
catch: () => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const config: InternalAxiosRequestConfig<any> = {
|
||||
headers: new AxiosHeaders()
|
||||
}
|
||||
|
||||
describe('get', () => {
|
||||
const client = new MastodonAPI.Client('testToken', 'https://pleroma.io/api/v1')
|
||||
const mockResponse: AxiosResponse<Array<Entity.Status>> = {
|
||||
data: [status],
|
||||
status: 200,
|
||||
statusText: '200OK',
|
||||
headers: {},
|
||||
config: config
|
||||
}
|
||||
it('should be responsed', async () => {
|
||||
;(axios.get as any).mockResolvedValue(mockResponse)
|
||||
const response: Response<Array<Entity.Status>> = await client.get<Array<Entity.Status>>('/timelines/home')
|
||||
expect(response.data).toEqual([status])
|
||||
})
|
||||
})
|
||||
|
||||
describe('put', () => {
|
||||
const client = new MastodonAPI.Client('testToken', 'https://pleroma.io/api/v1')
|
||||
const mockResponse: AxiosResponse<Entity.Account> = {
|
||||
data: account,
|
||||
status: 200,
|
||||
statusText: '200OK',
|
||||
headers: {},
|
||||
config: config
|
||||
}
|
||||
it('should be responsed', async () => {
|
||||
;(axios.put as any).mockResolvedValue(mockResponse)
|
||||
const response: Response<Entity.Account> = await client.put<Entity.Account>('/accounts/update_credentials', {
|
||||
display_name: 'hoge'
|
||||
})
|
||||
expect(response.data).toEqual(account)
|
||||
})
|
||||
})
|
||||
|
||||
describe('patch', () => {
|
||||
const client = new MastodonAPI.Client('testToken', 'https://pleroma.io/api/v1')
|
||||
const mockResponse: AxiosResponse<Entity.Account> = {
|
||||
data: account,
|
||||
status: 200,
|
||||
statusText: '200OK',
|
||||
headers: {},
|
||||
config: config
|
||||
}
|
||||
it('should be responsed', async () => {
|
||||
;(axios.patch as any).mockResolvedValue(mockResponse)
|
||||
const response: Response<Entity.Account> = await client.patch<Entity.Account>('/accounts/update_credentials', {
|
||||
display_name: 'hoge'
|
||||
})
|
||||
expect(response.data).toEqual(account)
|
||||
})
|
||||
})
|
||||
|
||||
describe('post', () => {
|
||||
const client = new MastodonAPI.Client('testToken', 'https://pleroma.io/api/v1')
|
||||
const mockResponse: AxiosResponse<Entity.Status> = {
|
||||
data: status,
|
||||
status: 200,
|
||||
statusText: '200OK',
|
||||
headers: {},
|
||||
config: config
|
||||
}
|
||||
it('should be responsed', async () => {
|
||||
;(axios.post as any).mockResolvedValue(mockResponse)
|
||||
const response: Response<Entity.Status> = await client.post<Entity.Status>('/statuses', {
|
||||
status: 'hoge'
|
||||
})
|
||||
expect(response.data).toEqual(status)
|
||||
})
|
||||
})
|
||||
|
||||
describe('del', () => {
|
||||
const client = new MastodonAPI.Client('testToken', 'https://pleroma.io/api/v1')
|
||||
const mockResponse: AxiosResponse<{}> = {
|
||||
data: {},
|
||||
status: 200,
|
||||
statusText: '200OK',
|
||||
headers: {},
|
||||
config: config
|
||||
}
|
||||
it('should be responsed', async () => {
|
||||
;(axios.delete as any).mockResolvedValue(mockResponse)
|
||||
const response: Response<{}> = await client.del<{}>('/statuses/12asdf34')
|
||||
expect(response.data).toEqual({})
|
||||
})
|
||||
})
|
|
@ -1,222 +0,0 @@
|
|||
import PleromaEntity from '@/pleroma/entity'
|
||||
import Pleroma from '@/pleroma'
|
||||
import MegalodonNotificationType from '@/notification'
|
||||
import PleromaNotificationType from '@/pleroma/notification'
|
||||
import axios, { AxiosResponse, InternalAxiosRequestConfig, AxiosHeaders } from 'axios'
|
||||
|
||||
jest.mock('axios')
|
||||
|
||||
const account: PleromaEntity.Account = {
|
||||
id: '1',
|
||||
username: 'h3poteto',
|
||||
acct: 'h3poteto@pleroma.io',
|
||||
display_name: 'h3poteto',
|
||||
locked: false,
|
||||
noindex: null,
|
||||
suspended: null,
|
||||
limited: null,
|
||||
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,
|
||||
source: {
|
||||
privacy: null,
|
||||
sensitive: false,
|
||||
language: null,
|
||||
note: 'test',
|
||||
fields: []
|
||||
}
|
||||
}
|
||||
|
||||
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 unknownEvent: PleromaEntity.Notification = {
|
||||
account: account,
|
||||
created_at: '2021-01-31T23:33:26',
|
||||
id: '8',
|
||||
type: 'unknown'
|
||||
}
|
||||
|
||||
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: {
|
||||
throwIfRequested: () => {},
|
||||
promise: {
|
||||
then: () => {},
|
||||
catch: () => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
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.PollExpired,
|
||||
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 config: InternalAxiosRequestConfig<any> = {
|
||||
headers: new AxiosHeaders()
|
||||
}
|
||||
const mockResponse: AxiosResponse<Array<PleromaEntity.Notification>> = {
|
||||
data: [c.event],
|
||||
status: 200,
|
||||
statusText: '200OK',
|
||||
headers: {},
|
||||
config: config
|
||||
}
|
||||
;(axios.get as any).mockResolvedValue(mockResponse)
|
||||
const res = await client.getNotifications()
|
||||
expect(res.data[0].type).toEqual(c.expected)
|
||||
})
|
||||
})
|
||||
it('UnknownEvent should be ignored', async () => {
|
||||
const config: InternalAxiosRequestConfig<any> = {
|
||||
headers: new AxiosHeaders()
|
||||
}
|
||||
const mockResponse: AxiosResponse<Array<PleromaEntity.Notification>> = {
|
||||
data: [unknownEvent],
|
||||
status: 200,
|
||||
statusText: '200OK',
|
||||
headers: {},
|
||||
config: config
|
||||
}
|
||||
;(axios.get as any).mockResolvedValue(mockResponse)
|
||||
const res = await client.getNotifications()
|
||||
expect(res.data).toEqual([])
|
||||
})
|
||||
})
|
|
@ -1,6 +0,0 @@
|
|||
describe('test', () => {
|
||||
it('should be true', () => {
|
||||
const res = true
|
||||
expect(res).toEqual(true)
|
||||
})
|
||||
})
|
|
@ -1,80 +0,0 @@
|
|||
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.PollExpired,
|
||||
dist: MastodonNotificationType.Poll
|
||||
},
|
||||
{
|
||||
src: MegalodonNotificationType.FollowRequest,
|
||||
dist: MastodonNotificationType.FollowRequest
|
||||
},
|
||||
{
|
||||
src: MegalodonNotificationType.Status,
|
||||
dist: MastodonNotificationType.Status
|
||||
}
|
||||
]
|
||||
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.PollExpired
|
||||
},
|
||||
{
|
||||
src: MastodonNotificationType.FollowRequest,
|
||||
dist: MegalodonNotificationType.FollowRequest
|
||||
}
|
||||
]
|
||||
cases.forEach(c => {
|
||||
expect(MastodonAPI.Converter.decodeNotificationType(c.src)).toEqual(c.dist)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Reference in a new issue