refactor: The update checker has become the announcements checker

Replaces June's endpoint with a continuwuity endpoint.
Adds a JSON schema.

Closes #89
Closes #760
This commit is contained in:
Jade Ellis 2025-04-20 19:30:02 +01:00
parent 5486dbda24
commit 2d9bdc0979
No known key found for this signature in database
GPG key ID: 8705A2A3EBF77BD2
12 changed files with 238 additions and 166 deletions

View file

@ -36,9 +36,13 @@ jobs:
- name: Prepare static files for deployment
run: |
mkdir -p ./public/.well-known/matrix
mkdir -p ./public/.well-known/continuwuity
mkdir -p ./public/schema
# Copy the Matrix .well-known files
cp ./docs/static/server ./public/.well-known/matrix/server
cp ./docs/static/client ./public/.well-known/matrix/client
cp ./docs/static/announcements.json ./public/.well-known/continuwuity/announcements
cp ./docs/static/announcements.schema.json ./public/schema/announcements.schema.json
# Copy the custom headers file
cp ./docs/static/_headers ./public/_headers
echo "Copied .well-known files and _headers to ./public"

View file

@ -113,14 +113,10 @@
#new_user_displayname_suffix = "🏳️‍⚧️"
# If enabled, conduwuit will send a simple GET request periodically to
# `https://pupbrain.dev/check-for-updates/stable` for any new
# announcements made. Despite the name, this is not an update check
# endpoint, it is simply an announcement check endpoint.
# `https://continuwuity.org/.well-known/continuwuity/announcements` for any new
# announcements or major updates. This is not an update check endpoint.
#
# This is disabled by default as this is rarely used except for security
# updates or major updates.
#
#allow_check_for_updates = false
#allow_announcements_check =
# Set this to any float value to multiply conduwuit's in-memory LRU caches
# with such as "auth_chain_cache_capacity".

View file

