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",
"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": {
"columns": [
{
@ -17,6 +17,11 @@
"ordinal": 2,
"name": "user_uuid",
"type_info": "Uuid"
},
{
"ordinal": 3,
"name": "user_name",
"type_info": "Text"
}
],
"parameters": {
@ -25,10 +30,11 @@
]
},
"nullable": [
false,
false,
false,
false
]
},
"hash": "3064614b0f62b3018296246bf497ec473acad4946ff3adab9aa84d1f748c9cdf"
"hash": "304da1f7fec4fcd69c2e0e0bbb24edb0bce2e988c6fea1eb856b7625b4d1f16f"
}

View File

@ -1,6 +1,6 @@
{
"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": {
"columns": [
{
@ -17,6 +17,11 @@
"ordinal": 2,
"name": "user_uuid",
"type_info": "Uuid"
},
{
"ordinal": 3,
"name": "user_name",
"type_info": "Text"
}
],
"parameters": {
@ -25,10 +30,11 @@
]
},
"nullable": [
false,
false,
false,
false
]
},
"hash": "3511e9df911493be0de8543c6cbf0dec74c617a15c67ee6ff367d18be335eff3"
"hash": "f9a80c40a2dea06b391a065c946472ff8e1a8e63425155e88b472b91c1e24f3a"
}

View File

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

View File

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

View File

@ -481,7 +481,7 @@ async fn test_publish_reactions() {
.reactions;
let reaction_count: HashMap<String, i32> = reactions
.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();
assert_eq!(reaction_count.len(), 2);
assert_eq!(*reaction_count.get(like_emoji).unwrap(), 2);
@ -505,7 +505,7 @@ async fn test_publish_reactions() {
.reactions;
let reaction_count: HashMap<String, i32> = reactions
.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();
assert_eq!(reaction_count.len(), 2);
assert_eq!(*reaction_count.get(like_emoji).unwrap(), 1);
@ -519,7 +519,7 @@ async fn test_publish_reactions() {
.reactions;
let reaction_count: HashMap<String, i32> = reactions
.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();
assert_eq!(reaction_count.len(), 1);
assert_eq!(*reaction_count.get(like_emoji).unwrap(), 1);