upd: megalodon to v7

This commit is contained in:
Mar0xy 2023-09-24 01:44:53 +02:00
parent b4674ce65c
commit afda15260f
No known key found for this signature in database
GPG key ID: 56569BBE47D2C828
234 changed files with 21334 additions and 7675 deletions

1
.gitignore vendored
View file

@ -60,6 +60,7 @@ temp
# Sharkey # Sharkey
/packages/megalodon/lib /packages/megalodon/lib
/packages/megalodon-bk
# blender backups # blender backups
*.blend1 *.blend1

View file

@ -20,7 +20,7 @@ export function getClient(BASE_URL: string, authorization: string | undefined):
const accessTokenArr = authorization?.split(" ") ?? [null]; const accessTokenArr = authorization?.split(" ") ?? [null];
const accessToken = accessTokenArr[accessTokenArr.length - 1]; const accessToken = accessTokenArr[accessTokenArr.length - 1];
const generator = (megalodon as any).default; const generator = (megalodon as any).default;
const client = generator(BASE_URL, accessToken) as MegalodonInterface; const client = generator('misskey', BASE_URL, accessToken) as MegalodonInterface;
return client; return client;
} }
@ -303,7 +303,7 @@ export class MastodonApiServerService {
const accessTokens = _request.headers.authorization; const accessTokens = _request.headers.authorization;
const client = getClient(BASE_URL, accessTokens); const client = getClient(BASE_URL, accessTokens);
try { try {
const data = await client.getAccountFeaturedTags(convertId(_request.params.id, IdType.SharkeyId)); const data = await client.getFeaturedTags();
reply.send(data.data.map((tag) => convertFeaturedTag(tag))); reply.send(data.data.map((tag) => convertFeaturedTag(tag)));
} catch (e: any) { } catch (e: any) {
console.error(e); console.error(e);

View file

@ -83,8 +83,6 @@ export function convertNotification(notification: Entity.Notification) {
notification.id = convertId(notification.id, IdConvertType.MastodonId); notification.id = convertId(notification.id, IdConvertType.MastodonId);
if (notification.status) if (notification.status)
notification.status = convertStatus(notification.status); notification.status = convertStatus(notification.status);
if (notification.reaction)
notification.reaction = convertReaction(notification.reaction);
return notification; return notification;
} }
@ -120,8 +118,6 @@ export function convertStatus(status: Entity.Status) {
})); }));
if (status.poll) status.poll = convertPoll(status.poll); if (status.poll) status.poll = convertPoll(status.poll);
if (status.reblog) status.reblog = convertStatus(status.reblog); if (status.reblog) status.reblog = convertStatus(status.reblog);
if (status.quote) status.quote = convertStatus(status.quote);
status.reactions = status.reactions.map(convertReaction);
return status; return status;
} }

View file

