feat: return user struct instead of uuid for reaction

This commit is contained in:
Khor Shu Heng 2024-07-27 12:55:07 +08:00
parent b861f0a703
commit 62f32e8757
5 changed files with 43 additions and 21 deletions

View File

@ -1,6 +1,6 @@
{ {
"db_name": "PostgreSQL", "db_name": "PostgreSQL",
"query": "\n SELECT\n avr.comment_id,\n avr.reaction_type,\n au.uuid AS user_uuid\n FROM af_published_view_reaction avr\n INNER JOIN af_user au ON avr.created_by = au.uid\n WHERE view_id = $1\n ", "query": "\n SELECT\n avr.comment_id,\n avr.reaction_type,\n au.uuid AS user_uuid,\n au.name AS user_name\n FROM af_published_view_reaction avr\n INNER JOIN af_user au ON avr.created_by = au.uid\n WHERE view_id = $1\n ",
"describe": { "describe": {
"columns": [ "columns": [
{ {
@ -17,6 +17,11 @@
"ordinal": 2, "ordinal": 2,
"name": "user_uuid", "name": "user_uuid",
"type_info": "Uuid" "type_info": "Uuid"
},
{
"ordinal": 3,
"name": "user_name",
"type_info": "Text"
} }
], ],
"parameters": { "parameters": {
@ -25,10 +30,11 @@
] ]
}, },
"nullable": [ "nullable": [
false,
false, false,
false, false,
false false
] ]
}, },
"hash": "3064614b0f62b3018296246bf497ec473acad4946ff3adab9aa84d1f748c9cdf" "hash": "304da1f7fec4fcd69c2e0e0bbb24edb0bce2e988c6fea1eb856b7625b4d1f16f"
} }

View File

@ -1,6 +1,6 @@
{ {
"db_name": "PostgreSQL", "db_name": "PostgreSQL",
"query": "\n SELECT\n avr.comment_id,\n avr.reaction_type,\n au.uuid AS user_uuid\n FROM af_published_view_reaction avr\n INNER JOIN af_user au ON avr.created_by = au.uid\n WHERE comment_id = $1\n ", "query": "\n SELECT\n avr.comment_id,\n avr.reaction_type,\n au.uuid AS user_uuid,\n au.name AS user_name\n FROM af_published_view_reaction avr\n INNER JOIN af_user au ON avr.created_by = au.uid\n WHERE comment_id = $1\n ",
"describe": { "describe": {
"columns": [ "columns": [
{ {
@ -17,6 +17,11 @@
"ordinal": 2, "ordinal": 2,
"name": "user_uuid", "name": "user_uuid",
"type_info": "Uuid" "type_info": "Uuid"
},
{
"ordinal": 3,
"name": "user_name",
"type_info": "Text"
} }
], ],
"parameters": { "parameters": {
@ -25,10 +30,11 @@
] ]
}, },
"nullable": [ "nullable": [
false,
false, false,
false, false,
false false
] ]
}, },
"hash": "3511e9df911493be0de8543c6cbf0dec74c617a15c67ee6ff367d18be335eff3" "hash": "f9a80c40a2dea06b391a065c946472ff8e1a8e63425155e88b472b91c1e24f3a"
} }

View File

