infoserv: type the news kind as an enum at the store boundary
All checks were successful
CI / check (push) Successful in 3m53s

This commit is contained in:
Jean Chevronnet 2026-07-17 13:53:53 +00:00
parent ee95225eaf
commit 7a5f1502a4
No known key found for this signature in database
11 changed files with 49 additions and 31 deletions

View file

@ -40,7 +40,7 @@ pub(crate) use event::{apply, Scope};
// echo-api SDK crate; re-exported so the engine keeps naming them locally and
// modules importing `crate::engine::db::{ChanError, ...}` are unaffected.
pub use echo_api::{
AccountView, AjoinView, AkillView, BotView, Caps, ForbidKind, ForbidView, GroupView, HelpView, IgnoreView, MemoView, NewsView, Privs, ReportView, SuspensionView, ChanAccessView, ChanAkickView, ChanError, ChanSetting, ChannelView, CertError, CodeKind, Kicker, RegError, Store, TriggerView, VhostView, XlineKind,
AccountView, AjoinView, AkillView, BotView, Caps, ForbidKind, ForbidView, GroupView, HelpView, IgnoreView, MemoView, NewsKind, NewsView, Privs, ReportView, SuspensionView, ChanAccessView, ChanAkickView, ChanError, ChanSetting, ChannelView, CertError, CodeKind, Kicker, RegError, Store, TriggerView, VhostView, XlineKind,
};
#[derive(Debug, Clone, Serialize, Deserialize)]

View file

