Broken code please help I'm dumb

pull/5/head
~erin 2021-07-17 19:41:41 -04:00
parent 8e6663a4df
commit fd0405f55a
Signed by: erin
GPG Key ID: DA70E064A8C70F44
7 changed files with 717 additions and 218 deletions

801
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
[package]
name = "pogchat"
version = "0.2.0"
version = "0.3.0"
authors = ["Erin Nova <erin@the-system.eu.org>"]
edition = "2018"
@ -15,3 +15,9 @@ uuid = { version = "0.8.1", features = ["serde", "v4"] }
log = "0.4.0"
env_logger = "0.8.4"
chrono = { version = "0.4.11", features = ["serde"] }
serde_derive = "1.0"
[dependencies.rocket_contrib]
version = "0.4.10"
default-features = false
features = ["json"]

View File

@ -29,3 +29,6 @@ Whenever user sends a message, client will send message & token and backend will
- [ ] Basic messaging system
- [ ] Token generation & storage
- [x] Pronouns
- [ ] Change pronouns
- [ ] User management (banning, etc.)
- [ ] Blacklist words from chat/names

View File

@ -20,99 +20,7 @@ pub fn index() -> &'static str {
`GET /api/about/pronouns/<name>` Get the pronouns of a user"
}
/*
// The output is wrapped in a Result to allow matching on errors
// Returns an Iterator to the Reader of the lines of the file.
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where P: AsRef<Path>, {
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}
// Function to read json from file into the vector
fn read_json() -> Vec<User> {
// Create path to file
let path = Path::new("users.json");
let display = path.display();
let mut users: Vec<User> = Vec::new(); // Create an empty vector of users
// Read through the lines and append them to the array
if let Ok(lines) = read_lines(&path) {
for line in lines {
if let Ok(user) = line {
info!("read {} from json file {}", display, &user);
// Parse line from file into a data structure
let user: User = serde_json::from_str(&user).unwrap();
users.push(user);
}
}
}
return users;
}
// Function to append the last value of the users vector to the file
fn append_json(users_list: &Vec<User>) -> Result<()> {
// Create a file to write to
let path = Path::new("users.json");
let display = path.display();
let mut file = match OpenOptions::new()
.write(true)
.create(true)
.append(true)
.open(&path){
Err(why) => panic!("couldn't create {}: {}", display, why),
Ok(file) => file,
};
// Serialize the last user value
let users_json = serde_json::to_string(&users_list[users_list.len()-1])?;
// Write to the file
match file.write_all(users_json.as_bytes()) {
Err(why) => panic!("couldn't write to {}: {}", display, why),
Ok(_) => info!("succesfully wrote to {}", display),
};
// Add newline
match file.write_all("\n".as_bytes()) {
Err(why) => panic!("couldn't write to {}: {}", display, why),
Ok(_) => info!("succesfully wrote to {}", display),
};
Ok(())
}
// Function to write whole vector of users to file
fn write_json(users_list: &Vec<User>) -> Result<()> {
// Create a file to write to
let path = Path::new("users.json");
let display = path.display();
let mut file = match OpenOptions::new()
.write(true)
.create(true)
.open(&path){
Err(why) => panic!("couldn't create {}: {}", display, why),
Ok(file) => file,
};
let mut users_json = String::new();
for i in 0..users_list.len() {
// Serialize the users
users_json += &serde_json::to_string(&users_list[i])?;
if i <= users_list.len()-2 { // don't append newline if it's the last element
users_json += "\n";
}
}
// Write to the file
match file.write_all(users_json.as_bytes()) {
Err(why) => panic!("couldn't write to {}: {}", display, why),
Ok(_) => info!("succesfully wrote to {}", display),
};
Ok(())
}
*/
// Post request to register a user and pin
#[post("/api/register/<name>/<pin>/<pronouns>")]
pub fn register_user(name: &str, pin: i32, pronouns: &str) -> String {

15
src/chat.rs Normal file
View File

@ -0,0 +1,15 @@
/* Contains Rocket code for chat/message functionality */
extern crate log;
extern crate rocket_contrib;
use crate::message::Message;
extern crate serde_derive;
use rocket_contrib::json::{Json, JsonValue};
use rocket::{data::FromData, response::Responder};
#[post("/api/message/send", format = "json", data = "<message>")]
pub fn send_message(message: Json<Message>) -> JsonValue {
json!({
"status": "ok",
"reason": "bruh"
})
}

View File

@ -1,12 +1,14 @@
#[macro_use]
extern crate log;
#[macro_use] extern crate log;
#[macro_use] extern crate rocket;
#[macro_use] extern crate rocket_contrib;
use rocket::fairing::AdHoc;
mod auth;
mod user;
mod message;
mod file_io;
mod chat;
#[launch]
fn rocket() -> _ {
@ -22,7 +24,7 @@ fn rocket() -> _ {
rocket::build()
.mount(
"/",
routes![auth::index, auth::get_user, auth::register_user, auth::check_pin, auth::change, auth::get_user_name, auth::get_user_pronouns],
routes![auth::index, auth::get_user, auth::register_user, auth::check_pin, auth::change, auth::get_user_name, auth::get_user_pronouns, chat::send_message],
)
.attach(cors_fairing)
}

View File

@ -1,11 +1,15 @@
use uuid::Uuid;
use chrono::prelude::*;
use crate::user::User;
use serde::{Deserialize, Serialize};
#[derive(Clone)]
#[derive(Deserialize, Serialize)]
pub struct Message {
/*
pub id: Uuid,
pub user: User,
pub body: String,
pub created_at: DateTime<Utc>,
pub created_at: DateTime<Utc>, */
id: u8,
body: String,
}