forked from administration/panel
198 lines
4.3 KiB
TypeScript
198 lines
4.3 KiB
TypeScript
"use server";
|
|
|
|
import { Filter, MongoClient } from "mongodb";
|
|
import type {
|
|
AccountStrike,
|
|
Bot,
|
|
Channel,
|
|
Message,
|
|
Report,
|
|
Server,
|
|
SnapshotContent,
|
|
User,
|
|
} from "revolt-api";
|
|
import { ulid } from "ulid";
|
|
import { publishMessage } from "./redis";
|
|
|
|
let client: MongoClient;
|
|
|
|
function mongo() {
|
|
if (!client) {
|
|
client = new MongoClient(process.env.MONGODB!);
|
|
}
|
|
|
|
return client;
|
|
}
|
|
|
|
export default mongo;
|
|
|
|
export async function fetchUserById(id: string) {
|
|
return await mongo()
|
|
.db("revolt")
|
|
.collection<User>("users")
|
|
.findOne({ _id: id });
|
|
}
|
|
|
|
export async function fetchUsersById(ids: string[]) {
|
|
return await mongo()
|
|
.db("revolt")
|
|
.collection<User>("users")
|
|
.find({ _id: { $in: ids } })
|
|
.toArray();
|
|
}
|
|
|
|
export async function fetchChannelById(id: string) {
|
|
return await mongo()
|
|
.db("revolt")
|
|
.collection<Channel>("channels")
|
|
.findOne({ _id: id });
|
|
}
|
|
|
|
export async function updateLastMessageId(
|
|
channelId: string,
|
|
messageId: string
|
|
) {
|
|
return await mongo()
|
|
.db("revolt")
|
|
.collection<Channel>("channels")
|
|
.updateOne({ _id: channelId }, { $set: { last_message_id: messageId } });
|
|
}
|
|
|
|
export async function findDM(user_a: string, user_b: string) {
|
|
return await mongo()
|
|
.db("revolt")
|
|
.collection<Channel & { channel_type: "DirectMessage" }>("channels")
|
|
.findOne({
|
|
channel_type: "DirectMessage",
|
|
recipients: {
|
|
$all: [user_a, user_b],
|
|
},
|
|
});
|
|
}
|
|
|
|
export async function createDM(
|
|
userA: string,
|
|
userB: string,
|
|
lastMessageId?: string
|
|
) {
|
|
const newChannel: Channel & { channel_type: "DirectMessage" } = {
|
|
_id: ulid(),
|
|
channel_type: "DirectMessage",
|
|
active: typeof lastMessageId !== "undefined",
|
|
recipients: [userA, userB],
|
|
};
|
|
|
|
await mongo()
|
|
.db("revolt")
|
|
.collection<Channel & { channel_type: "DirectMessage" }>("channels")
|
|
.insertOne(newChannel);
|
|
|
|
await publishMessage(userA + "!", {
|
|
type: "ChannelCreate",
|
|
...newChannel,
|
|
});
|
|
|
|
await publishMessage(userB + "!", {
|
|
type: "ChannelCreate",
|
|
...newChannel,
|
|
});
|
|
|
|
return newChannel;
|
|
}
|
|
|
|
export async function fetchServerById(id: string) {
|
|
return await mongo()
|
|
.db("revolt")
|
|
.collection<Server>("servers")
|
|
.findOne({ _id: id });
|
|
}
|
|
|
|
export async function fetchServers(query: Filter<Server>) {
|
|
return await mongo()
|
|
.db("revolt")
|
|
.collection<Server>("servers")
|
|
.find(query)
|
|
.toArray();
|
|
}
|
|
|
|
export async function fetchMessageById(id: string) {
|
|
return await mongo()
|
|
.db("revolt")
|
|
.collection<Message>("messages")
|
|
.findOne({ _id: id });
|
|
}
|
|
|
|
export async function fetchMessages(query: Filter<Message>) {
|
|
return await mongo()
|
|
.db("revolt")
|
|
.collection<Message>("messages")
|
|
.find(query, { sort: { _id: -1 } })
|
|
.toArray();
|
|
}
|
|
|
|
export async function fetchReports(
|
|
query: Filter<Report> = { status: "Created" }
|
|
) {
|
|
return await mongo()
|
|
.db("revolt")
|
|
.collection<Report>("safety_reports")
|
|
.find(query as never, {
|
|
sort: {
|
|
_id: -1,
|
|
},
|
|
})
|
|
.toArray();
|
|
}
|
|
|
|
export async function fetchReportById(id: string) {
|
|
return await mongo()
|
|
.db("revolt")
|
|
.collection<Report>("safety_reports")
|
|
.findOne({ _id: id });
|
|
}
|
|
|
|
export async function fetchMembershipsByUser(userId: string) {
|
|
return await mongo()
|
|
.db("revolt")
|
|
.collection<{ _id: { user: string; server: string } }>("server_members")
|
|
.find({ "_id.user": userId })
|
|
.toArray();
|
|
}
|
|
|
|
export async function fetchSnapshots(
|
|
query: Filter<{ _id: string; report_id: string; content: SnapshotContent }>
|
|
) {
|
|
return await mongo()
|
|
.db("revolt")
|
|
.collection<{ _id: string; report_id: string; content: SnapshotContent }>(
|
|
"safety_snapshots"
|
|
)
|
|
.find(query)
|
|
.toArray();
|
|
}
|
|
|
|
export async function fetchSnapshotsByReport(reportId: string) {
|
|
return await mongo()
|
|
.db("revolt")
|
|
.collection<{ content: SnapshotContent }>("safety_snapshots")
|
|
.find({ report_id: reportId })
|
|
.toArray()
|
|
.then((snapshots) => snapshots.map((snapshot) => snapshot!.content));
|
|
}
|
|
|
|
export async function fetchStrikesByUser(userId: string) {
|
|
return await mongo()
|
|
.db("revolt")
|
|
.collection<AccountStrike>("safety_strikes")
|
|
.find({ user_id: userId })
|
|
.toArray();
|
|
}
|
|
|
|
export async function fetchBotsByUser(userId: string) {
|
|
return await mongo()
|
|
.db("revolt")
|
|
.collection<Bot>("bots")
|
|
.find({ owner: userId })
|
|
.toArray();
|
|
}
|