2023-03-22 01:31:01 -04:00
|
|
|
// const { Conversation } = require('./plugins');
|
|
|
|
|
const Conversation = require('./schema/convoSchema');
|
2023-02-07 16:22:35 -05:00
|
|
|
const { getMessages, deleteMessages } = require('./Message');
|
2023-02-06 14:05:02 -05:00
|
|
|
|
2023-03-14 01:24:43 +08:00
|
|
|
const getConvo = async (user, conversationId) => {
|
feat: Auth and User System (#205)
* server-side JWT auth implementation
* move oauth routes and strategies, fix bugs
* backend modifications for wiring up the frontend login and reg forms
* Add frontend data services for login and registration
* Add login and registration forms
* Implment auth context, functional client side auth
* protect routes with jwt auth
* finish local strategy (using local storage)
* Start setting up google auth
* disable token refresh, remove old auth middleware
* refactor client, add ApiErrorBoundary context
* disable google and facebook strategies
* fix: fix presets not displaying specific to user
* fix: fix issue with browser refresh
* fix: casing issue with User.js (#11)
* delete user.js to be renamed
* fix: fix casing issue with User.js
* comment out api error watcher temporarily
* fix: issue with api error watcher (#12)
* delete user.js to be renamed
* fix: fix casing issue with User.js
* comment out api error watcher temporarily
* feat: add google auth social login
* fix: make google login url dynamic based on dev/prod
* fix: bug where UI is briefly displayed before redirecting to login
* fix: fix cookie expires value for local auth
* Update README.md
* Update LOCAL_INSTALL structure
* Add local testing instructions
* Only load google strategy if client id and secret are provided
* Update .env.example files with new params
* fix issue with not redirecting to register form
* only show google login button if value is set in .env
* cleanup log messages
* Add label to button for google login on login form
* doc: fix client/server url values in .env.example
* feat: add error message details to registration failure
* Restore preventing paste on confirm password
* auto-login user after registering
* feat: forgot password (#24)
* make login/reg pages look like openai's
* add password reset data services
* new form designs similar to openai, add password reset pages
* add api's for password reset
* email utils for password reset
* remove bcrypt salt rounds from process.env
* refactor: restructure api auth code, consolidate routes (#25)
* add api's for password reset
* remove bcrypt salt rounds from process.env
* refactor: consolidate auth routes, use controller pattern
* refactor: code cleanup
* feat: migrate data to first user (#26)
* refactor: use /api for auth routes
* fix: use user id instead of username
* feat: migrate data to first user on register
* fix: fix social login routes after refactor (#27)
* refactor: use /api for auth routes
* fix: use user id instead of username
* feat: migrate data to first user on register
* fix: fix social login routes
* fix: issue with auto-login when logging out then logging in with new browser window (#28)
* refactor: use /api for auth routes
* fix: use user id instead of username
* feat: migrate data to first user on register
* fix: fix social login routes
* fix: fix issue with auto-login in new tab
* doc: Update README and .env.example files with user system information (#29)
* refactor: use /api for auth routes
* fix: use user id instead of username
* feat: migrate data to first user on register
* fix: fix social login routes
* fix: fix issue with auto-login in new tab
* doc: update README and .env.example files
* Fixup: LOCAL_INSTALL.md PS instructions (#200) (#30)
Co-authored-by: alfredo-f <alfredo.fomitchenko@mail.polimi.it>
* feat: send user with completion to protect against abuse (#31)
* Fixup: LOCAL_INSTALL.md PS instructions (#200)
* server-side JWT auth implementation
* move oauth routes and strategies, fix bugs
* backend modifications for wiring up the frontend login and reg forms
* Add frontend data services for login and registration
* Add login and registration forms
* Implment auth context, functional client side auth
* protect routes with jwt auth
* finish local strategy (using local storage)
* Start setting up google auth
* disable token refresh, remove old auth middleware
* refactor client, add ApiErrorBoundary context
* disable google and facebook strategies
* fix: fix presets not displaying specific to user
* fix: fix issue with browser refresh
* fix: casing issue with User.js (#11)
* delete user.js to be renamed
* fix: fix casing issue with User.js
* comment out api error watcher temporarily
* feat: add google auth social login
* fix: make google login url dynamic based on dev/prod
* fix: bug where UI is briefly displayed before redirecting to login
* fix: fix cookie expires value for local auth
* Only load google strategy if client id and secret are provided
* Update .env.example files with new params
* fix issue with not redirecting to register form
* only show google login button if value is set in .env
* cleanup log messages
* Add label to button for google login on login form
* doc: fix client/server url values in .env.example
* feat: add error message details to registration failure
* Restore preventing paste on confirm password
* auto-login user after registering
* feat: forgot password (#24)
* make login/reg pages look like openai's
* add password reset data services
* new form designs similar to openai, add password reset pages
* add api's for password reset
* email utils for password reset
* remove bcrypt salt rounds from process.env
* refactor: restructure api auth code, consolidate routes (#25)
* add api's for password reset
* remove bcrypt salt rounds from process.env
* refactor: consolidate auth routes, use controller pattern
* refactor: code cleanup
* feat: migrate data to first user (#26)
* refactor: use /api for auth routes
* fix: use user id instead of username
* feat: migrate data to first user on register
* fix: fix social login routes after refactor (#27)
* refactor: use /api for auth routes
* fix: use user id instead of username
* feat: migrate data to first user on register
* fix: fix social login routes
* fix: issue with auto-login when logging out then logging in with new browser window (#28)
* refactor: use /api for auth routes
* fix: use user id instead of username
* feat: migrate data to first user on register
* fix: fix social login routes
* fix: fix issue with auto-login in new tab
* doc: Update README and .env.example files with user system information (#29)
* refactor: use /api for auth routes
* fix: use user id instead of username
* feat: migrate data to first user on register
* fix: fix social login routes
* fix: fix issue with auto-login in new tab
* doc: update README and .env.example files
* Send user id to openai to protect against abuse
* add meilisearch to gitignore
* Remove webpack
---------
Co-authored-by: alfredo-f <alfredo.fomitchenko@mail.polimi.it>
---------
Co-authored-by: Danny Avila <110412045+danny-avila@users.noreply.github.com>
Co-authored-by: Alfredo Fomitchenko <alfredo.fomitchenko@mail.polimi.it>
2023-05-07 10:04:51 -07:00
|
|
|
console.log('getConvo -> userId', user);
|
2023-03-08 22:30:29 -05:00
|
|
|
try {
|
2023-03-14 01:24:43 +08:00
|
|
|
return await Conversation.findOne({ user, conversationId }).exec();
|
2023-03-08 22:30:29 -05:00
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error);
|
|
|
|
|
return { message: 'Error getting single conversation' };
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-02-06 14:05:02 -05:00
|
|
|
module.exports = {
|
2023-03-17 19:58:13 -04:00
|
|
|
Conversation,
|
2023-04-11 03:26:38 +08:00
|
|
|
saveConvo: async (user, { conversationId, newConversationId, ...convo }) => {
|
2023-02-08 00:02:29 -05:00
|
|
|
try {
|
|
|
|
|
const messages = await getMessages({ conversationId });
|
2023-04-11 03:26:38 +08:00
|
|
|
const update = { ...convo, messages, user };
|
2023-03-15 00:54:50 +08:00
|
|
|
if (newConversationId) {
|
|
|
|
|
update.conversationId = newConversationId;
|
|
|
|
|
}
|
2023-02-06 14:05:02 -05:00
|
|
|
|
2023-04-11 03:26:38 +08:00
|
|
|
return await Conversation.findOneAndUpdate({ conversationId: conversationId, user }, update, {
|
|
|
|
|
new: true,
|
|
|
|
|
upsert: true
|
2023-03-28 01:19:44 +08:00
|
|
|
}).exec();
|
2023-02-11 10:22:15 -05:00
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error);
|
2023-04-11 03:26:38 +08:00
|
|
|
return { message: 'Error saving conversation' };
|
2023-02-11 10:22:15 -05:00
|
|
|
}
|
|
|
|
|
},
|
2023-03-14 01:24:43 +08:00
|
|
|
getConvosByPage: async (user, pageNumber = 1, pageSize = 12) => {
|
2023-03-07 14:22:33 -05:00
|
|
|
try {
|
2023-03-16 13:32:04 -04:00
|
|
|
const totalConvos = (await Conversation.countDocuments({ user })) || 1;
|
2023-03-15 04:05:14 +08:00
|
|
|
const totalPages = Math.ceil(totalConvos / pageSize);
|
2023-03-16 13:32:04 -04:00
|
|
|
const convos = await Conversation.find({ user })
|
2023-03-14 14:51:26 -04:00
|
|
|
.sort({ createdAt: -1, created: -1 })
|
2023-03-15 04:05:14 +08:00
|
|
|
.skip((pageNumber - 1) * pageSize)
|
2023-03-07 14:22:33 -05:00
|
|
|
.limit(pageSize)
|
|
|
|
|
.exec();
|
2023-03-15 04:05:14 +08:00
|
|
|
return { conversations: convos, pages: totalPages, pageNumber, pageSize };
|
2023-03-07 14:22:33 -05:00
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error);
|
|
|
|
|
return { message: 'Error getting conversations' };
|
|
|
|
|
}
|
|
|
|
|
},
|
2023-03-18 01:40:49 -04:00
|
|
|
getConvosQueried: async (user, convoIds, pageNumber = 1, pageSize = 12) => {
|
|
|
|
|
try {
|
|
|
|
|
if (!convoIds || convoIds.length === 0) {
|
|
|
|
|
return { conversations: [], pages: 1, pageNumber, pageSize };
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-18 17:49:24 -04:00
|
|
|
const cache = {};
|
2023-03-22 16:06:11 -04:00
|
|
|
const convoMap = {};
|
2023-03-18 17:49:24 -04:00
|
|
|
const promises = [];
|
2023-03-18 23:18:36 -04:00
|
|
|
// will handle a syncing solution soon
|
|
|
|
|
const deletedConvoIds = [];
|
2023-03-18 17:49:24 -04:00
|
|
|
|
2023-04-11 03:26:38 +08:00
|
|
|
convoIds.forEach((convo) =>
|
2023-03-18 23:18:36 -04:00
|
|
|
promises.push(
|
|
|
|
|
Conversation.findOne({
|
|
|
|
|
user,
|
2023-03-28 01:19:44 +08:00
|
|
|
conversationId: convo.conversationId
|
2023-03-18 23:18:36 -04:00
|
|
|
}).exec()
|
|
|
|
|
)
|
|
|
|
|
);
|
2023-03-18 17:49:24 -04:00
|
|
|
|
2023-03-18 23:18:36 -04:00
|
|
|
const results = (await Promise.all(promises)).filter((convo, i) => {
|
|
|
|
|
if (!convo) {
|
|
|
|
|
deletedConvoIds.push(convoIds[i].conversationId);
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
const page = Math.floor(i / pageSize) + 1;
|
|
|
|
|
if (!cache[page]) {
|
|
|
|
|
cache[page] = [];
|
|
|
|
|
}
|
|
|
|
|
cache[page].push(convo);
|
2023-03-22 16:06:11 -04:00
|
|
|
convoMap[convo.conversationId] = convo;
|
2023-03-18 23:18:36 -04:00
|
|
|
return true;
|
|
|
|
|
}
|
2023-03-18 01:40:49 -04:00
|
|
|
});
|
2023-03-18 23:18:36 -04:00
|
|
|
|
2023-03-18 17:49:24 -04:00
|
|
|
// const startIndex = (pageNumber - 1) * pageSize;
|
|
|
|
|
// const convos = results.slice(startIndex, startIndex + pageSize);
|
2023-03-18 01:40:49 -04:00
|
|
|
const totalPages = Math.ceil(results.length / pageSize);
|
2023-03-18 17:49:24 -04:00
|
|
|
cache.pages = totalPages;
|
|
|
|
|
cache.pageSize = pageSize;
|
|
|
|
|
return {
|
|
|
|
|
cache,
|
2023-03-18 23:18:36 -04:00
|
|
|
conversations: cache[pageNumber] || [],
|
|
|
|
|
pages: totalPages || 1,
|
2023-03-18 17:49:24 -04:00
|
|
|
pageNumber,
|
2023-03-18 23:18:36 -04:00
|
|
|
pageSize,
|
|
|
|
|
// will handle a syncing solution soon
|
2023-03-22 16:06:11 -04:00
|
|
|
filter: new Set(deletedConvoIds),
|
|
|
|
|
convoMap
|
2023-03-18 17:49:24 -04:00
|
|
|
};
|
2023-03-18 01:40:49 -04:00
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error);
|
|
|
|
|
return { message: 'Error fetching conversations' };
|
|
|
|
|
}
|
|
|
|
|
},
|
2023-03-08 22:30:29 -05:00
|
|
|
getConvo,
|
2023-03-20 01:35:02 -04:00
|
|
|
/* chore: this method is not properly error handled */
|
2023-03-14 01:24:43 +08:00
|
|
|
getConvoTitle: async (user, conversationId) => {
|
2023-03-07 14:22:33 -05:00
|
|
|
try {
|
2023-03-14 01:24:43 +08:00
|
|
|
const convo = await getConvo(user, conversationId);
|
2023-03-20 01:35:02 -04:00
|
|
|
/* ChatGPT Browser was triggering error here due to convo being saved later */
|
|
|
|
|
if (convo && !convo.title) {
|
|
|
|
|
return null;
|
|
|
|
|
} else {
|
2023-03-20 02:30:08 -04:00
|
|
|
// TypeError: Cannot read properties of null (reading 'title')
|
|
|
|
|
return convo?.title || 'New Chat';
|
2023-03-20 01:35:02 -04:00
|
|
|
}
|
2023-03-07 14:22:33 -05:00
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error);
|
2023-03-28 01:19:44 +08:00
|
|
|
return { message: 'Error getting conversation title' };
|
2023-03-07 14:22:33 -05:00
|
|
|
}
|
2023-03-05 14:41:50 -05:00
|
|
|
},
|
2023-03-14 01:24:43 +08:00
|
|
|
deleteConvos: async (user, filter) => {
|
2023-03-31 03:22:57 +08:00
|
|
|
let toRemove = await Conversation.find({ ...filter, user }).select('conversationId');
|
2023-04-11 03:26:38 +08:00
|
|
|
const ids = toRemove.map((instance) => instance.conversationId);
|
2023-03-31 03:22:57 +08:00
|
|
|
let deleteCount = await Conversation.deleteMany({ ...filter, user }).exec();
|
|
|
|
|
deleteCount.messages = await deleteMessages({ conversationId: { $in: ids } });
|
2023-02-07 16:22:35 -05:00
|
|
|
return deleteCount;
|
|
|
|
|
}
|
2023-02-06 14:05:02 -05:00
|
|
|
};
|