@ -46,7 +46,7 @@ export class apiAccountMastodon {
acct.source = { acct.source = {
note: acct.note, note: acct.note,
fields: acct.fields, fields: acct.fields,
privacy: await (this.client as any).getDefaultPostPrivacy(), privacy: "",
sensitive: false, sensitive: false,
language: "", language: "",
}; };
@ -72,7 +72,7 @@ export class apiAccountMastodon {
public async lookup() { public async lookup() {
try { try {
const data = await this.client.search((this.request.query as any).acct, "accounts"); const data = await this.client.search((this.request.query as any).acct, { type: "accounts" });
return convertAccount(data.data.accounts[0]); return convertAccount(data.data.accounts[0]);
} catch (e: any) { } catch (e: any) {
console.error(e); console.error(e);

View file

@ -46,6 +46,7 @@ export function getConfig(): UserConfig {
base: '/vite/', base: '/vite/',
server: { server: {
host: '0.0.0.0',
port: 5173, port: 5173,
}, },

View file

@ -0,0 +1,3 @@
node_modules
./src
tsconfig.json

View file

@ -1,16 +1,35 @@
{ {
"name": "megalodon", "name": "megalodon",
"private": true, "version": "7.0.1",
"description": "Mastodon API client for node.js and browser",
"main": "./lib/src/index.js", "main": "./lib/src/index.js",
"typings": "./lib/src/index.d.ts", "typings": "./lib/src/index.d.ts",
"scripts": { "scripts": {
"build": "tsc -p ./", "build": "tsc -p ./",
"build:debug": "pnpm run build", "lint": "eslint --ext .js,.ts src",
"lint": "pnpm biome check **/*.ts --apply",
"format": "pnpm biome format --write src/**/*.ts",
"doc": "typedoc --out ../docs ./src", "doc": "typedoc --out ../docs ./src",
"test": "NODE_ENV=test jest -u --maxWorkers=3" "test": "NODE_ENV=test jest -u --maxWorkers=3"
}, },
"engines": {
"node": ">=15.0.0"
},
"repository": {
"type": "git",
"url": "git+https://github.com/h3poteto/megalodon.git"
},
"keywords": [
"mastodon",
"client",
"api",
"streaming",
"rest",
"proxy"
],
"author": "h3poteto",
"license": "MIT",
"bugs": {
"url": "https://github.com/h3poteto/megalodon/issues"
},
"jest": { "jest": {
"moduleFileExtensions": [ "moduleFileExtensions": [
"ts", "ts",
@ -25,59 +44,44 @@
], ],
"preset": "ts-jest/presets/default", "preset": "ts-jest/presets/default",
"transform": { "transform": {
"^.+\\.(ts|tsx)$": "ts-jest" "^.+\\.(ts|tsx)$": ["ts-jest", {
},
"globals": {
"ts-jest": {
"tsconfig": "tsconfig.json" "tsconfig": "tsconfig.json"
} }]
}, },
"testEnvironment": "node" "testEnvironment": "node"
}, },
"homepage": "https://github.com/h3poteto/megalodon#readme",
"dependencies": { "dependencies": {
"@types/oauth": "^0.9.0", "@types/oauth": "^0.9.2",
"@types/ws": "^8.5.4", "@types/ws": "^8.5.5",
"axios": "1.2.2", "axios": "1.5.0",
"dayjs": "^1.11.7", "dayjs": "^1.11.9",
"form-data": "^4.0.0", "form-data": "^4.0.0",
"https-proxy-agent": "^5.0.1", "https-proxy-agent": "^7.0.2",
"oauth": "^0.10.0", "oauth": "^0.10.0",
"object-assign-deep": "^0.4.0", "object-assign-deep": "^0.4.0",
"parse-link-header": "^2.0.0", "parse-link-header": "^2.0.0",
"socks-proxy-agent": "^7.0.0", "socks-proxy-agent": "^8.0.2",
"typescript": "4.9.4", "typescript": "5.1.6",
"uuid": "^9.0.0", "uuid": "^9.0.1",
"ws": "8.12.0", "ws": "8.14.2"
"async-lock": "1.4.0"
}, },
"devDependencies": { "devDependencies": {
"@types/core-js": "^2.5.0", "@types/core-js": "^2.5.6",
"@types/form-data": "^2.5.0", "@types/form-data": "^2.5.0",
"@types/jest": "^29.4.0", "@types/jest": "^29.5.5",
"@types/object-assign-deep": "^0.4.0", "@types/object-assign-deep": "^0.4.1",
"@types/parse-link-header": "^2.0.0", "@types/parse-link-header": "^2.0.1",
"@types/uuid": "^9.0.0", "@types/uuid": "^9.0.4",
"@types/node": "18.11.18", "@typescript-eslint/eslint-plugin": "^6.7.2",
"@typescript-eslint/eslint-plugin": "^5.49.0", "@typescript-eslint/parser": "^6.7.2",
"@typescript-eslint/parser": "^5.49.0", "eslint": "^8.49.0",
"@types/async-lock": "1.4.0", "eslint-config-prettier": "^9.0.0",
"eslint": "^8.32.0", "jest": "^29.7.0",
"eslint-config-prettier": "^8.6.0", "jest-worker": "^29.7.0",
"eslint-config-standard": "^16.0.3",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-node": "^11.0.0",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-promise": "^6.1.1",
"eslint-plugin-standard": "^5.0.0",
"jest": "^29.4.0",
"jest-worker": "^29.4.0",
"lodash": "^4.17.14", "lodash": "^4.17.14",
"prettier": "^2.8.3", "prettier": "^3.0.3",
"ts-jest": "^29.0.5", "ts-jest": "^29.1.1",
"typedoc": "^0.23.24" "typedoc": "^0.25.1"
},
"directories": {
"lib": "lib",
"test": "test"
} }
} }

View file

@ -1 +1 @@
declare module "axios/lib/adapters/http"; declare module 'axios/lib/adapters/http'

View file

@ -1,13 +1,13 @@
export class RequestCanceledError extends Error { export class RequestCanceledError extends Error {
public isCancel: boolean; public isCancel: boolean
constructor(msg: string) { constructor(msg: string) {
super(msg); super(msg)
this.isCancel = true; this.isCancel = true
Object.setPrototypeOf(this, RequestCanceledError); Object.setPrototypeOf(this, RequestCanceledError)
} }
} }
export const isCancel = (value: any): boolean => { export const isCancel = (value: any): boolean => {
return value && value.isCancel; return value && value.isCancel
}; }

View file

@ -1,3 +0,0 @@
import MisskeyAPI from "./misskey/api_client";
export default MisskeyAPI.Converter;

View file

@ -1,3 +1,3 @@
export const NO_REDIRECT = "urn:ietf:wg:oauth:2.0:oob"; export const NO_REDIRECT = 'urn:ietf:wg:oauth:2.0:oob'
export const DEFAULT_SCOPE = ["read", "write", "follow"]; export const DEFAULT_SCOPE = ['read', 'write', 'follow']
export const DEFAULT_UA = "megalodon"; export const DEFAULT_UA = 'megalodon'

View file

@ -0,0 +1,137 @@
import axios, { AxiosRequestConfig } from 'axios'
import proxyAgent, { ProxyConfig } from './proxy_config'
import { NodeinfoError } from './megalodon'
const NODEINFO_10 = 'http://nodeinfo.diaspora.software/ns/schema/1.0'
const NODEINFO_20 = 'http://nodeinfo.diaspora.software/ns/schema/2.0'
const NODEINFO_21 = 'http://nodeinfo.diaspora.software/ns/schema/2.1'
type Links = {
links: Array<Link>
}
type Link = {
href: string
rel: string
}
type Nodeinfo10 = {
software: Software
metadata: Metadata
}
type Nodeinfo20 = {
software: Software
metadata: Metadata
}
type Nodeinfo21 = {
software: Software
metadata: Metadata
}
type Software = {
name: string
}
type Metadata = {
upstream?: {
name: string
}
}
/**
* Detect SNS type.
* Now support Mastodon, Pleroma and Pixelfed. Throws an error when no known platform can be detected.
*
* @param url Base URL of SNS.
* @param proxyConfig Proxy setting, or set false if don't use proxy.
* @return SNS name.
*/
export const detector = async (
url: string,
proxyConfig: ProxyConfig | false = false
): Promise<'mastodon' | 'pleroma' | 'misskey' | 'friendica'> => {
let options: AxiosRequestConfig = {
timeout: 20000
}
if (proxyConfig) {
options = Object.assign(options, {
httpsAgent: proxyAgent(proxyConfig)
})
}
const res = await axios.get<Links>(url + '/.well-known/nodeinfo', options)
const link = res.data.links.find(l => l.rel === NODEINFO_20 || l.rel === NODEINFO_21)
if (!link) throw new NodeinfoError('Could not find nodeinfo')
switch (link.rel) {
case NODEINFO_10: {
const res = await axios.get<Nodeinfo10>(link.href, options)
switch (res.data.software.name) {
case 'pleroma':
return 'pleroma'
case 'akkoma':
return 'pleroma'
case 'mastodon':
return 'mastodon'
case "wildebeest":
return "mastodon"
case 'misskey':
return 'misskey'
case 'friendica':
return 'friendica'
default:
if (res.data.metadata.upstream?.name && res.data.metadata.upstream.name === 'mastodon') {
return 'mastodon'
}
throw new NodeinfoError('Unknown SNS')
}
}
case NODEINFO_20: {
const res = await axios.get<Nodeinfo20>(link.href, options)
switch (res.data.software.name) {
case 'pleroma':
return 'pleroma'
case 'akkoma':
return 'pleroma'
case 'mastodon':
return 'mastodon'
case "wildebeest":
return "mastodon"
case 'misskey':
return 'misskey'
case 'friendica':
return 'friendica'
default:
if (res.data.metadata.upstream?.name && res.data.metadata.upstream.name === 'mastodon') {
return 'mastodon'
}
throw new NodeinfoError('Unknown SNS')
}
}
case NODEINFO_21: {
const res = await axios.get<Nodeinfo21>(link.href, options)
switch (res.data.software.name) {
case 'pleroma':
return 'pleroma'
case 'akkoma':
return 'pleroma'
case 'mastodon':
return 'mastodon'
case "wildebeest":
return "mastodon"
case 'misskey':
return 'misskey'
case 'friendica':
return 'friendica'
default:
if (res.data.metadata.upstream?.name && res.data.metadata.upstream.name === 'mastodon') {
return 'mastodon'
}
throw new NodeinfoError('Unknown SNS')
}
}
default:
throw new NodeinfoError('Could not find nodeinfo')
}
}

View file

@ -1,27 +1,35 @@
/// <reference path="emoji.ts" /> /// <reference path="emoji.ts" />
/// <reference path="source.ts" /> /// <reference path="source.ts" />
/// <reference path="field.ts" /> /// <reference path="field.ts" />
/// <reference path="role.ts" />
namespace Entity { namespace Entity {
export type Account = { export type Account = {
id: string; id: string
username: string; username: string
acct: string; acct: string
display_name: string; display_name: string
locked: boolean; locked: boolean
created_at: string; discoverable?: boolean
followers_count: number; group: boolean | null
following_count: number; noindex: boolean | null
statuses_count: number; suspended: boolean | null
note: string; limited: boolean | null
url: string; created_at: string
avatar: string; followers_count: number
avatar_static: string; following_count: number
header: string; statuses_count: number
header_static: string; note: string
emojis: Array<Emoji>; url: string
moved: Account | null; avatar: string
fields: Array<Field>; avatar_static: string
bot: boolean | null; header: string
source?: Source; header_static: string
}; emojis: Array<Emoji>
moved: Account | null
fields: Array<Field>
bot: boolean | null
source?: Source
role?: Role
mute_expires_at?: string
}
} }

View file

@ -1,8 +1,8 @@
namespace Entity { namespace Entity {
export type Activity = { export type Activity = {
week: string; week: string
statuses: string; statuses: string
logins: string; logins: string
registrations: string; registrations: string
}; }
} }

View file

@ -1,34 +1,40 @@
/// <reference path="tag.ts" />
/// <reference path="emoji.ts" /> /// <reference path="emoji.ts" />
/// <reference path="reaction.ts" />
namespace Entity { namespace Entity {
export type Announcement = { export type Announcement = {
id: string; id: string
content: string; content: string
starts_at: string | null; starts_at: string | null
ends_at: string | null; ends_at: string | null
published: boolean; published: boolean
all_day: boolean; all_day: boolean
published_at: string; published_at: string
updated_at: string; updated_at: string | null
read?: boolean; read: boolean | null
mentions: Array<AnnouncementAccount>; mentions: Array<AnnouncementAccount>
statuses: Array<AnnouncementStatus>; statuses: Array<AnnouncementStatus>
tags: Array<Tag>; tags: Array<StatusTag>
emojis: Array<Emoji>; emojis: Array<Emoji>
reactions: Array<Reaction>; reactions: Array<AnnouncementReaction>
}; }
export type AnnouncementAccount = { export type AnnouncementAccount = {
id: string; id: string
username: string; username: string
url: string; url: string
acct: string; acct: string
}; }
export type AnnouncementStatus = { export type AnnouncementStatus = {
id: string; id: string
url: string; url: string
}; }
export type AnnouncementReaction = {
name: string
count: number
me: boolean | null
url: string | null
static_url: string | null
}
} }

View file

@ -1,7 +1,7 @@
namespace Entity { namespace Entity {
export type Application = { export type Application = {
name: string; name: string
website?: string | null; website?: string | null
vapid_key?: string | null; vapid_key?: string | null
}; }
} }

View file

@ -1,14 +1,14 @@
/// <reference path="attachment.ts" /> /// <reference path="attachment.ts" />
namespace Entity { namespace Entity {
export type AsyncAttachment = { export type AsyncAttachment = {
id: string; id: string
type: "unknown" | "image" | "gifv" | "video" | "audio"; type: 'unknown' | 'image' | 'gifv' | 'video' | 'audio'
url: string | null; url: string | null
remote_url: string | null; remote_url: string | null
preview_url: string; preview_url: string
text_url: string | null; text_url: string | null
meta: Meta | null; meta: Meta | null
description: string | null; description: string | null
blurhash: string | null; blurhash: string | null
}; }
} }

View file

@ -1,49 +1,49 @@
namespace Entity { namespace Entity {
export type Sub = { export type Sub = {
// For Image, Gifv, and Video // For Image, Gifv, and Video
width?: number; width?: number
height?: number; height?: number
size?: string; size?: string
aspect?: number; aspect?: number
// For Gifv and Video // For Gifv and Video
frame_rate?: string; frame_rate?: string
// For Audio, Gifv, and Video // For Audio, Gifv, and Video
duration?: number; duration?: number
bitrate?: number; bitrate?: number
}; }
export type Focus = { export type Focus = {
x: number; x: number
y: number; y: number
}; }
export type Meta = { export type Meta = {
original?: Sub; original?: Sub
small?: Sub; small?: Sub
focus?: Focus; focus?: Focus
length?: string; length?: string
duration?: number; duration?: number
fps?: number; fps?: number
size?: string; size?: string
width?: number; width?: number
height?: number; height?: number
aspect?: number; aspect?: number
audio_encode?: string; audio_encode?: string
audio_bitrate?: string; audio_bitrate?: string
audio_channel?: string; audio_channel?: string
}; }
export type Attachment = { export type Attachment = {
id: string; id: string
type: "unknown" | "image" | "gifv" | "video" | "audio"; type: 'unknown' | 'image' | 'gifv' | 'video' | 'audio'
url: string; url: string
remote_url: string | null; remote_url: string | null
preview_url: string | null; preview_url: string | null
text_url: string | null; text_url: string | null
meta: Meta | null; meta: Meta | null
description: string | null; description: string | null
blurhash: string | null; blurhash: string | null
}; }
} }

View file

@ -1,16 +1,18 @@
namespace Entity { namespace Entity {
export type Card = { export type Card = {
url: string; url: string
title: string; title: string
description: string; description: string
type: "link" | "photo" | "video" | "rich"; type: 'link' | 'photo' | 'video' | 'rich'
image?: string; image: string | null
author_name?: string; author_name: string | null
author_url?: string; author_url: string | null
provider_name?: string; provider_name: string | null
provider_url?: string; provider_url: string | null
html?: string; html: string | null
width?: number; width: number | null
height?: number; height: number | null
}; embed_url: string | null
blurhash: string | null
}
} }

View file

@ -2,7 +2,7 @@
namespace Entity { namespace Entity {
export type Context = { export type Context = {
ancestors: Array<Status>; ancestors: Array<Status>
descendants: Array<Status>; descendants: Array<Status>
}; }
} }

View file

@ -3,9 +3,9 @@
namespace Entity { namespace Entity {
export type Conversation = { export type Conversation = {
id: string; id: string
accounts: Array<Account>; accounts: Array<Account>
last_status: Status | null; last_status: Status | null
unread: boolean; unread: boolean
}; }
} }

View file

@ -1,9 +1,9 @@
namespace Entity { namespace Entity {
export type Emoji = { export type Emoji = {
shortcode: string; shortcode: string
static_url: string; static_url: string
url: string; url: string
visible_in_picker: boolean; visible_in_picker: boolean
category: string; category?: string
}; }
} }

View file

@ -1,8 +1,8 @@
namespace Entity { namespace Entity {
export type FeaturedTag = { export type FeaturedTag = {
id: string; id: string
name: string; name: string
statuses_count: number; statuses_count: number
last_status_at: string; last_status_at: string
}; }
} }

View file

@ -1,7 +1,7 @@
namespace Entity { namespace Entity {
export type Field = { export type Field = {
name: string; name: string
value: string; value: string
verified_at: string | null; verified_at: string | null
}; }
} }

View file

@ -1,12 +1,12 @@
namespace Entity { namespace Entity {
export type Filter = { export type Filter = {
id: string; id: string
phrase: string; phrase: string
context: Array<FilterContext>; context: Array<FilterContext>
expires_at: string | null; expires_at: string | null
irreversible: boolean; irreversible: boolean
whole_word: boolean; whole_word: boolean
}; }
export type FilterContext = string; export type FilterContext = string
} }

View file

@ -0,0 +1,27 @@
/// <reference path="emoji.ts" />
/// <reference path="field.ts" />
namespace Entity {
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>
}
}

View file

@ -1,7 +1,7 @@
namespace Entity { namespace Entity {
export type History = { export type History = {
day: string; day: string
uses: number; uses: number
accounts: number; accounts: number
}; }
} }

View file

@ -1,9 +1,9 @@
namespace Entity { namespace Entity {
export type IdentityProof = { export type IdentityProof = {
provider: string; provider: string
provider_username: string; provider_username: string
updated_at: string; updated_at: string
proof_url: string; proof_url: string
profile_url: string; profile_url: string
}; }
} }

View file

@ -4,38 +4,37 @@
namespace Entity { namespace Entity {
export type Instance = { export type Instance = {
uri: string; uri: string
title: string; title: string
description: string; description: string
email: string; email: string
version: string; version: string
thumbnail: string | null; thumbnail: string | null
urls: URLs; urls: URLs | null
stats: Stats; stats: Stats
languages: Array<string>; languages: Array<string>
contact_account: Account | null; registrations: boolean
max_toot_chars?: number; approval_required: boolean
registrations?: boolean; invites_enabled?: boolean
configuration?: { configuration: {
statuses: { statuses: {
max_characters: number; max_characters: number
max_media_attachments: number; max_media_attachments?: number
characters_reserved_per_url: number; characters_reserved_per_url?: number
}; }
media_attachments: { polls?: {
supported_mime_types: Array<string>; max_options: number
image_size_limit: number; max_characters_per_option: number
image_matrix_limit: number; min_expiration: number
video_size_limit: number; max_expiration: number
video_frame_limit: number; }
video_matrix_limit: number; }
}; contact_account?: Account
polls: { rules?: Array<InstanceRule>
max_options: number; }
max_characters_per_option: number;
min_expiration: number; export type InstanceRule = {
max_expiration: number; id: string
}; text: string
}; }
};
} }

View file

@ -1,6 +1,9 @@
namespace Entity { namespace Entity {
export type List = { export type List = {
id: string; id: string
title: string; title: string
}; replies_policy: RepliesPolicy | null
}
export type RepliesPolicy = 'followed' | 'list' | 'none'
} }

View file

@ -1,15 +1,15 @@
namespace Entity { namespace Entity {
export type Marker = { export type Marker = {
home?: { home?: {
last_read_id: string; last_read_id: string
version: number; version: number
updated_at: string; updated_at: string
}; }
notifications?: { notifications?: {
last_read_id: string; last_read_id: string
version: number; version: number
updated_at: string; updated_at: string
unread_count?: number; unread_count?: number
}; }
}; }
} }

View file

@ -1,8 +1,8 @@
namespace Entity { namespace Entity {
export type Mention = { export type Mention = {
id: string; id: string
username: string; username: string
url: string; url: string
acct: string; acct: string
}; }
} }

View file

@ -3,13 +3,14 @@
namespace Entity { namespace Entity {
export type Notification = { export type Notification = {
account: Account; account: Account
created_at: string; created_at: string
id: string; id: string
status?: Status; status?: Status
reaction?: Reaction; emoji?: string
type: NotificationType; type: NotificationType
}; target?: Account
}
export type NotificationType = string;
export type NotificationType = string
} }

View file

@ -2,13 +2,12 @@
namespace Entity { namespace Entity {
export type Poll = { export type Poll = {
id: string; id: string
expires_at: string | null; expires_at: string | null
expired: boolean; expired: boolean
multiple: boolean; multiple: boolean
votes_count: number; votes_count: number
options: Array<PollOption>; options: Array<PollOption>
voted: boolean; voted: boolean
own_votes: Array<number>; }
};
} }

View file

@ -1,6 +1,6 @@
namespace Entity { namespace Entity {
export type PollOption = { export type PollOption = {
title: string; title: string
votes_count: number | null; votes_count: number | null
}; }
} }

View file

@ -1,9 +1,9 @@
namespace Entity { namespace Entity {
export type Preferences = { export type Preferences = {
"posting:default:visibility": "public" | "unlisted" | "private" | "direct"; 'posting:default:visibility': 'public' | 'unlisted' | 'private' | 'direct'
"posting:default:sensitive": boolean; 'posting:default:sensitive': boolean
"posting:default:language": string | null; 'posting:default:language': string | null
"reading:expand:media": "default" | "show_all" | "hide_all"; 'reading:expand:media': 'default' | 'show_all' | 'hide_all'
"reading:expand:spoilers": boolean; 'reading:expand:spoilers': boolean
}; }
} }

View file

@ -1,16 +1,16 @@
namespace Entity { namespace Entity {
export type Alerts = { export type Alerts = {
follow: boolean; follow: boolean
favourite: boolean; favourite: boolean
mention: boolean; mention: boolean
reblog: boolean; reblog: boolean
poll: boolean; poll: boolean
}; }
export type PushSubscription = { export type PushSubscription = {
id: string; id: string
endpoint: string; endpoint: string
server_key: string; server_key: string
alerts: Alerts; alerts: Alerts
}; }
} }

View file

@ -2,11 +2,9 @@
namespace Entity { namespace Entity {
export type Reaction = { export type Reaction = {
count: number; count: number
me: boolean; me: boolean
name: string; name: string
url?: string; accounts?: Array<Account>
static_url?: string; }
accounts?: Array<Account>;
};
} }

View file

@ -1,17 +1,17 @@
namespace Entity { namespace Entity {
export type Relationship = { export type Relationship = {
id: string; id: string
following: boolean; following: boolean
followed_by: boolean; followed_by: boolean
delivery_following?: boolean; blocking: boolean
blocking: boolean; blocked_by: boolean
blocked_by: boolean; muting: boolean
muting: boolean; muting_notifications: boolean
muting_notifications: boolean; requested: boolean
requested: boolean; domain_blocking: boolean
domain_blocking: boolean; showing_reblogs: boolean
showing_reblogs: boolean; endorsed: boolean
endorsed: boolean; notifying: boolean
notifying: boolean; note: string | null
}; }
} }

View file

@ -1,9 +1,18 @@
/// <reference path="account.ts" />
namespace Entity { namespace Entity {
export type Report = { export type Report = {
id: string; id: string
action_taken: string; action_taken: boolean
comment: string; action_taken_at: string | null
account_id: string; status_ids: Array<string> | null
status_ids: Array<string>; rule_ids: Array<string> | null
}; // These parameters don't exist in Pleroma
category: Category | null
comment: string | null
forwarded: boolean | null
target_account?: Account | null
}
export type Category = 'spam' | 'violation' | 'other'
} }

View file

@ -4,8 +4,8 @@
namespace Entity { namespace Entity {
export type Results = { export type Results = {
accounts: Array<Account>; accounts: Array<Account>
statuses: Array<Status>; statuses: Array<Status>
hashtags: Array<Tag>; hashtags: Array<Tag>
}; }
} }

View file

@ -0,0 +1,5 @@
namespace Entity {
export type Role = {
name: string
}
}

View file

@ -2,9 +2,9 @@
/// <reference path="status_params.ts" /> /// <reference path="status_params.ts" />
namespace Entity { namespace Entity {
export type ScheduledStatus = { export type ScheduledStatus = {
id: string; id: string
scheduled_at: string; scheduled_at: string
params: StatusParams; params: StatusParams
media_attachments: Array<Attachment>; media_attachments: Array<Attachment> | null
}; }
} }

View file

@ -1,10 +1,10 @@
/// <reference path="field.ts" /> /// <reference path="field.ts" />
namespace Entity { namespace Entity {
export type Source = { export type Source = {
privacy: string | null; privacy: string | null
sensitive: boolean | null; sensitive: boolean | null
language: string | null; language: string | null
note: string; note: string
fields: Array<Field>; fields: Array<Field>
}; }
} }

View file

@ -1,7 +1,7 @@
namespace Entity { namespace Entity {
export type Stats = { export type Stats = {
user_count: number; user_count: number
status_count: number; status_count: number
domain_count: number; domain_count: number
}; }
} }

View file

@ -1,7 +1,6 @@
/// <reference path="account.ts" /> /// <reference path="account.ts" />
/// <reference path="application.ts" /> /// <reference path="application.ts" />
/// <reference path="mention.ts" /> /// <reference path="mention.ts" />
/// <reference path="tag.ts" />
/// <reference path="attachment.ts" /> /// <reference path="attachment.ts" />
/// <reference path="emoji.ts" /> /// <reference path="emoji.ts" />
/// <reference path="card.ts" /> /// <reference path="card.ts" />
@ -10,36 +9,41 @@
namespace Entity { namespace Entity {
export type Status = { export type Status = {
id: string; id: string
uri: string; uri: string
url: string; url: string
account: Account; account: Account
in_reply_to_id: string | null; in_reply_to_id: string | null
in_reply_to_account_id: string | null; in_reply_to_account_id: string | null
reblog: Status | null; reblog: Status | null
content: string; content: string
plain_content: string | null; plain_content: string | null
created_at: string; created_at: string
emojis: Emoji[]; emojis: Emoji[]
replies_count: number; replies_count: number
reblogs_count: number; reblogs_count: number
favourites_count: number; favourites_count: number
reblogged: boolean | null; reblogged: boolean | null
favourited: boolean | null; favourited: boolean | null
muted: boolean | null; muted: boolean | null
sensitive: boolean; sensitive: boolean
spoiler_text: string; spoiler_text: string
visibility: "public" | "unlisted" | "private" | "direct"; visibility: 'public' | 'unlisted' | 'private' | 'direct'
media_attachments: Array<Attachment>; media_attachments: Array<Attachment>
mentions: Array<Mention>; mentions: Array<Mention>
tags: Array<Tag>; tags: Array<StatusTag>
card: Card | null; card: Card | null
poll: Poll | null; poll: Poll | null
application: Application | null; application: Application | null
language: string | null; language: string | null
pinned: boolean | null; pinned: boolean | null
reactions: Array<Reaction>; emoji_reactions: Array<Reaction>
quote: Status | null; quote: boolean
bookmarked: boolean; bookmarked: boolean
}; }
export type StatusTag = {
name: string
url: string
}
} }

View file

@ -1,12 +1,12 @@
namespace Entity { namespace Entity {
export type StatusParams = { export type StatusParams = {
text: string; text: string
in_reply_to_id: string | null; in_reply_to_id: string | null
media_ids: Array<string> | null; media_ids: Array<string> | null
sensitive: boolean | null; sensitive: boolean | null
spoiler_text: string | null; spoiler_text: string | null
visibility: "public" | "unlisted" | "private" | "direct"; visibility: 'public' | 'unlisted' | 'private' | 'direct' | null
scheduled_at: string | null; scheduled_at: string | null
application_id: string; application_id: number | null
}; }
} }

View file

@ -0,0 +1,7 @@
namespace Entity {
export type StatusSource = {
id: string
text: string
spoiler_text: string
}
}

View file

@ -2,9 +2,9 @@
namespace Entity { namespace Entity {
export type Tag = { export type Tag = {
name: string; name: string
url: string; url: string
history: Array<History> | null; history: Array<History>
following?: boolean; following?: boolean
}; }
} }

View file

@ -1,8 +1,8 @@
namespace Entity { namespace Entity {
export type Token = { export type Token = {
access_token: string; access_token: string
token_type: string; token_type: string
scope: string; scope: string
created_at: number; created_at: number
}; }
} }

View file

@ -1,5 +1,5 @@
namespace Entity { namespace Entity {
export type URLs = { export type URLs = {
streaming_api: string; streaming_api: string
}; }
} }

View file

@ -11,6 +11,7 @@
/// <reference path="./entities/featured_tag.ts" /> /// <reference path="./entities/featured_tag.ts" />
/// <reference path="./entities/field.ts" /> /// <reference path="./entities/field.ts" />
/// <reference path="./entities/filter.ts" /> /// <reference path="./entities/filter.ts" />
/// <reference path="./entities/follow_request.ts" />
/// <reference path="./entities/history.ts" /> /// <reference path="./entities/history.ts" />
/// <reference path="./entities/identity_proof.ts" /> /// <reference path="./entities/identity_proof.ts" />
/// <reference path="./entities/instance.ts" /> /// <reference path="./entities/instance.ts" />
@ -31,8 +32,9 @@
/// <reference path="./entities/stats.ts" /> /// <reference path="./entities/stats.ts" />
/// <reference path="./entities/status.ts" /> /// <reference path="./entities/status.ts" />
/// <reference path="./entities/status_params.ts" /> /// <reference path="./entities/status_params.ts" />
/// <reference path="./entities/status_source.ts" />
/// <reference path="./entities/tag.ts" /> /// <reference path="./entities/tag.ts" />
/// <reference path="./entities/token.ts" /> /// <reference path="./entities/token.ts" />
/// <reference path="./entities/urls.ts" /> /// <reference path="./entities/urls.ts" />
export default Entity; export default Entity

View file

@ -1,11 +1,11 @@
import Entity from "./entity"; import Entity from './entity'
namespace FilterContext { namespace FilterContext {
export const Home: Entity.FilterContext = "home"; export const Home: Entity.FilterContext = 'home'
export const Notifications: Entity.FilterContext = "notifications"; export const Notifications: Entity.FilterContext = 'notifications'
export const Public: Entity.FilterContext = "public"; export const Public: Entity.FilterContext = 'public'
export const Thread: Entity.FilterContext = "thread"; export const Thread: Entity.FilterContext = 'thread'
export const Account: Entity.FilterContext = "account"; export const Account: Entity.FilterContext = 'account'
} }
export default FilterContext; export default FilterContext

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,768 @@
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,
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

View file

@ -0,0 +1,29 @@
/// <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
}
}

View file

@ -0,0 +1,8 @@
namespace FriendicaEntity {
export type Activity = {
week: string
statuses: string
logins: string
registrations: string
}
}

View file

@ -0,0 +1,7 @@
namespace FriendicaEntity {
export type Application = {
name: string
website?: string | null
vapid_key?: string | null
}
}

View file

@ -0,0 +1,14 @@
/// <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
}
}

View file

@ -0,0 +1,49 @@
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
}
}

