Compare commits

...

55 commits

Author SHA1 Message Date
Laura Hausmann 870618a196
[mastodon-client] fix mentions of remote local users without host 2023-07-18 18:00:54 +02:00
Laura Hausmann 006ff07a0c
[mastodon-client] improve mention regexes 2023-07-18 16:51:03 +02:00
Laura Hausmann 57fe933bb6
[mastodon-client] fix getDefaultPostPrivacy 2023-07-10 18:59:06 +02:00
Laura Hausmann 5682259ecf
[mastodon-client] userDetail: fallback to username if displayname is null 2023-07-10 18:59:05 +02:00
Laura Hausmann 045ed1e202
[mastodon-client] return actual status bookmark state 2023-07-10 00:13:16 +02:00
Laura Hausmann 9aceebd26f
[mastodon-client] enforce limits for favorites and bookmarks 2023-07-10 00:13:16 +02:00
marcin mikołajczak f8b87f0ad2
Fix Mastodon compatibility string
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
2023-07-09 23:50:35 +02:00
Laura Hausmann d232f1c468
[mastodon-client] return 404 instead of 401 on NO_SUCH_NOTE 2023-07-09 23:49:55 +02:00
Laura Hausmann 7c94dbde77
[server/api/notes/show] return correct status code 2023-07-09 23:49:55 +02:00
Laura Hausmann cb31a0f91c
[mastodon-client] fix posting polls 2023-07-09 23:49:55 +02:00
Laura Hausmann 93693ec994
[mastodon-client] populate user details for notifications 2023-07-09 22:00:22 +02:00
Laura Hausmann ef389d58ac
[mastodon-client] fix poll notifications 2023-07-09 22:00:22 +02:00
Laura Hausmann 380e48227e
[mastodon-client] enforce limit for notifications 2023-07-09 03:44:51 +02:00
Laura Hausmann 857b440582
[mastodon-client] always sort timelines by id (descending) 2023-07-09 03:18:08 +02:00
Laura Hausmann b04cf29ec0
[mastodon-client] populate user details for account following/followers 2023-07-09 00:36:12 +02:00
Laura Hausmann 02f23bc62c
[mastodon-client] enforce limits for /following and /followers 2023-07-09 00:22:05 +02:00
Laura Hausmann dd80726c13
[mastodon-client] TODO/FIXME cleanup 2023-07-09 00:14:27 +02:00
Laura Hausmann 80f12cd9e6
[mastodon-client] drive file name fixup 2023-07-09 00:09:49 +02:00
Laura Hausmann 8b3aed0531
[mastodon-client] media caption support 2023-07-09 00:09:49 +02:00
Laura Hausmann 75b1af0ef5
[mastodon-client] send proper user preferences 2023-07-08 21:56:12 +02:00
Laura Hausmann ae35aad518
[mastodon-client] send actual default post privacy instead of fallback value 2023-07-08 21:56:12 +02:00
Laura Hausmann b5bd8f7011
[mastodon-client] remove reactions reply 2023-07-08 18:59:00 +02:00
Laura Hausmann da4d25c796
increase ap/show rate limit 2023-07-08 04:13:15 +02:00
Laura Hausmann e42a8739a1
[mastodon-client] handle user & note URLs in search 2023-07-08 04:13:15 +02:00
Laura Hausmann 57d9ac9029
[mastodon-client] render bio newlines correctly 2023-07-08 04:13:15 +02:00
Laura Hausmann 0045b7c9a4
[mastodon-client] fix polls 2023-07-08 02:33:03 +02:00
Laura Hausmann 5417d148a4
[mastodon-client] fix local instance mentions 2023-07-08 02:32:58 +02:00
Laura Hausmann f230e3c350
[mastodon-client] fix search type param 2023-07-07 23:52:02 +02:00
Laura Hausmann 991988ccd7
[mastodon-client] populate details for quote and reblog fields 2023-07-07 23:23:54 +02:00
Laura Hausmann 36eab09058
[mastodon-client] populate user data for favorited_by and reblogged_by 2023-07-07 22:59:54 +02:00
Laura Hausmann 72e27ca49a
[mastodon-client] implement favorited_by 2023-07-07 22:43:58 +02:00
Laura Hausmann f3fdf1d4e2
[mastodon-client] populate note details for notifications 2023-07-07 22:08:46 +02:00
Laura Hausmann 1c16ff70ec
[mastodon-client] populate user details for all notes 2023-07-07 21:52:16 +02:00
Laura Hausmann efc3ec29cd
[mastodon-client] Don't display unsupported notification types 2023-07-07 21:52:16 +02:00
Laura Hausmann 568a9c58e2
[mastodon-client] Fix global timeline 2023-07-07 21:52:16 +02:00
Laura Hausmann fa8d965e8b
[mastodon-client] Fail gracefully if user resolve fails 2023-07-07 19:01:15 +02:00
Laura Hausmann 4512609545
[mastodon-client] Remove even more debug messages 2023-07-06 04:47:06 +02:00
Laura Hausmann 7cf26cf2e3
PR cleanup 2023-07-06 04:47:06 +02:00
Laura Hausmann b84e8116f3
[mastodon-client] code cleanup, remove redundant function calls 2023-07-06 04:47:06 +02:00
Laura Hausmann bdcd567a41
Remove debug logging 2023-07-06 04:47:06 +02:00
Laura Hausmann 9a0d74a5c5
[mastodon-client] fix mentions 2023-07-06 04:47:05 +02:00
Laura Hausmann 4aa088f173
[mastodon-client] remove debug logging 2023-07-06 04:47:05 +02:00
Laura Hausmann 33aa901486
[mastodon-client] Remove compiled megalodon code 2023-07-06 04:47:05 +02:00
Laura Hausmann 92b4222bc3
[mastodon-client] general code cleanup, rename megalodon package 2023-07-06 04:47:05 +02:00
Laura Hausmann 87f7b32ba3
[mastodon-client] Disable emoji reactions preview in TL 2023-07-06 04:47:05 +02:00
Laura Hausmann 1f11bb3559
[mastodon-client] Move megalodon to main repo 2023-07-06 04:47:05 +02:00
Laura Hausmann 9d4bef0a91
[mastodon-client] Code formatting pass 2023-07-06 04:46:42 +02:00
Laura Hausmann eb275465fe
[mastodon-client] Fix posting statuses with attachment 2023-07-06 04:46:42 +02:00
Laura Hausmann 74a8c5dc2a
[mastodon-client] megalodon compat changes 2023-07-06 04:46:42 +02:00
Laura Hausmann 03b8ba29c7
[mastodon-client] Add transparent.png asset 2023-07-06 04:46:42 +02:00
Laura Hausmann 80df8bbf15
[mastodon-client] Add endpoints 2023-07-06 04:46:42 +02:00
Laura Hausmann cd439ca773
Fix mastodon api conversion for new note id type 2023-07-06 04:46:21 +02:00
Laura Hausmann 1455d4d5a2
[e2net] add custom logos 2023-07-06 04:38:22 +02:00
Kainoa Kanter 7851f2ee3a
Merge pull request '[PR]: Add signature to remote note lookups' (#10374) from e2net/calckey:fix/authenticated-remote-note-lookups into develop
Reviewed-on: https://codeberg.org/calckey/calckey/pulls/10374
2023-07-06 01:37:33 +02:00
Kainoa Kanter 41ebcb3ec8
Merge pull request 'Fix note url resolver' (#10371) from e2net/calckey:fix/db-resolve-note-url into develop
Reviewed-on: https://codeberg.org/calckey/calckey/pulls/10371
2023-07-06 01:37:28 +02:00
122 changed files with 9385 additions and 285 deletions

3
.gitignore vendored
View file

@ -48,6 +48,9 @@ packages/backend/assets/sounds/None.mp3
!packages/backend/src/db
packages/megalodon/lib
packages/megalodon/.idea
# blender backups
*.blend1
*.blend2

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.9 KiB

View file

@ -44,6 +44,7 @@
"seedrandom": "^3.0.5"
},
"devDependencies": {
"@types/node": "18.11.18",
"@types/gulp": "4.0.10",
"@types/gulp-rename": "2.0.1",
"chalk": "4.1.2",

Binary file not shown.

Before

Width:  |  Height:  |  Size: 473 KiB

After

Width:  |  Height:  |  Size: 6.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 68 B

View file

@ -1,12 +1,16 @@
pub use sea_orm_migration::prelude::*;
mod m20230531_180824_drop_reversi;
mod m20230627_185451_index_note_url;
pub struct Migrator;
#[async_trait::async_trait]
impl MigratorTrait for Migrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
vec![Box::new(m20230531_180824_drop_reversi::Migration)]
vec![
Box::new(m20230531_180824_drop_reversi::Migration),
Box::new(m20230627_185451_index_note_url::Migration),
]
}
}

View file

@ -0,0 +1,38 @@
use sea_orm_migration::prelude::*;
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_index(
Index::create()
.name("IDX_note_url")
.table(Note::Table)
.col(Note::Url)
.if_not_exists()
.to_owned(),
)
.await
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_index(
Index::drop()
.name("IDX_note_url")
.table(Note::Table)
.to_owned(),
)
.await
}
}
/// Learn more at https://docs.rs/sea-query#iden
#[derive(Iden)]
enum Note {
Table,
Url,
}

View file

