Radish alpha
h
rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5
Radicle Heartwood Protocol & Stack
Radicle
Git
httpd: Add author alias to reactions
Merged did:key:z6MkkfM3...sVz5 opened 2 years ago
2 files changed +42 -20 64fd4196 5bdc2ad1
modified radicle-httpd/src/api/json.rs
@@ -133,13 +133,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(),
@@ -156,6 +153,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 {
@@ -208,9 +227,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(),
@@ -229,9 +246,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(),
@@ -251,9 +266,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(),
modified radicle-httpd/src/api/v1/projects.rs
@@ -2297,7 +2297,9 @@ mod routes {
                  "reactions": [
                    {
                      "emoji": "🚀",
-
                      "authors": ["z6Mkk7oqY4pPxhMmGEotDYsFo97vhCj85BLY1H256HrJmjN8"]
+
                      "authors": [
+
                        { "id": "did:key:z6Mkk7oqY4pPxhMmGEotDYsFo97vhCj85BLY1H256HrJmjN8" }
+
                      ],
                    },
                  ],
                  "timestamp": TIMESTAMP,
@@ -3057,9 +3059,10 @@ mod routes {
                  ],
                  "reactions": [
                    {
-
                      "location": null,
                      "emoji": "🙏",
-
                      "authors": ["z6Mkk7oqY4pPxhMmGEotDYsFo97vhCj85BLY1H256HrJmjN8"],
+
                      "authors": [
+
                        { "id": "did:key:z6Mkk7oqY4pPxhMmGEotDYsFo97vhCj85BLY1H256HrJmjN8" }
+
                      ],
                    },
                    {
                      "location": {
@@ -3075,7 +3078,9 @@ mod routes {
                        }
                      },
                      "emoji": "🚀",
-
                      "authors": ["z6Mkk7oqY4pPxhMmGEotDYsFo97vhCj85BLY1H256HrJmjN8"]
+
                      "authors": [
+
                        { "id": "did:key:z6Mkk7oqY4pPxhMmGEotDYsFo97vhCj85BLY1H256HrJmjN8" }
+
                      ]
                    },
                  ],
                  "base": PARENT,
@@ -3264,7 +3269,9 @@ mod routes {
                      "reactions": [
                        {
                          "emoji": "🚀",
-
                          "authors": ["z6Mkk7oqY4pPxhMmGEotDYsFo97vhCj85BLY1H256HrJmjN8"]
+
                          "authors": [
+
                            { "id": "did:key:z6Mkk7oqY4pPxhMmGEotDYsFo97vhCj85BLY1H256HrJmjN8" }
+
                          ],
                        },
                      ],
                      "timestamp": TIMESTAMP,
@@ -3507,7 +3514,9 @@ mod routes {
                          "reactions": [
                            {
                              "emoji": "🚀",
-
                              "authors": ["z6Mkk7oqY4pPxhMmGEotDYsFo97vhCj85BLY1H256HrJmjN8"],
+
                              "authors": [
+
                                { "id": "did:key:z6Mkk7oqY4pPxhMmGEotDYsFo97vhCj85BLY1H256HrJmjN8" }
+
                              ],
                            },
                          ],
                          "timestamp": 1671125284,