add themes, add public and private msg publishing

This commit is contained in:
glyph 2022-03-23 11:41:21 +02:00
parent 77c1ccb1c7
commit 41bd39d422
31 changed files with 410 additions and 170 deletions

View File

@ -6,7 +6,10 @@ use rouille::{post_input, try_or_400, Request, Response};
use crate::{
error::PeachWebError,
templates,
utils::flash::{FlashRequest, FlashResponse},
utils::{
flash::{FlashRequest, FlashResponse},
theme,
},
};
// HELPER AND ROUTES FOR /auth/change (GET and POST)
@ -48,8 +51,11 @@ pub fn build_template(request: &Request) -> PreEscaped<String> {
let body =
templates::nav::build_template(form_template, "Change Password", Some("/settings/admin"));
// query the current theme so we can pass it into the base template builder
let theme = theme::get_theme();
// render the base template with the provided body
templates::base::build_template(body)
templates::base::build_template(body, theme)
}
/// Verify, validate and set a new password, overwriting the current password.

View File

@ -5,7 +5,10 @@ use rouille::{post_input, try_or_400, Request, Response};
use crate::{
templates,
utils::flash::{FlashRequest, FlashResponse},
utils::{
flash::{FlashRequest, FlashResponse},
theme,
},
};
// HELPER AND ROUTES FOR /auth/login (GET and POST)
@ -42,8 +45,11 @@ pub fn build_template(request: &Request) -> PreEscaped<String> {
// parameters are template, title and back url
let body = templates::nav::build_template(form_template, "Login", Some("/"));
// query the current theme so we can pass it into the base template builder
let theme = theme::get_theme();
// render the base template with the provided body
templates::base::build_template(body)
templates::base::build_template(body, theme)
}
/// Parse and verify the submitted password. If verification succeeds, set the

View File

@ -6,7 +6,10 @@ use rouille::{post_input, try_or_400, Request, Response};
use crate::{
error::PeachWebError,
templates,
utils::flash::{FlashRequest, FlashResponse},
utils::{
flash::{FlashRequest, FlashResponse},
theme,
},
};
// HELPER AND ROUTES FOR /auth/reset (GET and POST)
@ -48,8 +51,11 @@ pub fn build_template(request: &Request) -> PreEscaped<String> {
let body =
templates::nav::build_template(form_template, "Reset Password", Some("/settings/admin"));
// query the current theme so we can pass it into the base template builder
let theme = theme::get_theme();
// render the base template with the provided body
templates::base::build_template(body)
templates::base::build_template(body, theme)
}
/// Verify, validate and set a new password, overwriting the current password.

View File

@ -1,6 +1,6 @@
use maud::{html, PreEscaped};
use crate::templates;
use crate::{templates, utils::theme};
/// Guide template builder.
pub fn build_template() -> PreEscaped<String> {
@ -98,6 +98,9 @@ pub fn build_template() -> PreEscaped<String> {
// title is "" and back button link is `None` because this is the homepage
let body = templates::nav::build_template(guide_template, "Guide", Some("/"));
// query the current theme so we can pass it into the base template builder
let theme = theme::get_theme();
// render the base template with the provided body
templates::base::build_template(body)
templates::base::build_template(body, theme)
}

View File

@ -1,7 +1,7 @@
use maud::{html, PreEscaped};
use peach_lib::sbot::SbotStatus;
use crate::templates;
use crate::{templates, utils::theme};
/// Read the state of the go-sbot process and define status-related
/// elements accordingly.
@ -97,6 +97,9 @@ pub fn build_template() -> PreEscaped<String> {
// title is "" and back button link is `None` because this is the homepage
let body = templates::nav::build_template(home_template, "", None);
// query the current theme so we can pass it into the base template builder
let theme = theme::get_theme();
// render the base template with the provided body
templates::base::build_template(body)
templates::base::build_template(body, theme)
}

View File

@ -1,6 +1,9 @@
use maud::PreEscaped;
use crate::{templates, utils::sbot};
use crate::{
templates,
utils::{sbot, theme},
};
// ROUTE: /scuttlebutt/blocks
@ -16,8 +19,11 @@ pub fn build_template() -> PreEscaped<String> {
// wrap the nav bars around the error template content
let body = templates::nav::build_template(error_template, "Blocks", Some("/"));
// query the current theme so we can pass it into the base template builder
let theme = theme::get_theme();
// render the base template with the provided body
templates::base::build_template(body)
templates::base::build_template(body, theme)
}
}
}

