This repository has been archived by the owner on Oct 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
You can now send and accept friend requests
- Loading branch information
Showing
5 changed files
with
121 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import express from "express"; | ||
import Auth from "../../../util/Auth.js"; | ||
import FeatureFlag from "../../../util/FeatureFlagMiddleware.js"; | ||
import db from "../../../db.js"; | ||
import Reply from "../../../classes/reply/Reply.js"; | ||
import {getUserBulk} from "../channel.js"; | ||
import {isValidObjectId} from "mongoose"; | ||
import InvalidReplyMessage from "../../../classes/reply/InvalidReplyMessage.js"; | ||
import {getNick} from "../../../util/getNickname.js"; | ||
import {Scope} from "@sentry/node"; | ||
|
||
const router = express.Router({ | ||
mergeParams: true | ||
}); | ||
|
||
// Kill switch | ||
router.use(FeatureFlag("LQ_Friends")); | ||
|
||
router.use(Auth); | ||
|
||
/** | ||
* List friend requests (incoming & outgoing) | ||
*/ | ||
router.get("/requests", async (req, res) => { | ||
let FriendRequests = db.getFriendRequests(); | ||
let requests = await FriendRequests.find({$or: [{sender: res.locals.user._id}, {receiver: res.locals.user._id}]}) | ||
res.reply(new Reply(200, true, { | ||
message: "Here are your friend requests (incoming & outgoing)", | ||
requests | ||
})) | ||
}) | ||
|
||
/** | ||
* Send or accept friend requests | ||
*/ | ||
router.post("/requests/:username", async (req, res) => { | ||
let otherParty = await getUserByName(req.params.username) | ||
if (!otherParty) return res.reply(new InvalidReplyMessage("Invalid user")) | ||
if (otherParty._id.equals(res.locals.user._id)) { | ||
let reply = new Reply(999, false, { | ||
message: "baka" | ||
}) | ||
reply.request.cat = "this request is so stupid there isnt a cat to describe it" | ||
res.status(999).json(reply) | ||
} | ||
let Friend = db.getFriends() | ||
let existingFriend = await Friend.findOne({parties: [res.locals.user._id, otherParty._id]}); | ||
if (existingFriend) return res.reply(new InvalidReplyMessage("You are already friends with this user")) | ||
let FriendRequest = db.getFriendRequests() | ||
let existingRequest = await FriendRequest.findOne({$or: [{sender: res.locals.user._id, receiver: otherParty._id}, {receiver: res.locals.user._id, sender: otherParty._id}]}) | ||
if (existingRequest) { | ||
if (existingRequest.sender.equals(res.locals.user._id)) return res.reply(new InvalidReplyMessage("You have already sent a friend request to this user")); | ||
// ACCEPT FRIEND REQUEST | ||
await FriendRequest.deleteOne({_id: existingRequest._id}); | ||
let friend = new Friend({ | ||
startedAt: new Date(), | ||
parties: [res.locals.user._id, otherParty._id] | ||
}) | ||
await friend.save(); | ||
return res.reply(new Reply(200, true, { | ||
message: "Friend request accepted" | ||
})) | ||
} else { | ||
// SEND FRIEND REQUEST | ||
let friendRequest = new FriendRequest({ | ||
sender: res.locals.user._id, | ||
receiver: otherParty._id, | ||
reason: req.body.reason && typeof req.body.reason === "string" ? req.body.reason : null | ||
}) | ||
await friendRequest.save(); | ||
return res.reply(new Reply(201, true, { | ||
message: "Request sent!" | ||
})); | ||
} | ||
}) | ||
|
||
async function getUserByName(username : string) { | ||
let LoginUser = db.getLoginUsers() | ||
let loginUser = await LoginUser.findOne({username: username}) | ||
if (loginUser) return (await getUserBulk([loginUser._id], null))[0] | ||
return null | ||
// This is actually terrible because LITauth usernames arent supposed to be exposed | ||
// FIXME | ||
} | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import * as Mongoose from "mongoose"; | ||
export default new Mongoose.Schema({ | ||
sender: {type: Mongoose.Types.ObjectId, required: true}, // Who sent it | ||
receiver: {type: Mongoose.Types.ObjectId, required: true}, // Who is it for | ||
reason: {type: String, required: false}, // Free form text field for users to explain why they requested to be friends | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import * as Mongoose from "mongoose"; | ||
export default new Mongoose.Schema({ | ||
parties: [{type: Object, required: true}], // Who is friends | ||
startedAt: Date, // When did the request get accepted | ||
}); |