From e39dfd12de1a42e0bdbd0336f6c401339fcaf0c3 Mon Sep 17 00:00:00 2001 From: CI Date: Fri, 7 Jun 2024 12:05:04 +0000 Subject: [PATCH 1/5] chore(deps): update dependency vite to v5.2.13 --- packages/client/package.json | 2 +- packages/sw/package.json | 2 +- pnpm-lock.yaml | 28 ++++++++++++++-------------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/packages/client/package.json b/packages/client/package.json index 648655e9dd..bcfdc60520 100644 --- a/packages/client/package.json +++ b/packages/client/package.json @@ -84,7 +84,7 @@ "typescript": "5.4.5", "unicode-emoji-json": "0.6.0", "uuid": "9.0.1", - "vite": "5.2.12", + "vite": "5.2.13", "vite-plugin-compression": "0.5.1", "vue": "3.4.27", "vue-draggable-plus": "0.5.0", diff --git a/packages/sw/package.json b/packages/sw/package.json index 22d380be54..e3a47685fc 100644 --- a/packages/sw/package.json +++ b/packages/sw/package.json @@ -11,7 +11,7 @@ "devDependencies": { "firefish-js": "workspace:*", "idb-keyval": "6.2.1", - "vite": "5.2.12", + "vite": "5.2.13", "vite-plugin-compression": "0.5.1" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c30b66c537..395e03c3d3 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -586,7 +586,7 @@ importers: version: 9.0.8 '@vitejs/plugin-vue': specifier: 5.0.5 - version: 5.0.5(vite@5.2.12(@types/node@20.14.2)(sass@1.77.4)(stylus@0.57.0)(terser@5.31.1))(vue@3.4.27(typescript@5.4.5)) + version: 5.0.5(vite@5.2.13(@types/node@20.14.2)(sass@1.77.4)(stylus@0.57.0)(terser@5.31.1))(vue@3.4.27(typescript@5.4.5)) '@vue/runtime-core': specifier: 3.4.27 version: 3.4.27 @@ -738,11 +738,11 @@ importers: specifier: 9.0.1 version: 9.0.1 vite: - specifier: 5.2.12 - version: 5.2.12(@types/node@20.14.2)(sass@1.77.4)(stylus@0.57.0)(terser@5.31.1) + specifier: 5.2.13 + version: 5.2.13(@types/node@20.14.2)(sass@1.77.4)(stylus@0.57.0)(terser@5.31.1) vite-plugin-compression: specifier: 0.5.1 - version: 0.5.1(vite@5.2.12(@types/node@20.14.2)(sass@1.77.4)(stylus@0.57.0)(terser@5.31.1)) + version: 0.5.1(vite@5.2.13(@types/node@20.14.2)(sass@1.77.4)(stylus@0.57.0)(terser@5.31.1)) vue: specifier: 3.4.27 version: 3.4.27(typescript@5.4.5) @@ -930,11 +930,11 @@ importers: specifier: 6.2.1 version: 6.2.1 vite: - specifier: 5.2.12 - version: 5.2.12(@types/node@20.14.2)(sass@1.77.4)(stylus@0.57.0)(terser@5.31.1) + specifier: 5.2.13 + version: 5.2.13(@types/node@20.14.2)(sass@1.77.4)(stylus@0.57.0)(terser@5.31.1) vite-plugin-compression: specifier: 0.5.1 - version: 0.5.1(vite@5.2.12(@types/node@20.14.2)(sass@1.77.4)(stylus@0.57.0)(terser@5.31.1)) + version: 0.5.1(vite@5.2.13(@types/node@20.14.2)(sass@1.77.4)(stylus@0.57.0)(terser@5.31.1)) packages: @@ -7717,8 +7717,8 @@ packages: peerDependencies: vite: '>=2.0.0' - vite@5.2.12: - resolution: {integrity: sha512-/gC8GxzxMK5ntBwb48pR32GGhENnjtY30G4A0jemunsBkiEZFw60s8InGpN8gkhHEkjnRK1aSAxeQgwvFhUHAA==} + vite@5.2.13: + resolution: {integrity: sha512-SSq1noJfY9pR3I1TUENL3rQYDQCFqgD+lM6fTRAM8Nv6Lsg5hDLaXkjETVeBt+7vZBCMoibD+6IWnT2mJ+Zb/A==} engines: {node: ^18.0.0 || >=20.0.0} hasBin: true peerDependencies: @@ -10127,9 +10127,9 @@ snapshots: '@ungap/structured-clone@1.2.0': {} - '@vitejs/plugin-vue@5.0.5(vite@5.2.12(@types/node@20.14.2)(sass@1.77.4)(stylus@0.57.0)(terser@5.31.1))(vue@3.4.27(typescript@5.4.5))': + '@vitejs/plugin-vue@5.0.5(vite@5.2.13(@types/node@20.14.2)(sass@1.77.4)(stylus@0.57.0)(terser@5.31.1))(vue@3.4.27(typescript@5.4.5))': dependencies: - vite: 5.2.12(@types/node@20.14.2)(sass@1.77.4)(stylus@0.57.0)(terser@5.31.1) + vite: 5.2.13(@types/node@20.14.2)(sass@1.77.4)(stylus@0.57.0)(terser@5.31.1) vue: 3.4.27(typescript@5.4.5) '@volar/language-core@2.2.5': @@ -15925,16 +15925,16 @@ snapshots: core-util-is: 1.0.2 extsprintf: 1.3.0 - vite-plugin-compression@0.5.1(vite@5.2.12(@types/node@20.14.2)(sass@1.77.4)(stylus@0.57.0)(terser@5.31.1)): + vite-plugin-compression@0.5.1(vite@5.2.13(@types/node@20.14.2)(sass@1.77.4)(stylus@0.57.0)(terser@5.31.1)): dependencies: chalk: 4.1.2 debug: 4.3.5 fs-extra: 10.1.0 - vite: 5.2.12(@types/node@20.14.2)(sass@1.77.4)(stylus@0.57.0)(terser@5.31.1) + vite: 5.2.13(@types/node@20.14.2)(sass@1.77.4)(stylus@0.57.0)(terser@5.31.1) transitivePeerDependencies: - supports-color - vite@5.2.12(@types/node@20.14.2)(sass@1.77.4)(stylus@0.57.0)(terser@5.31.1): + vite@5.2.13(@types/node@20.14.2)(sass@1.77.4)(stylus@0.57.0)(terser@5.31.1): dependencies: esbuild: 0.20.2 postcss: 8.4.38 From 0839fe27b2dd20ad364e98df0ab0dbf257e8dd58 Mon Sep 17 00:00:00 2001 From: naskya Date: Fri, 7 Jun 2024 22:21:53 +0900 Subject: [PATCH 2/5] refactor (backend-rs): save nodeinfo cache directly --- packages/backend-rs/index.d.ts | 93 ++++++++++--------- packages/backend-rs/index.js | 3 +- .../src/federation/nodeinfo/generate.rs | 52 +++++++---- .../src/federation/nodeinfo/schema.rs | 20 ++-- packages/backend-rs/src/lib.rs | 2 +- packages/backend/src/boot/master.ts | 3 + packages/macro-rs/src/lib.rs | 29 ++++++ 7 files changed, 128 insertions(+), 74 deletions(-) diff --git a/packages/backend-rs/index.d.ts b/packages/backend-rs/index.d.ts index 06bb7e1430..487f87bdda 100644 --- a/packages/backend-rs/index.d.ts +++ b/packages/backend-rs/index.d.ts @@ -216,6 +216,7 @@ export function acctToString(acct: Acct): string export function fetchNodeinfo(host: string): Promise export function nodeinfo_2_1(): Promise export function nodeinfo_2_0(): Promise +export function updateNodeinfoCache(): Promise /** NodeInfo schema version 2.0. */ export interface Nodeinfo { /** The schema version, must be 2.0. */ @@ -241,16 +242,16 @@ export interface Software20 { version: string } export enum Protocol { - Activitypub = 'activitypub', - Buddycloud = 'buddycloud', - Dfrn = 'dfrn', - Diaspora = 'diaspora', - Libertree = 'libertree', - Ostatus = 'ostatus', - Pumpio = 'pumpio', - Tent = 'tent', - Xmpp = 'xmpp', - Zot = 'zot' + Activitypub = 0, + Buddycloud = 1, + Dfrn = 2, + Diaspora = 3, + Libertree = 4, + Ostatus = 5, + Pumpio = 6, + Tent = 7, + Xmpp = 8, + Zot = 9 } /** The third party sites this server can connect to via their application API. */ export interface Services { @@ -261,45 +262,45 @@ export interface Services { } /** The third party sites this server can retrieve messages from for combined display with regular traffic. */ export enum Inbound { - Atom1 = 'atom1', - Gnusocial = 'gnusocial', - Imap = 'imap', - Pnut = 'pnut', - Pop3 = 'pop3', - Pumpio = 'pumpio', - Rss2 = 'rss2', - Twitter = 'twitter' + Atom1 = 0, + Gnusocial = 1, + Imap = 2, + Pnut = 3, + Pop3 = 4, + Pumpio = 5, + Rss2 = 6, + Twitter = 7 } /** The third party sites this server can publish messages to on the behalf of a user. */ export enum Outbound { - Atom1 = 'atom1', - Blogger = 'blogger', - Buddycloud = 'buddycloud', - Diaspora = 'diaspora', - Dreamwidth = 'dreamwidth', - Drupal = 'drupal', - Facebook = 'facebook', - Friendica = 'friendica', - Gnusocial = 'gnusocial', - Google = 'google', - Insanejournal = 'insanejournal', - Libertree = 'libertree', - Linkedin = 'linkedin', - Livejournal = 'livejournal', - Mediagoblin = 'mediagoblin', - Myspace = 'myspace', - Pinterest = 'pinterest', - Pnut = 'pnut', - Posterous = 'posterous', - Pumpio = 'pumpio', - Redmatrix = 'redmatrix', - Rss2 = 'rss2', - Smtp = 'smtp', - Tent = 'tent', - Tumblr = 'tumblr', - Twitter = 'twitter', - Wordpress = 'wordpress', - Xmpp = 'xmpp' + Atom1 = 0, + Blogger = 1, + Buddycloud = 2, + Diaspora = 3, + Dreamwidth = 4, + Drupal = 5, + Facebook = 6, + Friendica = 7, + Gnusocial = 8, + Google = 9, + Insanejournal = 10, + Libertree = 11, + Linkedin = 12, + Livejournal = 13, + Mediagoblin = 14, + Myspace = 15, + Pinterest = 16, + Pnut = 17, + Posterous = 18, + Pumpio = 19, + Redmatrix = 20, + Rss2 = 21, + Smtp = 22, + Tent = 23, + Tumblr = 24, + Twitter = 25, + Wordpress = 26, + Xmpp = 27 } /** Usage statistics for this server. */ export interface Usage { diff --git a/packages/backend-rs/index.js b/packages/backend-rs/index.js index 1801db3f63..b4a6bea090 100644 --- a/packages/backend-rs/index.js +++ b/packages/backend-rs/index.js @@ -310,7 +310,7 @@ if (!nativeBinding) { throw new Error(`Failed to load native binding`) } -const { SECOND, MINUTE, HOUR, DAY, USER_ONLINE_THRESHOLD, USER_ACTIVE_THRESHOLD, FILE_TYPE_BROWSERSAFE, fetchMeta, updateMetaCache, metaToPugArgs, loadConfig, stringToAcct, acctToString, fetchNodeinfo, nodeinfo_2_1, nodeinfo_2_0, Protocol, Inbound, Outbound, greet, initializeRustLogger, showServerInfo, isBlockedServer, isSilencedServer, isAllowedServer, checkWordMute, getFullApAccount, isSelfHost, isSameOrigin, extractHost, toPuny, isUnicodeEmoji, sqlLikeEscape, safeForSql, formatMilliseconds, getImageSizeFromUrl, getNoteSummary, isQuote, isSafeUrl, latestVersion, toMastodonId, fromMastodonId, nyaify, hashPassword, verifyPassword, isOldPasswordAlgorithm, decodeReaction, countReactions, toDbReaction, removeOldAttestationChallenges, cpuInfo, cpuUsage, memoryUsage, storageUsage, AntennaSrc, DriveFileUsageHint, MutedNoteReason, NoteVisibility, NotificationType, PageVisibility, PollNoteVisibility, RelayStatus, UserEmojiModPerm, UserProfileFfvisibility, UserProfileMutingNotificationTypes, updateAntennasOnNewNote, updateAntennaCache, watchNote, unwatchNote, PushNotificationKind, sendPushNotification, publishToChannelStream, publishToChatStream, ChatIndexEvent, publishToChatIndexStream, publishToBroadcastStream, publishToGroupChatStream, publishToModerationStream, ChatEvent, getTimestamp, genId, genIdAt, generateSecureRandomString, generateUserToken } = nativeBinding +const { SECOND, MINUTE, HOUR, DAY, USER_ONLINE_THRESHOLD, USER_ACTIVE_THRESHOLD, FILE_TYPE_BROWSERSAFE, fetchMeta, updateMetaCache, metaToPugArgs, loadConfig, stringToAcct, acctToString, fetchNodeinfo, nodeinfo_2_1, nodeinfo_2_0, updateNodeinfoCache, Protocol, Inbound, Outbound, greet, initializeRustLogger, showServerInfo, isBlockedServer, isSilencedServer, isAllowedServer, checkWordMute, getFullApAccount, isSelfHost, isSameOrigin, extractHost, toPuny, isUnicodeEmoji, sqlLikeEscape, safeForSql, formatMilliseconds, getImageSizeFromUrl, getNoteSummary, isQuote, isSafeUrl, latestVersion, toMastodonId, fromMastodonId, nyaify, hashPassword, verifyPassword, isOldPasswordAlgorithm, decodeReaction, countReactions, toDbReaction, removeOldAttestationChallenges, cpuInfo, cpuUsage, memoryUsage, storageUsage, AntennaSrc, DriveFileUsageHint, MutedNoteReason, NoteVisibility, NotificationType, PageVisibility, PollNoteVisibility, RelayStatus, UserEmojiModPerm, UserProfileFfvisibility, UserProfileMutingNotificationTypes, updateAntennasOnNewNote, updateAntennaCache, watchNote, unwatchNote, PushNotificationKind, sendPushNotification, publishToChannelStream, publishToChatStream, ChatIndexEvent, publishToChatIndexStream, publishToBroadcastStream, publishToGroupChatStream, publishToModerationStream, ChatEvent, getTimestamp, genId, genIdAt, generateSecureRandomString, generateUserToken } = nativeBinding module.exports.SECOND = SECOND module.exports.MINUTE = MINUTE @@ -328,6 +328,7 @@ module.exports.acctToString = acctToString module.exports.fetchNodeinfo = fetchNodeinfo module.exports.nodeinfo_2_1 = nodeinfo_2_1 module.exports.nodeinfo_2_0 = nodeinfo_2_0 +module.exports.updateNodeinfoCache = updateNodeinfoCache module.exports.Protocol = Protocol module.exports.Inbound = Inbound module.exports.Outbound = Outbound diff --git a/packages/backend-rs/src/federation/nodeinfo/generate.rs b/packages/backend-rs/src/federation/nodeinfo/generate.rs index 63621e9e92..c6a7bcdab7 100644 --- a/packages/backend-rs/src/federation/nodeinfo/generate.rs +++ b/packages/backend-rs/src/federation/nodeinfo/generate.rs @@ -2,21 +2,27 @@ use crate::{ config::{local_server_info, CONFIG}, - database::{cache, db_conn}, + database::db_conn, federation::nodeinfo::schema::*, model::entity::{note, user}, }; use sea_orm::prelude::*; use serde_json::json; -use std::collections::HashMap; +use std::{collections::HashMap, sync::Mutex}; + +static CACHE: Mutex> = Mutex::new(None); + +fn set_cache(nodeinfo: &Nodeinfo21) { + let _ = CACHE + .lock() + .map(|mut cache| *cache = Some(nodeinfo.to_owned())); +} /// Errors that can occur while generating NodeInfo of the local server #[derive(thiserror::Error, Debug)] pub enum Error { #[error("Database error: {0}")] Db(#[from] DbErr), - #[error("Cache error: {0}")] - Cache(#[from] cache::Error), #[error("Failed to serialize nodeinfo to JSON: {0}")] Json(#[from] serde_json::Error), } @@ -62,10 +68,12 @@ async fn statistics() -> Result<(u64, u64, u64, u64), DbErr> { /// Generates NodeInfo (version 2.1) of the local server. /// This function doesn't use caches and returns the latest information. async fn generate_nodeinfo_2_1() -> Result { + tracing::info!("generating NodeInfo"); + let (local_users, local_active_halfyear, local_active_month, local_posts) = statistics().await?; let meta = local_server_info().await?; - let metadata = HashMap::from([ + let mut metadata = HashMap::from([ ( "nodeName".to_string(), json!(meta.name.unwrap_or_else(|| CONFIG.host.clone())), @@ -98,6 +106,7 @@ async fn generate_nodeinfo_2_1() -> Result { json!(meta.theme_color.unwrap_or_else(|| "#31748f".to_string())), ), ]); + metadata.shrink_to_fit(); Ok(Nodeinfo21 { version: "2.1".to_string(), @@ -126,19 +135,24 @@ async fn generate_nodeinfo_2_1() -> Result { }) } +async fn nodeinfo_2_1_impl(use_cache: bool) -> Result { + if use_cache { + if let Some(nodeinfo) = CACHE.lock().ok().and_then(|cache| cache.to_owned()) { + return Ok(nodeinfo); + } + } + + let nodeinfo = generate_nodeinfo_2_1().await?; + + tracing::info!("updating cache"); + set_cache(&nodeinfo); + + Ok(nodeinfo) +} + /// Returns NodeInfo (version 2.1) of the local server. pub async fn nodeinfo_2_1() -> Result { - const NODEINFO_2_1_CACHE_KEY: &str = "nodeinfo_2_1"; - - let cached = cache::get::(NODEINFO_2_1_CACHE_KEY).await?; - - if let Some(nodeinfo) = cached { - Ok(nodeinfo) - } else { - let nodeinfo = generate_nodeinfo_2_1().await?; - cache::set(NODEINFO_2_1_CACHE_KEY, &nodeinfo, 60 * 60).await?; - Ok(nodeinfo) - } + nodeinfo_2_1_impl(true).await } /// Returns NodeInfo (version 2.0) of the local server. @@ -155,3 +169,9 @@ pub async fn nodeinfo_2_1_as_json() -> Result { pub async fn nodeinfo_2_0_as_json() -> Result { Ok(serde_json::to_value(nodeinfo_2_0().await?)?) } + +#[crate::ts_export(js_name = "updateNodeinfoCache")] +pub async fn update_cache() -> Result<(), Error> { + nodeinfo_2_1_impl(false).await?; + Ok(()) +} diff --git a/packages/backend-rs/src/federation/nodeinfo/schema.rs b/packages/backend-rs/src/federation/nodeinfo/schema.rs index 2876afbbdb..82f1dfe728 100644 --- a/packages/backend-rs/src/federation/nodeinfo/schema.rs +++ b/packages/backend-rs/src/federation/nodeinfo/schema.rs @@ -10,8 +10,8 @@ use std::collections::HashMap; // * #[serde(tag = "version", rename = "2.1")] (https://github.com/3Hren/msgpack-rust/issues/318) /// NodeInfo schema version 2.1. -#[cfg_attr(test, derive(Debug, PartialEq))] -#[derive(Deserialize, Serialize)] +#[cfg_attr(test, derive(Debug, PartialEq, Deserialize))] +#[derive(Clone, Serialize)] #[serde(rename_all = "camelCase")] pub struct Nodeinfo21 { /// The schema version, must be 2.1. @@ -53,8 +53,8 @@ pub struct Nodeinfo20 { } /// Metadata about server software in use (version 2.1). -#[cfg_attr(test, derive(Debug, PartialEq))] -#[derive(Deserialize, Serialize)] +#[cfg_attr(test, derive(Debug, PartialEq, Deserialize))] +#[derive(Clone, Serialize)] #[serde(rename_all = "camelCase")] pub struct Software21 { /// The canonical name of this server software. @@ -82,7 +82,7 @@ pub struct Software20 { #[cfg_attr(test, derive(Debug, PartialEq))] #[derive(Deserialize, Serialize)] #[serde(rename_all = "lowercase")] -#[crate::export(string_enum = "lowercase")] +#[crate::derive_clone_and_export] pub enum Protocol { Activitypub, Buddycloud, @@ -98,7 +98,7 @@ pub enum Protocol { /// The third party sites this server can connect to via their application API. #[cfg_attr(test, derive(Debug, PartialEq))] -#[derive(Deserialize, Serialize)] +#[derive(Clone, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] #[crate::export(object)] pub struct Services { @@ -112,7 +112,7 @@ pub struct Services { #[cfg_attr(test, derive(Debug, PartialEq))] #[derive(Deserialize, Serialize)] #[serde(rename_all = "lowercase")] -#[crate::export(string_enum = "lowercase")] +#[crate::derive_clone_and_export] pub enum Inbound { #[serde(rename = "atom1.0")] Atom1, @@ -131,7 +131,7 @@ pub enum Inbound { #[cfg_attr(test, derive(Debug, PartialEq))] #[derive(Deserialize, Serialize)] #[serde(rename_all = "lowercase")] -#[crate::export(string_enum = "lowercase")] +#[crate::derive_clone_and_export] pub enum Outbound { #[serde(rename = "atom1.0")] Atom1, @@ -167,7 +167,7 @@ pub enum Outbound { /// Usage statistics for this server. #[cfg_attr(test, derive(Debug, PartialEq))] -#[derive(Deserialize, Serialize)] +#[derive(Clone, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] #[crate::export(object)] pub struct Usage { @@ -178,7 +178,7 @@ pub struct Usage { /// statistics about the users of this server. #[cfg_attr(test, derive(Debug, PartialEq))] -#[derive(Deserialize, Serialize)] +#[derive(Clone, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] #[crate::export(object)] pub struct Users { diff --git a/packages/backend-rs/src/lib.rs b/packages/backend-rs/src/lib.rs index d62ab792d6..6a124de340 100644 --- a/packages/backend-rs/src/lib.rs +++ b/packages/backend-rs/src/lib.rs @@ -1,4 +1,4 @@ -use macro_rs::{export, ts_export}; +use macro_rs::{derive_clone_and_export, export, ts_export}; pub mod config; pub mod database; diff --git a/packages/backend/src/boot/master.ts b/packages/backend/src/boot/master.ts index b70b9a2f11..dbf51c08c5 100644 --- a/packages/backend/src/boot/master.ts +++ b/packages/backend/src/boot/master.ts @@ -8,6 +8,7 @@ import { removeOldAttestationChallenges, showServerInfo, updateMetaCache, + updateNodeinfoCache, type Config, } from "backend-rs"; import { config } from "@/config.js"; @@ -51,6 +52,8 @@ export async function masterMain() { import("../daemons/queue-stats.js").then((x) => x.default()); // Update meta cache every 5 minitues setInterval(() => updateMetaCache(), 1000 * 60 * 5); + // Update nodeinfo cache every hour + setInterval(() => updateNodeinfoCache(), 1000 * 60 * 60); // Remove old attestation challenges setInterval(() => removeOldAttestationChallenges(), 1000 * 60 * 30); } diff --git a/packages/macro-rs/src/lib.rs b/packages/macro-rs/src/lib.rs index 943dd56cd1..0860298048 100644 --- a/packages/macro-rs/src/lib.rs +++ b/packages/macro-rs/src/lib.rs @@ -25,6 +25,35 @@ pub fn read_version_from_package_json(_item: proc_macro::TokenStream) -> proc_ma quote! { #version }.into() } +/// Export an enum to TypeScript, and derive [Clone]. +/// +/// You need this macro because [`napi_derive::napi`](https://docs.rs/napi-derive/latest/napi_derive/attr.napi.html) +/// automatically derives the [Clone] trait for enums and causes conflicts. +/// +/// This is a wrapper of [`napi_derive::napi`](https://docs.rs/napi-derive/latest/napi_derive/attr.napi.html) +/// that expands to +/// ```no_run +/// #[cfg_attr(not(feature = "napi"), derive(Clone))] +/// #[cfg_attr(feature = "napi", napi_derive::napi(attr))] +/// # fn f() {} // to work around doc test compilation error +/// ``` +/// where `attr` is given attribute(s). +#[proc_macro_attribute] +pub fn derive_clone_and_export( + attr: proc_macro::TokenStream, + item: proc_macro::TokenStream, +) -> proc_macro::TokenStream { + let attr: TokenStream = attr.into(); + let item: TokenStream = item.into(); + + quote! { + #[cfg_attr(not(feature = "napi"), derive(Clone))] + #[cfg_attr(feature = "napi", napi_derive::napi(#attr))] + #item + } + .into() +} + /// Export a function, struct, enum, const, etc. to TypeScript. /// /// This is a wrapper of [macro@napi] that expands to From 7644e3b69096a967f0793c7fb9892d2a137ab25a Mon Sep 17 00:00:00 2001 From: naskya Date: Fri, 7 Jun 2024 22:25:09 +0900 Subject: [PATCH 3/5] fix (macro-rs): fix doctest --- packages/macro-rs/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/macro-rs/src/lib.rs b/packages/macro-rs/src/lib.rs index 0860298048..eebbe198cc 100644 --- a/packages/macro-rs/src/lib.rs +++ b/packages/macro-rs/src/lib.rs @@ -35,7 +35,7 @@ pub fn read_version_from_package_json(_item: proc_macro::TokenStream) -> proc_ma /// ```no_run /// #[cfg_attr(not(feature = "napi"), derive(Clone))] /// #[cfg_attr(feature = "napi", napi_derive::napi(attr))] -/// # fn f() {} // to work around doc test compilation error +/// # enum E {} // to work around doc test compilation error /// ``` /// where `attr` is given attribute(s). #[proc_macro_attribute] From b9f9e62079b7628629c3d3cb49e6687550fb3c7a Mon Sep 17 00:00:00 2001 From: naskya Date: Fri, 7 Jun 2024 22:34:45 +0900 Subject: [PATCH 4/5] chore (backend-rs): be more specific about error variants --- .../src/federation/nodeinfo/generate.rs | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/packages/backend-rs/src/federation/nodeinfo/generate.rs b/packages/backend-rs/src/federation/nodeinfo/generate.rs index c6a7bcdab7..e83fb059b1 100644 --- a/packages/backend-rs/src/federation/nodeinfo/generate.rs +++ b/packages/backend-rs/src/federation/nodeinfo/generate.rs @@ -18,15 +18,6 @@ fn set_cache(nodeinfo: &Nodeinfo21) { .map(|mut cache| *cache = Some(nodeinfo.to_owned())); } -/// Errors that can occur while generating NodeInfo of the local server -#[derive(thiserror::Error, Debug)] -pub enum Error { - #[error("Database error: {0}")] - Db(#[from] DbErr), - #[error("Failed to serialize nodeinfo to JSON: {0}")] - Json(#[from] serde_json::Error), -} - /// Fetches the number of total/active local users and local posts. /// /// # Return value @@ -67,7 +58,7 @@ async fn statistics() -> Result<(u64, u64, u64, u64), DbErr> { /// Generates NodeInfo (version 2.1) of the local server. /// This function doesn't use caches and returns the latest information. -async fn generate_nodeinfo_2_1() -> Result { +async fn generate_nodeinfo_2_1() -> Result { tracing::info!("generating NodeInfo"); let (local_users, local_active_halfyear, local_active_month, local_posts) = @@ -135,7 +126,7 @@ async fn generate_nodeinfo_2_1() -> Result { }) } -async fn nodeinfo_2_1_impl(use_cache: bool) -> Result { +async fn nodeinfo_2_1_impl(use_cache: bool) -> Result { if use_cache { if let Some(nodeinfo) = CACHE.lock().ok().and_then(|cache| cache.to_owned()) { return Ok(nodeinfo); @@ -151,15 +142,24 @@ async fn nodeinfo_2_1_impl(use_cache: bool) -> Result { } /// Returns NodeInfo (version 2.1) of the local server. -pub async fn nodeinfo_2_1() -> Result { +pub async fn nodeinfo_2_1() -> Result { nodeinfo_2_1_impl(true).await } /// Returns NodeInfo (version 2.0) of the local server. -pub async fn nodeinfo_2_0() -> Result { +pub async fn nodeinfo_2_0() -> Result { Ok(nodeinfo_2_1().await?.into()) } +#[cfg(feature = "napi")] +#[derive(thiserror::Error, Debug)] +pub enum Error { + #[error("Database error: {0}")] + Db(#[from] DbErr), + #[error("Failed to serialize nodeinfo into JSON: {0}")] + Json(#[from] serde_json::Error), +} + #[crate::ts_export(js_name = "nodeinfo_2_1")] pub async fn nodeinfo_2_1_as_json() -> Result { Ok(serde_json::to_value(nodeinfo_2_1().await?)?) @@ -171,7 +171,7 @@ pub async fn nodeinfo_2_0_as_json() -> Result { } #[crate::ts_export(js_name = "updateNodeinfoCache")] -pub async fn update_cache() -> Result<(), Error> { +pub async fn update_cache() -> Result<(), DbErr> { nodeinfo_2_1_impl(false).await?; Ok(()) } From 00e9f0ad4816c45e0cb99d0a2dd733524ac923cf Mon Sep 17 00:00:00 2001 From: naskya Date: Fri, 7 Jun 2024 22:46:36 +0900 Subject: [PATCH 5/5] chore (backend-rs, minor): fix code style --- packages/backend-rs/src/service/antenna/process_new_note.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/backend-rs/src/service/antenna/process_new_note.rs b/packages/backend-rs/src/service/antenna/process_new_note.rs index 853fbccbe1..b9eed0eff8 100644 --- a/packages/backend-rs/src/service/antenna/process_new_note.rs +++ b/packages/backend-rs/src/service/antenna/process_new_note.rs @@ -81,5 +81,7 @@ async fn add_note_to_antenna(antenna_id: &str, note: &Note) -> Result<(), Error> .await?; // for streaming API - Ok(stream::antenna::publish(antenna_id.to_string(), note).await?) + stream::antenna::publish(antenna_id.to_string(), note).await?; + + Ok(()) }