diff --git a/docs.md b/docs.md new file mode 100644 index 0000000..add44e5 --- /dev/null +++ b/docs.md @@ -0,0 +1,75 @@ +# Docs for smk-chat +## API +### /api/getCredentialsById/:userId +IN: _userId_ (in route) +OUT: _lastname_, _firstname_, _middlename_ +Return credentials of the user whose id is userId. +Return -1 if no such user exist. +Requires client to be logged in. + +### /api/getIdByCredentials +IN: _lastname_, _firstname_, _middlename_ +OUT: _UserID_ +Returns an ID of the user, whose lastname, firstname and middlename were passed. +Returns -1 if user does not exist. +Requires client to be logged in. + +### /api/getMessagesFromChat/by-amount +IN: _chatId_, _amount_ +OUT: array of objects that represent a message, like {"author_id": ..., time_sent: ..., content: ...} +Returns an array of last _amount_ ordered by date of described objects if everything is okay +Returns HTTP/1.1 400 "Chat with id _ID_ does not exist." if no chat with supplied id does not exist. +Returns HTTP/1.1 403 "You are not a member of this chat" if the statement is true ;) +Requires client to be logged in. + +### /api/getMessagesFromChat/by-time +IN: _chatId_, _fromTimestamp_, _toTimestamp_ +OUT: array of objects that represent a message, like {"author_id": ..., time_sent: ..., content: ...} +_fromTimestamp_ must be lower that _toTimestamp_, otherwise it might return nothing. +Returns an array of described objects if everything is okay +Returns HTTP/1.1 400 "Chat with id _ID_ does not exist." if no chat with supplied id does not exist. +Returns HTTP/1.1 200 and an empty string if no messages in chat are found /shrug. +Requires client to be logged in. + +### /api/getChats +IN: _UserID_ +OUT: Array of chat IDs +Returs an array with ids of chats which user with passed ID is member in. +Return empty string if user has no membership in any chat. +Requires client to be logged in. + +### /api/getChatInfo/:chatId +IN: _chatId_ (in route) +OUT: JSON describes a chat with id: {name: ..., admins: ..., members: ...} +Note: JSON does not include messages. You have to use __/api/getMessagesFromChat__ to query messages. +Returns aforementioned json if the chat exists and user is a member of this chat. +Returns -1 if chat does not exist +Return -2 if user is not a member of the chat +Requires client to be logged in. + +### /api/createChat +IN: _UserId_, array of UserIDs that are to be invited. +OUT: "Ok" if successful, "User with id _MEMBERID_ does not exist." +Return -1 if amout of users to invite is 0. +Requires client to be logged in + +### /api/logout +IN: none. +OUT: redirect to __/login__. +Removes client's session, thus unlogging a user. +Requires client to be logged in. + +### /api/register +IN: _lastname_, _firstname_, _middlename_, _password_ +OUT: redirect to __/__ +Checks if user exist. If so, returns 400 with response "Such user exists.". +Otherwise, registers a user with given data. +Requires clinet to be not logged in. + +### /api/login +IN: _lastname_, _firstname_, _middlename_, _password_ +OUT: redirect to __/__ +Checks if user exists. If not, returns 400 with response "No such user.". +Otherwise, compares passwords +If passwords match, creating session and redirects to __/__ +Requires client to be not logged in. diff --git a/src/index.js b/src/index.js index 48f7be7..f5d5b84 100644 --- a/src/index.js +++ b/src/index.js @@ -167,11 +167,6 @@ app.get('/chat/:chatId', middleware.requireToBeLoggedIn, (req, res) => { //======================backend=========================== -//IN: userId (in route) -//OUT: lastname, firstname, middlename -//Return credentials of the user whose id is userId. -//Return -1 if no such user exist. -//Requires client to be logged in. app.get('/api/getCredentialsById/:id', middleware.requireToBeLoggedIn, async (req, res) => { try { const userId = req.params.id; @@ -182,15 +177,8 @@ app.get('/api/getCredentialsById/:id', middleware.requireToBeLoggedIn, async (re console.log(`[ERROR] in /api/getCredentialsById/${req.params.id}}: ${e}`) res.status(500).send(); } - }); -//IN: lastname, firstname, middlename -//OUT: UserID -//Returns an ID of the user, whose lastname, firstname and middlename were passed. -//Returns -1 if user does not exist. -//Requires client to be logged in. - app.get('/api/getIdByCredentials', middleware.requireToBeLoggedIn, async (req, res) => { try { const { lastname, firstname, middlename } = req.body; @@ -202,13 +190,6 @@ app.get('/api/getIdByCredentials', middleware.requireToBeLoggedIn, async (req, r } }); - -//IN: chatId, amount -//OUT: array of objects that represent a message, like {"author_id": ..., time_sent: ..., content: ...} -//Returns an array of last ${amount} ordered by date of described objects if everything is okay -//Returns HTTP/1.1 400 "Chat with id ${ID} does not exist." if no chat with supplied id does not exist. -//Returns HTTP/1.1 403 "You are not a member of this chat" if the statement is true ;) -//Requires client to be logged in. app.post('/api/getMessagesFromChat/by-amount', middleware.requireToBeLoggedIn, async (req, res) => { try { const userId = sessions[req.session.token].userId; @@ -245,14 +226,6 @@ app.post('/api/getMessagesFromChat/by-amount', middleware.requireToBeLoggedIn, a } }); - -//IN: chatId, fromTimestamp, toTimestamp -//OUT: array of objects that represent a message, like {"author_id": ..., time_sent: ..., content: ...} -//fromTimestamp must be lower that toTimestamp, otherwise it might return nothing. -//Returns an array of described objects if everything is okay -//Returns HTTP/1.1 400 "Chat with id ${ID} does not exist." if no chat with supplied id does not exist. -//Returns HTTP/1.1 200 and an empty string if no messages in chat are found /shrug. -//Requires client to be logged in. app.post('/api/getMessagesFromChat/by-time', middleware.requireToBeLoggedIn, async (req, res) => { try { const userId = sessions[req.session.token].userId; @@ -280,11 +253,6 @@ app.post('/api/getMessagesFromChat/by-time', middleware.requireToBeLoggedIn, asy } }); - -//IN: UserID -//OUT: Array of chat IDs -//Returs ids of chats which user with passed ID is member in. -//Return empty string if user has no membership in any chat. app.get('/api/getChats', middleware.requireToBeLoggedIn, async (req, res) => { try { const userId = sessions[req.session.token].userId @@ -297,13 +265,6 @@ app.get('/api/getChats', middleware.requireToBeLoggedIn, async (req, res) => { } }); -//IN: chatId (in route) -//OUT: JSON describes a chat with id: -//{name: ..., admins: ..., members: ...} -//Note: JSON does not include messages. You have to use /api/getMessagesFromChat to query messages. -//Returns aforementioned json if the chat exists and user is a member of this chat. -//Returns -1 if chat does not exist -//Return -2 if user is not a member of the chat app.get('/api/getChatInfo/:chatId', middleware.requireToBeLoggedIn, async (req, res) => { try { const chatId = req.params.chatId; @@ -321,8 +282,7 @@ app.get('/api/getChatInfo/:chatId', middleware.requireToBeLoggedIn, async (req, membersRequest.rows.forEach((member, index, membersRequest) => { members.push(member.id) }); - // console.log(members) - // console.log(sessions[req.session.token]) + if (!members.includes(sessions[req.session.token].userId)) { return res.send("-2").status(403).end(); }