View File

@ -1,6 +1,9 @@
use maud::PreEscaped;
use crate::{templates, utils::sbot};
use crate::{
templates,
utils::{sbot, theme},
};
// ROUTE: /scuttlebutt/follows
@ -16,8 +19,11 @@ pub fn build_template() -> PreEscaped<String> {
// wrap the nav bars around the error template content
let body = templates::nav::build_template(error_template, "Follows", Some("/"));
// query the current theme so we can pass it into the base template builder
let theme = theme::get_theme();
// render the base template with the provided body
templates::base::build_template(body)
templates::base::build_template(body, theme)
}
}
}

View File

@ -1,6 +1,9 @@
use maud::PreEscaped;
use crate::{templates, utils::sbot};
use crate::{
templates,
utils::{sbot, theme},
};
// ROUTE: /scuttlebutt/friends
@ -16,8 +19,11 @@ pub fn build_template() -> PreEscaped<String> {
// wrap the nav bars around the error template content
let body = templates::nav::build_template(error_template, "Friends", Some("/"));
// query the current theme so we can pass it into the base template builder
let theme = theme::get_theme();
// render the base template with the provided body
templates::base::build_template(body)
templates::base::build_template(body, theme)
}
}
}

View File

@ -6,7 +6,7 @@ use crate::{
templates,
utils::{
flash::{FlashRequest, FlashResponse},
sbot,
sbot, theme,
},
};
@ -73,7 +73,10 @@ pub fn build_template(request: &Request) -> PreEscaped<String> {
let body =
templates::nav::build_template(invite_form_template, "Invites", Some("/scuttlebutt/peers"));
templates::base::build_template(body)
// query the current theme so we can pass it into the base template builder
let theme = theme::get_theme();
templates::base::build_template(body, theme)
}
/// Parse the invite uses data and attempt to generate an invite code.

View File

@ -3,6 +3,8 @@ pub mod follows;
pub mod friends;
pub mod invites;
pub mod peers;
pub mod private;
pub mod profile;
pub mod profile_update;
pub mod publish;
pub mod search;

View File

@ -1,7 +1,7 @@
use maud::{html, PreEscaped};
use peach_lib::sbot::SbotStatus;
use crate::templates;
use crate::{templates, utils::theme};
/// Scuttlebutt peer menu template builder.
///
@ -37,6 +37,9 @@ pub fn build_template() -> PreEscaped<String> {
// parameters are template, title and back url
let body = templates::nav::build_template(menu_template, "Scuttlebutt Peers", Some("/"));
// query the current theme so we can pass it into the base template builder
let theme = theme::get_theme();
// render the base template with the provided body
templates::base::build_template(body)
templates::base::build_template(body, theme)
}

View File

