mirror of
https://github.com/aziascreations/BakaShrot.git
synced 2026-11-23 20:58:46 +01:00
Added the list_topics and list_nodes role flags, Filtered the nodes listed by the roles
- `list_topics` / `list_nodes`: listing answers every topic, every node with messages, without reading any (`query` gives both). - Without them, listing nodes only answers those with messages in the topics the roles may read (every node until now). Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
@@ -473,20 +473,41 @@ async fn get_messages(shared: &Arc<Shared>, peer: &Peer, get: GetMessages) -> Pa
|
||||
}
|
||||
|
||||
/// Topics: those with messages (pushed here or replicated) that it may
|
||||
/// push to or read, every one if it may query (queries read everything).
|
||||
/// Nodes: every one with messages.
|
||||
/// push to or read; every one with `list_topics` or `query`. Nodes: those
|
||||
/// with messages in the topics it may read; every one with `list_nodes`
|
||||
/// or `query`.
|
||||
async fn list_names(shared: &Arc<Shared>, peer: &Peer, list: ListNames) -> Packet {
|
||||
let permissions = &peer.permissions;
|
||||
let found = match list.kind {
|
||||
NameKind::Topic => shared.read(|reader| reader.topics()).await,
|
||||
NameKind::Node => shared.read(|reader| reader.nodes()).await,
|
||||
NameKind::Topic => shared
|
||||
.read(|reader| reader.topics())
|
||||
.await
|
||||
.map(|mut topics| {
|
||||
if !permissions.lists_every_topic() {
|
||||
topics
|
||||
.retain(|topic| permissions.can_push(topic) || permissions.can_read(topic));
|
||||
}
|
||||
topics
|
||||
}),
|
||||
NameKind::Node if permissions.lists_every_node() => {
|
||||
shared.read(|reader| reader.nodes()).await
|
||||
}
|
||||
NameKind::Node => shared.read(|reader| reader.heads()).await.map(|heads| {
|
||||
let mut nodes: Vec<String> = heads
|
||||
.into_iter()
|
||||
.filter(|head| permissions.can_read(&head.topic))
|
||||
.map(|head| head.node)
|
||||
.collect();
|
||||
// As `reader.nodes()` answers them: in order, none twice.
|
||||
nodes.sort_by_cached_key(|node| node.to_ascii_lowercase());
|
||||
nodes.dedup_by(|a, b| names_equal(a, b));
|
||||
nodes
|
||||
}),
|
||||
};
|
||||
let mut names = match found {
|
||||
Ok(names) => names,
|
||||
Err(refused) => return refused.into(),
|
||||
};
|
||||
if list.kind == NameKind::Topic && !peer.permissions.can_query() {
|
||||
names.retain(|topic| peer.permissions.can_push(topic) || peer.permissions.can_read(topic));
|
||||
}
|
||||
let truncated = names.len() > MAX_NAMES_LIST_ENTRIES;
|
||||
names.truncate(MAX_NAMES_LIST_ENTRIES);
|
||||
NamesList {
|
||||
|
||||
@@ -41,6 +41,10 @@ read_topics = ["logs"]
|
||||
[roles.analyst]
|
||||
query = true
|
||||
|
||||
[roles.explorer]
|
||||
list_topics = true
|
||||
list_nodes = true
|
||||
|
||||
[roles.peer]
|
||||
send_topics = ["*"]
|
||||
accept_topics = ["*"]
|
||||
@@ -604,8 +608,24 @@ fn names() {
|
||||
assert_eq!(names(&mut sensor, NameKind::Topic), ["logs", "sensors"]);
|
||||
assert_eq!(names(&mut screen, NameKind::Topic), ["logs"]);
|
||||
assert_eq!(names(&mut analyst, NameKind::Topic), ["logs", "sensors"]);
|
||||
// Nodes: every one with messages.
|
||||
assert_eq!(names(&mut screen, NameKind::Node), ["garage", "kitchen"]);
|
||||
// Nodes: those of the topics it may read; every one for a query role.
|
||||
assert!(names(&mut sensor, NameKind::Node).is_empty());
|
||||
assert_eq!(names(&mut screen, NameKind::Node), ["kitchen"]);
|
||||
assert_eq!(names(&mut analyst, NameKind::Node), ["garage", "kitchen"]);
|
||||
// `list_topics` and `list_nodes`: every one, without reading any.
|
||||
let mut explorer = running
|
||||
.connect(&running.issue("browser", &["explorer"]))
|
||||
.unwrap();
|
||||
assert_eq!(names(&mut explorer, NameKind::Topic), ["logs", "sensors"]);
|
||||
assert_eq!(names(&mut explorer, NameKind::Node), ["garage", "kitchen"]);
|
||||
assert_eq!(
|
||||
code(
|
||||
explorer
|
||||
.get_messages("logs", "kitchen", None, 1)
|
||||
.unwrap_err()
|
||||
),
|
||||
ErrorCode::NotAllowed.code()
|
||||
);
|
||||
}
|
||||
|
||||
/// The query parts of `OldSource/Tests/BigMessages-Test.ps1`.
|
||||
|
||||
Reference in New Issue
Block a user