channel recipients

This commit is contained in:
Flam3rboy 2021-08-31 17:58:28 +02:00
parent 9400fc8fb8
commit d073c52f29
4 changed files with 39 additions and 24 deletions

View File

@ -5,13 +5,14 @@ import { HTTPError } from "lambert-server";
import { DmChannelCreateSchema } from "../../../schema/Channel"; import { DmChannelCreateSchema } from "../../../schema/Channel";
import { check } from "../../../util/instanceOf"; import { check } from "../../../util/instanceOf";
import { In } from "typeorm"; import { In } from "typeorm";
import { Recipient } from "../../../../../util/dist/entities/Recipient";
const router: Router = Router(); const router: Router = Router();
router.get("/", async (req: Request, res: Response) => { router.get("/", async (req: Request, res: Response) => {
var channels = await Channel.find({ recipient_ids: req.user_id }); const recipients = await Recipient.find({ where: { id: req.user_id }, relations: ["channel"] });
res.json(channels); res.json(recipients.map((x) => x.channel));
}); });
router.post("/", check(DmChannelCreateSchema), async (req: Request, res: Response) => { router.post("/", check(DmChannelCreateSchema), async (req: Request, res: Response) => {
@ -34,7 +35,7 @@ router.post("/", check(DmChannelCreateSchema), async (req: Request, res: Respons
owner_id: req.user_id, owner_id: req.user_id,
created_at: new Date(), created_at: new Date(),
last_message_id: null, last_message_id: null,
recipient_ids: [...body.recipients, req.user_id] recipients: [...body.recipients.map((x) => new Recipient({ id: x })), new Recipient({ id: req.user_id })]
}).save(); }).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);

View File

@ -55,7 +55,7 @@
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",
"sqlite3": "^5.0.2", "sqlite3": "^5.0.2",
"typeorm": "^0.2.37", "typeorm": "^0.2.37",
"typescript": "^4.3.5", "typescript": "^4.4.2",
"typescript-json-schema": "^0.50.1" "typescript-json-schema": "^0.50.1"
}, },
"devDependencies": { "devDependencies": {
@ -1949,7 +1949,7 @@
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",
"sqlite3": "^5.0.2", "sqlite3": "^5.0.2",
"typeorm": "^0.2.37", "typeorm": "^0.2.37",
"typescript": "^4.3.5", "typescript": "^4.4.2",
"typescript-json-schema": "^0.50.1" "typescript-json-schema": "^0.50.1"
} }
}, },

View File

