mirror of
https://github.com/LemmyNet/lemmy.git
synced 2024-10-01 01:36:12 -04:00
parent
458bb60144
commit
95a6ed1ce0
@ -3,4 +3,5 @@ pub mod ban;
|
||||
pub mod block;
|
||||
pub mod follow;
|
||||
pub mod hide;
|
||||
pub mod random;
|
||||
pub mod transfer;
|
||||
|
57
crates/api/src/community/random.rs
Normal file
57
crates/api/src/community/random.rs
Normal file
@ -0,0 +1,57 @@
|
||||
use activitypub_federation::config::Data;
|
||||
use actix_web::web::Json;
|
||||
use lemmy_api_common::{
|
||||
community::CommunityResponse,
|
||||
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::{LemmyErrorType, LemmyResult};
|
||||
|
||||
#[tracing::instrument(skip(context))]
|
||||
pub async fn get_random_community(
|
||||
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_local_community(&mut context.pool())
|
||||
.await?
|
||||
.ok_or(LemmyErrorType::CouldntFindCommunity)?
|
||||
.id;
|
||||
|
||||
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?
|
||||
.ok_or(LemmyErrorType::CouldntFindCommunity)?;
|
||||
|
||||
let discussion_languages =
|
||||
CommunityLanguage::read(&mut context.pool(), random_community_id).await?;
|
||||
|
||||
Ok(Json(CommunityResponse {
|
||||
community_view,
|
||||
discussion_languages,
|
||||
}))
|
||||
}
|
@ -35,8 +35,7 @@ use crate::{
|
||||
use chrono::{DateTime, Utc};
|
||||
use diesel::{
|
||||
deserialize,
|
||||
dsl,
|
||||
dsl::{exists, insert_into},
|
||||
dsl::{self, exists, insert_into, not},
|
||||
pg::Pg,
|
||||
result::Error,
|
||||
select,
|
||||
@ -196,6 +195,20 @@ impl Community {
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn get_random_local_community(pool: &mut DbPool<'_>) -> Result<Option<Self>, Error> {
|
||||
let conn = &mut get_conn(pool).await?;
|
||||
sql_function!(fn random() -> Text);
|
||||
community::table
|
||||
.filter(community::local)
|
||||
.filter(not(community::deleted))
|
||||
.filter(not(community::removed))
|
||||
.order(random())
|
||||
.limit(1)
|
||||
.first::<Self>(conn)
|
||||
.await
|
||||
.optional()
|
||||
}
|
||||
}
|
||||
|
||||
impl CommunityModerator {
|
||||
|
@ -17,6 +17,7 @@ use lemmy_api::{
|
||||
block::block_community,
|
||||
follow::follow_community,
|
||||
hide::hide_community,
|
||||
random::get_random_community,
|
||||
transfer::transfer_community,
|
||||
},
|
||||
local_user::{
|
||||
@ -193,6 +194,7 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimitCell) {
|
||||
.wrap(rate_limit.message())
|
||||
.route("", web::get().to(get_community))
|
||||
.route("", web::put().to(update_community))
|
||||
.route("/random", web::get().to(get_random_community))
|
||||
.route("/hide", web::put().to(hide_community))
|
||||
.route("/list", web::get().to(list_communities))
|
||||
.route("/follow", web::post().to(follow_community))
|
||||
|
Loading…
Reference in New Issue
Block a user