@ -0,0 +1,131 @@
use maud::{html, Markup, PreEscaped};
use peach_lib::sbot::SbotStatus;
use rouille::{post_input, try_or_400, Request, Response};
use crate::{
templates,
utils::{
flash::{FlashRequest, FlashResponse},
sbot, theme,
},
};
// ROUTE: /scuttlebutt/private
fn public_key_input_template(ssb_id: &Option<String>) -> Markup {
match ssb_id {
Some(id) => {
html! { input type="text" id="publicKey" name="recipient" placeholder="@xYz...=.ed25519" value=(id); }
}
// render the input with autofocus if no ssb_id has been provided
None => {
html! { input type="text" id="publicKey" name="recipient" placeholder="@xYz...=.ed25519" autofocus; }
}
}
}
fn private_message_textarea_template(ssb_id: &Option<String>) -> Markup {
match ssb_id {
Some(_) => {
html! { textarea id="privatePost" class="center input message-input" name="text" title="Compose a private message" placeholder="Write a private message..." autofocus { "" } }
}
// render the textarea with autofocus if an ssb_id has been provided
None => {
html! { textarea id="privatePost" class="center input message-input" name="text" title="Compose a private message" placeholder="Write a private message..." { "" } }
}
}
}
/// Scuttlebutt private message template builder.
///
/// Render a form for publishing a provate message. The recipient input field
/// is populated with the provided ssb_id. If no recipient is provided, the
/// template autofocuses on the recipient input field.
pub fn build_template(request: &Request, ssb_id: Option<String>) -> PreEscaped<String> {
// check for flash cookies; will be (None, None) if no flash cookies are found
let (flash_name, flash_msg) = request.retrieve_flash();
let profile_template = match SbotStatus::read() {
// only render the private message elements if the sbot is active
Ok(status) if status.state == Some("active".to_string()) => {
// retrieve the local public key (set to blank if an error is returned)
let local_id = match sbot::get_local_id() {
Ok(id) => id,
Err(_) => "".to_string(),
};
html! {
(PreEscaped("<!-- SCUTTLEBUTT PRIVATE MESSAGE FORM -->"))
div class="card card-wide center" {
form id="sbotConfig" class="center" action="/scuttlebutt/private" method="post" {
div class="center" style="display: flex; flex-direction: column; margin-bottom: 1rem;" title="Public key (ID) of the peer being written to" {
label for="publicKey" class="label-small font-gray" {
"PUBLIC KEY"
}
(public_key_input_template(&ssb_id))
}
(PreEscaped("<!-- input for message contents -->"))
(private_message_textarea_template(&ssb_id))
(PreEscaped("<!-- hidden input field to pass the public key of the local peer -->"))
input type="hidden" id="localId" name="id" value=(local_id);
(PreEscaped("<!-- BUTTONS -->"))
input id="publish" class="button button-primary center" type="submit" style="margin-top: 1rem;" title="Publish private message to peer" value="Publish";
}
// render flash message if cookies were found in the request
@if let (Some(name), Some(msg)) = (flash_name, flash_msg) {
(PreEscaped("<!-- FLASH MESSAGE -->"))
(templates::flash::build_template(name, msg))
}
}
}
}
_ => templates::inactive::build_template("Private messaging is unavailable."),
};
let body = templates::nav::build_template(profile_template, "Profile", Some("/"));
// query the current theme so we can pass it into the base template builder
let theme = theme::get_theme();
templates::base::build_template(body, theme)
}
/// Publish a private message.
///
/// Parse the public key and private message text from the submitted form
/// and publish the message. Set a flash message communicating the outcome
/// of the publishing attempt and redirect to the private message page.
pub fn handle_form(request: &Request) -> Response {
// query the request body for form data
// return a 400 error if the admin_id field is missing
let data = try_or_400!(post_input!(request, {
id: String,
text: String,
recipient: String
}));
// now we need to add the local id to the recipients vector,
// otherwise the local id will not be able to read the message.
let recipients = vec![data.id, data.recipient];
let (flash_name, flash_msg) = match SbotStatus::read() {
Ok(status) if status.state == Some("active".to_string()) => {
match sbot::publish_private_msg(data.text, recipients) {
Ok(success_msg) => (
"flash_name=success".to_string(),
format!("flash_msg={}", success_msg),
),
Err(error_msg) => (
"flash_name=error".to_string(),
format!("flash_msg={}", error_msg),
),
}
}
_ => (
"flash_name=warning".to_string(),
"Private messaging is unavailable.".to_string(),
),
};
Response::redirect_303("/scuttlebutt/private").add_flash(flash_name, flash_msg)
}

View File

@ -4,7 +4,7 @@ use rouille::Request;
use crate::{
templates,
utils::{flash::FlashRequest, sbot, sbot::Profile},
utils::{flash::FlashRequest, sbot, sbot::Profile, theme},
};
// ROUTE: /scuttlebutt/profile
@ -112,7 +112,7 @@ fn social_interaction_buttons_template(profile: &Profile) -> Markup {
}
@if let Some(ssb_id) = &profile.id {
form class="center" {
a id="privateMessage" class="button button-primary center" href={ "/scuttlebutt/private?public_key=" (ssb_id) } title="Private Message" {
a id="privateMessage" class="button button-primary center" href={ "/scuttlebutt/private/" (ssb_id) } title="Private Message" {
"Send Private Message"
}
}
@ -152,11 +152,11 @@ pub fn build_template(request: &Request, ssb_id: Option<String>) -> PreEscaped<S
// private message buttons
(social_interaction_buttons_template(&profile))
}
}
// render flash message if cookies were found in the request
@if let (Some(name), Some(msg)) = (flash_name, flash_msg) {
(PreEscaped("<!-- FLASH MESSAGE -->"))
(templates::flash::build_template(name, msg))
// render flash message if cookies were found in the request
@if let (Some(name), Some(msg)) = (flash_name, flash_msg) {
(PreEscaped("<!-- FLASH MESSAGE -->"))
(templates::flash::build_template(name, msg))
}
}
}
}
@ -171,5 +171,8 @@ pub fn build_template(request: &Request, ssb_id: Option<String>) -> PreEscaped<S
let body = templates::nav::build_template(profile_template, "Profile", Some("/"));
templates::base::build_template(body)
// query the current theme so we can pass it into the base template builder
let theme = theme::get_theme();
templates::base::build_template(body, theme)
}