@ -1,3 +1,6 @@
/.well-known/matrix/*
Access-Control-Allow-Origin: *
Content-Type: application/json
/.well-known/continuwuity/*
Access-Control-Allow-Origin: *
Content-Type: application/json

9
docs/static/announcements.json vendored Normal file
View file

@ -0,0 +1,9 @@
{
"$schema": "https://continuwuity.org/schema/announcements.schema.json",
"announcements": [
{
"id": 1,
"message": "Welcome to Continuwuity! Important announcements about the project will appear here."
}
]
}

31
docs/static/announcements.schema.json vendored Normal file
View file

@ -0,0 +1,31 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"$id": "https://continwuity.org/schema/announcements.schema.json",
"type": "object",
"properties": {
"updates": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"message": {
"type": "string"
},
"date": {
"type": "string"
}
},
"required": [
"id",
"message"
]
}
}
},
"required": [
"updates"
]
}

View file

@ -11,7 +11,7 @@ pub(crate) enum GlobalsCommand {
CurrentCount,
LastCheckForUpdatesId,
LastCheckForAnnouncementsId,
/// - This returns an empty `Ok(BTreeMap<..>)` when there are no keys found
/// for the server.
@ -39,9 +39,12 @@ pub(super) async fn process(subcommand: GlobalsCommand, context: &Context<'_>) -
write!(context, "Query completed in {query_time:?}:\n\n```rs\n{results:#?}\n```")
},
| GlobalsCommand::LastCheckForUpdatesId => {
| GlobalsCommand::LastCheckForAnnouncementsId => {
let timer = tokio::time::Instant::now();
let results = services.updates.last_check_for_updates_id().await;
let results = services
.announcements
.last_check_for_announcements_id()
.await;
let query_time = timer.elapsed();
write!(context, "Query completed in {query_time:?}:\n\n```rs\n{results:#?}\n```")

View file

@ -161,14 +161,10 @@ pub struct Config {
pub new_user_displayname_suffix: String,
/// If enabled, conduwuit will send a simple GET request periodically to
/// `https://pupbrain.dev/check-for-updates/stable` for any new
/// announcements made. Despite the name, this is not an update check
/// endpoint, it is simply an announcement check endpoint.
///
/// This is disabled by default as this is rarely used except for security
/// updates or major updates.
#[serde(default, alias = "allow_announcements_check")]
pub allow_check_for_updates: bool,
/// `https://continuwuity.org/.well-known/continuwuity/announcements` for any new
/// announcements or major updates. This is not an update check endpoint.
#[serde(alias = "allow_check_for_updates", default = "true_fn")]
pub allow_announcements_check: bool,
/// Set this to any float value to multiply conduwuit's in-memory LRU caches
/// with such as "auth_chain_cache_capacity".

View file

@ -0,0 +1,169 @@
//! # Announcements service
//!
//! This service is responsible for checking for announcements and sending them
//! to the client.
//!
//! It is used to send announcements to the admin room and logs.
//! Annuncements are stored in /docs/static/announcements right now.
//! The highest seen announcement id is stored in the database. When the
//! announcement check is run, all announcements with an ID higher than those
//! seen before are printed to the console and sent to the admin room.
//!
//! Old announcements should be deleted to avoid spamming the room on first
//! install.
//!
//! Announcements are displayed as markdown in the admin room, but plain text in
//! the console.
use std::{sync::Arc, time::Duration};
use async_trait::async_trait;
use conduwuit::{Result, Server, debug, info, warn};
use database::{Deserialized, Map};
use ruma::events::room::message::RoomMessageEventContent;
use serde::Deserialize;
use tokio::{
sync::Notify,
time::{MissedTickBehavior, interval},
};
use crate::{Dep, admin, client, globals};
pub struct Service {
interval: Duration,
interrupt: Notify,
db: Arc<Map>,
services: Services,
}
struct Services {
admin: Dep<admin::Service>,
client: Dep<client::Service>,
globals: Dep<globals::Service>,
server: Arc<Server>,
}
#[derive(Debug, Deserialize)]
struct CheckForAnnouncementsResponse {
announcements: Vec<CheckForAnnouncementsResponseEntry>,
}
#[derive(Debug, Deserialize)]
struct CheckForAnnouncementsResponseEntry {
id: u64,
date: Option<String>,
message: String,
}
const CHECK_FOR_ANNOUNCEMENTS_URL: &str =
"https://continuwuity.org/.well-known/continuwuity/announcements";
const CHECK_FOR_ANNOUNCEMENTS_INTERVAL: u64 = 7200; // 2 hours
const LAST_CHECK_FOR_ANNOUNCEMENTS_ID: &[u8; 25] = b"last_seen_announcement_id";
// In conduwuit, this was under b"a"
#[async_trait]
impl crate::Service for Service {
fn build(args: crate::Args<'_>) -> Result<Arc<Self>> {
Ok(Arc::new(Self {
interval: Duration::from_secs(CHECK_FOR_ANNOUNCEMENTS_INTERVAL),
interrupt: Notify::new(),
db: args.db["global"].clone(),
services: Services {
globals: args.depend::<globals::Service>("globals"),
admin: args.depend::<admin::Service>("admin"),
client: args.depend::<client::Service>("client"),
server: args.server.clone(),
},
}))
}
#[tracing::instrument(skip_all, name = "announcements", level = "debug")]
async fn worker(self: Arc<Self>) -> Result<()> {
if !self.services.globals.allow_announcements_check() {
debug!("Disabling announcements check");
return Ok(());
}
let mut i = interval(self.interval);
i.set_missed_tick_behavior(MissedTickBehavior::Delay);
i.reset_after(self.interval);
loop {
tokio::select! {
() = self.interrupt.notified() => break,
_ = i.tick() => (),
}
if let Err(e) = self.check().await {
warn!(%e, "Failed to check for announcements");
}
}
Ok(())
}
fn interrupt(&self) { self.interrupt.notify_waiters(); }
fn name(&self) -> &str { crate::service::make_name(std::module_path!()) }
}
impl Service {
#[tracing::instrument(skip_all)]
async fn check(&self) -> Result<()> {
debug_assert!(self.services.server.running(), "server must not be shutting down");
let response = self
.services
.client
.default
.get(CHECK_FOR_ANNOUNCEMENTS_URL)
.send()
.await?
.text()
.await?;
let response = serde_json::from_str::<CheckForAnnouncementsResponse>(&response)?;
for announcement in &response.announcements {
if announcement.id > self.last_check_for_announcements_id().await {
self.handle(announcement).await;
self.update_check_for_announcements_id(announcement.id);
}
}
Ok(())
}
#[tracing::instrument(skip_all)]
async fn handle(&self, announcement: &CheckForAnnouncementsResponseEntry) {
if let Some(date) = &announcement.date {
info!("[announcements] {date} {:#}", announcement.message);
} else {
info!("[announcements] {:#}", announcement.message);
}
self.services
.admin
.send_message(RoomMessageEventContent::text_markdown(format!(
"### New announcement{}\n\n{}",
announcement
.date
.as_ref()
.map_or_else(String::new, |date| format!(" - `{date}`")),
announcement.message
)))
.await
.ok();
}
#[inline]
pub fn update_check_for_announcements_id(&self, id: u64) {
self.db.raw_put(LAST_CHECK_FOR_ANNOUNCEMENTS_ID, id);
}
pub async fn last_check_for_announcements_id(&self) -> u64 {
self.db
.get(LAST_CHECK_FOR_ANNOUNCEMENTS_ID)
.await
.deserialized()
.unwrap_or(0_u64)
}
}

View file

@ -127,7 +127,9 @@ impl Service {
&self.server.config.new_user_displayname_suffix
}
pub fn allow_check_for_updates(&self) -> bool { self.server.config.allow_check_for_updates }
pub fn allow_announcements_check(&self) -> bool {
self.server.config.allow_announcements_check
}
pub fn trusted_servers(&self) -> &[OwnedServerName] { &self.server.config.trusted_servers }

View file

@ -8,6 +8,7 @@ pub mod services;
pub mod account_data;
pub mod admin;
pub mod announcements;
pub mod appservice;
pub mod client;
pub mod config;
@ -26,7 +27,6 @@ pub mod server_keys;
pub mod sync;
pub mod transaction_ids;
pub mod uiaa;
pub mod updates;
pub mod users;
extern crate conduwuit_core as conduwuit;

View file

@ -10,11 +10,12 @@ use futures::{Stream, StreamExt, TryStreamExt};
use tokio::sync::Mutex;
use crate::{
account_data, admin, appservice, client, config, emergency, federation, globals, key_backups,
account_data, admin, announcements, appservice, client, config, emergency, federation,
globals, key_backups,
manager::Manager,
media, moderation, presence, pusher, resolver, rooms, sending, server_keys, service,
service::{Args, Map, Service},
sync, transaction_ids, uiaa, updates, users,
sync, transaction_ids, uiaa, users,
};
pub struct Services {
@ -37,9 +38,9 @@ pub struct Services {
pub sync: Arc<sync::Service>,
pub transaction_ids: Arc<transaction_ids::Service>,
pub uiaa: Arc<uiaa::Service>,
pub updates: Arc<updates::Service>,
pub users: Arc<users::Service>,
pub moderation: Arc<moderation::Service>,
pub announcements: Arc<announcements::Service>,
manager: Mutex<Option<Arc<Manager>>>,
pub(crate) service: Arc<Map>,
@ -105,9 +106,9 @@ impl Services {
sync: build!(sync::Service),
transaction_ids: build!(transaction_ids::Service),
uiaa: build!(uiaa::Service),
updates: build!(updates::Service),
users: build!(users::Service),
moderation: build!(moderation::Service),
announcements: build!(announcements::Service),
manager: Mutex::new(None),
service,

View file

@ -1,142 +0,0 @@
use std::{sync::Arc, time::Duration};
use async_trait::async_trait;
use conduwuit::{Result, Server, debug, info, warn};
use database::{Deserialized, Map};
use ruma::events::room::message::RoomMessageEventContent;
use serde::Deserialize;
use tokio::{
sync::Notify,
time::{MissedTickBehavior, interval},
};
use crate::{Dep, admin, client, globals};
pub struct Service {
interval: Duration,
interrupt: Notify,
db: Arc<Map>,
services: Services,
}
struct Services {
admin: Dep<admin::Service>,
client: Dep<client::Service>,
globals: Dep<globals::Service>,
server: Arc<Server>,
}
#[derive(Debug, Deserialize)]
struct CheckForUpdatesResponse {
updates: Vec<CheckForUpdatesResponseEntry>,
}
#[derive(Debug, Deserialize)]
struct CheckForUpdatesResponseEntry {
id: u64,
date: String,
message: String,
}
const CHECK_FOR_UPDATES_URL: &str = "https://pupbrain.dev/check-for-updates/stable";
const CHECK_FOR_UPDATES_INTERVAL: u64 = 7200; // 2 hours
const LAST_CHECK_FOR_UPDATES_COUNT: &[u8; 1] = b"u";
#[async_trait]
impl crate::Service for Service {
fn build(args: crate::Args<'_>) -> Result<Arc<Self>> {
Ok(Arc::new(Self {
interval: Duration::from_secs(CHECK_FOR_UPDATES_INTERVAL),
interrupt: Notify::new(),
db: args.db["global"].clone(),
services: Services {
globals: args.depend::<globals::Service>("globals"),
admin: args.depend::<admin::Service>("admin"),
client: args.depend::<client::Service>("client"),
server: args.server.clone(),
},
}))
}
#[tracing::instrument(skip_all, name = "updates", level = "debug")]
async fn worker(self: Arc<Self>) -> Result<()> {
if !self.services.globals.allow_check_for_updates() {
debug!("Disabling update check");
return Ok(());
}
let mut i = interval(self.interval);
i.set_missed_tick_behavior(MissedTickBehavior::Delay);
i.reset_after(self.interval);
loop {
tokio::select! {
() = self.interrupt.notified() => break,
_ = i.tick() => (),
}
if let Err(e) = self.check().await {
warn!(%e, "Failed to check for updates");
}
}
Ok(())
}
fn interrupt(&self) { self.interrupt.notify_waiters(); }
fn name(&self) -> &str { crate::service::make_name(std::module_path!()) }
}
impl Service {
#[tracing::instrument(skip_all)]
async fn check(&self) -> Result<()> {
debug_assert!(self.services.server.running(), "server must not be shutting down");
let response = self
.services
.client
.default
.get(CHECK_FOR_UPDATES_URL)
.send()
.await?
.text()
.await?;
let response = serde_json::from_str::<CheckForUpdatesResponse>(&response)?;
for update in &response.updates {
if update.id > self.last_check_for_updates_id().await {
self.handle(update).await;
self.update_check_for_updates_id(update.id);
}
}
Ok(())
}
#[tracing::instrument(skip_all)]
async fn handle(&self, update: &CheckForUpdatesResponseEntry) {
info!("{} {:#}", update.date, update.message);
self.services
.admin
.send_message(RoomMessageEventContent::text_markdown(format!(
"### the following is a message from the conduwuit puppy\n\nit was sent on \
`{}`:\n\n@room: {}",
update.date, update.message
)))
.await
.ok();
}
#[inline]
pub fn update_check_for_updates_id(&self, id: u64) {
self.db.raw_put(LAST_CHECK_FOR_UPDATES_COUNT, id);
}
pub async fn last_check_for_updates_id(&self) -> u64 {
self.db
.get(LAST_CHECK_FOR_UPDATES_COUNT)
.await
.deserialized()
.unwrap_or(0_u64)
}
}