chore: 🎨 format

This commit is contained in:
ThatOneCalculator 2023-07-13 18:32:23 -07:00
parent 361873c6f4
commit 2e4c30e572
No known key found for this signature in database
GPG Key ID: 8703CACD01000000
42 changed files with 1504 additions and 1386 deletions

View File

@ -5,7 +5,8 @@
"typings": "./lib/src/index.d.ts", "typings": "./lib/src/index.d.ts",
"scripts": { "scripts": {
"build": "tsc -p ./", "build": "tsc -p ./",
"lint": "eslint --ext .js,.ts src", "lint": "pnpm rome check --apply src/**/*.ts",
"format": "pnpm rome 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"
}, },

View File

@ -3,25 +3,25 @@
/// <reference path="field.ts" /> /// <reference path="field.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 created_at: string;
followers_count: number followers_count: number;
following_count: number following_count: number;
statuses_count: number statuses_count: number;
note: string note: string;
url: string url: string;
avatar: string avatar: string;
avatar_static: string avatar_static: string;
header: string header: string;
header_static: string header_static: string;
emojis: Array<Emoji> emojis: Array<Emoji>;
moved: Account | null moved: Account | null;
fields: Array<Field> fields: Array<Field>;
bot: boolean | null bot: boolean | null;
source?: Source source?: Source;
} };
} }

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

@ -4,31 +4,31 @@
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;
read?: boolean read?: boolean;
mentions: Array<AnnouncementAccount> mentions: Array<AnnouncementAccount>;
statuses: Array<AnnouncementStatus> statuses: Array<AnnouncementStatus>;
tags: Array<Tag> tags: Array<Tag>;
emojis: Array<Emoji> emojis: Array<Emoji>;
reactions: Array<Reaction> reactions: Array<Reaction>;
} };
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;
} };
} }

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,16 @@
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;
author_name?: string author_name?: string;
author_url?: string author_url?: string;
provider_name?: string provider_name?: string;
provider_url?: string provider_url?: string;
html?: string html?: string;
width?: number width?: number;
height?: number height?: number;
} };
} }

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

@ -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,38 @@
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;
stats: Stats stats: Stats;
languages: Array<string> languages: Array<string>;
contact_account: Account | null contact_account: Account | null;
max_toot_chars?: number max_toot_chars?: number;
registrations?: boolean registrations?: 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: { media_attachments: {
supported_mime_types: Array<string> supported_mime_types: Array<string>;
image_size_limit: number image_size_limit: number;
image_matrix_limit: number image_matrix_limit: number;
video_size_limit: number video_size_limit: number;
video_frame_limit: number video_frame_limit: number;
video_matrix_limit: number video_matrix_limit: number;
} };
polls: { polls: {
max_options: number max_options: number;
max_characters_per_option: number max_characters_per_option: number;
min_expiration: number min_expiration: number;
max_expiration: number max_expiration: number;
} };
} };
} };
} }

View File

@ -1,6 +1,6 @@
namespace Entity { namespace Entity {
export type List = { export type List = {
id: string id: string;
title: string title: string;
} };
} }

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,13 @@
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;
emoji?: string emoji?: string;
type: NotificationType type: NotificationType;
} };
export type NotificationType = string export type NotificationType = string;
} }

View File

@ -2,13 +2,13 @@
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> 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,10 +2,10 @@
namespace Entity { namespace Entity {
export type Reaction = { export type Reaction = {
count: number count: number;
me: boolean me: boolean;
name: string name: string;
url?: string url?: string;
accounts?: Array<Account> 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 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;
} };
} }

View File

@ -1,9 +1,9 @@
namespace Entity { namespace Entity {
export type Report = { export type Report = {
id: string id: string;
action_taken: string action_taken: string;
comment: string comment: string;
account_id: string account_id: string;
status_ids: Array<string> status_ids: Array<string>;
} };
} }

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

@ -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>;
} };
} }

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

@ -10,36 +10,36 @@
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<Tag>;
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;
emoji_reactions: Array<Reaction> emoji_reactions: Array<Reaction>;
quote: Status | null quote: Status | null;
bookmarked: boolean bookmarked: boolean;
} };
} }

View File

@ -10,14 +10,14 @@
namespace Entity { namespace Entity {
export type StatusEdit = { export type StatusEdit = {
account: Account account: Account;
content: string content: string;
plain_content: string | null plain_content: string | null;
created_at: string created_at: string;
emojis: Emoji[] emojis: Emoji[];
sensitive: boolean sensitive: boolean;
spoiler_text: string spoiler_text: string;
media_attachments: Array<Attachment> media_attachments: Array<Attachment>;
poll: Poll | null poll: Poll | null;
} };
} }

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";
scheduled_at: string | null scheduled_at: string | null;
application_id: string application_id: 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> | null;
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

