1
0
Fork 0
forked from alemi/upub

feat: show in user queries if follow/by self

This commit is contained in:
əlemi 2024-05-01 17:47:37 +02:00
parent 75d03195d9
commit 4527ff884e
Signed by: alemi
GPG key ID: A4895B84D311642C

View file

@ -5,9 +5,9 @@ pub mod outbox;
pub mod following; pub mod following;
use axum::extract::{Path, Query, State}; use axum::extract::{Path, Query, State};
use sea_orm::EntityTrait; use sea_orm::{ColumnTrait, EntityTrait, QueryFilter, SelectColumns};
use apb::{ActorMut, Node, ObjectMut}; use apb::{ActorMut, CollectionMut, Node, Object, ObjectMut};
use crate::{errors::UpubError, model::{self, user}, server::{auth::AuthIdentity, fetcher::Fetcher, Context}, url}; use crate::{errors::UpubError, model::{self, user}, server::{auth::AuthIdentity, fetcher::Fetcher, Context}, url};
use super::{jsonld::LD, JsonLD, TryFetch}; use super::{jsonld::LD, JsonLD, TryFetch};
@ -32,18 +32,48 @@ pub async fn view(
.one(ctx.db()).await? .one(ctx.db()).await?
{ {
// local user // local user
Some((user, Some(cfg))) => { Some((user_model, Some(cfg))) => {
let mut user = user.ap() let mut user = user_model.ap()
.set_inbox(Node::link(url!(ctx, "/users/{id}/inbox"))) .set_inbox(Node::link(url!(ctx, "/users/{id}/inbox")))
.set_outbox(Node::link(url!(ctx, "/users/{id}/outbox"))) .set_outbox(Node::link(url!(ctx, "/users/{id}/outbox")))
.set_following(Node::link(url!(ctx, "/users/{id}/following"))) .set_following(Node::link(url!(ctx, "/users/{id}/following")))
.set_followers(Node::link(url!(ctx, "/users/{id}/followers"))); .set_followers(Node::link(url!(ctx, "/users/{id}/followers")));
if !cfg.show_followers_count { // TODO maybe this thing could be made as a single join, to avoid triple db roundtrip for
// each fetch made by local users? it's indexed and fast but still...
if let Some(my_id) = auth.my_id() {
if !auth.is(&uid) {
let followed_by_me = model::relation::Entity::find()
.filter(model::relation::Column::Follower.eq(my_id))
.filter(model::relation::Column::Following.eq(&uid))
.select_column(model::relation::Column::Follower)
.into_tuple::<String>()
.all(ctx.db())
.await?;
user
.audience()
.update(|x| x.set_ordered_items(apb::Node::links(followed_by_me)));
let following_me = model::relation::Entity::find()
.filter(model::relation::Column::Following.eq(my_id))
.filter(model::relation::Column::Follower.eq(&uid))
.select_column(model::relation::Column::Following)
.into_tuple::<String>()
.all(ctx.db())
.await?;
user
.generator()
.update(|x| x.set_ordered_items(apb::Node::links(following_me)));
}
}
if !auth.is(&uid) && !cfg.show_followers_count {
user = user.set_audience(apb::Node::Empty); user = user.set_audience(apb::Node::Empty);
} }
if !cfg.show_following_count { if !auth.is(&uid) && !cfg.show_following_count {
user = user.set_generator(apb::Node::Empty); user = user.set_generator(apb::Node::Empty);
} }