@ -16,15 +16,15 @@ pub fn convert_id(in_id: String, id_convert_type: IdConvertType) -> napi::Result
use IdConvertType::*;
match id_convert_type {
MastodonId => {
let mut out: i64 = 0;
let mut out: i128 = 0;
for (i, c) in in_id.to_lowercase().chars().rev().enumerate() {
out += num_from_char(c)? as i64 * 36_i64.pow(i as u32);
out += num_from_char(c)? as i128 * 36_i128.pow(i as u32);
}
Ok(out.to_string())
}
CalckeyId => {
let mut input: i64 = match in_id.parse() {
let mut input: i128 = match in_id.parse() {
Ok(s) => s,
Err(_) => {
return Err(Error::new(

View file

@ -28,7 +28,7 @@
"@bull-board/api": "5.2.0",
"@bull-board/koa": "5.2.0",
"@bull-board/ui": "5.2.0",
"@calckey/megalodon": "5.2.0",
"megalodon": "workspace:*",
"@discordapp/twemoji": "14.1.2",
"@elastic/elasticsearch": "7.17.0",
"@koa/cors": "3.4.3",

View file

@ -7,6 +7,8 @@ import DbResolver from "@/remote/activitypub/db-resolver.js";
import { getApId } from "@/remote/activitypub/type.js";
import { shouldBlockInstance } from "@/misc/should-block-instance.js";
import type { IncomingMessage } from "http";
import type { CacheableRemoteUser } from "@/models/entities/user.js";
import type { UserPublickey } from "@/models/entities/user-publickey.js";
export async function hasSignature(req: IncomingMessage): Promise<string> {
const meta = await fetchMeta();
@ -95,3 +97,22 @@ export async function checkFetch(req: IncomingMessage): Promise<number> {
}
return 200;
}
export async function getSignatureUser(req: IncomingMessage): Promise<{
user: CacheableRemoteUser;
key: UserPublickey | null;
} | null> {
const signature = httpSignature.parseRequest(req, { headers: [] });
const keyId = new URL(signature.keyId);
const dbResolver = new DbResolver();
// Retrieve from DB by HTTP-Signature keyId
const authUser = await dbResolver.getAuthUserFromKeyId(signature.keyId);
if (authUser) {
return authUser;
}
// Resolve if failed to retrieve by keyId
keyId.hash = "";
return await dbResolver.getAuthUserFromApId(getApId(keyId.toString()));
}

View file

@ -80,8 +80,15 @@ export default class DbResolver {
id: parsed.id,
});
} else {
return await Notes.findOneBy({
uri: parsed.uri,
return await Notes.findOne({
where: [
{
uri: parsed.uri,
},
{
url: parsed.uri,
},
],
});
}
}

View file

@ -3,6 +3,7 @@ import { getUserKeypair } from "@/misc/keypair-store.js";
import type { User } from "@/models/entities/user.js";
import { getResponse } from "../../misc/fetch.js";
import { createSignedPost, createSignedGet } from "./ap-request.js";
import { apLogger } from "@/remote/activitypub/logger.js";
export default async (user: { id: User["id"] }, url: string, object: any) => {
const body = JSON.stringify(object);
@ -35,6 +36,7 @@ export default async (user: { id: User["id"] }, url: string, object: any) => {
* @param url URL to fetch
*/
export async function signedGet(url: string, user: { id: User["id"] }) {
apLogger.debug(`Running signedGet on url: ${url}`);
const keypair = await getUserKeypair(user.id);
const req = createSignedGet({

View file

@ -23,6 +23,7 @@ import renderCreate from "@/remote/activitypub/renderer/create.js";
import { renderActivity } from "@/remote/activitypub/renderer/index.js";
import renderFollow from "@/remote/activitypub/renderer/follow.js";
import { shouldBlockInstance } from "@/misc/should-block-instance.js";
import { apLogger } from "@/remote/activitypub/logger.js";
export default class Resolver {
private history: Set<string>;
@ -34,6 +35,15 @@ export default class Resolver {
this.recursionLimit = recursionLimit;
}
public setUser(user) {
this.user = user;
}
public reset(): Resolver {
this.history = new Set();
return this;
}
public getHistory(): string[] {
return Array.from(this.history);
}
@ -56,15 +66,20 @@ export default class Resolver {
}
if (typeof value !== "string") {
apLogger.debug("Object to resolve is not a string");
if (typeof value.id !== "undefined") {
const host = extractDbHost(getApId(value));
if (await shouldBlockInstance(host)) {
throw new Error("instance is blocked");
}
}
apLogger.debug("Returning existing object:");
apLogger.debug(JSON.stringify(value, null, 2));
return value;
}
apLogger.debug(`Resolving: ${value}`);
if (value.includes("#")) {
// URLs with fragment parts cannot be resolved correctly because
// the fragment part does not get transmitted over HTTP(S).
@ -102,6 +117,9 @@ export default class Resolver {
this.user = await getInstanceActor();
}
apLogger.debug("Getting object from remote, authenticated as user:");
apLogger.debug(JSON.stringify(this.user, null, 2));
const object = (
this.user
? await signedGet(value, this.user)

View file

@ -20,7 +20,11 @@ import {
import type { ILocalUser, User } from "@/models/entities/user.js";
import { renderLike } from "@/remote/activitypub/renderer/like.js";
import { getUserKeypair } from "@/misc/keypair-store.js";
import { checkFetch, hasSignature } from "@/remote/activitypub/check-fetch.js";
import {
checkFetch,
hasSignature,
getSignatureUser,
} from "@/remote/activitypub/check-fetch.js";
import { getInstanceActor } from "@/services/instance-actor.js";
import { fetchMeta } from "@/misc/fetch-meta.js";
import renderFollow from "@/remote/activitypub/renderer/follow.js";
@ -28,6 +32,7 @@ import Featured from "./activitypub/featured.js";
import Following from "./activitypub/following.js";
import Followers from "./activitypub/followers.js";
import Outbox, { packActivity } from "./activitypub/outbox.js";
import { serverLogger } from "./index.js";
// Init router
const router = new Router();
@ -84,7 +89,7 @@ router.get("/notes/:note", async (ctx, next) => {
const note = await Notes.findOneBy({
id: ctx.params.note,
visibility: In(["public" as const, "home" as const]),
visibility: In(["public" as const, "home" as const, "followers" as const]),
localOnly: false,
});
@ -103,6 +108,37 @@ router.get("/notes/:note", async (ctx, next) => {
return;
}
if (note.visibility === "followers") {
serverLogger.debug(
"Responding to request for follower-only note, validating access...",
);
const remoteUser = await getSignatureUser(ctx.req);
serverLogger.debug("Local note author user:");
serverLogger.debug(JSON.stringify(note, null, 2));
serverLogger.debug("Authenticated remote user:");
serverLogger.debug(JSON.stringify(remoteUser, null, 2));
if (remoteUser == null) {
serverLogger.debug("Rejecting: no user");
ctx.status = 401;
return;
}
const relation = await Users.getRelation(remoteUser.user.id, note.userId);
serverLogger.debug("Relation:");
serverLogger.debug(JSON.stringify(relation, null, 2));
if (!relation.isFollowing || relation.isBlocked) {
serverLogger.debug(
"Rejecting: authenticated user is not following us or was blocked by us",
);
ctx.status = 403;
return;
}
serverLogger.debug("Accepting: access criteria met");
}
ctx.body = renderActivity(await renderNote(note, false));
const meta = await fetchMeta();

View file

@ -10,7 +10,7 @@ import type { Note } from "@/models/entities/note.js";
import type { CacheableLocalUser, User } from "@/models/entities/user.js";
import { isActor, isPost, getApId } from "@/remote/activitypub/type.js";
import type { SchemaType } from "@/misc/schema.js";
import { HOUR } from "@/const.js";
import { MINUTE } from "@/const.js";
import { shouldBlockInstance } from "@/misc/should-block-instance.js";
import { updateQuestion } from "@/remote/activitypub/models/question.js";
import { populatePoll } from "@/models/repositories/note.js";
@ -22,8 +22,8 @@ export const meta = {
requireCredential: true,
limit: {
duration: HOUR,
max: 30,
duration: MINUTE,
max: 10,
},
errors: {
@ -127,6 +127,7 @@ async function fetchAny(
// fetching Object once from remote
const resolver = new Resolver();
resolver.setUser(me);
const object = await resolver.resolve(uri);
// /@user If a URI other than the id is specified,
@ -144,8 +145,12 @@ async function fetchAny(
return await mergePack(
me,
isActor(object) ? await createPerson(getApId(object)) : null,
isPost(object) ? await createNote(getApId(object), undefined, true) : null,
isActor(object)
? await createPerson(getApId(object), resolver.reset())
: null,
isPost(object)
? await createNote(getApId(object), resolver.reset(), true)
: null,
);
}

View file

@ -33,7 +33,7 @@ export const paramDef = {
} as const;
export default define(meta, paramDef, async (ps, user) => {
if (ps.key !== "reactions") return;
if (ps.key !== "reactions" && ps.key !== "defaultNoteVisibility") return;
const query = RegistryItems.createQueryBuilder("item")
.where("item.domain IS NULL")
.andWhere("item.userId = :userId", { userId: user.id })

View file

@ -21,6 +21,7 @@ export const meta = {
message: "No such note.",
code: "NO_SUCH_NOTE",
id: "24fcbfc6-2e37-42b6-8388-c29b3861a08d",
httpStatusCode: 404,
},
},
} as const;

View file

@ -112,7 +112,7 @@ mastoFileRouter.post("/v2/media", upload.single("file"), async (ctx) => {
ctx.status = 401;
return;
}
const data = await client.uploadMedia(multipartData);
const data = await client.uploadMedia(multipartData, ctx.request.body);
ctx.body = convertAttachment(data.data as Entity.Attachment);
} catch (e: any) {
console.error(e);

View file

@ -1,5 +1,5 @@
import Router from "@koa/router";
import megalodon, { MegalodonInterface } from "@calckey/megalodon";
import megalodon, { MegalodonInterface } from "megalodon";
import { apiAuthMastodon } from "./endpoints/auth.js";
import { apiAccountMastodon } from "./endpoints/account.js";
import { apiStatusMastodon } from "./endpoints/status.js";
@ -18,11 +18,7 @@ export function getClient(
const accessTokenArr = authorization?.split(" ") ?? [null];
const accessToken = accessTokenArr[accessTokenArr.length - 1];
const generator = (megalodon as any).default;
const client = generator(
"misskey",
BASE_URL,
accessToken,
) as MegalodonInterface;
const client = generator(BASE_URL, accessToken) as MegalodonInterface;
return client;
}

View file

@ -1,9 +1,11 @@
import { Entity } from "@calckey/megalodon";
import { Entity } from "megalodon";
import { convertId, IdType } from "../index.js";
function simpleConvert(data: any) {
data.id = convertId(data.id, IdType.MastodonId);
return data;
// copy the object to bypass weird pass by reference bugs
const result = Object.assign({}, data);
result.id = convertId(data.id, IdType.MastodonId);
return result;
}
export function convertAccount(account: Entity.Account) {
@ -21,6 +23,9 @@ export function convertFilter(filter: Entity.Filter) {
export function convertList(list: Entity.List) {
return simpleConvert(list);
}
export function convertFeaturedTag(tag: Entity.FeaturedTag) {
return simpleConvert(tag);
}
export function convertNotification(notification: Entity.Notification) {
notification.account = convertAccount(notification.account);

View file

@ -7,6 +7,7 @@ import { argsToBools, convertTimelinesArgsId, limitToInt } from "./timeline.js";
import { convertId, IdType } from "../../index.js";
import {
convertAccount,
convertFeaturedTag,
convertList,
convertRelationship,
convertStatus,
@ -42,12 +43,12 @@ export function apiAccountMastodon(router: Router): void {
acct.url = `${BASE_URL}/@${acct.url}`;
acct.note = acct.note || "";
acct.avatar_static = acct.avatar;
acct.header = acct.header || "https://http.cat/404";
acct.header_static = acct.header || "https://http.cat/404";
acct.header = acct.header || "/static-assets/transparent.png";
acct.header_static = acct.header || "/static-assets/transparent.png";
acct.source = {
note: acct.note,
fields: acct.fields,
privacy: "public",
privacy: await client.getDefaultPostPrivacy(),
sensitive: false,
language: "",
};
@ -164,6 +165,25 @@ export function apiAccountMastodon(router: Router): void {
}
},
);
router.get<{ Params: { id: string } }>(
"/v1/accounts/:id/featured_tags",
async (ctx) => {
const BASE_URL = `${ctx.protocol}://${ctx.hostname}`;
const accessTokens = ctx.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const data = await client.getAccountFeaturedTags(
convertId(ctx.params.id, IdType.CalckeyId),
);
ctx.body = data.data.map((tag) => convertFeaturedTag(tag));
} catch (e: any) {
console.error(e);
console.error(e.response.data);
ctx.status = 401;
ctx.body = e.response.data;
}
},
);
router.get<{ Params: { id: string } }>(
"/v1/accounts/:id/followers",
async (ctx) => {
@ -342,6 +362,34 @@ export function apiAccountMastodon(router: Router): void {
}
},
);
router.get("/v1/featured_tags", async (ctx) => {
const BASE_URL = `${ctx.protocol}://${ctx.hostname}`;
const accessTokens = ctx.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const data = await client.getFeaturedTags();
ctx.body = data.data.map((tag) => convertFeaturedTag(tag));
} catch (e: any) {
console.error(e);
console.error(e.response.data);
ctx.status = 401;
ctx.body = e.response.data;
}
});
router.get("/v1/followed_tags", async (ctx) => {
const BASE_URL = `${ctx.protocol}://${ctx.hostname}`;
const accessTokens = ctx.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const data = await client.getFollowedTags();
ctx.body = data.data;
} catch (e: any) {
console.error(e);
console.error(e.response.data);
ctx.status = 401;
ctx.body = e.response.data;
}
});
router.get("/v1/bookmarks", async (ctx) => {
const BASE_URL = `${ctx.protocol}://${ctx.hostname}`;
const accessTokens = ctx.headers.authorization;

View file

@ -1,4 +1,4 @@
import megalodon, { MegalodonInterface } from "@calckey/megalodon";
import megalodon, { MegalodonInterface } from "megalodon";
import Router from "@koa/router";
import { koaBody } from "koa-body";
import { getClient } from "../ApiMastodonCompatibleService.js";

View file

@ -1,4 +1,4 @@
import megalodon, { MegalodonInterface } from "@calckey/megalodon";
import megalodon, { MegalodonInterface } from "megalodon";
import Router from "@koa/router";
import { getClient } from "../ApiMastodonCompatibleService.js";
import { IdType, convertId } from "../../index.js";

View file

@ -1,4 +1,5 @@
import { Entity } from "@calckey/megalodon";
import { Entity } from "megalodon";
import config from "@/config/index.js";
import { fetchMeta } from "@/misc/fetch-meta.js";
import { Users, Notes } from "@/models/index.js";
import { IsNull, MoreThan } from "typeorm";
@ -17,14 +18,14 @@ export async function getInstance(response: Entity.Instance) {
response.description ||
"This is a vanilla Calckey Instance. It doesnt seem to have a description. BTW you are using the Mastodon api to access this server :)",
email: response.email || "",
version: "3.0.0 compatible (3.5+ Calckey)", //I hope this version string is correct, we will need to test it.
version: `3.0.0 (compatible; Calckey ${config.version})`,
urls: response.urls,
stats: {
user_count: await totalUsers,
status_count: await totalStatuses,
domain_count: response.stats.domain_count,
},
thumbnail: response.thumbnail || "https://http.cat/404",
thumbnail: response.thumbnail || "/static-assets/transparent.png",
languages: meta.langs,
registrations: !meta.disableRegistration || response.registrations,
approval_required: !response.registrations,
@ -96,8 +97,8 @@ export async function getInstance(response: Entity.Instance) {
url: `${response.uri}/`,
avatar: `${response.uri}/static-assets/badges/info.png`,
avatar_static: `${response.uri}/static-assets/badges/info.png`,
header: "https://http.cat/404",
header_static: "https://http.cat/404",
header: "/static-assets/transparent.png",
header_static: "/static-assets/transparent.png",
followers_count: -1,
following_count: 0,
statuses_count: 0,

View file

@ -1,9 +1,9 @@
import megalodon, { MegalodonInterface } from "@calckey/megalodon";
import megalodon, { MegalodonInterface } from "megalodon";
import Router from "@koa/router";
import { koaBody } from "koa-body";
import { convertId, IdType } from "../../index.js";
import { getClient } from "../ApiMastodonCompatibleService.js";
import { convertTimelinesArgsId, toTextWithReaction } from "./timeline.js";
import { convertTimelinesArgsId } from "./timeline.js";
import { convertNotification } from "../converters.js";
function toLimitToInt(q: any) {
if (q.limit) if (typeof q.limit === "string") q.limit = parseInt(q.limit, 10);
@ -25,10 +25,6 @@ export function apiNotificationsMastodon(router: Router): void {
n = convertNotification(n);
if (n.type !== "follow" && n.type !== "follow_request") {
if (n.type === "reaction") n.type = "favourite";
n.status = toTextWithReaction(
n.status ? [n.status] : [],
ctx.hostname,
)[0];
return n;
} else {
return n;
@ -52,11 +48,13 @@ export function apiNotificationsMastodon(router: Router): void {
convertId(ctx.params.id, IdType.CalckeyId),
);
const data = convertNotification(dataRaw.data);
if (data.type !== "follow" && data.type !== "follow_request") {
if (data.type === "reaction") data.type = "favourite";
ctx.body = toTextWithReaction([data as any], ctx.request.hostname)[0];
} else {
ctx.body = data;
ctx.body = data;
if (
data.type !== "follow" &&
data.type !== "follow_request" &&
data.type === "reaction"
) {
data.type = "favourite";
}
} catch (e: any) {
console.error(e);

View file

@ -1,8 +1,8 @@
import megalodon, { MegalodonInterface } from "@calckey/megalodon";
import megalodon, { MegalodonInterface } from "megalodon";
import Router from "@koa/router";
import { getClient } from "../ApiMastodonCompatibleService.js";
import axios from "axios";
import { Converter } from "@calckey/megalodon";
import { Converter } from "megalodon";
import { convertTimelinesArgsId, limitToInt } from "./timeline.js";
import { convertAccount, convertStatus } from "../converters.js";
@ -30,21 +30,26 @@ export function apiSearchMastodon(router: Router): void {
try {
const query: any = convertTimelinesArgsId(limitToInt(ctx.query));
const type = query.type;
if (type) {
const data = await client.search(query.q, type, query);
ctx.body = data.data.accounts.map((account) => convertAccount(account));
} else {
const acct = await client.search(query.q, "accounts", query);
const stat = await client.search(query.q, "statuses", query);
const tags = await client.search(query.q, "hashtags", query);
ctx.body = {
accounts: acct.data.accounts.map((account) =>
convertAccount(account),
),
statuses: stat.data.statuses.map((status) => convertStatus(status)),
hashtags: tags.data.hashtags,
};
}
const acct =
!type || type === "accounts"
? await client.search(query.q, "accounts", query)
: null;
const stat =
!type || type === "statuses"
? await client.search(query.q, "statuses", query)
: null;
const tags =
!type || type === "hashtags"
? await client.search(query.q, "hashtags", query)
: null;
ctx.body = {
accounts:
acct?.data?.accounts.map((account) => convertAccount(account)) ?? [],
statuses:
stat?.data?.statuses.map((status) => convertStatus(status)) ?? [],
hashtags: tags?.data?.hashtags ?? [],
};
} catch (e: any) {
console.error(e);
ctx.status = 401;
@ -103,7 +108,7 @@ async function getHighlight(
i: accessToken,
});
const data: MisskeyEntity.Note[] = api.data;
return data.map((note) => Converter.note(note, domain));
return data.map((note) => new Converter(BASE_URL).note(note, domain));
} catch (e: any) {
console.log(e);
console.log(e.response.data);
@ -131,7 +136,7 @@ async function getFeaturedUser(
return data.map((u) => {
return {
source: "past_interactions",
account: Converter.userDetail(u, host),
account: new Converter(BASE_URL).userDetail(u, host),
};
});
} catch (e: any) {

View file

@ -59,9 +59,33 @@ export function apiStatusMastodon(router: Router): void {
}
if (!body.media_ids) body.media_ids = undefined;
if (body.media_ids && !body.media_ids.length) body.media_ids = undefined;
if (body.media_ids) {
body.media_ids = (body.media_ids as string[]).map((p) =>
convertId(p, IdType.CalckeyId),
);
}
const { sensitive } = body;
body.sensitive =
typeof sensitive === "string" ? sensitive === "true" : sensitive;
if (body.poll) {
if (
body.poll.expires_in != null &&
typeof body.poll.expires_in === "string"
)
body.poll.expires_in = parseInt(body.poll.expires_in);
if (
body.poll.multiple != null &&
typeof body.poll.multiple === "string"
)
body.poll.multiple = body.poll.multiple == "true";
if (
body.poll.hide_totals != null &&
typeof body.poll.hide_totals === "string"
)
body.poll.hide_totals = body.poll.hide_totals == "true";
}
const data = await client.postStatus(text, body);
ctx.body = convertStatus(data.data);
} catch (e: any) {
@ -81,7 +105,7 @@ export function apiStatusMastodon(router: Router): void {
ctx.body = convertStatus(data.data);
} catch (e: any) {
console.error(e);
ctx.status = 401;
ctx.status = ctx.status == 404 ? 404 : 401;
ctx.body = e.response.data;
}
});
@ -118,27 +142,7 @@ export function apiStatusMastodon(router: Router): void {
id,
convertTimelinesArgsId(limitToInt(ctx.query as any)),
);
const status = await client.getStatus(id);
let reqInstance = axios.create({
headers: {
Authorization: ctx.headers.authorization,
},
});
const reactionsAxios = await reqInstance.get(
`${BASE_URL}/api/notes/reactions?noteId=${id}`,
);
const reactions: IReaction[] = reactionsAxios.data;
const text = reactions
.map((r) => `${r.type.replace("@.", "")} ${r.user.username}`)
.join("<br />");
data.data.descendants.unshift(
statusModel(
status.data.id,
status.data.account.id,
status.data.emojis,
text,
),
);
data.data.ancestors = data.data.ancestors.map((status) =>
convertStatus(status),
);
@ -153,6 +157,24 @@ export function apiStatusMastodon(router: Router): void {
}
},
);
router.get<{ Params: { id: string } }>(
"/v1/statuses/:id/history",
async (ctx) => {
const BASE_URL = `${ctx.protocol}://${ctx.hostname}`;
const accessTokens = ctx.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const data = await client.getStatusHistory(
convertId(ctx.params.id, IdType.CalckeyId),
);
ctx.body = data.data.map((account) => convertAccount(account));
} catch (e: any) {
console.error(e);
ctx.status = 401;
ctx.body = e.response.data;
}
},
);
router.get<{ Params: { id: string } }>(
"/v1/statuses/:id/reblogged_by",
async (ctx) => {
@ -174,7 +196,19 @@ export function apiStatusMastodon(router: Router): void {
router.get<{ Params: { id: string } }>(
"/v1/statuses/:id/favourited_by",
async (ctx) => {
ctx.body = [];
const BASE_URL = `${ctx.protocol}://${ctx.hostname}`;
const accessTokens = ctx.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const data = await client.getStatusFavouritedBy(
convertId(ctx.params.id, IdType.CalckeyId),
);
ctx.body = data.data.map((account) => convertAccount(account));
} catch (e: any) {
console.error(e);
ctx.status = 401;
ctx.body = e.response.data;
}
},
);
router.post<{ Params: { id: string } }>(
@ -421,65 +455,3 @@ async function getFirstReaction(
return react;
}
}
export function statusModel(
id: string | null,
acctId: string | null,
emojis: MastodonEntity.Emoji[],
content: string,
) {
const now = new Date().toISOString();
return {
id: "9atm5frjhb",
uri: "https://http.cat/404", // ""
url: "https://http.cat/404", // "",
account: {
id: "9arzuvv0sw",
username: "Reactions",
acct: "Reactions",
display_name: "Reactions to this post",
locked: false,
created_at: now,
followers_count: 0,
following_count: 0,
statuses_count: 0,
note: "",
url: "https://http.cat/404",
avatar: "/static-assets/badges/info.png",
avatar_static: "/static-assets/badges/info.png",
header: "https://http.cat/404", // ""
header_static: "https://http.cat/404", // ""
emojis: [],
fields: [],
moved: null,
bot: false,
},
in_reply_to_id: id,
in_reply_to_account_id: acctId,
reblog: null,
content: `<p>${content}</p>`,
plain_content: null,
created_at: now,
emojis: emojis,
replies_count: 0,
reblogs_count: 0,
favourites_count: 0,
favourited: false,
reblogged: false,
muted: false,
sensitive: false,
spoiler_text: "",
visibility: "public" as const,
media_attachments: [],
mentions: [],
tags: [],
card: null,
poll: null,
application: null,
language: null,
pinned: false,
emoji_reactions: [],
bookmarked: false,
quote: null,
};
}

View file

@ -1,8 +1,5 @@
import Router from "@koa/router";
import megalodon, { Entity, MegalodonInterface } from "@calckey/megalodon";
import { getClient } from "../ApiMastodonCompatibleService.js";
import { statusModel } from "./status.js";
import Autolinker from "autolinker";
import { ParsedUrlQuery } from "querystring";
import { convertAccount, convertList, convertStatus } from "../converters.js";
import { convertId, IdType } from "../../index.js";
@ -41,66 +38,6 @@ export function convertTimelinesArgsId(q: ParsedUrlQuery) {
return q;
}
export function toTextWithReaction(status: Entity.Status[], host: string) {
return status.map((t) => {
if (!t) return statusModel(null, null, [], "no content");
t.quote = null as any;
if (!t.emoji_reactions) return t;
if (t.reblog) t.reblog = toTextWithReaction([t.reblog], host)[0];
const reactions = t.emoji_reactions.map((r) => {
const emojiNotation = r.url ? `:${r.name.replace("@.", "")}:` : r.name;
return `${emojiNotation} (${r.count}${r.me ? `* ` : ""})`;
});
const reaction = t.emoji_reactions as Entity.Reaction[];
const emoji = t.emojis || [];
for (const r of reaction) {
if (!r.url) continue;
emoji.push({
shortcode: r.name,
url: r.url,
static_url: r.url,
visible_in_picker: true,
category: "",
});
}
const isMe = reaction.findIndex((r) => r.me) > -1;
const total = reaction.reduce((sum, reaction) => sum + reaction.count, 0);
t.favourited = isMe;
t.favourites_count = total;
t.emojis = emoji;
t.content = `<p>${autoLinker(t.content, host)}</p><p>${reactions.join(
", ",
)}</p>`;
return t;
});
}
export function autoLinker(input: string, host: string) {
return Autolinker.link(input, {
hashtag: "twitter",
mention: "twitter",
email: false,
stripPrefix: false,
replaceFn: function (match) {
switch (match.type) {
case "url":
return true;
case "mention":
console.log("Mention: ", match.getMention());
console.log("Mention Service Name: ", match.getServiceName());
return `<a href="https://${host}/@${encodeURIComponent(
match.getMention(),
)}" target="_blank">@${match.getMention()}</a>`;
case "hashtag":
console.log("Hashtag: ", match.getHashtag());
return `<a href="https://${host}/tags/${encodeURIComponent(
match.getHashtag(),
)}" target="_blank">#${match.getHashtag()}</a>`;
}
return false;
},
});
}
export function apiTimelineMastodon(router: Router): void {
router.get("/v1/timelines/public", async (ctx, reply) => {
const BASE_URL = `${ctx.protocol}://${ctx.hostname}`;
@ -108,15 +45,15 @@ export function apiTimelineMastodon(router: Router): void {
const client = getClient(BASE_URL, accessTokens);
try {
const query: any = ctx.query;
const data = query.local
? await client.getLocalTimeline(
convertTimelinesArgsId(argsToBools(limitToInt(query))),
)
: await client.getPublicTimeline(
convertTimelinesArgsId(argsToBools(limitToInt(query))),
);
let resp = data.data.map((status) => convertStatus(status));
ctx.body = toTextWithReaction(resp, ctx.hostname);
const data =
query.local === "true"
? await client.getLocalTimeline(
convertTimelinesArgsId(argsToBools(limitToInt(query))),
)
: await client.getPublicTimeline(
convertTimelinesArgsId(argsToBools(limitToInt(query))),
);
ctx.body = data.data.map((status) => convertStatus(status));
} catch (e: any) {
console.error(e);
console.error(e.response.data);
@ -135,8 +72,7 @@ export function apiTimelineMastodon(router: Router): void {
ctx.params.hashtag,
convertTimelinesArgsId(argsToBools(limitToInt(ctx.query))),
);
let resp = data.data.map((status) => convertStatus(status));
ctx.body = toTextWithReaction(resp, ctx.hostname);
ctx.body = data.data.map((status) => convertStatus(status));
} catch (e: any) {
console.error(e);
console.error(e.response.data);
@ -153,8 +89,7 @@ export function apiTimelineMastodon(router: Router): void {
const data = await client.getHomeTimeline(
convertTimelinesArgsId(limitToInt(ctx.query)),
);
let resp = data.data.map((status) => convertStatus(status));
ctx.body = toTextWithReaction(resp, ctx.hostname);
ctx.body = data.data.map((status) => convertStatus(status));
} catch (e: any) {
console.error(e);
console.error(e.response.data);
@ -173,8 +108,7 @@ export function apiTimelineMastodon(router: Router): void {
convertId(ctx.params.listId, IdType.CalckeyId),
convertTimelinesArgsId(limitToInt(ctx.query)),
);
let resp = data.data.map((status) => convertStatus(status));
ctx.body = toTextWithReaction(resp, ctx.hostname);
ctx.body = data.data.map((status) => convertStatus(status));
} catch (e: any) {
console.error(e);
console.error(e.response.data);

View file

@ -25,9 +25,8 @@ import { readNotification } from "../common/read-notification.js";
import channels from "./channels/index.js";
import type Channel from "./channel.js";
import type { StreamEventEmitter, StreamMessages } from "./types.js";
import { Converter } from "@calckey/megalodon";
import { Converter } from "megalodon";
import { getClient } from "../mastodon/ApiMastodonCompatibleService.js";
import { toTextWithReaction } from "../mastodon/endpoints/timeline.js";
/**
* Main stream connection
@ -400,12 +399,7 @@ export default class Connection {
JSON.stringify({
stream: [payload.id],
event: "update",
payload: JSON.stringify(
toTextWithReaction(
[Converter.note(payload.body, this.host)],
this.host,
)[0],
),
payload: JSON.stringify(Converter.note(payload.body, this.host)),
}),
);
this.onSubscribeNote({
@ -415,7 +409,7 @@ export default class Connection {
// reaction
const client = getClient(this.host, this.accessToken);
client.getStatus(payload.id).then((data) => {
const newPost = toTextWithReaction([data.data], this.host);
const newPost = [data.data];
const targetPost = newPost[0];
for (const stream of this.currentSubscribe) {
this.wsConnection.send(
@ -442,10 +436,6 @@ export default class Connection {
if (payload.id === "user") {
const body = Converter.notification(payload.body, this.host);
if (body.type === "reaction") body.type = "favourite";
body.status = toTextWithReaction(
body.status ? [body.status] : [],
"",
)[0];
this.wsConnection.send(
JSON.stringify({
stream: ["user"],

View file

@ -21,7 +21,7 @@ import { createTemp } from "@/misc/create-temp.js";
import { publishMainStream } from "@/services/stream.js";
import * as Acct from "@/misc/acct.js";
import { envOption } from "@/env.js";
import megalodon, { MegalodonInterface } from "@calckey/megalodon";
import megalodon, { MegalodonInterface } from "megalodon";
import activityPub from "./activitypub.js";
import nodeinfo from "./nodeinfo.js";
import wellKnown from "./well-known.js";
@ -160,7 +160,7 @@ mastoRouter.post("/oauth/token", async (ctx) => {
let client_id: any = body.client_id;
const BASE_URL = `${ctx.request.protocol}://${ctx.request.hostname}`;
const generator = (megalodon as any).default;
const client = generator("misskey", BASE_URL, null) as MegalodonInterface;
const client = generator(BASE_URL, null) as MegalodonInterface;
let m = null;
let token = null;
if (body.code) {

View file

@ -0,0 +1,81 @@
{
"name": "megalodon",
"private": true,
"main": "./lib/src/index.js",
"typings": "./lib/src/index.d.ts",
"scripts": {
"build": "tsc -p ./",
"lint": "eslint --ext .js,.ts src",
"doc": "typedoc --out ../docs ./src",
"test": "NODE_ENV=test jest -u --maxWorkers=3"
},
"jest": {
"moduleFileExtensions": [
"ts",
"js"
],
"moduleNameMapper": {
"^@/(.+)": "<rootDir>/src/$1",
"^~/(.+)": "<rootDir>/$1"
},
"testMatch": [
"**/test/**/*.spec.ts"
],
"preset": "ts-jest/presets/default",
"transform": {
"^.+\\.(ts|tsx)$": "ts-jest"
},
"globals": {
"ts-jest": {
"tsconfig": "tsconfig.json"
}
},
"testEnvironment": "node"
},
"dependencies": {
"@types/oauth": "^0.9.0",
"@types/ws": "^8.5.4",
"axios": "1.2.2",
"dayjs": "^1.11.7",
"form-data": "^4.0.0",
"https-proxy-agent": "^5.0.1",
"oauth": "^0.10.0",
"object-assign-deep": "^0.4.0",
"parse-link-header": "^2.0.0",
"socks-proxy-agent": "^7.0.0",
"typescript": "4.9.4",
"uuid": "^9.0.0",
"ws": "8.12.0",
"async-lock": "1.4.0"
},
"devDependencies": {
"@types/core-js": "^2.5.0",
"@types/form-data": "^2.5.0",
"@types/jest": "^29.4.0",
"@types/object-assign-deep": "^0.4.0",
"@types/parse-link-header": "^2.0.0",
"@types/uuid": "^9.0.0",
"@types/node": "18.11.18",
"@typescript-eslint/eslint-plugin": "^5.49.0",
"@typescript-eslint/parser": "^5.49.0",
"@types/async-lock": "1.4.0",
"eslint": "^8.32.0",
"eslint-config-prettier": "^8.6.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",
"prettier": "^2.8.3",
"ts-jest": "^29.0.5",
"typedoc": "^0.23.24"
},
"directories": {
"lib": "lib",
"test": "test"
}
}

1
packages/megalodon/src/axios.d.ts vendored Normal file
View file

@ -0,0 +1 @@
declare module 'axios/lib/adapters/http'

View file

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

View file

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

View file

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

View file

@ -0,0 +1,27 @@
/// <reference path="emoji.ts" />
/// <reference path="source.ts" />
/// <reference path="field.ts" />
namespace Entity {
export type Account = {
id: string
username: string
acct: string
display_name: string
locked: boolean
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 | null
source?: Source
}
}

View file

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

View file

@ -0,0 +1,34 @@
/// <reference path="tag.ts" />
/// <reference path="emoji.ts" />
/// <reference path="reaction.ts" />
namespace Entity {
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
mentions: Array<AnnouncementAccount>
statuses: Array<AnnouncementStatus>
tags: Array<Tag>
emojis: Array<Emoji>
reactions: Array<Reaction>
}
export type AnnouncementAccount = {
id: string
username: string
url: string
acct: string
}
export type AnnouncementStatus = {
id: string
url: string
}
}

View file

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

View file

@ -0,0 +1,14 @@
/// <reference path="attachment.ts" />
namespace Entity {
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 Entity {
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,16 @@
namespace Entity {
export type Card = {
url: string
title: string
description: string
type: 'link' | 'photo' | 'video' | 'rich'
image?: string
author_name?: string
author_url?: string
provider_name?: string
provider_url?: string
html?: string
width?: number
height?: number
}
}

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -0,0 +1,12 @@
namespace Entity {
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,7 @@
namespace Entity {
export type History = {
day: string
uses: number
accounts: number
}
}

View file

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

View file

@ -0,0 +1,41 @@
/// <reference path="account.ts" />
/// <reference path="urls.ts" />
/// <reference path="stats.ts" />
namespace Entity {
export type Instance = {
uri: string
title: string
description: string
email: string
version: string
thumbnail: string | null
urls: URLs
stats: Stats
languages: Array<string>
contact_account: Account | null
max_toot_chars?: number
registrations?: boolean
configuration?: {
statuses: {
max_characters: number
max_media_attachments: number
characters_reserved_per_url: number
}
media_attachments: {
supported_mime_types: Array<string>
image_size_limit: number
image_matrix_limit: number
video_size_limit: number
video_frame_limit: number
video_matrix_limit: number
}
polls: {
max_options: number
max_characters_per_option: number
min_expiration: number
max_expiration: number
}
}
}
}

View file

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

View file

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

View file

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

View file

@ -0,0 +1,15 @@
/// <reference path="account.ts" />
/// <reference path="status.ts" />
namespace Entity {
export type Notification = {
account: Account
created_at: string
id: string
status?: Status
emoji?: string
type: NotificationType
}
export type NotificationType = string
}

View file

@ -0,0 +1,14 @@
/// <reference path="poll_option.ts" />
namespace Entity {
export type Poll = {
id: string
expires_at: string | null
expired: boolean
multiple: boolean
votes_count: number
options: Array<PollOption>
voted: boolean
own_votes: Array<number>
}
}

View file

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

View file

@ -0,0 +1,9 @@
namespace Entity {
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 Entity {
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,11 @@
/// <reference path="account.ts" />
namespace Entity {
export type Reaction = {
count: number
me: boolean
name: string
url?: string
accounts?: Array<Account>
}
}

View file

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

View file

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

View file

@ -0,0 +1,11 @@
/// <reference path="account.ts" />
/// <reference path="status.ts" />
/// <reference path="tag.ts" />
namespace Entity {
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 Entity {
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 Entity {
export type Source = {
privacy: string | null
sensitive: boolean | null
language: string | null
note: string
fields: Array<Field>
}
}

View file

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

View file

@ -0,0 +1,45 @@
/// <reference path="account.ts" />
/// <reference path="application.ts" />
/// <reference path="mention.ts" />
/// <reference path="tag.ts" />
/// <reference path="attachment.ts" />
/// <reference path="emoji.ts" />
/// <reference path="card.ts" />
/// <reference path="poll.ts" />
/// <reference path="reaction.ts" />
namespace Entity {
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
plain_content: string | null
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<Tag>
card: Card | null
poll: Poll | null
application: Application | null
language: string | null
pinned: boolean | null
emoji_reactions: Array<Reaction>
quote: Status | null
bookmarked: boolean
}
}

View file

@ -0,0 +1,23 @@
/// <reference path="account.ts" />
/// <reference path="application.ts" />
/// <reference path="mention.ts" />
/// <reference path="tag.ts" />
/// <reference path="attachment.ts" />
/// <reference path="emoji.ts" />
/// <reference path="card.ts" />
/// <reference path="poll.ts" />
/// <reference path="reaction.ts" />
namespace Entity {
export type StatusEdit = {
account: Account
content: string
plain_content: string | null
created_at: string
emojis: Emoji[]
sensitive: boolean
spoiler_text: string
media_attachments: Array<Attachment>
poll: Poll | null
}
}

View file

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

View file

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

View file

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

View file

@ -0,0 +1,5 @@
namespace Entity {
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/announcement.ts" />
/// <reference path="./entities/application.ts" />
/// <reference path="./entities/async_attachment.ts" />
/// <reference path="./entities/attachment.ts" />
/// <reference path="./entities/card.ts" />
/// <reference path="./entities/context.ts" />
/// <reference path="./entities/conversation.ts" />
/// <reference path="./entities/emoji.ts" />
/// <reference path="./entities/featured_tag.ts" />
/// <reference path="./entities/field.ts" />
/// <reference path="./entities/filter.ts" />
/// <reference path="./entities/history.ts" />
/// <reference path="./entities/identity_proof.ts" />
/// <reference path="./entities/instance.ts" />
/// <reference path="./entities/list.ts" />
/// <reference path="./entities/marker.ts" />
/// <reference path="./entities/mention.ts" />
/// <reference path="./entities/notification.ts" />
/// <reference path="./entities/poll.ts" />
/// <reference path="./entities/poll_option.ts" />
/// <reference path="./entities/preferences.ts" />
/// <reference path="./entities/push_subscription.ts" />
/// <reference path="./entities/reaction.ts" />
/// <reference path="./entities/relationship.ts" />
/// <reference path="./entities/report.ts" />
/// <reference path="./entities/results.ts" />
/// <reference path="./entities/scheduled_status.ts" />
/// <reference path="./entities/source.ts" />
/// <reference path="./entities/stats.ts" />
/// <reference path="./entities/status.ts" />
/// <reference path="./entities/status_params.ts" />
/// <reference path="./entities/tag.ts" />
/// <reference path="./entities/token.ts" />
/// <reference path="./entities/urls.ts" />
export default Entity

View file

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

View file

@ -0,0 +1,28 @@
import Response from './response'
import OAuth from './oauth'
import { isCancel, RequestCanceledError } from './cancel'
import { ProxyConfig } from './proxy_config'
import generator, { detector, MegalodonInterface, WebSocketInterface } from './megalodon'
import Misskey from './misskey'
import Entity from './entity'
import NotificationType from './notification'
import FilterContext from './filter_context'
import Converter from './converter'
export {
Response,
OAuth,
RequestCanceledError,
isCancel,
ProxyConfig,
detector,
MegalodonInterface,
WebSocketInterface,
NotificationType,
FilterContext,
Misskey,
Entity,
Converter
}
export default generator

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,645 @@
import axios, { AxiosResponse, AxiosRequestConfig } from 'axios'
import dayjs from 'dayjs'
import FormData from 'form-data'
import { DEFAULT_UA } from '../default'
import proxyAgent, { ProxyConfig } from '../proxy_config'
import Response from '../response'
import MisskeyEntity from './entity'
import MegalodonEntity from '../entity'
import WebSocket from './web_socket'
import MisskeyNotificationType from './notification'
import NotificationType from '../notification'
namespace MisskeyAPI {
export namespace Entity {
export type App = MisskeyEntity.App
export type Announcement = MisskeyEntity.Announcement
export type Blocking = MisskeyEntity.Blocking
export type Choice = MisskeyEntity.Choice
export type CreatedNote = MisskeyEntity.CreatedNote
export type Emoji = MisskeyEntity.Emoji
export type Favorite = MisskeyEntity.Favorite
export type Field = MisskeyEntity.Field
export type File = MisskeyEntity.File
export type Follower = MisskeyEntity.Follower
export type Following = MisskeyEntity.Following
export type FollowRequest = MisskeyEntity.FollowRequest
export type Hashtag = MisskeyEntity.Hashtag
export type List = MisskeyEntity.List
export type Meta = MisskeyEntity.Meta
export type Mute = MisskeyEntity.Mute
export type Note = MisskeyEntity.Note
export type Notification = MisskeyEntity.Notification
export type Poll = MisskeyEntity.Poll
export type Reaction = MisskeyEntity.Reaction
export type Relation = MisskeyEntity.Relation
export type User = MisskeyEntity.User
export type UserDetail = MisskeyEntity.UserDetail
export type UserDetailMe = MisskeyEntity.UserDetailMe
export type GetAll = MisskeyEntity.GetAll
export type UserKey = MisskeyEntity.UserKey
export type Session = MisskeyEntity.Session
export type Stats = MisskeyEntity.Stats
export type State = MisskeyEntity.State
export type APIEmoji = { emojis: Emoji[] }
}
export class Converter {
private baseUrl: string
private instanceHost: string
private plcUrl: string
private modelOfAcct = {
id: "1",
username: 'none',
acct: 'none',
display_name: 'none',
locked: true,
bot: true,
discoverable: false,
group: false,
created_at: '1971-01-01T00:00:00.000Z',
note: '',
url: 'plc',
avatar: 'plc',
avatar_static: 'plc',
header: 'plc',
header_static: 'plc',
followers_count: -1,
following_count: 0,
statuses_count: 0,
last_status_at: '1971-01-01T00:00:00.000Z',
noindex: true,
emojis: [],
fields: [],
moved: null
}
constructor(baseUrl: string) {
this.baseUrl = baseUrl;
this.instanceHost = baseUrl.substring(baseUrl.indexOf('//') + 2);
this.plcUrl = `${baseUrl}/static-assets/transparent.png`;
this.modelOfAcct.url = this.plcUrl;
this.modelOfAcct.avatar = this.plcUrl;
this.modelOfAcct.avatar_static = this.plcUrl;
this.modelOfAcct.header = this.plcUrl;
this.modelOfAcct.header_static = this.plcUrl;
}
// FIXME: Properly render MFM instead of just escaping HTML characters.
escapeMFM = (text: string): string => text
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#39;')
.replace(/`/g, '&#x60;')
.replace(/\r?\n/g, '<br>');
emoji = (e: Entity.Emoji): MegalodonEntity.Emoji => {
return {
shortcode: e.name,
static_url: e.url,
url: e.url,
visible_in_picker: true,
category: e.category
}
}
field = (f: Entity.Field): MegalodonEntity.Field => ({
name: f.name,
value: this.escapeMFM(f.value),
verified_at: null
})
user = (u: Entity.User): MegalodonEntity.Account => {
let acct = u.username
let acctUrl = `https://${u.host || this.instanceHost}/@${u.username}`
if (u.host) {
acct = `${u.username}@${u.host}`
acctUrl = `https://${u.host}/@${u.username}`
}
return {
id: u.id,
username: u.username,
acct: acct,
display_name: u.name || u.username,
locked: false,
created_at: new Date().toISOString(),
followers_count: 0,
following_count: 0,
statuses_count: 0,
note: '',
url: acctUrl,
avatar: u.avatarUrl,
avatar_static: u.avatarUrl,
header: this.plcUrl,
header_static: this.plcUrl,
emojis: u.emojis.map(e => this.emoji(e)),
moved: null,
fields: [],
bot: false
}
}
userDetail = (u: Entity.UserDetail, host: string): MegalodonEntity.Account => {
let acct = u.username
host = host.replace('https://', '')
let acctUrl = `https://${host || u.host || this.instanceHost}/@${u.username}`
if (u.host) {
acct = `${u.username}@${u.host}`
acctUrl = `https://${u.host}/@${u.username}`
}
return {
id: u.id,
username: u.username,
acct: acct,
display_name: u.name || u.username,
locked: u.isLocked,
created_at: u.createdAt,
followers_count: u.followersCount,
following_count: u.followingCount,
statuses_count: u.notesCount,
note: u.description?.replace(/\n|\\n/g, '<br>') ?? '',
url: acctUrl,
avatar: u.avatarUrl,
avatar_static: u.avatarUrl,
header: u.bannerUrl ?? this.plcUrl,
header_static: u.bannerUrl ?? this.plcUrl,
emojis: u.emojis.map(e => this.emoji(e)),
moved: null,
fields: u.fields.map(f => this.field(f)),
bot: u.isBot,
}
}
userPreferences = (u: MisskeyAPI.Entity.UserDetailMe, v: 'public' | 'unlisted' | 'private' | 'direct'): MegalodonEntity.Preferences => {
return {
"reading:expand:media": "default",
"reading:expand:spoilers": false,
"posting:default:language": u.lang,
"posting:default:sensitive": u.alwaysMarkNsfw,
"posting:default:visibility": v
}
}
visibility = (v: 'public' | 'home' | 'followers' | 'specified'): 'public' | 'unlisted' | 'private' | 'direct' => {
switch (v) {
case 'public':
return v
case 'home':
return 'unlisted'
case 'followers':
return 'private'
case 'specified':
return 'direct'
}
}
encodeVisibility = (v: 'public' | 'unlisted' | 'private' | 'direct'): 'public' | 'home' | 'followers' | 'specified' => {
switch (v) {
case 'public':
return v
case 'unlisted':
return 'home'
case 'private':
return 'followers'
case 'direct':
return 'specified'
}
}
fileType = (s: string): 'unknown' | 'image' | 'gifv' | 'video' | 'audio' => {
if (s === 'image/gif') {
return 'gifv'
}
if (s.includes('image')) {
return 'image'
}
if (s.includes('video')) {
return 'video'
}
if (s.includes('audio')) {
return 'audio'
}
return 'unknown'
}
file = (f: Entity.File): MegalodonEntity.Attachment => {
return {
id: f.id,
type: this.fileType(f.type),
url: f.url,
remote_url: f.url,
preview_url: f.thumbnailUrl,
text_url: f.url,
meta: {
width: f.properties.width,
height: f.properties.height
},
description: f.comment,
blurhash: f.blurhash
}
}
follower = (f: Entity.Follower): MegalodonEntity.Account => {
return this.user(f.follower)
}
following = (f: Entity.Following): MegalodonEntity.Account => {
return this.user(f.followee)
}
relation = (r: Entity.Relation): MegalodonEntity.Relationship => {
return {
id: r.id,
following: r.isFollowing,
followed_by: r.isFollowed,
blocking: r.isBlocking,
blocked_by: r.isBlocked,
muting: r.isMuted,
muting_notifications: false,
requested: r.hasPendingFollowRequestFromYou,
domain_blocking: false,
showing_reblogs: true,
endorsed: false,
notifying: false
}
}
choice = (c: Entity.Choice): MegalodonEntity.PollOption => {
return {
title: c.text,
votes_count: c.votes
}
}
poll = (p: Entity.Poll, id: string): MegalodonEntity.Poll => {
const now = dayjs()
const expire = dayjs(p.expiresAt)
const count = p.choices.reduce((sum, choice) => sum + choice.votes, 0)
return {
id: id,
expires_at: p.expiresAt,
expired: now.isAfter(expire),
multiple: p.multiple,
votes_count: count,
options: p.choices.map(c => this.choice(c)),
voted: p.choices.some(c => c.isVoted),
own_votes: p.choices.filter(c => c.isVoted).map(c => p.choices.indexOf(c))
}
}
note = (n: Entity.Note, host: string): MegalodonEntity.Status => {
host = host.replace('https://', '')
return {
id: n.id,
uri: n.uri ? n.uri : `https://${host}/notes/${n.id}`,
url: n.uri ? n.uri : `https://${host}/notes/${n.id}`,
account: this.user(n.user),
in_reply_to_id: n.replyId,
in_reply_to_account_id: n.reply?.userId ?? null,
reblog: n.renote ? this.note(n.renote, host) : null,
content: n.text ? this.escapeMFM(n.text) : '',
plain_content: n.text ? n.text : null,
created_at: n.createdAt,
emojis: n.emojis.map(e => this.emoji(e)),
replies_count: n.repliesCount,
reblogs_count: n.renoteCount,
favourites_count: this.getTotalReactions(n.reactions),
reblogged: false,
favourited: !!n.myReaction,
muted: false,
sensitive: n.files ? n.files.some(f => f.isSensitive) : false,
spoiler_text: n.cw ? n.cw : '',
visibility: this.visibility(n.visibility),
media_attachments: n.files ? n.files.map(f => this.file(f)) : [],
mentions: [],
tags: [],
card: null,
poll: n.poll ? this.poll(n.poll, n.id) : null,
application: null,
language: null,
pinned: null,
emoji_reactions: this.mapReactions(n.reactions, n.myReaction),
bookmarked: false,
quote: n.renote && n.text ? this.note(n.renote, host) : null
}
}
mapReactions = (r: { [key: string]: number }, myReaction?: string): Array<MegalodonEntity.Reaction> => {
return Object.keys(r).map(key => {
if (myReaction && key === myReaction) {
return {
count: r[key],
me: true,
name: key
}
}
return {
count: r[key],
me: false,
name: key
}
})
}
getTotalReactions = (r: { [key: string]: number }): number => {
return Object.values(r).length > 0 ? Object.values(r).reduce((previousValue, currentValue) => previousValue + currentValue) : 0
}
reactions = (r: Array<Entity.Reaction>): Array<MegalodonEntity.Reaction> => {
const result: Array<MegalodonEntity.Reaction> = []
for (const e of r) {
const i = result.findIndex(res => res.name === e.type)
if (i >= 0) {
result[i].count++
} else {
result.push({
count: 1,
me: false,
name: e.type
})
}
}
return result
}
noteToConversation = (n: Entity.Note, host: string): MegalodonEntity.Conversation => {
const accounts: Array<MegalodonEntity.Account> = [this.user(n.user)]
if (n.reply) {
accounts.push(this.user(n.reply.user))
}
return {
id: n.id,
accounts: accounts,
last_status: this.note(n, host),
unread: false
}
}
list = (l: Entity.List): MegalodonEntity.List => ({
id: l.id,
title: l.name
})
encodeNotificationType = (e: MegalodonEntity.NotificationType): MisskeyEntity.NotificationType => {
switch (e) {
case NotificationType.Follow:
return MisskeyNotificationType.Follow
case NotificationType.Mention:
return MisskeyNotificationType.Reply
case NotificationType.Favourite:
case NotificationType.EmojiReaction:
return MisskeyNotificationType.Reaction
case NotificationType.Reblog:
return MisskeyNotificationType.Renote
case NotificationType.Poll:
return MisskeyNotificationType.PollEnded
case NotificationType.FollowRequest:
return MisskeyNotificationType.ReceiveFollowRequest
default:
return e
}
}
decodeNotificationType = (e: MisskeyEntity.NotificationType): MegalodonEntity.NotificationType => {
switch (e) {
case MisskeyNotificationType.Follow:
return NotificationType.Follow
case MisskeyNotificationType.Mention:
case MisskeyNotificationType.Reply:
return NotificationType.Mention
case MisskeyNotificationType.Renote:
case MisskeyNotificationType.Quote:
return NotificationType.Reblog
case MisskeyNotificationType.Reaction:
return NotificationType.EmojiReaction
case MisskeyNotificationType.PollEnded:
return NotificationType.Poll
case MisskeyNotificationType.ReceiveFollowRequest:
return NotificationType.FollowRequest
case MisskeyNotificationType.FollowRequestAccepted:
return NotificationType.Follow
default:
return e
}
}
announcement = (a: Entity.Announcement): MegalodonEntity.Announcement => ({
id: a.id,
content: `<h1>${this.escapeMFM(a.title)}</h1>${this.escapeMFM(a.text)}`,
starts_at: null,
ends_at: null,
published: true,
all_day: false,
published_at: a.createdAt,
updated_at: a.updatedAt,
read: a.isRead,
mentions: [],
statuses: [],
tags: [],
emojis: [],
reactions: [],
})
notification = (n: Entity.Notification, host: string): MegalodonEntity.Notification => {
let notification = {
id: n.id,
account: n.user ? this.user(n.user) : this.modelOfAcct,
created_at: n.createdAt,
type: this.decodeNotificationType(n.type)
}
if (n.note) {
notification = Object.assign(notification, {
status: this.note(n.note, host)
})
if (notification.type === NotificationType.Poll) {
notification = Object.assign(notification, {
account: this.note(n.note, host).account
})
}
}
if (n.reaction) {
notification = Object.assign(notification, {
emoji: n.reaction
})
}
return notification
}
stats = (s: Entity.Stats): MegalodonEntity.Stats => {
return {
user_count: s.usersCount,
status_count: s.notesCount,
domain_count: s.instances
}
}
meta = (m: Entity.Meta, s: Entity.Stats): MegalodonEntity.Instance => {
const wss = m.uri.replace(/^https:\/\//, 'wss://')
return {
uri: m.uri,
title: m.name,
description: m.description,
email: m.maintainerEmail,
version: m.version,
thumbnail: m.bannerUrl,
urls: {
streaming_api: `${wss}/streaming`
},
stats: this.stats(s),
languages: m.langs,
contact_account: null,
max_toot_chars: m.maxNoteTextLength,
registrations: !m.disableRegistration
}
}
hashtag = (h: Entity.Hashtag): MegalodonEntity.Tag => {
return {
name: h.tag,
url: h.tag,
history: null,
following: false
}
}
}
export const DEFAULT_SCOPE = [
'read:account',
'write:account',
'read:blocks',
'write:blocks',
'read:drive',
'write:drive',
'read:favorites',
'write:favorites',
'read:following',
'write:following',
'read:mutes',
'write:mutes',
'write:notes',
'read:notifications',
'write:notifications',
'read:reactions',
'write:reactions',
'write:votes'
]
/**
* Interface
*/
export interface Interface {
post<T = any>(path: string, params?: any, headers?: { [key: string]: string }): Promise<Response<T>>
cancel(): void
socket(channel: 'user' | 'localTimeline' | 'hybridTimeline' | 'globalTimeline' | 'conversation' | 'list', listId?: string): WebSocket
}
/**
* Misskey API client.
*
* Usign axios for request, you will handle promises.
*/
export class Client implements Interface {
private accessToken: string | null
private baseUrl: string
private userAgent: string
private abortController: AbortController
private proxyConfig: ProxyConfig | false = false
private converter: Converter
/**
* @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.
* @param converter Converter instance.
*/
constructor(baseUrl: string, accessToken: string | null, userAgent: string = DEFAULT_UA, proxyConfig: ProxyConfig | false = false, converter: Converter) {
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
}
/**
* 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: any = {}, headers: { [key: string]: string } = {}): Promise<Response<T>> {
let options: AxiosRequestConfig = {
headers: headers,
maxContentLength: Infinity,
maxBodyLength: Infinity
}
if (this.proxyConfig) {
options = Object.assign(options, {
httpAgent: proxyAgent(this.proxyConfig),
httpsAgent: proxyAgent(this.proxyConfig)
})
}
let bodyParams = params
if (this.accessToken) {
if (params instanceof FormData) {
bodyParams.append('i', this.accessToken)
} else {
bodyParams = Object.assign(params, {
i: this.accessToken
})
}
}
return axios.post<T>(this.baseUrl + path, bodyParams, options).then((resp: AxiosResponse<T>) => {
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 Misskey API.
*
* @param channel Channel name is user, localTimeline, hybridTimeline, globalTimeline, conversation or list.
* @param listId This parameter is required only list channel.
*/
public socket(
channel: 'user' | 'localTimeline' | 'hybridTimeline' | 'globalTimeline' | 'conversation' | 'list',
listId?: string
): WebSocket {
if (!this.accessToken) {
throw new Error('accessToken is required')
}
const url = `${this.baseUrl}/streaming`
const streaming = new WebSocket(url, channel, this.accessToken, listId, this.userAgent, this.proxyConfig, this.converter)
process.nextTick(() => {
streaming.start()
})
return streaming
}
}
}
export default MisskeyAPI

View file

@ -0,0 +1,6 @@
namespace MisskeyEntity {
export type GetAll = {
tutorial: number
defaultNoteVisibility: 'public' | 'home' | 'followers' | 'specified'
}
}

View file

@ -0,0 +1,10 @@
namespace MisskeyEntity {
export type Announcement = {
id: string
createdAt: string
updatedAt: string
text: string
title: string
isRead?: boolean
}
}

View file

@ -0,0 +1,9 @@
namespace MisskeyEntity {
export type App = {
id: string
name: string
callbackUrl: string
permission: Array<string>
secret: string
}
}

View file

@ -0,0 +1,10 @@
/// <reference path="userDetail.ts" />
namespace MisskeyEntity {
export type Blocking = {
id: string
createdAt: string
blockeeId: string
blockee: UserDetail
}
}

View file

@ -0,0 +1,7 @@
/// <reference path="note.ts" />
namespace MisskeyEntity {
export type CreatedNote = {
createdNote: Note
}
}

View file

@ -0,0 +1,9 @@
namespace MisskeyEntity {
export type Emoji = {
name: string
host: string | null
url: string
aliases: Array<string>
category: string
}
}

View file

@ -0,0 +1,10 @@
/// <reference path="note.ts" />
namespace MisskeyEntity {
export type Favorite = {
id: string
createdAt: string
noteId: string
note: Note
}
}

View file

@ -0,0 +1,6 @@
namespace MisskeyEntity {
export type Field = {
name: string
value: string
}
}

View file

@ -0,0 +1,20 @@
namespace MisskeyEntity {
export type File = {
id: string
createdAt: string
name: string
type: string
md5: string
size: number
isSensitive: boolean
properties: {
width: number
height: number
avgColor: string
}
url: string
thumbnailUrl: string
comment: string
blurhash: string
}
}

View file

@ -0,0 +1,9 @@
/// <reference path="user.ts" />
namespace MisskeyEntity {
export type FollowRequest = {
id: string
follower: User
followee: User
}
}

View file

@ -0,0 +1,11 @@
/// <reference path="userDetail.ts" />
namespace MisskeyEntity {
export type Follower = {
id: string
createdAt: string
followeeId: string
followerId: string
follower: UserDetail
}
}

View file

@ -0,0 +1,11 @@
/// <reference path="userDetail.ts" />
namespace MisskeyEntity {
export type Following = {
id: string
createdAt: string
followeeId: string
followerId: string
followee: UserDetail
}
}

View file

@ -0,0 +1,7 @@
namespace MisskeyEntity {
export type Hashtag = {
tag: string
chart: Array<number>
usersCount: number
}
}

View file

@ -0,0 +1,8 @@
namespace MisskeyEntity {
export type List = {
id: string
createdAt: string
name: string
userIds: Array<string>
}
}

View file

@ -0,0 +1,18 @@
/// <reference path="emoji.ts" />
namespace MisskeyEntity {
export type Meta = {
maintainerName: string
maintainerEmail: string
name: string
version: string
uri: string
description: string
langs: Array<string>
disableRegistration: boolean
disableLocalTimeline: boolean
bannerUrl: string
maxNoteTextLength: 300
emojis: Array<Emoji>
}
}

View file

@ -0,0 +1,10 @@
/// <reference path="userDetail.ts" />
namespace MisskeyEntity {
export type Mute = {
id: string
createdAt: string
muteeId: string
mutee: UserDetail
}
}

View file

@ -0,0 +1,32 @@
/// <reference path="user.ts" />
/// <reference path="emoji.ts" />
/// <reference path="file.ts" />
/// <reference path="poll.ts" />
namespace MisskeyEntity {
export type Note = {
id: string
createdAt: string
userId: string
user: User
text: string | null
cw: string | null
visibility: 'public' | 'home' | 'followers' | 'specified'
renoteCount: number
repliesCount: number
reactions: { [key: string]: number }
emojis: Array<Emoji>
fileIds: Array<string>
files: Array<File>
replyId: string | null
renoteId: string | null
uri?: string
reply?: Note
renote?: Note
viaMobile?: boolean
tags?: Array<string>
poll?: Poll
mentions?: Array<string>
myReaction?: string
}
}

View file

@ -0,0 +1,17 @@
/// <reference path="user.ts" />
/// <reference path="note.ts" />
namespace MisskeyEntity {
export type Notification = {
id: string
createdAt: string
// https://github.com/syuilo/misskey/blob/056942391aee135eb6c77aaa63f6ed5741d701a6/src/models/entities/notification.ts#L50-L62
type: NotificationType
userId: string
user: User
note?: Note
reaction?: string
}
export type NotificationType = string
}

View file

@ -0,0 +1,13 @@
namespace MisskeyEntity {
export type Choice = {
text: string
votes: number
isVoted: boolean
}
export type Poll = {
multiple: boolean
expiresAt: string
choices: Array<Choice>
}
}

View file

@ -0,0 +1,11 @@
/// <reference path="user.ts" />
namespace MisskeyEntity {
export type Reaction = {
id: string
createdAt: string
user: User
url?: string
type: string
}
}

View file

@ -0,0 +1,12 @@
namespace MisskeyEntity {
export type Relation = {
id: string
isFollowing: boolean
hasPendingFollowRequestFromYou: boolean
hasPendingFollowRequestToYou: boolean
isFollowed: boolean
isBlocking: boolean
isBlocked: boolean
isMuted: boolean
}
}

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