Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating IUser schema #735

Merged
merged 4 commits into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/gameplay/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ class Game extends Room {
// assign them the sockets but with shuffled.
this.playersInGame[i].username =
this.socketsOfPlayers[i].request.user.username;
this.playersInGame[i].userId = this.socketsOfPlayers[i].request.user._id;
this.playersInGame[i].userId = this.socketsOfPlayers[i].request.user.id;

this.playersInGame[i].request = this.socketsOfPlayers[i].request;

Expand Down
3 changes: 2 additions & 1 deletion src/gameplay/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface See {
}

export interface IUser {
id: string;
username: string;
usernameLower?: string;
password?: string;
Expand Down Expand Up @@ -75,7 +76,7 @@ export interface IUser {
matchmakingBlacklist?: string[];

// Mongoose methods
save: () => Promise<void>;
save: () => Promise<this>;
markModified: (path: string) => void;
}

Expand Down
6 changes: 3 additions & 3 deletions src/routes/forum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ router.get(
if (!found.whoLikedId) found.whoLikedId = [];

// person has already liked it, so unlike it and remove their name
if (found.whoLikedId.includes(req.user._id)) {
const i = found.whoLikedId.indexOf(req.user._id);
if (found.whoLikedId.includes(req.user.id)) {
const i = found.whoLikedId.indexOf(req.user.id);
// remove their id
found.whoLikedId.splice(i, 1);
found.likes -= 1;
res.status(200).send('unliked');
} else {
// add a like
found.whoLikedId.push(req.user._id);
found.whoLikedId.push(req.user.id);
found.likes += 1;
res.status(200).send('liked');

Expand Down
2 changes: 1 addition & 1 deletion src/routes/forum/forumThreadCommentReplyRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ async function createCommentReply(req, res) {
allowedAttributes: sanitizeHtmlAllowedAttributesForumThread,
}),

author: { id: req.user._id, username: req.user.username },
author: { id: req.user.id, username: req.user.username },

timeCreated: d,
timeLastEdit: d,
Expand Down
2 changes: 1 addition & 1 deletion src/routes/forum/forumThreadCommentRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ router.post(
allowedTags: sanitizeHtml.defaults.allowedTags.concat(allowedHtmlTags),
allowedAttributes: allowedHtmlAttributes,
}),
author: { id: req.user._id, username: req.user.username },
author: { id: req.user.id, username: req.user.username },
timeCreated: new Date(),
timeLastEdit: new Date(),
likes: 0,
Expand Down
4 changes: 2 additions & 2 deletions src/routes/forum/forumThreadRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ router.get(
});
});

const userIdString = req.user._id.toString().replace(' ', '');
const userIdString = req.user.id.toString().replace(' ', '');

