wayBetter for X

This commit is contained in:
CRTkafa 2026-10-01 06:01:13 +07:00
commit df5dff619e
720 changed files with 147465 additions and 0 deletions

View file

@ -0,0 +1,116 @@
//! Power User Mode (deck columns), X side: which feature flags a column may override, who may
//! configure a column and which unread reports are accepted. Headless, so the rules are tested
//! without the desktop feature; `desktop.rs` wires them to the Tauri commands.
//!
//! Owner-approved (2026-09-28): `tab_report` may carry `unread` (a number about the tab itself,
//! capped at 999), accepted only from `tab-*` webviews that are on x.com right now; the shell-only
//! `column_configure` hands a column its config (validated here, data only).
use crate::{policy, site::SITE};
use url::Url;
use waybetter_host::tabs::{self, ColumnConfig};
/// Feature flags a column may set over the global ones (see `src/inject/features.ts`). Only
/// display and playback flags plus the navigation-only Following tab switch; none of them sends
/// anything to X.
pub const COLUMN_FLAGS: &[&str] = &[
"disableVideoAutoplay",
"stopVideoLoop",
"hidePromotedPosts",
"hideEngagementCounts",
"hideViewCounts",
"hideVerifiedBadges",
"fullNumbers",
"fixTimelineJump",
"fixNewPostsPill",
"followingTimelineDefault",
"disableTweetTextFormatting",
];
/// `column_configure`: only the trusted local shell may configure a column, and only with a
/// bounded config for a page on x.com. `None` removes the column's config.
pub fn configure(caller_label: &str, caller_url: &Url, development: bool, id: &str, config: Option<ColumnConfig>) -> Result<Option<ColumnConfig>, String> {
policy::require_local(caller_label, caller_url, development)?;
if !policy::valid_tab_id(id) {
return Err("VALIDATION: invalid tab id".into());
}
config.map(|config| config.validated(&SITE, COLUMN_FLAGS)).transpose()
}
/// An unread count reported by a tab: accepted only from a `tab-*` webview on exactly x.com, capped.
pub fn accept_unread(label: &str, url: &Url, unread: Option<u64>) -> Option<u16> {
let unread = unread?;
policy::require_capture(label, url).ok()?;
Some(tabs::cap_unread(unread))
}
/// The script that hands `config` (or null) to the column module of the X runtime, parked in
/// `__btColumn` until the runtime exists.
pub fn column_script(config: Option<&ColumnConfig>) -> String {
let json = serde_json::to_string(&config).unwrap_or_else(|_| "null".into());
tabs::deliver_script(SITE.runtime_global, "column", "set", "__btColumn", &json)
}
#[cfg(test)]
mod tests {
use super::*;
fn url(value: &str) -> Url {
Url::parse(value).unwrap()
}
fn config() -> ColumnConfig {
serde_json::from_value(serde_json::json!({
"url": "https://x.com/notifications", "kind": "notifications", "density": "compact",
"flags": { "disableVideoAutoplay": true, "hidePromotedPosts": true }
}))
.unwrap()
}
#[test]
fn column_configure_is_shell_only() {
let shell = url("http://tauri.localhost/");
assert!(configure("shell", &shell, false, "col1", Some(config())).unwrap().is_some());
assert!(configure("shell", &shell, false, "col1", None).unwrap().is_none(), "null removes");
// An X tab (or any other webview) can never configure a column, not even its own.
for (label, from) in [("tab-col1", "https://x.com/home"), ("popover-1", "http://tauri.localhost/"), ("shell", "https://x.com/")] {
assert!(configure(label, &url(from), false, "col1", Some(config())).is_err(), "{label} {from}");
}
assert!(configure("shell", &shell, false, "bad id", Some(config())).is_err());
}
#[test]
fn column_flags_are_allow_listed() {
let shell = url("http://tauri.localhost/");
let mut bad = config();
bad.flags.insert("fixLikeFollow".into(), false);
assert!(configure("shell", &shell, false, "c", Some(bad)).is_err(), "the like/follow fix is not a column flag");
let mut other_site = config();
other_site.url = "https://twitter.com/home".into();
assert!(configure("shell", &shell, false, "c", Some(other_site)).is_err());
for flag in COLUMN_FLAGS {
assert!(!flag.contains("Follow") || *flag == "followingTimelineDefault", "no follow actions: {flag}");
}
}
#[test]
fn unread_is_accepted_only_from_x_tabs_and_capped() {
let x = url("https://x.com/home");
assert_eq!(accept_unread("tab-c1", &x, Some(4)), Some(4));
assert_eq!(accept_unread("tab-c1", &x, Some(5000)), Some(999));
assert_eq!(accept_unread("tab-c1", &x, None), None);
assert_eq!(accept_unread("shell", &x, Some(4)), None);
assert_eq!(accept_unread("popover-1", &x, Some(4)), None);
for bad in ["https://example.com/", "https://twitter.com/home", "http://x.com/", "https://www.x.com/"] {
assert_eq!(accept_unread("tab-c1", &url(bad), Some(4)), None, "{bad}");
}
}
#[test]
fn column_script_parks_the_config_until_the_runtime_exists() {
let script = column_script(Some(&config()));
assert!(script.starts_with("(()=>{const v={\"url\":\"https://x.com/notifications\""));
assert!(script.contains("R.column.set(v);else window.__btColumn=v;"));
assert!(column_script(None).contains("const v=null;"));
}
}