Compare commits

...

6 commits

Author SHA1 Message Date
Dessalines ae01d2ecdc
Merge 9807b02efd into d3737d4453 2024-04-27 23:07:25 +08:00
Dessalines 9807b02efd Dont pass ref and clone. 2024-04-27 11:05:26 -04:00
Dessalines 95ed87e87e Merge remote-tracking branch 'origin/main' into fix_community_outbox 2024-04-27 11:01:02 -04:00
dullbananas d3737d4453
Optimize actor_language.rs (#4612)
* Remove useless transaction in actor_language.rs

* Update actor_language.rs

* site

* community

* Update actor_language.rs

* Update actor_language.rs

* Update actor_language.rs

* Update actor_language.rs

* Update actor_language.rs
2024-04-27 10:59:58 -04:00
Dessalines 6b4b56b0eb Fixing tests. 2024-04-25 14:10:15 -04:00
Dessalines 54cd77d72c Fix communities with broken outboxes, and use PostView. Fixes #4658 2024-04-25 12:36:41 -04:00
5 changed files with 118 additions and 90 deletions

View file

@ -66,7 +66,6 @@ impl CreateOrUpdatePage {
kind: CreateOrUpdateType,
context: Data<LemmyContext>,
) -> LemmyResult<()> {
let post = ApubPost(post);
let community_id = post.community_id;
let person: ApubPerson = Person::read(&mut context.pool(), person_id)
.await?
@ -78,7 +77,7 @@ impl CreateOrUpdatePage {
.into();
let create_or_update =
CreateOrUpdatePage::new(post, &person, &community, kind, &context).await?;
CreateOrUpdatePage::new(post.into(), &person, &community, kind, &context).await?;
let is_mod_action = create_or_update.object.is_mod_action(&context).await?;
let activity = AnnouncableActivities::CreateOrUpdatePost(create_or_update);
send_activity_in_community(

View file

@ -1,6 +1,6 @@
use crate::{
activity_lists::AnnouncableActivities,
objects::{community::ApubCommunity, post::ApubPost},
objects::community::ApubCommunity,
protocol::{
activities::{
community::announce::AnnounceActivity,
@ -18,11 +18,8 @@ use activitypub_federation::{
};
use futures::future::join_all;
use lemmy_api_common::{context::LemmyContext, utils::generate_outbox_url};
use lemmy_db_schema::{
source::{person::Person, post::Post},
traits::Crud,
utils::FETCH_LIMIT_MAX,
};
use lemmy_db_schema::{utils::FETCH_LIMIT_MAX, SortType};
use lemmy_db_views::{post_view::PostQuery, structs::SiteView};
use lemmy_utils::{
error::{LemmyError, LemmyResult},
LemmyErrorType,
@ -41,19 +38,30 @@ impl Collection for ApubCommunityOutbox {
#[tracing::instrument(skip_all)]
async fn read_local(owner: &Self::Owner, data: &Data<Self::DataType>) -> LemmyResult<Self::Kind> {
let post_list: Vec<ApubPost> = Post::list_for_community(&mut data.pool(), owner.id)
let site = SiteView::read_local(&mut data.pool())
.await?
.into_iter()
.map(Into::into)
.collect();
.ok_or(LemmyErrorType::LocalSiteNotSetup)?
.site;
let post_views = PostQuery {
community_id: Some(owner.id),
sort: Some(SortType::New),
limit: Some(FETCH_LIMIT_MAX),
..Default::default()
}
.list(&site, &mut data.pool())
.await?;
let mut ordered_items = vec![];
for post in post_list {
let person = Person::read(&mut data.pool(), post.creator_id)
.await?
.ok_or(LemmyErrorType::CouldntFindPerson)?
.into();
let create =
CreateOrUpdatePage::new(post, &person, owner, CreateOrUpdateType::Create, data).await?;
for post_view in post_views {
let create = CreateOrUpdatePage::new(
post_view.post.into(),
&post_view.creator.into(),
owner,
CreateOrUpdateType::Create,
data,
)
.await?;
let announcable = AnnouncableActivities::CreateOrUpdatePost(create);
let announce = AnnounceActivity::new(announcable.try_into()?, owner, data)?;
ordered_items.push(announce);

View file

@ -128,7 +128,14 @@ pub(crate) mod tests {
use crate::protocol::objects::{group::Group, tombstone::Tombstone};
use actix_web::body::to_bytes;
use lemmy_db_schema::{
source::{community::CommunityInsertForm, instance::Instance},
newtypes::InstanceId,
source::{
community::CommunityInsertForm,
instance::Instance,
local_site::{LocalSite, LocalSiteInsertForm},
local_site_rate_limit::{LocalSiteRateLimit, LocalSiteRateLimitInsertForm},
site::{Site, SiteInsertForm},
},
traits::Crud,
CommunityVisibility,
};
@ -142,6 +149,8 @@ pub(crate) mod tests {
) -> LemmyResult<(Instance, Community)> {
let instance =
Instance::read_or_create(&mut context.pool(), "my_domain.tld".to_string()).await?;
create_local_site(context, instance.id).await?;
let community_form = CommunityInsertForm::builder()
.name("testcom6".to_string())
.title("nada".to_owned())
@ -154,6 +163,28 @@ pub(crate) mod tests {
Ok((instance, community))
}
/// Necessary for the community outbox fetching
async fn create_local_site(
context: &Data<LemmyContext>,
instance_id: InstanceId,
) -> LemmyResult<()> {
// Create a local site, since this is necessary for community fetching.
let site_form = SiteInsertForm::builder()
.name("test site".to_string())
.instance_id(instance_id)
.build();
let site = Site::create(&mut context.pool(), &site_form).await?;
let local_site_form = LocalSiteInsertForm::builder().site_id(site.id).build();
let local_site = LocalSite::create(&mut context.pool(), &local_site_form).await?;
let local_site_rate_limit_form = LocalSiteRateLimitInsertForm::builder()
.local_site_id(local_site.id)
.build();
LocalSiteRateLimit::create(&mut context.pool(), &local_site_rate_limit_form).await?;
Ok(())
}
async fn decode_response<T: DeserializeOwned>(res: HttpResponse) -> LemmyResult<T> {
let body = to_bytes(res.into_body()).await.unwrap();
let body = std::str::from_utf8(&body)?;
@ -164,6 +195,7 @@ pub(crate) mod tests {
#[serial]
async fn test_get_community() -> LemmyResult<()> {
let context = LemmyContext::init_test_context().await;
let (instance, community) = init(false, CommunityVisibility::Public, &context).await?;
// fetch invalid community
let query = CommunityQuery {
@ -172,8 +204,6 @@ pub(crate) mod tests {
let res = get_apub_community_http(query.into(), context.reset_request_count()).await;
assert!(res.is_err());
let (instance, community) = init(false, CommunityVisibility::Public, &context).await?;
// fetch valid community
let query = CommunityQuery {
community_name: community.name.clone(),

View file

@ -43,20 +43,13 @@ impl LocalUserLanguage {
};
let conn = &mut get_conn(pool).await?;
conn
.build_transaction()
.run(|conn| {
Box::pin(async move {
let langs = local_user_language
.filter(local_user_id.eq(for_local_user_id))
.order(language_id)
.select(language_id)
.get_results(conn)
.await?;
convert_read_languages(conn, langs).await
}) as _
})
.await
let langs = local_user_language
.filter(local_user_id.eq(for_local_user_id))
.order(language_id)
.select(language_id)
.get_results(conn)
.await?;
convert_read_languages(conn, langs).await
}
/// Update the user's languages.
@ -90,24 +83,33 @@ impl LocalUserLanguage {
.build_transaction()
.run(|conn| {
Box::pin(async move {
use crate::schema::local_user_language::dsl::{local_user_id, local_user_language};
// Clear the current user languages
delete(local_user_language.filter(local_user_id.eq(for_local_user_id)))
.execute(conn)
.await?;
use crate::schema::local_user_language::dsl::{
language_id,
local_user_id,
local_user_language,
};
// Delete old languages, not including new languages
let delete_old = delete(local_user_language)
.filter(local_user_id.eq(for_local_user_id))
.filter(language_id.ne_all(&lang_ids))
.execute(conn);
let forms = lang_ids
.into_iter()
.map(|l| LocalUserLanguageForm {
.iter()
.map(|&l| LocalUserLanguageForm {
local_user_id: for_local_user_id,
language_id: l,
})
.collect::<Vec<_>>();
insert_into(local_user_language)
// Insert new languages
let insert_new = insert_into(local_user_language)
.values(forms)
.execute(conn)
.await?;
.on_conflict((language_id, local_user_id))
.do_nothing()
.execute(conn);
tokio::try_join!(delete_old, insert_new)?;
Ok(())
}) as _
})
@ -159,25 +161,30 @@ impl SiteLanguage {
.build_transaction()
.run(|conn| {
Box::pin(async move {
use crate::schema::site_language::dsl::{site_id, site_language};
use crate::schema::site_language::dsl::{language_id, site_id, site_language};
// Clear the current languages
delete(site_language.filter(site_id.eq(for_site_id)))
.execute(conn)
.await?;
// Delete old languages, not including new languages
let delete_old = delete(site_language)
.filter(site_id.eq(for_site_id))
.filter(language_id.ne_all(&lang_ids))
.execute(conn);
let forms = lang_ids
.into_iter()
.map(|l| SiteLanguageForm {
.iter()
.map(|&l| SiteLanguageForm {
site_id: for_site_id,
language_id: l,
})
.collect::<Vec<_>>();
insert_into(site_language)
// Insert new languages
let insert_new = insert_into(site_language)
.values(forms)
.get_result::<Self>(conn)
.await?;
.on_conflict((site_id, language_id))
.do_nothing()
.execute(conn);
tokio::try_join!(delete_old, insert_new)?;
CommunityLanguage::limit_languages(conn, instance_id).await?;
@ -278,8 +285,8 @@ impl CommunityLanguage {
}
let form = lang_ids
.into_iter()
.map(|language_id| CommunityLanguageForm {
.iter()
.map(|&language_id| CommunityLanguageForm {
community_id: for_community_id,
language_id,
})
@ -289,25 +296,25 @@ impl CommunityLanguage {
.build_transaction()
.run(|conn| {
Box::pin(async move {
use crate::schema::community_language::dsl::{community_id, community_language};
use diesel::result::DatabaseErrorKind::UniqueViolation;
// Clear the current languages
delete(community_language.filter(community_id.eq(for_community_id)))
.execute(conn)
.await?;
use crate::schema::community_language::dsl::{
community_id,
community_language,
language_id,
};
// Delete old languages, not including new languages
let delete_old = delete(community_language)
.filter(community_id.eq(for_community_id))
.filter(language_id.ne_all(&lang_ids))
.execute(conn);
let insert_res = insert_into(community_language)
// Insert new languages
let insert_new = insert_into(community_language)
.values(form)
.get_result::<Self>(conn)
.await;
.on_conflict((community_id, language_id))
.do_nothing()
.execute(conn);
if let Err(Error::DatabaseError(UniqueViolation, _info)) = insert_res {
// race condition: this function was probably called simultaneously from another caller. ignore error
// tracing::warn!("unique error: {_info:#?}");
// _info.constraint_name() should be = "community_language_community_id_language_id_key"
return Ok(());
}
insert_res?;
tokio::try_join!(delete_old, insert_new)?;
Ok(())
}) as _

View file

@ -84,22 +84,6 @@ impl Post {
.await
}
pub async fn list_for_community(
pool: &mut DbPool<'_>,
the_community_id: CommunityId,
) -> Result<Vec<Self>, Error> {
let conn = &mut get_conn(pool).await?;
post::table
.filter(post::community_id.eq(the_community_id))
.filter(post::deleted.eq(false))
.filter(post::removed.eq(false))
.then_order_by(post::featured_community.desc())
.then_order_by(post::published.desc())
.limit(FETCH_LIMIT_MAX)
.load::<Self>(conn)
.await
}
pub async fn list_featured_for_community(
pool: &mut DbPool<'_>,
the_community_id: CommunityId,