@ -160,13 +160,14 @@ impl Db {
.map(|g| Privs::from_names(&g.privs))
}
/// Add a news item of `kind` ("logon"/"oper"). Returns its stable id.
pub fn news_add(&mut self, kind: &str, text: &str, setter: &str) -> u64 {
/// Add a news item of `kind`. Returns its stable id.
pub fn news_add(&mut self, kind: NewsKind, text: &str, setter: &str) -> u64 {
let id = self.net.news_seq;
let ts = now();
let _ = self.log.append(Event::NewsAdded { id, kind: kind.to_string(), text: text.to_string(), setter: setter.to_string(), ts });
let wire = kind.wire();
let _ = self.log.append(Event::NewsAdded { id, kind: wire.to_string(), text: text.to_string(), setter: setter.to_string(), ts });
self.net.news_seq = id + 1;
self.net.news.push(News { id, kind: kind.to_string(), text: text.to_string(), setter: setter.to_string(), ts });
self.net.news.push(News { id, kind: wire.to_string(), text: text.to_string(), setter: setter.to_string(), ts });
id
}
@ -181,11 +182,12 @@ impl Db {
}
/// The news items of `kind`, oldest first.
pub fn news(&self, kind: &str) -> Vec<NewsView> {
pub fn news(&self, kind: NewsKind) -> Vec<NewsView> {
let wire = kind.wire();
self.net
.news
.iter()
.filter(|n| n.kind == kind)
.filter(|n| n.kind == wire)
.map(|n| NewsView { id: n.id, text: n.text.clone(), setter: n.setter.clone(), ts: n.ts })
.collect()
}

View file

@ -281,13 +281,13 @@ impl Store for Db {
fn channel_note(&self, channel: &str) -> Option<String> {
Db::channel_note(self, channel)
}
fn news_add(&mut self, kind: &str, text: &str, setter: &str) -> u64 {
fn news_add(&mut self, kind: NewsKind, text: &str, setter: &str) -> u64 {
Db::news_add(self, kind, text, setter)
}
fn news_del(&mut self, id: u64) -> bool {
Db::news_del(self, id)
}
fn news(&self, kind: &str) -> Vec<NewsView> {
fn news(&self, kind: NewsKind) -> Vec<NewsView> {
Db::news(self, kind)
}
fn report_file(&mut self, reporter: &str, target: &str, reason: &str) -> Option<u64> {

View file

@ -568,8 +568,8 @@
vf.sort();
let mut jp = db.jupes();
jp.sort();
let mut nw = db.news("logon");
nw.extend(db.news("oper"));
let mut nw = db.news(NewsKind::Logon);
nw.extend(db.news(NewsKind::Oper));
let rp = db.reports(false);
let hp = db.help_tickets(false);
let mut se = db.session_exceptions();
@ -626,8 +626,8 @@
db.set_account_note("bob", Some("watch this one".into()));
db.set_channel_note("#chan", Some("chan note".into()));
db.set_email("carol", Some("c@x.io".into())).unwrap();
db.news_add("logon", "welcome all", "oper");
db.news_add("oper", "staff notice", "oper");
db.news_add(NewsKind::Logon, "welcome all", "oper");
db.news_add(NewsKind::Oper, "staff notice", "oper");
let rid = db.report_file("carol", "bob", "spam").unwrap();
db.report_close(rid);
let hid = db.help_request("carol", "help me").unwrap();
@ -644,7 +644,7 @@
db.akill_del(XlineKind::Qline, "spam*").unwrap();
db.forbid_add(ForbidKind::Chan, "#evil*", "oper", "bad").unwrap();
db.forbid_del(ForbidKind::Chan, "#evil*").unwrap();
let nid = db.news_add("logon", "temp notice", "oper");
let nid = db.news_add(NewsKind::Logon, "temp notice", "oper");
db.news_del(nid);
db.certfp_add("bob", "1122334455667788990011223344556677889900").unwrap();
db.certfp_del("bob", "1122334455667788990011223344556677889900").unwrap();

View file

@ -10,7 +10,7 @@ use base64::{engine::general_purpose::STANDARD, Engine as _};
use tokio::sync::mpsc;
use crate::proto::{AuthThen, NetAction, NetEvent, RegReply};
use db::{Db, LogEntry, RegError};
use db::{Db, LogEntry, NewsKind, RegError};
use scram::Verifier;
use echo_api::Privs;
use service::{Sender, Service, ServiceCtx};
@ -754,7 +754,7 @@ impl Engine {
// The news items of `kind` as server-sourced notices to `uid`, each tagged
// with `label` so it reads as an announcement. Empty if we have no SID yet.
fn news_notices(&self, kind: &str, label: &str, uid: &str) -> Vec<NetAction> {
fn news_notices(&self, kind: NewsKind, label: &str, uid: &str) -> Vec<NetAction> {
if self.sid.is_empty() {
return Vec::new();
}
@ -1100,7 +1100,7 @@ impl Engine {
// Greet with the logon news, and — separately — prompt-then-
// enforce if they arrived on a registered nick they aren't
// identified to.
let mut out = self.news_notices("logon", "News", &uid);
let mut out = self.news_notices(NewsKind::Logon, "News", &uid);
out.extend(self.enforce_registered_nick(&uid, &arriving_nick));
// A user already logged in the moment they connect (SASL during
// registration) never ran the IDENTIFY path, so give them their
@ -1446,7 +1446,7 @@ impl Engine {
self.db.mark_account_seen(value, self.now_secs());
// An operator logging in is shown the staff (oper) news.
if self.oper_privs(value).any() {
for note in self.news_notices("oper", "Staff News", target) {
for note in self.news_notices(NewsKind::Oper, "Staff News", target) {
self.emit_irc(note);
}
}

View file

@ -5160,10 +5160,10 @@ fn infoserv_posts_a_bulletin_admin_only() {
svc_login(&mut e, "alice");
// Not an oper yet: refused.
assert!(svc_ask(&mut e, "42SAAAAAJ", "POST scheduled maintenance tonight").iter().any(|l| l.contains("Access denied")));
assert_eq!(e.db.news("logon").len(), 0);
assert_eq!(e.db.news(NewsKind::Logon).len(), 0);
svc_oper(&mut e, "alice");
svc_ask(&mut e, "42SAAAAAJ", "POST scheduled maintenance tonight");
assert_eq!(e.db.news("logon").len(), 1, "bulletin posted");
assert_eq!(e.db.news(NewsKind::Logon).len(), 1, "bulletin posted");
}
#[test]