♻️ Avoid iterating item path twice to move to trash (#433)

This commit is contained in:
Eric Doughty-Papassideris
2024-05-28 16:10:10 +02:00
committed by ericlinagora
parent 71e3240c94
commit 2056996a30
@@ -632,12 +632,12 @@ export class DocumentsService {
this.logger.error({ error: `${error}` }, "Failed to grant access to the drive item"); this.logger.error({ error: `${error}` }, "Failed to grant access to the drive item");
throw new CrudException("User does not have access to this item or its children", 401); throw new CrudException("User does not have access to this item or its children", 401);
} }
const path = await getPath(item.parent_id, this.repository, true, context);
const previousParentId = item.parent_id; const previousParentId = item.parent_id;
if ( if (
(await isInTrash(item, this.repository, context)) || (await isInTrash(item, this.repository, context)) ||
item.parent_id === this.TRASH || item.parent_id === this.TRASH ||
(await getPath(item.parent_id, this.repository, true, context))[0].id === this.TRASH path[0].id === this.TRASH
) { ) {
//This item is already in trash, we can delete it definitively //This item is already in trash, we can delete it definitively
@@ -681,22 +681,19 @@ export class DocumentsService {
const creator = await this.userRepository.findOne({ id: item.creator }); const creator = await this.userRepository.findOne({ id: item.creator });
if (creator.type === "anonymous") { if (creator.type === "anonymous") {
const loadedCreators = new Map<string, User>(); const loadedCreators = new Map<string, User>();
const path = await getPath( let firstOwnedItem: DriveFile | undefined;
item.id, for (let i = path.length - 1; i >= 0; i--) {
this.repository, const item = path[i];
true, if (!item.creator) continue;
context, const user =
async item => { loadedCreators.get(item.creator) ??
if (!item.creator) return true; (await this.userRepository.findOne({ id: item.creator }));
const user = loadedCreators.set(item.creator, user);
loadedCreators.get(item.creator) ?? if (user.type !== "anonymous") {
(await this.userRepository.findOne({ id: item.creator })); firstOwnedItem = item;
loadedCreators.set(item.creator, user); break;
return user.type !== "anonymous"; }
}, }
true,
);
const [firstOwnedItem] = path;
if (firstOwnedItem) { if (firstOwnedItem) {
const firstKnownCreator = loadedCreators.get(firstOwnedItem.creator); const firstKnownCreator = loadedCreators.get(firstOwnedItem.creator);
const accessEntitiesWithoutUser = item.access_info.entities.filter( const accessEntitiesWithoutUser = item.access_info.entities.filter(
@@ -704,6 +701,8 @@ export class DocumentsService {
); );
item.access_info.entities = [ item.access_info.entities = [
...accessEntitiesWithoutUser, ...accessEntitiesWithoutUser,
// This is not functionally required, but creates an audit trace of what
// happened to this anonymously uploaded file
{ {
type: "user", type: "user",
id: firstKnownCreator.id, id: firstKnownCreator.id,