Compare commits
No commits in common. "4d2906bf7828318da8dbbd987feaefd86f53ff89" and "28889eb338aa8f0a2d335db98445674f8caa64c2" have entirely different histories.
4d2906bf78
...
28889eb338
5 changed files with 60 additions and 66 deletions
|
@ -1,5 +1,6 @@
|
||||||
use leptos::*;
|
use leptos::*;
|
||||||
use leptos_router::*;
|
use leptos_router::*;
|
||||||
|
use reqwest::Method;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
|
||||||
use leptos_use::{storage::use_local_storage, use_cookie, utils::{FromToStringCodec, JsonCodec}};
|
use leptos_use::{storage::use_local_storage, use_cookie, utils::{FromToStringCodec, JsonCodec}};
|
||||||
|
@ -35,21 +36,36 @@ pub fn App() -> impl IntoView {
|
||||||
|
|
||||||
let title_target = move || if auth.present() { "/web/home" } else { "/web/server" };
|
let title_target = move || if auth.present() { "/web/home" } else { "/web/server" };
|
||||||
|
|
||||||
local_tl.more(auth); // public outbox never contains private posts
|
if let Some(tok) = token.get_untracked() {
|
||||||
spawn_local(async move {
|
spawn_local(async move {
|
||||||
// refresh token first, or verify that we're still authed
|
// refresh token first, or verify that we're still authed
|
||||||
if Auth::refresh(auth.token, set_token, set_userid).await {
|
match reqwest::Client::new()
|
||||||
home_tl.more(auth); // home inbox requires auth to be read
|
.request(Method::PATCH, format!("{URL_BASE}/auth"))
|
||||||
|
.json(&serde_json::json!({"token": tok}))
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Err(e) => tracing::error!("could not refresh token: {e}"),
|
||||||
|
Ok(res) => match res.error_for_status() {
|
||||||
|
Err(e) => tracing::error!("server rejected refresh: {e}"),
|
||||||
|
Ok(doc) => match doc.json::<AuthResponse>().await {
|
||||||
|
Err(e) => tracing::error!("failed parsing auth response: {e}"),
|
||||||
|
Ok(auth) => {
|
||||||
|
set_token.set(Some(auth.token));
|
||||||
|
set_userid.set(Some(auth.user));
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
server_tl.more(auth); // server inbox may contain private posts
|
|
||||||
});
|
|
||||||
|
|
||||||
|
server_tl.more(auth);
|
||||||
// refresh token every hour
|
local_tl.more(auth);
|
||||||
set_interval(
|
if auth.token.get_untracked().is_some() { home_tl.more(auth) };
|
||||||
move || spawn_local(async move { Auth::refresh(auth.token, set_token, set_userid).await; }),
|
})
|
||||||
std::time::Duration::from_secs(3600)
|
} else {
|
||||||
);
|
server_tl.more(auth);
|
||||||
|
local_tl.more(auth);
|
||||||
|
}
|
||||||
|
|
||||||
view! {
|
view! {
|
||||||
<nav class="w-100 mt-1 mb-1 pb-s">
|
<nav class="w-100 mt-1 mb-1 pb-s">
|
||||||
|
|
|
@ -1,6 +1,13 @@
|
||||||
use leptos::*;
|
use leptos::*;
|
||||||
use reqwest::Method;
|
use crate::URL_BASE;
|
||||||
use crate::{components::AuthResponse, URL_BASE};
|
|
||||||
|
pub trait AuthToken {
|
||||||
|
fn present(&self) -> bool;
|
||||||
|
fn token(&self) -> String;
|
||||||
|
fn user_id(&self) -> String;
|
||||||
|
fn username(&self) -> String;
|
||||||
|
fn outbox(&self) -> String;
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub struct Auth {
|
pub struct Auth {
|
||||||
|
@ -8,16 +15,16 @@ pub struct Auth {
|
||||||
pub userid: Signal<Option<String>>,
|
pub userid: Signal<Option<String>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Auth {
|
impl AuthToken for Auth {
|
||||||
pub fn token(&self) -> String {
|
fn token(&self) -> String {
|
||||||
self.token.get().unwrap_or_default()
|
self.token.get().unwrap_or_default()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn user_id(&self) -> String {
|
fn user_id(&self) -> String {
|
||||||
self.userid.get().unwrap_or_default()
|
self.userid.get().unwrap_or_default()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn username(&self) -> String {
|
fn username(&self) -> String {
|
||||||
// TODO maybe cache this?? how often do i need it?
|
// TODO maybe cache this?? how often do i need it?
|
||||||
self.userid.get()
|
self.userid.get()
|
||||||
.unwrap_or_default()
|
.unwrap_or_default()
|
||||||
|
@ -27,40 +34,11 @@ impl Auth {
|
||||||
.to_string()
|
.to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn present(&self) -> bool {
|
fn present(&self) -> bool {
|
||||||
self.token.get().map_or(false, |x| !x.is_empty())
|
self.token.get().map_or(false, |x| !x.is_empty())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn outbox(&self) -> String {
|
fn outbox(&self) -> String {
|
||||||
format!("{URL_BASE}/actors/{}/outbox", self.username())
|
format!("{URL_BASE}/actors/{}/outbox", self.username())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn refresh(
|
|
||||||
token: Signal<Option<String>>,
|
|
||||||
set_token: WriteSignal<Option<String>>,
|
|
||||||
set_userid: WriteSignal<Option<String>>
|
|
||||||
) -> bool {
|
|
||||||
if let Some(tok) = token.get_untracked() {
|
|
||||||
match reqwest::Client::new()
|
|
||||||
.request(Method::PATCH, format!("{URL_BASE}/auth"))
|
|
||||||
.json(&serde_json::json!({"token": tok}))
|
|
||||||
.send()
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Err(e) => tracing::error!("could not refresh token: {e}"),
|
|
||||||
Ok(res) => match res.error_for_status() {
|
|
||||||
Err(e) => tracing::error!("server rejected refresh: {e}"),
|
|
||||||
Ok(doc) => match doc.json::<AuthResponse>().await {
|
|
||||||
Err(e) => tracing::error!("failed parsing auth response: {e}"),
|
|
||||||
Ok(auth) => {
|
|
||||||
set_token.set(Some(auth.token));
|
|
||||||
set_userid.set(Some(auth.user));
|
|
||||||
return true;
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -83,23 +83,21 @@ pub fn TimelineRepliesRecursive(tl: Timeline, root: String) -> impl IntoView {
|
||||||
let root_values = move || tl.feed
|
let root_values = move || tl.feed
|
||||||
.get()
|
.get()
|
||||||
.into_iter()
|
.into_iter()
|
||||||
|
.filter_map(|x| CACHE.get(&x))
|
||||||
.filter_map(|x| {
|
.filter_map(|x| {
|
||||||
let document = CACHE.get(&x)?;
|
let oid = match x.object_type().ok()? {
|
||||||
let (oid, reply) = match document.object_type().ok()? {
|
|
||||||
// if it's a create, get and check created object: does it reply to root?
|
// if it's a create, get and check created object: does it reply to root?
|
||||||
apb::ObjectType::Activity(apb::ActivityType::Create) => {
|
apb::ObjectType::Activity(apb::ActivityType::Create) =>
|
||||||
let object = CACHE.get(document.object().id().ok()?)?;
|
CACHE.get(x.object().id().ok()?)?.in_reply_to().id().str()?,
|
||||||
(object.id().str()?, object.in_reply_to().id().str()?)
|
|
||||||
},
|
|
||||||
|
|
||||||
// if it's a raw note, directly check if it replies to root
|
// if it's a raw note, directly check if it replies to root
|
||||||
apb::ObjectType::Note => (document.id().str()?, document.in_reply_to().id().str()?),
|
apb::ObjectType::Note => x.in_reply_to().id().str()?,
|
||||||
|
|
||||||
// if it's anything else, check if it relates to root, maybe like or announce?
|
// if it's anything else, check if it relates to root, maybe like or announce?
|
||||||
_ => (document.id().str()?, document.object().id().str()?),
|
_ => x.object().id().str()?,
|
||||||
};
|
};
|
||||||
if reply == root {
|
if oid == root {
|
||||||
Some((oid, document))
|
Some((oid, x))
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
@ -110,7 +108,7 @@ pub fn TimelineRepliesRecursive(tl: Timeline, root: String) -> impl IntoView {
|
||||||
<For
|
<For
|
||||||
each=root_values
|
each=root_values
|
||||||
key=|(id, _obj)| id.clone()
|
key=|(id, _obj)| id.clone()
|
||||||
children=move |(id, obj)|
|
children=move |(id, obj)| {
|
||||||
view! {
|
view! {
|
||||||
<div class="context depth-r">
|
<div class="context depth-r">
|
||||||
<Item item=obj replies=true />
|
<Item item=obj replies=true />
|
||||||
|
@ -119,6 +117,7 @@ pub fn TimelineRepliesRecursive(tl: Timeline, root: String) -> impl IntoView {
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
|
}
|
||||||
/ >
|
/ >
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,8 @@ use apb::{Base, Object};
|
||||||
pub fn ObjectPage(tl: Timeline) -> impl IntoView {
|
pub fn ObjectPage(tl: Timeline) -> impl IntoView {
|
||||||
let params = use_params_map();
|
let params = use_params_map();
|
||||||
let auth = use_context::<Auth>().expect("missing auth context");
|
let auth = use_context::<Auth>().expect("missing auth context");
|
||||||
|
let id = params.get().get("id").cloned().unwrap_or_default();
|
||||||
|
let uid = uriproxy::uri(URL_BASE, uriproxy::UriClass::Object, &id);
|
||||||
let object = create_local_resource(
|
let object = create_local_resource(
|
||||||
move || params.get().get("id").cloned().unwrap_or_default(),
|
move || params.get().get("id").cloned().unwrap_or_default(),
|
||||||
move |oid| async move {
|
move |oid| async move {
|
||||||
|
@ -36,7 +38,7 @@ pub fn ObjectPage(tl: Timeline) -> impl IntoView {
|
||||||
};
|
};
|
||||||
if let Ok(ctx) = obj.context().id() {
|
if let Ok(ctx) = obj.context().id() {
|
||||||
let tl_url = format!("{}/context/page", Uri::api(U::Object, ctx, false));
|
let tl_url = format!("{}/context/page", Uri::api(U::Object, ctx, false));
|
||||||
if !tl.next.get_untracked().starts_with(&tl_url) {
|
if !tl.next.get().starts_with(&tl_url) {
|
||||||
tl.reset(tl_url);
|
tl.reset(tl_url);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -62,8 +64,7 @@ pub fn ObjectPage(tl: Timeline) -> impl IntoView {
|
||||||
{move || match object.get() {
|
{move || match object.get() {
|
||||||
None => view! { <p class="center"> loading ... </p> }.into_view(),
|
None => view! { <p class="center"> loading ... </p> }.into_view(),
|
||||||
Some(None) => {
|
Some(None) => {
|
||||||
let raw_id = params.get().get("id").cloned().unwrap_or_default();
|
let uid = uid.clone();
|
||||||
let uid = uriproxy::uri(URL_BASE, uriproxy::UriClass::Object, &raw_id);
|
|
||||||
view! { <p class="center"><code>loading failed</code><sup><small><a class="clean" href={uid} target="_blank">"↗"</a></small></sup></p> }.into_view()
|
view! { <p class="center"><code>loading failed</code><sup><small><a class="clean" href={uid} target="_blank">"↗"</a></small></sup></p> }.into_view()
|
||||||
},
|
},
|
||||||
Some(Some(o)) => {
|
Some(Some(o)) => {
|
||||||
|
|
|
@ -33,7 +33,7 @@ pub fn UserPage(tl: Timeline) -> impl IntoView {
|
||||||
move |id| {
|
move |id| {
|
||||||
async move {
|
async move {
|
||||||
let tl_url = format!("{}/outbox/page", Uri::api(U::Actor, &id, false));
|
let tl_url = format!("{}/outbox/page", Uri::api(U::Actor, &id, false));
|
||||||
if !tl.next.get_untracked().starts_with(&tl_url) {
|
if !tl.next.get().starts_with(&tl_url) {
|
||||||
tl.reset(tl_url);
|
tl.reset(tl_url);
|
||||||
}
|
}
|
||||||
match CACHE.get(&Uri::full(U::Actor, &id)) {
|
match CACHE.get(&Uri::full(U::Actor, &id)) {
|
||||||
|
|
Loading…
Reference in a new issue