257 lines
8.6 KiB
Rust
257 lines
8.6 KiB
Rust
//! Web server route handlers.
|
|
|
|
use async_std::channel::Sender;
|
|
use log::{info, warn};
|
|
use rocket::{form::Form, get, post, response::Redirect, uri, FromForm, State};
|
|
use rocket_dyn_templates::{tera::Context, Template};
|
|
|
|
use crate::{
|
|
db::{Database, Peer},
|
|
sbot,
|
|
task_loop::Task,
|
|
utils, WhoAmI,
|
|
};
|
|
|
|
#[derive(FromForm)]
|
|
pub struct PeerForm {
|
|
pub public_key: String,
|
|
}
|
|
|
|
#[get("/")]
|
|
pub async fn home(db: &State<Database>) -> Template {
|
|
let peers = db.get_peers();
|
|
let mut peers_unread = Vec::new();
|
|
for peer in peers {
|
|
let unread_count = db.get_unread_post_count(&peer.public_key);
|
|
peers_unread.push((peer, unread_count.to_string()));
|
|
}
|
|
|
|
let mut context = Context::new();
|
|
context.insert("peers", &peers_unread);
|
|
|
|
Template::render("base", &context.into_json())
|
|
}
|
|
|
|
#[get("/posts/<public_key>/<msg_id>/delete")]
|
|
pub async fn delete_post(db: &State<Database>, public_key: &str, msg_id: &str) -> Redirect {
|
|
// Delete the post from the database. This method cannot panic, so we're
|
|
// safe to unwrap the result.
|
|
match db.remove_post(public_key, msg_id) {
|
|
Ok(_) => info!(
|
|
"Removed post {} by {} from 'posts' database tree",
|
|
msg_id, public_key
|
|
),
|
|
Err(e) => warn!(
|
|
"Failed to remove post {} by {} from 'posts' database tree: {}",
|
|
msg_id, public_key, e
|
|
),
|
|
}
|
|
|
|
Redirect::to(uri!(posts(public_key)))
|
|
}
|
|
|
|
#[get("/posts/<public_key>")]
|
|
pub async fn posts(db: &State<Database>, public_key: &str) -> Template {
|
|
let peers = db.get_peers();
|
|
let mut peers_unread = Vec::new();
|
|
for peer in peers {
|
|
let unread_count = db.get_unread_post_count(&peer.public_key);
|
|
peers_unread.push((peer, unread_count.to_string()));
|
|
}
|
|
|
|
let posts = db.get_posts(public_key).unwrap();
|
|
|
|
let mut context = Context::new();
|
|
context.insert("selected_peer", &public_key);
|
|
context.insert("peers", &peers_unread);
|
|
context.insert("posts", &posts);
|
|
|
|
Template::render("base", &context.into_json())
|
|
}
|
|
|
|
#[get("/posts/<public_key>/<msg_id>")]
|
|
pub async fn post(db: &State<Database>, public_key: &str, msg_id: &str) -> Template {
|
|
let peers = db.get_peers();
|
|
let mut peers_unread = Vec::new();
|
|
for peer in peers {
|
|
let unread_count = db.get_unread_post_count(&peer.public_key);
|
|
peers_unread.push((peer, unread_count.to_string()));
|
|
}
|
|
|
|
let posts = db.get_posts(public_key).unwrap();
|
|
let post = db.get_post(public_key, msg_id).unwrap();
|
|
|
|
let mut context = Context::new();
|
|
context.insert("peers", &peers_unread);
|
|
context.insert("selected_peer", &public_key);
|
|
context.insert(
|
|
"selected_peer_encoded",
|
|
&uri_encode::encode_uri_component(public_key),
|
|
);
|
|
context.insert("selected_post", &msg_id);
|
|
context.insert(
|
|
"selected_post_encoded",
|
|
&uri_encode::encode_uri_component(msg_id),
|
|
);
|
|
context.insert("posts", &posts);
|
|
context.insert("post", &post);
|
|
context.insert("post_is_selected", &true);
|
|
|
|
Template::render("base", &context.into_json())
|
|
}
|
|
|
|
#[get("/posts/<public_key>/<msg_id>/read")]
|
|
pub async fn mark_post_read(db: &State<Database>, public_key: &str, msg_id: &str) -> Redirect {
|
|
// Retrieve the post from the database, mark it as read and reinsert it.
|
|
if let Ok(Some(mut post)) = db.get_post(public_key, msg_id) {
|
|
post.read = true;
|
|
db.add_post(public_key, post).unwrap();
|
|
} else {
|
|
warn!(
|
|
"Failed to find post {} authored by {} in 'posts' database tree",
|
|
msg_id, public_key
|
|
)
|
|
}
|
|
|
|
Redirect::to(uri!(post(public_key, msg_id)))
|
|
}
|
|
|
|
#[get("/posts/<public_key>/<msg_id>/unread")]
|
|
pub async fn mark_post_unread(db: &State<Database>, public_key: &str, msg_id: &str) -> Redirect {
|
|
// Retrieve the post from the database, mark it as unread and reinsert it.
|
|
if let Ok(Some(mut post)) = db.get_post(public_key, msg_id) {
|
|
post.read = false;
|
|
db.add_post(public_key, post).unwrap();
|
|
} else {
|
|
warn!(
|
|
"Failed to find post {} authored by {} in 'posts' database tree",
|
|
msg_id, public_key
|
|
)
|
|
}
|
|
|
|
Redirect::to(uri!(post(public_key, msg_id)))
|
|
}
|
|
|
|
#[get("/posts/download_latest")]
|
|
pub async fn download_latest_posts(db: &State<Database>, tx: &State<Sender<Task>>) -> Redirect {
|
|
for peer in db.get_peers() {
|
|
// Fetch the latest root posts authored by each peer we're
|
|
// subscribed to. Posts will be added to the key-value database.
|
|
if let Err(e) = tx
|
|
.send(Task::FetchLatestPosts(peer.public_key.clone()))
|
|
.await
|
|
{
|
|
warn!("Task loop error: {}", e)
|
|
}
|
|
|
|
// Fetch the latest name for each peer we're subscribed to and update
|
|
// the database.
|
|
if let Err(e) = tx.send(Task::FetchLatestName(peer.public_key)).await {
|
|
warn!("Task loop error: {}", e)
|
|
}
|
|
}
|
|
|
|
Redirect::to(uri!(home))
|
|
}
|
|
|
|
#[post("/subscribe", data = "<peer>")]
|
|
pub async fn subscribe_form(
|
|
db: &State<Database>,
|
|
whoami: &State<WhoAmI>,
|
|
tx: &State<Sender<Task>>,
|
|
peer: Form<PeerForm>,
|
|
) -> Redirect {
|
|
info!("Subscribing to peer {}", &peer.public_key);
|
|
|
|
if let Err(e) = utils::validate_public_key(&peer.public_key) {
|
|
warn!("Public key {} is invalid: {}", &peer.public_key, e)
|
|
} else {
|
|
info!("Public key {} is valid", &peer.public_key);
|
|
|
|
// Retrieve the name of the peer to which we are subscribing.
|
|
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);
|
|
|
|
// Add the peer to the database and then check the follow state.
|
|
// Follow the peer if our local instance is not already following.
|
|
if db.add_peer(peer_info).is_ok() {
|
|
info!("Added {} to 'peers' database tree", &peer.public_key);
|
|
match sbot::is_following(&whoami.0, &peer.public_key).await {
|
|
Ok(status) if status.as_str() == "false" => {
|
|
match sbot::follow_peer(&peer.public_key).await {
|
|
Ok(_) => info!("Followed peer {}", &peer.public_key),
|
|
Err(e) => warn!("Failed to follow peer {}: {}", &peer.public_key, e),
|
|
}
|
|
}
|
|
Ok(status) if status.as_str() == "true" => {
|
|
info!(
|
|
"Already following peer {}. No further action taken",
|
|
&peer.public_key
|
|
)
|
|
}
|
|
_ => (),
|
|
}
|
|
|
|
let peer_id = peer.public_key.to_string();
|
|
// Fetch all root posts authored by the peer we're subscribing
|
|
// to. Posts will be added to the key-value database.
|
|
if let Err(e) = tx.send(Task::FetchAllPosts(peer_id)).await {
|
|
warn!("Task loop error: {}", e)
|
|
}
|
|
} else {
|
|
warn!(
|
|
"Failed to add peer {} to 'peers' database tree",
|
|
&peer.public_key
|
|
)
|
|
}
|
|
}
|
|
|
|
Redirect::to(uri!(home))
|
|
}
|
|
|
|
#[post("/unsubscribe", data = "<peer>")]
|
|
pub async fn unsubscribe_form(
|
|
db: &State<Database>,
|
|
whoami: &State<WhoAmI>,
|
|
peer: Form<PeerForm>,
|
|
) -> Redirect {
|
|
info!("Unsubscribing from peer {}", &peer.public_key);
|
|
|
|
if let Err(e) = utils::validate_public_key(&peer.public_key) {
|
|
warn!("Public key {} is invalid: {}", &peer.public_key, e)
|
|
} else {
|
|
info!("Public key {} is valid", &peer.public_key);
|
|
if db.remove_peer(&peer.public_key).is_ok() {
|
|
info!(
|
|
"Removed peer {} from 'peers' database tree",
|
|
&peer.public_key
|
|
);
|
|
match sbot::is_following(&whoami.0, &peer.public_key).await {
|
|
Ok(status) if status.as_str() == "true" => {
|
|
info!("Unfollowing peer {}", &peer.public_key);
|
|
match sbot::unfollow_peer(&peer.public_key).await {
|
|
Ok(_) => {
|
|
info!("Unfollowed peer {}", &peer.public_key);
|
|
}
|
|
Err(e) => warn!("Failed to unfollow peer {}: {}", &peer.public_key, e),
|
|
}
|
|
}
|
|
_ => (),
|
|
}
|
|
} else {
|
|
warn!(
|
|
"Failed to remove peer {} from 'peers' database tree",
|
|
&peer.public_key
|
|
);
|
|
}
|
|
}
|
|
|
|
Redirect::to(uri!(home))
|
|
}
|