+ 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:
shockrah
2021-05-06 13:29:32 -07:00
parent a628d56e25
commit 181b1cadc4
6 changed files with 136 additions and 95 deletions

View File

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

View File

@@ -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>,
}

View 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)
}
}