View File

@ -8,6 +8,7 @@ use crate::{
flash::{FlashRequest, FlashResponse},
sbot,
sbot::Profile,
theme,
},
};
@ -103,7 +104,10 @@ pub fn build_template(request: &Request) -> PreEscaped<String> {
Some("/scuttlebutt/profile"),
);
templates::base::build_template(body)
// query the current theme so we can pass it into the base template builder
let theme = theme::get_theme();
templates::base::build_template(body, theme)
}
/// Update the name, description and picture for the local Scuttlebutt profile.

View File

@ -0,0 +1,41 @@
use peach_lib::sbot::SbotStatus;
use rouille::{post_input, try_or_400, Request, Response};
use crate::utils::{flash::FlashResponse, sbot};
// ROUTE: /scuttlebutt/publish
/// Publish a public Scuttlebutt post.
///
/// Parse the post text from the submitted form and publish the message.
/// Redirect to the profile page of the PeachCloud local identity with a flash
/// message describing the outcome of the action (may be successful or
/// unsuccessful).
pub fn handle_form(request: &Request) -> Response {
// query the request body for form data
// return a 400 error if the admin_id field is missing
let data = try_or_400!(post_input!(request, {
text: String,
}));
let (flash_name, flash_msg) = match SbotStatus::read() {
Ok(status) if status.state == Some("active".to_string()) => {
match sbot::publish_public_post(data.text) {
Ok(success_msg) => (
"flash_name=success".to_string(),
format!("flash_msg={}", success_msg),
),
Err(error_msg) => (
"flash_name=error".to_string(),
format!("flash_msg={}", error_msg),
),
}
}
_ => (
"flash_name=warning".to_string(),
"Public posting is unavailable.".to_string(),
),
};
Response::redirect_303("/scuttlebutt/profile").add_flash(flash_name, flash_msg)
}

View File

@ -5,7 +5,7 @@ use crate::{
templates,
utils::{
flash::{FlashRequest, FlashResponse},
sbot,
sbot, theme,
},
};
@ -38,7 +38,10 @@ pub fn build_template(request: &Request) -> PreEscaped<String> {
let body =
templates::nav::build_template(search_template, "Search", Some("/scuttlebutt/peers"));
templates::base::build_template(body)
// query the current theme so we can pass it into the base template builder
let theme = theme::get_theme();
templates::base::build_template(body, theme)
}
/// Parse the public key, verify that it's valid and then redirect to the

View File

@ -2,7 +2,10 @@ use maud::{html, PreEscaped};
use peach_lib::config_manager;
use rouille::Request;
use crate::{templates, utils::flash::FlashRequest};
use crate::{
templates,
utils::{flash::FlashRequest, theme},
};
/// Administrator settings menu template builder.
pub fn build_template(request: &Request) -> PreEscaped<String> {
@ -68,6 +71,9 @@ pub fn build_template(request: &Request) -> PreEscaped<String> {
Some("/settings/admin"),
);
// query the current theme so we can pass it into the base template builder
let theme = theme::get_theme();
// render the base template with the provided body
templates::base::build_template(body)
templates::base::build_template(body, theme)
}

View File

