✨ typeorm api rewrite done
This commit is contained in:
@@ -1,20 +1,10 @@
|
||||
import { Router, Request, Response } from "express";
|
||||
import {
|
||||
Channel,
|
||||
ChannelCreateEvent,
|
||||
toObject,
|
||||
ChannelType,
|
||||
Snowflake,
|
||||
trimSpecial,
|
||||
Channel,
|
||||
DMChannel,
|
||||
User,
|
||||
emitEvent
|
||||
} from "@fosscord/util";
|
||||
import { Channel, ChannelCreateEvent, ChannelType, Snowflake, trimSpecial, User, emitEvent } from "@fosscord/util";
|
||||
import { HTTPError } from "lambert-server";
|
||||
|
||||
import { DmChannelCreateSchema } from "../../../schema/Channel";
|
||||
import { check } from "../../../util/instanceOf";
|
||||
import { In } from "typeorm";
|
||||
|
||||
const router: Router = Router();
|
||||
|
||||
@@ -29,8 +19,10 @@ router.post("/", check(DmChannelCreateSchema), async (req: Request, res: Respons
|
||||
|
||||
body.recipients = body.recipients.filter((x) => x !== req.user_id).unique();
|
||||
|
||||
if (!(await Promise.all(body.recipients.map((x) => User.exists({ id: x })))).every((x) => x)) {
|
||||
throw new HTTPError("Recipient not found");
|
||||
const recipients = await User.find({ id: In(body.recipients) });
|
||||
|
||||
if (recipients.length !== body.recipients.length) {
|
||||
throw new HTTPError("Recipient/s not found");
|
||||
}
|
||||
|
||||
const type = body.recipients.length === 1 ? ChannelType.DM : ChannelType.GROUP_DM;
|
||||
@@ -40,13 +32,12 @@ router.post("/", check(DmChannelCreateSchema), async (req: Request, res: Respons
|
||||
name,
|
||||
type,
|
||||
owner_id: req.user_id,
|
||||
id: Snowflake.generate(),
|
||||
created_at: new Date(),
|
||||
last_message_id: null,
|
||||
recipient_ids: [...body.recipients, req.user_id]
|
||||
}).save();
|
||||
|
||||
await emitEvent({ event: "CHANNEL_CREATE", data: channel), user_id: req.user_id } as ChannelCreateEvent;
|
||||
await emitEvent({ event: "CHANNEL_CREATE", data: channel, user_id: req.user_id } as ChannelCreateEvent);
|
||||
|
||||
res.json(channel);
|
||||
});
|
||||
|
||||
@@ -5,12 +5,19 @@ const router = Router();
|
||||
|
||||
router.post("/", async (req: Request, res: Response) => {
|
||||
const user = await User.findOneOrFail({ id: req.user_id }); //User object
|
||||
let correctpass = true;
|
||||
|
||||
if (user.data.hash) {
|
||||
// guest accounts can delete accounts without password
|
||||
correctpass = await bcrypt.compare(req.body.password, user.data.hash); //Not sure if user typed right password :/
|
||||
}
|
||||
|
||||
// TODO: decrement guild member count
|
||||
|
||||
let correctpass = await bcrypt.compare(req.body.password, user!.data.hash); //Not sure if user typed right password :/
|
||||
if (correctpass) {
|
||||
await Promise.all([
|
||||
User.deleteOne({ id: req.user_id }), //Yeetus user deletus
|
||||
Member.deleteMany({ id: req.user_id })
|
||||
User.delete({ id: req.user_id }), //Yeetus user deletus
|
||||
Member.delete({ id: req.user_id })
|
||||
]);
|
||||
|
||||
res.sendStatus(204);
|
||||
|
||||
@@ -5,9 +5,14 @@ import bcrypt from "bcrypt";
|
||||
const router = Router();
|
||||
|
||||
router.post("/", async (req: Request, res: Response) => {
|
||||
const user = await User.findOneOrFail({ id: req.user_id }); //User object
|
||||
const user = await User.findOneOrFail(req.user_id); //User object
|
||||
let correctpass = true;
|
||||
|
||||
if (user.data.hash) {
|
||||
// guest accounts can delete accounts without password
|
||||
correctpass = await bcrypt.compare(req.body.password, user.data.hash); //Not sure if user typed right password :/
|
||||
}
|
||||
|
||||
let correctpass = await bcrypt.compare(req.body.password, user!.data.hash); //Not sure if user typed right password :/
|
||||
if (correctpass) {
|
||||
await User.update({ id: req.user_id }, { disabled: true });
|
||||
|
||||
|
||||
@@ -1,31 +1,26 @@
|
||||
import { Router, Request, Response } from "express";
|
||||
import { Guild, Member, User, GuildDeleteEvent, GuildMemberRemoveEvent, toObject, emitEvent } from "@fosscord/util";
|
||||
import { Guild, Member, User, GuildDeleteEvent, GuildMemberRemoveEvent, emitEvent } from "@fosscord/util";
|
||||
import { HTTPError } from "lambert-server";
|
||||
|
||||
import { getPublicUser } from "../../../util/User";
|
||||
import { In } from "typeorm";
|
||||
|
||||
const router: Router = Router();
|
||||
|
||||
router.get("/", async (req: Request, res: Response) => {
|
||||
const user = await User.findOneOrFail({ id: req.user_id }, { guilds: true });
|
||||
if (!user) throw new HTTPError("User not found", 404);
|
||||
const members = await Member.find({ relations: ["guild"], where: { id: req.user_id } });
|
||||
|
||||
var guildIDs = user.guilds || [];
|
||||
var guild = await Guild.find({ id: { $in: guildIDs } }).populate({ path: "joined_at", match: { id: req.user_id } });
|
||||
res.json(guild);
|
||||
res.json(members.map((x) => x.guild));
|
||||
});
|
||||
|
||||
// user send to leave a certain guild
|
||||
router.delete("/:id", async (req: Request, res: Response) => {
|
||||
const guild_id = req.params.id;
|
||||
const guild = await Guild.findOneOrFail({ id: guild_id }, { guild_id: true });
|
||||
const guild = await Guild.findOneOrFail({ where: { id: guild_id }, select: ["owner_id"] });
|
||||
|
||||
if (!guild) throw new HTTPError("Guild doesn't exist", 404);
|
||||
if (guild.owner_id === req.user_id) throw new HTTPError("You can't leave your own guild", 400);
|
||||
|
||||
await Promise.all([
|
||||
Member.deleteOne({ id: req.user_id, guild_id: guild_id }),
|
||||
User.update({ id: req.user_id }, { $pull: { guilds: guild_id } }),
|
||||
Member.delete({ id: req.user_id, guild_id: guild_id }),
|
||||
emitEvent({
|
||||
event: "GUILD_DELETE",
|
||||
data: {
|
||||
@@ -35,7 +30,7 @@ router.delete("/:id", async (req: Request, res: Response) => {
|
||||
} as GuildDeleteEvent)
|
||||
]);
|
||||
|
||||
const user = await getPublicUser(req.user_id);
|
||||
const user = await User.getPublicUser(req.user_id);
|
||||
|
||||
await emitEvent({
|
||||
event: "GUILD_MEMBER_REMOVE",
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { Router, Request, Response } from "express";
|
||||
import { User, toObject, PublicUserProjection } from "@fosscord/util";
|
||||
import { getPublicUser } from "../../../util/User";
|
||||
import { User } from "@fosscord/util";
|
||||
import { UserModifySchema } from "../../../schema/User";
|
||||
import { check } from "../../../util/instanceOf";
|
||||
import { handleFile } from "../../../util/cdn";
|
||||
@@ -8,29 +7,29 @@ import { handleFile } from "../../../util/cdn";
|
||||
const router: Router = Router();
|
||||
|
||||
router.get("/", async (req: Request, res: Response) => {
|
||||
res.json(await getPublicUser(req.user_id));
|
||||
res.json(await User.getPublicUser(req.user_id));
|
||||
});
|
||||
|
||||
const UserUpdateProjection = {
|
||||
accent_color: true,
|
||||
avatar: true,
|
||||
banner: true,
|
||||
bio: true,
|
||||
bot: true,
|
||||
discriminator: true,
|
||||
email: true,
|
||||
flags: true,
|
||||
id: true,
|
||||
locale: true,
|
||||
mfa_enabled: true,
|
||||
nsfw_alllowed: true,
|
||||
phone: true,
|
||||
public_flags: true,
|
||||
purchased_flags: true,
|
||||
// token: true, // this isn't saved in the db and needs to be set manually
|
||||
username: true,
|
||||
verified: true
|
||||
};
|
||||
const UserUpdateProjection = [
|
||||
"accent_color",
|
||||
"avatar",
|
||||
"banner",
|
||||
"bio",
|
||||
"bot",
|
||||
"discriminator",
|
||||
"email",
|
||||
"flags",
|
||||
"id",
|
||||
"locale",
|
||||
"mfa_enabled",
|
||||
"nsfw_alllowed",
|
||||
"phone",
|
||||
"public_flags",
|
||||
"purchased_flags",
|
||||
// "token", // this isn't saved in the db and needs to be set manually
|
||||
"username",
|
||||
"verified"
|
||||
];
|
||||
|
||||
router.patch("/", check(UserModifySchema), async (req: Request, res: Response) => {
|
||||
const body = req.body as UserModifySchema;
|
||||
@@ -38,7 +37,7 @@ router.patch("/", check(UserModifySchema), async (req: Request, res: Response) =
|
||||
if (body.avatar) body.avatar = await handleFile(`/avatars/${req.user_id}`, body.avatar as string);
|
||||
if (body.banner) body.banner = await handleFile(`/banners/${req.user_id}`, body.banner as string);
|
||||
|
||||
const user = await User.findOneOrFailAndUpdate({ id: req.user_id }, body, { projection: UserUpdateProjection, new: true });
|
||||
const user = await new User({ ...body }, { id: req.user_id }).save();
|
||||
// TODO: dispatch user update event
|
||||
|
||||
res.json(user);
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
import { Router, Request, Response } from "express";
|
||||
import { getPublicUser } from "../../../util/User";
|
||||
import { User } from "../../../../../util/dist";
|
||||
|
||||
const router: Router = Router();
|
||||
|
||||
router.get("/", async (req: Request, res: Response) => {
|
||||
const user = await getPublicUser(req.user_id, { data: true });
|
||||
const user = await User.getPublicUser(req.user_id, { relations: ["connected_accounts"] });
|
||||
|
||||
res.json({
|
||||
connected_accounts: user.data.connected_accounts,
|
||||
connected_accounts: user.connected_accounts,
|
||||
premium_guild_since: null, // TODO
|
||||
premium_since: null, // TODO
|
||||
user: {
|
||||
|
||||
@@ -2,11 +2,10 @@ import {
|
||||
RelationshipAddEvent,
|
||||
User,
|
||||
PublicUserProjection,
|
||||
toObject,
|
||||
RelationshipType,
|
||||
RelationshipRemoveEvent,
|
||||
UserDocument,
|
||||
emitEvent
|
||||
emitEvent,
|
||||
Relationship
|
||||
} from "@fosscord/util";
|
||||
import { Router, Response, Request } from "express";
|
||||
import { HTTPError } from "lambert-server";
|
||||
@@ -15,40 +14,36 @@ import { check, Length } from "../../../util/instanceOf";
|
||||
|
||||
const router = Router();
|
||||
|
||||
const userProjection = { "data.relationships": true, ...PublicUserProjection };
|
||||
const userProjection: (keyof User)[] = ["relationships", ...PublicUserProjection];
|
||||
|
||||
router.get("/", async (req: Request, res: Response) => {
|
||||
const user = await User.findOneOrFail({ id: req.user_id }, { data: { relationships: true } }).populate({
|
||||
path: "data.relationships.id",
|
||||
model: User
|
||||
});
|
||||
return res.json(user.data.relationships);
|
||||
const user = await User.findOneOrFail({ where: { id: req.user_id }, select: ["relationships"] });
|
||||
|
||||
return res.json(user.relationships);
|
||||
});
|
||||
|
||||
async function addRelationship(req: Request, res: Response, friend: UserDocument, type: RelationshipType) {
|
||||
async function updateRelationship(req: Request, res: Response, friend: User, type: RelationshipType) {
|
||||
const id = friend.id;
|
||||
if (id === req.user_id) throw new HTTPError("You can't add yourself as a friend");
|
||||
|
||||
const user = await User.findOneOrFail({ id: req.user_id }, userProjection);
|
||||
const newUserRelationships = [...user.data.relationships];
|
||||
const newFriendRelationships = [...friend.data.relationships];
|
||||
const user = await User.findOneOrFail(req.user_id, { relations: ["relationships"], select: userProjection });
|
||||
|
||||
var relationship = newUserRelationships.find((x) => x.id === id);
|
||||
const friendRequest = newFriendRelationships.find((x) => x.id === req.user_id);
|
||||
var relationship = user.relationships.find((x) => x.id === id);
|
||||
const friendRequest = friend.relationships.find((x) => x.id === req.user_id);
|
||||
|
||||
if (type === RelationshipType.blocked) {
|
||||
if (relationship) {
|
||||
if (relationship.type === RelationshipType.blocked) throw new HTTPError("You already blocked the user");
|
||||
relationship.type = RelationshipType.blocked;
|
||||
} else {
|
||||
relationship = { id, type: RelationshipType.blocked };
|
||||
newUserRelationships.push(relationship);
|
||||
relationship = new Relationship({ id, type: RelationshipType.blocked });
|
||||
user.relationships.push(relationship);
|
||||
}
|
||||
|
||||
if (friendRequest && friendRequest.type !== RelationshipType.blocked) {
|
||||
newFriendRelationships.remove(friendRequest);
|
||||
friend.relationships.remove(friendRequest);
|
||||
await Promise.all([
|
||||
User.update({ id: friend.id }, { "data.relationships": newFriendRelationships }),
|
||||
user.save(),
|
||||
emitEvent({
|
||||
event: "RELATIONSHIP_REMOVE",
|
||||
data: friendRequest,
|
||||
@@ -58,12 +53,12 @@ async function addRelationship(req: Request, res: Response, friend: UserDocument
|
||||
}
|
||||
|
||||
await Promise.all([
|
||||
User.update({ id: req.user_id }, { "data.relationships": newUserRelationships }),
|
||||
user.save(),
|
||||
emitEvent({
|
||||
event: "RELATIONSHIP_ADD",
|
||||
data: {
|
||||
...relationship,
|
||||
user: { ...friend, data: undefined }
|
||||
user: { ...friend }
|
||||
},
|
||||
user_id: req.user_id
|
||||
} as RelationshipAddEvent)
|
||||
@@ -72,32 +67,31 @@ async function addRelationship(req: Request, res: Response, friend: UserDocument
|
||||
return res.sendStatus(204);
|
||||
}
|
||||
|
||||
var incoming_relationship = { id: req.user_id, nickname: undefined, type: RelationshipType.incoming };
|
||||
var outgoing_relationship = { id, nickname: undefined, type: RelationshipType.outgoing };
|
||||
var incoming_relationship = new Relationship({ nickname: undefined, type: RelationshipType.incoming }, { id: req.user_id });
|
||||
var outgoing_relationship = new Relationship({ nickname: undefined, type: RelationshipType.outgoing }, { id });
|
||||
|
||||
if (friendRequest) {
|
||||
if (friendRequest.type === RelationshipType.blocked) throw new HTTPError("The user blocked you");
|
||||
// accept friend request
|
||||
// @ts-ignore
|
||||
incoming_relationship = friendRequest;
|
||||
incoming_relationship.type = RelationshipType.friends;
|
||||
outgoing_relationship.type = RelationshipType.friends;
|
||||
} else newFriendRelationships.push(incoming_relationship);
|
||||
} else friend.relationships.push(incoming_relationship);
|
||||
|
||||
if (relationship) {
|
||||
if (relationship.type === RelationshipType.outgoing) throw new HTTPError("You already sent a friend request");
|
||||
if (relationship.type === RelationshipType.blocked) throw new HTTPError("Unblock the user before sending a friend request");
|
||||
if (relationship.type === RelationshipType.friends) throw new HTTPError("You are already friends with the user");
|
||||
} else newUserRelationships.push(outgoing_relationship);
|
||||
} else user.relationships.push(outgoing_relationship);
|
||||
|
||||
await Promise.all([
|
||||
User.update({ id: req.user_id }, { "data.relationships": newUserRelationships }),
|
||||
User.update({ id: friend.id }, { "data.relationships": newFriendRelationships }),
|
||||
user.save(),
|
||||
friend.save(),
|
||||
emitEvent({
|
||||
event: "RELATIONSHIP_ADD",
|
||||
data: {
|
||||
...outgoing_relationship,
|
||||
user: { ...friend, data: undefined }
|
||||
user: { ...friend }
|
||||
},
|
||||
user_id: req.user_id
|
||||
} as RelationshipAddEvent),
|
||||
@@ -106,7 +100,7 @@ async function addRelationship(req: Request, res: Response, friend: UserDocument
|
||||
data: {
|
||||
...incoming_relationship,
|
||||
should_notify: true,
|
||||
user: { ...user, data: undefined }
|
||||
user: { ...user }
|
||||
},
|
||||
user_id: id
|
||||
} as RelationshipAddEvent)
|
||||
@@ -116,14 +110,23 @@ async function addRelationship(req: Request, res: Response, friend: UserDocument
|
||||
}
|
||||
|
||||
router.put("/:id", check({ $type: new Length(Number, 1, 4) }), async (req: Request, res: Response) => {
|
||||
return await addRelationship(req, res, await User.findOneOrFail({ id: req.params.id }), req.body.type);
|
||||
return await updateRelationship(
|
||||
req,
|
||||
res,
|
||||
await User.findOneOrFail(req.params.id, { relations: ["relationships"], select: userProjection }),
|
||||
req.body.type
|
||||
);
|
||||
});
|
||||
|
||||
router.post("/", check({ discriminator: String, username: String }), async (req: Request, res: Response) => {
|
||||
return await addRelationship(
|
||||
return await updateRelationship(
|
||||
req,
|
||||
res,
|
||||
await User.findOneOrFail(req.body as { discriminator: string; username: string }),
|
||||
await User.findOneOrFail({
|
||||
relations: ["relationships"],
|
||||
select: userProjection,
|
||||
where: req.body as { discriminator: string; username: string }
|
||||
}),
|
||||
req.body.type
|
||||
);
|
||||
});
|
||||
@@ -132,17 +135,15 @@ router.delete("/:id", async (req: Request, res: Response) => {
|
||||
const { id } = req.params;
|
||||
if (id === req.user_id) throw new HTTPError("You can't remove yourself as a friend");
|
||||
|
||||
const user = await User.findOneOrFail({ id: req.user_id });
|
||||
if (!user) throw new HTTPError("Invalid token", 400);
|
||||
const user = await User.findOneOrFail(req.user_id, { select: userProjection, relations: ["relationships"] });
|
||||
const friend = await User.findOneOrFail(id, { select: userProjection, relations: ["relationships"] });
|
||||
|
||||
const friend = await User.findOneOrFail({ id }, userProjection);
|
||||
if (!friend) throw new HTTPError("User not found", 404);
|
||||
const relationship = user.relationships.find((x) => x.id === id);
|
||||
const friendRequest = friend.relationships.find((x) => x.id === req.user_id);
|
||||
|
||||
const relationship = user.data.relationships.find((x) => x.id === id);
|
||||
const friendRequest = friend.data.relationships.find((x) => x.id === req.user_id);
|
||||
if (relationship?.type === RelationshipType.blocked) {
|
||||
// unblock user
|
||||
user.data.relationships.remove(relationship);
|
||||
user.relationships.remove(relationship);
|
||||
|
||||
await Promise.all([
|
||||
user.save(),
|
||||
@@ -153,8 +154,8 @@ router.delete("/:id", async (req: Request, res: Response) => {
|
||||
if (!relationship || !friendRequest) throw new HTTPError("You are not friends with the user", 404);
|
||||
if (friendRequest.type === RelationshipType.blocked) throw new HTTPError("The user blocked you");
|
||||
|
||||
user.data.relationships.remove(relationship);
|
||||
friend.data.relationships.remove(friendRequest);
|
||||
user.relationships.remove(relationship);
|
||||
friend.relationships.remove(friendRequest);
|
||||
|
||||
await Promise.all([
|
||||
user.save(),
|
||||
|
||||
Reference in New Issue
Block a user