social/logic.rs now has the contacts as rows (contact_item, people), the buttons of a contact and what they ask the window to do next, the bell (notify, the rows, read, clear, open), the search with what each result opens, and what an engine event does to the friends, the pokes, the offline messages and the notices (event). events::handle runs that first, then events::apply, so a front end has one entry point; Social<D> is the state those keep, with what the messages page keeps as D for now. Refresh got people, conversations and dm, the host got dm_shown and now_ms, and Persist.Settings pages are told apart from the new notices. The Slint side (social/bind.rs) writes the Bridge, shows the desktop toasts and navigates. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
572 lines
18 KiB
Rust
572 lines
18 KiB
Rust
//! Texts of the home, friends, messages, bell and events screens: times
|
||
//! ("12m ago", "Yesterday"), message previews without BBCode (read by
|
||
//! `vm::bbcode`), mentions, event dates, a poke's message, the private
|
||
//! chats of the home sidebar; pure, so they are tested here.
|
||
|
||
pub mod logic;
|
||
|
||
use std::collections::HashSet;
|
||
|
||
use chrono::{DateTime, Local, NaiveDateTime, TimeZone};
|
||
use voelin_core::{HistoryMessage, VoiceState};
|
||
use voelin_model::{ChatMessage, ChatTarget};
|
||
|
||
use crate::state::AppState;
|
||
|
||
/// Now, in milliseconds since the epoch.
|
||
pub fn now_ms() -> i64 {
|
||
chrono::Utc::now().timestamp_millis()
|
||
}
|
||
|
||
/// What a notification of the bell is about.
|
||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||
pub enum NoticeKind {
|
||
/// Someone mentioned us.
|
||
Mention,
|
||
/// Someone poked us.
|
||
Poke,
|
||
/// A private message.
|
||
Message,
|
||
/// A scheduled event starts soon.
|
||
Event,
|
||
/// A friend came online.
|
||
Friend,
|
||
}
|
||
|
||
impl NoticeKind {
|
||
/// The name the bell's rows carry.
|
||
pub fn name(self) -> &'static str {
|
||
match self {
|
||
Self::Mention => "mention",
|
||
Self::Poke => "poke",
|
||
Self::Message => "message",
|
||
Self::Event => "event",
|
||
Self::Friend => "friend",
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Where a notification leads.
|
||
#[derive(Clone, Debug, PartialEq)]
|
||
pub enum NoticeTarget {
|
||
/// A chat of a session.
|
||
Chat(i64, ChatTarget),
|
||
/// A private chat with someone, in a session.
|
||
Dm(i64, String),
|
||
/// An event of a session's gateway (session, event id).
|
||
Event(i64, i64),
|
||
/// A contact, by unique id.
|
||
Contact(String),
|
||
}
|
||
|
||
/// A notification a transition asks for; the front end adds it to the bell
|
||
/// as the setting of its kind says.
|
||
#[derive(Clone, Debug, PartialEq)]
|
||
pub struct NoticeRequest {
|
||
/// What it is about.
|
||
pub kind: NoticeKind,
|
||
/// The headline.
|
||
pub title: String,
|
||
/// The line under it.
|
||
pub body: String,
|
||
/// Where it leads.
|
||
pub target: NoticeTarget,
|
||
/// Whose avatar it shows (initials from the name).
|
||
pub name: String,
|
||
/// Whose avatar picture it shows.
|
||
pub uid: Option<String>,
|
||
}
|
||
|
||
/// Whether a message is ours: by one of our unique ids, or by our client in
|
||
/// its session (`own_client`).
|
||
pub fn own_message(
|
||
own_uids: &HashSet<String>,
|
||
own_client: Option<u16>,
|
||
message: &ChatMessage,
|
||
) -> bool {
|
||
message.author_uid.as_ref().is_some_and(|u| own_uids.contains(u))
|
||
|| (own_client.is_some() && message.author_id == own_client)
|
||
}
|
||
|
||
/// A poke we got.
|
||
#[derive(Clone, Debug, PartialEq)]
|
||
pub struct Poke {
|
||
/// The session it came in.
|
||
pub session: i64,
|
||
/// Who poked, by unique id, when known.
|
||
pub uid: Option<String>,
|
||
/// Who poked, by name.
|
||
pub name: String,
|
||
/// What they wrote with it.
|
||
pub message: String,
|
||
/// When, in milliseconds since the epoch.
|
||
pub ts_ms: i64,
|
||
}
|
||
|
||
/// The avatar picture of someone by unique id, from any session.
|
||
pub fn avatar_of<L>(state: &AppState<L>, uid: &str) -> crate::image::ImageRef {
|
||
crate::avatar::image(state.sessions.values().find_map(|v| v.avatars.get(uid)))
|
||
}
|
||
|
||
/// Pokes from `uid` as lines of their private chat, among its messages
|
||
/// (with the time of each, oldest first).
|
||
pub fn poke_lines<L>(
|
||
state: &AppState<L>,
|
||
pokes: &[Poke],
|
||
session: i64,
|
||
uid: &str,
|
||
) -> Vec<(i64, crate::bridge::ChatLine)> {
|
||
pokes
|
||
.iter()
|
||
.enumerate()
|
||
.filter(|(_, p)| p.session == session && p.uid.as_deref() == Some(uid))
|
||
.map(|(i, p)| {
|
||
let text = if p.message.is_empty() {
|
||
"👋 poked you".to_owned()
|
||
} else {
|
||
format!("👋 poked you: {}", p.message)
|
||
};
|
||
let message = ChatMessage {
|
||
target: ChatTarget::Private(uid.to_owned()),
|
||
author_name: p.name.clone(),
|
||
author_uid: p.uid.clone(),
|
||
author_id: None,
|
||
text,
|
||
ts_ms: p.ts_ms,
|
||
via_relay: false,
|
||
blocked: false,
|
||
};
|
||
let mut line = crate::chat::line(&message, None);
|
||
line.key = -1_000_000 - i as i32;
|
||
line.avatar = avatar_of(state, uid);
|
||
(p.ts_ms, line)
|
||
})
|
||
.collect()
|
||
}
|
||
|
||
/// Whether a message of a session is ours.
|
||
pub fn is_own<L>(state: &AppState<L>, session: i64, message: &ChatMessage) -> bool {
|
||
let own_client = state.sessions.get(&session).and_then(|v| v.state.own_client);
|
||
own_message(&state.own_uids, own_client, message)
|
||
}
|
||
|
||
/// Where someone is in one session.
|
||
#[derive(Clone, Debug, PartialEq)]
|
||
pub struct Spot {
|
||
/// The session.
|
||
pub session: i64,
|
||
/// The server's name.
|
||
pub server: String,
|
||
/// The client's id there.
|
||
pub client: u16,
|
||
/// The channel it is in.
|
||
pub channel: u64,
|
||
/// The channel's name as shown (a spacer's text).
|
||
pub channel_title: String,
|
||
/// Its away message, if it is away.
|
||
pub away: Option<String>,
|
||
/// It streams.
|
||
pub streaming: bool,
|
||
/// We are connected with voice there (messages, pokes, moving).
|
||
pub voice: bool,
|
||
}
|
||
|
||
/// A server's name: its bookmark's, else what the session calls it.
|
||
pub fn server_name<L>(state: &AppState<L>, session: i64) -> String {
|
||
state.bookmarks.iter().find(|b| b.id == session).map(|b| b.name.clone()).unwrap_or_else(|| {
|
||
state.sessions.get(&session).map(|v| v.presence.server_name.clone()).unwrap_or_default()
|
||
})
|
||
}
|
||
|
||
/// Every session where the client with this unique id is, in bookmark
|
||
/// order.
|
||
pub fn spots_of<L>(state: &AppState<L>, uid: &str) -> Vec<Spot> {
|
||
let mut ids: Vec<i64> = state.sessions.keys().copied().collect();
|
||
ids.sort_by_key(|id| state.bookmarks.iter().position(|b| b.id == *id).unwrap_or(usize::MAX));
|
||
let mut spots = Vec::new();
|
||
for id in ids {
|
||
let view = &state.sessions[&id];
|
||
let Some(c) = view.presence.clients.values().find(|c| c.uid.as_deref() == Some(uid)) else {
|
||
continue;
|
||
};
|
||
let channel = view.presence.channels.get(&c.channel);
|
||
spots.push(Spot {
|
||
session: id,
|
||
server: server_name(state, id),
|
||
client: c.id,
|
||
channel: c.channel,
|
||
channel_title: channel
|
||
.map(|ch| crate::tree::channel_title(ch).0.to_owned())
|
||
.unwrap_or_default(),
|
||
away: c.away.clone(),
|
||
streaming: c.streaming == Some(true),
|
||
voice: view.state.voice == VoiceState::Connected,
|
||
});
|
||
}
|
||
spots
|
||
}
|
||
|
||
/// What we call the peer of a private chat: its contact's nickname, the name
|
||
/// it has where it is now, the name on a message of theirs (`last`, or one in
|
||
/// any open chat), else the start of its unique id.
|
||
pub fn peer_name<L>(state: &AppState<L>, uid: &str, last: Option<&HistoryMessage>) -> String {
|
||
if let Some(c) = state.contacts.get(uid).filter(|c| !c.nickname.is_empty()) {
|
||
return c.nickname.clone();
|
||
}
|
||
if let Some(s) = spots_of(state, uid).first() {
|
||
return state.sessions[&s.session].nickname(s.client);
|
||
}
|
||
let from_peer = |m: &HistoryMessage| {
|
||
(m.message.author_uid.as_deref() == Some(uid)).then(|| m.message.author_name.clone())
|
||
};
|
||
last.and_then(from_peer)
|
||
.or_else(|| {
|
||
state.sessions.values().flat_map(|v| &v.tabs).find_map(|t| {
|
||
(t.target == ChatTarget::Private(uid.to_owned()))
|
||
.then(|| t.messages.iter().rev().find_map(|m| from_peer(&m.message)))
|
||
.flatten()
|
||
})
|
||
})
|
||
.unwrap_or_else(|| uid.chars().take(8).collect())
|
||
}
|
||
|
||
/// A chat of any server.
|
||
#[derive(Clone, Debug, PartialEq)]
|
||
pub struct ChatRef {
|
||
/// The bookmark (session) it belongs to, when known.
|
||
pub session: Option<i64>,
|
||
/// The server's unique id.
|
||
pub server_uid: Option<String>,
|
||
/// Which chat of the server.
|
||
pub target: ChatTarget,
|
||
/// Its newest message.
|
||
pub last: Option<HistoryMessage>,
|
||
/// Messages not read.
|
||
pub unread: i32,
|
||
}
|
||
|
||
fn local(ts_ms: i64) -> Option<NaiveDateTime> {
|
||
DateTime::from_timestamp_millis(ts_ms).map(|t| t.with_timezone(&Local).naive_local())
|
||
}
|
||
|
||
fn now_local() -> NaiveDateTime {
|
||
Local::now().naive_local()
|
||
}
|
||
|
||
/// "now", "5m ago", "2h ago", "3d ago", else the date ("12 Mar").
|
||
pub fn ago(ts_ms: i64) -> String {
|
||
ago_at(ts_ms, chrono::Utc::now().timestamp_millis())
|
||
}
|
||
|
||
/// [`ago`] for a given time `now_ms`.
|
||
pub fn ago_at(ts_ms: i64, now_ms: i64) -> String {
|
||
if ts_ms <= 0 {
|
||
return String::new();
|
||
}
|
||
let minutes = (now_ms - ts_ms) / 60_000;
|
||
match minutes {
|
||
i64::MIN..=0 => "now".into(),
|
||
1..=59 => format!("{minutes}m ago"),
|
||
60..=1439 => format!("{}h ago", minutes / 60),
|
||
1440..=10079 => format!("{}d ago", minutes / 1440),
|
||
_ => local(ts_ms).map(|t| t.format("%-d %b").to_string()).unwrap_or_default(),
|
||
}
|
||
}
|
||
|
||
/// The time of a conversation in a list: "11:42" today, "Yesterday", the
|
||
/// weekday within a week ("Mon"), else the date ("12 Mar", with the year
|
||
/// when it is another).
|
||
pub fn list_time(ts_ms: i64) -> String {
|
||
local(ts_ms).map(|t| list_time_at(t, now_local())).unwrap_or_default()
|
||
}
|
||
|
||
/// [`list_time`] for a given local time `now`.
|
||
pub fn list_time_at(then: NaiveDateTime, now: NaiveDateTime) -> String {
|
||
use chrono::Datelike;
|
||
let days = (now.date() - then.date()).num_days();
|
||
match days {
|
||
i64::MIN..=0 => then.format("%H:%M").to_string(),
|
||
1 => "Yesterday".into(),
|
||
2..=6 => then.format("%a").to_string(),
|
||
_ if then.year() == now.year() => then.format("%-d %b").to_string(),
|
||
_ => then.format("%-d %b %Y").to_string(),
|
||
}
|
||
}
|
||
|
||
/// Chat text without BBCode (`[b]`, `[URL=…]`, `[/URL]`), on one line.
|
||
pub fn plain(text: &str) -> String {
|
||
without_emoji(&crate::bbcode::parse(text).one_line())
|
||
}
|
||
|
||
/// Text without emoji, runs of spaces as one: one line of text draws no
|
||
/// emoji (they are pictures in the chat).
|
||
pub fn without_emoji(text: &str) -> String {
|
||
let mut words = String::with_capacity(text.len());
|
||
for run in crate::emoji::split(text).into_iter().filter(|r| r.emoji.is_none()) {
|
||
words.extend(run.text.chars().filter(|c| !is_emoji(*c)));
|
||
}
|
||
words.split_whitespace().collect::<Vec<_>>().join(" ")
|
||
}
|
||
|
||
/// Pictographs, symbols and their joiners and variation selectors.
|
||
fn is_emoji(c: char) -> bool {
|
||
matches!(u32::from(c), 0x1F000..=0x1FAFF | 0x2600..=0x27BF | 0xFE0F | 0x200D)
|
||
}
|
||
|
||
/// A one-line preview of a message: "You: …" for our own, "Sent a
|
||
/// picture" / "Sent a file" for links without text.
|
||
pub fn preview(text: &str, mine: bool, files: &[voelin_model::FileRef]) -> String {
|
||
let body = if files.is_empty() { plain(text) } else { String::new() };
|
||
let body = if body.is_empty() && !files.is_empty() {
|
||
if files.iter().any(|f| crate::chat::is_picture(&f.name)) {
|
||
"Sent a picture".to_owned()
|
||
} else {
|
||
"Sent a file".to_owned()
|
||
}
|
||
} else {
|
||
body
|
||
};
|
||
if mine { format!("You: {body}") } else { body }
|
||
}
|
||
|
||
/// Whether `text` names `nick` as a word (case does not matter): a mention.
|
||
pub fn mentions(text: &str, nick: &str) -> bool {
|
||
let nick = nick.trim().to_lowercase();
|
||
if nick.is_empty() {
|
||
return false;
|
||
}
|
||
let text = text.to_lowercase();
|
||
text.match_indices(&nick).any(|(at, _)| {
|
||
let before = text[..at].chars().next_back();
|
||
let after = text[at + nick.len()..].chars().next();
|
||
!before.is_some_and(char::is_alphanumeric) && !after.is_some_and(char::is_alphanumeric)
|
||
})
|
||
}
|
||
|
||
/// Case-insensitive search in any of `fields`.
|
||
pub fn matches(query: &str, fields: &[&str]) -> bool {
|
||
let q = query.trim().to_lowercase();
|
||
q.is_empty() || fields.iter().any(|f| f.to_lowercase().contains(&q))
|
||
}
|
||
|
||
/// A poke's message as it is sent: trimmed, and cut to the
|
||
/// [`voelin_core::POKE_MESSAGE_MAX`] characters servers take.
|
||
pub fn poke_message(text: &str) -> String {
|
||
let cut: String = text.trim().chars().take(voelin_core::POKE_MESSAGE_MAX).collect();
|
||
cut.trim_end().to_owned()
|
||
}
|
||
|
||
/// The private chats of the home sidebar, by their index in `chats`
|
||
/// (newest first): at most `n`, those with unread messages first.
|
||
pub fn sidebar_dms(chats: &[ChatRef], n: usize) -> Vec<usize> {
|
||
let mut private: Vec<usize> = (0..chats.len())
|
||
.filter(|&i| matches!(chats[i].target, voelin_model::ChatTarget::Private(_)))
|
||
.collect();
|
||
private.sort_by_key(|&i| chats[i].unread == 0);
|
||
private.truncate(n);
|
||
private
|
||
}
|
||
|
||
/// The channels the search (Ctrl+K) finds for `query`, with their titles:
|
||
/// a spacer by its text, never a line or an empty spacer.
|
||
pub fn found_channels<'a>(
|
||
channels: impl IntoIterator<Item = &'a voelin_model::ChannelInfo>,
|
||
query: &str,
|
||
) -> Vec<(u64, &'a str)> {
|
||
channels
|
||
.into_iter()
|
||
.filter_map(|c| Some((c.id, crate::tree::listed_title(c)?)))
|
||
.filter(|(_, title)| matches(query, &[*title]))
|
||
.collect()
|
||
}
|
||
|
||
/// The date block of an event: "SAT", "26", "APR".
|
||
pub fn date_block(ts_ms: i64) -> (String, String, String) {
|
||
local(ts_ms)
|
||
.map(|t| {
|
||
(
|
||
t.format("%a").to_string().to_uppercase(),
|
||
t.format("%-d").to_string(),
|
||
t.format("%b").to_string().to_uppercase(),
|
||
)
|
||
})
|
||
.unwrap_or_default()
|
||
}
|
||
|
||
/// "Sat 26 Apr, 19:00 – 21:00" (the end's date too when it is another day).
|
||
pub fn event_when(start_ms: i64, end_ms: Option<i64>) -> String {
|
||
let Some(start) = local(start_ms) else { return String::new() };
|
||
let mut out = start.format("%a %-d %b, %H:%M").to_string();
|
||
if let Some(end) = end_ms.and_then(local) {
|
||
let end = if end.date() == start.date() {
|
||
end.format("%H:%M")
|
||
} else {
|
||
end.format("%a %-d %b, %H:%M")
|
||
};
|
||
out.push_str(&format!(" – {end}"));
|
||
}
|
||
out
|
||
}
|
||
|
||
/// "Starts in 15 min", "Starts in 3 h", "Happening now", or nothing (more
|
||
/// than a day away, or over).
|
||
pub fn soon(start_ms: i64, end_ms: Option<i64>, now_ms: i64) -> String {
|
||
let minutes = (start_ms - now_ms) / 60_000;
|
||
let over = end_ms.map_or(now_ms - start_ms > 3 * 3_600_000, |end| now_ms > end);
|
||
match minutes {
|
||
_ if over => String::new(),
|
||
i64::MIN..=0 => "Happening now".into(),
|
||
1..=59 => format!("Starts in {minutes} min"),
|
||
60..=1439 => format!("Starts in {} h", minutes / 60),
|
||
_ => String::new(),
|
||
}
|
||
}
|
||
|
||
/// `2026-10-04 19:00` in local time → Unix milliseconds.
|
||
pub fn parse_local(text: &str) -> Option<i64> {
|
||
let t = NaiveDateTime::parse_from_str(text.trim(), "%Y-%m-%d %H:%M").ok()?;
|
||
Local.from_local_datetime(&t).earliest().map(|t| t.timestamp_millis())
|
||
}
|
||
|
||
/// Unix milliseconds → `2026-10-04 19:00` in local time.
|
||
pub fn format_local(ts_ms: i64) -> String {
|
||
local(ts_ms).map(|t| t.format("%Y-%m-%d %H:%M").to_string()).unwrap_or_default()
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use super::*;
|
||
|
||
fn at(text: &str) -> NaiveDateTime {
|
||
NaiveDateTime::parse_from_str(text, "%Y-%m-%d %H:%M").unwrap()
|
||
}
|
||
|
||
#[test]
|
||
fn times() {
|
||
let now = 1_800_000_000_000;
|
||
assert_eq!(ago_at(now - 30_000, now), "now");
|
||
assert_eq!(ago_at(now - 12 * 60_000, now), "12m ago");
|
||
assert_eq!(ago_at(now - 2 * 3_600_000, now), "2h ago");
|
||
assert_eq!(ago_at(now - 3 * 86_400_000, now), "3d ago");
|
||
assert_eq!(ago_at(0, now), "");
|
||
let now = at("2026-10-03 14:00");
|
||
assert_eq!(list_time_at(at("2026-10-03 11:42"), now), "11:42");
|
||
assert_eq!(list_time_at(at("2026-10-02 23:00"), now), "Yesterday");
|
||
assert_eq!(list_time_at(at("2026-09-28 10:00"), now), "Mon");
|
||
assert_eq!(list_time_at(at("2026-03-12 10:00"), now), "12 Mar");
|
||
assert_eq!(list_time_at(at("2025-03-12 10:00"), now), "12 Mar 2025");
|
||
}
|
||
|
||
#[test]
|
||
fn previews() {
|
||
assert_eq!(plain("[b]Hi[/b] there"), "Hi there");
|
||
assert_eq!(plain("see [URL=https://x.org]this[/URL]"), "see this");
|
||
assert_eq!(plain("a [not a tag here] b"), "a [not a tag here] b");
|
||
assert_eq!(plain("[1] item"), "[1] item");
|
||
assert_eq!(plain("later? 👀 Posting ❤️"), "later? Posting");
|
||
// Only known tags go.
|
||
assert_eq!(plain("[nick] hi"), "[nick] hi");
|
||
assert_eq!(plain("[quote=Mira]raid?[/quote]\nyes [i]now[/i]"), "raid? yes now");
|
||
assert_eq!(preview("Hello", true, &[]), "You: Hello");
|
||
let file = |name: &str| voelin_model::FileRef { name: name.into(), ..Default::default() };
|
||
assert_eq!(
|
||
preview("[URL=ts3file://x]a.png[/URL]", false, &[file("a.png")]),
|
||
"Sent a picture"
|
||
);
|
||
assert_eq!(preview("x", true, &[file("notes.txt")]), "You: Sent a file");
|
||
}
|
||
|
||
#[test]
|
||
fn mention_words() {
|
||
assert!(mentions("hey Nova, raid?", "nova"));
|
||
assert!(mentions("@Nova", "Nova"));
|
||
assert!(!mentions("supernova", "Nova"));
|
||
assert!(!mentions("anything", ""));
|
||
assert!(matches("kai", &["Kairo", "x"]));
|
||
assert!(matches("", &[]));
|
||
assert!(!matches("zz", &["Kairo"]));
|
||
}
|
||
|
||
#[test]
|
||
fn poke_messages() {
|
||
assert_eq!(poke_message(" Raid in 5? "), "Raid in 5?");
|
||
assert_eq!(poke_message(""), "");
|
||
assert_eq!(poke_message(" \n "), "");
|
||
let exact = "x".repeat(100);
|
||
assert_eq!(poke_message(&exact), exact);
|
||
// Characters, not bytes: two bytes each.
|
||
assert_eq!(poke_message(&"ä".repeat(150)), "ä".repeat(100));
|
||
assert_eq!(poke_message(&format!("{}€!", "ü".repeat(99))), format!("{}€", "ü".repeat(99)));
|
||
// A cut that ends in a space leaves it out.
|
||
assert_eq!(poke_message(&format!("{} more", "a".repeat(99))), "a".repeat(99));
|
||
}
|
||
|
||
/// The search shows spacers by their text and leaves out lines.
|
||
#[test]
|
||
fn search_channels() {
|
||
let channel = |id, parent, name: &str| voelin_model::ChannelInfo {
|
||
id,
|
||
parent,
|
||
name: name.into(),
|
||
..Default::default()
|
||
};
|
||
let channels = [
|
||
channel(1, 0, "[cspacer0]Gaming"),
|
||
channel(2, 0, "[*spacer]---"),
|
||
channel(3, 0, "[spacer1]"),
|
||
// Only top-level channels are spacers.
|
||
channel(4, 1, "[cspacer]Game Night"),
|
||
channel(5, 0, "Lobby"),
|
||
];
|
||
assert_eq!(found_channels(&channels, "gam"), [(1, "Gaming"), (4, "[cspacer]Game Night")]);
|
||
assert!(found_channels(&channels, "-").is_empty());
|
||
// A spacer is not found by its prefix; a sub-channel's name is all
|
||
// its own.
|
||
assert_eq!(found_channels(&channels, "spacer"), [(4, "[cspacer]Game Night")]);
|
||
assert_eq!(found_channels(&channels, "").len(), 3);
|
||
}
|
||
|
||
#[test]
|
||
fn sidebar_chats() {
|
||
use voelin_model::ChatTarget;
|
||
let chat = |target: ChatTarget, unread| ChatRef {
|
||
session: Some(1),
|
||
server_uid: None,
|
||
target,
|
||
last: None,
|
||
unread,
|
||
};
|
||
let dm = |uid: &str, unread| chat(ChatTarget::Private(uid.into()), unread);
|
||
let chats = [
|
||
dm("a", 0),
|
||
chat(ChatTarget::Server, 4),
|
||
dm("b", 2),
|
||
chat(ChatTarget::Channel(3), 1),
|
||
dm("c", 0),
|
||
dm("d", 1),
|
||
dm("e", 0),
|
||
dm("f", 0),
|
||
];
|
||
// Private chats only, unread first, otherwise newest first; capped.
|
||
assert_eq!(sidebar_dms(&chats, 5), [2, 5, 0, 4, 6]);
|
||
assert_eq!(sidebar_dms(&chats, 1), [2]);
|
||
assert!(sidebar_dms(&chats[1..2], 5).is_empty());
|
||
}
|
||
|
||
#[test]
|
||
fn event_dates() {
|
||
let start = parse_local("2026-04-25 19:00").unwrap();
|
||
assert_eq!(format_local(start), "2026-04-25 19:00");
|
||
assert_eq!(date_block(start), ("SAT".into(), "25".into(), "APR".into()));
|
||
let end = parse_local("2026-04-25 21:30").unwrap();
|
||
assert_eq!(event_when(start, Some(end)), "Sat 25 Apr, 19:00 – 21:30");
|
||
assert_eq!(event_when(start, None), "Sat 25 Apr, 19:00");
|
||
assert!(parse_local("tomorrow").is_none());
|
||
assert_eq!(soon(start, Some(end), start - 15 * 60_000), "Starts in 15 min");
|
||
assert_eq!(soon(start, Some(end), start - 3 * 3_600_000), "Starts in 3 h");
|
||
assert_eq!(soon(start, Some(end), start + 60_000), "Happening now");
|
||
assert_eq!(soon(start, Some(end), end + 60_000), "");
|
||
assert_eq!(soon(start, None, start - 2 * 86_400_000), "");
|
||
}
|
||
}
|