View file

@ -0,0 +1,17 @@
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
}
}

View file

@ -0,0 +1,8 @@
/// <reference path="status.ts" />
namespace FriendicaEntity {
export type Context = {
ancestors: Array<Status>
descendants: Array<Status>
}
}

View file

@ -0,0 +1,11 @@
/// <reference path="account.ts" />
/// <reference path="status.ts" />
namespace FriendicaEntity {
export type Conversation = {
id: string
accounts: Array<Account>
last_status: Status | null
unread: boolean
}
}

View file

@ -0,0 +1,8 @@
namespace FriendicaEntity {
export type Emoji = {
shortcode: string
static_url: string
url: string
visible_in_picker: boolean
}
}

View file

@ -0,0 +1,8 @@
namespace FriendicaEntity {
export type FeaturedTag = {
id: string
name: string
statuses_count: number
last_status_at: string
}
}

View file

@ -0,0 +1,7 @@
namespace FriendicaEntity {
export type Field = {
name: string
value: string
verified_at: string | null
}
}

View file

@ -0,0 +1,12 @@
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
}

View file

@ -0,0 +1,27 @@
/// <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>
}
}

View file

@ -0,0 +1,7 @@
namespace FriendicaEntity {
export type History = {
day: string
uses: number
accounts: number
}
}

