add: chat message reactions
This commit is contained in:
parent
a4298f95f6
commit
a37312fecf
20 changed files with 557 additions and 25 deletions
103
crates/app/src/routes/api/v1/channels/message_reactions.rs
Normal file
103
crates/app/src/routes/api/v1/channels/message_reactions.rs
Normal file
|
@ -0,0 +1,103 @@
|
|||
use crate::{get_user_from_token, routes::api::v1::CreateMessageReaction, State};
|
||||
use axum::{Extension, Json, extract::Path, response::IntoResponse};
|
||||
use axum_extra::extract::CookieJar;
|
||||
use tetratto_core::model::{channels::MessageReaction, oauth, ApiReturn, Error};
|
||||
|
||||
pub async fn get_request(
|
||||
jar: CookieJar,
|
||||
Extension(data): Extension<State>,
|
||||
Path(id): Path<usize>,
|
||||
) -> impl IntoResponse {
|
||||
let data = &(data.read().await).0;
|
||||
let user = match get_user_from_token!(jar, data, oauth::AppScope::UserReact) {
|
||||
Some(ua) => ua,
|
||||
None => return Json(Error::NotAllowed.into()),
|
||||
};
|
||||
|
||||
match data
|
||||
.get_message_reactions_by_owner_message(user.id, id)
|
||||
.await
|
||||
{
|
||||
Ok(r) => Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "Reactions exists".to_string(),
|
||||
payload: Some(r),
|
||||
}),
|
||||
Err(e) => Json(e.into()),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn create_request(
|
||||
jar: CookieJar,
|
||||
Extension(data): Extension<State>,
|
||||
Json(req): Json<CreateMessageReaction>,
|
||||
) -> impl IntoResponse {
|
||||
let data = &(data.read().await).0;
|
||||
let user = match get_user_from_token!(jar, data, oauth::AppScope::UserReact) {
|
||||
Some(ua) => ua,
|
||||
None => return Json(Error::NotAllowed.into()),
|
||||
};
|
||||
|
||||
let message_id = match req.message.parse::<usize>() {
|
||||
Ok(n) => n,
|
||||
Err(e) => return Json(Error::MiscError(e.to_string()).into()),
|
||||
};
|
||||
|
||||
// check for existing reaction
|
||||
if let Ok(r) = data
|
||||
.get_message_reaction_by_owner_message_emoji(user.id, message_id, &req.emoji)
|
||||
.await
|
||||
{
|
||||
if let Err(e) = data.delete_message_reaction(r.id, &user).await {
|
||||
return Json(e.into());
|
||||
} else {
|
||||
return Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "Reaction removed".to_string(),
|
||||
payload: (),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// create reaction
|
||||
match data
|
||||
.create_message_reaction(MessageReaction::new(user.id, message_id, req.emoji), &user)
|
||||
.await
|
||||
{
|
||||
Ok(_) => Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "Reaction created".to_string(),
|
||||
payload: (),
|
||||
}),
|
||||
Err(e) => Json(e.into()),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn delete_request(
|
||||
jar: CookieJar,
|
||||
Extension(data): Extension<State>,
|
||||
Path((id, emoji)): Path<(usize, String)>,
|
||||
) -> impl IntoResponse {
|
||||
let data = &(data.read().await).0;
|
||||
let user = match get_user_from_token!(jar, data, oauth::AppScope::UserReact) {
|
||||
Some(ua) => ua,
|
||||
None => return Json(Error::NotAllowed.into()),
|
||||
};
|
||||
|
||||
let reaction = match data
|
||||
.get_message_reaction_by_owner_message_emoji(user.id, id, &emoji)
|
||||
.await
|
||||
{
|
||||
Ok(r) => r,
|
||||
Err(e) => return Json(e.into()),
|
||||
};
|
||||
|
||||
match data.delete_message_reaction(reaction.id, &user).await {
|
||||
Ok(_) => Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "Reaction deleted".to_string(),
|
||||
payload: (),
|
||||
}),
|
||||
Err(e) => Json(e.into()),
|
||||
}
|
||||
}
|
|
@ -1,2 +1,3 @@
|
|||
pub mod channels;
|
||||
pub mod message_reactions;
|
||||
pub mod messages;
|
||||
|
|
|
@ -16,12 +16,16 @@ use tetratto_core::model::{
|
|||
|
||||
/// Expand a unicode emoji into its Gemoji shortcode.
|
||||
pub async fn get_emoji_shortcode(emoji: String) -> impl IntoResponse {
|
||||
match emojis::get(&emoji) {
|
||||
Some(e) => match e.shortcode() {
|
||||
Some(s) => s.to_string(),
|
||||
None => e.name().replace(" ", "-"),
|
||||
match emoji.as_str() {
|
||||
"👍" => "thumbs_up".to_string(),
|
||||
"👎" => "thumbs_down".to_string(),
|
||||
_ => match emojis::get(&emoji) {
|
||||
Some(e) => match e.shortcode() {
|
||||
Some(s) => s.to_string(),
|
||||
None => e.name().replace(" ", "-"),
|
||||
},
|
||||
None => String::new(),
|
||||
},
|
||||
None => String::new(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -41,6 +41,19 @@ pub fn routes() -> Router {
|
|||
.route("/reactions", post(reactions::create_request))
|
||||
.route("/reactions/{id}", get(reactions::get_request))
|
||||
.route("/reactions/{id}", delete(reactions::delete_request))
|
||||
// message reactions
|
||||
.route(
|
||||
"/message_reactions",
|
||||
post(channels::message_reactions::create_request),
|
||||
)
|
||||
.route(
|
||||
"/message_reactions/{id}",
|
||||
get(channels::message_reactions::get_request),
|
||||
)
|
||||
.route(
|
||||
"/message_reactions/{id}/{emoji}",
|
||||
delete(channels::message_reactions::delete_request),
|
||||
)
|
||||
// communities
|
||||
.route(
|
||||
"/communities/find/{id}",
|
||||
|
@ -907,3 +920,9 @@ pub struct UpdateNoteContent {
|
|||
pub struct RenderMarkdown {
|
||||
pub content: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct CreateMessageReaction {
|
||||
pub message: String,
|
||||
pub emoji: String,
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue