Skip to content

Commit

Permalink
đŸ”„ refactor: Update shared links API and data handling; improve query …
Browse files Browse the repository at this point in the history
…parameters and response structure
  • Loading branch information
berry-13 committed Jan 6, 2025
1 parent 683f4a5 commit 4e6582e
Show file tree
Hide file tree
Showing 18 changed files with 630 additions and 364 deletions.
43 changes: 37 additions & 6 deletions api/models/Share.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { nanoid } = require('nanoid');
const { Constants } = require('librechat-data-provider');
const { Conversation } = require('~/models/Conversation');
const SharedLink = require('./schema/shareSchema');
const { getMessages } = require('./Message');
const logger = require('~/config/winston');
Expand Down Expand Up @@ -94,23 +95,50 @@ async function getSharedMessages(shareId) {
}
}

async function getSharedLinks(user, pageNumber = 1, pageSize = 25, isPublic = true) {
const query = { user, isPublic };

async function getSharedLinks(
user,
pageNumber = 1,
pageSize = 25,
isPublic = true,
sortBy = 'createdAt',
sortDirection = 'desc',
search = '',
) {
try {
const query = { user, isPublic };

if (search) {
const searchResults = await Conversation.meiliSearch(search, {
attributesToHighlight: ['title'],
});

const conversationIds = searchResults.hits.map((hit) => hit.id);

query['conversation'] = { $in: conversationIds };
}

const sort = {};
sort[sortBy] = sortDirection === 'desc' ? -1 : 1;

const [totalConvos, sharedLinks] = await Promise.all([
SharedLink.countDocuments(query).exec(),
SharedLink.find(query)
.sort({ updatedAt: -1 })
.sort(sort)
.skip((pageNumber - 1) * pageSize)
.limit(pageSize)
.select('-_id -__v -user')
.select('-__v -user')
.lean()
.exec(),
]);

return {
sharedLinks,
sharedLinks: sharedLinks.map((link) => ({
shareId: link.shareId,
title: link.conversation?.title || 'Untitled',
isPublic: link.isPublic,
createdAt: link.createdAt,
})),
totalCount: totalConvos,
pages: Math.ceil((totalConvos || 1) / pageSize),
pageNumber,
pageSize,
Expand Down Expand Up @@ -155,11 +183,14 @@ async function createSharedLink(user, conversationId) {
throw new ShareServiceError('Share already exists', 'SHARE_EXISTS');
}

const title = conversationMessages[0]?.title || 'Untitled';

const shareId = nanoid();
await SharedLink.create({
shareId,
conversationId,
messages: conversationMessages,
title,
user,
});

Expand Down
8 changes: 0 additions & 8 deletions api/models/schema/shareSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,6 @@ const shareSchema = mongoose.Schema(
index: true,
},
isPublic: {
type: Boolean,
default: false,
},
isVisible: {
type: Boolean,
default: false,
},
isAnonymous: {
type: Boolean,
default: true,
},
Expand Down
45 changes: 31 additions & 14 deletions api/server/routes/share.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,40 @@ if (allowSharedLinks) {
*/
router.get('/', requireJwtAuth, async (req, res) => {
try {
let pageNumber = req.query.pageNumber || 1;
pageNumber = parseInt(pageNumber, 10);
const params = {
pageNumber: Math.max(1, parseInt(req.query.pageNumber) || 1),
pageSize: Math.max(1, parseInt(req.query.pageSize) || 10),
isPublic: isEnabled(req.query.isPublic),
sortBy: ['createdAt', 'title'].includes(req.query.sortBy) ? req.query.sortBy : 'createdAt',
sortDirection: ['asc', 'desc'].includes(req.query.sortDirection)
? req.query.sortDirection
: 'desc',
search: req.query.search?.trim() || undefined,
};

if (isNaN(pageNumber) || pageNumber < 1) {
return res.status(400).json({ error: 'Invalid page number' });
}

let pageSize = req.query.pageSize || 25;
pageSize = parseInt(pageSize, 10);
const result = await getSharedLinks(
req.user.id,
params.pageNumber,
params.pageSize,
params.isPublic,
params.sortBy,
params.sortDirection,
params.search,
);

if (isNaN(pageSize) || pageSize < 1) {
return res.status(400).json({ error: 'Invalid page size' });
}
const isPublic = req.query.isPublic === 'true';
res.status(200).send(await getSharedLinks(req.user.id, pageNumber, pageSize, isPublic));
res.status(200).send({
links: result.sharedLinks,
totalCount: result.totalCount,
pageNumber: result.pageNumber,
pageSize: result.pageSize,
pages: result.pages,
});
} catch (error) {
res.status(500).json({ message: 'Error getting shared links' });
console.error('Error getting shared links:', error);
res.status(500).json({
message: 'Error getting shared links',
error: error.message,
});
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ export default function ConvoOptions({
/>
{showShareDialog && (
<ShareButton
title={title ?? ''}
conversationId={conversationId ?? ''}
open={showShareDialog}
onOpenChange={setShowShareDialog}
Expand Down
198 changes: 0 additions & 198 deletions client/src/components/Nav/SettingsTabs/Data/SharedLinkTable.tsx

This file was deleted.

Loading

0 comments on commit 4e6582e

Please sign in to comment.