Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Be more explicit about returning deleted actors or not #2335

Merged
merged 2 commits into from Jul 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/apub/src/fetcher/mod.rs
Expand Up @@ -58,7 +58,7 @@ where
let identifier = identifier.to_string();
Ok(
blocking(context.pool(), move |conn| {
DbActor::read_from_name(conn, &identifier)
DbActor::read_from_name(conn, &identifier, false)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Search should probably not list deleted users or communities.

})
.await??,
)
Expand Down
8 changes: 4 additions & 4 deletions crates/apub/src/http/community.rs
Expand Up @@ -35,7 +35,7 @@ pub(crate) async fn get_apub_community_http(
context: web::Data<LemmyContext>,
) -> Result<HttpResponse, LemmyError> {
let community: ApubCommunity = blocking(context.pool(), move |conn| {
Community::read_from_name(conn, &info.community_name)
Community::read_from_name(conn, &info.community_name, true)
})
.await??
.into();
Expand Down Expand Up @@ -66,7 +66,7 @@ pub(crate) async fn get_apub_community_followers(
context: web::Data<LemmyContext>,
) -> Result<HttpResponse, LemmyError> {
let community = blocking(context.pool(), move |conn| {
Community::read_from_name(conn, &info.community_name)
Community::read_from_name(conn, &info.community_name, false)
})
.await??;
let followers = GroupFollowers::new(community, &context).await?;
Expand All @@ -80,7 +80,7 @@ pub(crate) async fn get_apub_community_outbox(
context: web::Data<LemmyContext>,
) -> Result<HttpResponse, LemmyError> {
let community = blocking(context.pool(), move |conn| {
Community::read_from_name(conn, &info.community_name)
Community::read_from_name(conn, &info.community_name, false)
})
.await??;
let id = ObjectId::new(generate_outbox_url(&community.actor_id)?);
Expand All @@ -97,7 +97,7 @@ pub(crate) async fn get_apub_community_moderators(
context: web::Data<LemmyContext>,
) -> Result<HttpResponse, LemmyError> {
let community: ApubCommunity = blocking(context.pool(), move |conn| {
Community::read_from_name(conn, &info.community_name)
Community::read_from_name(conn, &info.community_name, false)
})
.await??
.into();
Expand Down
4 changes: 2 additions & 2 deletions crates/apub/src/http/person.rs
Expand Up @@ -28,7 +28,7 @@ pub(crate) async fn get_apub_person_http(
let user_name = info.into_inner().user_name;
// TODO: this needs to be able to read deleted persons, so that it can send tombstones
let person: ApubPerson = blocking(context.pool(), move |conn| {
Person::read_from_name(conn, &user_name)
Person::read_from_name(conn, &user_name, true)
})
.await??
.into();
Expand Down Expand Up @@ -60,7 +60,7 @@ pub(crate) async fn get_apub_person_outbox(
context: web::Data<LemmyContext>,
) -> Result<HttpResponse, LemmyError> {
let person = blocking(context.pool(), move |conn| {
Person::read_from_name(conn, &info.user_name)
Person::read_from_name(conn, &info.user_name, false)
})
.await??;
let outbox_id = generate_outbox_url(&person.actor_id)?.into();
Expand Down
16 changes: 12 additions & 4 deletions crates/db_schema/src/impls/community.rs
Expand Up @@ -332,12 +332,20 @@ impl ApubActor for Community {
)
}

fn read_from_name(conn: &PgConnection, community_name: &str) -> Result<Community, Error> {
fn read_from_name(
conn: &PgConnection,
community_name: &str,
include_deleted: bool,
) -> Result<Community, Error> {
use crate::schema::community::dsl::*;
community
let mut q = community
.into_boxed()
.filter(local.eq(true))
.filter(lower(name).eq(lower(community_name)))
.first::<Self>(conn)
.filter(lower(name).eq(lower(community_name)));
if !include_deleted {
q = q.filter(deleted.eq(false)).filter(removed.eq(false));
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a little awkward, not sure if theres a better way to write the query.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do .into_boxed()

Then you can do:

`if .... {
q = q.filter(...)
;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like this? Gives me error "overflow evaluating the requirement _: Sized".


    let mut q = community
      .filter(local.eq(true))
      .filter(lower(name).eq(lower(community_name))).into_boxed();
    if !include_deleted {
      q = q.filter(deleted.eq(false))
        .filter(removed.eq(false)).into_boxed();
    }
    q.first::<Self>(conn)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You do the into_boxed before filters I think.

q.first::<Self>(conn)
}

fn read_from_name_and_domain(
Expand Down
17 changes: 12 additions & 5 deletions crates/db_schema/src/impls/person.rs
Expand Up @@ -305,12 +305,19 @@ impl ApubActor for Person {
)
}

fn read_from_name(conn: &PgConnection, from_name: &str) -> Result<Person, Error> {
person
.filter(deleted.eq(false))
fn read_from_name(
conn: &PgConnection,
from_name: &str,
include_deleted: bool,
) -> Result<Person, Error> {
let mut q = person
.into_boxed()
.filter(local.eq(true))
.filter(lower(name).eq(lower(from_name)))
.first::<Person>(conn)
.filter(lower(name).eq(lower(from_name)));
if !include_deleted {
q = q.filter(deleted.eq(false))
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for this one here.

q.first::<Self>(conn)
}

fn read_from_name_and_domain(
Expand Down
8 changes: 7 additions & 1 deletion crates/db_schema/src/traits.rs
Expand Up @@ -168,7 +168,13 @@ pub trait ApubActor {
fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result<Option<Self>, Error>
where
Self: Sized;
fn read_from_name(conn: &PgConnection, actor_name: &str) -> Result<Self, Error>
/// - actor_name is the name of the community or user to read.
/// - include_deleted, if true, will return communities or users that were deleted/removed
fn read_from_name(
conn: &PgConnection,
actor_name: &str,
include_deleted: bool,
) -> Result<Self, Error>
where
Self: Sized;
fn read_from_name_and_domain(
Expand Down
4 changes: 2 additions & 2 deletions crates/routes/src/feeds.rs
Expand Up @@ -181,7 +181,7 @@ fn get_feed_user(
protocol_and_hostname: &str,
) -> Result<ChannelBuilder, LemmyError> {
let site_view = SiteView::read_local(conn)?;
let person = Person::read_from_name(conn, user_name)?;
let person = Person::read_from_name(conn, user_name, false)?;

let posts = PostQueryBuilder::create(conn)
.listing_type(ListingType::All)
Expand Down Expand Up @@ -210,7 +210,7 @@ fn get_feed_community(
protocol_and_hostname: &str,
) -> Result<ChannelBuilder, LemmyError> {
let site_view = SiteView::read_local(conn)?;
let community = Community::read_from_name(conn, community_name)?;
let community = Community::read_from_name(conn, community_name, false)?;

let posts = PostQueryBuilder::create(conn)
.listing_type(ListingType::Community)
Expand Down
4 changes: 2 additions & 2 deletions crates/routes/src/webfinger.rs
Expand Up @@ -46,13 +46,13 @@ async fn get_webfinger_response(

let name_ = name.clone();
let user_id: Option<Url> = blocking(context.pool(), move |conn| {
Person::read_from_name(conn, &name_)
Person::read_from_name(conn, &name_, false)
})
.await?
.ok()
.map(|c| c.actor_id.into());
let community_id: Option<Url> = blocking(context.pool(), move |conn| {
Community::read_from_name(conn, &name)
Community::read_from_name(conn, &name, false)
})
.await?
.ok()
Expand Down