View file

@ -0,0 +1,9 @@
namespace FriendicaEntity {
export type IdentityProof = {
provider: string
provider_username: string
updated_at: string
proof_url: string
profile_url: string
}
}

View file

@ -0,0 +1,28 @@
/// <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
}
}

View file

@ -0,0 +1,9 @@
namespace FriendicaEntity {
export type List = {
id: string
title: string
replies_policy: RepliesPolicy
}
export type RepliesPolicy = 'followed' | 'list' | 'none'
}

View file

@ -0,0 +1,14 @@
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
}
}
}

View file

@ -0,0 +1,8 @@
namespace FriendicaEntity {
export type Mention = {
id: string
username: string
url: string
acct: string
}
}

View file

@ -0,0 +1,14 @@
/// <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
}

View file

@ -0,0 +1,13 @@
/// <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
}
}

View file

@ -0,0 +1,6 @@
namespace FriendicaEntity {
export type PollOption = {
title: string
votes_count: number | null
}
}

View file

@ -0,0 +1,9 @@
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
}
}

View file

@ -0,0 +1,16 @@
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
}
}

View file

@ -0,0 +1,17 @@
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
}
}

View file

@ -0,0 +1,16 @@
/// <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'
}

View file

@ -0,0 +1,11 @@
/// <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>
}
}

