Added message post to api

This commit is contained in:
Akif9748 2022-04-03 21:39:26 +03:00
parent 6beeddd17e
commit 6b446d1b57
5 changed files with 76 additions and 9 deletions

View File

@ -78,6 +78,22 @@ app.get("/message/:id", (req, res) => {
})
app.post("/message/", (req, res) => {
const error = (status, error) =>
res.status(status).json(new ApiResponse(status, { error }));
const { threadID = null, content = null } = req.body;
const thread = new Thread().getId(threadID);
if (!req.body.content) return error(400, "Missing message content in request body.");
if (!thread) return error(404, "We have not got this thread.");
const message = new Message(content, new User().getName(req.headers.username), thread).takeId().write();
thread.push(message.id).write();
res.status(200).json(new ApiResponse(200, message));
})
app.get("/user/:id", (req, res) => {
const error = (status, error) =>

View File

@ -22,7 +22,7 @@ app.post("/", (req, res) => {
error(res, 404, `We have got an user named ${username}!`)
else {
const user2 = new User(req.body.username, req.body.avatar).takeId()
const user2 = new User(req.body.username, req.body.avatar ?? null).takeId()
db.set("secret." + username, { id: user2.id, key: password })
req.session.loggedin = true;
req.session.username = username;

View File

@ -19,6 +19,13 @@ app.get("/", (req, res) => {
});
app.get("/open*", (req, res) => {
const user = new User().getId(req.session.userid)
res.render("openThread", { user })
});
app.get("/:id", (req, res) => {
const { id } = req.params;
@ -34,17 +41,11 @@ app.get("/:id", (req, res) => {
});
app.use(require("../middlewares/login"));
app.get("/open*", (req, res) => {
const user = new User().getId(req.session.userid)
res.render("openThread", { user })
});
app.post("/", (req, res) => {
const user = new User().getId(req.session.userid);

50
tests/postmessage.py Normal file
View File

@ -0,0 +1,50 @@
import requests
# Headers for login to Akf-forum
headers = {
"username": "testUser",
"password": "testPassword"
}
# Body for message parameters
body = {
"content": "This message sent via API",
"threadID": 1
}
r = requests.post("http://localhost:3000/api/message/",
headers=headers, data=body)
print(r.json())
example_response = {
'status': 200,
'result': {
'content': 'This message sent via API',
'author': {
'name': 'testUser',
'avatar': '',
'time': 1649009854217,
'admin': False,
'deleted': False,
'id': 2
},
'time': 1649010863471,
'thread': {
'author': {
'name': 'Akif9748',
'avatar': 'https://www.technopat.net/sosyal/data/avatars/o/298/298223.jpg?1644694020',
'time': 1647895891332, 'admin': True, 'deleted': False, 'id': 0
},
'title': 'API TEST',
'messages': [4, 6],
'time': 1649010834064,
'deleted': False,
'id': 1
},
'deleted': False,
'edited': False,
'react': {},
'id': 6
}
}