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:
parent
99ba9adb80
commit
85824b87e4
|
@ -5,7 +5,7 @@ use rocket_dyn_templates::{context, Template};
|
||||||
|
|
||||||
use crate::sbot;
|
use crate::sbot;
|
||||||
use crate::utils;
|
use crate::utils;
|
||||||
use crate::db::Database;
|
use crate::db::{Database, Peer};
|
||||||
|
|
||||||
#[derive(FromForm)]
|
#[derive(FromForm)]
|
||||||
pub struct PeerForm {
|
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));
|
return Err(Flash::error(Redirect::to(uri!(home)), validation_err_msg));
|
||||||
} else {
|
} else {
|
||||||
info!("Public key {} is valid.", &peer.public_key);
|
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 {
|
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) => {
|
Err(e) => {
|
||||||
warn!("{}", e);
|
warn!("{}", e);
|
||||||
return Err(Flash::error(Redirect::to(uri!(home)), 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 {
|
} else {
|
||||||
info!("Public key {} is valid.", &peer.public_key);
|
info!("Public key {} is valid.", &peer.public_key);
|
||||||
match sbot::unfollow_if_not_following(&peer.public_key).await {
|
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) => {
|
Err(e) => {
|
||||||
warn!("{}", e);
|
warn!("{}", e);
|
||||||
return Err(Flash::error(Redirect::to(uri!(home)), e));
|
return Err(Flash::error(Redirect::to(uri!(home)), e));
|
||||||
|
|
10
src/sbot.rs
10
src/sbot.rs
|
@ -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> {
|
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 {
|
if let Ok(whoami) = whoami().await {
|
||||||
match is_following(&whoami, remote_peer).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 {
|
match follow_peer(remote_peer).await {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
info!("Followed peer {}", &remote_peer);
|
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" => {
|
Ok(status) if status.as_str() == "true" => {
|
||||||
|
info!("Status: '{}'", status);
|
||||||
info!("Already following peer {}. No further action taken", &remote_peer);
|
info!("Already following peer {}. No further action taken", &remote_peer);
|
||||||
Ok(())
|
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> {
|
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 {
|
if let Ok(whoami) = whoami().await {
|
||||||
match is_following(&whoami, remote_peer).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);
|
info!("Already not following peer {}. No further action taken", &remote_peer);
|
||||||
Ok(())
|
Ok(())
|
||||||
},
|
},
|
||||||
Ok(status) if status.as_str() == "true" => {
|
Ok(status) if status.as_str() == "true" => {
|
||||||
|
info!("Status: '{}'", status);
|
||||||
match unfollow_peer(remote_peer).await {
|
match unfollow_peer(remote_peer).await {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
info!("Unfollowed peer {}", &remote_peer);
|
info!("Unfollowed peer {}", &remote_peer);
|
||||||
|
|
Loading…
Reference in New Issue