-
-
Notifications
You must be signed in to change notification settings - Fork 884
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding a get_random_community endpoint. (#5042)
* Adding a get_random_community endpoint. - Fixes #4698 * Fixing issue from main. * Adding ListingType to the query. * More concise query filter.
- Loading branch information
1 parent
483bdd5
commit 432d46c
Showing
8 changed files
with
98 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ pub mod ban; | |
pub mod block; | ||
pub mod follow; | ||
pub mod hide; | ||
pub mod random; | ||
pub mod transfer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use activitypub_federation::config::Data; | ||
use actix_web::web::{Json, Query}; | ||
use lemmy_api_common::{ | ||
community::{CommunityResponse, GetRandomCommunity}, | ||
context::LemmyContext, | ||
utils::{check_private_instance, is_mod_or_admin_opt}, | ||
}; | ||
use lemmy_db_schema::source::{ | ||
actor_language::CommunityLanguage, | ||
community::Community, | ||
local_site::LocalSite, | ||
}; | ||
use lemmy_db_views::structs::LocalUserView; | ||
use lemmy_db_views_actor::structs::CommunityView; | ||
use lemmy_utils::error::LemmyResult; | ||
|
||
#[tracing::instrument(skip(context))] | ||
pub async fn get_random_community( | ||
data: Query<GetRandomCommunity>, | ||
context: Data<LemmyContext>, | ||
local_user_view: Option<LocalUserView>, | ||
) -> LemmyResult<Json<CommunityResponse>> { | ||
let local_site = LocalSite::read(&mut context.pool()).await?; | ||
|
||
check_private_instance(&local_user_view, &local_site)?; | ||
|
||
let local_user = local_user_view.as_ref().map(|u| &u.local_user); | ||
|
||
let random_community_id = | ||
Community::get_random_community_id(&mut context.pool(), &data.type_).await?; | ||
|
||
let is_mod_or_admin = is_mod_or_admin_opt( | ||
&mut context.pool(), | ||
local_user_view.as_ref(), | ||
Some(random_community_id), | ||
) | ||
.await | ||
.is_ok(); | ||
|
||
let community_view = CommunityView::read( | ||
&mut context.pool(), | ||
random_community_id, | ||
local_user, | ||
is_mod_or_admin, | ||
) | ||
.await?; | ||
|
||
let discussion_languages = | ||
CommunityLanguage::read(&mut context.pool(), random_community_id).await?; | ||
|
||
Ok(Json(CommunityResponse { | ||
community_view, | ||
discussion_languages, | ||
})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters