Skip to content

Commit

Permalink
address PR review
Browse files Browse the repository at this point in the history
  • Loading branch information
zeerooth committed Nov 10, 2023
1 parent 7afd1b0 commit 8606f0b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 41 deletions.
17 changes: 6 additions & 11 deletions crates/kitsune-core/src/activitypub/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
};
use diesel::{ExpressionMethods, SelectableHelper};
use diesel_async::{AsyncPgConnection, RunQueryDsl};
use futures_util::future::join_all;
use futures_util::{future::try_join_all, TryFutureExt};
use http::Uri;
use iso8601_timestamp::Timestamp;
use kitsune_db::{
Expand Down Expand Up @@ -81,24 +81,19 @@ async fn handle_custom_emojis(
}

let futures = emoji_iter.clone().filter_map(|emoji| {
emoji
.id
.as_ref()
.map(|remote_id| fetcher.fetch_emoji(remote_id))
let remote_id = emoji.id.as_ref()?;
Some(fetcher.fetch_emoji(remote_id).map_ok(move |f| (f, emoji)))
});

let emojis = join_all(futures)
.await
.into_iter()
.collect::<Result<Vec<_>, _>>()?
let emojis = try_join_all(futures)
.await?
.iter()
.zip(emoji_iter)
.map(|(resolved_emoji, emoji_tag)| PostCustomEmoji {
post_id,
custom_emoji_id: resolved_emoji.id,
emoji_text: emoji_tag.name.to_string(),
})
.collect::<Vec<_>>();
.collect::<Vec<PostCustomEmoji>>();

diesel::insert_into(posts_custom_emojis::table)
.values(emojis)
Expand Down
17 changes: 7 additions & 10 deletions crates/kitsune-core/src/mapping/activitypub/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,21 +275,18 @@ impl IntoObject for CustomEmoji {
// Officially we don't have any info about remote emojis as we're not the origin
// Let's pretend we're not home and do not answer
let name = match self.domain {
None => Ok::<String, ApiError>(format!(":{}:", self.shortcode)),
None => Ok(format!(":{}:", self.shortcode)),
Some(_) => Err(ApiError::NotFound),
}?;
let icon = state
.db_pool
.with_connection(|db_conn| {
async move {
media_attachments::table
.find(self.media_attachment_id)
.get_result::<DbMediaAttachment>(db_conn)
.map_err(Error::from)
.and_then(|media_attachment| media_attachment.into_object(state))
.await
}
.scoped()
media_attachments::table
.find(self.media_attachment_id)
.get_result::<DbMediaAttachment>(db_conn)
.map_err(Error::from)
.and_then(|media_attachment| media_attachment.into_object(state))
.scoped()
})
.await?;

Expand Down
17 changes: 9 additions & 8 deletions crates/kitsune-core/src/mapping/mastodon/sealed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,20 +623,21 @@ impl IntoMastodon for (DbCustomEmoji, DbMediaAttachment, Option<Timestamp>) {
}

async fn into_mastodon(self, state: MapperState<'_>) -> Result<Self::Output> {
let shortcode = if let Some(ref domain) = self.0.domain {
format!(":{}@{}:", self.0.shortcode, domain)
let (emoji, attachment, last_used) = self;
let shortcode = if let Some(ref domain) = emoji.domain {
format!(":{}@{}:", emoji.shortcode, domain)
} else {
format!(":{}:", self.0.shortcode)
format!(":{}:", emoji.shortcode)
};
let url = state.url_service.media_url(self.1.id);
let category = if self.2.is_some() {
let url = state.url_service.media_url(attachment.id);
let category = if last_used.is_some() {
Some(String::from("recently used"))
} else if self.0.endorsed {
} else if emoji.endorsed {
Some(String::from("endorsed"))
} else if self.0.domain.is_none() {
} else if emoji.domain.is_none() {
Some(String::from("local"))
} else {
Some(self.0.domain.unwrap())
Some(emoji.domain.unwrap())
};
Ok(CustomEmoji {
shortcode,
Expand Down
6 changes: 2 additions & 4 deletions crates/kitsune-core/src/service/custom_emoji.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ impl CustomEmojiService {
pub async fn get_by_id(&self, id: Uuid) -> Result<CustomEmoji> {
let query = custom_emojis::table
.find(id)
.select(CustomEmoji::as_select())
.into_boxed();
.select(CustomEmoji::as_select());

self.db_pool
.with_connection(|db_conn| async move { query.get_result(db_conn).await }.scoped())
Expand Down Expand Up @@ -137,8 +136,7 @@ impl CustomEmojiService {
MediaAttachment::as_select(),
posts::created_at.nullable(),
))
.limit(get_emoji_list.limit)
.into_boxed();
.limit(get_emoji_list.limit);

self.db_pool
.with_connection(|db_conn| {
Expand Down
11 changes: 3 additions & 8 deletions lib/post-process/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,16 @@ fn enforce_prefix<'a>(lexer: &Lexer<'a, PostElement<'a>>) -> bool {
}

#[inline]
fn emoji_split<'a>(lexer: &Lexer<'a, PostElement<'a>>) -> Option<(&'a str, Option<&'a str>)> {
if !enforce_prefix(lexer) || !enforce_postfix(lexer) {
return None;
}

let slice = lexer.slice();
let slice = slice.trim_matches(':');
fn emoji_split<'a>(lexer: &Lexer<'a, PostElement<'a>>) -> (&'a str, Option<&'a str>) {
let slice = lexer.slice().trim_matches(':');

let emoji_data = if let Some((shortcode, domain)) = slice.split_once('@') {
(shortcode, Some(domain))
} else {
(slice, None)
};

Some(emoji_data)
emoji_data
}

#[inline]
Expand Down

0 comments on commit 8606f0b

Please sign in to comment.