2023-05-05 19:33:05 -07:00
|
|
|
const db = require("../db");
|
2023-05-07 18:43:57 -07:00
|
|
|
const authwrap = require("./authwrap");
|
2023-05-05 19:33:05 -07:00
|
|
|
|
|
|
|
const send_message = async (msg, respond, socket) => {
|
2023-05-08 20:51:17 -07:00
|
|
|
if (!msg.thread) {
|
|
|
|
return respond({
|
|
|
|
success: false,
|
|
|
|
message: "thread ID required",
|
|
|
|
});
|
|
|
|
}
|
2023-05-07 18:43:57 -07:00
|
|
|
// add message and send it to everyone
|
|
|
|
const id = await db.query(
|
2023-05-08 20:51:17 -07:00
|
|
|
"insert into posts (user, thread, content) values (?, ?, ?) returning id",
|
|
|
|
[msg.auth_user.id, msg.thread, msg.message]
|
2023-05-07 18:43:57 -07:00
|
|
|
);
|
|
|
|
socket.broadcast.emit("new_message", {
|
|
|
|
id: id.rows[0].id,
|
2023-05-12 17:44:07 -07:00
|
|
|
name: msg.auth_user.name,
|
2023-05-07 18:43:57 -07:00
|
|
|
message: msg.message,
|
2023-05-08 20:51:17 -07:00
|
|
|
thread: msg.thread,
|
2023-05-07 18:43:57 -07:00
|
|
|
});
|
|
|
|
return respond({
|
|
|
|
success: true,
|
|
|
|
id: id.rows[0].id,
|
|
|
|
});
|
2023-05-05 19:33:05 -07:00
|
|
|
};
|
|
|
|
|
2023-05-07 18:43:57 -07:00
|
|
|
module.exports = authwrap(send_message);
|