From 331384b0ff5f2702b2112b180b021439791b1f45 Mon Sep 17 00:00:00 2001 From: naskya Date: Sat, 10 Aug 2024 21:07:29 +0900 Subject: [PATCH] refactor (backend): port renderReject to backend-rs --- packages/backend-rs/index.d.ts | 60 +++++++++++-------- packages/backend-rs/index.js | 3 +- .../federation/activitypub/object/accept.rs | 5 +- .../src/federation/activitypub/object/mod.rs | 9 +++ .../federation/activitypub/object/reject.rs | 29 +++++++++ .../src/remote/activitypub/renderer/reject.ts | 8 --- .../backend/src/services/blocking/create.ts | 4 +- .../backend/src/services/following/create.ts | 4 +- .../backend/src/services/following/delete.ts | 6 +- .../backend/src/services/following/reject.ts | 6 +- 10 files changed, 89 insertions(+), 45 deletions(-) create mode 100644 packages/backend-rs/src/federation/activitypub/object/reject.rs delete mode 100644 packages/backend/src/remote/activitypub/renderer/reject.ts diff --git a/packages/backend-rs/index.d.ts b/packages/backend-rs/index.d.ts index d63c76405f..c7295b54c2 100644 --- a/packages/backend-rs/index.d.ts +++ b/packages/backend-rs/index.d.ts @@ -48,6 +48,20 @@ export interface Acct { export declare function acctToString(acct: Acct): string +export type Activity = 'Accept'| +'Add'| +'Emoji'| +'Flag'| +'Follow'| +'Hashtag'| +'Like'| +'Mention'| +'Image'| +'Read'| +'Reject'| +'Remove'| +'Tombstone'; + export interface Ad { id: string createdAt: DateTimeWithTimeZone @@ -106,13 +120,13 @@ export type AntennaSrc = 'all'| export interface ApAccept { id: string - type: ApObject + type: Activity actor: string object: ApFollow } export interface ApAdd { - type: ApObject + type: Activity actor: string target: string object: string @@ -120,14 +134,14 @@ export interface ApAdd { export interface ApEmoji { id: string - type: ApObject + type: Activity name: string updated: string icon: Icon } export interface ApFlag { - type: ApObject + type: Activity actor: string content: string object: string @@ -135,20 +149,20 @@ export interface ApFlag { export interface ApFollow { id: string - type: ApObject + type: Activity actor: string object: string } export interface ApHashtag { id: string - type: ApObject + type: Activity name: string } export interface ApLike { id: string - type: ApObject + type: Activity actor: string object: string content: string @@ -156,24 +170,11 @@ export interface ApLike { } export interface ApMention { - type: ApObject + type: Activity href: string name: string } -export type ApObject = 'Accept'| -'Add'| -'Emoji'| -'Flag'| -'Follow'| -'Hashtag'| -'Like'| -'Mention'| -'Image'| -'Read'| -'Remove'| -'Tombstone'; - export interface App { id: string createdAt: DateTimeWithTimeZone @@ -186,13 +187,20 @@ export interface App { } export interface ApRead { - type: ApObject + type: Activity actor: string object: string } +export interface ApReject { + id: string + type: Activity + actor: string + object: ApFollow +} + export interface ApRemove { - type: ApObject + type: Activity actor: string target: string object: string @@ -200,7 +208,7 @@ export interface ApRemove { export interface ApTombstone { id: string - type: ApObject + type: Activity } export interface AttestationChallenge { @@ -604,7 +612,7 @@ export interface Hashtag { } export interface Icon { - type: ApObject + type: Activity mediaType: string url: string } @@ -1372,6 +1380,8 @@ export declare function renderMention(user: UserLike): ApMention export declare function renderRead(userId: string, messageUri: string): ApRead +export declare function renderReject(userId: string, followObject: ApFollow): ApReject + export declare function renderRemove(userId: string, noteId: string): ApRemove export declare function renderTombstone(noteId: string): ApTombstone diff --git a/packages/backend-rs/index.js b/packages/backend-rs/index.js index f2f6cf834b..4d63d6d6ad 100644 --- a/packages/backend-rs/index.js +++ b/packages/backend-rs/index.js @@ -362,8 +362,8 @@ if (!nativeBinding) { } module.exports.acctToString = nativeBinding.acctToString +module.exports.Activity = nativeBinding.Activity module.exports.AntennaSrc = nativeBinding.AntennaSrc -module.exports.ApObject = nativeBinding.ApObject module.exports.ChatEvent = nativeBinding.ChatEvent module.exports.ChatIndexEvent = nativeBinding.ChatIndexEvent module.exports.checkWordMute = nativeBinding.checkWordMute @@ -448,6 +448,7 @@ module.exports.renderHashtag = nativeBinding.renderHashtag module.exports.renderLike = nativeBinding.renderLike module.exports.renderMention = nativeBinding.renderMention module.exports.renderRead = nativeBinding.renderRead +module.exports.renderReject = nativeBinding.renderReject module.exports.renderRemove = nativeBinding.renderRemove module.exports.renderTombstone = nativeBinding.renderTombstone module.exports.safeForSql = nativeBinding.safeForSql diff --git a/packages/backend-rs/src/federation/activitypub/object/accept.rs b/packages/backend-rs/src/federation/activitypub/object/accept.rs index fd6118d56c..f0de0a34a2 100644 --- a/packages/backend-rs/src/federation/activitypub/object/accept.rs +++ b/packages/backend-rs/src/federation/activitypub/object/accept.rs @@ -1,6 +1,5 @@ use super::*; -use crate::{config::CONFIG, misc::user}; -use uuid::Uuid; +use crate::misc::user; #[macros::export(object)] pub struct ApAccept { @@ -16,7 +15,7 @@ impl ApAccept { #[allow(dead_code)] // TODO: remove this line by actually using it fn new(user_id: String, follow_object: follow::ApFollow) -> Self { Self { - id: format!("{}/{}", CONFIG.url, Uuid::new_v4()), + id: random_local_uri(), r#type: Activity::Accept, actor: user::local_uri(user_id), object: follow_object, diff --git a/packages/backend-rs/src/federation/activitypub/object/mod.rs b/packages/backend-rs/src/federation/activitypub/object/mod.rs index ce958c7678..8ed84f5cf3 100644 --- a/packages/backend-rs/src/federation/activitypub/object/mod.rs +++ b/packages/backend-rs/src/federation/activitypub/object/mod.rs @@ -7,6 +7,7 @@ pub mod hashtag; pub mod like; pub mod mention; pub mod read; +pub mod reject; pub mod remove; pub mod tombstone; @@ -24,12 +25,20 @@ pub enum Activity { Mention, Image, Read, + Reject, Remove, Tombstone, } const AS_PUBLIC_URL: &str = "https://www.w3.org/ns/activitystreams#Public"; +use crate::config::CONFIG; +use uuid::Uuid; + +fn random_local_uri() -> String { + format!("{}/{}", CONFIG.url, Uuid::new_v4()) +} + #[macros::export(object)] pub struct UserLike { pub id: String, diff --git a/packages/backend-rs/src/federation/activitypub/object/reject.rs b/packages/backend-rs/src/federation/activitypub/object/reject.rs new file mode 100644 index 0000000000..d8e4d28eb5 --- /dev/null +++ b/packages/backend-rs/src/federation/activitypub/object/reject.rs @@ -0,0 +1,29 @@ +use super::*; +use crate::misc::user; + +#[macros::export(object)] +pub struct ApReject { + pub id: String, + pub r#type: Activity, + pub actor: String, + pub object: follow::ApFollow, +} + +impl ApObject for ApReject {} + +impl ApReject { + #[allow(dead_code)] // TODO: remove this line by actually using it + fn new(user_id: String, follow_object: follow::ApFollow) -> Self { + Self { + id: random_local_uri(), + r#type: Activity::Accept, + actor: user::local_uri(user_id), + object: follow_object, + } + } +} + +#[macros::ts_export] +pub fn render_reject(user_id: String, follow_object: follow::ApFollow) -> ApReject { + ApReject::new(user_id, follow_object) +} diff --git a/packages/backend/src/remote/activitypub/renderer/reject.ts b/packages/backend/src/remote/activitypub/renderer/reject.ts deleted file mode 100644 index 75c3b9d065..0000000000 --- a/packages/backend/src/remote/activitypub/renderer/reject.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { config } from "@/config.js"; -import type { User } from "@/models/entities/user.js"; - -export default (object: any, user: { id: User["id"] }) => ({ - type: "Reject", - actor: `${config.url}/users/${user.id}`, - object, -}); diff --git a/packages/backend/src/services/blocking/create.ts b/packages/backend/src/services/blocking/create.ts index 9bc59e70e9..847179112e 100644 --- a/packages/backend/src/services/blocking/create.ts +++ b/packages/backend/src/services/blocking/create.ts @@ -2,7 +2,6 @@ import { renderActivity } from "@/remote/activitypub/renderer/index.js"; import { renderUndo } from "@/remote/activitypub/renderer/undo.js"; import { renderBlock } from "@/remote/activitypub/renderer/block.js"; import { deliver } from "@/queue/index.js"; -import renderReject from "@/remote/activitypub/renderer/reject.js"; import type { Blocking } from "@/models/entities/blocking.js"; import type { User } from "@/models/entities/user.js"; import { @@ -20,6 +19,7 @@ import { publishToUserStream, UserEvent, renderFollow, + renderReject, } from "backend-rs"; import { getActiveWebhooks } from "@/misc/webhook-cache.js"; import { webhookDeliver } from "@/queue/index.js"; @@ -103,8 +103,8 @@ async function cancelRequest(follower: User, followee: User) { if (Users.isRemoteUser(follower) && Users.isLocalUser(followee)) { const content = renderActivity( renderReject( + followee.id, renderFollow(follower, followee, request.requestId!), - followee, ), ); deliver(followee.id, content, follower.inbox); diff --git a/packages/backend/src/services/following/create.ts b/packages/backend/src/services/following/create.ts index c5821786c8..403fa72517 100644 --- a/packages/backend/src/services/following/create.ts +++ b/packages/backend/src/services/following/create.ts @@ -1,5 +1,4 @@ import { renderActivity } from "@/remote/activitypub/renderer/index.js"; -import renderReject from "@/remote/activitypub/renderer/reject.js"; import { deliver } from "@/queue/index.js"; import createFollowRequest from "./requests/create.js"; import { registerOrFetchInstanceDoc } from "@/services/register-or-fetch-instance-doc.js"; @@ -23,6 +22,7 @@ import { UserEvent, renderAccept, renderFollow, + renderReject, } from "backend-rs"; import { createNotification } from "@/services/create-notification.js"; import { isDuplicateKeyValueError } from "@/misc/is-duplicate-key-value-error.js"; @@ -196,7 +196,7 @@ export default async function ( if (Users.isRemoteUser(follower) && Users.isLocalUser(followee) && blocked) { // リモートフォローを受けてブロックしていた場合は、エラーにするのではなくRejectを送り返しておしまい。 const content = renderActivity( - renderReject(renderFollow(follower, followee, requestId), followee), + renderReject(followee.id, renderFollow(follower, followee, requestId)), ); deliver(followee.id, content, follower.inbox); return; diff --git a/packages/backend/src/services/following/delete.ts b/packages/backend/src/services/following/delete.ts index e46d8422db..6d5775faac 100644 --- a/packages/backend/src/services/following/delete.ts +++ b/packages/backend/src/services/following/delete.ts @@ -4,10 +4,10 @@ import { publishToUserStream, UserEvent, renderFollow, + renderReject, } from "backend-rs"; import { renderActivity } from "@/remote/activitypub/renderer/index.js"; import { renderUndo } from "@/remote/activitypub/renderer/undo.js"; -import renderReject from "@/remote/activitypub/renderer/reject.js"; import { deliver, webhookDeliver } from "@/queue/index.js"; import Logger from "../logger.js"; import { registerOrFetchInstanceDoc } from "@/services/register-or-fetch-instance-doc.js"; @@ -22,6 +22,7 @@ export default async function ( id: User["id"]; host: User["host"]; uri: User["host"]; + username: User["username"]; inbox: User["inbox"]; sharedInbox: User["sharedInbox"]; }, @@ -29,6 +30,7 @@ export default async function ( id: User["id"]; host: User["host"]; uri: User["host"]; + username: User["username"]; inbox: User["inbox"]; sharedInbox: User["sharedInbox"]; }, @@ -79,7 +81,7 @@ export default async function ( if (Users.isLocalUser(followee) && Users.isRemoteUser(follower)) { // local user has null host const content = renderActivity( - renderReject(renderFollow(follower, followee), followee), + renderReject(followee.id, renderFollow(follower, followee)), ); deliver(followee.id, content, follower.inbox); } diff --git a/packages/backend/src/services/following/reject.ts b/packages/backend/src/services/following/reject.ts index aaed1df3a8..d0c256277e 100644 --- a/packages/backend/src/services/following/reject.ts +++ b/packages/backend/src/services/following/reject.ts @@ -1,5 +1,4 @@ import { renderActivity } from "@/remote/activitypub/renderer/index.js"; -import renderReject from "@/remote/activitypub/renderer/reject.js"; import { deliver, webhookDeliver } from "@/queue/index.js"; import { Event, @@ -7,6 +6,7 @@ import { publishToUserStream, UserEvent, renderFollow, + renderReject, } from "backend-rs"; import type { ILocalUser, IRemoteUser } from "@/models/entities/user.js"; import { Users, FollowRequests, Followings } from "@/models/index.js"; @@ -19,6 +19,7 @@ type Local = id: ILocalUser["id"]; host: ILocalUser["host"]; uri: ILocalUser["uri"]; + username: ILocalUser["username"]; }; type Remote = | IRemoteUser @@ -26,6 +27,7 @@ type Remote = id: IRemoteUser["id"]; host: IRemoteUser["host"]; uri: IRemoteUser["uri"]; + username: IRemoteUser["username"]; inbox: IRemoteUser["inbox"]; }; type Both = Local | Remote; @@ -109,8 +111,8 @@ async function deliverReject(followee: Local, follower: Remote) { const content = renderActivity( renderReject( + followee.id, renderFollow(follower, followee, request?.requestId || undefined), - followee, ), ); deliver(followee.id, content, follower.inbox);