1
0
Fork 0
mirror of https://github.com/Nutomic/ibis.git synced 2024-11-22 09:41:10 +00:00

Rework how site data is fetched

This commit is contained in:
Felix Ableitner 2024-11-14 12:33:25 +01:00
parent 66f89d3bfd
commit 61806e12a6
19 changed files with 180 additions and 192 deletions

View file

@ -68,7 +68,7 @@ pub(in crate::backend::api) async fn create_article(
instance_id: local_instance.id,
local: true,
protected: false,
approved: !data.config.article_approval,
approved: !data.config.config.article_approval,
};
let article = DbArticle::create(form, &data)?;
@ -214,7 +214,7 @@ pub(in crate::backend::api) async fn fork_article(
instance_id: local_instance.id,
local: true,
protected: false,
approved: !data.config.article_approval,
approved: !data.config.config.article_approval,
};
let article = DbArticle::create(form, &data)?;

View file

@ -12,12 +12,12 @@ use crate::{
search_article,
},
instance::{follow_instance, get_instance, resolve_instance},
user::{get_user, login_user, logout_user, my_profile, register_user, validate},
user::{get_user, login_user, logout_user, register_user, validate},
},
database::IbisData,
error::MyResult,
},
common::{LocalUserView, AUTH_COOKIE},
common::{LocalUserView, SiteView, AUTH_COOKIE},
};
use activitypub_federation::config::Data;
use anyhow::anyhow;
@ -28,9 +28,12 @@ use axum::{
middleware::{self, Next},
response::Response,
routing::{get, post},
Extension,
Json,
Router,
};
use axum_extra::extract::CookieJar;
use axum_macros::debug_handler;
use instance::list_remote_instances;
use user::{count_notifications, list_notifications};
@ -59,8 +62,8 @@ pub fn api_routes() -> Router<()> {
.route("/user/notifications/count", get(count_notifications))
.route("/account/register", post(register_user))
.route("/account/login", post(login_user))
.route("/account/my_profile", get(my_profile))
.route("/account/logout", get(logout_user))
.route("/site", get(site_view))
.route_layer(middleware::from_fn(auth))
}
@ -73,8 +76,7 @@ async fn auth(
let auth = request
.headers()
.get(AUTH_COOKIE)
.map(|h| h.to_str().ok())
.flatten()
.and_then(|h| h.to_str().ok())
.or(jar.get(AUTH_COOKIE).map(|c| c.value()));
if let Some(auth) = auth {
@ -92,3 +94,14 @@ fn check_is_admin(user: &LocalUserView) -> MyResult<()> {
}
Ok(())
}
#[debug_handler]
pub(in crate::backend::api) async fn site_view(
data: Data<IbisData>,
user: Option<Extension<LocalUserView>>,
) -> MyResult<Json<SiteView>> {
Ok(Json(SiteView {
my_profile: user.map(|u| u.0),
config: data.config.config.clone(),
}))
}

View file

@ -76,7 +76,7 @@ pub(in crate::backend::api) async fn register_user(
jar: CookieJar,
Form(form): Form<RegisterUserForm>,
) -> MyResult<(CookieJar, Json<LocalUserView>)> {
if !data.config.registration_open {
if !data.config.config.registration_open {
return Err(anyhow!("Registration is closed").into());
}
let user = DbPerson::create_local(form.username, form.password, false, &data)?;
@ -121,19 +121,6 @@ fn create_cookie(jwt: String, data: &Data<IbisData>) -> Cookie<'static> {
.build()
}
#[debug_handler]
pub(in crate::backend::api) async fn my_profile(
data: Data<IbisData>,
jar: CookieJar,
) -> MyResult<Json<LocalUserView>> {
let jwt = jar.get(AUTH_COOKIE).map(|c| c.value());
if let Some(jwt) = jwt {
Ok(Json(validate(jwt, &data).await?))
} else {
Err(anyhow!("invalid/missing auth").into())
}
}
#[debug_handler]
pub(in crate::backend::api) async fn logout_user(
data: Data<IbisData>,

View file

@ -1,4 +1,4 @@
use crate::backend::error::MyResult;
use crate::{backend::error::MyResult, common::SharedConfig};
use config::Config;
use doku::Document;
use serde::Deserialize;
@ -10,17 +10,10 @@ use smart_default::SmartDefault;
pub struct IbisConfig {
/// Details about the PostgreSQL database connection
pub database: IbisConfigDatabase,
/// Whether users can create new accounts
#[default = true]
#[doku(example = "true")]
pub registration_open: bool,
/// Whether admins need to approve new articles
#[default = false]
#[doku(example = "false")]
pub article_approval: bool,
/// Details of the initial admin account
pub setup: IbisConfigSetup,
pub federation: IbisConfigFederation,
pub config: SharedConfig,
}
impl IbisConfig {

View file

@ -86,7 +86,6 @@ impl DbInstance {
Ok(InstanceView {
instance,
followers,
registration_open: data.config.registration_open,
})
}

View file

@ -136,7 +136,7 @@ async fn leptos_routes_handler(
let cookie = jar.get(AUTH_COOKIE).map(|c| c.value().to_string());
provide_context(Auth(cookie));
},
move || view! { <App/> },
move || view! { <App /> },
);
handler(req).await.into_response()

View file

@ -34,7 +34,7 @@ async fn node_info(data: Data<IbisData>) -> MyResult<Json<NodeInfo>> {
version: env!("CARGO_PKG_VERSION").to_string(),
},
protocols: vec!["activitypub".to_string()],
open_registrations: data.config.registration_open,
open_registrations: data.config.config.registration_open,
}))
}

