Added undelete ways to api

This commit is contained in:
Akif9748 2022-08-26 20:12:23 +03:00
parent 79fa3d2e30
commit 14cf3b1237
5 changed files with 64 additions and 12 deletions

View file

@ -31,6 +31,10 @@ And, you can learn about API in `util/APIDOCS.md`.
## Roadmap ## Roadmap
### TO-DO:
- If thread deleted, not show its messages in API.
### Frontend
### User ### User
| To do | Is done? | Priority | | To do | Is done? | Priority |
| ----- | -------- | -------- | | ----- | -------- | -------- |
@ -66,16 +70,15 @@ And, you can learn about API in `util/APIDOCS.md`.
| Edit | 🔴 | MEDIUM | | Edit | 🔴 | MEDIUM |
### API ### API
| To do | Is done? | Priority | | To do | Is done?
| ----- | -------- | -------- | | ----- | --------
| RATELIMITS | 🟢 | MEDIUM | | RATELIMITS | 🟢
| Other clients for forum via API | 🟢 | LOW | | Get message**s** | 🟢
| Get message**s** | 🟢 | MEDIUM | | Create message & thread & user | 🟢
| Send message | 🟢 | MEDIUM | | Get message & thread & user | 🟢
| Create thread | 🟢 | MEDIUM | | Delete message & thread & user | 🟢
| Get info about thread | 🟢 | MEDIUM | | Undelete message & thread & user | 🟢
| Delete message & thread | 🟢 | HIGH | | Edit message & thread & user | 🔴
| Edit message & thread | 🔴 | HIGH |
### Other ### Other
| To do | Is done? | Priority | | To do | Is done? | Priority |

View file

@ -10,7 +10,7 @@ app.get("/:id", async (req, res) => {
const message = await MessageModel.get(req.params.id); const message = await MessageModel.get(req.params.id);
if (!message || (message.deleted && req.user && !req.user.admin)) return res.error(404, `We don't have any thread with id ${id}.`); if (!message || (message.deleted && req.user && !req.user.admin)) return res.error(404, `We don't have any message with id ${req.params.id}.`);
res.complate(message.toObject({ virtuals: true })); res.complate(message.toObject({ virtuals: true }));
@ -59,7 +59,8 @@ app.post("/:id/react/:type", async (req, res) => {
app.post("/:id/delete", async (req, res) => { app.post("/:id/delete", async (req, res) => {
const message = await MessageModel.get(req.params.id); const message = await MessageModel.get(req.params.id);
if (!message || (message.deleted && req.user && !req.user.admin)) return res.error(404, "We have not got any message declared as this id."); if (!message || (message.deleted && req.user && !req.user.admin))
return res.error(404, `We don't have any message with id ${req.params.id}.`);
const user = req.user; const user = req.user;
if (user.id != message.authorID && !user.admin) if (user.id != message.authorID && !user.admin)
return res.error(403, "You have not got permission for this."); return res.error(403, "You have not got permission for this.");
@ -70,4 +71,20 @@ app.post("/:id/delete", async (req, res) => {
}) })
app.post("/:id/undelete", async (req, res) => {
if (!req.user.admin) return res.error(403, "You have not got permission for this.");
const message = await MessageModel.get(req.params.id);
if (!message ) return res.error(404, `We don't have any message with id ${req.params.id}.`);
if (!message.deleted) return res.error(404, "This message is not deleted, first, delete it.");
message.deleted = false;
await message.save();
res.complate(message.toObject({ virtuals: true }));
})
module.exports = app; module.exports = app;

View file

@ -65,5 +65,19 @@ app.post("/:id/delete", async (req, res) => {
res.complate(thread.toObject({ virtuals: true })); res.complate(thread.toObject({ virtuals: true }));
}) })
app.post("/:id/undelete", async (req, res) => {
if (!req.user.admin) return res.error(403, "You have not got permission for this.");
const thread = await ThreadModel.get(req.params.id);
if (!thread ) return res.error(404, `We don't have any thread with id ${req.params.id}.`);
if (!thread.deleted) return res.error(404, "This thread is not deleted, first, delete it.");
thread.deleted = false;
await thread.save();
res.complate(thread.toObject({ virtuals: true }));
})
module.exports = app; module.exports = app;

View file

@ -29,6 +29,21 @@ app.post("/:id/delete/", async (req, res) => {
res.complate(member); res.complate(member);
}); });
app.post("/:id/undelete/", async (req, res) => {
if (!req.user.admin) return res.error(403, "You have not got permission for this.");
const member = await UserModel.get(id);
if (!member ) return res.error(404, `We don't have any user with id ${req.params.id}.`);
if (!member.deleted) return res.error(404, "This user is not deleted, first, delete it.");
member.deleted = false;
await member.save();
res.complate(member.toObject({ virtuals: true }));
})
app.post("/:id/admin/", async (req, res) => { app.post("/:id/admin/", async (req, res) => {
const user = req.user; const user = req.user;

View file

@ -19,16 +19,19 @@ You need this headers for send request to API:
### Request types: ### Request types:
- GET `/api/users/:id` for fetch user. - GET `/api/users/:id` for fetch user.
- POST `/api/users/:id/delete` for delete user. - POST `/api/users/:id/delete` for delete user.
- POST `/api/users/:id/undelete` for undelete user.
- POST `/api/users/:id/admin` for give admin permissions for a user. - POST `/api/users/:id/admin` for give admin permissions for a user.
- GET `/api/threads/:id` for fetch thread. - GET `/api/threads/:id` for fetch thread.
- GET `/api/threads/:id/messages/:limit` for fetch messages in thread. - GET `/api/threads/:id/messages/:limit` for fetch messages in thread.
- POST `/api/threads` for create thread. - POST `/api/threads` for create thread.
- POST `/api/threads/:id/delete` for delete thread. - POST `/api/threads/:id/delete` for delete thread.
- POST `/api/threads/:id/undelete` for undelete thread.
- GET `/api/messages/:id` for fetch message. - GET `/api/messages/:id` for fetch message.
- POST `/api/messages` for create message. - POST `/api/messages` for create message.
- POST `/api/messages/:id/delete` for delete message. - POST `/api/messages/:id/delete` for delete message.
- POST `/api/messages/:id/undelete` for undelete message.
- POST `/api/messages/:id/react/:type` for react to a message. - POST `/api/messages/:id/react/:type` for react to a message.
### Example request: ### Example request: