Skip to content

Commit bda5270

Browse files
committed
feat(trashbin): Show user who deleted a file
Signed-off-by: Christopher Ng <chrng8@gmail.com>
1 parent 153705d commit bda5270

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

apps/files_trashbin/src/main.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ import { getContents } from './services/trashbin'
3232
import './actions/restoreAction'
3333
import { Column, Node, View, getNavigation } from '@nextcloud/files'
3434
import { dirname, joinPaths } from '@nextcloud/paths'
35+
import { getCurrentUser } from '@nextcloud/auth'
36+
37+
import Vue from 'vue'
38+
import NcUserBubble from '@nextcloud/vue/dist/Components/NcUserBubble.js'
3539

3640
const parseOriginalLocation = (node: Node): string => {
3741
const path = node.attributes?.['trashbin-original-location'] !== undefined ? String(node.attributes?.['trashbin-original-location']) : null
@@ -45,6 +49,34 @@ const parseOriginalLocation = (node: Node): string => {
4549
return joinPaths(t('files_trashbin', 'All files'), dir)
4650
}
4751

52+
interface DeletedBy {
53+
userId: null | string
54+
displayName: null | string
55+
label: null | string
56+
}
57+
58+
const generateLabel = (userId: null | string, displayName: null | string) => {
59+
const currentUserId = getCurrentUser()?.uid
60+
if (userId === currentUserId) {
61+
return t('files_trashbin', 'You')
62+
}
63+
if (!userId && !displayName) {
64+
return t('files_trashbin', 'Unknown')
65+
}
66+
return null
67+
}
68+
69+
const parseDeletedBy = (node: Node): DeletedBy => {
70+
const userId = node.attributes?.['trashbin-deleted-by-id'] !== undefined ? String(node.attributes?.['trashbin-deleted-by-id']) : null
71+
const displayName = node.attributes?.['trashbin-deleted-by-display-name'] !== undefined ? String(node.attributes?.['trashbin-deleted-by-display-name']) : null
72+
const label = generateLabel(userId, displayName)
73+
return {
74+
userId,
75+
displayName,
76+
label,
77+
}
78+
}
79+
4880
const Navigation = getNavigation()
4981
Navigation.register(new View({
5082
id: 'trashbin',
@@ -78,6 +110,33 @@ Navigation.register(new View({
78110
},
79111
}),
80112

113+
new Column({
114+
id: 'deleted-by',
115+
title: t('files_trashbin', 'Deleted by'),
116+
render(node) {
117+
const { userId, displayName, label } = parseDeletedBy(node)
118+
if (label) {
119+
const span = document.createElement('span')
120+
span.textContent = label
121+
return span
122+
}
123+
124+
const UserBubble = Vue.extend(NcUserBubble)
125+
const propsData = {
126+
size: 32,
127+
user: userId ?? undefined,
128+
displayName: displayName ?? t('files_trashbin', 'Unknown'),
129+
}
130+
const userBubble = new UserBubble({ propsData }).$mount().$el
131+
return userBubble as HTMLElement
132+
},
133+
sort(nodeA, nodeB) {
134+
const deletedByA = parseDeletedBy(nodeA).label ?? parseDeletedBy(nodeA).displayName ?? t('files_trashbin', 'Unknown')
135+
const deletedByB = parseDeletedBy(nodeB).label ?? parseDeletedBy(nodeB).displayName ?? t('files_trashbin', 'Unknown')
136+
return deletedByA.localeCompare(deletedByB)
137+
},
138+
}),
139+
81140
new Column({
82141
id: 'deleted',
83142
title: t('files_trashbin', 'Deleted'),

apps/files_trashbin/src/services/trashbin.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ const data = `<?xml version="1.0"?>
3535
<nc:trashbin-deletion-time />
3636
<nc:trashbin-original-location />
3737
<nc:trashbin-title />
38+
<nc:trashbin-deleted-by-id />
39+
<nc:trashbin-deleted-by-display-name />
3840
${getDavProperties()}
3941
</d:prop>
4042
</d:propfind>`

0 commit comments

Comments
 (0)