refactor (backend): port renderReject to backend-rs

This commit is contained in:
naskya 2024-08-10 21:07:29 +09:00
parent 7e9db5fcff
commit 331384b0ff
No known key found for this signature in database
GPG key ID: 712D413B3A9FED5C
10 changed files with 89 additions and 45 deletions

View file

@ -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

View file

@ -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

View file

@ -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,

View file

@ -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,

View file

@ -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)
}

View file

@ -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,
});

View file

@ -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);

View file

@ -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;

View file

@ -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);
}

View file

@ -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);