threads
parent
8c7d8ce512
commit
57505e719d
46
DOCS.md
46
DOCS.md
|
@ -48,7 +48,8 @@ Response format:
|
|||
|
||||
```json
|
||||
{
|
||||
"before": 100 // message ID. if ommitted just starts from end
|
||||
"before": 100, // message ID. if ommitted just starts from end
|
||||
"thread": 1 // thread ID
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -69,7 +70,8 @@ Message format:
|
|||
```json
|
||||
{
|
||||
"name": "unique_username",
|
||||
"message": "message"
|
||||
"message": "message",
|
||||
"thread": 1 // thread ID
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -82,6 +84,43 @@ Response format:
|
|||
}
|
||||
```
|
||||
|
||||
## create_thread
|
||||
|
||||
Message format:
|
||||
|
||||
```json
|
||||
{
|
||||
"name": "thread name"
|
||||
}
|
||||
```
|
||||
|
||||
Response format:
|
||||
|
||||
```json
|
||||
{
|
||||
"success": true,
|
||||
"id": 111
|
||||
}
|
||||
```
|
||||
|
||||
## list_threads
|
||||
|
||||
no request message
|
||||
|
||||
Response format:
|
||||
|
||||
```json
|
||||
{
|
||||
"success": true,
|
||||
"threads": [
|
||||
{
|
||||
"id": 1,
|
||||
"name": "meow"
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
# sent by server
|
||||
|
||||
## new_message
|
||||
|
@ -90,6 +129,7 @@ Response format:
|
|||
{
|
||||
"id": 69,
|
||||
"name": "unique_username",
|
||||
"message": "msg text"
|
||||
"message": "msg text",
|
||||
"thread": 1 // thread ID
|
||||
}
|
||||
```
|
||||
|
|
|
@ -13,7 +13,10 @@ CREATE TABLE authentications (
|
|||
|
||||
CREATE TABLE threads (
|
||||
id integer primary key asc,
|
||||
created timestamp default current_timestamp
|
||||
name text,
|
||||
creator integer,
|
||||
created timestamp default current_timestamp,
|
||||
foreign key(creator) references users(id)
|
||||
);
|
||||
|
||||
CREATE TABLE posts (
|
||||
|
@ -26,4 +29,4 @@ CREATE TABLE posts (
|
|||
foreign key(thread) references threads(id)
|
||||
);
|
||||
|
||||
INSERT INTO threads default values;
|
||||
INSERT INTO threads (name) values ("meow");
|
|
@ -2,10 +2,14 @@ const create_user = require("./create_user");
|
|||
const get_history = require("./get_history");
|
||||
const send_message = require("./send_message");
|
||||
const authenticate = require("./authenticate");
|
||||
const create_thread = require("./create_thread");
|
||||
const list_threads = require("./list_threads");
|
||||
|
||||
module.exports = {
|
||||
create_user,
|
||||
get_history,
|
||||
send_message,
|
||||
authenticate,
|
||||
create_thread,
|
||||
list_threads,
|
||||
};
|
||||
|
|
|
@ -53,7 +53,7 @@ const authenticate = async (msg, respond, socket) => {
|
|||
});
|
||||
}
|
||||
} catch (err) {
|
||||
console.err("error in authentication: " + err);
|
||||
console.error("error in authentication: " + err);
|
||||
return respond({
|
||||
success: false,
|
||||
message: "Message signature verification failed",
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
const db = require("../db");
|
||||
|
||||
const create_thread = async (msg, respond) => {
|
||||
// validate inputs
|
||||
if (!msg.name) {
|
||||
return respond({
|
||||
success: false,
|
||||
message: "Thread name required",
|
||||
});
|
||||
}
|
||||
// add to db
|
||||
const insert = await db.query(
|
||||
"insert into threads (name, creator) values (?, ?) returning id",
|
||||
[msg.name, msg.auth_user.id]
|
||||
);
|
||||
// respond
|
||||
return respond({
|
||||
success: true,
|
||||
id: insert.rows[0].id,
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = create_thread;
|
|
@ -14,6 +14,7 @@ const create_user = async (msg, respond) => {
|
|||
msg.name,
|
||||
]);
|
||||
if (result.rows.length > 0) {
|
||||
console.log(result);
|
||||
return respond({
|
||||
success: false,
|
||||
message: "A user with this name already exists on this server",
|
||||
|
|
|
@ -7,13 +7,20 @@ const get_history = async (msg, respond) => {
|
|||
message: "before must be a number",
|
||||
});
|
||||
}
|
||||
if (!msg.thread) {
|
||||
return respond({
|
||||
success: false,
|
||||
message: "thread ID required",
|
||||
});
|
||||
}
|
||||
const messages = await db.query(
|
||||
`select users.name, posts.id, content from posts
|
||||
join users on posts.user = users.id
|
||||
${msg.before ? "where posts.id < ?" : ""}
|
||||
${msg.before ? "where posts.id < ? and" : "where"}
|
||||
thread = ?
|
||||
order by posts.created desc
|
||||
limit 101`,
|
||||
msg.before ? [msg.before] : []
|
||||
msg.before ? [msg.before, msg.thread] : [msg.thread]
|
||||
);
|
||||
return respond({
|
||||
success: true,
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
const db = require("../db");
|
||||
|
||||
const list_threads = async (msg, respond) => {
|
||||
const threads = await db.query(
|
||||
"select name, id from threads order by created desc"
|
||||
);
|
||||
// respond
|
||||
return respond({
|
||||
success: true,
|
||||
threads: threads.rows,
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = list_threads;
|
|
@ -2,15 +2,22 @@ const db = require("../db");
|
|||
const authwrap = require("./authwrap");
|
||||
|
||||
const send_message = async (msg, respond, socket) => {
|
||||
if (!msg.thread) {
|
||||
return respond({
|
||||
success: false,
|
||||
message: "thread ID required",
|
||||
});
|
||||
}
|
||||
// add message and send it to everyone
|
||||
const id = await db.query(
|
||||
"insert into posts (user, thread, content) values (?, 1, ?) returning id",
|
||||
[msg.auth_user.id, msg.message]
|
||||
"insert into posts (user, thread, content) values (?, ?, ?) returning id",
|
||||
[msg.auth_user.id, msg.thread, msg.message]
|
||||
);
|
||||
socket.broadcast.emit("new_message", {
|
||||
id: id.rows[0].id,
|
||||
name: msg.name,
|
||||
message: msg.message,
|
||||
thread: msg.thread,
|
||||
});
|
||||
return respond({
|
||||
success: true,
|
||||
|
|
Loading…
Reference in New Issue