View file

@ -0,0 +1,10 @@
/// <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>
}
}

View file

@ -0,0 +1,10 @@
/// <reference path="field.ts" />
namespace FriendicaEntity {
export type Source = {
privacy: string | null
sensitive: boolean | null
language: string | null
note: string
fields: Array<Field>
}
}

View file

@ -0,0 +1,7 @@
namespace FriendicaEntity {
export type Stats = {
user_count: number
status_count: number
domain_count: number
}
}

View file

@ -0,0 +1,48 @@
/// <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
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
}
}

View file

@ -0,0 +1,12 @@
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
}
}

View file

@ -0,0 +1,7 @@
namespace FriendicaEntity {
export type StatusSource = {
id: string
text: string
spoiler_text: string
}
}

View file

@ -0,0 +1,10 @@
/// <reference path="history.ts" />
namespace FriendicaEntity {
export type Tag = {
name: string
url: string
history: Array<History>
following?: boolean
}
}

View file

@ -0,0 +1,8 @@
namespace FriendicaEntity {
export type Token = {
access_token: string
token_type: string
scope: string
created_at: number
}
}

View file

@ -0,0 +1,5 @@
namespace FriendicaEntity {
export type URLs = {
streaming_api: string
}
}

View file

@ -0,0 +1,38 @@
/// <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

