2023-01-13 07:42:29 -05:00
|
|
|
import OError from '@overleaf/o-error'
|
|
|
|
import { db, ObjectId } from './mongodb.js'
|
|
|
|
import * as HistoryStoreManager from './HistoryStoreManager.js'
|
|
|
|
import * as UpdatesProcessor from './UpdatesProcessor.js'
|
|
|
|
import * as WebApiManager from './WebApiManager.js'
|
|
|
|
|
|
|
|
export function getLabels(projectId, callback) {
|
2024-02-05 04:27:55 -05:00
|
|
|
_toObjectId(projectId, function (error, projectId) {
|
|
|
|
if (error) {
|
2023-01-13 07:42:29 -05:00
|
|
|
return callback(OError.tag(error))
|
|
|
|
}
|
2024-02-05 04:27:55 -05:00
|
|
|
db.projectHistoryLabels
|
2023-12-15 05:51:11 -05:00
|
|
|
.find({ project_id: new ObjectId(projectId) })
|
2023-01-13 07:42:29 -05:00
|
|
|
.toArray(function (error, labels) {
|
2024-02-05 04:27:55 -05:00
|
|
|
if (error) {
|
2023-01-13 07:42:29 -05:00
|
|
|
return callback(OError.tag(error))
|
|
|
|
}
|
|
|
|
const formattedLabels = labels.map(_formatLabel)
|
2024-02-05 04:27:55 -05:00
|
|
|
callback(null, formattedLabels)
|
2023-01-13 07:42:29 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export function createLabel(
|
|
|
|
projectId,
|
|
|
|
userId,
|
|
|
|
version,
|
|
|
|
comment,
|
|
|
|
createdAt,
|
|
|
|
shouldValidateExists,
|
|
|
|
callback
|
|
|
|
) {
|
|
|
|
const validateVersionExists = function (callback) {
|
|
|
|
if (shouldValidateExists === false) {
|
2024-02-05 04:27:55 -05:00
|
|
|
callback()
|
2023-01-13 07:42:29 -05:00
|
|
|
} else {
|
2024-02-05 04:27:55 -05:00
|
|
|
_validateChunkExistsForVersion(projectId.toString(), version, callback)
|
2023-01-13 07:42:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-05 04:27:55 -05:00
|
|
|
_toObjectId(projectId, userId, function (error, projectId, userId) {
|
|
|
|
if (error) {
|
2023-01-13 07:42:29 -05:00
|
|
|
return callback(OError.tag(error))
|
|
|
|
}
|
2024-02-05 04:27:55 -05:00
|
|
|
validateVersionExists(function (error) {
|
|
|
|
if (error) {
|
2023-01-13 07:42:29 -05:00
|
|
|
return callback(OError.tag(error))
|
|
|
|
}
|
|
|
|
|
|
|
|
createdAt = createdAt != null ? new Date(createdAt) : new Date()
|
|
|
|
|
|
|
|
const label = {
|
2023-12-15 05:51:11 -05:00
|
|
|
project_id: new ObjectId(projectId),
|
2023-01-13 07:42:29 -05:00
|
|
|
comment,
|
|
|
|
version,
|
2023-12-15 05:51:11 -05:00
|
|
|
user_id: new ObjectId(userId),
|
2023-01-13 07:42:29 -05:00
|
|
|
created_at: createdAt,
|
|
|
|
}
|
|
|
|
db.projectHistoryLabels.insertOne(label, function (error, confirmation) {
|
2024-02-05 04:27:55 -05:00
|
|
|
if (error) {
|
2023-01-13 07:42:29 -05:00
|
|
|
return callback(OError.tag(error))
|
|
|
|
}
|
|
|
|
label._id = confirmation.insertedId
|
|
|
|
callback(null, _formatLabel(label))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-01-22 04:02:51 -05:00
|
|
|
export function deleteLabelForUser(projectId, userId, labelId, callback) {
|
2024-02-05 04:27:55 -05:00
|
|
|
_toObjectId(
|
2023-01-13 07:42:29 -05:00
|
|
|
projectId,
|
|
|
|
userId,
|
|
|
|
labelId,
|
|
|
|
function (error, projectId, userId, labelId) {
|
2024-02-05 04:27:55 -05:00
|
|
|
if (error) {
|
2023-01-13 07:42:29 -05:00
|
|
|
return callback(OError.tag(error))
|
|
|
|
}
|
2024-02-05 04:27:55 -05:00
|
|
|
db.projectHistoryLabels.deleteOne(
|
2023-01-13 07:42:29 -05:00
|
|
|
{
|
2023-12-15 05:51:11 -05:00
|
|
|
_id: new ObjectId(labelId),
|
|
|
|
project_id: new ObjectId(projectId),
|
|
|
|
user_id: new ObjectId(userId),
|
2023-01-13 07:42:29 -05:00
|
|
|
},
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-01-22 04:02:51 -05:00
|
|
|
export function deleteLabel(projectId, labelId, callback) {
|
2024-02-05 04:27:55 -05:00
|
|
|
_toObjectId(projectId, labelId, function (error, projectId, labelId) {
|
|
|
|
if (error) {
|
2024-01-22 04:02:51 -05:00
|
|
|
return callback(OError.tag(error))
|
|
|
|
}
|
2024-02-05 04:27:55 -05:00
|
|
|
db.projectHistoryLabels.deleteOne(
|
2024-01-22 04:02:51 -05:00
|
|
|
{
|
|
|
|
_id: new ObjectId(labelId),
|
|
|
|
project_id: new ObjectId(projectId),
|
|
|
|
},
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-01-13 07:42:29 -05:00
|
|
|
export function transferLabels(fromUserId, toUserId, callback) {
|
2024-02-05 04:27:55 -05:00
|
|
|
_toObjectId(fromUserId, toUserId, function (error, fromUserId, toUserId) {
|
|
|
|
if (error) {
|
|
|
|
return callback(OError.tag(error))
|
2023-01-13 07:42:29 -05:00
|
|
|
}
|
2024-02-05 04:27:55 -05:00
|
|
|
db.projectHistoryLabels.updateMany(
|
|
|
|
{
|
|
|
|
user_id: fromUserId,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
$set: { user_id: toUserId },
|
|
|
|
},
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
})
|
2023-01-13 07:42:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function _toObjectId(...args1) {
|
|
|
|
const adjustedLength = Math.max(args1.length, 1)
|
|
|
|
const args = args1.slice(0, adjustedLength - 1)
|
|
|
|
const callback = args1[adjustedLength - 1]
|
|
|
|
try {
|
2023-12-15 05:51:11 -05:00
|
|
|
const ids = args.map(id => new ObjectId(id))
|
2024-02-05 04:27:55 -05:00
|
|
|
callback(null, ...ids)
|
2023-01-13 07:42:29 -05:00
|
|
|
} catch (error) {
|
2024-02-05 04:27:55 -05:00
|
|
|
callback(error)
|
2023-01-13 07:42:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function _formatLabel(label) {
|
|
|
|
return {
|
|
|
|
id: label._id,
|
|
|
|
comment: label.comment,
|
|
|
|
version: label.version,
|
|
|
|
user_id: label.user_id,
|
|
|
|
created_at: label.created_at,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function _validateChunkExistsForVersion(projectId, version, callback) {
|
2024-02-05 04:27:55 -05:00
|
|
|
UpdatesProcessor.processUpdatesForProject(projectId, function (error) {
|
|
|
|
if (error) {
|
2023-01-13 07:42:29 -05:00
|
|
|
return callback(error)
|
|
|
|
}
|
2024-02-05 04:27:55 -05:00
|
|
|
WebApiManager.getHistoryId(projectId, function (error, historyId) {
|
|
|
|
if (error) {
|
2023-01-13 07:42:29 -05:00
|
|
|
return callback(error)
|
|
|
|
}
|
2024-02-05 04:27:55 -05:00
|
|
|
HistoryStoreManager.getChunkAtVersion(
|
2023-01-13 07:42:29 -05:00
|
|
|
projectId,
|
|
|
|
historyId,
|
|
|
|
version,
|
|
|
|
function (error) {
|
2024-02-05 04:27:55 -05:00
|
|
|
if (error) {
|
2023-01-13 07:42:29 -05:00
|
|
|
return callback(error)
|
|
|
|
}
|
2024-02-05 04:27:55 -05:00
|
|
|
callback()
|
2023-01-13 07:42:29 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|