@ -14,7 +14,8 @@ import { Send } from "../util/Send";
import WebSocket from "../util/WebSocket"; import WebSocket from "../util/WebSocket";
import "missing-native-js-functions"; import "missing-native-js-functions";
import { Channel as AMQChannel } from "amqplib"; import { Channel as AMQChannel } from "amqplib";
import { In } from "../../../util/node_modules/typeorm"; import { In, Like } from "../../../util/node_modules/typeorm";
import { Recipient } from "../../../util/dist/entities/Recipient";
// TODO: close connection on Invalidated Token // TODO: close connection on Invalidated Token
// TODO: check intent // TODO: check intent
@ -28,10 +29,9 @@ export async function setupListener(this: WebSocket) {
const members = await Member.find({ where: { id: this.user_id } }); const members = await Member.find({ where: { id: this.user_id } });
const guild_ids = members.map((x) => x.guild_id); const guild_ids = members.map((x) => x.guild_id);
const user = await User.findOneOrFail({ id: this.user_id }); const user = await User.findOneOrFail({ id: this.user_id });
const channels = await Channel.find({ const recipients = await Recipient.find({ where: { id: this.user_id }, relations: ["channel"] });
where: [{ recipient_ids: this.user_id }, { guild_id: In(guild_ids) }], const channels = await Channel.find({ guild_id: In(guild_ids) });
}); const dm_channels = recipients.map((x) => x.channel);
const dm_channels = channels.filter((x) => !x.guild_id);
const guild_channels = channels.filter((x) => x.guild_id); const guild_channels = channels.filter((x) => x.guild_id);
const opts: { acknowledge: boolean; channel?: AMQChannel } = { acknowledge: true }; const opts: { acknowledge: boolean; channel?: AMQChannel } = { acknowledge: true };

View File

@ -1,12 +1,25 @@
import { CLOSECODES, Payload, OPCODES } from "../util/Constants"; import { CLOSECODES, Payload, OPCODES } from "../util/Constants";
import WebSocket from "../util/WebSocket"; import WebSocket from "../util/WebSocket";
import { Channel, checkToken, Guild, Intents, Member, ReadyEventData, User, EVENTEnum, Config } from "@fosscord/util"; import {
Channel,
checkToken,
Guild,
Intents,
Member,
ReadyEventData,
User,
EVENTEnum,
Config,
dbConnection,
} from "@fosscord/util";
import { setupListener } from "../listener/listener"; import { setupListener } from "../listener/listener";
import { IdentifySchema } from "../schema/Identify"; import { IdentifySchema } from "../schema/Identify";
import { Send } from "../util/Send"; import { Send } from "../util/Send";
// import experiments from "./experiments.json"; // import experiments from "./experiments.json";
const experiments: any = []; const experiments: any = [];
import { check } from "./instanceOf"; import { check } from "./instanceOf";
import { Like } from "../../../util/node_modules/typeorm";
import { Recipient } from "../../../util/dist/entities/Recipient";
// TODO: bot sharding // TODO: bot sharding
// TODO: check priviliged intents // TODO: check priviliged intents
@ -42,17 +55,21 @@ export async function onIdentify(this: WebSocket, data: Payload) {
} }
} }
const members = await Member.find({ where: { id: this.user_id }, relations: ["guild"] }); const members = await Member.find({
where: { id: this.user_id },
relations: ["guild", "guild.channels", "guild.emojis", "guild.roles", "guild.stickers", "user", "roles"],
});
const merged_members = members.map((x: any) => { const merged_members = members.map((x: any) => {
const y = { ...x, user_id: x.id }; return [x];
delete y.settings;
delete y.id;
return [y];
}) as Member[][]; }) as Member[][];
const guilds = members.map((x) => x.guild); const guilds = members.map((x) => ({ ...x.guild, joined_at: x.joined_at }));
const user_guild_settings_entries = members.map((x) => x.settings); const user_guild_settings_entries = members.map((x) => x.settings);
const channels = await Channel.find({ where: { recipient_ids: this.user_id } }); const recipients = await Recipient.find({
where: { id: this.user_id },
relations: ["channel", "channel.recipients"],
});
const channels = recipients.map((x) => x.channel);
const user = await User.findOneOrFail({ id: this.user_id }); const user = await User.findOneOrFail({ id: this.user_id });
if (!user) return this.close(CLOSECODES.Authentication_failed); if (!user) return this.close(CLOSECODES.Authentication_failed);
@ -63,6 +80,7 @@ export async function onIdentify(this: WebSocket, data: Payload) {
public_flags: user.public_flags, public_flags: user.public_flags,
avatar: user.avatar, avatar: user.avatar,
bot: user.bot, bot: user.bot,
bio: user.bio,
}; };
const privateUser = { const privateUser = {
@ -116,11 +134,7 @@ export async function onIdentify(this: WebSocket, data: Payload) {
partial: false, // TODO partial partial: false, // TODO partial
version: 642, version: 642,
}, },
// @ts-ignore private_channels: channels,
private_channels: channels.map((x): ChannelDocument => {
delete x.recipients;
return x;
}),
session_id: "", // TODO session_id: "", // TODO
analytics_token: "", // TODO analytics_token: "", // TODO
connected_accounts: [], // TODO connected_accounts: [], // TODO
@ -134,7 +148,7 @@ export async function onIdentify(this: WebSocket, data: Payload) {
// @ts-ignore // @ts-ignore
experiments: experiments, // TODO experiments: experiments, // TODO
guild_join_requests: [], // TODO what is this? guild_join_requests: [], // TODO what is this?
users: [public_user, ...channels.map((x: any) => x.recipients).flat()].unique(), // TODO users: [public_user].unique(), // TODO
merged_members: merged_members, merged_members: merged_members,
// shard // TODO: only for bots sharding // shard // TODO: only for bots sharding
// application // TODO for applications // application // TODO for applications