mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-11-14 14:13:43 +01:00
Merge pull request #57 from overleaf/jpa-module-local-db-object
[misc] simplify the mongodb collection access using a shared db construct
This commit is contained in:
@@ -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)
|
||||
}
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -2,18 +2,25 @@ const Settings = require('settings-sharelatex')
|
||||
const { MongoClient, ObjectId } = require('mongodb')
|
||||
|
||||
const clientPromise = MongoClient.connect(Settings.mongo.url)
|
||||
const dbPromise = clientPromise.then((client) => client.db())
|
||||
|
||||
async function getCollection(name) {
|
||||
return (await dbPromise).collection(name)
|
||||
let setupDbPromise
|
||||
async function waitForDb() {
|
||||
if (!setupDbPromise) {
|
||||
setupDbPromise = setupDb()
|
||||
}
|
||||
await setupDbPromise
|
||||
}
|
||||
|
||||
async function waitForDb() {
|
||||
await clientPromise
|
||||
const db = {}
|
||||
async function setupDb() {
|
||||
const internalDb = (await clientPromise).db()
|
||||
|
||||
db.messages = internalDb.collection('messages')
|
||||
db.rooms = internalDb.collection('rooms')
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
db,
|
||||
ObjectId,
|
||||
getCollection,
|
||||
waitForDb
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
* DS207: Consider shorter variations of null checks
|
||||
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
||||
*/
|
||||
const { waitForDb } = require('../../../../app/js/mongodb')
|
||||
const app = require('../../../../app')
|
||||
|
||||
module.exports = {
|
||||
@@ -25,9 +26,10 @@ module.exports = {
|
||||
return callback()
|
||||
} else if (this.initing) {
|
||||
return this.callbacks.push(callback)
|
||||
} else {
|
||||
this.initing = true
|
||||
this.callbacks.push(callback)
|
||||
}
|
||||
this.initing = true
|
||||
this.callbacks.push(callback)
|
||||
waitForDb().then(() => {
|
||||
return app.listen(3010, 'localhost', (error) => {
|
||||
if (error != null) {
|
||||
throw error
|
||||
@@ -41,6 +43,6 @@ module.exports = {
|
||||
return result
|
||||
})()
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user