mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-07 20:31:06 -05:00
[misc] simplify mongodb collection access using a shared db construct
Resolve the getCollection Promises once and store the result in a shared `db` object which can get imported by all the call-sites. The http server is starting only after a Promise of `waitForDb()` resolves. This covers the app code and the acceptance tests: REF: 586706a9439c3591fc9613dc877f055096ca073a REF: d026569d2eb4123e30c771a55a001b42d5ade72f
This commit is contained in:
parent
a52c0fe9fb
commit
4080784310
3 changed files with 94 additions and 113 deletions
|
@ -13,12 +13,10 @@
|
|||
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
||||
*/
|
||||
let MessageManager
|
||||
const { ObjectId, getCollection } = require('../../mongodb')
|
||||
const { db, ObjectId } = require('../../mongodb')
|
||||
const metrics = require('@overleaf/metrics')
|
||||
const logger = require('logger-sharelatex')
|
||||
|
||||
const messagesCollectionPromise = getCollection('messages')
|
||||
|
||||
module.exports = MessageManager = {
|
||||
createMessage(room_id, user_id, content, timestamp, callback) {
|
||||
if (callback == null) {
|
||||
|
@ -31,15 +29,13 @@ module.exports = MessageManager = {
|
|||
timestamp
|
||||
}
|
||||
newMessageOpts = this._ensureIdsAreObjectIds(newMessageOpts)
|
||||
messagesCollectionPromise.then((messages) =>
|
||||
messages.insertOne(newMessageOpts, function (error, confirmation) {
|
||||
if (error) {
|
||||
return callback(error)
|
||||
}
|
||||
newMessageOpts._id = confirmation.insertedId
|
||||
callback(null, newMessageOpts)
|
||||
})
|
||||
)
|
||||
db.messages.insertOne(newMessageOpts, function (error, confirmation) {
|
||||
if (error) {
|
||||
return callback(error)
|
||||
}
|
||||
newMessageOpts._id = confirmation.insertedId
|
||||
callback(null, newMessageOpts)
|
||||
})
|
||||
},
|
||||
|
||||
getMessages(room_id, limit, before, callback) {
|
||||
|
@ -51,39 +47,33 @@ module.exports = MessageManager = {
|
|||
query.timestamp = { $lt: before }
|
||||
}
|
||||
query = this._ensureIdsAreObjectIds(query)
|
||||
messagesCollectionPromise.then((messages) =>
|
||||
messages
|
||||
.find(query)
|
||||
.sort({ timestamp: -1 })
|
||||
.limit(limit)
|
||||
.toArray(callback)
|
||||
)
|
||||
db.messages
|
||||
.find(query)
|
||||
.sort({ timestamp: -1 })
|
||||
.limit(limit)
|
||||
.toArray(callback)
|
||||
},
|
||||
|
||||
findAllMessagesInRooms(room_ids, callback) {
|
||||
if (callback == null) {
|
||||
callback = function (error, messages) {}
|
||||
}
|
||||
messagesCollectionPromise.then((messages) =>
|
||||
messages
|
||||
.find({
|
||||
room_id: { $in: room_ids }
|
||||
})
|
||||
.toArray(callback)
|
||||
)
|
||||
db.messages
|
||||
.find({
|
||||
room_id: { $in: room_ids }
|
||||
})
|
||||
.toArray(callback)
|
||||
},
|
||||
|
||||
deleteAllMessagesInRoom(room_id, callback) {
|
||||
if (callback == null) {
|
||||
callback = function (error) {}
|
||||
}
|
||||
messagesCollectionPromise.then((messages) =>
|
||||
messages.deleteMany(
|
||||
{
|
||||
room_id
|
||||
},
|
||||
callback
|
||||
)
|
||||
db.messages.deleteMany(
|
||||
{
|
||||
room_id
|
||||
},
|
||||
callback
|
||||
)
|
||||
},
|
||||
|
||||
|
@ -95,17 +85,15 @@ module.exports = MessageManager = {
|
|||
_id: message_id,
|
||||
room_id
|
||||
})
|
||||
messagesCollectionPromise.then((messages) =>
|
||||
messages.updateOne(
|
||||
query,
|
||||
{
|
||||
$set: {
|
||||
content,
|
||||
edited_at: timestamp
|
||||
}
|
||||
},
|
||||
callback
|
||||
)
|
||||
db.messages.updateOne(
|
||||
query,
|
||||
{
|
||||
$set: {
|
||||
content,
|
||||
edited_at: timestamp
|
||||
}
|
||||
},
|
||||
callback
|
||||
)
|
||||
},
|
||||
|
||||
|
@ -117,9 +105,7 @@ module.exports = MessageManager = {
|
|||
_id: message_id,
|
||||
room_id
|
||||
})
|
||||
messagesCollectionPromise.then((messages) =>
|
||||
messages.deleteOne(query, callback)
|
||||
)
|
||||
db.messages.deleteOne(query, callback)
|
||||
},
|
||||
|
||||
_ensureIdsAreObjectIds(query) {
|
||||
|
|
|
@ -12,12 +12,10 @@
|
|||
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
||||
*/
|
||||
let ThreadManager
|
||||
const { ObjectId, getCollection } = require('../../mongodb')
|
||||
const { db, ObjectId } = require('../../mongodb')
|
||||
const logger = require('logger-sharelatex')
|
||||
const metrics = require('@overleaf/metrics')
|
||||
|
||||
const roomsCollectionPromise = getCollection('rooms')
|
||||
|
||||
module.exports = ThreadManager = {
|
||||
GLOBAL_THREAD: 'GLOBAL',
|
||||
|
||||
|
@ -50,58 +48,52 @@ module.exports = ThreadManager = {
|
|||
}
|
||||
}
|
||||
|
||||
roomsCollectionPromise.then((rooms) =>
|
||||
rooms.updateOne(query, { $set: update }, { upsert: true }, function (
|
||||
error
|
||||
) {
|
||||
if (error != null) {
|
||||
return callback(error)
|
||||
}
|
||||
rooms.findOne(query, callback)
|
||||
})
|
||||
)
|
||||
db.rooms.updateOne(query, { $set: update }, { upsert: true }, function (
|
||||
error
|
||||
) {
|
||||
if (error != null) {
|
||||
return callback(error)
|
||||
}
|
||||
db.rooms.findOne(query, callback)
|
||||
})
|
||||
},
|
||||
|
||||
findAllThreadRooms(project_id, callback) {
|
||||
if (callback == null) {
|
||||
callback = function (error, rooms) {}
|
||||
}
|
||||
roomsCollectionPromise.then((rooms) =>
|
||||
rooms
|
||||
.find(
|
||||
{
|
||||
project_id: ObjectId(project_id.toString()),
|
||||
thread_id: { $exists: true }
|
||||
},
|
||||
{
|
||||
thread_id: 1,
|
||||
resolved: 1
|
||||
}
|
||||
)
|
||||
.toArray(callback)
|
||||
)
|
||||
db.rooms
|
||||
.find(
|
||||
{
|
||||
project_id: ObjectId(project_id.toString()),
|
||||
thread_id: { $exists: true }
|
||||
},
|
||||
{
|
||||
thread_id: 1,
|
||||
resolved: 1
|
||||
}
|
||||
)
|
||||
.toArray(callback)
|
||||
},
|
||||
|
||||
resolveThread(project_id, thread_id, user_id, callback) {
|
||||
if (callback == null) {
|
||||
callback = function (error) {}
|
||||
}
|
||||
roomsCollectionPromise.then((rooms) =>
|
||||
rooms.updateOne(
|
||||
{
|
||||
project_id: ObjectId(project_id.toString()),
|
||||
thread_id: ObjectId(thread_id.toString())
|
||||
},
|
||||
{
|
||||
$set: {
|
||||
resolved: {
|
||||
user_id,
|
||||
ts: new Date()
|
||||
}
|
||||
db.rooms.updateOne(
|
||||
{
|
||||
project_id: ObjectId(project_id.toString()),
|
||||
thread_id: ObjectId(thread_id.toString())
|
||||
},
|
||||
{
|
||||
$set: {
|
||||
resolved: {
|
||||
user_id,
|
||||
ts: new Date()
|
||||
}
|
||||
},
|
||||
callback
|
||||
)
|
||||
}
|
||||
},
|
||||
callback
|
||||
)
|
||||
},
|
||||
|
||||
|
@ -109,19 +101,17 @@ module.exports = ThreadManager = {
|
|||
if (callback == null) {
|
||||
callback = function (error) {}
|
||||
}
|
||||
roomsCollectionPromise.then((rooms) =>
|
||||
rooms.updateOne(
|
||||
{
|
||||
project_id: ObjectId(project_id.toString()),
|
||||
thread_id: ObjectId(thread_id.toString())
|
||||
},
|
||||
{
|
||||
$unset: {
|
||||
resolved: true
|
||||
}
|
||||
},
|
||||
callback
|
||||
)
|
||||
db.rooms.updateOne(
|
||||
{
|
||||
project_id: ObjectId(project_id.toString()),
|
||||
thread_id: ObjectId(thread_id.toString())
|
||||
},
|
||||
{
|
||||
$unset: {
|
||||
resolved: true
|
||||
}
|
||||
},
|
||||
callback
|
||||
)
|
||||
},
|
||||
|
||||
|
@ -136,18 +126,16 @@ module.exports = ThreadManager = {
|
|||
if (error != null) {
|
||||
return callback(error)
|
||||
}
|
||||
roomsCollectionPromise.then((rooms) =>
|
||||
rooms.deleteOne(
|
||||
{
|
||||
_id: room._id
|
||||
},
|
||||
function (error) {
|
||||
if (error != null) {
|
||||
return callback(error)
|
||||
}
|
||||
return callback(null, room._id)
|
||||
db.rooms.deleteOne(
|
||||
{
|
||||
_id: room._id
|
||||
},
|
||||
function (error) {
|
||||
if (error != null) {
|
||||
return callback(error)
|
||||
}
|
||||
)
|
||||
return callback(null, room._id)
|
||||
}
|
||||
)
|
||||
})
|
||||
}
|
||||
|
|
|
@ -11,7 +11,14 @@ async function waitForDb() {
|
|||
await clientPromise
|
||||
}
|
||||
|
||||
const db = {}
|
||||
waitForDb().then(async function () {
|
||||
db.messages = await getCollection('messages')
|
||||
db.rooms = await getCollection('rooms')
|
||||
})
|
||||
|
||||
module.exports = {
|
||||
db,
|
||||
ObjectId,
|
||||
getCollection,
|
||||
waitForDb
|
||||
|
|
Loading…
Reference in a new issue