Skip to content

Commit

Permalink
feat: add file filter chat files checkbox(use for remove unused chat …
Browse files Browse the repository at this point in the history
…files)
  • Loading branch information
moonrailgun committed Jul 6, 2024
1 parent 3cea16e commit c4e70ef
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 12 deletions.
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion server/admin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"tailchat-server-sdk": "workspace:^",
"tushan": "^0.3.4",
"tushan": "^0.3.9",
"vite-express": "0.8.0"
},
"devDependencies": {
Expand Down
7 changes: 6 additions & 1 deletion server/admin/src/client/fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ export const fileFields = [
createFileSizeField('size', {
list: {
width: 120,
sort: true,
},
}),
createTextField('metaData.content-type'),
Expand All @@ -170,7 +171,11 @@ export const fileFields = [
width: 80,
},
}),
createDateTimeField('createdAt'),
createDateTimeField('createdAt', {
list: {
sort: true,
},
}),
];

export const mailFields = [
Expand Down
28 changes: 24 additions & 4 deletions server/admin/src/client/resources/file.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
import filesize from 'filesize';
import React from 'react';
import React, { useState } from 'react';
import {
createTextField,
ListTable,
useAsync,
useTranslation,
Typography,
styled,
Checkbox,
} from 'tushan';
import { fileFields } from '../fields';
import { request } from '../request';

const Row = styled.div`
display: flex;
gap: 20px;
justify-content: end;
`;

export const FileList: React.FC = React.memo(() => {
const { t } = useTranslation();
const [isOnlyChatFiles, setIsOnlyChatFiles] = useState(false);
const { value: totalSize = 0 } = useAsync(async () => {
const { data } = await request.get('/file/filesizeSum');

Expand All @@ -20,9 +29,19 @@ export const FileList: React.FC = React.memo(() => {

return (
<>
<Typography.Paragraph style={{ textAlign: 'right' }}>
{t('custom.file.fileTotalSize')}: {filesize(totalSize)}
</Typography.Paragraph>
<Row>
<Checkbox
checked={isOnlyChatFiles}
onClick={() => {
setIsOnlyChatFiles(!isOnlyChatFiles);
}}
>
Only show chat files
</Checkbox>
<Typography.Paragraph>
{t('custom.file.fileTotalSize')}: {filesize(totalSize)}
</Typography.Paragraph>
</Row>
<ListTable
filter={[
createTextField('q', {
Expand All @@ -33,6 +52,7 @@ export const FileList: React.FC = React.memo(() => {
action={{ detail: true, delete: true }}
batchAction={{ delete: true }}
showSizeChanger={true}
meta={isOnlyChatFiles ? 'onlyChat' : undefined}
/>
</>
);
Expand Down
51 changes: 50 additions & 1 deletion server/admin/src/server/router/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ import userModel from '../../../../models/user/user';
import messageModel from '../../../../models/chat/message';
import fileModel from '../../../../models/file';
import groupModel from '../../../../models/group/group';
import { raExpressMongoose } from '../middleware/express-mongoose-ra-json-server';
import {
raExpressMongoose,
virtualId,
} from '../middleware/express-mongoose-ra-json-server';
import { cacheRouter } from './cache';
import discoverModel from '../../../../plugins/com.msgbyte.discover/models/discover';
import { analyticsRouter } from './analytics';
import _ from 'lodash';

const router = Router();

Expand Down Expand Up @@ -292,6 +296,51 @@ router.delete('/file/:id', auth(), async (req, res) => {
router.use(
'/file',
auth(),
async (req, res, next) => {
const onlyChatFile = req.query.meta === 'onlyChat';

if (!onlyChatFile) {
return next();
}

// only return chatted file rather than all file
const result = await fileModel
.aggregate()
.lookup({
from: 'users',
localField: 'url',
foreignField: 'avatar',
as: 'avatarMatchedUser',
})
.match({
'avatarMatchedUser.0': { $exists: false },
})
.project({
avatarMatchedUser: 0,
})
.facet({
metadata: [{ $count: 'total' }],
data: [
{
$sort: {
[typeof req.query._sort === 'string'
? req.query._sort === 'id'
? '_id'
: req.query._sort
: '_id']: req.query._order === 'ASC' ? 1 : -1,
},
},
{ $skip: Number(req.query._start) },
{ $limit: Number(req.query._end) - Number(req.query._start) },
],
})
.exec();

const list = _.get(result, '0.data');
const total = _.get(result, '0.metadata.0.total');

return res.set('X-Total-Count', total).json(virtualId(list)).end();
},
raExpressMongoose(fileModel, {
q: ['objectName'],
allowedRegexFields: ['objectName'],
Expand Down
1 change: 1 addition & 0 deletions server/models/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { User } from './user/user';
},
})
@index({ bucketName: 1, objectName: 1 })
@index({ url: 1 })
export class File extends TimeStamps implements Base {
_id: Types.ObjectId;
id: string;
Expand Down
2 changes: 2 additions & 0 deletions server/models/user/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
ReturnModelType,
modelOptions,
Severity,
index,
} from '@typegoose/typegoose';
import { Base, TimeStamps } from '@typegoose/typegoose/lib/defaultClasses';
import type { Types } from 'mongoose';
Expand Down Expand Up @@ -32,6 +33,7 @@ export interface UserLoginRes extends User {
allowMixed: Severity.ALLOW,
},
})
@index({ avatar: 1 })
export class User extends TimeStamps implements Base {
_id: Types.ObjectId;
id: string;
Expand Down

0 comments on commit c4e70ef

Please sign in to comment.