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.
Get channel by ID, get channel messages
Co-authoried-by: Hakase <hakase@litdevs.org>
- Loading branch information
Showing
15 changed files
with
241 additions
and
103 deletions.
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
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,33 @@ | ||
import express from 'express'; | ||
import {Auth} from "./auth.js"; | ||
import Database from "../../db.js"; | ||
import P from "../../util/PermissionMiddleware.js"; | ||
import {isValidObjectId} from "mongoose"; | ||
import InvalidReplyMessage from "../../classes/reply/InvalidReplyMessage.js"; | ||
import NotFoundReply from "../../classes/reply/NotFoundReply.js"; | ||
import Reply from "../../classes/reply/Reply.js"; | ||
import ServerErrorReply from "../../classes/reply/ServerErrorReply.js"; | ||
import messages from "./channel/messages.js"; | ||
|
||
|
||
const router = express.Router(); | ||
|
||
const database = new Database() | ||
|
||
router.use(Auth); | ||
|
||
router.use("/:channelId/messages", messages) | ||
|
||
router.get("/:channelId", P("READ_CHANNEL", "channel"), async (req, res) => { | ||
try { | ||
// Validity and existence are checked by permission manager | ||
let channel = await database.Channels.findOne({ _id: req.params.channelId }) | ||
res.reply(new Reply(200, true, { message: "Here is the channel", channel })) | ||
} catch (e) { | ||
console.error(e); | ||
res.reply(new ServerErrorReply()) | ||
} | ||
}) | ||
|
||
|
||
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,48 @@ | ||
import express from 'express'; | ||
import Database from "../../../db.js"; | ||
import {Auth} from "../auth.js"; | ||
import Reply from "../../../classes/reply/Reply.js"; | ||
import P from "../../../util/PermissionMiddleware.js"; | ||
import ServerErrorReply from "../../../classes/reply/ServerErrorReply.js"; | ||
import InvalidReplyMessage from "../../../classes/reply/InvalidReplyMessage.js"; | ||
import {Types} from "mongoose"; | ||
import messageSchema from "../../../schemas/messageSchema.js"; | ||
import {getNickBulk} from "../../../util/getNickname.js"; | ||
|
||
const router = express.Router({ mergeParams: true }); | ||
|
||
const database = new Database() | ||
|
||
router.use(Auth); | ||
|
||
router.get("/", P(["READ_CHANNEL_HISTORY", "READ_CHANNEL"], "channel"), async (req, res) => { | ||
try { | ||
// Optionally client can provide a timestamp to get messages before or after | ||
let startTimestamp = Infinity; | ||
let endTimestamp = 0; | ||
if (req.query.startTimestamp) startTimestamp = Number(req.query.startTimestamp) | ||
if (req.query.endTimestamp) endTimestamp = Number(req.query.endTimestamp) | ||
if (isNaN(startTimestamp)) return res.status(400).json(new InvalidReplyMessage("Invalid startTimestamp")); | ||
if (isNaN(endTimestamp)) return res.status(400).json(new InvalidReplyMessage("Invalid endTimestamp")); | ||
|
||
let quark = await database.Quarks.findOne({ channels: new Types.ObjectId(req.params.channelId) }); | ||
|
||
// Find messages in specified range, if endTimestamp is specified get messages after that timestamp, otherwise get messages before that timestamp | ||
// The naming is a bit backwards, isn't it? | ||
let messages = await database.Messages.find({ channelId: req.params.channelId, timestamp: { $lt: startTimestamp, $gt: endTimestamp } }).sort({ timestamp: endTimestamp === 0 ? -1 : 1 }).limit(50) | ||
// Replace author usernames with nicknames in this quark | ||
let authorIds = messages.map(message => message.authorId); | ||
let nicknames = await getNickBulk(authorIds, quark._id) | ||
messages = messages.map(message => { | ||
message.author.username = nicknames.find(nick => nick.userId.equals(message.authorId)).nickname || message.author.username; | ||
return message; | ||
}) | ||
res.reply(new Reply(200, true, {message: "Here are the messages", messages})); | ||
} catch (e) { | ||
console.error(e); | ||
return res.status(500).json(new ServerErrorReply()); | ||
} | ||
}) | ||
|
||
|
||
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
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
Oops, something went wrong.