@ -849,7 +849,7 @@ pub struct GlobalComments {
pub comments: Vec<GlobalComment>, pub comments: Vec<GlobalComment>,
} }
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug, Clone)]
pub struct AFWebUser { pub struct AFWebUser {
pub uid: Uuid, pub uid: Uuid,
pub name: String, pub name: String,
@ -886,7 +886,7 @@ pub struct Reactions {
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct Reaction { pub struct Reaction {
pub reaction_type: String, pub reaction_type: String,
pub react_user_uids: Vec<Uuid>, pub react_users: Vec<AFWebUser>,
pub comment_id: Uuid, pub comment_id: Uuid,
} }

View File

@ -1243,7 +1243,8 @@ pub async fn select_reactions_for_published_view<'a, E: Executor<'a, Database =
SELECT SELECT
avr.comment_id, avr.comment_id,
avr.reaction_type, avr.reaction_type,
au.uuid AS user_uuid au.uuid AS user_uuid,
au.name AS user_name
FROM af_published_view_reaction avr FROM af_published_view_reaction avr
INNER JOIN af_user au ON avr.created_by = au.uid INNER JOIN af_user au ON avr.created_by = au.uid
WHERE view_id = $1 WHERE view_id = $1
@ -1252,16 +1253,20 @@ pub async fn select_reactions_for_published_view<'a, E: Executor<'a, Database =
) )
.fetch_all(executor) .fetch_all(executor)
.await?; .await?;
let reaction_to_users_map: HashMap<ReactionKey, Vec<Uuid>> = rows.iter().fold( let reaction_to_users_map: HashMap<ReactionKey, Vec<AFWebUser>> = rows.iter().fold(
HashMap::new(), HashMap::new(),
|mut acc: HashMap<ReactionKey, Vec<Uuid>>, row| { |mut acc: HashMap<ReactionKey, Vec<AFWebUser>>, row| {
let users = acc let users = acc
.entry(ReactionKey { .entry(ReactionKey {
comment_id: row.comment_id, comment_id: row.comment_id,
reaction_type: row.reaction_type.clone(), reaction_type: row.reaction_type.clone(),
}) })
.or_default(); .or_default();
users.push(row.user_uuid); users.push(AFWebUser {
uid: row.user_uuid.clone(),
name: row.user_name.clone(),
avatar_url: None,
});
acc acc
}, },
); );
@ -1273,11 +1278,11 @@ pub async fn select_reactions_for_published_view<'a, E: Executor<'a, Database =
comment_id, comment_id,
reaction_type, reaction_type,
}, },
user_uuids, users,
)| Reaction { )| Reaction {
comment_id: *comment_id, comment_id: *comment_id,
reaction_type: reaction_type.clone(), reaction_type: reaction_type.clone(),
react_user_uids: user_uuids.clone(), react_users: users.clone(),
}, },
) )
.collect(); .collect();
@ -1294,7 +1299,8 @@ pub async fn select_reactions_for_comment<'a, E: Executor<'a, Database = Postgre
SELECT SELECT
avr.comment_id, avr.comment_id,
avr.reaction_type, avr.reaction_type,
au.uuid AS user_uuid au.uuid AS user_uuid,
au.name AS user_name
FROM af_published_view_reaction avr FROM af_published_view_reaction avr
INNER JOIN af_user au ON avr.created_by = au.uid INNER JOIN af_user au ON avr.created_by = au.uid
WHERE comment_id = $1 WHERE comment_id = $1
@ -1303,19 +1309,23 @@ pub async fn select_reactions_for_comment<'a, E: Executor<'a, Database = Postgre
) )
.fetch_all(executor) .fetch_all(executor)
.await?; .await?;
let reaction_type_to_users_map: HashMap<String, Vec<Uuid>> = rows.iter().fold( let reaction_type_to_users_map: HashMap<String, Vec<AFWebUser>> = rows.iter().fold(
HashMap::new(), HashMap::new(),
|mut acc: HashMap<String, Vec<Uuid>>, row| { |mut acc: HashMap<String, Vec<AFWebUser>>, row| {
let users = acc.entry(row.reaction_type.clone()).or_default(); let users = acc.entry(row.reaction_type.clone()).or_default();
users.push(row.user_uuid); users.push(AFWebUser {
uid: row.user_uuid,
name: row.user_name.clone(),
avatar_url: None,
});
acc acc
}, },
); );
let reactions = reaction_type_to_users_map let reactions = reaction_type_to_users_map
.iter() .iter()
.map(|(reaction_type, user_uuids)| Reaction { .map(|(reaction_type, users)| Reaction {
reaction_type: reaction_type.clone(), reaction_type: reaction_type.clone(),
react_user_uids: user_uuids.clone(), react_users: users.clone(),
comment_id: *comment_id, comment_id: *comment_id,
}) })
.collect(); .collect();

View File

@ -481,7 +481,7 @@ async fn test_publish_reactions() {
.reactions; .reactions;
let reaction_count: HashMap<String, i32> = reactions let reaction_count: HashMap<String, i32> = reactions
.iter() .iter()
.map(|r| (r.reaction_type.clone(), r.react_user_uids.len() as i32)) .map(|r| (r.reaction_type.clone(), r.react_users.len() as i32))
.collect(); .collect();
assert_eq!(reaction_count.len(), 2); assert_eq!(reaction_count.len(), 2);
assert_eq!(*reaction_count.get(like_emoji).unwrap(), 2); assert_eq!(*reaction_count.get(like_emoji).unwrap(), 2);
@ -505,7 +505,7 @@ async fn test_publish_reactions() {
.reactions; .reactions;
let reaction_count: HashMap<String, i32> = reactions let reaction_count: HashMap<String, i32> = reactions
.iter() .iter()
.map(|r| (r.reaction_type.clone(), r.react_user_uids.len() as i32)) .map(|r| (r.reaction_type.clone(), r.react_users.len() as i32))
.collect(); .collect();
assert_eq!(reaction_count.len(), 2); assert_eq!(reaction_count.len(), 2);
assert_eq!(*reaction_count.get(like_emoji).unwrap(), 1); assert_eq!(*reaction_count.get(like_emoji).unwrap(), 1);
@ -519,7 +519,7 @@ async fn test_publish_reactions() {
.reactions; .reactions;
let reaction_count: HashMap<String, i32> = reactions let reaction_count: HashMap<String, i32> = reactions
.iter() .iter()
.map(|r| (r.reaction_type.clone(), r.react_user_uids.len() as i32)) .map(|r| (r.reaction_type.clone(), r.react_users.len() as i32))
.collect(); .collect();
assert_eq!(reaction_count.len(), 1); assert_eq!(reaction_count.len(), 1);
assert_eq!(*reaction_count.get(like_emoji).unwrap(), 1); assert_eq!(*reaction_count.get(like_emoji).unwrap(), 1);