main
june moretz 2023-05-08 23:51:17 -04:00
parent 8c7d8ce512
commit 57505e719d
9 changed files with 109 additions and 10 deletions

46
DOCS.md
View File

@ -48,7 +48,8 @@ Response format:
```json ```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 ```json
{ {
"name": "unique_username", "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 # sent by server
## new_message ## new_message
@ -90,6 +129,7 @@ Response format:
{ {
"id": 69, "id": 69,
"name": "unique_username", "name": "unique_username",
"message": "msg text" "message": "msg text",
"thread": 1 // thread ID
} }
``` ```

View File

@ -13,7 +13,10 @@ CREATE TABLE authentications (
CREATE TABLE threads ( CREATE TABLE threads (
id integer primary key asc, 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 ( CREATE TABLE posts (
@ -26,4 +29,4 @@ CREATE TABLE posts (
foreign key(thread) references threads(id) foreign key(thread) references threads(id)
); );
INSERT INTO threads default values; INSERT INTO threads (name) values ("meow");

View File

@ -2,10 +2,14 @@ const create_user = require("./create_user");
const get_history = require("./get_history"); const get_history = require("./get_history");
const send_message = require("./send_message"); const send_message = require("./send_message");
const authenticate = require("./authenticate"); const authenticate = require("./authenticate");
const create_thread = require("./create_thread");
const list_threads = require("./list_threads");
module.exports = { module.exports = {
create_user, create_user,
get_history, get_history,
send_message, send_message,
authenticate, authenticate,
create_thread,
list_threads,
}; };

View File

@ -53,7 +53,7 @@ const authenticate = async (msg, respond, socket) => {
}); });
} }
} catch (err) { } catch (err) {
console.err("error in authentication: " + err); console.error("error in authentication: " + err);
return respond({ return respond({
success: false, success: false,
message: "Message signature verification failed", message: "Message signature verification failed",

23
src/create_thread.js Normal file
View File

@ -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;

View File

@ -14,6 +14,7 @@ const create_user = async (msg, respond) => {
msg.name, msg.name,
]); ]);
if (result.rows.length > 0) { if (result.rows.length > 0) {
console.log(result);
return respond({ return respond({
success: false, success: false,
message: "A user with this name already exists on this server", message: "A user with this name already exists on this server",

View File

@ -7,13 +7,20 @@ const get_history = async (msg, respond) => {
message: "before must be a number", message: "before must be a number",
}); });
} }
if (!msg.thread) {
return respond({
success: false,
message: "thread ID required",
});
}
const messages = await db.query( const messages = await db.query(
`select users.name, posts.id, content from posts `select users.name, posts.id, content from posts
join users on posts.user = users.id 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 order by posts.created desc
limit 101`, limit 101`,
msg.before ? [msg.before] : [] msg.before ? [msg.before, msg.thread] : [msg.thread]
); );
return respond({ return respond({
success: true, success: true,

14
src/list_threads.js Normal file
View File

@ -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;

View File

@ -2,15 +2,22 @@ const db = require("../db");
const authwrap = require("./authwrap"); const authwrap = require("./authwrap");
const send_message = async (msg, respond, socket) => { 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 // add message and send it to everyone
const id = await db.query( const id = await db.query(
"insert into posts (user, thread, content) values (?, 1, ?) returning id", "insert into posts (user, thread, content) values (?, ?, ?) returning id",
[msg.auth_user.id, msg.message] [msg.auth_user.id, msg.thread, msg.message]
); );
socket.broadcast.emit("new_message", { socket.broadcast.emit("new_message", {
id: id.rows[0].id, id: id.rows[0].id,
name: msg.name, name: msg.name,
message: msg.message, message: msg.message,
thread: msg.thread,
}); });
return respond({ return respond({
success: true, success: true,