2022-12-13 07:37:49 -05:00
|
|
|
import { db, ObjectId } from '../../mongodb.js'
|
2014-08-15 05:50:36 -04:00
|
|
|
|
2022-12-13 07:37:49 -05:00
|
|
|
export async function createMessage(roomId, userId, content, timestamp) {
|
2022-01-07 06:48:39 -05:00
|
|
|
let newMessageOpts = {
|
|
|
|
content,
|
|
|
|
room_id: roomId,
|
|
|
|
user_id: userId,
|
|
|
|
timestamp,
|
|
|
|
}
|
|
|
|
newMessageOpts = _ensureIdsAreObjectIds(newMessageOpts)
|
2022-01-07 07:10:54 -05:00
|
|
|
const confirmation = await db.messages.insertOne(newMessageOpts)
|
|
|
|
newMessageOpts._id = confirmation.insertedId
|
|
|
|
return newMessageOpts
|
2022-01-07 06:48:39 -05:00
|
|
|
}
|
2014-08-15 05:50:36 -04:00
|
|
|
|
2022-12-13 07:37:49 -05:00
|
|
|
export async function getMessages(roomId, limit, before) {
|
2022-01-07 06:48:39 -05:00
|
|
|
let query = { room_id: roomId }
|
|
|
|
if (before) {
|
|
|
|
query.timestamp = { $lt: before }
|
|
|
|
}
|
|
|
|
query = _ensureIdsAreObjectIds(query)
|
2024-05-22 05:37:08 -04:00
|
|
|
return await db.messages
|
|
|
|
.find(query)
|
|
|
|
.sort({ timestamp: -1 })
|
|
|
|
.limit(limit)
|
|
|
|
.toArray()
|
2022-01-07 06:48:39 -05:00
|
|
|
}
|
2017-01-24 09:44:32 -05:00
|
|
|
|
2022-12-13 07:37:49 -05:00
|
|
|
export async function findAllMessagesInRooms(roomIds) {
|
2024-05-22 05:37:08 -04:00
|
|
|
return await db.messages
|
2022-01-07 06:48:39 -05:00
|
|
|
.find({
|
|
|
|
room_id: { $in: roomIds },
|
|
|
|
})
|
2022-01-07 07:10:54 -05:00
|
|
|
.toArray()
|
2022-01-07 06:48:39 -05:00
|
|
|
}
|
2017-01-24 09:44:32 -05:00
|
|
|
|
2022-12-13 07:37:49 -05:00
|
|
|
export async function deleteAllMessagesInRoom(roomId) {
|
2022-01-07 07:10:54 -05:00
|
|
|
await db.messages.deleteMany({
|
|
|
|
room_id: roomId,
|
|
|
|
})
|
2022-01-07 06:48:39 -05:00
|
|
|
}
|
|
|
|
|
2022-12-13 07:37:49 -05:00
|
|
|
export async function deleteAllMessagesInRooms(roomIds) {
|
2022-03-16 08:20:52 -04:00
|
|
|
await db.messages.deleteMany({
|
|
|
|
room_id: { $in: roomIds },
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-12-13 07:37:49 -05:00
|
|
|
export async function updateMessage(
|
|
|
|
roomId,
|
|
|
|
messageId,
|
|
|
|
userId,
|
|
|
|
content,
|
|
|
|
timestamp
|
|
|
|
) {
|
2022-01-07 06:48:39 -05:00
|
|
|
const query = _ensureIdsAreObjectIds({
|
|
|
|
_id: messageId,
|
|
|
|
room_id: roomId,
|
|
|
|
})
|
2022-03-31 03:54:33 -04:00
|
|
|
if (userId) {
|
2023-10-17 08:48:51 -04:00
|
|
|
query.user_id = new ObjectId(userId)
|
2022-03-31 03:54:33 -04:00
|
|
|
}
|
2022-03-31 05:23:22 -04:00
|
|
|
const res = await db.messages.updateOne(query, {
|
2022-01-07 07:10:54 -05:00
|
|
|
$set: {
|
|
|
|
content,
|
|
|
|
edited_at: timestamp,
|
2022-01-07 06:48:39 -05:00
|
|
|
},
|
2022-01-07 07:10:54 -05:00
|
|
|
})
|
2022-03-31 05:23:22 -04:00
|
|
|
return res.modifiedCount === 1
|
2022-01-07 06:48:39 -05:00
|
|
|
}
|
2017-03-16 12:08:57 -04:00
|
|
|
|
2022-12-13 07:37:49 -05:00
|
|
|
export async function deleteMessage(roomId, messageId) {
|
2022-01-07 06:48:39 -05:00
|
|
|
const query = _ensureIdsAreObjectIds({
|
|
|
|
_id: messageId,
|
|
|
|
room_id: roomId,
|
|
|
|
})
|
2022-01-07 07:10:54 -05:00
|
|
|
await db.messages.deleteOne(query)
|
2022-01-07 06:48:39 -05:00
|
|
|
}
|
2017-03-17 11:16:38 -04:00
|
|
|
|
2022-01-07 06:48:39 -05:00
|
|
|
function _ensureIdsAreObjectIds(query) {
|
|
|
|
if (query.user_id && !(query.user_id instanceof ObjectId)) {
|
2023-10-17 08:48:51 -04:00
|
|
|
query.user_id = new ObjectId(query.user_id)
|
2022-01-07 06:48:39 -05:00
|
|
|
}
|
|
|
|
if (query.room_id && !(query.room_id instanceof ObjectId)) {
|
2023-10-17 08:48:51 -04:00
|
|
|
query.room_id = new ObjectId(query.room_id)
|
2022-01-07 06:48:39 -05:00
|
|
|
}
|
|
|
|
if (query._id && !(query._id instanceof ObjectId)) {
|
2023-10-17 08:48:51 -04:00
|
|
|
query._id = new ObjectId(query._id)
|
2022-01-07 06:48:39 -05:00
|
|
|
}
|
|
|
|
return query
|
|
|
|
}
|