node: Introduce `AliasStore` trait

httpd: use the `AliasStore` trait

Signed-off-by: xphoniex <dj.2dixx@gmail.com>
This commit is contained in:
xphoniex 2023-05-28 09:06:47 +00:00 committed by Alexis Sellier
parent 4652f309b4
commit 738d0dcbad
No known key found for this signature in database
4 changed files with 19 additions and 9 deletions

View File

@ -14,7 +14,7 @@ use radicle::cob::thread;
use radicle::cob::thread::{CommentId, Thread}; use radicle::cob::thread::{CommentId, Thread};
use radicle::cob::{ActorId, Author, Reaction, Timestamp}; use radicle::cob::{ActorId, Author, Reaction, Timestamp};
use radicle::git::RefString; use radicle::git::RefString;
use radicle::node::tracking::store as TrackingStore; use radicle::node::AliasStore;
use radicle::prelude::NodeId; use radicle::prelude::NodeId;
use radicle::storage::{git, refs, ReadRepository}; use radicle::storage::{git, refs, ReadRepository};
use radicle_surf::blob::Blob; use radicle_surf::blob::Blob;
@ -96,7 +96,7 @@ pub(crate) fn tree(tree: &Tree, path: &str, stats: &Stats) -> Value {
} }
/// Returns JSON for an `issue`. /// Returns JSON for an `issue`.
pub(crate) fn issue(id: IssueId, issue: Issue, aliases: &TrackingStore::Config) -> Value { pub(crate) fn issue(id: IssueId, issue: Issue, aliases: &impl AliasStore) -> Value {
json!({ json!({
"id": id.to_string(), "id": id.to_string(),
"author": author(&issue.author(), aliases.alias(issue.author().id())), "author": author(&issue.author(), aliases.alias(issue.author().id())),
@ -116,7 +116,7 @@ pub(crate) fn patch(
id: PatchId, id: PatchId,
patch: Patch, patch: Patch,
repo: &git::Repository, repo: &git::Repository,
aliases: &TrackingStore::Config, aliases: &impl AliasStore,
) -> Value { ) -> Value {
json!({ json!({
"id": id.to_string(), "id": id.to_string(),
@ -249,7 +249,7 @@ impl<'a> Comment<'a> {
id: &'a CommentId, id: &'a CommentId,
comment: &'a thread::Comment, comment: &'a thread::Comment,
thread: &'a Thread, thread: &'a Thread,
aliases: &TrackingStore::Config, aliases: &impl AliasStore,
) -> Self { ) -> Self {
let comment_author = Author::new(comment.author()); let comment_author = Author::new(comment.author());
Self { Self {

View File

@ -15,6 +15,7 @@ use tower_http::set_header::SetResponseHeaderLayer;
use radicle::cob::{issue, patch, thread, ActorId, Tag}; use radicle::cob::{issue, patch, thread, ActorId, Tag};
use radicle::identity::Id; use radicle::identity::Id;
use radicle::node::routing::Store; use radicle::node::routing::Store;
use radicle::node::AliasStore;
use radicle::node::NodeId; use radicle::node::NodeId;
use radicle::storage::git::paths; use radicle::storage::git::paths;
use radicle::storage::{ReadRepository, ReadStorage, WriteRepository}; use radicle::storage::{ReadRepository, ReadStorage, WriteRepository};
@ -456,7 +457,7 @@ async fn issues_handler(
let tracking_store = &ctx.profile.tracking()?; let tracking_store = &ctx.profile.tracking()?;
let issues = issues let issues = issues
.into_iter() .into_iter()
.map(|(id, issue, _)| api::json::issue(id, issue, tracking_store)) .map(|(id, issue, _)| api::json::issue(id, issue, &tracking_store))
.skip(page * per_page) .skip(page * per_page)
.take(per_page) .take(per_page)
.collect::<Vec<_>>(); .collect::<Vec<_>>();
@ -578,7 +579,7 @@ async fn issue_handler(
Ok::<_, Error>(Json(api::json::issue( Ok::<_, Error>(Json(api::json::issue(
issue_id.into(), issue_id.into(),
issue, issue,
tracking_store, &tracking_store,
))) )))
} }
@ -740,7 +741,7 @@ async fn patches_handler(
let tracking_store = &ctx.profile.tracking()?; let tracking_store = &ctx.profile.tracking()?;
let patches = patches let patches = patches
.into_iter() .into_iter()
.map(|(id, patch, _)| api::json::patch(id, patch, &repo, tracking_store)) .map(|(id, patch, _)| api::json::patch(id, patch, &repo, &tracking_store))
.skip(page * per_page) .skip(page * per_page)
.take(per_page) .take(per_page)
.collect::<Vec<_>>(); .collect::<Vec<_>>();
@ -765,7 +766,7 @@ async fn patch_handler(
patch_id.into(), patch_id.into(),
patch, patch,
&repo, &repo,
tracking_store, &tracking_store,
))) )))
} }

View File

@ -694,6 +694,12 @@ impl Handle for Node {
} }
} }
/// A trait for different sources which can potentially return an alias.
pub trait AliasStore {
/// Returns alias of a `NodeId`.
fn alias(&self, nid: &NodeId) -> Option<String>;
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;

View File

@ -5,6 +5,7 @@ use std::{fmt, io, ops::Not as _, time};
use sqlite as sql; use sqlite as sql;
use thiserror::Error; use thiserror::Error;
use crate::node::AliasStore;
use crate::prelude::{Id, NodeId}; use crate::prelude::{Id, NodeId};
use super::{Node, Policy, Repo, Scope}; use super::{Node, Policy, Repo, Scope};
@ -254,10 +255,12 @@ impl Config {
} }
Ok(Box::new(entries.into_iter())) Ok(Box::new(entries.into_iter()))
} }
}
impl AliasStore for &Config {
/// Retrieve `alias` of given node. /// Retrieve `alias` of given node.
/// Calls `Self::node_policy` under the hood. /// Calls `Self::node_policy` under the hood.
pub fn alias(&self, nid: &NodeId) -> Option<String> { fn alias(&self, nid: &NodeId) -> Option<String> {
self.node_policy(nid) self.node_policy(nid)
.map(|node| node.and_then(|n| n.alias)) .map(|node| node.and_then(|n| n.alias))
.unwrap_or(None) .unwrap_or(None)