@ -1,83 +1,83 @@
import axios, { AxiosResponse, AxiosRequestConfig } from 'axios' import axios, { AxiosResponse, AxiosRequestConfig } from "axios";
import dayjs from 'dayjs' import dayjs from "dayjs";
import FormData from 'form-data' import FormData from "form-data";
import { DEFAULT_UA } from '../default' import { DEFAULT_UA } from "../default";
import proxyAgent, { ProxyConfig } from '../proxy_config' import proxyAgent, { ProxyConfig } from "../proxy_config";
import Response from '../response' import Response from "../response";
import MisskeyEntity from './entity' import MisskeyEntity from "./entity";
import MegalodonEntity from '../entity' import MegalodonEntity from "../entity";
import WebSocket from './web_socket' import WebSocket from "./web_socket";
import MisskeyNotificationType from './notification' import MisskeyNotificationType from "./notification";
import NotificationType from '../notification' import NotificationType from "../notification";
namespace MisskeyAPI { namespace MisskeyAPI {
export namespace Entity { export namespace Entity {
export type App = MisskeyEntity.App export type App = MisskeyEntity.App;
export type Announcement = MisskeyEntity.Announcement export type Announcement = MisskeyEntity.Announcement;
export type Blocking = MisskeyEntity.Blocking export type Blocking = MisskeyEntity.Blocking;
export type Choice = MisskeyEntity.Choice export type Choice = MisskeyEntity.Choice;
export type CreatedNote = MisskeyEntity.CreatedNote export type CreatedNote = MisskeyEntity.CreatedNote;
export type Emoji = MisskeyEntity.Emoji export type Emoji = MisskeyEntity.Emoji;
export type Favorite = MisskeyEntity.Favorite export type Favorite = MisskeyEntity.Favorite;
export type Field = MisskeyEntity.Field export type Field = MisskeyEntity.Field;
export type File = MisskeyEntity.File export type File = MisskeyEntity.File;
export type Follower = MisskeyEntity.Follower export type Follower = MisskeyEntity.Follower;
export type Following = MisskeyEntity.Following export type Following = MisskeyEntity.Following;
export type FollowRequest = MisskeyEntity.FollowRequest export type FollowRequest = MisskeyEntity.FollowRequest;
export type Hashtag = MisskeyEntity.Hashtag export type Hashtag = MisskeyEntity.Hashtag;
export type List = MisskeyEntity.List export type List = MisskeyEntity.List;
export type Meta = MisskeyEntity.Meta export type Meta = MisskeyEntity.Meta;
export type Mute = MisskeyEntity.Mute export type Mute = MisskeyEntity.Mute;
export type Note = MisskeyEntity.Note export type Note = MisskeyEntity.Note;
export type Notification = MisskeyEntity.Notification export type Notification = MisskeyEntity.Notification;
export type Poll = MisskeyEntity.Poll export type Poll = MisskeyEntity.Poll;
export type Reaction = MisskeyEntity.Reaction export type Reaction = MisskeyEntity.Reaction;
export type Relation = MisskeyEntity.Relation export type Relation = MisskeyEntity.Relation;
export type User = MisskeyEntity.User export type User = MisskeyEntity.User;
export type UserDetail = MisskeyEntity.UserDetail export type UserDetail = MisskeyEntity.UserDetail;
export type UserDetailMe = MisskeyEntity.UserDetailMe export type UserDetailMe = MisskeyEntity.UserDetailMe;
export type GetAll = MisskeyEntity.GetAll export type GetAll = MisskeyEntity.GetAll;
export type UserKey = MisskeyEntity.UserKey export type UserKey = MisskeyEntity.UserKey;
export type Session = MisskeyEntity.Session export type Session = MisskeyEntity.Session;
export type Stats = MisskeyEntity.Stats export type Stats = MisskeyEntity.Stats;
export type State = MisskeyEntity.State export type State = MisskeyEntity.State;
export type APIEmoji = { emojis: Emoji[] } export type APIEmoji = { emojis: Emoji[] };
} }
export class Converter { export class Converter {
private baseUrl: string private baseUrl: string;
private instanceHost: string private instanceHost: string;
private plcUrl: string private plcUrl: string;
private modelOfAcct = { private modelOfAcct = {
id: "1", id: "1",
username: 'none', username: "none",
acct: 'none', acct: "none",
display_name: 'none', display_name: "none",
locked: true, locked: true,
bot: true, bot: true,
discoverable: false, discoverable: false,
group: false, group: false,
created_at: '1971-01-01T00:00:00.000Z', created_at: "1971-01-01T00:00:00.000Z",
note: '', note: "",
url: 'plc', url: "plc",
avatar: 'plc', avatar: "plc",
avatar_static: 'plc', avatar_static: "plc",
header: 'plc', header: "plc",
header_static: 'plc', header_static: "plc",
followers_count: -1, followers_count: -1,
following_count: 0, following_count: 0,
statuses_count: 0, statuses_count: 0,
last_status_at: '1971-01-01T00:00:00.000Z', last_status_at: "1971-01-01T00:00:00.000Z",
noindex: true, noindex: true,
emojis: [], emojis: [],
fields: [], fields: [],
moved: null moved: null,
} };
constructor(baseUrl: string) { constructor(baseUrl: string) {
this.baseUrl = baseUrl; this.baseUrl = baseUrl;
this.instanceHost = baseUrl.substring(baseUrl.indexOf('//') + 2); this.instanceHost = baseUrl.substring(baseUrl.indexOf("//") + 2);
this.plcUrl = `${baseUrl}/static-assets/transparent.png`; this.plcUrl = `${baseUrl}/static-assets/transparent.png`;
this.modelOfAcct.url = this.plcUrl; this.modelOfAcct.url = this.plcUrl;
this.modelOfAcct.avatar = this.plcUrl; this.modelOfAcct.avatar = this.plcUrl;
@ -86,17 +86,16 @@ namespace MisskeyAPI {
this.modelOfAcct.header_static = this.plcUrl; this.modelOfAcct.header_static = this.plcUrl;
} }
// FIXME: Properly render MFM instead of just escaping HTML characters. // FIXME: Properly render MFM instead of just escaping HTML characters.
escapeMFM = (text: string): string => text escapeMFM = (text: string): string =>
.replace(/&/g, '&amp;') text
.replace(/</g, '&lt;') .replace(/&/g, "&amp;")
.replace(/>/g, '&gt;') .replace(/</g, "&lt;")
.replace(/"/g, '&quot;') .replace(/>/g, "&gt;")
.replace(/'/g, '&#39;') .replace(/"/g, "&quot;")
.replace(/`/g, '&#x60;') .replace(/'/g, "&#39;")
.replace(/\r?\n/g, '<br>'); .replace(/`/g, "&#x60;")
.replace(/\r?\n/g, "<br>");
emoji = (e: Entity.Emoji): MegalodonEntity.Emoji => { emoji = (e: Entity.Emoji): MegalodonEntity.Emoji => {
return { return {
@ -104,22 +103,22 @@ namespace MisskeyAPI {
static_url: e.url, static_url: e.url,
url: e.url, url: e.url,
visible_in_picker: true, visible_in_picker: true,
category: e.category category: e.category,
} };
} };
field = (f: Entity.Field): MegalodonEntity.Field => ({ field = (f: Entity.Field): MegalodonEntity.Field => ({
name: f.name, name: f.name,
value: this.escapeMFM(f.value), value: this.escapeMFM(f.value),
verified_at: null verified_at: null,
}) });
user = (u: Entity.User): MegalodonEntity.Account => { user = (u: Entity.User): MegalodonEntity.Account => {
let acct = u.username let acct = u.username;
let acctUrl = `https://${u.host || this.instanceHost}/@${u.username}` let acctUrl = `https://${u.host || this.instanceHost}/@${u.username}`;
if (u.host) { if (u.host) {
acct = `${u.username}@${u.host}` acct = `${u.username}@${u.host}`;
acctUrl = `https://${u.host}/@${u.username}` acctUrl = `https://${u.host}/@${u.username}`;
} }
return { return {
id: u.id, id: u.id,
@ -131,26 +130,31 @@ namespace MisskeyAPI {
followers_count: 0, followers_count: 0,
following_count: 0, following_count: 0,
statuses_count: 0, statuses_count: 0,
note: '', note: "",
url: acctUrl, url: acctUrl,
avatar: u.avatarUrl, avatar: u.avatarUrl,
avatar_static: u.avatarUrl, avatar_static: u.avatarUrl,
header: this.plcUrl, header: this.plcUrl,
header_static: this.plcUrl, header_static: this.plcUrl,
emojis: u.emojis.map(e => this.emoji(e)), emojis: u.emojis.map((e) => this.emoji(e)),
moved: null, moved: null,
fields: [], fields: [],
bot: false bot: false,
} };
} };
userDetail = (u: Entity.UserDetail, host: string): MegalodonEntity.Account => { userDetail = (
let acct = u.username u: Entity.UserDetail,
host = host.replace('https://', '') host: string,
let acctUrl = `https://${host || u.host || this.instanceHost}/@${u.username}` ): MegalodonEntity.Account => {
let acct = u.username;
host = host.replace("https://", "");
let acctUrl = `https://${host || u.host || this.instanceHost}/@${
u.username
}`;
if (u.host) { if (u.host) {
acct = `${u.username}@${u.host}` acct = `${u.username}@${u.host}`;
acctUrl = `https://${u.host}/@${u.username}` acctUrl = `https://${u.host}/@${u.username}`;
} }
return { return {
id: u.id, id: u.id,
@ -162,70 +166,79 @@ namespace MisskeyAPI {
followers_count: u.followersCount, followers_count: u.followersCount,
following_count: u.followingCount, following_count: u.followingCount,
statuses_count: u.notesCount, statuses_count: u.notesCount,
note: u.description?.replace(/\n|\\n/g, '<br>') ?? '', note: u.description?.replace(/\n|\\n/g, "<br>") ?? "",
url: acctUrl, url: acctUrl,
avatar: u.avatarUrl, avatar: u.avatarUrl,
avatar_static: u.avatarUrl, avatar_static: u.avatarUrl,
header: u.bannerUrl ?? this.plcUrl, header: u.bannerUrl ?? this.plcUrl,
header_static: u.bannerUrl ?? this.plcUrl, header_static: u.bannerUrl ?? this.plcUrl,
emojis: u.emojis.map(e => this.emoji(e)), emojis: u.emojis.map((e) => this.emoji(e)),
moved: null, moved: null,
fields: u.fields.map(f => this.field(f)), fields: u.fields.map((f) => this.field(f)),
bot: u.isBot, bot: u.isBot,
} };
} };
userPreferences = (u: MisskeyAPI.Entity.UserDetailMe, v: 'public' | 'unlisted' | 'private' | 'direct'): MegalodonEntity.Preferences => { userPreferences = (
u: MisskeyAPI.Entity.UserDetailMe,
v: "public" | "unlisted" | "private" | "direct",
): MegalodonEntity.Preferences => {
return { return {
"reading:expand:media": "default", "reading:expand:media": "default",
"reading:expand:spoilers": false, "reading:expand:spoilers": false,
"posting:default:language": u.lang, "posting:default:language": u.lang,
"posting:default:sensitive": u.alwaysMarkNsfw, "posting:default:sensitive": u.alwaysMarkNsfw,
"posting:default:visibility": v "posting:default:visibility": v,
} };
} };
visibility = (v: 'public' | 'home' | 'followers' | 'specified'): 'public' | 'unlisted' | 'private' | 'direct' => { visibility = (
v: "public" | "home" | "followers" | "specified",
): "public" | "unlisted" | "private" | "direct" => {
switch (v) { switch (v) {
case 'public': case "public":
return v return v;
case 'home': case "home":
return 'unlisted' return "unlisted";
case 'followers': case "followers":
return 'private' return "private";
case 'specified': case "specified":
return 'direct' return "direct";
}
} }
};
encodeVisibility = (v: 'public' | 'unlisted' | 'private' | 'direct'): 'public' | 'home' | 'followers' | 'specified' => { encodeVisibility = (
v: "public" | "unlisted" | "private" | "direct",
): "public" | "home" | "followers" | "specified" => {
switch (v) { switch (v) {
case 'public': case "public":
return v return v;
case 'unlisted': case "unlisted":
return 'home' return "home";
case 'private': case "private":
return 'followers' return "followers";
case 'direct': case "direct":
return 'specified' return "specified";
}
} }
};
fileType = (s: string): 'unknown' | 'image' | 'gifv' | 'video' | 'audio' => { fileType = (
if (s === 'image/gif') { s: string,
return 'gifv' ): "unknown" | "image" | "gifv" | "video" | "audio" => {
if (s === "image/gif") {
return "gifv";
} }
if (s.includes('image')) { if (s.includes("image")) {
return 'image' return "image";
} }
if (s.includes('video')) { if (s.includes("video")) {
return 'video' return "video";
} }
if (s.includes('audio')) { if (s.includes("audio")) {
return 'audio' return "audio";
}
return 'unknown'
} }
return "unknown";
};
file = (f: Entity.File): MegalodonEntity.Attachment => { file = (f: Entity.File): MegalodonEntity.Attachment => {
return { return {
@ -237,20 +250,20 @@ namespace MisskeyAPI {
text_url: f.url, text_url: f.url,
meta: { meta: {
width: f.properties.width, width: f.properties.width,
height: f.properties.height height: f.properties.height,
}, },
description: f.comment, description: f.comment,
blurhash: f.blurhash blurhash: f.blurhash,
} };
} };
follower = (f: Entity.Follower): MegalodonEntity.Account => { follower = (f: Entity.Follower): MegalodonEntity.Account => {
return this.user(f.follower) return this.user(f.follower);
} };
following = (f: Entity.Following): MegalodonEntity.Account => { following = (f: Entity.Following): MegalodonEntity.Account => {
return this.user(f.followee) return this.user(f.followee);
} };
relation = (r: Entity.Relation): MegalodonEntity.Relationship => { relation = (r: Entity.Relation): MegalodonEntity.Relationship => {
return { return {
@ -265,35 +278,37 @@ namespace MisskeyAPI {
domain_blocking: false, domain_blocking: false,
showing_reblogs: true, showing_reblogs: true,
endorsed: false, endorsed: false,
notifying: false notifying: false,
} };
} };
choice = (c: Entity.Choice): MegalodonEntity.PollOption => { choice = (c: Entity.Choice): MegalodonEntity.PollOption => {
return { return {
title: c.text, title: c.text,
votes_count: c.votes votes_count: c.votes,
} };
} };
poll = (p: Entity.Poll, id: string): MegalodonEntity.Poll => { poll = (p: Entity.Poll, id: string): MegalodonEntity.Poll => {
const now = dayjs() const now = dayjs();
const expire = dayjs(p.expiresAt) const expire = dayjs(p.expiresAt);
const count = p.choices.reduce((sum, choice) => sum + choice.votes, 0) const count = p.choices.reduce((sum, choice) => sum + choice.votes, 0);
return { return {
id: id, id: id,
expires_at: p.expiresAt, expires_at: p.expiresAt,
expired: now.isAfter(expire), expired: now.isAfter(expire),
multiple: p.multiple, multiple: p.multiple,
votes_count: count, votes_count: count,
options: p.choices.map(c => this.choice(c)), options: p.choices.map((c) => this.choice(c)),
voted: p.choices.some(c => c.isVoted), voted: p.choices.some((c) => c.isVoted),
own_votes: p.choices.filter(c => c.isVoted).map(c => p.choices.indexOf(c)) own_votes: p.choices
} .filter((c) => c.isVoted)
} .map((c) => p.choices.indexOf(c)),
};
};
note = (n: Entity.Note, host: string): MegalodonEntity.Status => { note = (n: Entity.Note, host: string): MegalodonEntity.Status => {
host = host.replace('https://', '') host = host.replace("https://", "");
return { return {
id: n.id, id: n.id,
@ -303,20 +318,20 @@ namespace MisskeyAPI {
in_reply_to_id: n.replyId, in_reply_to_id: n.replyId,
in_reply_to_account_id: n.reply?.userId ?? null, in_reply_to_account_id: n.reply?.userId ?? null,
reblog: n.renote ? this.note(n.renote, host) : null, reblog: n.renote ? this.note(n.renote, host) : null,
content: n.text ? this.escapeMFM(n.text) : '', content: n.text ? this.escapeMFM(n.text) : "",
plain_content: n.text ? n.text : null, plain_content: n.text ? n.text : null,
created_at: n.createdAt, created_at: n.createdAt,
emojis: n.emojis.map(e => this.emoji(e)), emojis: n.emojis.map((e) => this.emoji(e)),
replies_count: n.repliesCount, replies_count: n.repliesCount,
reblogs_count: n.renoteCount, reblogs_count: n.renoteCount,
favourites_count: this.getTotalReactions(n.reactions), favourites_count: this.getTotalReactions(n.reactions),
reblogged: false, reblogged: false,
favourited: !!n.myReaction, favourited: !!n.myReaction,
muted: false, muted: false,
sensitive: n.files ? n.files.some(f => f.isSensitive) : false, sensitive: n.files ? n.files.some((f) => f.isSensitive) : false,
spoiler_text: n.cw ? n.cw : '', spoiler_text: n.cw ? n.cw : "",
visibility: this.visibility(n.visibility), visibility: this.visibility(n.visibility),
media_attachments: n.files ? n.files.map(f => this.file(f)) : [], media_attachments: n.files ? n.files.map((f) => this.file(f)) : [],
mentions: [], mentions: [],
tags: [], tags: [],
card: null, card: null,
@ -326,110 +341,124 @@ namespace MisskeyAPI {
pinned: null, pinned: null,
emoji_reactions: this.mapReactions(n.reactions, n.myReaction), emoji_reactions: this.mapReactions(n.reactions, n.myReaction),
bookmarked: false, bookmarked: false,
quote: n.renote && n.text ? this.note(n.renote, host) : null quote: n.renote && n.text ? this.note(n.renote, host) : null,
} };
} };
mapReactions = (r: { [key: string]: number }, myReaction?: string): Array<MegalodonEntity.Reaction> => { mapReactions = (
return Object.keys(r).map(key => { r: { [key: string]: number },
myReaction?: string,
): Array<MegalodonEntity.Reaction> => {
return Object.keys(r).map((key) => {
if (myReaction && key === myReaction) { if (myReaction && key === myReaction) {
return { return {
count: r[key], count: r[key],
me: true, me: true,
name: key name: key,
} };
} }
return { return {
count: r[key], count: r[key],
me: false, me: false,
name: key name: key,
} };
}) });
} };
getTotalReactions = (r: { [key: string]: number }): number => { getTotalReactions = (r: { [key: string]: number }): number => {
return Object.values(r).length > 0 ? Object.values(r).reduce((previousValue, currentValue) => previousValue + currentValue) : 0 return Object.values(r).length > 0
} ? Object.values(r).reduce(
(previousValue, currentValue) => previousValue + currentValue,
)
: 0;
};
reactions = (r: Array<Entity.Reaction>): Array<MegalodonEntity.Reaction> => { reactions = (
const result: Array<MegalodonEntity.Reaction> = [] r: Array<Entity.Reaction>,
): Array<MegalodonEntity.Reaction> => {
const result: Array<MegalodonEntity.Reaction> = [];
for (const e of r) { for (const e of r) {
const i = result.findIndex(res => res.name === e.type) const i = result.findIndex((res) => res.name === e.type);
if (i >= 0) { if (i >= 0) {
result[i].count++ result[i].count++;
} else { } else {
result.push({ result.push({
count: 1, count: 1,
me: false, me: false,
name: e.type name: e.type,
}) });
} }
} }
return result return result;
} };
noteToConversation = (n: Entity.Note, host: string): MegalodonEntity.Conversation => { noteToConversation = (
const accounts: Array<MegalodonEntity.Account> = [this.user(n.user)] n: Entity.Note,
host: string,
): MegalodonEntity.Conversation => {
const accounts: Array<MegalodonEntity.Account> = [this.user(n.user)];
if (n.reply) { if (n.reply) {
accounts.push(this.user(n.reply.user)) accounts.push(this.user(n.reply.user));
} }
return { return {
id: n.id, id: n.id,
accounts: accounts, accounts: accounts,
last_status: this.note(n, host), last_status: this.note(n, host),
unread: false unread: false,
} };
} };
list = (l: Entity.List): MegalodonEntity.List => ({ list = (l: Entity.List): MegalodonEntity.List => ({
id: l.id, id: l.id,
title: l.name title: l.name,
}) });
encodeNotificationType = (e: MegalodonEntity.NotificationType): MisskeyEntity.NotificationType => { encodeNotificationType = (
e: MegalodonEntity.NotificationType,
): MisskeyEntity.NotificationType => {
switch (e) { switch (e) {
case NotificationType.Follow: case NotificationType.Follow:
return MisskeyNotificationType.Follow return MisskeyNotificationType.Follow;
case NotificationType.Mention: case NotificationType.Mention:
return MisskeyNotificationType.Reply return MisskeyNotificationType.Reply;
case NotificationType.Favourite: case NotificationType.Favourite:
case NotificationType.EmojiReaction: case NotificationType.EmojiReaction:
return MisskeyNotificationType.Reaction return MisskeyNotificationType.Reaction;
case NotificationType.Reblog: case NotificationType.Reblog:
return MisskeyNotificationType.Renote return MisskeyNotificationType.Renote;
case NotificationType.Poll: case NotificationType.Poll:
return MisskeyNotificationType.PollEnded return MisskeyNotificationType.PollEnded;
case NotificationType.FollowRequest: case NotificationType.FollowRequest:
return MisskeyNotificationType.ReceiveFollowRequest return MisskeyNotificationType.ReceiveFollowRequest;
default: default:
return e return e;
}
} }
};
decodeNotificationType = (e: MisskeyEntity.NotificationType): MegalodonEntity.NotificationType => { decodeNotificationType = (
e: MisskeyEntity.NotificationType,
): MegalodonEntity.NotificationType => {
switch (e) { switch (e) {
case MisskeyNotificationType.Follow: case MisskeyNotificationType.Follow:
return NotificationType.Follow return NotificationType.Follow;
case MisskeyNotificationType.Mention: case MisskeyNotificationType.Mention:
case MisskeyNotificationType.Reply: case MisskeyNotificationType.Reply:
return NotificationType.Mention return NotificationType.Mention;
case MisskeyNotificationType.Renote: case MisskeyNotificationType.Renote:
case MisskeyNotificationType.Quote: case MisskeyNotificationType.Quote:
return NotificationType.Reblog return NotificationType.Reblog;
case MisskeyNotificationType.Reaction: case MisskeyNotificationType.Reaction:
return NotificationType.EmojiReaction return NotificationType.EmojiReaction;
case MisskeyNotificationType.PollEnded: case MisskeyNotificationType.PollEnded:
return NotificationType.Poll return NotificationType.Poll;
case MisskeyNotificationType.ReceiveFollowRequest: case MisskeyNotificationType.ReceiveFollowRequest:
return NotificationType.FollowRequest return NotificationType.FollowRequest;
case MisskeyNotificationType.FollowRequestAccepted: case MisskeyNotificationType.FollowRequestAccepted:
return NotificationType.Follow return NotificationType.Follow;
default: default:
return e return e;
} }
} };
announcement = (a: Entity.Announcement): MegalodonEntity.Announcement => ({ announcement = (a: Entity.Announcement): MegalodonEntity.Announcement => ({
id: a.id, id: a.id,
@ -446,43 +475,46 @@ namespace MisskeyAPI {
tags: [], tags: [],
emojis: [], emojis: [],
reactions: [], reactions: [],
}) });
notification = (n: Entity.Notification, host: string): MegalodonEntity.Notification => { notification = (
n: Entity.Notification,
host: string,
): MegalodonEntity.Notification => {
let notification = { let notification = {
id: n.id, id: n.id,
account: n.user ? this.user(n.user) : this.modelOfAcct, account: n.user ? this.user(n.user) : this.modelOfAcct,
created_at: n.createdAt, created_at: n.createdAt,
type: this.decodeNotificationType(n.type) type: this.decodeNotificationType(n.type),
} };
if (n.note) { if (n.note) {
notification = Object.assign(notification, { notification = Object.assign(notification, {
status: this.note(n.note, host) status: this.note(n.note, host),
}) });
if (notification.type === NotificationType.Poll) { if (notification.type === NotificationType.Poll) {
notification = Object.assign(notification, { notification = Object.assign(notification, {
account: this.note(n.note, host).account account: this.note(n.note, host).account,
}) });
} }
} }
if (n.reaction) { if (n.reaction) {
notification = Object.assign(notification, { notification = Object.assign(notification, {
emoji: n.reaction emoji: n.reaction,
}) });
}
return notification
} }
return notification;
};
stats = (s: Entity.Stats): MegalodonEntity.Stats => { stats = (s: Entity.Stats): MegalodonEntity.Stats => {
return { return {
user_count: s.usersCount, user_count: s.usersCount,
status_count: s.notesCount, status_count: s.notesCount,
domain_count: s.instances domain_count: s.instances,
} };
} };
meta = (m: Entity.Meta, s: Entity.Stats): MegalodonEntity.Instance => { meta = (m: Entity.Meta, s: Entity.Stats): MegalodonEntity.Instance => {
const wss = m.uri.replace(/^https:\/\//, 'wss://') const wss = m.uri.replace(/^https:\/\//, "wss://");
return { return {
uri: m.uri, uri: m.uri,
title: m.name, title: m.name,
@ -491,54 +523,67 @@ namespace MisskeyAPI {
version: m.version, version: m.version,
thumbnail: m.bannerUrl, thumbnail: m.bannerUrl,
urls: { urls: {
streaming_api: `${wss}/streaming` streaming_api: `${wss}/streaming`,
}, },
stats: this.stats(s), stats: this.stats(s),
languages: m.langs, languages: m.langs,
contact_account: null, contact_account: null,
max_toot_chars: m.maxNoteTextLength, max_toot_chars: m.maxNoteTextLength,
registrations: !m.disableRegistration registrations: !m.disableRegistration,
} };
} };
hashtag = (h: Entity.Hashtag): MegalodonEntity.Tag => { hashtag = (h: Entity.Hashtag): MegalodonEntity.Tag => {
return { return {
name: h.tag, name: h.tag,
url: h.tag, url: h.tag,
history: null, history: null,
following: false following: false,
} };
} };
} }
export const DEFAULT_SCOPE = [ export const DEFAULT_SCOPE = [
'read:account', "read:account",
'write:account', "write:account",
'read:blocks', "read:blocks",
'write:blocks', "write:blocks",
'read:drive', "read:drive",
'write:drive', "write:drive",
'read:favorites', "read:favorites",
'write:favorites', "write:favorites",
'read:following', "read:following",
'write:following', "write:following",
'read:mutes', "read:mutes",
'write:mutes', "write:mutes",
'write:notes', "write:notes",
'read:notifications', "read:notifications",
'write:notifications', "write:notifications",
'read:reactions', "read:reactions",
'write:reactions', "write:reactions",
'write:votes' "write:votes",
] ];
/** /**
* Interface * Interface
*/ */
export interface Interface { export interface Interface {
post<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>> post<T = any>(
cancel(): void path: string,
socket(channel: 'user' | 'localTimeline' | 'hybridTimeline' | 'globalTimeline' | 'conversation' | 'list', listId?: string): WebSocket params?: any,
headers?: { [key: string]: string },
): Promise<Response<T>>;
cancel(): void;
socket(
channel:
| "user"
| "localTimeline"
| "hybridTimeline"
| "globalTimeline"
| "conversation"
| "list",
listId?: string,
): WebSocket;
} }
/** /**
@ -547,12 +592,12 @@ namespace MisskeyAPI {
* Usign axios for request, you will handle promises. * Usign axios for request, you will handle promises.
*/ */
export class Client implements Interface { export class Client implements Interface {
private accessToken: string | null private accessToken: string | null;
private baseUrl: string private baseUrl: string;
private userAgent: string private userAgent: string;
private abortController: AbortController private abortController: AbortController;
private proxyConfig: ProxyConfig | false = false private proxyConfig: ProxyConfig | false = false;
private converter: Converter private converter: Converter;
/** /**
* @param baseUrl hostname or base URL * @param baseUrl hostname or base URL
@ -561,14 +606,20 @@ namespace MisskeyAPI {
* @param proxyConfig Proxy setting, or set false if don't use proxy. * @param proxyConfig Proxy setting, or set false if don't use proxy.
* @param converter Converter instance. * @param converter Converter instance.
*/ */
constructor(baseUrl: string, accessToken: string | null, userAgent: string = DEFAULT_UA, proxyConfig: ProxyConfig | false = false, converter: Converter) { constructor(
this.accessToken = accessToken baseUrl: string,
this.baseUrl = baseUrl accessToken: string | null,
this.userAgent = userAgent userAgent: string = DEFAULT_UA,
this.proxyConfig = proxyConfig proxyConfig: ProxyConfig | false = false,
this.abortController = new AbortController() converter: Converter,
this.converter = converter ) {
axios.defaults.signal = this.abortController.signal this.accessToken = accessToken;
this.baseUrl = baseUrl;
this.userAgent = userAgent;
this.proxyConfig = proxyConfig;
this.abortController = new AbortController();
this.converter = converter;
axios.defaults.signal = this.abortController.signal;
} }
/** /**
@ -577,38 +628,44 @@ namespace MisskeyAPI {
* @param params Form data * @param params Form data
* @param headers Request header object * @param headers Request header object
*/ */
public async post<T>(path: string, params: any = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> { public async post<T>(
path: string,
params: any = {},
headers: { [key: string]: string } = {},
): Promise<Response<T>> {
let options: AxiosRequestConfig = { let options: AxiosRequestConfig = {
headers: headers, headers: headers,
maxContentLength: Infinity, maxContentLength: Infinity,
maxBodyLength: Infinity maxBodyLength: Infinity,
} };
if (this.proxyConfig) { if (this.proxyConfig) {
options = Object.assign(options, { options = Object.assign(options, {
httpAgent: proxyAgent(this.proxyConfig), httpAgent: proxyAgent(this.proxyConfig),
httpsAgent: proxyAgent(this.proxyConfig) httpsAgent: proxyAgent(this.proxyConfig),
}) });
} }
let bodyParams = params let bodyParams = params;
if (this.accessToken) { if (this.accessToken) {
if (params instanceof FormData) { if (params instanceof FormData) {
bodyParams.append('i', this.accessToken) bodyParams.append("i", this.accessToken);
} else { } else {
bodyParams = Object.assign(params, { bodyParams = Object.assign(params, {
i: this.accessToken i: this.accessToken,
}) });
} }
} }
return axios.post<T>(this.baseUrl + path, bodyParams, options).then((resp: AxiosResponse<T>) => { return axios
.post<T>(this.baseUrl + path, bodyParams, options)
.then((resp: AxiosResponse<T>) => {
const res: Response<T> = { const res: Response<T> = {
data: resp.data, data: resp.data,
status: resp.status, status: resp.status,
statusText: resp.statusText, statusText: resp.statusText,
headers: resp.headers headers: resp.headers,
} };
return res return res;
}) });
} }
/** /**
@ -616,7 +673,7 @@ namespace MisskeyAPI {
* @returns void * @returns void
*/ */
public cancel(): void { public cancel(): void {
return this.abortController.abort() return this.abortController.abort();
} }
/** /**
@ -626,20 +683,34 @@ namespace MisskeyAPI {
* @param listId This parameter is required only list channel. * @param listId This parameter is required only list channel.
*/ */
public socket( public socket(
channel: 'user' | 'localTimeline' | 'hybridTimeline' | 'globalTimeline' | 'conversation' | 'list', channel:
listId?: string | "user"
| "localTimeline"
| "hybridTimeline"
| "globalTimeline"
| "conversation"
| "list",
listId?: string,
): WebSocket { ): WebSocket {
if (!this.accessToken) { if (!this.accessToken) {
throw new Error('accessToken is required') throw new Error("accessToken is required");
} }
const url = `${this.baseUrl}/streaming` const url = `${this.baseUrl}/streaming`;
const streaming = new WebSocket(url, channel, this.accessToken, listId, this.userAgent, this.proxyConfig, this.converter) const streaming = new WebSocket(
url,
channel,
this.accessToken,
listId,
this.userAgent,
this.proxyConfig,
this.converter,
);
process.nextTick(() => { process.nextTick(() => {
streaming.start() streaming.start();
}) });
return streaming return streaming;
} }
} }
} }
export default MisskeyAPI export default MisskeyAPI;

View File

@ -25,4 +25,4 @@
/// <reference path="entities/session.ts" /> /// <reference path="entities/session.ts" />
/// <reference path="entities/stats.ts" /> /// <reference path="entities/stats.ts" />
export default MisskeyEntity export default MisskeyEntity;

View File

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

View File

@ -1,34 +1,43 @@
import WS from 'ws' import WS from "ws";
import dayjs, { Dayjs } from 'dayjs' import dayjs, { Dayjs } from "dayjs";
import { v4 as uuid } from 'uuid' import { v4 as uuid } from "uuid";
import { EventEmitter } from 'events' import { EventEmitter } from "events";
import { WebSocketInterface } from '../megalodon' import { WebSocketInterface } from "../megalodon";
import proxyAgent, { ProxyConfig } from '../proxy_config' import proxyAgent, { ProxyConfig } from "../proxy_config";
import MisskeyAPI from './api_client' import MisskeyAPI from "./api_client";
/** /**
* WebSocket * WebSocket
* Misskey is not support http streaming. It supports websocket instead of streaming. * Misskey is not support http streaming. It supports websocket instead of streaming.
* So this class connect to Misskey server with WebSocket. * So this class connect to Misskey server with WebSocket.
*/ */
export default class WebSocket extends EventEmitter implements WebSocketInterface { export default class WebSocket
public url: string extends EventEmitter
public channel: 'user' | 'localTimeline' | 'hybridTimeline' | 'globalTimeline' | 'conversation' | 'list' implements WebSocketInterface
public parser: any {
public headers: { [key: string]: string } public url: string;
public proxyConfig: ProxyConfig | false = false public channel:
public listId: string | null = null | "user"
private _converter: MisskeyAPI.Converter | "localTimeline"
private _accessToken: string | "hybridTimeline"
private _reconnectInterval: number | "globalTimeline"
private _reconnectMaxAttempts: number | "conversation"
private _reconnectCurrentAttempts: number | "list";
private _connectionClosed: boolean public parser: any;
private _client: WS | null = null public headers: { [key: string]: string };
private _channelID: string public proxyConfig: ProxyConfig | false = false;
private _pongReceivedTimestamp: Dayjs public listId: string | null = null;
private _heartbeatInterval: number = 60000 private _converter: MisskeyAPI.Converter;
private _pongWaiting: boolean = false private _accessToken: string;
private _reconnectInterval: number;
private _reconnectMaxAttempts: number;
private _reconnectCurrentAttempts: number;
private _connectionClosed: boolean;
private _client: WS | null = null;
private _channelID: string;
private _pongReceivedTimestamp: Dayjs;
private _heartbeatInterval = 60000;
private _pongWaiting = false;
/** /**
* @param url Full url of websocket: e.g. wss://misskey.io/streaming * @param url Full url of websocket: e.g. wss://misskey.io/streaming
@ -38,66 +47,72 @@ export default class WebSocket extends EventEmitter implements WebSocketInterfac
*/ */
constructor( constructor(
url: string, url: string,
channel: 'user' | 'localTimeline' | 'hybridTimeline' | 'globalTimeline' | 'conversation' | 'list', channel:
| "user"
| "localTimeline"
| "hybridTimeline"
| "globalTimeline"
| "conversation"
| "list",
accessToken: string, accessToken: string,
listId: string | undefined, listId: string | undefined,
userAgent: string, userAgent: string,
proxyConfig: ProxyConfig | false = false, proxyConfig: ProxyConfig | false = false,
converter: MisskeyAPI.Converter converter: MisskeyAPI.Converter,
) { ) {
super() super();
this.url = url this.url = url;
this.parser = new Parser() this.parser = new Parser();
this.channel = channel this.channel = channel;
this.headers = { this.headers = {
'User-Agent': userAgent "User-Agent": userAgent,
} };
if (listId === undefined) { if (listId === undefined) {
this.listId = null this.listId = null;
} else { } else {
this.listId = listId this.listId = listId;
} }
this.proxyConfig = proxyConfig this.proxyConfig = proxyConfig;
this._accessToken = accessToken this._accessToken = accessToken;
this._reconnectInterval = 10000 this._reconnectInterval = 10000;
this._reconnectMaxAttempts = Infinity this._reconnectMaxAttempts = Infinity;
this._reconnectCurrentAttempts = 0 this._reconnectCurrentAttempts = 0;
this._connectionClosed = false this._connectionClosed = false;
this._channelID = uuid() this._channelID = uuid();
this._pongReceivedTimestamp = dayjs() this._pongReceivedTimestamp = dayjs();
this._converter = converter this._converter = converter;
} }
/** /**
* Start websocket connection. * Start websocket connection.
*/ */
public start() { public start() {
this._connectionClosed = false this._connectionClosed = false;
this._resetRetryParams() this._resetRetryParams();
this._startWebSocketConnection() this._startWebSocketConnection();
} }
private baseUrlToHost(baseUrl: string): string { private baseUrlToHost(baseUrl: string): string {
return baseUrl.replace('https://', '') return baseUrl.replace("https://", "");
} }
/** /**
* Reset connection and start new websocket connection. * Reset connection and start new websocket connection.
*/ */
private _startWebSocketConnection() { private _startWebSocketConnection() {
this._resetConnection() this._resetConnection();
this._setupParser() this._setupParser();
this._client = this._connect() this._client = this._connect();
this._bindSocket(this._client) this._bindSocket(this._client);
} }
/** /**
* Stop current connection. * Stop current connection.
*/ */
public stop() { public stop() {
this._connectionClosed = true this._connectionClosed = true;
this._resetConnection() this._resetConnection();
this._resetRetryParams() this._resetRetryParams();
} }
/** /**
@ -105,13 +120,13 @@ export default class WebSocket extends EventEmitter implements WebSocketInterfac
*/ */
private _resetConnection() { private _resetConnection() {
if (this._client) { if (this._client) {
this._client.close(1000) this._client.close(1000);
this._client.removeAllListeners() this._client.removeAllListeners();
this._client = null this._client = null;
} }
if (this.parser) { if (this.parser) {
this.parser.removeAllListeners() this.parser.removeAllListeners();
} }
} }
@ -119,7 +134,7 @@ export default class WebSocket extends EventEmitter implements WebSocketInterfac
* Resets the parameters used in reconnect. * Resets the parameters used in reconnect.
*/ */
private _resetRetryParams() { private _resetRetryParams() {
this._reconnectCurrentAttempts = 0 this._reconnectCurrentAttempts = 0;
} }
/** /**
@ -127,15 +142,15 @@ export default class WebSocket extends EventEmitter implements WebSocketInterfac
*/ */
private _connect(): WS { private _connect(): WS {
let options: WS.ClientOptions = { let options: WS.ClientOptions = {
headers: this.headers headers: this.headers,
} };
if (this.proxyConfig) { if (this.proxyConfig) {
options = Object.assign(options, { options = Object.assign(options, {
agent: proxyAgent(this.proxyConfig) agent: proxyAgent(this.proxyConfig),
}) });
} }
const cli: WS = new WS(`${this.url}?i=${this._accessToken}`, options) const cli: WS = new WS(`${this.url}?i=${this._accessToken}`, options);
return cli return cli;
} }
/** /**
@ -143,65 +158,65 @@ export default class WebSocket extends EventEmitter implements WebSocketInterfac
*/ */
private _channel() { private _channel() {
if (!this._client) { if (!this._client) {
return return;
} }
switch (this.channel) { switch (this.channel) {
case 'conversation': case "conversation":
this._client.send( this._client.send(
JSON.stringify({ JSON.stringify({
type: 'connect', type: "connect",
body: { body: {
channel: 'main', channel: "main",
id: this._channelID id: this._channelID,
} },
}) }),
) );
break break;
case 'user': case "user":
this._client.send( this._client.send(
JSON.stringify({ JSON.stringify({
type: 'connect', type: "connect",
body: { body: {
channel: 'main', channel: "main",
id: this._channelID id: this._channelID,
} },
}) }),
) );
this._client.send( this._client.send(
JSON.stringify({ JSON.stringify({
type: 'connect', type: "connect",
body: { body: {
channel: 'homeTimeline', channel: "homeTimeline",
id: this._channelID id: this._channelID,
} },
}) }),
) );
break break;
case 'list': case "list":
this._client.send( this._client.send(
JSON.stringify({ JSON.stringify({
type: 'connect', type: "connect",
body: { body: {
channel: 'userList', channel: "userList",
id: this._channelID, id: this._channelID,
params: { params: {
listId: this.listId listId: this.listId,
} },
} },
}) }),
) );
break break;
default: default:
this._client.send( this._client.send(
JSON.stringify({ JSON.stringify({
type: 'connect', type: "connect",
body: { body: {
channel: this.channel, channel: this.channel,
id: this._channelID id: this._channelID,
} },
}) }),
) );
break break;
} }
} }
@ -214,23 +229,23 @@ export default class WebSocket extends EventEmitter implements WebSocketInterfac
// Skip reconnect when client is connecting. // Skip reconnect when client is connecting.
// https://github.com/websockets/ws/blob/7.2.1/lib/websocket.js#L365 // https://github.com/websockets/ws/blob/7.2.1/lib/websocket.js#L365
if (this._client && this._client.readyState === WS.CONNECTING) { if (this._client && this._client.readyState === WS.CONNECTING) {
return return;
} }
if (this._reconnectCurrentAttempts < this._reconnectMaxAttempts) { if (this._reconnectCurrentAttempts < this._reconnectMaxAttempts) {
this._reconnectCurrentAttempts++ this._reconnectCurrentAttempts++;
this._clearBinding() this._clearBinding();
if (this._client) { if (this._client) {
// In reconnect, we want to close the connection immediately, // In reconnect, we want to close the connection immediately,
// because recoonect is necessary when some problems occur. // because recoonect is necessary when some problems occur.
this._client.terminate() this._client.terminate();
} }
// Call connect methods // Call connect methods
console.log('Reconnecting') console.log("Reconnecting");
this._client = this._connect() this._client = this._connect();
this._bindSocket(this._client) this._bindSocket(this._client);
} }
}, this._reconnectInterval) }, this._reconnectInterval);
} }
/** /**
@ -238,11 +253,11 @@ export default class WebSocket extends EventEmitter implements WebSocketInterfac
*/ */
private _clearBinding() { private _clearBinding() {
if (this._client) { if (this._client) {
this._client.removeAllListeners('close') this._client.removeAllListeners("close");
this._client.removeAllListeners('pong') this._client.removeAllListeners("pong");
this._client.removeAllListeners('open') this._client.removeAllListeners("open");
this._client.removeAllListeners('message') this._client.removeAllListeners("message");
this._client.removeAllListeners('error') this._client.removeAllListeners("error");
} }
} }
@ -251,76 +266,97 @@ export default class WebSocket extends EventEmitter implements WebSocketInterfac
* @param client A WebSocket instance. * @param client A WebSocket instance.
*/ */
private _bindSocket(client: WS) { private _bindSocket(client: WS) {
client.on('close', (code: number, _reason: Buffer) => { client.on("close", (code: number, _reason: Buffer) => {
if (code === 1000) { if (code === 1000) {
this.emit('close', {}) this.emit("close", {});
} else { } else {
console.log(`Closed connection with ${code}`) console.log(`Closed connection with ${code}`);
if (!this._connectionClosed) { if (!this._connectionClosed) {
this._reconnect() this._reconnect();
} }
} }
}) });
client.on('pong', () => { client.on("pong", () => {
this._pongWaiting = false this._pongWaiting = false;
this.emit('pong', {}) this.emit("pong", {});
this._pongReceivedTimestamp = dayjs() this._pongReceivedTimestamp = dayjs();
// It is required to anonymous function since get this scope in checkAlive. // It is required to anonymous function since get this scope in checkAlive.
setTimeout(() => this._checkAlive(this._pongReceivedTimestamp), this._heartbeatInterval) setTimeout(
}) () => this._checkAlive(this._pongReceivedTimestamp),
client.on('open', () => { this._heartbeatInterval,
this.emit('connect', {}) );
this._channel() });
client.on("open", () => {
this.emit("connect", {});
this._channel();
// Call first ping event. // Call first ping event.
setTimeout(() => { setTimeout(() => {
client.ping('') client.ping("");
}, 10000) }, 10000);
}) });
client.on('message', (data: WS.Data, isBinary: boolean) => { client.on("message", (data: WS.Data, isBinary: boolean) => {
this.parser.parse(data, isBinary, this._channelID) this.parser.parse(data, isBinary, this._channelID);
}) });
client.on('error', (err: Error) => { client.on("error", (err: Error) => {
this.emit('error', err) this.emit("error", err);
}) });
} }
/** /**
* Set up parser when receive message. * Set up parser when receive message.
*/ */
private _setupParser() { private _setupParser() {
this.parser.on('update', (note: MisskeyAPI.Entity.Note) => { this.parser.on("update", (note: MisskeyAPI.Entity.Note) => {
this.emit('update', this._converter.note(note, this.baseUrlToHost(this.url))) this.emit(
}) "update",
this.parser.on('notification', (notification: MisskeyAPI.Entity.Notification) => { this._converter.note(note, this.baseUrlToHost(this.url)),
this.emit('notification', this._converter.notification(notification, this.baseUrlToHost(this.url))) );
}) });
this.parser.on('conversation', (note: MisskeyAPI.Entity.Note) => { this.parser.on(
this.emit('conversation', this._converter.noteToConversation(note, this.baseUrlToHost(this.url))) "notification",
}) (notification: MisskeyAPI.Entity.Notification) => {
this.parser.on('error', (err: Error) => { this.emit(
this.emit('parser-error', err) "notification",
}) this._converter.notification(
notification,
this.baseUrlToHost(this.url),
),
);
},
);
this.parser.on("conversation", (note: MisskeyAPI.Entity.Note) => {
this.emit(
"conversation",
this._converter.noteToConversation(note, this.baseUrlToHost(this.url)),
);
});
this.parser.on("error", (err: Error) => {
this.emit("parser-error", err);
});
} }
/** /**
* Call ping and wait to pong. * Call ping and wait to pong.
*/ */
private _checkAlive(timestamp: Dayjs) { private _checkAlive(timestamp: Dayjs) {
const now: Dayjs = dayjs() const now: Dayjs = dayjs();
// Block multiple calling, if multiple pong event occur. // Block multiple calling, if multiple pong event occur.
// It the duration is less than interval, through ping. // It the duration is less than interval, through ping.
if (now.diff(timestamp) > this._heartbeatInterval - 1000 && !this._connectionClosed) { if (
now.diff(timestamp) > this._heartbeatInterval - 1000 &&
!this._connectionClosed
) {
// Skip ping when client is connecting. // Skip ping when client is connecting.
// https://github.com/websockets/ws/blob/7.2.1/lib/websocket.js#L289 // https://github.com/websockets/ws/blob/7.2.1/lib/websocket.js#L289
if (this._client && this._client.readyState !== WS.CONNECTING) { if (this._client && this._client.readyState !== WS.CONNECTING) {
this._pongWaiting = true this._pongWaiting = true;
this._client.ping('') this._client.ping("");
setTimeout(() => { setTimeout(() => {
if (this._pongWaiting) { if (this._pongWaiting) {
this._pongWaiting = false this._pongWaiting = false;
this._reconnect() this._reconnect();
} }
}, 10000) }, 10000);
} }
} }
} }
@ -336,79 +372,87 @@ export class Parser extends EventEmitter {
* @param channelID Parse only messages which has same channelID. * @param channelID Parse only messages which has same channelID.
*/ */
public parse(data: WS.Data, isBinary: boolean, channelID: string) { public parse(data: WS.Data, isBinary: boolean, channelID: string) {
const message = isBinary ? data : data.toString() const message = isBinary ? data : data.toString();
if (typeof message !== 'string') { if (typeof message !== "string") {
this.emit('heartbeat', {}) this.emit("heartbeat", {});
return return;
} }
if (message === '') { if (message === "") {
this.emit('heartbeat', {}) this.emit("heartbeat", {});
return return;
} }
let obj: { let obj: {
type: string type: string;
body: { body: {
id: string id: string;
type: string type: string;
body: any body: any;
} };
} };
let body: { let body: {
id: string id: string;
type: string type: string;
body: any body: any;
} };
try { try {
obj = JSON.parse(message) obj = JSON.parse(message);
if (obj.type !== 'channel') { if (obj.type !== "channel") {
return return;
} }
if (!obj.body) { if (!obj.body) {
return return;
} }
body = obj.body body = obj.body;
if (body.id !== channelID) { if (body.id !== channelID) {
return return;
} }
} catch (err) { } catch (err) {
this.emit('error', new Error(`Error parsing websocket reply: ${message}, error message: ${err}`)) this.emit(
return "error",
new Error(
`Error parsing websocket reply: ${message}, error message: ${err}`,
),
);
return;
} }
switch (body.type) { switch (body.type) {
case 'note': case "note":
this.emit('update', body.body as MisskeyAPI.Entity.Note) this.emit("update", body.body as MisskeyAPI.Entity.Note);
break break;
case 'notification': case "notification":
this.emit('notification', body.body as MisskeyAPI.Entity.Notification) this.emit("notification", body.body as MisskeyAPI.Entity.Notification);
break break;
case 'mention': { case "mention": {
const note = body.body as MisskeyAPI.Entity.Note const note = body.body as MisskeyAPI.Entity.Note;
if (note.visibility === 'specified') { if (note.visibility === "specified") {
this.emit('conversation', note) this.emit("conversation", note);
} }
break break;
} }
// When renote and followed event, the same notification will be received. // When renote and followed event, the same notification will be received.
case 'renote': case "renote":
case 'followed': case "followed":
case 'follow': case "follow":
case 'unfollow': case "unfollow":
case 'receiveFollowRequest': case "receiveFollowRequest":
case 'meUpdated': case "meUpdated":
case 'readAllNotifications': case "readAllNotifications":
case 'readAllUnreadSpecifiedNotes': case "readAllUnreadSpecifiedNotes":
case 'readAllAntennas': case "readAllAntennas":
case 'readAllUnreadMentions': case "readAllUnreadMentions":
case 'unreadNotification': case "unreadNotification":
// Ignore these events // Ignore these events
break break;
default: default:
this.emit('error', new Error(`Unknown event has received: ${JSON.stringify(body)}`)) this.emit(
break "error",
new Error(`Unknown event has received: ${JSON.stringify(body)}`),
);
break;
} }
} }
} }