@ -1,6 +1,6 @@
use maud::{html, PreEscaped};
use crate::templates;
use crate::{templates, utils::theme};
/// Administrator settings menu template builder.
pub fn build_template() -> PreEscaped<String> {
@ -22,6 +22,9 @@ pub fn build_template() -> PreEscaped<String> {
let body =
templates::nav::build_template(menu_template, "Administrator Settings", Some("/settings"));
// query the current theme so we can pass it into the base template builder
let theme = theme::get_theme();
// render the base template with the provided body
templates::base::build_template(body)
templates::base::build_template(body, theme)
}

View File

@ -1,6 +1,6 @@
use maud::{html, PreEscaped};
use crate::{templates, CONFIG};
use crate::{templates, utils::theme, CONFIG};
// ROUTE: /settings
@ -25,6 +25,9 @@ pub fn build_template() -> PreEscaped<String> {
// parameters are template, title and back url
let body = templates::nav::build_template(menu_template, "Settings", Some("/"));
// query the current theme so we can pass it into the base template builder
let theme = theme::get_theme();
// render the base template with the provided body
templates::base::build_template(body)
templates::base::build_template(body, theme)
}

View File

@ -3,4 +3,4 @@ pub mod admin;
pub mod menu;
//pub mod network;
pub mod scuttlebutt;
//pub mod theme;
pub mod theme;

View File

@ -7,7 +7,7 @@ use crate::{
templates,
utils::{
flash::{FlashRequest, FlashResponse},
sbot,
sbot, theme,
},
};
@ -38,7 +38,7 @@ fn read_status_and_config() -> (String, SbotConfig, String, String) {
Some(index) => {
let (ip, port) = sbot_config.lis.split_at(index);
// remove the : from the port
(ip.to_string(), port.replace(':', "").to_string())
(ip.to_string(), port.replace(':', ""))
}
// if no ':' separator is found, assume an ip has been configured (without port)
None => (sbot_config.lis.to_string(), String::new()),
@ -178,8 +178,11 @@ pub fn build_template(request: &Request) -> PreEscaped<String> {
let body =
templates::nav::build_template(menu_template, "Scuttlebutt Settings", Some("/settings"));
// query the current theme so we can pass it into the base template builder
let theme = theme::get_theme();
// render the base template with the provided body
templates::base::build_template(body)
templates::base::build_template(body, theme)
}
/// Parse the sbot configuration values and write to file.

View File

@ -2,7 +2,10 @@ use maud::{html, PreEscaped};
use peach_lib::sbot::SbotStatus;
use rouille::Request;
use crate::{templates, utils::flash::FlashRequest};
use crate::{
templates,
utils::{flash::FlashRequest, theme},
};
/// Read the status of the go-sbot service and render buttons accordingly.
fn render_process_buttons() -> PreEscaped<String> {
@ -55,6 +58,9 @@ pub fn build_template(request: &Request) -> PreEscaped<String> {
let body =
templates::nav::build_template(menu_template, "Scuttlebutt Settings", Some("/settings"));
// query the current theme so we can pass it into the base template builder
let theme = theme::get_theme();
// render the base template with the provided body
templates::base::build_template(body)
templates::base::build_template(body, theme)
}

View File

@ -1,16 +1,16 @@
use rocket::{get, response::Redirect};
use rouille::Response;
use crate::routes::authentication::Authenticated;
use crate::{utils, utils::Theme};
use crate::utils::{theme, theme::Theme};
// ROUTE: /settings/theme/{theme}
/// Set the user-interface theme according to the query parameter value.
#[get("/theme?<theme>")]
pub fn set_theme(_auth: Authenticated, theme: &str) -> Redirect {
match theme {
"light" => utils::set_theme(Theme::Light),
"dark" => utils::set_theme(Theme::Dark),
pub fn set_theme(theme: String) -> Response {
match theme.as_str() {
"light" => theme::set_theme(Theme::Light),
"dark" => theme::set_theme(Theme::Dark),
_ => (),
}
Redirect::to("/")
Response::redirect_303("/")
}

View File

@ -1,8 +1,10 @@
use maud::{html, Markup, PreEscaped};
use peach_lib::sbot::{SbotConfig, SbotStatus};
use crate::templates;
use crate::utils::sbot;
use crate::{
templates,
utils::{sbot, theme},
};
// HTML RENDERING FOR ELEMENTS
@ -274,6 +276,9 @@ pub fn build_template() -> PreEscaped<String> {
// parameters are template, title and back url
let body = templates::nav::build_template(status_template, "Settings", Some("/"));
// query the current theme so we can pass it into the base template builder
let theme = theme::get_theme();
// render the base template with the provided body
templates::base::build_template(body)
templates::base::build_template(body, theme)
}

View File

@ -3,10 +3,10 @@ use maud::{html, PreEscaped, DOCTYPE};
/// Base template builder.
///
/// Takes an HTML body as input and splices it into the base template.
pub fn build_template(body: PreEscaped<String>) -> PreEscaped<String> {
pub fn build_template(body: PreEscaped<String>, theme: String) -> PreEscaped<String> {
html! {
(DOCTYPE)
html lang="en" data-theme="light";
html lang="en" data-theme=(theme);
head {
meta charset="utf-8";
meta name="description" content="PeachCloud web interface";

View File

@ -3,4 +3,5 @@ pub mod error;
pub mod flash;
pub mod inactive;
pub mod nav;
pub mod not_found;
pub mod peers_list;

View File

@ -19,7 +19,7 @@ pub fn build_template(
"dark" => (
"/icons/hermies_hex_light.svg",
html! {
a class="nav-item" href="/theme?theme=light" {
a class="nav-item" href="/settings/theme/light" {
img class="icon-medium nav-icon-right icon-active" title="Toggle theme" src="/icons/sun.png" alt="Sun";
}
},
@ -28,7 +28,7 @@ pub fn build_template(
_ => (
"/icons/hermies_hex.svg",
html! {
a class="nav-item" href="/theme?theme=dark" {
a class="nav-item" href="/settings/theme/dark" {
img class="icon-medium nav-icon-right icon-active" title="Toggle theme" src="/icons/moon.png" alt="Moon";
}
},

View File

@ -0,0 +1,34 @@
use maud::{html, PreEscaped};
use crate::{templates, utils::theme};
// 404 ROUTE NOT FOUND CATCHER
/// 404 template builder.
pub fn build_template() -> PreEscaped<String> {
let not_found_template = html! {
div class="card center" {
div class="capsule-container" {
div class="capsule info-border" {
p {
"No PeachCloud resource exists for this URL. Please ensure that the URL in the address bar is correct."
}
p {
"Click the back arrow in the top-left or the PeachCloud logo at the bottom of your screen to return Home."
}
}
}
}
};
// wrap the nav bars around the settings menu template content
// parameters are template, title and back url
let body =
templates::nav::build_template(not_found_template, "404: Route Not Found", Some("/"));
// query the current theme so we can pass it into the base template builder
let theme = theme::get_theme();
// render the base template with the provided body
templates::base::build_template(body, theme)
}

View File

@ -3,7 +3,7 @@ use std::collections::HashMap;
use maud::{html, Markup, PreEscaped};
use peach_lib::sbot::SbotStatus;
use crate::templates;
use crate::{templates, utils::theme};
/// Render an unordered list of peers with one list element for each peer.
fn peers_template(peers: Vec<HashMap<String, String>>) -> Markup {
@ -71,6 +71,9 @@ pub fn build_template(peers: Vec<HashMap<String, String>>, title: &str) -> PreEs
let body =
templates::nav::build_template(peer_list_template, title, Some("/scuttlebutt/peers"));
// query the current theme so we can pass it into the base template builder
let theme = theme::get_theme();
// render the base template with the provided body
templates::base::build_template(body)
templates::base::build_template(body, theme)
}

View File

@ -497,6 +497,59 @@ pub fn get_friends_list() -> Result<Vec<HashMap<String, String>>, Box<dyn Error>
})
}
/// Retrieve the local public key (id).
pub fn get_local_id() -> Result<String, Box<dyn Error>> {
// retrieve latest go-sbot configuration parameters
let sbot_config = SbotConfig::read().ok();
task::block_on(async {
let mut sbot_client = init_sbot_with_config(&sbot_config).await?;
let local_id = sbot_client.whoami().await?;
Ok(local_id)
})
}
/// Publish a public post.
pub fn publish_public_post(text: String) -> Result<String, String> {
// retrieve latest go-sbot configuration parameters
let sbot_config = SbotConfig::read().ok();
task::block_on(async {
let mut sbot_client = init_sbot_with_config(&sbot_config)
.await
.map_err(|e| e.to_string())?;
// TODO: debug!("Publishing new Scuttlebutt public post");
match sbot_client.publish_post(&text).await {
Ok(_) => Ok("Published post".to_string()),
Err(e) => Err(format!("Failed to publish post: {}", e)),
}
})
}
/// Publish a private message.
pub fn publish_private_msg(text: String, recipients: Vec<String>) -> Result<String, String> {
// retrieve latest go-sbot configuration parameters
let sbot_config = SbotConfig::read().ok();
task::block_on(async {
let mut sbot_client = init_sbot_with_config(&sbot_config)
.await
.map_err(|e| e.to_string())?;
// TODO: debug!("Publishing a new Scuttlebutt private message");
match sbot_client
.publish_private(text.to_string(), recipients)
.await
{
Ok(_) => Ok("Published private message".to_string()),
Err(e) => Err(format!("Failed to publish private message: {}", e)),
}
})
}
// FILEPATH FUNCTIONS
/// Return the path of the ssb-go directory.

View File

@ -23,112 +23,3 @@ pub fn set_theme(theme: Theme) {
let mut writable_theme = THEME.write().unwrap();
*writable_theme = theme;
}
// get_cookie
// set_cookie
/*
pub mod monitor;
use std::io::prelude::*;
use std::{fs, fs::File, path::Path};
use dirs;
use golgi::blobs;
use log::info;
use peach_lib::sbot::SbotConfig;
use rocket::{
fs::TempFile,
response::{Redirect, Responder},
serde::Serialize,
};
use rocket_dyn_templates::Template;
use temporary::Directory;
use crate::{error::PeachWebError, THEME};
// FILEPATH FUNCTIONS
// return the path of the ssb-go directory
pub fn get_go_ssb_path() -> Result<String, PeachWebError> {
let go_ssb_path = match SbotConfig::read() {
Ok(conf) => conf.repo,
// return the default path if unable to read `config.toml`
Err(_) => {
// determine the home directory
let mut home_path = dirs::home_dir().ok_or_else(|| PeachWebError::HomeDir)?;
// add the go-ssb subdirectory
home_path.push(".ssb-go");
// convert the PathBuf to a String
home_path
.into_os_string()
.into_string()
.map_err(|_| PeachWebError::OsString)?
}
};
Ok(go_ssb_path)
}
// check whether a blob is in the blobstore
pub async fn blob_is_stored_locally(blob_path: &str) -> Result<bool, PeachWebError> {
let go_ssb_path = get_go_ssb_path()?;
let complete_path = format!("{}/blobs/sha256/{}", go_ssb_path, blob_path);
let blob_exists_locally = Path::new(&complete_path).exists();
Ok(blob_exists_locally)
}
// take the path to a file, add it to the blobstore and return the blob id
pub async fn write_blob_to_store(file: &mut TempFile<'_>) -> Result<String, PeachWebError> {
// create temporary directory and path
let temp_dir = Directory::new("blob")?;
// we performed a `file.name().is_some()` check before calling `write_blob_to_store`
// so it should be safe to do a simple unwrap here
let filename = file.name().expect("retrieving filename from uploaded file");
let temp_path = temp_dir.join(filename);
// write file to temporary path
file.persist_to(&temp_path).await?;
// open the file and read it into a buffer
let mut file = File::open(&temp_path)?;
let mut buffer = Vec::new();
file.read_to_end(&mut buffer)?;
// hash the bytes representing the file
let (hex_hash, blob_id) = blobs::hash_blob(&buffer)?;
// define the blobstore path and blob filename
let (blob_dir, blob_filename) = hex_hash.split_at(2);
let go_ssb_path = get_go_ssb_path()?;
let blobstore_sub_dir = format!("{}/blobs/sha256/{}", go_ssb_path, blob_dir);
// create the blobstore sub-directory
fs::create_dir_all(&blobstore_sub_dir)?;
// copy the file to the blobstore
let blob_path = format!("{}/{}", blobstore_sub_dir, blob_filename);
fs::copy(temp_path, blob_path)?;
Ok(blob_id)
}
// HELPER FUNCTIONS
#[derive(Debug, Serialize)]
pub struct FlashContext {
pub flash_name: Option<String>,
pub flash_msg: Option<String>,
}
/// A helper enum which allows routes to either return a Template or a Redirect
/// from: https://github.com/SergioBenitez/Rocket/issues/253#issuecomment-532356066
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Responder)]
pub enum TemplateOrRedirect {
Template(Template),
Redirect(Redirect),
}
*/