Skip to content

Commit

Permalink
♻️ Avoid iterating item path twice to move to trash (#433)
Browse files Browse the repository at this point in the history
  • Loading branch information
ericlinagora committed May 28, 2024
1 parent 139e95a commit 06d6db2
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions tdrive/backend/node/src/services/documents/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -632,12 +632,12 @@ export class DocumentsService {
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);
}

const path = await getPath(item.parent_id, this.repository, true, context);
const previousParentId = item.parent_id;
if (
(await isInTrash(item, this.repository, context)) ||
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

Expand Down Expand Up @@ -681,29 +681,28 @@ export class DocumentsService {
const creator = await this.userRepository.findOne({ id: item.creator });
if (creator.type === "anonymous") {
const loadedCreators = new Map<string, User>();
const path = await getPath(
item.id,
this.repository,
true,
context,
async item => {
if (!item.creator) return true;
const user =
loadedCreators.get(item.creator) ??
(await this.userRepository.findOne({ id: item.creator }));
loadedCreators.set(item.creator, user);
return user.type !== "anonymous";
},
true,
);
const [firstOwnedItem] = path;
let firstOwnedItem: DriveFile | undefined;
for (let i = path.length - 1; i >= 0; i--) {
const item = path[i];
if (!item.creator) continue;
const user =
loadedCreators.get(item.creator) ??
(await this.userRepository.findOne({ id: item.creator }));
loadedCreators.set(item.creator, user);
if (user.type !== "anonymous") {
firstOwnedItem = item;
break;
}
}
if (firstOwnedItem) {
const firstKnownCreator = loadedCreators.get(firstOwnedItem.creator);
const accessEntitiesWithoutUser = item.access_info.entities.filter(
({ id, type }) => type != "user" || id != firstKnownCreator.id,
);
item.access_info.entities = [
...accessEntitiesWithoutUser,
// This is not functionally required, but creates an audit trace of what
// happened to this anonymously uploaded file
{
type: "user",
id: firstKnownCreator.id,
Expand Down

0 comments on commit 06d6db2

Please sign in to comment.