Add custom_emoji list route

This commit is contained in:
Freakazoid182 2024-03-31 16:49:32 +02:00
parent 60f9a97dfa
commit fd275231bc
5 changed files with 81 additions and 5 deletions

View file

@ -1,6 +1,7 @@
use lemmy_db_schema::newtypes::CustomEmojiId;
use lemmy_db_views::structs::CustomEmojiView;
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
#[cfg(feature = "full")]
use ts_rs::TS;
use url::Url;
@ -46,3 +47,21 @@ pub struct DeleteCustomEmoji {
pub struct CustomEmojiResponse {
pub custom_emoji: CustomEmojiView,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
#[cfg_attr(feature = "full", derive(TS))]
#[cfg_attr(feature = "full", ts(export))]
/// A response for custom emojis.
pub struct ListCustomEmojisResponse {
pub custom_emojis: Vec<CustomEmojiView>,
}
#[skip_serializing_none]
#[derive(Debug, Serialize, Deserialize, Clone, Copy, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "full", derive(TS))]
#[cfg_attr(feature = "full", ts(export))]
/// Fetches a list of registration applications.
pub struct ListCustomEmojis {
pub page: Option<i64>,
pub limit: Option<i64>,
}

View file

@ -0,0 +1,27 @@
use actix_web::web::{Data, Json, Query};
use lemmy_api_common::{
context::LemmyContext,
custom_emoji::{ListCustomEmojis, ListCustomEmojisResponse},
};
use lemmy_db_views::structs::{CustomEmojiView, LocalUserView, SiteView};
use lemmy_utils::error::LemmyError;
#[tracing::instrument(skip(context))]
pub async fn list_custom_emojis(
data: Query<ListCustomEmojis>,
local_user_view: Option<LocalUserView>,
context: Data<LemmyContext>,
) -> Result<Json<ListCustomEmojisResponse>, LemmyError> {
let local_site = SiteView::read_local(&mut context.pool()).await?;
let custom_emojis = CustomEmojiView::list(
&mut context.pool(),
local_site.local_site.id,
data.page,
data.limit,
)
.await
.map_err(|e| anyhow::anyhow!("Failed to construct custom emojis response: {e}"))?;
Ok(Json(ListCustomEmojisResponse { custom_emojis }))
}

View file

@ -1,3 +1,4 @@
pub mod create;
pub mod delete;
pub mod list;
pub mod update;

View file

@ -5,7 +5,7 @@ use lemmy_db_schema::{
newtypes::{CustomEmojiId, LocalSiteId},
schema::{custom_emoji, custom_emoji_keyword},
source::{custom_emoji::CustomEmoji, custom_emoji_keyword::CustomEmojiKeyword},
utils::{get_conn, DbPool},
utils::{get_conn, limit_and_offset, DbPool},
};
use std::collections::HashMap;
@ -57,6 +57,33 @@ impl CustomEmojiView {
Ok(CustomEmojiView::from_tuple_to_vec(emojis))
}
pub async fn list(
pool: &mut DbPool<'_>,
for_local_site_id: LocalSiteId,
page: Option<i64>,
limit: Option<i64>,
) -> Result<Vec<Self>, Error> {
let conn = &mut get_conn(pool).await?;
let (limit, offset) = limit_and_offset(page, limit)?;
let emojis = custom_emoji::table
.filter(custom_emoji::local_site_id.eq(for_local_site_id))
.left_join(
custom_emoji_keyword::table.on(custom_emoji_keyword::custom_emoji_id.eq(custom_emoji::id)),
)
.order(custom_emoji::category)
.then_order_by(custom_emoji::id)
.select((
custom_emoji::all_columns,
custom_emoji_keyword::all_columns.nullable(), // (or all the columns if you want)
))
.limit(limit)
.offset(offset)
.load::<CustomEmojiTuple>(conn)
.await?;
Ok(CustomEmojiView::from_tuple_to_vec(emojis))
}
fn from_tuple_to_vec(items: Vec<CustomEmojiTuple>) -> Vec<Self> {
let mut result = Vec::new();
let mut hash: HashMap<CustomEmojiId, Vec<CustomEmojiKeyword>> = HashMap::new();

View file

@ -104,9 +104,10 @@ use lemmy_api_crud::{
update::update_community,
},
custom_emoji::{
create::create_custom_emoji,
delete::delete_custom_emoji,
update::update_custom_emoji,
create::create_custom_emoji,
delete::delete_custom_emoji,
list::list_custom_emojis,
update::update_custom_emoji
},
post::{
create::create_post,
@ -361,7 +362,8 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimitCell) {
.wrap(rate_limit.message())
.route("", web::post().to(create_custom_emoji))
.route("", web::put().to(update_custom_emoji))
.route("/delete", web::post().to(delete_custom_emoji)),
.route("/delete", web::post().to(delete_custom_emoji))
.route("/list", web::get().to(list_custom_emojis)),
),
);
cfg.service(