add basic peer lookup, improve follows and profile route handlers

This commit is contained in:
glyph 2022-02-28 16:08:05 +02:00
parent 436a516c3e
commit e10468c337
4 changed files with 90 additions and 41 deletions

View File

@ -72,6 +72,7 @@ pub fn mount_peachpub_routes(rocket: Rocket<Build>) -> Rocket<Build> {
invites,
create_invite,
peers,
peer_lookup,
friends,
follows,
followers,

View File

@ -14,7 +14,10 @@ use rocket::{
use rocket_dyn_templates::{tera::Context, Template};
use crate::{
context::{sbot, sbot::ProfileContext},
context::{
sbot,
sbot::{FollowsContext, ProfileContext},
},
routes::authentication::Authenticated,
utils,
};
@ -141,6 +144,32 @@ pub fn private(flash: Option<FlashMessage>, _auth: Authenticated) -> Template {
Template::render("scuttlebutt/messages", &context)
}
// HELPERS AND ROUTES FOR /peer/lookup
/// Lookup and define peer relationships.
#[get("/peer/lookup")]
pub fn peer_lookup(flash: Option<FlashMessage>, _auth: Authenticated) -> Template {
// retrieve current ui theme
let theme = utils::get_theme();
// retrieve go-sbot systemd process status
let sbot_status = SbotStatus::read().ok();
let mut context = Context::new();
context.insert("theme", &theme);
context.insert("sbot_status", &sbot_status);
context.insert("title", &Some("Peer Lookup"));
// check to see if there is a flash message to display
if let Some(flash) = flash {
// add flash message contents to the context object
context.insert("flash_name", &Some(flash.kind().to_string()));
context.insert("flash_msg", &Some(flash.message().to_string()));
};
Template::render("scuttlebutt/peer_lookup", &context.into_json())
}
// HELPERS AND ROUTES FOR /peers
#[derive(Debug, Serialize)]
@ -301,16 +330,19 @@ pub fn block(pub_key: Form<PublicKey>, _auth: Authenticated) -> Flash<Redirect>
/// or the profile of a peer. If the public key query parameter is not provided,
/// the local profile is displayed (ie. the profile of the public key associated
/// with the local PeachCloud device).
// TODO: this query param approach is not going to work because some characters
// get escaped. maybe we can use a RawString type?
#[get("/profile?<pub_key>")]
#[get("/profile?<public_key>")]
pub async fn profile(
pub_key: Option<String>,
mut public_key: Option<String>,
flash: Option<FlashMessage<'_>>,
_auth: Authenticated,
) -> Template {
if let Some(ref key) = public_key {
// `url_decode` replaces '+' with ' ', so we need to revert that
public_key = Some(key.replace(' ', "+"));
}
// build the profile context object
let context = ProfileContext::build(pub_key).await;
let context = ProfileContext::build(public_key).await;
match context {
// we were able to build the context without errors
@ -548,47 +580,35 @@ pub fn friends(flash: Option<FlashMessage>, _auth: Authenticated) -> Template {
// HELPERS AND ROUTES FOR /follows
#[derive(Debug, Serialize)]
pub struct FollowsContext {
pub back: Option<String>,
pub flash_name: Option<String>,
pub flash_msg: Option<String>,
pub title: Option<String>,
pub theme: Option<String>,
}
impl FollowsContext {
pub fn build() -> FollowsContext {
FollowsContext {
back: None,
flash_name: None,
flash_msg: None,
title: None,
theme: None,
}
}
}
/// A list of follows (peers we follow who do not follow us), with each list item displaying the name, image and public
/// key of the peer.
#[get("/follows")]
pub fn follows(flash: Option<FlashMessage>, _auth: Authenticated) -> Template {
// retrieve current ui theme
let theme = utils::get_theme();
pub async fn follows(flash: Option<FlashMessage<'_>>, _auth: Authenticated) -> Template {
// build the follows context object
let context = FollowsContext::build().await;
let mut context = FollowsContext::build();
context.back = Some("/scuttlebutt/peers".to_string());
context.title = Some("Follows".to_string());
context.theme = Some(theme);
match context {
// we were able to build the context without errors
Ok(mut context) => {
// check to see if there is a flash message to display
if let Some(flash) = flash {
// add flash message contents to the context object
context.flash_name = Some(flash.kind().to_string());
context.flash_msg = Some(flash.message().to_string());
};
// check to see if there is a flash message to display
if let Some(flash) = flash {
// add flash message contents to the context object
context.flash_name = Some(flash.kind().to_string());
context.flash_msg = Some(flash.message().to_string());
};
Template::render("scuttlebutt/peers_list", &context)
}
// an error occurred while building the context
Err(e) => {
// build the default context and pass along the error message
let mut context = FollowsContext::default();
context.flash_name = Some("error".to_string());
context.flash_msg = Some(e.to_string());
Template::render("scuttlebutt/peers_list", &context)
Template::render("scuttlebutt/peers_list", &context)
}
}
}
// HELPERS AND ROUTES FOR /followers

View File

@ -0,0 +1,26 @@
{%- extends "nav" -%}
{%- block card %}
{# ASSIGN VARIABLES #}
{# ---------------- #}
<!-- PEER LOOKUP FORM -->
<div class="card center">
<form id="sbotConfig" class="center" action="/scuttlebutt/peers/lookup" method="post">
<div class="center" style="display: flex; flex-direction: column; margin-bottom: 2rem;" title="Public key (ID) of a peer">
<label for="publicKey" class="label-small font-gray">PUBLIC KEY</label>
<input type="text" id="publicKey" name="public_key" placeholder="@xYz...=.sha256">
</div>
<!-- RELATIONSHIP CHECKBOXES -->
<div class="center">
<input type="checkbox" id="following" style="margin-bottom: 1rem;" name="following"{% if is_following and is_following == true %} checked{% endif %}>
<label class="font-normal" for="following" title="Follow or unfollow the peer represented by the public key above">Following</label><br>
<input type="checkbox" id="blocking" name="blocking"{% if is_blocking and is_blocking == true %} checked{% endif %}>
<label class="font-normal" for="blocking" title="Block or unblock the peer represented by the public key above">Blocking</label><br>
</div>
<!-- BUTTONS -->
<input id="lookupRelationship" class="button button-primary center" style="margin-top: 2rem;" type="submit" title="Lookup relationship" value="Lookup">
<input id="setRelationship" class="button button-primary center" type="submit" title="Publish relationship" value="Publish">
</form>
<!-- FLASH MESSAGE -->
{% include "snippets/flash_message" %}
</div>
{%- endblock card -%}

View File

@ -5,6 +5,8 @@
<div class="card-container">
<!-- BUTTONS -->
<div id="buttons">
<!-- links to a form with peer_id input and checkboxes for follow etc. -->
<a id="lookup" class="button button-primary center" href="/scuttlebutt/peer/lookup" title="Define Peer Relationship">Peer Lookup</a>
<a id="friends" class="button button-primary center" href="/scuttlebutt/friends" title="List Friends">Friends</a>
<a id="follows" class="button button-primary center" href="/scuttlebutt/follows" title="List Follows">Follows</a>
<a id="followers" class="button button-primary center" href="/scuttlebutt/followers" title="List Followers">Followers</a>