const idsOfLikedPosts = [];
const addToLikedPosts = (item) => {
Expand Down Expand Up @@ -189,7 +189,7 @@ router.post(
timeLastEdit: new Date(),
whoLastEdit: req.user.username,
author: {
id: req.user._id,
id: req.user.id,
username: req.user.username,
},
comments: [],
Expand Down
2 changes: 1 addition & 1 deletion src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,7 @@ router.get('/ajax/hideNotification', (req, res) => {
router.get('/ajax/hideAllNotifications', (req, res) => {
// console.log("hide all nofications");

User.findById(req.user._id)
User.findById(req.user.id)
.populate('notifications')
.exec(async (err, foundUser) => {
if (err) {
Expand Down
10 changes: 5 additions & 5 deletions src/routes/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export const isLoggedIn = asyncMiddleware(async (req, res, next) => {
res.locals.bansChecked = true;
// Check bans
const ban = await Ban.findOne({
'bannedPlayer.id': user._id, // User ID match
'bannedPlayer.id': user.id, // User ID match
whenRelease: { $gt: new Date() }, // Unexpired ban
userBan: true, // User ban
disabled: false, // Ban must be active
Expand Down Expand Up @@ -158,7 +158,7 @@ const checkOwnership = (name, model, query, isOwner) => [
next();
} else {
console.log(
`${req.user._id} ${req.user.username} has attempted to do something bad`,
`${req.user.id} ${req.user.username} has attempted to do something bad`,
);
req.flash('error', 'You are not the owner!');
res.redirect('back');
Expand All @@ -181,7 +181,7 @@ export const checkForumThreadOwnership = checkOwnership(
(req) => ({
_id: req.params.id,
}),
(req, thread) => thread.author.id && thread.author.id.equals(req.user._id),
(req, thread) => thread.author.id && thread.author.id.equals(req.user.id),
);

export const checkForumThreadCommentOwnership = checkOwnership(
Expand All @@ -190,7 +190,7 @@ export const checkForumThreadCommentOwnership = checkOwnership(
(req) => ({
_id: req.params.comment_id,
}),
(req, comment) => comment.author.id && comment.author.id.equals(req.user._id),
(req, comment) => comment.author.id && comment.author.id.equals(req.user.id),
);

export const checkForumThreadCommentReplyOwnership = checkOwnership(
Expand All @@ -199,7 +199,7 @@ export const checkForumThreadCommentReplyOwnership = checkOwnership(
(req) => ({
_id: req.params.reply_id,
}),
(req, reply) => reply.author.id && reply.author.id.equals(req.user._id),
(req, reply) => reply.author.id && reply.author.id.equals(req.user.id),
);

export const isModMiddleware = (req, res, next) => {
Expand Down
26 changes: 16 additions & 10 deletions src/routes/mod.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ router.post('/ban', async (req, res) => {
const userIsPercy = isPercival(req.user.username);

if ((userIsMod ^ userIsPercy) !== 1) {
throw Error("Expected requesting user to either be a mod or a Percy.");
throw Error('Expected requesting user to either be a mod or a Percy.');
}

try {
Expand Down Expand Up @@ -186,13 +186,13 @@ router.post('/ban', async (req, res) => {
singleIPBan: req.body['SingleIPBanCheckbox'] === 'on' ? true : false,
userBan: req.body['userBanCheckbox'] === 'on' ? true : false,
bannedPlayer: {
id: banUser._id,
id: banUser.id,
username: banUser.username,
usernameLower: banUser.usernameLower,
},
bannedIPs: ipsToBan,
modWhoBanned: {
id: modUser._id,
id: modUser.id,
username: modUser.username,
usernameLower: modUser.usernameLower,
},
Expand All @@ -210,18 +210,24 @@ router.post('/ban', async (req, res) => {
await ModLog.create({
type: 'ban',
modWhoMade: {
id: modUser._id,
id: modUser.id,
username: modUser.username,
usernameLower: modUser.usernameLower,
},
data: banData,
dateCreated: new Date(),
});

sendToDiscordMods(`${userIsMod ? "Moderator" : "Percival"} "${req.user.usernameLower}" banned "${banUser.usernameLower}" for \
${req.body['duration']} ${req.body['duration_units']} for reason "${req.body.reason}" with description \
"${req.body['descriptionByMod']}".`
, false);
sendToDiscordMods(
`${userIsMod ? 'Moderator' : 'Percival'} "${
req.user.usernameLower
}" banned "${banUser.usernameLower}" for \
${req.body['duration']} ${req.body['duration_units']} for reason "${
req.body.reason
}" with description \
"${req.body['descriptionByMod']}".`,
false,
);

// Delete all the sessions associated with this username
const dbResult = await MongoClient.connect(process.env.DATABASEURL);
Expand Down Expand Up @@ -355,10 +361,10 @@ router.post('/report', async (req, res) => {
reason: req.body.reason,
reportedPlayer: {
username: reportedUser.username.toLowerCase(),
id: reportedUser._id,
id: reportedUser.id,
},
playerWhoReported: {
id: userWhoReported._id,
id: userWhoReported.id,
username: userWhoReported.username.toLowerCase(),
},
description: req.body.desc,
Expand Down
8 changes: 4 additions & 4 deletions src/routes/profile/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ router.post('/mod/deleteuseravatar', isModMiddleware, async (req, res) => {
await ModLog.create({
type: 'avatarDelete',
modWhoMade: {
id: modWhoProcessed._id,
id: modWhoProcessed.id,
username: modWhoProcessed.username,
usernameLower: modWhoProcessed.usernameLower,
},
Expand Down Expand Up @@ -329,7 +329,7 @@ router.post(

let str = `Your avatar request was approved by ${modWhoProcessed.username}! Their comment was: "${modComment}"`;
createNotification(
userRequestingAvatar._id,
userRequestingAvatar.id,
str,
'#',
modWhoProcessed.username,
Expand All @@ -342,7 +342,7 @@ router.post(

let str = `Your avatar request was rejected by ${modWhoProcessed.username}. Their comment was: "${modComment}"`;
createNotification(
userRequestingAvatar._id,
userRequestingAvatar.id,
str,
'#',
modWhoProcessed.username,
Expand All @@ -353,7 +353,7 @@ router.post(
ModLog.create({
type: 'avatar',
modWhoMade: {
id: modWhoProcessed._id,
id: modWhoProcessed.id,
username: modWhoProcessed.username,
usernameLower: modWhoProcessed.usernameLower,
},
Expand Down
2 changes: 1 addition & 1 deletion src/sockets/commands/mod/miplinkedaccs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export const miplinkedaccs: Command = {
ModLog.create({
type: 'miplinkedaccs',
modWhoMade: {
id: modUser._id,
id: modUser.id,
username: modUser.username,
usernameLower: modUser.usernameLower,
},
Expand Down
4 changes: 2 additions & 2 deletions src/sockets/commands/mod/mnotify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const mnotify: Command = {
classStr: 'server-text',
});
} else if (foundUser) {
const userIdTarget = foundUser._id;
const userIdTarget = foundUser.id;
const stringToSay = str;
const link = '#';

Expand All @@ -43,7 +43,7 @@ export const mnotify: Command = {
},
data: {
targetUser: {
id: foundUser._id,
id: foundUser.id,
username: foundUser.username,
usernameLower: foundUser.usernameLower,
},
Expand Down
2 changes: 1 addition & 1 deletion src/sockets/commands/mod/munban.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const munban: Command = {
ModLog.create({
type: 'munban',
modWhoMade: {
id: modUser._id,
id: modUser.id,
username: modUser.username,
usernameLower: modUser.usernameLower,
},
Expand Down