mirror of
https://github.com/plankanban/planka.git
synced 2025-12-19 09:13:20 +03:00
feat: Track storage usage
This commit is contained in:
@@ -3,12 +3,28 @@
|
||||
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
|
||||
*/
|
||||
|
||||
const { makeWhereQueryBuilder } = require('../helpers');
|
||||
|
||||
const hasAvatarChanged = (avatar, prevAvatar) => {
|
||||
if (!avatar && !prevAvatar) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!avatar || !prevAvatar) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return avatar.uploadedFileId !== prevAvatar.uploadedFileId;
|
||||
};
|
||||
|
||||
const buildWhereQuery = makeWhereQueryBuilder(User);
|
||||
|
||||
const defaultFind = (criteria) => User.find(criteria).sort('id');
|
||||
|
||||
/* Query methods */
|
||||
|
||||
const createOne = (values) => {
|
||||
if (sails.config.custom.activeUsersLimit) {
|
||||
if (!_.isNil(sails.config.custom.activeUsersLimit)) {
|
||||
return sails.getDatastore().transaction(async (db) => {
|
||||
const queryResult = await sails
|
||||
.sendNativeQuery('SELECT NULL FROM user_account WHERE is_deactivated = $1 FOR UPDATE', [
|
||||
@@ -62,29 +78,119 @@ const getOneActiveByEmailOrUsername = (emailOrUsername) => {
|
||||
});
|
||||
};
|
||||
|
||||
const updateOne = (criteria, values) => {
|
||||
if (values.isDeactivated === false && sails.config.custom.activeUsersLimit) {
|
||||
return sails.getDatastore().transaction(async (db) => {
|
||||
const queryResult = await sails
|
||||
.sendNativeQuery('SELECT NULL FROM user_account WHERE is_deactivated = $1 FOR UPDATE', [
|
||||
false,
|
||||
])
|
||||
.usingConnection(db);
|
||||
const updateOne = async (criteria, values) => {
|
||||
const enforceActiveLimit =
|
||||
values.isDeactivated === false && !_.isNil(sails.config.custom.activeUsersLimit);
|
||||
|
||||
if (queryResult.rowCount >= sails.config.custom.activeUsersLimit) {
|
||||
throw 'activeLimitReached';
|
||||
if (!_.isUndefined(values.avatar) || enforceActiveLimit) {
|
||||
return sails.getDatastore().transaction(async (db) => {
|
||||
if (enforceActiveLimit) {
|
||||
const queryResult = await sails
|
||||
.sendNativeQuery('SELECT NULL FROM user_account WHERE is_deactivated = $1 FOR UPDATE', [
|
||||
false,
|
||||
])
|
||||
.usingConnection(db);
|
||||
|
||||
if (queryResult.rowCount >= sails.config.custom.activeUsersLimit) {
|
||||
throw 'activeLimitReached';
|
||||
}
|
||||
}
|
||||
|
||||
return User.updateOne(criteria)
|
||||
let prevAvatar;
|
||||
if (!_.isUndefined(values.avatar)) {
|
||||
const [whereQuery, whereQueryValues] = buildWhereQuery(criteria);
|
||||
|
||||
const queryResult = await sails
|
||||
.sendNativeQuery(
|
||||
`SELECT avatar FROM user_account WHERE ${whereQuery} FOR UPDATE`,
|
||||
whereQueryValues,
|
||||
)
|
||||
.usingConnection(db);
|
||||
|
||||
if (queryResult.rowCount === 0) {
|
||||
return { user: null };
|
||||
}
|
||||
|
||||
[{ avatar: prevAvatar }] = queryResult.rows;
|
||||
}
|
||||
|
||||
const user = await User.updateOne(criteria)
|
||||
.set({ ...values })
|
||||
.usingConnection(db);
|
||||
|
||||
let uploadedFile;
|
||||
if (hasAvatarChanged(user.avatar, prevAvatar)) {
|
||||
if (prevAvatar) {
|
||||
const queryResult = await sails
|
||||
.sendNativeQuery(
|
||||
'UPDATE uploaded_file SET references_total = CASE WHEN references_total > 1 THEN references_total - 1 END, updated_at = $1 WHERE id = $2 RETURNING *',
|
||||
[new Date().toISOString(), prevAvatar.uploadedFileId],
|
||||
)
|
||||
.usingConnection(db);
|
||||
|
||||
const [row] = queryResult.rows;
|
||||
|
||||
uploadedFile = {
|
||||
id: row.id,
|
||||
type: row.type,
|
||||
mimeType: row.mime_type,
|
||||
size: row.size,
|
||||
referencesTotal: row.references_total,
|
||||
createdAt: row.created_at,
|
||||
updatedAt: row.updated_at,
|
||||
};
|
||||
}
|
||||
|
||||
if (user.avatar) {
|
||||
const queryResult = await sails
|
||||
.sendNativeQuery(
|
||||
'UPDATE uploaded_file SET references_total = references_total + 1, updated_at = $1 WHERE id = $2 AND references_total IS NOT NULL',
|
||||
[new Date().toISOString(), user.avatar.uploadedFileId],
|
||||
)
|
||||
.usingConnection(db);
|
||||
|
||||
if (queryResult.rowCount === 0) {
|
||||
throw 'uploadedFileNotFound';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { user, uploadedFile };
|
||||
});
|
||||
}
|
||||
|
||||
return User.updateOne(criteria).set({ ...values });
|
||||
const user = await User.updateOne(criteria).set({ ...values });
|
||||
return { user };
|
||||
};
|
||||
|
||||
const deleteOne = (criteria) => User.destroyOne(criteria);
|
||||
const deleteOne = (criteria) =>
|
||||
sails.getDatastore().transaction(async (db) => {
|
||||
const user = await User.destroyOne(criteria).usingConnection(db);
|
||||
|
||||
let uploadedFile;
|
||||
if (user.avatar) {
|
||||
const queryResult = await sails
|
||||
.sendNativeQuery(
|
||||
'UPDATE uploaded_file SET references_total = CASE WHEN references_total > 1 THEN references_total - 1 END, updated_at = $1 WHERE id = $2 RETURNING *',
|
||||
[new Date().toISOString(), user.avatar.uploadedFileId],
|
||||
)
|
||||
.usingConnection(db);
|
||||
|
||||
const [row] = queryResult.rows;
|
||||
|
||||
uploadedFile = {
|
||||
id: row.id,
|
||||
type: row.type,
|
||||
mimeType: row.mime_type,
|
||||
size: row.size,
|
||||
referencesTotal: row.references_total,
|
||||
createdAt: row.created_at,
|
||||
updatedAt: row.updated_at,
|
||||
};
|
||||
}
|
||||
|
||||
return { user, uploadedFile };
|
||||
});
|
||||
|
||||
module.exports = {
|
||||
createOne,
|
||||
|
||||
Reference in New Issue
Block a user