View file

@ -6,6 +6,7 @@ use chrono::{DateTime, Utc};
use newtypes::{ArticleId, ConflictId, EditId, InstanceId, PersonId};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use smart_default::SmartDefault;
use url::Url;
use uuid::Uuid;
#[cfg(feature = "ssr")]
@ -17,6 +18,7 @@ use {
},
activitypub_federation::fetch::{collection_id::CollectionId, object_id::ObjectId},
diesel::{Identifiable, Queryable, Selectable},
doku::Document,
};
pub const MAIN_PAGE_NAME: &str = "Main_Page";
@ -311,7 +313,6 @@ impl DbInstance {
pub struct InstanceView {
pub instance: DbInstance,
pub followers: Vec<DbPerson>,
pub registration_open: bool,
}
#[derive(Deserialize, Serialize, Clone, Debug)]
@ -320,6 +321,30 @@ pub struct GetUserForm {
pub domain: Option<String>,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, SmartDefault)]
#[serde(default)]
#[serde(deny_unknown_fields)]
#[cfg_attr(feature = "ssr", derive(Queryable, Document))]
#[cfg_attr(feature = "ssr", diesel(check_for_backend(diesel::pg::Pg)))]
pub struct SharedConfig {
/// Whether users can create new accounts
#[default = true]
#[cfg_attr(feature = "ssr", doku(example = "true"))]
pub registration_open: bool,
/// Whether admins need to approve new articles
#[default = false]
#[cfg_attr(feature = "ssr", doku(example = "false"))]
pub article_approval: bool,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Default)]
#[cfg_attr(feature = "ssr", derive(Queryable))]
#[cfg_attr(feature = "ssr", diesel(check_for_backend(diesel::pg::Pg)))]
pub struct SiteView {
pub my_profile: Option<LocalUserView>,
pub config: SharedConfig,
}
#[test]
fn test_edit_versions() {
let default = EditVersion::default();

View file

@ -24,6 +24,7 @@ use crate::{
RegisterUserForm,
ResolveObject,
SearchArticleForm,
SiteView,
},
frontend::error::MyResult,
};
@ -210,10 +211,8 @@ impl ApiClient {
}
}
pub async fn my_profile(&self) -> MyResult<LocalUserView> {
let req = self
.client
.get(self.request_endpoint("/api/v1/account/my_profile"));
pub async fn site(&self) -> MyResult<SiteView> {
let req = self.client.get(self.request_endpoint("/api/v1/site"));
handle_json_res(req).await
}

View file

@ -1,5 +1,5 @@
use crate::{
common::LocalUserView,
common::SiteView,
frontend::{
api::CLIENT,
components::nav::Nav,
@ -23,60 +23,51 @@ use crate::{
},
},
};
use leptos::{
component,
create_local_resource,
create_rw_signal,
expect_context,
provide_context,
use_context,
view,
DynAttrs,
IntoView,
RwSignal,
SignalGet,
SignalUpdate,
};
use leptos::*;
use leptos_meta::{provide_meta_context, *};
use leptos_router::{Route, Router, Routes};
// https://book.leptos.dev/15_global_state.html
#[derive(Clone)]
pub struct GlobalState {
pub(crate) my_profile: Option<LocalUserView>,
pub fn site() -> Resource<(), SiteView> {
use_context::<Resource<(), SiteView>>().unwrap()
}
impl GlobalState {
pub fn update_my_profile() {
create_local_resource(
move || (),
|_| async move {
let my_profile = CLIENT.my_profile().await.ok();
expect_context::<RwSignal<GlobalState>>()
.update(|state| state.my_profile = my_profile.clone());
},
);
}
pub fn is_logged_in() -> bool {
site().with_default(|site| site.my_profile.is_some())
}
pub fn is_admin() -> bool {
site().with_default(|site| {
site.my_profile
.as_ref()
.map(|p| p.local_user.admin)
.unwrap_or(false)
})
}
pub fn is_admin() -> fn() -> bool {
move || {
use_context::<RwSignal<GlobalState>>()
.expect("global state is provided")
.get()
.my_profile
.map(|p| p.local_user.admin)
.unwrap_or(false)
}
pub trait DefaultResource<T> {
fn with_default<O>(&self, f: impl FnOnce(&T) -> O) -> O;
}
impl<T: Default> DefaultResource<T> for Resource<(), T> {
fn with_default<O>(&self, f: impl FnOnce(&T) -> O) -> O {
self.with(|x| match x {
Some(x) => f(x),
None => f(&T::default()),
})
}
}
#[component]
pub fn App() -> impl IntoView {
// TODO: should create_resource() but then things break
let site_resource = create_local_resource(
move || (),
|_| async move {
let site = CLIENT.site().await.unwrap();
site
},
);
provide_context(site_resource);
provide_meta_context();
let global_state = GlobalState { my_profile: None };
// Load user profile in case we are already logged in
GlobalState::update_my_profile();
provide_context(create_rw_signal(global_state));
let darkmode = DarkMode::init();
provide_context(darkmode.clone());

View file

@ -2,7 +2,7 @@ use crate::{
common::{validation::can_edit_article, ArticleView, GetInstance},
frontend::{
api::CLIENT,
app::GlobalState,
app::{is_admin, is_logged_in},
article_link,
article_title,
components::instance_follow_button::InstanceFollowButton,
@ -32,7 +32,7 @@ pub fn ArticleNav(
.get()
.map(|article_| {
let title = article_title(&article_.article);
let instance = create_local_resource(
let instance = create_resource(
move || article_.article.instance_id,
move |instance_id| async move {
let form = GetInstance {
@ -41,7 +41,6 @@ pub fn ArticleNav(
CLIENT.get_instance(&form).await.unwrap()
},
);
let global_state = use_context::<RwSignal<GlobalState>>().unwrap();
let article_link = article_link(&article_.article);
let article_link_ = article_link.clone();
let protected = article_.article.protected;
@ -54,24 +53,14 @@ pub fn ArticleNav(
"History"
</A>
<Show when=move || {
global_state
.with(|state| {
let is_admin = state
.my_profile
.as_ref()
.map(|p| p.local_user.admin)
.unwrap_or(false);
state.my_profile.is_some()
&& can_edit_article(&article_.article, is_admin).is_ok()
})
is_logged_in()
&& can_edit_article(&article_.article, is_admin()).is_ok()
}>
<A class=tab_classes.edit href=format!("{article_link}/edit")>
"Edit"
</A>
</Show>
<Show when=move || {
global_state.with(|state| state.my_profile.is_some())
}>
<Show when=is_logged_in>
<A
class=tab_classes.actions
href=format!("{article_link_}/actions")

View file

@ -1,24 +1,28 @@
use crate::{
common::{newtypes::InstanceId, DbInstance, FollowInstance},
frontend::{api::CLIENT, app::GlobalState},
frontend::{
api::CLIENT,
app::{site, DefaultResource},
},
};
use leptos::{component, *};
#[component]
pub fn InstanceFollowButton(instance: DbInstance) -> impl IntoView {
let global_state = use_context::<RwSignal<GlobalState>>().unwrap();
let follow_action = create_action(move |instance_id: &InstanceId| {
let instance_id = *instance_id;
async move {
let form = FollowInstance { id: instance_id };
CLIENT.follow_instance(form).await.unwrap();
GlobalState::update_my_profile();
site().refetch();
}
});
let is_following = global_state
.get_untracked()
.my_profile
.map(|p| p.following.contains(&instance))
let is_following = site()
.with_default(|site| {
site.clone()
.my_profile
.map(|p| p.following.contains(&instance))
})
.unwrap_or(false);
let follow_text = if is_following {
"Following instance"

View file

@ -1,24 +1,17 @@
use crate::frontend::{api::CLIENT, app::GlobalState, dark_mode::DarkMode};
use leptos::{component, use_context, view, IntoView, RwSignal, SignalWith, *};
use crate::frontend::{
api::CLIENT,
app::{is_logged_in, site, DefaultResource},
dark_mode::DarkMode,
};
use leptos::{component, view, IntoView, *};
use leptos_router::*;
#[component]
pub fn Nav() -> impl IntoView {
let global_state = use_context::<RwSignal<GlobalState>>().unwrap();
let logout_action = create_action(move |_| async move {
CLIENT.logout().await.unwrap();
GlobalState::update_my_profile();
site().refetch();
});
let registration_open = create_local_resource(
|| (),
move |_| async move {
CLIENT
.get_local_instance()
.await
.map(|i| i.registration_open)
.unwrap_or_default()
},
);
let notification_count = create_resource(
|| (),
move |_| async move { CLIENT.notifications_count().await.unwrap_or_default() },
@ -56,19 +49,21 @@ pub fn Nav() -> impl IntoView {
<li>
<A href="/article/list">"Articles"</A>
</li>
<Show when=move || global_state.with(|state| state.my_profile.is_some())>
<li>
<A href="/article/create">"Create Article"</A>
</li>
<li>
<A href="/notifications">
"Notifications "
<span class="indicator-item indicator-end badge badge-neutral">
{notification_count}
</span>
</A>
</li>
</Show>
<Transition>
<Show when=is_logged_in>
<li>
<A href="/article/create">"Create Article"</A>
</li>
<li>
<A href="/notifications">
"Notifications "
<span class="indicator-item indicator-end badge badge-neutral">
{move || notification_count.get()}
</span>
</A>
</li>
</Show>
</Transition>
<li>
<form
class="form-control m-0 p-1"
@ -96,42 +91,46 @@ pub fn Nav() -> impl IntoView {
</form>
</li>
<div class="divider"></div>
<Show
when=move || global_state.with(|state| state.my_profile.is_some())
fallback=move || {
view! {
<li>
<A href="/login">"Login"</A>
</li>
<Show when=move || registration_open.get().unwrap_or_default()>
<Transition>
<Show
when=is_logged_in
fallback=move || {
view! {
<li>
<A href="/register">"Register"</A>
<A href="/login">"Login"</A>
</li>
</Show>
<Show when=move || {
site().with_default(|s| s.config.registration_open)
}>
<li>
<A href="/register">"Register"</A>
</li>
</Show>
}
}
}
>
>
{
let my_profile = global_state
.with(|state| state.my_profile.clone().unwrap());
let profile_link = format!("/user/{}", my_profile.person.username);
view! {
<p class="self-center pb-2">
"Logged in as " <a class="link" href=profile_link>
{my_profile.person.username}
</a>
</p>
<button
class="btn btn-outline btn-xs w-min self-center"
on:click=move |_| logout_action.dispatch(())
>
Logout
</button>
{
let my_profile = site()
.with_default(|site| site.clone().my_profile.unwrap());
let profile_link = format!("/user/{}", my_profile.person.username);
view! {
<p class="self-center pb-2">
"Logged in as " <a class="link" href=profile_link>
{my_profile.person.username}
</a>
</p>
<button
class="btn btn-outline btn-xs w-min self-center"
on:click=move |_| logout_action.dispatch(())
>
Logout
</button>
}
}
}
</Show>
</Show>
</Transition>
<div class="flex-grow min-h-2"></div>
<div class="m-1 grid gap-2">
<label class="flex cursor-pointer gap-2">

View file

@ -2,7 +2,7 @@ use crate::{
common::{newtypes::ArticleId, ForkArticleForm, ProtectArticleForm},
frontend::{
api::CLIENT,
app::GlobalState,
app::is_admin,
article_link,
components::article_nav::{ActiveTab, ArticleNav},
pages::article_resource,
@ -14,7 +14,6 @@ use leptos_router::Redirect;
#[component]
pub fn ArticleActions() -> impl IntoView {
let global_state = use_context::<RwSignal<GlobalState>>().unwrap();
let article = article_resource();
let (new_title, set_new_title) = create_signal(String::new());
let (fork_response, set_fork_response) = create_signal(Option::<DbArticle>::None);
@ -68,17 +67,7 @@ pub fn ArticleActions() -> impl IntoView {
.map(|err| {
view! { <p class="alert">{err}</p> }
})
}}
<Show when=move || {
global_state
.with(|state| {
state
.my_profile
.as_ref()
.map(|p| p.local_user.admin)
.unwrap_or_default() && article.article.local
})
}>
}} <Show when=move || { is_admin() && article.article.local }>
<button
class="btn btn-secondary"
on:click=move |_| {

View file

@ -1,6 +1,6 @@
use crate::{
common::LoginUserForm,
frontend::{api::CLIENT, app::GlobalState, components::credentials::*},
frontend::{api::CLIENT, app::site, components::credentials::*},
};
use leptos::*;
use leptos_router::Redirect;
@ -20,9 +20,8 @@ pub fn Login() -> impl IntoView {
let result = CLIENT.login(credentials).await;
set_wait_for_response.update(|w| *w = false);
match result {
Ok(res) => {
expect_context::<RwSignal<GlobalState>>()
.update(|state| state.my_profile = Some(res));
Ok(_res) => {
site().refetch();
set_login_response.update(|v| *v = Some(()));
set_login_error.update(|e| *e = None);
}

View file

@ -6,7 +6,7 @@ use leptos::*;
#[component]
pub fn Notifications() -> impl IntoView {
let notifications = create_local_resource(
let notifications = create_resource(
move || {},
|_| async move { CLIENT.notifications_list().await.unwrap() },
);

View file

@ -1,6 +1,6 @@
use crate::{
common::{LocalUserView, RegisterUserForm},
frontend::{api::CLIENT, app::GlobalState, components::credentials::*, error::MyResult},
frontend::{api::CLIENT, app::site, components::credentials::*, error::MyResult},
};
use leptos::{logging::log, *};
@ -20,9 +20,8 @@ pub fn Register() -> impl IntoView {
let result: MyResult<LocalUserView> = CLIENT.register(credentials).await;
set_wait_for_response.update(|w| *w = false);
match result {
Ok(res) => {
expect_context::<RwSignal<GlobalState>>()
.update(|state| state.my_profile = Some(res));
Ok(_res) => {
site().refetch();
set_register_response.update(|v| *v = Some(()));
set_register_error.update(|e| *e = None);
}

View file

@ -5,7 +5,7 @@ use ibis::{
config::{IbisConfig, IbisConfigDatabase, IbisConfigFederation},
start,
},
common::RegisterUserForm,
common::{RegisterUserForm, SharedConfig},
frontend::{api::ApiClient, error::MyResult},
};
use reqwest::ClientBuilder;
@ -124,12 +124,14 @@ impl IbisInstance {
connection_url,
..Default::default()
},
registration_open: true,
federation: IbisConfigFederation {
domain: domain.clone(),
..Default::default()
},
article_approval,
config: SharedConfig {
registration_open: true,
article_approval,
},
..Default::default()
};
let client = ClientBuilder::new().cookie_store(true).build().unwrap();

View file

@ -112,7 +112,7 @@ async fn test_follow_instance() -> MyResult<()> {
let data = TestData::start(false).await;
// check initial state
let alpha_user = data.alpha.my_profile().await?;
let alpha_user = data.alpha.site().await?.my_profile.unwrap();
assert_eq!(0, alpha_user.following.len());
let beta_instance = data.beta.get_local_instance().await?;
assert_eq!(0, beta_instance.followers.len());
@ -122,7 +122,7 @@ async fn test_follow_instance() -> MyResult<()> {
.await?;
// check that follow was federated
let alpha_user = data.alpha.my_profile().await?;
let alpha_user = data.alpha.site().await?.my_profile.unwrap();
assert_eq!(1, alpha_user.following.len());
assert_eq!(beta_instance.instance.ap_id, alpha_user.following[0].ap_id);
@ -610,13 +610,13 @@ async fn test_user_registration_login() -> MyResult<()> {
};
data.alpha.login(login_data).await?;
let my_profile = data.alpha.my_profile().await?;
let my_profile = data.alpha.site().await?.my_profile.unwrap();
assert_eq!(username, my_profile.person.username);
data.alpha.logout().await?;
let my_profile_after_logout = data.alpha.my_profile().await;
assert!(my_profile_after_logout.is_err());
let my_profile_after_logout = data.alpha.site().await?.my_profile;
assert!(my_profile_after_logout.is_none());
data.stop()
}
@ -635,7 +635,7 @@ async fn test_user_profile() -> MyResult<()> {
data.beta
.resolve_article(create_res.article.ap_id.into_inner())
.await?;
let domain = extract_domain(&data.alpha.my_profile().await?.person.ap_id);
let domain = extract_domain(&data.alpha.site().await?.my_profile.unwrap().person.ap_id);
// Now we can fetch the remote user from local api
let params = GetUserForm {