View file

@ -0,0 +1,14 @@
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

View file

@ -0,0 +1,18 @@
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() {}
}

View file

@ -1,17 +1,15 @@
import Response from "./response"; import Response from './response'
import OAuth from "./oauth"; import OAuth from './oauth'
import { isCancel, RequestCanceledError } from "./cancel"; import { isCancel, RequestCanceledError } from './cancel'
import { ProxyConfig } from "./proxy_config"; import { ProxyConfig } from './proxy_config'
import generator, { import generator, { MegalodonInterface, WebSocketInterface } from './megalodon'
detector, import { detector } from './detector'
MegalodonInterface, import Mastodon from './mastodon'
WebSocketInterface, import Pleroma from './pleroma'
} from "./megalodon"; import Misskey from './misskey'
import Misskey from "./misskey"; import Entity from './entity'
import Entity from "./entity"; import NotificationType from './notification'
import NotificationType from "./notification"; import FilterContext from './filter_context'
import FilterContext from "./filter_context";
import Converter from "./converter";
export { export {
Response, Response,
@ -24,9 +22,10 @@ export {
WebSocketInterface, WebSocketInterface,
NotificationType, NotificationType,
FilterContext, FilterContext,
Mastodon,
Pleroma,
Misskey, Misskey,
Entity, Entity
Converter, }
};
export default generator; export default generator

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,661 @@
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,
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

View file

@ -0,0 +1,35 @@
/// <reference path="emoji.ts" />
/// <reference path="source.ts" />
/// <reference path="field.ts" />
/// <reference path="role.ts" />
namespace MastodonEntity {
export type Account = {
id: string
username: string
acct: string
display_name: string
locked: boolean
discoverable?: boolean
group: boolean | null
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
role?: Role
mute_expires_at?: string
}
}

View file

@ -0,0 +1,8 @@
namespace MastodonEntity {
export type Activity = {
week: string
statuses: string
logins: string
registrations: string
}
}

View file

@ -0,0 +1,40 @@
/// <reference path="emoji.ts" />
namespace MastodonEntity {
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
read: boolean | null
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
}
}

Some files were not shown because too many files have changed in this diff Show more