Actually follow/unfollow and store to sled

there's something wrong here though, it sometimes doesn't seem to work...
This commit is contained in:
Daan Wynen 2022-09-27 21:18:19 +02:00
parent 99ba9adb80
commit 85824b87e4
2 changed files with 37 additions and 5 deletions

View File

@ -5,7 +5,7 @@ use rocket_dyn_templates::{context, Template};
use crate::sbot;
use crate::utils;
use crate::db::Database;
use crate::db::{Database, Peer};
#[derive(FromForm)]
pub struct PeerForm {
@ -30,8 +30,28 @@ pub async fn subscribe_form(db: &State<Database>, peer: Form<PeerForm>) -> Resul
return Err(Flash::error(Redirect::to(uri!(home)), validation_err_msg));
} else {
info!("Public key {} is valid.", &peer.public_key);
let peer_name = match sbot::get_name(&peer.public_key).await {
Ok(name) => name,
Err(e) => {
warn!("Failed to fetch name for peer {}: {}", &peer.public_key, e);
String::from("")
}
};
let peer_info = Peer::new(&peer.public_key).set_name(&peer_name);
match sbot::follow_if_not_following(&peer.public_key).await {
Ok(_) => (),
Ok(_) => {
if db.add_peer(peer_info).is_ok() {
info!("Added {} to 'peers' database tree", &peer.public_key);
} else {
let err_msg = format!("Failed to add peer {} to 'peers' database tree.",
&peer.public_key
);
warn!("{}", err_msg);
return Err(Flash::error(Redirect::to(uri!(home)), err_msg));
}
},
Err(e) => {
warn!("{}", e);
return Err(Flash::error(Redirect::to(uri!(home)), e));
@ -51,7 +71,13 @@ pub async fn unsubscribe_form(db: &State<Database>, peer: Form<PeerForm>) -> Re
} else {
info!("Public key {} is valid.", &peer.public_key);
match sbot::unfollow_if_not_following(&peer.public_key).await {
Ok(_) => (),
Ok(_) => {
if db.remove_peer(&peer.public_key).is_ok() {
info!("Removed peer {} from 'peers' database tree.", &peer.public_key);
} else {
warn!("Failed to remove peer {} from 'peers' database tree", &peer.public_key);
}
},
Err(e) => {
warn!("{}", e);
return Err(Flash::error(Redirect::to(uri!(home)), e));

View File

@ -52,9 +52,11 @@ pub async fn get_name(public_key: &str) -> Result<String, String> {
pub async fn follow_if_not_following(remote_peer: &str) -> Result<(), String> {
info!("Seeing whether we should follow {}", remote_peer);
if let Ok(whoami) = whoami().await {
match is_following(&whoami, remote_peer).await {
Ok(status) if status.as_str() == "false" => {
Ok(status) if status.as_str() != "true" => {
info!("Status: '{}'", status);
match follow_peer(remote_peer).await {
Ok(_) => {
info!("Followed peer {}", &remote_peer);
@ -68,6 +70,7 @@ pub async fn follow_if_not_following(remote_peer: &str) -> Result<(), String> {
}
},
Ok(status) if status.as_str() == "true" => {
info!("Status: '{}'", status);
info!("Already following peer {}. No further action taken", &remote_peer);
Ok(())
},
@ -81,13 +84,16 @@ pub async fn follow_if_not_following(remote_peer: &str) -> Result<(), String> {
}
pub async fn unfollow_if_not_following(remote_peer: &str) -> Result<(), String> {
info!("Seeing whether we should unfollow {}", remote_peer);
if let Ok(whoami) = whoami().await {
match is_following(&whoami, remote_peer).await {
Ok(status) if status.as_str() == "false" => {
Ok(status) if status.as_str() != "true" => {
info!("Status: '{}'", status);
info!("Already not following peer {}. No further action taken", &remote_peer);
Ok(())
},
Ok(status) if status.as_str() == "true" => {
info!("Status: '{}'", status);
match unfollow_peer(remote_peer).await {
Ok(_) => {
info!("Unfollowed peer {}", &remote_peer);