mirror of
https://github.com/spacebarchat/server.git
synced 2026-05-23 21:16:07 +00:00
Merge branch 'master' into fix/claim_accounts
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { Config, listenEvent } from "@fosscord/util";
|
||||
import { Config, getRights, listenEvent, Rights } from "@fosscord/util";
|
||||
import { NextFunction, Request, Response, Router } from "express";
|
||||
import { getIpAdress } from "@fosscord/api";
|
||||
import { API_PREFIX_TRAILING_SLASH } from "./Authentication";
|
||||
@@ -9,6 +9,7 @@ import { API_PREFIX_TRAILING_SLASH } from "./Authentication";
|
||||
|
||||
/*
|
||||
? bucket limit? Max actions/sec per bucket?
|
||||
(ANSWER: a small fosscord instance might not need a complex rate limiting system)
|
||||
|
||||
TODO: delay database requests to include multiple queries
|
||||
TODO: different for methods (GET/POST)
|
||||
@@ -44,6 +45,12 @@ export default function rateLimit(opts: {
|
||||
onlyIp?: boolean;
|
||||
}): any {
|
||||
return async (req: Request, res: Response, next: NextFunction): Promise<any> => {
|
||||
// exempt user? if so, immediately short circuit
|
||||
if (req.user_id) {
|
||||
const rights = await getRights(req.user_id);
|
||||
if (rights.has("BYPASS_RATE_LIMITS")) return;
|
||||
}
|
||||
|
||||
const bucket_id = opts.bucket || req.originalUrl.replace(API_PREFIX_TRAILING_SLASH, "");
|
||||
var executor_id = getIpAdress(req);
|
||||
if (!opts.onlyIp && req.user_id) executor_id = req.user_id;
|
||||
@@ -53,12 +60,12 @@ export default function rateLimit(opts: {
|
||||
if (opts.GET && ["GET", "OPTIONS", "HEAD"].includes(req.method)) max_hits = opts.GET;
|
||||
else if (opts.MODIFY && ["POST", "DELETE", "PATCH", "PUT"].includes(req.method)) max_hits = opts.MODIFY;
|
||||
|
||||
const offender = Cache.get(executor_id + bucket_id);
|
||||
let offender = Cache.get(executor_id + bucket_id);
|
||||
|
||||
if (offender) {
|
||||
const reset = offender.expires_at.getTime();
|
||||
const resetAfterMs = reset - Date.now();
|
||||
const resetAfterSec = resetAfterMs / 1000;
|
||||
let reset = offender.expires_at.getTime();
|
||||
let resetAfterMs = reset - Date.now();
|
||||
let resetAfterSec = Math.ceil(resetAfterMs / 1000);
|
||||
|
||||
if (resetAfterMs <= 0) {
|
||||
offender.hits = 0;
|
||||
@@ -70,6 +77,11 @@ export default function rateLimit(opts: {
|
||||
|
||||
if (offender.blocked) {
|
||||
const global = bucket_id === "global";
|
||||
// each block violation pushes the expiry one full window further
|
||||
reset += opts.window * 1000;
|
||||
offender.expires_at = new Date(offender.expires_at.getTime() + opts.window * 1000);
|
||||
resetAfterMs = reset - Date.now();
|
||||
resetAfterSec = Math.ceil(resetAfterMs / 1000);
|
||||
|
||||
console.log("blocked bucket: " + bucket_id, { resetAfterMs });
|
||||
return (
|
||||
@@ -151,7 +163,7 @@ export async function initRateLimits(app: Router) {
|
||||
app.use("/auth/register", rateLimit({ onlyIp: true, success: true, ...routes.auth.register }));
|
||||
}
|
||||
|
||||
async function hitRoute(opts: { executor_id: string; bucket_id: string; max_hits: number; window: number }) {
|
||||
async function hitRoute(opts: { executor_id: string; bucket_id: string; max_hits: number; window: number; }) {
|
||||
const id = opts.executor_id + opts.bucket_id;
|
||||
var limit = Cache.get(id);
|
||||
if (!limit) {
|
||||
|
||||
@@ -128,7 +128,7 @@ router.post("/", route({ body: "RegisterSchema" }), async (req: Request, res: Re
|
||||
throw FieldErrors({
|
||||
date_of_birth: { code: "BASE_TYPE_REQUIRED", message: req.t("common:field.BASE_TYPE_REQUIRED") }
|
||||
});
|
||||
} else if (register.dateOfBirth.minimum) {
|
||||
} else if (register.dateOfBirth.required && register.dateOfBirth.minimum) {
|
||||
const minimum = new Date();
|
||||
minimum.setFullYear(minimum.getFullYear() - register.dateOfBirth.minimum);
|
||||
body.date_of_birth = new Date(body.date_of_birth as Date);
|
||||
|
||||
@@ -4,8 +4,9 @@ import { route } from "@fosscord/api";
|
||||
|
||||
const router = Router();
|
||||
|
||||
// TODO: check if message exists
|
||||
// TODO: public read receipts & privacy scoping
|
||||
// TODO: send read state event to all channel members
|
||||
// TODO: advance-only notification cursor
|
||||
|
||||
export interface MessageAcknowledgeSchema {
|
||||
manual?: boolean;
|
||||
|
||||
@@ -1,12 +1,38 @@
|
||||
import { Channel, emitEvent, getPermission, getRights, MessageDeleteEvent, Message, MessageUpdateEvent } from "@fosscord/util";
|
||||
import {
|
||||
Attachment,
|
||||
Channel,
|
||||
Embed,
|
||||
DiscordApiErrors,
|
||||
emitEvent,
|
||||
FosscordApiErrors,
|
||||
getPermission,
|
||||
getRights,
|
||||
Message,
|
||||
MessageCreateEvent,
|
||||
MessageDeleteEvent,
|
||||
MessageUpdateEvent,
|
||||
Snowflake,
|
||||
uploadFile
|
||||
} from "@fosscord/util";
|
||||
import { Router, Response, Request } from "express";
|
||||
import multer from "multer";
|
||||
import { route } from "@fosscord/api";
|
||||
import { handleMessage, postHandleMessage } from "@fosscord/api";
|
||||
import { MessageCreateSchema } from "../index";
|
||||
import { HTTPError } from "lambert-server";
|
||||
|
||||
const router = Router();
|
||||
// TODO: message content/embed string length limit
|
||||
|
||||
const messageUpload = multer({
|
||||
limits: {
|
||||
fileSize: 1024 * 1024 * 100,
|
||||
fields: 10,
|
||||
files: 1
|
||||
},
|
||||
storage: multer.memoryStorage()
|
||||
}); // max upload 50 mb
|
||||
|
||||
router.patch("/", route({ body: "MessageCreateSchema", permission: "SEND_MESSAGES", right: "SEND_MESSAGES" }), async (req: Request, res: Response) => {
|
||||
const { message_id, channel_id } = req.params;
|
||||
var body = req.body as MessageCreateSchema;
|
||||
@@ -51,6 +77,95 @@ router.patch("/", route({ body: "MessageCreateSchema", permission: "SEND_MESSAGE
|
||||
return res.json(message);
|
||||
});
|
||||
|
||||
|
||||
// Backfill message with specific timestamp
|
||||
router.put(
|
||||
"/",
|
||||
messageUpload.single("file"),
|
||||
async (req, res, next) => {
|
||||
if (req.body.payload_json) {
|
||||
req.body = JSON.parse(req.body.payload_json);
|
||||
}
|
||||
|
||||
next();
|
||||
},
|
||||
route({ body: "MessageCreateSchema", permission: "SEND_MESSAGES", right: "SEND_BACKDATED_EVENTS" }),
|
||||
async (req: Request, res: Response) => {
|
||||
const { channel_id, message_id } = req.params;
|
||||
var body = req.body as MessageCreateSchema;
|
||||
const attachments: Attachment[] = [];
|
||||
|
||||
const rights = await getRights(req.user_id);
|
||||
rights.hasThrow("SEND_MESSAGES");
|
||||
|
||||
// regex to check if message contains anything other than numerals ( also no decimals )
|
||||
if (!message_id.match(/^\+?\d+$/)) {
|
||||
throw new HTTPError("Message IDs must be positive integers", 400);
|
||||
}
|
||||
|
||||
const snowflake = Snowflake.deconstruct(message_id)
|
||||
if (Date.now() < snowflake.timestamp) {
|
||||
// message is in the future
|
||||
throw FosscordApiErrors.CANNOT_BACKFILL_TO_THE_FUTURE;
|
||||
}
|
||||
|
||||
const exists = await Message.findOne({ where: { id: message_id, channel_id: channel_id }});
|
||||
if (exists) {
|
||||
throw FosscordApiErrors.CANNOT_REPLACE_BY_BACKFILL;
|
||||
}
|
||||
|
||||
if (req.file) {
|
||||
try {
|
||||
const file = await uploadFile(`/attachments/${req.params.channel_id}`, req.file);
|
||||
attachments.push({ ...file, proxy_url: file.url });
|
||||
} catch (error) {
|
||||
return res.status(400).json(error);
|
||||
}
|
||||
}
|
||||
const channel = await Channel.findOneOrFail({ where: { id: channel_id }, relations: ["recipients", "recipients.user"] });
|
||||
|
||||
const embeds = body.embeds || [];
|
||||
if (body.embed) embeds.push(body.embed);
|
||||
let message = await handleMessage({
|
||||
...body,
|
||||
type: 0,
|
||||
pinned: false,
|
||||
author_id: req.user_id,
|
||||
id: message_id,
|
||||
embeds,
|
||||
channel_id,
|
||||
attachments,
|
||||
edited_timestamp: undefined,
|
||||
timestamp: new Date(snowflake.timestamp),
|
||||
});
|
||||
|
||||
//Fix for the client bug
|
||||
delete message.member
|
||||
|
||||
await Promise.all([
|
||||
message.save(),
|
||||
emitEvent({ event: "MESSAGE_CREATE", channel_id: channel_id, data: message } as MessageCreateEvent),
|
||||
channel.save()
|
||||
]);
|
||||
|
||||
postHandleMessage(message).catch((e) => { }); // no await as it shouldnt block the message send function and silently catch error
|
||||
|
||||
return res.json(message);
|
||||
}
|
||||
);
|
||||
|
||||
router.get("/", route({ permission: "VIEW_CHANNEL" }), async (req: Request, res: Response) => {
|
||||
const { message_id, channel_id } = req.params;
|
||||
|
||||
const message = await Message.findOneOrFail({ where: { id: message_id, channel_id }, relations: ["attachments"] });
|
||||
|
||||
const permissions = await getPermission(req.user_id, undefined, channel_id);
|
||||
|
||||
if (message.author_id !== req.user_id) permissions.hasThrow("READ_MESSAGE_HISTORY");
|
||||
|
||||
return res.json(message);
|
||||
});
|
||||
|
||||
router.delete("/", route({}), async (req: Request, res: Response) => {
|
||||
const { message_id, channel_id } = req.params;
|
||||
|
||||
|
||||
@@ -101,7 +101,7 @@ router.get("/:emoji", route({ permission: "VIEW_CHANNEL" }), async (req: Request
|
||||
res.json(users);
|
||||
});
|
||||
|
||||
router.put("/:emoji/:user_id", route({ permission: "READ_MESSAGE_HISTORY" }), async (req: Request, res: Response) => {
|
||||
router.put("/:emoji/:user_id", route({ permission: "READ_MESSAGE_HISTORY", right: "SELF_ADD_REACTIONS" }), async (req: Request, res: Response) => {
|
||||
const { message_id, channel_id, user_id } = req.params;
|
||||
if (user_id !== "@me") throw new HTTPError("Invalid user");
|
||||
const emoji = getEmoji(req.params.emoji);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Router, Response, Request } from "express";
|
||||
import { Channel, Config, emitEvent, getPermission, MessageDeleteBulkEvent, Message } from "@fosscord/util";
|
||||
import { Channel, Config, emitEvent, getPermission, getRights, MessageDeleteBulkEvent, Message } from "@fosscord/util";
|
||||
import { HTTPError } from "lambert-server";
|
||||
import { route } from "@fosscord/api";
|
||||
import { In } from "typeorm";
|
||||
@@ -12,22 +12,28 @@ export interface BulkDeleteSchema {
|
||||
messages: string[];
|
||||
}
|
||||
|
||||
// TODO: should users be able to bulk delete messages or only bots?
|
||||
// TODO: should this request fail, if you provide messages older than 14 days/invalid ids?
|
||||
// should users be able to bulk delete messages or only bots? ANSWER: all users
|
||||
// should this request fail, if you provide messages older than 14 days/invalid ids? ANSWER: NO
|
||||
// https://discord.com/developers/docs/resources/channel#bulk-delete-messages
|
||||
router.post("/", route({ body: "BulkDeleteSchema" }), async (req: Request, res: Response) => {
|
||||
const { channel_id } = req.params;
|
||||
const channel = await Channel.findOneOrFail({ id: channel_id });
|
||||
if (!channel.guild_id) throw new HTTPError("Can't bulk delete dm channel messages", 400);
|
||||
|
||||
const rights = await getRights(req.user_id);
|
||||
rights.hasThrow("SELF_DELETE_MESSAGES");
|
||||
|
||||
let superuser = rights.has("MANAGE_MESSAGES");
|
||||
const permission = await getPermission(req.user_id, channel?.guild_id, channel_id);
|
||||
permission.hasThrow("MANAGE_MESSAGES");
|
||||
|
||||
|
||||
const { maxBulkDelete } = Config.get().limits.message;
|
||||
|
||||
const { messages } = req.body as { messages: string[] };
|
||||
if (messages.length < 2) throw new HTTPError("You must at least specify 2 messages to bulk delete");
|
||||
if (messages.length > maxBulkDelete) throw new HTTPError(`You cannot delete more than ${maxBulkDelete} messages`);
|
||||
if (messages.length === 0) throw new HTTPError("You must specify messages to bulk delete");
|
||||
if (!superuser) {
|
||||
permission.hasThrow("MANAGE_MESSAGES");
|
||||
if (messages.length > maxBulkDelete) throw new HTTPError(`You cannot delete more than ${maxBulkDelete} messages`);
|
||||
}
|
||||
|
||||
await Message.delete(messages.map((x) => ({ id: x })));
|
||||
|
||||
|
||||
@@ -8,8 +8,10 @@ import {
|
||||
Embed,
|
||||
emitEvent,
|
||||
getPermission,
|
||||
getRights,
|
||||
Message,
|
||||
MessageCreateEvent,
|
||||
Snowflake,
|
||||
uploadFile,
|
||||
Member
|
||||
} from "@fosscord/util";
|
||||
@@ -29,6 +31,8 @@ export function isTextChannel(type: ChannelType): boolean {
|
||||
case ChannelType.GUILD_VOICE:
|
||||
case ChannelType.GUILD_STAGE_VOICE:
|
||||
case ChannelType.GUILD_CATEGORY:
|
||||
case ChannelType.GUILD_FORUM:
|
||||
case ChannelType.DIRECTORY:
|
||||
throw new HTTPError("not a text channel", 400);
|
||||
case ChannelType.DM:
|
||||
case ChannelType.GROUP_DM:
|
||||
@@ -67,7 +71,11 @@ export interface MessageCreateSchema {
|
||||
};
|
||||
payload_json?: string;
|
||||
file?: any;
|
||||
attachments?: any[]; //TODO we should create an interface for attachments
|
||||
/**
|
||||
TODO: we should create an interface for attachments
|
||||
TODO: OpenWAAO<-->attachment-style metadata conversion
|
||||
**/
|
||||
attachments?: any[];
|
||||
sticker_ids?: string[];
|
||||
}
|
||||
|
||||
@@ -83,7 +91,7 @@ router.get("/", async (req: Request, res: Response) => {
|
||||
const before = req.query.before ? `${req.query.before}` : undefined;
|
||||
const after = req.query.after ? `${req.query.after}` : undefined;
|
||||
const limit = Number(req.query.limit) || 50;
|
||||
if (limit < 1 || limit > 100) throw new HTTPError("limit must be between 1 and 100");
|
||||
if (limit < 1 || limit > 100) throw new HTTPError("limit must be between 1 and 100", 422);
|
||||
|
||||
var halfLimit = Math.floor(limit / 2);
|
||||
|
||||
@@ -97,9 +105,16 @@ router.get("/", async (req: Request, res: Response) => {
|
||||
where: { channel_id },
|
||||
relations: ["author", "webhook", "application", "mentions", "mention_roles", "mention_channels", "sticker_items", "attachments"]
|
||||
};
|
||||
|
||||
|
||||
if (after) query.where.id = MoreThan(after);
|
||||
else if (before) query.where.id = LessThan(before);
|
||||
if (after) {
|
||||
if (after > new Snowflake()) return res.status(422);
|
||||
query.where.id = MoreThan(after);
|
||||
}
|
||||
else if (before) {
|
||||
if (before < req.params.channel_id) return res.status(422);
|
||||
query.where.id = LessThan(before);
|
||||
}
|
||||
else if (around) {
|
||||
query.where.id = [
|
||||
MoreThan((BigInt(around) - BigInt(halfLimit)).toString()),
|
||||
@@ -119,15 +134,18 @@ router.get("/", async (req: Request, res: Response) => {
|
||||
delete x.user_ids;
|
||||
});
|
||||
// @ts-ignore
|
||||
if (!x.author) x.author = { discriminator: "0000", username: "Deleted User", public_flags: "0", avatar: null };
|
||||
if (!x.author) x.author = { id: "4", discriminator: "0000", username: "Fosscord Ghost", public_flags: "0", avatar: null };
|
||||
x.attachments?.forEach((y: any) => {
|
||||
// dynamically set attachment proxy_url in case the endpoint changed
|
||||
const uri = y.proxy_url.startsWith("http") ? y.proxy_url : `https://example.org${y.proxy_url}`;
|
||||
y.proxy_url = `${endpoint == null ? "" : endpoint}${new URL(uri).pathname}`;
|
||||
});
|
||||
|
||||
//Some clients ( discord.js ) only check if a property exists within the response,
|
||||
//which causes erorrs when, say, the `application` property is `null`.
|
||||
|
||||
/**
|
||||
Some clients ( discord.js ) only check if a property exists within the response,
|
||||
which causes erorrs when, say, the `application` property is `null`.
|
||||
**/
|
||||
|
||||
for (var curr in x) {
|
||||
if (x[curr] === null)
|
||||
delete x[curr];
|
||||
@@ -147,15 +165,14 @@ const messageUpload = multer({
|
||||
},
|
||||
storage: multer.memoryStorage()
|
||||
}); // max upload 50 mb
|
||||
/**
|
||||
TODO: dynamically change limit of MessageCreateSchema with config
|
||||
|
||||
// TODO: dynamically change limit of MessageCreateSchema with config
|
||||
// TODO: check: sum of all characters in an embed structure must not exceed 6000 characters
|
||||
|
||||
// https://discord.com/developers/docs/resources/channel#create-message
|
||||
// TODO: text channel slowdown
|
||||
// TODO: trim and replace message content and every embed field
|
||||
// TODO: check allowed_mentions
|
||||
|
||||
https://discord.com/developers/docs/resources/channel#create-message
|
||||
TODO: text channel slowdown (per-user and across-users)
|
||||
Q: trim and replace message content and every embed field A: NO, given this cannot be implemented in E2EE channels
|
||||
TODO: only dispatch notifications for mentions denoted in allowed_mentions
|
||||
**/
|
||||
// Send message
|
||||
router.post(
|
||||
"/",
|
||||
@@ -167,7 +184,7 @@ router.post(
|
||||
|
||||
next();
|
||||
},
|
||||
route({ body: "MessageCreateSchema", permission: "SEND_MESSAGES" }),
|
||||
route({ body: "MessageCreateSchema", permission: "SEND_MESSAGES", right: "SEND_MESSAGES" }),
|
||||
async (req: Request, res: Response) => {
|
||||
const { channel_id } = req.params;
|
||||
var body = req.body as MessageCreateSchema;
|
||||
@@ -182,6 +199,9 @@ router.post(
|
||||
}
|
||||
}
|
||||
const channel = await Channel.findOneOrFail({ where: { id: channel_id }, relations: ["recipients", "recipients.user"] });
|
||||
if (!channel.isWritable()) {
|
||||
throw new HTTPError(`Cannot send messages to channel of type ${channel.type}`, 400)
|
||||
}
|
||||
|
||||
const embeds = body.embeds || [];
|
||||
if (body.embed) embeds.push(body.embed);
|
||||
@@ -235,3 +255,4 @@ router.post(
|
||||
return res.json(message);
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
import { HTTPError } from "lambert-server";
|
||||
import { route } from "@fosscord/api";
|
||||
import { isTextChannel } from "./messages";
|
||||
import { FindManyOptions, Between, Not } from "typeorm";
|
||||
import {
|
||||
Attachment,
|
||||
Channel,
|
||||
Config,
|
||||
Embed,
|
||||
DiscordApiErrors,
|
||||
emitEvent,
|
||||
FosscordApiErrors,
|
||||
getPermission,
|
||||
getRights,
|
||||
Message,
|
||||
MessageDeleteBulkEvent,
|
||||
Snowflake,
|
||||
uploadFile
|
||||
} from "@fosscord/util";
|
||||
import { Router, Response, Request } from "express";
|
||||
import multer from "multer";
|
||||
import { handleMessage, postHandleMessage } from "@fosscord/api";
|
||||
|
||||
const router: Router = Router();
|
||||
|
||||
export default router;
|
||||
|
||||
export interface PurgeSchema {
|
||||
before: string;
|
||||
after: string
|
||||
}
|
||||
|
||||
/**
|
||||
TODO: apply the delete bit by bit to prevent client and database stress
|
||||
**/
|
||||
router.post("/", route({ /*body: "PurgeSchema",*/ }), async (req: Request, res: Response) => {
|
||||
const { channel_id } = req.params;
|
||||
const channel = await Channel.findOneOrFail({ id: channel_id });
|
||||
|
||||
if (!channel.guild_id) throw new HTTPError("Can't purge dm channels", 400);
|
||||
isTextChannel(channel.type);
|
||||
|
||||
const rights = await getRights(req.user_id);
|
||||
if (!rights.has("MANAGE_MESSAGES")) {
|
||||
const permissions = await getPermission(req.user_id, channel.guild_id, channel_id);
|
||||
permissions.hasThrow("MANAGE_MESSAGES");
|
||||
permissions.hasThrow("MANAGE_CHANNELS");
|
||||
}
|
||||
|
||||
const { before, after } = req.body as PurgeSchema;
|
||||
|
||||
// TODO: send the deletion event bite-by-bite to prevent client stress
|
||||
|
||||
var query: FindManyOptions<Message> & { where: { id?: any; }; } = {
|
||||
order: { id: "ASC" },
|
||||
// take: limit,
|
||||
where: {
|
||||
channel_id,
|
||||
id: Between(after, before), // the right way around
|
||||
author_id: rights.has("SELF_DELETE_MESSAGES") ? undefined : Not(req.user_id)
|
||||
// if you lack the right of self-deletion, you can't delete your own messages, even in purges
|
||||
},
|
||||
relations: ["author", "webhook", "application", "mentions", "mention_roles", "mention_channels", "sticker_items", "attachments"]
|
||||
};
|
||||
|
||||
|
||||
const messages = await Message.find(query);
|
||||
const endpoint = Config.get().cdn.endpointPublic;
|
||||
|
||||
if (messages.length == 0) {
|
||||
res.sendStatus(304);
|
||||
return;
|
||||
}
|
||||
|
||||
await Message.delete(messages.map((x) => ({ id: x })));
|
||||
|
||||
await emitEvent({
|
||||
event: "MESSAGE_DELETE_BULK",
|
||||
channel_id,
|
||||
data: { ids: messages.map(x => x.id), channel_id, guild_id: channel.guild_id }
|
||||
} as MessageDeleteBulkEvent);
|
||||
|
||||
res.sendStatus(204);
|
||||
});
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Request, Response, Router } from "express";
|
||||
import { Member, getPermission, Role, GuildMemberUpdateEvent, emitEvent, Sticker, Emoji, Guild } from "@fosscord/util";
|
||||
import { Member, getPermission, getRights, Role, GuildMemberUpdateEvent, emitEvent, Sticker, Emoji, Rights, Guild } from "@fosscord/util";
|
||||
import { HTTPError } from "lambert-server";
|
||||
import { route } from "@fosscord/api";
|
||||
|
||||
@@ -52,27 +52,47 @@ router.put("/", route({}), async (req: Request, res: Response) => {
|
||||
|
||||
// TODO: Lurker mode
|
||||
|
||||
const rights = await getRights(req.user_id);
|
||||
|
||||
let { guild_id, member_id } = req.params;
|
||||
if (member_id === "@me") member_id = req.user_id;
|
||||
if (member_id === "@me") {
|
||||
member_id = req.user_id;
|
||||
rights.hasThrow("JOIN_GUILDS");
|
||||
} else {
|
||||
// TODO: join others by controller
|
||||
}
|
||||
|
||||
var guild = await Guild.findOneOrFail({
|
||||
where: { id: guild_id } });
|
||||
where: { id: guild_id }
|
||||
});
|
||||
|
||||
var emoji = await Emoji.find({
|
||||
where: { guild_id: guild_id } });
|
||||
where: { guild_id: guild_id }
|
||||
});
|
||||
|
||||
var roles = await Role.find({
|
||||
where: { guild_id: guild_id } });
|
||||
where: { guild_id: guild_id }
|
||||
});
|
||||
|
||||
var stickers = await Sticker.find({
|
||||
where: { guild_id: guild_id } });
|
||||
|
||||
where: { guild_id: guild_id }
|
||||
});
|
||||
|
||||
await Member.addToGuild(member_id, guild_id);
|
||||
res.send({...guild, emojis: emoji, roles: roles, stickers: stickers});
|
||||
res.send({ ...guild, emojis: emoji, roles: roles, stickers: stickers });
|
||||
});
|
||||
|
||||
router.delete("/", route({ permission: "KICK_MEMBERS" }), async (req: Request, res: Response) => {
|
||||
router.delete("/", route({}), async (req: Request, res: Response) => {
|
||||
const permission = await getPermission(req.user_id);
|
||||
const rights = await getRights(req.user_id);
|
||||
const { guild_id, member_id } = req.params;
|
||||
if (member_id !== "@me" || member_id === req.user_id) {
|
||||
// TODO: unless force-joined
|
||||
rights.hasThrow("SELF_LEAVE_GROUPS");
|
||||
} else {
|
||||
rights.hasThrow("KICK_BAN_MEMBERS");
|
||||
permission.hasThrow("KICK_MEMBERS");
|
||||
}
|
||||
|
||||
await Member.removeFromGuild(member_id, guild_id);
|
||||
res.sendStatus(204);
|
||||
|
||||
@@ -6,7 +6,6 @@ import { HTTPError } from "lambert-server";
|
||||
|
||||
const router = Router();
|
||||
|
||||
// TODO: not allowed for user -> only allowed for bots with privileged intents
|
||||
// TODO: send over websocket
|
||||
// TODO: check for GUILD_MEMBERS intent
|
||||
|
||||
|
||||
@@ -11,6 +11,10 @@ export const inactiveMembers = async (guild_id: string, user_id: string, days: n
|
||||
//Snowflake should have `generateFromTime` method? Or similar?
|
||||
var minId = BigInt(date.valueOf() - Snowflake.EPOCH) << BigInt(22);
|
||||
|
||||
/**
|
||||
idea: ability to customise the cutoff variable
|
||||
possible candidates: public read receipt, last presence, last VC leave
|
||||
**/
|
||||
var members = await Member.find({
|
||||
where: [
|
||||
{
|
||||
@@ -47,7 +51,7 @@ export const inactiveMembers = async (guild_id: string, user_id: string, days: n
|
||||
return members;
|
||||
};
|
||||
|
||||
router.get("/", route({ permission: "KICK_MEMBERS" }), async (req: Request, res: Response) => {
|
||||
router.get("/", route({}), async (req: Request, res: Response) => {
|
||||
const days = parseInt(req.query.days as string);
|
||||
|
||||
var roles = req.query.include_roles;
|
||||
@@ -65,7 +69,7 @@ export interface PruneSchema {
|
||||
days: number;
|
||||
}
|
||||
|
||||
router.post("/", route({ permission: "KICK_MEMBERS" }), async (req: Request, res: Response) => {
|
||||
router.post("/", route({ permission: "KICK_MEMBERS", right: "KICK_BAN_MEMBERS" }), async (req: Request, res: Response) => {
|
||||
const days = parseInt(req.body.days);
|
||||
|
||||
var roles = req.query.include_roles;
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
import { Router, Request, Response } from "express";
|
||||
import { Role, Member, GuildRoleUpdateEvent, GuildRoleDeleteEvent, emitEvent, handleFile } from "@fosscord/util";
|
||||
import { route } from "@fosscord/api";
|
||||
import { HTTPError } from "lambert-server";
|
||||
import { RoleModifySchema } from "../";
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.get("/", route({}), async (req: Request, res: Response) => {
|
||||
const { guild_id, role_id } = req.params;
|
||||
await Member.IsInGuildOrFail(req.user_id, guild_id);
|
||||
const role = await Role.findOneOrFail({ guild_id, id: role_id });
|
||||
return res.json(role);
|
||||
});
|
||||
|
||||
router.delete("/", route({ permission: "MANAGE_ROLES" }), async (req: Request, res: Response) => {
|
||||
const { guild_id, role_id } = req.params;
|
||||
if (role_id === guild_id) throw new HTTPError("You can't delete the @everyone role");
|
||||
|
||||
await Promise.all([
|
||||
Role.delete({
|
||||
id: role_id,
|
||||
guild_id: guild_id
|
||||
}),
|
||||
emitEvent({
|
||||
event: "GUILD_ROLE_DELETE",
|
||||
guild_id,
|
||||
data: {
|
||||
guild_id,
|
||||
role_id
|
||||
}
|
||||
} as GuildRoleDeleteEvent)
|
||||
]);
|
||||
|
||||
res.sendStatus(204);
|
||||
});
|
||||
|
||||
// TODO: check role hierarchy
|
||||
|
||||
router.patch("/", route({ body: "RoleModifySchema", permission: "MANAGE_ROLES" }), async (req: Request, res: Response) => {
|
||||
const { role_id, guild_id } = req.params;
|
||||
const body = req.body as RoleModifySchema;
|
||||
|
||||
if (body.icon) body.icon = await handleFile(`/role-icons/${role_id}`, body.icon as string);
|
||||
|
||||
const role = new Role({
|
||||
...body,
|
||||
id: role_id,
|
||||
guild_id,
|
||||
permissions: String(req.permission!.bitfield & BigInt(body.permissions || "0"))
|
||||
});
|
||||
|
||||
await Promise.all([
|
||||
role.save(),
|
||||
emitEvent({
|
||||
event: "GUILD_ROLE_UPDATE",
|
||||
guild_id,
|
||||
data: {
|
||||
guild_id,
|
||||
role
|
||||
}
|
||||
} as GuildRoleUpdateEvent)
|
||||
]);
|
||||
|
||||
res.json(role);
|
||||
});
|
||||
|
||||
export default router;
|
||||
-53
@@ -81,59 +81,6 @@ router.post("/", route({ body: "RoleModifySchema", permission: "MANAGE_ROLES" })
|
||||
res.json(role);
|
||||
});
|
||||
|
||||
router.delete("/:role_id", route({ permission: "MANAGE_ROLES" }), async (req: Request, res: Response) => {
|
||||
const guild_id = req.params.guild_id;
|
||||
const { role_id } = req.params;
|
||||
if (role_id === guild_id) throw new HTTPError("You can't delete the @everyone role");
|
||||
|
||||
await Promise.all([
|
||||
Role.delete({
|
||||
id: role_id,
|
||||
guild_id: guild_id
|
||||
}),
|
||||
emitEvent({
|
||||
event: "GUILD_ROLE_DELETE",
|
||||
guild_id,
|
||||
data: {
|
||||
guild_id,
|
||||
role_id
|
||||
}
|
||||
} as GuildRoleDeleteEvent)
|
||||
]);
|
||||
|
||||
res.sendStatus(204);
|
||||
});
|
||||
|
||||
// TODO: check role hierarchy
|
||||
|
||||
router.patch("/:role_id", route({ body: "RoleModifySchema", permission: "MANAGE_ROLES" }), async (req: Request, res: Response) => {
|
||||
const { role_id, guild_id } = req.params;
|
||||
const body = req.body as RoleModifySchema;
|
||||
|
||||
if (body.icon) body.icon = await handleFile(`/role-icons/${role_id}`, body.icon as string);
|
||||
|
||||
const role = new Role({
|
||||
...body,
|
||||
id: role_id,
|
||||
guild_id,
|
||||
permissions: String(req.permission!.bitfield & BigInt(body.permissions || "0"))
|
||||
});
|
||||
|
||||
await Promise.all([
|
||||
role.save(),
|
||||
emitEvent({
|
||||
event: "GUILD_ROLE_UPDATE",
|
||||
guild_id,
|
||||
data: {
|
||||
guild_id,
|
||||
role
|
||||
}
|
||||
} as GuildRoleUpdateEvent)
|
||||
]);
|
||||
|
||||
res.json(role);
|
||||
});
|
||||
|
||||
router.patch("/", route({ body: "RolePositionUpdateSchema" }), async (req: Request, res: Response) => {
|
||||
const { guild_id } = req.params;
|
||||
const body = req.body as RolePositionUpdateSchema;
|
||||
@@ -13,7 +13,7 @@ router.get("/:code", route({}), async (req: Request, res: Response) => {
|
||||
res.status(200).send(invite);
|
||||
});
|
||||
|
||||
router.post("/:code", route({right: "JOIN_GUILDS"}), async (req: Request, res: Response) => {
|
||||
router.post("/:code", route({right: "USE_MASS_INVITES"}), async (req: Request, res: Response) => {
|
||||
const { code } = req.params;
|
||||
const { guild_id } = await Invite.findOneOrFail({ code })
|
||||
const { features } = await Guild.findOneOrFail({ id: guild_id});
|
||||
|
||||
+17
-1
@@ -1,10 +1,26 @@
|
||||
import { Router, Response, Request } from "express";
|
||||
import { route } from "@fosscord/api";
|
||||
import { Config } from "@fosscord/util";
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.get("/", route({}), (req: Request, res: Response) => {
|
||||
res.send("pong");
|
||||
const { general } = Config.get();
|
||||
res.send({
|
||||
ping: "pong!",
|
||||
instance: {
|
||||
id: general.instanceId,
|
||||
name: general.instanceName,
|
||||
description: general.instanceDescription,
|
||||
image: general.image,
|
||||
|
||||
correspondenceEmail: general.correspondenceEmail,
|
||||
correspondenceUserID: general.correspondenceUserID,
|
||||
|
||||
frontPage: general.frontPage,
|
||||
tosPage: general.tosPage,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
export default router;
|
||||
|
||||
+6
-1
@@ -7,7 +7,12 @@ config();
|
||||
import { FosscordServer } from "./Server";
|
||||
import cluster from "cluster";
|
||||
import os from "os";
|
||||
const cores = Number(process.env.THREADS) || os.cpus().length;
|
||||
var cores = 1;
|
||||
try {
|
||||
cores = Number(process.env.THREADS) || os.cpus().length;
|
||||
} catch {
|
||||
console.log("[API] Failed to get thread count! Using 1...")
|
||||
}
|
||||
|
||||
if (cluster.isMaster && process.env.NODE_ENV == "production") {
|
||||
console.log(`Primary ${process.pid} is running`);
|
||||
|
||||
@@ -91,7 +91,8 @@ export async function handleMessage(opts: MessageOptions): Promise<Message> {
|
||||
if (opts.message_reference.channel_id !== opts.channel_id) throw new HTTPError("You can only reference messages from this channel");
|
||||
}
|
||||
}
|
||||
// Q: should be checked if the referenced message exists? ANSWER: NO
|
||||
/** Q: should be checked if the referenced message exists? ANSWER: NO
|
||||
otherwise backfilling won't work **/
|
||||
// @ts-ignore
|
||||
message.type = MessageType.REPLY;
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
FieldErrors,
|
||||
FosscordApiErrors,
|
||||
getPermission,
|
||||
getRights,
|
||||
PermissionResolvable,
|
||||
Permissions,
|
||||
RightResolvable,
|
||||
@@ -105,6 +106,8 @@ export function route(opts: RouteOptions) {
|
||||
|
||||
if (opts.right) {
|
||||
const required = new Rights(opts.right);
|
||||
req.rights = await getRights(req.user_id);
|
||||
|
||||
if (!req.rights || !req.rights.has(required)) {
|
||||
throw FosscordApiErrors.MISSING_RIGHTS.withParams(opts.right as string);
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ const blocklist: string[] = []; // TODO: update ones passwordblocklist is stored
|
||||
* - min <n> numbers
|
||||
* - min <n> symbols
|
||||
* - min <n> uppercase chars
|
||||
* - shannon entropy divided by password entropy
|
||||
* - shannon entropy folded into [0, 1) interval
|
||||
*
|
||||
* Returns: 0 > pw > 1
|
||||
*/
|
||||
@@ -46,15 +46,15 @@ export function checkPassword(password: string): number {
|
||||
strength = 0;
|
||||
}
|
||||
|
||||
let entropyMap;
|
||||
let entropyMap: { [key: string]: number } = {};
|
||||
for (let i = 0; i < password.length; i++) {
|
||||
if (entropyMap[password[i]]) entropyMap[password[i]]++;
|
||||
else entropyMap[password[i]] = 1;
|
||||
}
|
||||
|
||||
let entropies = Array(entropyMap);
|
||||
|
||||
let entropies = Object.values(entropyMap);
|
||||
|
||||
entropies.map(x => (x / entropyMap.length));
|
||||
strength += entropies.reduceRight((a, x), a - (x * Math.log2(x))) / Math.log2(password.length);
|
||||
strength += entropies.reduceRight((a: number, x: number) => a - (x * Math.log2(x))) / Math.log2(password.length);
|
||||
return strength;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user