reduce code repetition
This commit is contained in:
parent
69a8cc262e
commit
59ef5960a4
|
@ -23,6 +23,68 @@ use crate::{
|
||||||
utils,
|
utils,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// HELPER FUNCTIONS
|
||||||
|
|
||||||
|
/// Check to see if the go-sbot.service process is currently active.
|
||||||
|
/// Return an error in the form of a `String` if the process
|
||||||
|
/// check command fails. Otherwise, return the state of the process.
|
||||||
|
fn is_sbot_active() -> Result<bool, String> {
|
||||||
|
// retrieve go-sbot systemd process status
|
||||||
|
let sbot_status = match SbotStatus::read() {
|
||||||
|
Ok(status) => status,
|
||||||
|
Err(e) => return Err(format!("Failed to read sbot status: {}", e)),
|
||||||
|
};
|
||||||
|
|
||||||
|
if sbot_status.state == Some("active".to_string()) {
|
||||||
|
Ok(true)
|
||||||
|
} else {
|
||||||
|
Ok(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Ensure that the given public key is a valid ed25519 key.
|
||||||
|
fn validate_public_key(public_key: &str, redirect_url: String) -> Result<(), Flash<Redirect>> {
|
||||||
|
// ensure the id starts with the correct sigil link
|
||||||
|
if !public_key.starts_with('@') {
|
||||||
|
return Err(Flash::error(
|
||||||
|
Redirect::to(redirect_url),
|
||||||
|
"Invalid key: expected '@' sigil as first character",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
// find the dot index denoting the start of the algorithm definition tag
|
||||||
|
let dot_index = match public_key.rfind('.') {
|
||||||
|
Some(index) => index,
|
||||||
|
None => {
|
||||||
|
return Err(Flash::error(
|
||||||
|
Redirect::to(redirect_url),
|
||||||
|
"Invalid key: no dot index was found",
|
||||||
|
))
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// check hashing algorithm (must end with ".ed25519")
|
||||||
|
if !&public_key.ends_with(".ed25519") {
|
||||||
|
return Err(Flash::error(
|
||||||
|
Redirect::to(redirect_url),
|
||||||
|
"Invalid key: hashing algorithm must be ed25519",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
// obtain the base64 portion (substring) of the public key
|
||||||
|
let base64_str = &public_key[1..dot_index];
|
||||||
|
|
||||||
|
// length of a base64 encoded ed25519 public key
|
||||||
|
if base64_str.len() != 44 {
|
||||||
|
return Err(Flash::error(
|
||||||
|
Redirect::to(redirect_url),
|
||||||
|
"Invalid key: base64 data length is incorrect",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
// HELPERS AND ROUTES FOR INVITES
|
// HELPERS AND ROUTES FOR INVITES
|
||||||
|
|
||||||
#[get("/invites")]
|
#[get("/invites")]
|
||||||
|
@ -66,38 +128,39 @@ pub async fn create_invite(invite: Form<Invite>, _auth: Authenticated) -> Flash<
|
||||||
|
|
||||||
let url = uri!("/scuttlebutt", invites);
|
let url = uri!("/scuttlebutt", invites);
|
||||||
|
|
||||||
// retrieve go-sbot systemd process status
|
|
||||||
// TODO: handle unwrap properly
|
|
||||||
let sbot_status = SbotStatus::read().unwrap();
|
|
||||||
|
|
||||||
// we only want to try and interact with the sbot if it's active
|
// we only want to try and interact with the sbot if it's active
|
||||||
if sbot_status.state == Some("active".to_string()) {
|
match is_sbot_active() {
|
||||||
// retrieve latest go-sbot configuration parameters
|
Ok(true) => {
|
||||||
let sbot_config = SbotConfig::read().ok();
|
// retrieve latest go-sbot configuration parameters
|
||||||
|
let sbot_config = SbotConfig::read().ok();
|
||||||
|
|
||||||
// initialise sbot connection with ip:port and shscap from config file
|
// initialise sbot connection with ip:port and shscap from config file
|
||||||
match scuttlebutt::init_sbot_with_config(&sbot_config).await {
|
match scuttlebutt::init_sbot_with_config(&sbot_config).await {
|
||||||
Ok(mut sbot_client) => {
|
Ok(mut sbot_client) => {
|
||||||
debug!("Generating Scuttlebutt invite code");
|
debug!("Generating Scuttlebutt invite code");
|
||||||
match sbot_client.invite_create(uses).await {
|
match sbot_client.invite_create(uses).await {
|
||||||
// construct a custom flash msg to pass along the invite code
|
// construct a custom flash msg to pass along the invite code
|
||||||
Ok(code) => Flash::new(Redirect::to(url), "code", code),
|
Ok(code) => Flash::new(Redirect::to(url), "code", code),
|
||||||
Err(e) => Flash::error(
|
Err(e) => Flash::error(
|
||||||
Redirect::to(url),
|
Redirect::to(url),
|
||||||
format!("Failed to create invite code: {}", e),
|
format!("Failed to create invite code: {}", e),
|
||||||
),
|
),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Err(e) => Flash::error(
|
||||||
|
Redirect::to(url),
|
||||||
|
format!("Failed to initialise sbot: {}", e),
|
||||||
|
),
|
||||||
}
|
}
|
||||||
Err(e) => Flash::error(
|
|
||||||
Redirect::to(url),
|
|
||||||
format!("Failed to initialise sbot: {}", e),
|
|
||||||
),
|
|
||||||
}
|
}
|
||||||
} else {
|
Ok(false) => {
|
||||||
return Flash::warning(
|
return Flash::warning(
|
||||||
Redirect::to(url),
|
Redirect::to(url),
|
||||||
"The Sbot is currently inactive. As a result, new posts cannot be published. Visit the Scuttlebutt settings menu to start the Sbot and then try again",
|
"The Sbot is currently inactive. As a result, new posts cannot be published. Visit the Scuttlebutt settings menu to start the Sbot and then try again",
|
||||||
);
|
)
|
||||||
|
}
|
||||||
|
// failed to retrieve go-sbot systemd process status
|
||||||
|
Err(e) => return Flash::error(Redirect::to(url), e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -154,56 +217,50 @@ pub struct Private {
|
||||||
pub async fn private_post(private: Form<Private>, _auth: Authenticated) -> Flash<Redirect> {
|
pub async fn private_post(private: Form<Private>, _auth: Authenticated) -> Flash<Redirect> {
|
||||||
let url = uri!("/scuttlebutt", private(None::<String>));
|
let url = uri!("/scuttlebutt", private(None::<String>));
|
||||||
|
|
||||||
// retrieve go-sbot systemd process status
|
|
||||||
let sbot_status = match SbotStatus::read() {
|
|
||||||
Ok(status) => status,
|
|
||||||
Err(e) => {
|
|
||||||
return Flash::error(
|
|
||||||
Redirect::to(url),
|
|
||||||
format!("Failed to read sbot status: {}", e),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// we only want to try and interact with the sbot if it's active
|
// we only want to try and interact with the sbot if it's active
|
||||||
if sbot_status.state == Some("active".to_string()) {
|
match is_sbot_active() {
|
||||||
// retrieve latest go-sbot configuration parameters
|
Ok(true) => {
|
||||||
let sbot_config = SbotConfig::read().ok();
|
// retrieve latest go-sbot configuration parameters
|
||||||
|
let sbot_config = SbotConfig::read().ok();
|
||||||
|
|
||||||
let id = &private.id;
|
let id = &private.id;
|
||||||
let text = &private.text;
|
let text = &private.text;
|
||||||
let recipient = &private.recipient;
|
let recipient = &private.recipient;
|
||||||
// now we need to add the local id to the recipients vector,
|
// now we need to add the local id to the recipients vector,
|
||||||
// otherwise the local id will not be able to read the message.
|
// otherwise the local id will not be able to read the message.
|
||||||
let recipients = vec![id.to_string(), recipient.to_string()];
|
let recipients = vec![id.to_string(), recipient.to_string()];
|
||||||
|
|
||||||
// initialise sbot connection with ip:port and shscap from config file
|
// initialise sbot connection with ip:port and shscap from config file
|
||||||
match scuttlebutt::init_sbot_with_config(&sbot_config).await {
|
match scuttlebutt::init_sbot_with_config(&sbot_config).await {
|
||||||
Ok(mut sbot_client) => {
|
Ok(mut sbot_client) => {
|
||||||
debug!("Publishing a new Scuttlebutt private message");
|
debug!("Publishing a new Scuttlebutt private message");
|
||||||
match sbot_client
|
match sbot_client
|
||||||
.publish_private(text.to_string(), recipients)
|
.publish_private(text.to_string(), recipients)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
Flash::success(Redirect::to(url), format!("Published private message"))
|
Flash::success(Redirect::to(url), format!("Published private message"))
|
||||||
|
}
|
||||||
|
Err(e) => Flash::error(
|
||||||
|
Redirect::to(url),
|
||||||
|
format!("Failed to publish private message: {}", e),
|
||||||
|
),
|
||||||
}
|
}
|
||||||
Err(e) => Flash::error(
|
|
||||||
Redirect::to(url),
|
|
||||||
format!("Failed to publish private message: {}", e),
|
|
||||||
),
|
|
||||||
}
|
}
|
||||||
|
Err(e) => Flash::error(
|
||||||
|
Redirect::to(url),
|
||||||
|
format!("Failed to initialise sbot: {}", e),
|
||||||
|
),
|
||||||
}
|
}
|
||||||
Err(e) => Flash::error(
|
|
||||||
Redirect::to(url),
|
|
||||||
format!("Failed to initialise sbot: {}", e),
|
|
||||||
),
|
|
||||||
}
|
}
|
||||||
} else {
|
Ok(false) => {
|
||||||
return Flash::warning(
|
return Flash::warning(
|
||||||
Redirect::to(url),
|
Redirect::to(url),
|
||||||
"The Sbot is currently inactive. As a result, new private message cannot be published. Visit the Scuttlebutt settings menu to start the Sbot and then try again",
|
"The Sbot is currently inactive. As a result, new private message cannot be published. Visit the Scuttlebutt settings menu to start the Sbot and then try again",
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
// failed to retrieve go-sbot systemd process status
|
||||||
|
Err(e) => return Flash::error(Redirect::to(url), e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -240,48 +297,6 @@ pub struct Peer {
|
||||||
pub public_key: String,
|
pub public_key: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn validate_public_key(public_key: &str, redirect_url: String) -> Result<(), Flash<Redirect>> {
|
|
||||||
// ensure the id starts with the correct sigil link
|
|
||||||
if !public_key.starts_with('@') {
|
|
||||||
return Err(Flash::error(
|
|
||||||
Redirect::to(redirect_url),
|
|
||||||
"Invalid key: expected '@' sigil as first character",
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
// find the dot index denoting the start of the algorithm definition tag
|
|
||||||
let dot_index = match public_key.rfind('.') {
|
|
||||||
Some(index) => index,
|
|
||||||
None => {
|
|
||||||
return Err(Flash::error(
|
|
||||||
Redirect::to(redirect_url),
|
|
||||||
"Invalid key: no dot index was found",
|
|
||||||
))
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// check hashing algorithm (must end with ".ed25519")
|
|
||||||
if !&public_key.ends_with(".ed25519") {
|
|
||||||
return Err(Flash::error(
|
|
||||||
Redirect::to(redirect_url),
|
|
||||||
"Invalid key: hashing algorithm must be ed25519",
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
// obtain the base64 portion (substring) of the public key
|
|
||||||
let base64_str = &public_key[1..dot_index];
|
|
||||||
|
|
||||||
// length of a base64 encoded ed25519 public key
|
|
||||||
if base64_str.len() != 44 {
|
|
||||||
return Err(Flash::error(
|
|
||||||
Redirect::to(redirect_url),
|
|
||||||
"Invalid key: base64 data length is incorrect",
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Accept the peer search form and redirect to the profile for that peer.
|
/// Accept the peer search form and redirect to the profile for that peer.
|
||||||
#[post("/search", data = "<peer>")]
|
#[post("/search", data = "<peer>")]
|
||||||
pub async fn search_post(peer: Form<Peer>, _auth: Authenticated) -> Flash<Redirect> {
|
pub async fn search_post(peer: Form<Peer>, _auth: Authenticated) -> Flash<Redirect> {
|
||||||
|
@ -291,6 +306,7 @@ pub async fn search_post(peer: Form<Peer>, _auth: Authenticated) -> Flash<Redire
|
||||||
|
|
||||||
// validate the key before redirecting to profile url
|
// validate the key before redirecting to profile url
|
||||||
if let Err(flash) = validate_public_key(&public_key, search_url) {
|
if let Err(flash) = validate_public_key(&public_key, search_url) {
|
||||||
|
// redirect with error message
|
||||||
return flash;
|
return flash;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -343,46 +359,39 @@ pub struct Post {
|
||||||
#[post("/publish", data = "<post>")]
|
#[post("/publish", data = "<post>")]
|
||||||
pub async fn publish(post: Form<Post>, _auth: Authenticated) -> Flash<Redirect> {
|
pub async fn publish(post: Form<Post>, _auth: Authenticated) -> Flash<Redirect> {
|
||||||
let post_text = &post.text;
|
let post_text = &post.text;
|
||||||
|
|
||||||
let url = uri!("/scuttlebutt", profile(None::<String>));
|
let url = uri!("/scuttlebutt", profile(None::<String>));
|
||||||
|
|
||||||
// retrieve go-sbot systemd process status
|
|
||||||
let sbot_status = match SbotStatus::read() {
|
|
||||||
Ok(status) => status,
|
|
||||||
Err(e) => {
|
|
||||||
return Flash::error(
|
|
||||||
Redirect::to(url),
|
|
||||||
format!("Failed to read sbot status: {}", e),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// we only want to try and interact with the sbot if it's active
|
// we only want to try and interact with the sbot if it's active
|
||||||
if sbot_status.state == Some("active".to_string()) {
|
match is_sbot_active() {
|
||||||
// retrieve latest go-sbot configuration parameters
|
Ok(true) => {
|
||||||
let sbot_config = SbotConfig::read().ok();
|
// retrieve latest go-sbot configuration parameters
|
||||||
|
let sbot_config = SbotConfig::read().ok();
|
||||||
|
|
||||||
// initialise sbot connection with ip:port and shscap from config file
|
// initialise sbot connection with ip:port and shscap from config file
|
||||||
match scuttlebutt::init_sbot_with_config(&sbot_config).await {
|
match scuttlebutt::init_sbot_with_config(&sbot_config).await {
|
||||||
Ok(mut sbot_client) => {
|
Ok(mut sbot_client) => {
|
||||||
debug!("Publishing new Scuttlebutt public post");
|
debug!("Publishing new Scuttlebutt public post");
|
||||||
match sbot_client.publish_post(post_text).await {
|
match sbot_client.publish_post(post_text).await {
|
||||||
Ok(_) => Flash::success(Redirect::to(url), format!("Published post")),
|
Ok(_) => Flash::success(Redirect::to(url), format!("Published post")),
|
||||||
Err(e) => {
|
Err(e) => Flash::error(
|
||||||
Flash::error(Redirect::to(url), format!("Failed to publish post: {}", e))
|
Redirect::to(url),
|
||||||
|
format!("Failed to publish post: {}", e),
|
||||||
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Err(e) => Flash::error(
|
||||||
|
Redirect::to(url),
|
||||||
|
format!("Failed to initialise sbot: {}", e),
|
||||||
|
),
|
||||||
}
|
}
|
||||||
Err(e) => Flash::error(
|
|
||||||
Redirect::to(url),
|
|
||||||
format!("Failed to initialise sbot: {}", e),
|
|
||||||
),
|
|
||||||
}
|
}
|
||||||
} else {
|
Ok(false) => {
|
||||||
return Flash::warning(
|
return Flash::warning(
|
||||||
Redirect::to(url),
|
Redirect::to(url),
|
||||||
"The Sbot is currently inactive. As a result, new posts cannot be published. Visit the Scuttlebutt settings menu to start the Sbot and then try again",
|
"The Sbot is currently inactive. As a result, new posts cannot be published. Visit the Scuttlebutt settings menu to start the Sbot and then try again",
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
Err(e) => return Flash::error(Redirect::to(url), e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -394,46 +403,38 @@ pub async fn publish(post: Form<Post>, _auth: Authenticated) -> Flash<Redirect>
|
||||||
#[post("/follow", data = "<peer>")]
|
#[post("/follow", data = "<peer>")]
|
||||||
pub async fn follow(peer: Form<Peer>, _auth: Authenticated) -> Flash<Redirect> {
|
pub async fn follow(peer: Form<Peer>, _auth: Authenticated) -> Flash<Redirect> {
|
||||||
let public_key = &peer.public_key;
|
let public_key = &peer.public_key;
|
||||||
|
|
||||||
let url = uri!("/scuttlebutt", profile(Some(public_key)));
|
let url = uri!("/scuttlebutt", profile(Some(public_key)));
|
||||||
|
|
||||||
// retrieve go-sbot systemd process status
|
|
||||||
let sbot_status = match SbotStatus::read() {
|
|
||||||
Ok(status) => status,
|
|
||||||
Err(e) => {
|
|
||||||
return Flash::error(
|
|
||||||
Redirect::to(url),
|
|
||||||
format!("Failed to read sbot status: {}", e),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// we only want to try and interact with the sbot if it's active
|
// we only want to try and interact with the sbot if it's active
|
||||||
if sbot_status.state == Some("active".to_string()) {
|
match is_sbot_active() {
|
||||||
// retrieve latest go-sbot configuration parameters
|
Ok(true) => {
|
||||||
let sbot_config = SbotConfig::read().ok();
|
// retrieve latest go-sbot configuration parameters
|
||||||
|
let sbot_config = SbotConfig::read().ok();
|
||||||
|
|
||||||
// initialise sbot connection with ip:port and shscap from config file
|
// initialise sbot connection with ip:port and shscap from config file
|
||||||
match scuttlebutt::init_sbot_with_config(&sbot_config).await {
|
match scuttlebutt::init_sbot_with_config(&sbot_config).await {
|
||||||
Ok(mut sbot_client) => {
|
Ok(mut sbot_client) => {
|
||||||
debug!("Following Scuttlebutt peer");
|
debug!("Following Scuttlebutt peer");
|
||||||
match sbot_client.follow(public_key).await {
|
match sbot_client.follow(public_key).await {
|
||||||
Ok(_) => Flash::success(Redirect::to(url), format!("Followed peer")),
|
Ok(_) => Flash::success(Redirect::to(url), format!("Followed peer")),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
Flash::error(Redirect::to(url), format!("Failed to follow peer: {}", e))
|
Flash::error(Redirect::to(url), format!("Failed to follow peer: {}", e))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Err(e) => Flash::error(
|
||||||
|
Redirect::to(url),
|
||||||
|
format!("Failed to initialise sbot: {}", e),
|
||||||
|
),
|
||||||
}
|
}
|
||||||
Err(e) => Flash::error(
|
|
||||||
Redirect::to(url),
|
|
||||||
format!("Failed to initialise sbot: {}", e),
|
|
||||||
),
|
|
||||||
}
|
}
|
||||||
} else {
|
Ok(false) => {
|
||||||
return Flash::warning(
|
return Flash::warning(
|
||||||
Redirect::to(url),
|
Redirect::to(url),
|
||||||
"The Sbot is currently inactive. As a result, follow messages cannot be published. Visit the Scuttlebutt settings menu to start the Sbot and then try again",
|
"The Sbot is currently inactive. As a result, follow messages cannot be published. Visit the Scuttlebutt settings menu to start the Sbot and then try again",
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
Err(e) => return Flash::error(Redirect::to(url), e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -448,43 +449,37 @@ pub async fn unfollow(peer: Form<Peer>, _auth: Authenticated) -> Flash<Redirect>
|
||||||
|
|
||||||
let url = uri!("/scuttlebutt", profile(Some(public_key)));
|
let url = uri!("/scuttlebutt", profile(Some(public_key)));
|
||||||
|
|
||||||
// retrieve go-sbot systemd process status
|
|
||||||
let sbot_status = match SbotStatus::read() {
|
|
||||||
Ok(status) => status,
|
|
||||||
Err(e) => {
|
|
||||||
return Flash::error(
|
|
||||||
Redirect::to(url),
|
|
||||||
format!("Failed to read sbot status: {}", e),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// we only want to try and interact with the sbot if it's active
|
// we only want to try and interact with the sbot if it's active
|
||||||
if sbot_status.state == Some("active".to_string()) {
|
match is_sbot_active() {
|
||||||
// retrieve latest go-sbot configuration parameters
|
Ok(true) => {
|
||||||
let sbot_config = SbotConfig::read().ok();
|
// retrieve latest go-sbot configuration parameters
|
||||||
|
let sbot_config = SbotConfig::read().ok();
|
||||||
|
|
||||||
// initialise sbot connection with ip:port and shscap from config file
|
// initialise sbot connection with ip:port and shscap from config file
|
||||||
match scuttlebutt::init_sbot_with_config(&sbot_config).await {
|
match scuttlebutt::init_sbot_with_config(&sbot_config).await {
|
||||||
Ok(mut sbot_client) => {
|
Ok(mut sbot_client) => {
|
||||||
debug!("Unfollowing Scuttlebutt peer");
|
debug!("Unfollowing Scuttlebutt peer");
|
||||||
match sbot_client.unfollow(public_key).await {
|
match sbot_client.unfollow(public_key).await {
|
||||||
Ok(_) => Flash::success(Redirect::to(url), format!("Unfollowed peer")),
|
Ok(_) => Flash::success(Redirect::to(url), format!("Unfollowed peer")),
|
||||||
Err(e) => {
|
Err(e) => Flash::error(
|
||||||
Flash::error(Redirect::to(url), format!("Failed to unfollow peer: {}", e))
|
Redirect::to(url),
|
||||||
|
format!("Failed to unfollow peer: {}", e),
|
||||||
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Err(e) => Flash::error(
|
||||||
|
Redirect::to(url),
|
||||||
|
format!("Failed to initialise sbot: {}", e),
|
||||||
|
),
|
||||||
}
|
}
|
||||||
Err(e) => Flash::error(
|
|
||||||
Redirect::to(url),
|
|
||||||
format!("Failed to initialise sbot: {}", e),
|
|
||||||
),
|
|
||||||
}
|
}
|
||||||
} else {
|
Ok(false) => {
|
||||||
return Flash::warning(
|
return Flash::warning(
|
||||||
Redirect::to(url),
|
Redirect::to(url),
|
||||||
"The Sbot is currently inactive. As a result, follow messages cannot be published. Visit the Scuttlebutt settings menu to start the Sbot and then try again",
|
"The Sbot is currently inactive. As a result, follow messages cannot be published. Visit the Scuttlebutt settings menu to start the Sbot and then try again",
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
Err(e) => return Flash::error(Redirect::to(url), e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -496,46 +491,38 @@ pub async fn unfollow(peer: Form<Peer>, _auth: Authenticated) -> Flash<Redirect>
|
||||||
#[post("/block", data = "<peer>")]
|
#[post("/block", data = "<peer>")]
|
||||||
pub async fn block(peer: Form<Peer>, _auth: Authenticated) -> Flash<Redirect> {
|
pub async fn block(peer: Form<Peer>, _auth: Authenticated) -> Flash<Redirect> {
|
||||||
let public_key = &peer.public_key;
|
let public_key = &peer.public_key;
|
||||||
|
|
||||||
let url = uri!("/scuttlebutt", profile(Some(public_key)));
|
let url = uri!("/scuttlebutt", profile(Some(public_key)));
|
||||||
|
|
||||||
// retrieve go-sbot systemd process status
|
|
||||||
let sbot_status = match SbotStatus::read() {
|
|
||||||
Ok(status) => status,
|
|
||||||
Err(e) => {
|
|
||||||
return Flash::error(
|
|
||||||
Redirect::to(url),
|
|
||||||
format!("Failed to read sbot status: {}", e),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// we only want to try and interact with the sbot if it's active
|
// we only want to try and interact with the sbot if it's active
|
||||||
if sbot_status.state == Some("active".to_string()) {
|
match is_sbot_active() {
|
||||||
// retrieve latest go-sbot configuration parameters
|
Ok(true) => {
|
||||||
let sbot_config = SbotConfig::read().ok();
|
// retrieve latest go-sbot configuration parameters
|
||||||
|
let sbot_config = SbotConfig::read().ok();
|
||||||
|
|
||||||
// initialise sbot connection with ip:port and shscap from config file
|
// initialise sbot connection with ip:port and shscap from config file
|
||||||
match scuttlebutt::init_sbot_with_config(&sbot_config).await {
|
match scuttlebutt::init_sbot_with_config(&sbot_config).await {
|
||||||
Ok(mut sbot_client) => {
|
Ok(mut sbot_client) => {
|
||||||
debug!("Blocking Scuttlebutt peer");
|
debug!("Blocking Scuttlebutt peer");
|
||||||
match sbot_client.block(public_key).await {
|
match sbot_client.block(public_key).await {
|
||||||
Ok(_) => Flash::success(Redirect::to(url), format!("Blocked peer")),
|
Ok(_) => Flash::success(Redirect::to(url), format!("Blocked peer")),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
Flash::error(Redirect::to(url), format!("Failed to block peer: {}", e))
|
Flash::error(Redirect::to(url), format!("Failed to block peer: {}", e))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Err(e) => Flash::error(
|
||||||
|
Redirect::to(url),
|
||||||
|
format!("Failed to initialise sbot: {}", e),
|
||||||
|
),
|
||||||
}
|
}
|
||||||
Err(e) => Flash::error(
|
|
||||||
Redirect::to(url),
|
|
||||||
format!("Failed to initialise sbot: {}", e),
|
|
||||||
),
|
|
||||||
}
|
}
|
||||||
} else {
|
Ok(false) => {
|
||||||
return Flash::warning(
|
return Flash::warning(
|
||||||
Redirect::to(url),
|
Redirect::to(url),
|
||||||
"The Sbot is currently inactive. As a result, follow messages cannot be published. Visit the Scuttlebutt settings menu to start the Sbot and then try again",
|
"The Sbot is currently inactive. As a result, follow messages cannot be published. Visit the Scuttlebutt settings menu to start the Sbot and then try again",
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
Err(e) => return Flash::error(Redirect::to(url), e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -547,46 +534,39 @@ pub async fn block(peer: Form<Peer>, _auth: Authenticated) -> Flash<Redirect> {
|
||||||
#[post("/unblock", data = "<peer>")]
|
#[post("/unblock", data = "<peer>")]
|
||||||
pub async fn unblock(peer: Form<Peer>, _auth: Authenticated) -> Flash<Redirect> {
|
pub async fn unblock(peer: Form<Peer>, _auth: Authenticated) -> Flash<Redirect> {
|
||||||
let public_key = &peer.public_key;
|
let public_key = &peer.public_key;
|
||||||
|
|
||||||
let url = uri!("/scuttlebutt", profile(Some(public_key)));
|
let url = uri!("/scuttlebutt", profile(Some(public_key)));
|
||||||
|
|
||||||
// retrieve go-sbot systemd process status
|
|
||||||
let sbot_status = match SbotStatus::read() {
|
|
||||||
Ok(status) => status,
|
|
||||||
Err(e) => {
|
|
||||||
return Flash::error(
|
|
||||||
Redirect::to(url),
|
|
||||||
format!("Failed to read sbot status: {}", e),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// we only want to try and interact with the sbot if it's active
|
// we only want to try and interact with the sbot if it's active
|
||||||
if sbot_status.state == Some("active".to_string()) {
|
match is_sbot_active() {
|
||||||
// retrieve latest go-sbot configuration parameters
|
Ok(true) => {
|
||||||
let sbot_config = SbotConfig::read().ok();
|
// retrieve latest go-sbot configuration parameters
|
||||||
|
let sbot_config = SbotConfig::read().ok();
|
||||||
|
|
||||||
// initialise sbot connection with ip:port and shscap from config file
|
// initialise sbot connection with ip:port and shscap from config file
|
||||||
match scuttlebutt::init_sbot_with_config(&sbot_config).await {
|
match scuttlebutt::init_sbot_with_config(&sbot_config).await {
|
||||||
Ok(mut sbot_client) => {
|
Ok(mut sbot_client) => {
|
||||||
debug!("Unblocking Scuttlebutt peer");
|
debug!("Unblocking Scuttlebutt peer");
|
||||||
match sbot_client.unblock(public_key).await {
|
match sbot_client.unblock(public_key).await {
|
||||||
Ok(_) => Flash::success(Redirect::to(url), format!("Unblocked peer")),
|
Ok(_) => Flash::success(Redirect::to(url), format!("Unblocked peer")),
|
||||||
Err(e) => {
|
Err(e) => Flash::error(
|
||||||
Flash::error(Redirect::to(url), format!("Failed to unblock peer: {}", e))
|
Redirect::to(url),
|
||||||
|
format!("Failed to unblock peer: {}", e),
|
||||||
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Err(e) => Flash::error(
|
||||||
|
Redirect::to(url),
|
||||||
|
format!("Failed to initialise sbot: {}", e),
|
||||||
|
),
|
||||||
}
|
}
|
||||||
Err(e) => Flash::error(
|
|
||||||
Redirect::to(url),
|
|
||||||
format!("Failed to initialise sbot: {}", e),
|
|
||||||
),
|
|
||||||
}
|
}
|
||||||
} else {
|
Ok(false) => {
|
||||||
return Flash::warning(
|
return Flash::warning(
|
||||||
Redirect::to(url),
|
Redirect::to(url),
|
||||||
"The Sbot is currently inactive. As a result, follow messages cannot be published. Visit the Scuttlebutt settings menu to start the Sbot and then try again",
|
"The Sbot is currently inactive. As a result, follow messages cannot be published. Visit the Scuttlebutt settings menu to start the Sbot and then try again",
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
Err(e) => return Flash::error(Redirect::to(url), e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -697,105 +677,105 @@ pub async fn update_profile_post(
|
||||||
) -> Flash<Redirect> {
|
) -> Flash<Redirect> {
|
||||||
let url = uri!("/scuttlebutt", update_profile);
|
let url = uri!("/scuttlebutt", update_profile);
|
||||||
|
|
||||||
// retrieve go-sbot systemd process status
|
|
||||||
// TODO: handle unwrap properly
|
|
||||||
let sbot_status = SbotStatus::read().unwrap();
|
|
||||||
|
|
||||||
// we only want to try and interact with the sbot if it's active
|
// we only want to try and interact with the sbot if it's active
|
||||||
if sbot_status.state == Some("active".to_string()) {
|
match is_sbot_active() {
|
||||||
// retrieve latest go-sbot configuration parameters
|
Ok(true) => {
|
||||||
let sbot_config = SbotConfig::read().ok();
|
// retrieve latest go-sbot configuration parameters
|
||||||
|
let sbot_config = SbotConfig::read().ok();
|
||||||
|
|
||||||
// initialise sbot connection with ip:port and shscap from config file
|
// initialise sbot connection with ip:port and shscap from config file
|
||||||
match scuttlebutt::init_sbot_with_config(&sbot_config).await {
|
match scuttlebutt::init_sbot_with_config(&sbot_config).await {
|
||||||
Ok(mut sbot_client) => {
|
Ok(mut sbot_client) => {
|
||||||
// track whether the name, description or image have been updated
|
// track whether the name, description or image have been updated
|
||||||
let mut name_updated: bool = false;
|
let mut name_updated: bool = false;
|
||||||
let mut description_updated: bool = false;
|
let mut description_updated: bool = false;
|
||||||
let image_updated: bool;
|
let image_updated: bool;
|
||||||
|
|
||||||
// only update the name if it has changed
|
// only update the name if it has changed
|
||||||
if profile.new_name != profile.current_name {
|
if profile.new_name != profile.current_name {
|
||||||
debug!("Publishing new Scuttlebutt profile name");
|
debug!("Publishing new Scuttlebutt profile name");
|
||||||
let publish_name_res = sbot_client.publish_name(&profile.new_name).await;
|
let publish_name_res = sbot_client.publish_name(&profile.new_name).await;
|
||||||
if publish_name_res.is_err() {
|
if publish_name_res.is_err() {
|
||||||
return Flash::error(
|
|
||||||
Redirect::to(url),
|
|
||||||
format!("Failed to update name: {}", publish_name_res.unwrap_err()),
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
name_updated = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// only update the description if it has changed
|
|
||||||
if profile.new_description != profile.current_description {
|
|
||||||
debug!("Publishing new Scuttlebutt profile description");
|
|
||||||
let publish_description_res = sbot_client
|
|
||||||
.publish_description(&profile.new_description)
|
|
||||||
.await;
|
|
||||||
|
|
||||||
if publish_description_res.is_err() {
|
|
||||||
return Flash::error(
|
|
||||||
Redirect::to(url),
|
|
||||||
format!(
|
|
||||||
"Failed to update description: {}",
|
|
||||||
publish_description_res.unwrap_err()
|
|
||||||
),
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
description_updated = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// only update the image if a file was uploaded
|
|
||||||
if profile.image.name().is_some() {
|
|
||||||
match utils::write_blob_to_store(&mut profile.image).await {
|
|
||||||
Ok(blob_id) => {
|
|
||||||
// if the file was successfully added to the blobstore,
|
|
||||||
// publish an about image message with the blob id
|
|
||||||
let publish_image_res = sbot_client.publish_image(&blob_id).await;
|
|
||||||
|
|
||||||
if publish_image_res.is_err() {
|
|
||||||
return Flash::error(
|
|
||||||
Redirect::to(url),
|
|
||||||
format!(
|
|
||||||
"Failed to update image: {}",
|
|
||||||
publish_image_res.unwrap_err()
|
|
||||||
),
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
image_updated = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
return Flash::error(
|
return Flash::error(
|
||||||
Redirect::to(url),
|
Redirect::to(url),
|
||||||
format!("Failed to add image to blobstore: {}", e),
|
format!("Failed to update name: {}", publish_name_res.unwrap_err()),
|
||||||
)
|
);
|
||||||
|
} else {
|
||||||
|
name_updated = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
image_updated = false
|
|
||||||
}
|
|
||||||
|
|
||||||
if name_updated || description_updated || image_updated {
|
// only update the description if it has changed
|
||||||
return Flash::success(Redirect::to(url), "Profile updated");
|
if profile.new_description != profile.current_description {
|
||||||
} else {
|
debug!("Publishing new Scuttlebutt profile description");
|
||||||
// no updates were made but no errors were encountered either
|
let publish_description_res = sbot_client
|
||||||
return Flash::success(Redirect::to(url), "Profile info unchanged");
|
.publish_description(&profile.new_description)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
if publish_description_res.is_err() {
|
||||||
|
return Flash::error(
|
||||||
|
Redirect::to(url),
|
||||||
|
format!(
|
||||||
|
"Failed to update description: {}",
|
||||||
|
publish_description_res.unwrap_err()
|
||||||
|
),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
description_updated = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// only update the image if a file was uploaded
|
||||||
|
if profile.image.name().is_some() {
|
||||||
|
match utils::write_blob_to_store(&mut profile.image).await {
|
||||||
|
Ok(blob_id) => {
|
||||||
|
// if the file was successfully added to the blobstore,
|
||||||
|
// publish an about image message with the blob id
|
||||||
|
let publish_image_res = sbot_client.publish_image(&blob_id).await;
|
||||||
|
|
||||||
|
if publish_image_res.is_err() {
|
||||||
|
return Flash::error(
|
||||||
|
Redirect::to(url),
|
||||||
|
format!(
|
||||||
|
"Failed to update image: {}",
|
||||||
|
publish_image_res.unwrap_err()
|
||||||
|
),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
image_updated = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
return Flash::error(
|
||||||
|
Redirect::to(url),
|
||||||
|
format!("Failed to add image to blobstore: {}", e),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
image_updated = false
|
||||||
|
}
|
||||||
|
|
||||||
|
if name_updated || description_updated || image_updated {
|
||||||
|
return Flash::success(Redirect::to(url), "Profile updated");
|
||||||
|
} else {
|
||||||
|
// no updates were made but no errors were encountered either
|
||||||
|
return Flash::success(Redirect::to(url), "Profile info unchanged");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Err(e) => Flash::error(
|
||||||
|
Redirect::to(url),
|
||||||
|
format!("Failed to initialise sbot: {}", e),
|
||||||
|
),
|
||||||
}
|
}
|
||||||
Err(e) => Flash::error(
|
|
||||||
Redirect::to(url),
|
|
||||||
format!("Failed to initialise sbot: {}", e),
|
|
||||||
),
|
|
||||||
}
|
}
|
||||||
} else {
|
Ok(false) => {
|
||||||
return Flash::warning(
|
return Flash::warning(
|
||||||
Redirect::to(url),
|
Redirect::to(url),
|
||||||
"The Sbot is currently inactive. As a result, profile data cannot be updated. Visit the Scuttlebutt settings menu to start the Sbot and then try again",
|
"The Sbot is currently inactive. As a result, profile data cannot be updated. Visit the Scuttlebutt settings menu to start the Sbot and then try again",
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
Err(e) => return Flash::error(Redirect::to(url), e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue