+ Exposing serde_json to db-lib lyaer
This dep was already there but just being used (as we're using serde). Now we can use it at the db layer which makes neighbor struct creation easier * Cargo locks are being updated to reflect new dependancy changes These changes actually reduce the dependancy count overall + Adding a very simple table to hold all the neighbors that an instance may want to reference At some point we may want to support vanity join urls and this would be a good place to reference those There would also need to be a way for admins to add/edit/remove vanity urls but that's for another patch
This commit is contained in:
@@ -10,5 +10,6 @@ edition = "2018"
|
||||
mysql_async = "0.27"
|
||||
|
||||
serde = { version = "1.0.117", features = [ "derive" ] }
|
||||
serde_json = "1.0"
|
||||
tokio = { version = "1", features = ["fs", "io-util"] }
|
||||
rand = "0.8.3"
|
||||
|
||||
@@ -5,6 +5,7 @@ pub mod common;
|
||||
pub mod invites;
|
||||
pub mod channels;
|
||||
pub mod messages;
|
||||
pub mod neighbors;
|
||||
|
||||
use std::vec::Vec;
|
||||
|
||||
@@ -90,3 +91,11 @@ pub struct PublicMember {
|
||||
pub permissions: UBigInt,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct Neighbor {
|
||||
pub name: String,
|
||||
pub description: Option<String>,
|
||||
pub tags: Vec<String>,
|
||||
pub url: String,
|
||||
pub wsurl: Option<String>,
|
||||
}
|
||||
|
||||
19
json-api/db/src/neighbors.rs
Normal file
19
json-api/db/src/neighbors.rs
Normal file
@@ -0,0 +1,19 @@
|
||||
use crate::Neighbor;
|
||||
use mysql_async::Result as SqlResult;
|
||||
use mysql_async::{Pool, prelude::Queryable};
|
||||
use serde_json;
|
||||
|
||||
impl Neighbor {
|
||||
pub async fn get_all(p: &Pool) -> SqlResult<Vec<Neighbor>> {
|
||||
let mut conn = p.get_conn().await?;
|
||||
let q = "SELECT name, description, tags, url, wsurl FROM neighbors";
|
||||
type Types = (String, Option<String>, String, String, Option<String>);
|
||||
let set: Vec<Neighbor> = conn.exec_map(q, (), |(name, description, tags, url, wsurl): Types| {
|
||||
let json: Vec<String> = serde_json::from_str(tags.as_str()).unwrap_or(vec![]);
|
||||
Neighbor {
|
||||
name, description, tags: json, url, wsurl
|
||||
}
|
||||
}).await?;
|
||||
Ok(set)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user