Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Commit

Permalink
Fix database usage in various places, ignore v2 folders
Browse files Browse the repository at this point in the history
  • Loading branch information
emilianya committed Jan 28, 2024
1 parent 53c0020 commit c48969d
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 18 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,6 @@ dist/**/*
.sentryclirc

temp/**/*
!temp/.gitkeep
!temp/.gitkeep

src/routes/v2/
13 changes: 7 additions & 6 deletions src/classes/permissions/PermissionManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Permission from "./Permission.js";
import db from "../../db.js";
import Database from "../../db.js";
const database = new Database()
import {isValidObjectId, Types} from "mongoose";
import {
ConstantID_DenyAssignment, ConstantID_DenyRole, ConstantID_DMvQuark,
Expand Down Expand Up @@ -154,14 +155,14 @@ export default class PermissionManager {
if (scope?.quarkId && !isValidObjectId(scope.quarkId)) throw new Error(`PM: Invalid quark ID ${scope.quarkId}`);

// If scope is a channel, and no quark id was provided, get the quark id from the channel
let Quarks = db.getQuarks();
let Quarks = database.Quarks;
let scopeId = scope.scopeId;
let quarkId = scope.scopeId;
let quark
if (scope.scopeType === "channel" && !scope.quarkId) {
quark = await Quarks.findOne({channels: new Types.ObjectId(scope.scopeId)});
if (!quark) {
let Channels = db.getChannels()
let Channels = database.Channels
let dmChannel = await Channels.findOne({quark: ConstantID_DMvQuark, _id: scope.scopeId})
if (!dmChannel) throw new Error(`PM: Channel ${scope.scopeId} does not exist.`);
quarkId = ConstantID_DMvQuark;
Expand Down Expand Up @@ -224,10 +225,10 @@ export default class PermissionManager {
}

// Find all permission assignments that affect the user in this scope, both channel and quark level
let PermissionAssignments = db.getPermissionAssignments();
let RoleAssignments = db.getRoleAssignments();
let PermissionAssignments = database.PermissionAssignments;
let RoleAssignments = database.RoleAssignments;
let userRoles = await RoleAssignments.find({ user: userId, quark: quarkId }).distinct("role");
let Roles = db.getRoles();
let Roles = database.Roles;
let defaultRoles = await Roles.find({ isDefault: true, quark: quarkId }).distinct("_id");
const permissionAssignments = await PermissionAssignments.find({
$or: [
Expand Down
4 changes: 0 additions & 4 deletions src/routes/v1/auth.ts

This file was deleted.

7 changes: 4 additions & 3 deletions src/routes/v1/gateway.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { WebSocketServer } from "ws";
import { WsAuth } from "./auth.js";
import {WsAuth} from "../../util/Auth.js";
import crypto from "crypto";
import SubscriptionManager from "../../classes/SubscriptionManager.js";
import EventEmitter from "events";
import db from "../../db.js";
const database = new Database()
import {checkPermittedChannel} from "../../util/PermissionMiddleware.js";
import {ConstantID_DMvQuark, ConstantID_SystemUser} from "../../util/ConstantID.js";
import Database from "../../db.js";

// Create a new Subscription Manager and export it
const sm = new SubscriptionManager();
Expand Down Expand Up @@ -115,7 +116,7 @@ async function handleMessage(message, ws, user, socketId) {
ws.send(JSON.stringify({eventId: "error", message: "You are not permitted to subscribe to this event", code: 403, event}));
}
} else if (event.split("_")[0] === "quark") {
let Quarks = db.getQuarks();
let Quarks = database.Quarks;
try {
let quark = await Quarks.findOne({_id: event.split("_")[1], members: user._id});
if (ConstantID_DMvQuark.equals(event.split("_")[1]) || event.split("_")[1] === "dm") {
Expand Down
9 changes: 5 additions & 4 deletions src/util/getNickname.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import db from "../db.js";
import Database from "../db.js";
const db = new Database()

/**
* Get a user's nickname
Expand All @@ -7,13 +8,13 @@ import db from "../db.js";
* @returns {Promise<string>} Nickname
*/
export async function getNick(userId, quarkId = null) {
let Nick = db.getNicks();
let Nick = db.Nicks;
let nicks = await Nick.find({userId: userId});

// Find global nick if present, or fallback to username
let globalNick = nicks.find((nick) => nick.scope === "global");
if (!globalNick) {
let LoginUser = db.getLoginUsers();
let LoginUser = db.LoginUsers;
let user = await LoginUser.findOne({_id: userId})
globalNick = {nickname: user.username}
}
Expand All @@ -27,7 +28,7 @@ export async function getNick(userId, quarkId = null) {

export async function getNickBulk(userIds, quarkId = null) {
//console.time("getNickBulk");
let Nick = db.getNicks();
let Nick = db.Nicks;
let nicks = await Nick.find({userId: {$in: userIds}});

let nicknames = userIds.map(userId => {
Expand Down

1 comment on commit c48969d

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes deployed to server
Branch dev

Please sign in to comment.