httpd: Add author alias to reactions

This commit is contained in:
Sebastian Martinez 2024-02-27 16:43:19 +01:00 committed by cloudhead
parent 64fd41961e
commit 5bdc2ad1fb
No known key found for this signature in database
2 changed files with 42 additions and 20 deletions

View File

@ -137,13 +137,10 @@ pub(crate) fn patch(
"description": rev.description(),
"edits": rev.edits().map(|e| edit(e, aliases)).collect::<Vec<_>>(),
"reactions": rev.reactions().iter().flat_map(|(location, reaction)| {
let reactions = reaction.iter().fold(BTreeMap::new(), |mut acc: BTreeMap<&Reaction, Vec<_>>, (author, emoji)| {
reactions(reaction.iter().fold(BTreeMap::new(), |mut acc: BTreeMap<&Reaction, Vec<_>>, (author, emoji)| {
acc.entry(emoji).or_default().push(author);
acc
});
reactions.iter().map(|(emoji, authors)|
json!({ "location": location, "emoji": emoji, "authors": authors })
).collect::<Vec<_>>()
}), location.as_ref(), aliases)
}).collect::<Vec<_>>(),
"base": rev.base(),
"oid": rev.head(),
@ -160,6 +157,28 @@ pub(crate) fn patch(
})
}
/// Returns JSON for a `reaction`.
fn reactions(
reactions: BTreeMap<&Reaction, Vec<&ActorId>>,
location: Option<&CodeLocation>,
aliases: &impl AliasStore,
) -> Vec<Value> {
reactions
.into_iter()
.map(|(emoji, authors)| {
if let Some(l) = location {
json!({ "location": l, "emoji": emoji, "authors": authors.into_iter().map(|a|
author(&Author::from(*a), aliases.alias(a))
).collect::<Vec<_>>()})
} else {
json!({ "emoji": emoji, "authors": authors.into_iter().map(|a|
author(&Author::from(*a), aliases.alias(a))
).collect::<Vec<_>>()})
}
})
.collect::<Vec<_>>()
}
/// Returns JSON for an `author` and fills in `alias` when present.
fn author(author: &Author, alias: Option<Alias>) -> Value {
match alias {
@ -212,9 +231,7 @@ fn issue_comment(id: &CommentId, comment: &Comment, aliases: &impl AliasStore) -
"body": comment.body(),
"edits": comment.edits().map(|e| edit(e, aliases)).collect::<Vec<_>>(),
"embeds": comment.embeds().to_vec(),
"reactions": comment.reactions().iter().map(|(emoji, authors)|
json!({ "emoji": emoji, "authors": authors })
).collect::<Vec<_>>(),
"reactions": reactions(comment.reactions(), None, aliases),
"timestamp": comment.timestamp().as_secs(),
"replyTo": comment.reply_to(),
"resolved": comment.is_resolved(),
@ -233,9 +250,7 @@ fn patch_comment(
"body": comment.body(),
"edits": comment.edits().map(|e| edit(e, aliases)).collect::<Vec<_>>(),
"embeds": comment.embeds().to_vec(),
"reactions": comment.reactions().iter().map(|(emoji, authors)|
json!({ "emoji": emoji, "authors": authors })
).collect::<Vec<_>>(),
"reactions": reactions(comment.reactions(), None, aliases),
"timestamp": comment.timestamp().as_secs(),
"replyTo": comment.reply_to(),
"location": comment.location(),
@ -255,9 +270,7 @@ fn review_comment(
"body": comment.body(),
"edits": comment.edits().map(|e| edit(e, aliases)).collect::<Vec<_>>(),
"embeds": comment.embeds().to_vec(),
"reactions": comment.reactions().iter().map(|(emoji, authors)|
json!({ "emoji": emoji, "authors": authors })
).collect::<Vec<_>>(),
"reactions": reactions(comment.reactions(), None, aliases),
"timestamp": comment.timestamp().as_secs(),
"replyTo": comment.reply_to(),
"location": comment.location(),

View File

@ -2297,7 +2297,9 @@ mod routes {
"reactions": [
{
"emoji": "🚀",
"authors": ["z6Mkk7oqY4pPxhMmGEotDYsFo97vhCj85BLY1H256HrJmjN8"]
"authors": [
{ "id": "did:key:z6Mkk7oqY4pPxhMmGEotDYsFo97vhCj85BLY1H256HrJmjN8" }
],
},
],
"timestamp": TIMESTAMP,
@ -3131,9 +3133,10 @@ mod routes {
],
"reactions": [
{
"location": null,
"emoji": "🙏",
"authors": ["z6Mkk7oqY4pPxhMmGEotDYsFo97vhCj85BLY1H256HrJmjN8"],
"authors": [
{ "id": "did:key:z6Mkk7oqY4pPxhMmGEotDYsFo97vhCj85BLY1H256HrJmjN8" }
],
},
{
"location": {
@ -3149,7 +3152,9 @@ mod routes {
}
},
"emoji": "🚀",
"authors": ["z6Mkk7oqY4pPxhMmGEotDYsFo97vhCj85BLY1H256HrJmjN8"]
"authors": [
{ "id": "did:key:z6Mkk7oqY4pPxhMmGEotDYsFo97vhCj85BLY1H256HrJmjN8" }
]
},
],
"base": PARENT,
@ -3338,7 +3343,9 @@ mod routes {
"reactions": [
{
"emoji": "🚀",
"authors": ["z6Mkk7oqY4pPxhMmGEotDYsFo97vhCj85BLY1H256HrJmjN8"]
"authors": [
{ "id": "did:key:z6Mkk7oqY4pPxhMmGEotDYsFo97vhCj85BLY1H256HrJmjN8" }
],
},
],
"timestamp": TIMESTAMP,
@ -3581,7 +3588,9 @@ mod routes {
"reactions": [
{
"emoji": "🚀",
"authors": ["z6Mkk7oqY4pPxhMmGEotDYsFo97vhCj85BLY1H256HrJmjN8"],
"authors": [
{ "id": "did:key:z6Mkk7oqY4pPxhMmGEotDYsFo97vhCj85BLY1H256HrJmjN8" }
],
},
],
"timestamp": 1671125284,