refactor (backend): port renderFollow to backend-rs

This commit is contained in:
naskya 2024-07-29 17:52:19 +09:00
parent 2396a5db7d
commit 27e6469f8c
No known key found for this signature in database
GPG key ID: 712D413B3A9FED5C
18 changed files with 126 additions and 77 deletions

View file

@ -412,6 +412,13 @@ export declare function fetchMeta(): Promise<Meta>
/** Fetches and returns the NodeInfo (version 2.0) of a remote server. */
export declare function fetchNodeinfo(host: string): Promise<Nodeinfo>
export interface Follow {
id: string
type: Activity
actor: string
object: string
}
export interface Following {
id: string
createdAt: DateTimeWithTimeZone
@ -425,13 +432,6 @@ export interface Following {
followeeSharedInbox: string | null
}
export interface FollowRelay {
id: string
type: Activity
actor: string
object: string
}
export interface FollowRequest {
id: string
createdAt: DateTimeWithTimeZone
@ -1265,7 +1265,9 @@ export type RelayStatus = 'accepted'|
/** Delete all entries in the [attestation_challenge] table created at more than 5 minutes ago */
export declare function removeOldAttestationChallenges(): Promise<void>
export declare function renderFollowRelay(relayId: string): Promise<FollowRelay>
export declare function renderFollow(follower: UserLike, followee: UserLike, requestId?: string | undefined | null): Follow
export declare function renderFollowRelay(relayId: string): Promise<Follow>
export interface RenoteMuting {
id: string
@ -1537,6 +1539,12 @@ export interface UserKeypair {
privateKey: string
}
export interface UserLike {
id: string
host: string | null
uri: string
}
export interface UserList {
id: string
createdAt: DateTimeWithTimeZone

View file

@ -437,6 +437,7 @@ module.exports.PushNotificationKind = nativeBinding.PushNotificationKind
module.exports.PushSubscriptionType = nativeBinding.PushSubscriptionType
module.exports.RelayStatus = nativeBinding.RelayStatus
module.exports.removeOldAttestationChallenges = nativeBinding.removeOldAttestationChallenges
module.exports.renderFollow = nativeBinding.renderFollow
module.exports.renderFollowRelay = nativeBinding.renderFollowRelay
module.exports.safeForSql = nativeBinding.safeForSql
module.exports.sendPushNotification = nativeBinding.sendPushNotification

View file

@ -0,0 +1,65 @@
use super::*;
use crate::{config::CONFIG, federation::internal_actor, misc::user};
use serde::Serialize;
#[macros::export(object)]
pub struct UserLike {
pub id: String,
pub host: Option<String>,
pub uri: String,
}
#[derive(Serialize)]
#[macros::export(object)]
pub struct Follow {
pub id: String,
pub r#type: Activity,
pub actor: String,
pub object: String,
}
impl ActivityPubObject for Follow {}
impl Follow {
#[allow(dead_code)] // TODO: remove this line
fn new(follower: UserLike, followee: UserLike, request_id: Option<String>) -> Self {
Self {
id: request_id.unwrap_or_else(|| {
format!("{}/follows/{}/{}", CONFIG.url, follower.id, followee.id)
}),
r#type: Activity::Follow,
actor: match user::is_local!(follower) {
true => format!("{}/users/{}", CONFIG.url, follower.id),
false => follower.uri,
},
object: match user::is_local!(followee) {
true => format!("{}/users/{}", CONFIG.url, followee.id),
false => followee.uri,
},
}
}
#[allow(dead_code)] // TODO: remove this line
async fn new_relay(relay_id: String) -> Result<Self, internal_actor::relay::Error> {
Ok(Self {
id: format!("{}/activities/follow-relay/{}", CONFIG.url, relay_id),
r#type: Activity::Follow,
actor: format!(
"{}/users/{}",
CONFIG.url,
internal_actor::relay::get_id().await?
),
object: AS_PUBLIC_URL.to_owned(),
})
}
}
#[macros::ts_export]
pub fn render_follow(follower: UserLike, followee: UserLike, request_id: Option<String>) -> Follow {
Follow::new(follower, followee, request_id)
}
#[macros::ts_export]
pub async fn render_follow_relay(relay_id: String) -> Result<Follow, internal_actor::relay::Error> {
Follow::new_relay(relay_id).await
}

View file

@ -1,4 +1,4 @@
pub mod relay;
pub mod follow;
pub trait ActivityPubObject {}
@ -7,3 +7,5 @@ pub trait ActivityPubObject {}
pub enum Activity {
Follow,
}
const AS_PUBLIC_URL: &str = "https://www.w3.org/ns/activitystreams#Public";

View file

@ -1,28 +0,0 @@
use super::*;
use crate::{config::CONFIG, federation::internal_actor};
use serde::Serialize;
#[derive(Serialize)]
#[macros::export(object)]
pub struct FollowRelay {
pub id: String,
pub r#type: Activity,
pub actor: String,
pub object: String,
}
impl ActivityPubObject for FollowRelay {}
#[macros::export(js_name = "renderFollowRelay")]
pub async fn follow(relay_id: &str) -> Result<FollowRelay, internal_actor::relay::Error> {
Ok(FollowRelay {
id: format!("{}/activities/follow-relay/{}", CONFIG.url, relay_id),
r#type: Activity::Follow,
actor: format!(
"{}/users/{}",
CONFIG.url,
internal_actor::relay::get_id().await?
),
object: "https://www.w3.org/ns/activitystreams#Public".to_owned(),
})
}

View file

@ -1 +1,23 @@
pub mod count;
#[doc(hidden)] // hide the macro in the top doc page
#[macro_export]
macro_rules! is_local {
($user_like:expr) => {
$user_like.host.is_none()
};
}
#[doc(inline)] // show the macro in the module doc page
pub use is_local;
#[doc(hidden)] // hide the macro in the top doc page
#[macro_export]
macro_rules! is_remote {
($user_like:expr) => {
$user_like.host.is_some()
};
}
#[doc(inline)] // show the macro in the module doc page
pub use is_remote;

View file

@ -207,7 +207,7 @@ export async function createPerson(
try {
const data = await fetch(person.followers, {
headers: { Accept: "application/json" },
size: 1024 * 1024
size: 1024 * 1024,
});
const json_data = JSON.parse(await data.text());
@ -223,7 +223,7 @@ export async function createPerson(
try {
const data = await fetch(person.following, {
headers: { Accept: "application/json" },
size: 1024 * 1024
size: 1024 * 1024,
});
const json_data = JSON.parse(await data.text());
@ -492,7 +492,7 @@ export async function updatePerson(
try {
const data = await fetch(person.followers, {
headers: { Accept: "application/json" },
size: 1024 * 1024
size: 1024 * 1024,
});
const json_data = JSON.parse(await data.text());
@ -508,7 +508,7 @@ export async function updatePerson(
try {
const data = await fetch(person.following, {
headers: { Accept: "application/json" },
size: 1024 * 1024
size: 1024 * 1024,
});
const json_data = JSON.parse(await data.text());

View file

@ -1,22 +0,0 @@
import { config } from "@/config.js";
import type { User } from "@/models/entities/user.js";
import { Users } from "@/models/index.js";
export default (
follower: { id: User["id"]; host: User["host"]; uri: User["host"] },
followee: { id: User["id"]; host: User["host"]; uri: User["host"] },
requestId?: string,
) => {
const follow = {
id: requestId ?? `${config.url}/follows/${follower.id}/${followee.id}`,
type: "Follow",
actor: Users.isLocalUser(follower)
? `${config.url}/users/${follower.id}`
: follower.uri,
object: Users.isLocalUser(followee)
? `${config.url}/users/${followee.id}`
: followee.uri,
} as any;
return follow;
};

View file

@ -6,6 +6,7 @@ import {
isAllowedServer,
isBlockedServer,
isSelfHost,
renderFollow,
} from "backend-rs";
import { apGet } from "./request.js";
import type { IObject, ICollection, IOrderedCollection } from "./type.js";
@ -24,7 +25,6 @@ import { renderPerson } from "@/remote/activitypub/renderer/person.js";
import renderQuestion from "@/remote/activitypub/renderer/question.js";
import renderCreate from "@/remote/activitypub/renderer/create.js";
import { renderActivity } from "@/remote/activitypub/renderer/index.js";
import renderFollow from "@/remote/activitypub/renderer/follow.js";
import { apLogger } from "@/remote/activitypub/logger.js";
import { IsNull, Not } from "typeorm";

View file

@ -9,7 +9,12 @@ import renderKey from "@/remote/activitypub/renderer/key.js";
import { renderPerson } from "@/remote/activitypub/renderer/person.js";
import renderEmoji from "@/remote/activitypub/renderer/emoji.js";
import { inbox as processInbox } from "@/queue/index.js";
import { fetchMeta, getInstanceActor, isSelfHost } from "backend-rs";
import {
fetchMeta,
getInstanceActor,
isSelfHost,
renderFollow,
} from "backend-rs";
import {
Notes,
Users,
@ -24,7 +29,6 @@ import {
checkFetch,
getSignatureUser,
} from "@/remote/activitypub/check-fetch.js";
import renderFollow from "@/remote/activitypub/renderer/follow.js";
import Featured from "./activitypub/featured.js";
import Following from "./activitypub/following.js";
import Followers from "./activitypub/followers.js";

View file

@ -1,5 +1,4 @@
import { renderActivity } from "@/remote/activitypub/renderer/index.js";
import renderFollow from "@/remote/activitypub/renderer/follow.js";
import { renderUndo } from "@/remote/activitypub/renderer/undo.js";
import { renderBlock } from "@/remote/activitypub/renderer/block.js";
import { deliver } from "@/queue/index.js";
@ -20,6 +19,7 @@ import {
publishToMainStream,
publishToUserStream,
UserEvent,
renderFollow,
} from "backend-rs";
import { getActiveWebhooks } from "@/misc/webhook-cache.js";
import { webhookDeliver } from "@/queue/index.js";

View file

@ -156,7 +156,7 @@ async function fetchFaviconUrl(
// TODO
//timeout: 10000,
agent: getAgentByUrl,
size: 1024 * 1024
size: 1024 * 1024,
});
if (favicon.ok) {

View file

@ -1,5 +1,4 @@
import { renderActivity } from "@/remote/activitypub/renderer/index.js";
import renderFollow from "@/remote/activitypub/renderer/follow.js";
import renderAccept from "@/remote/activitypub/renderer/accept.js";
import renderReject from "@/remote/activitypub/renderer/reject.js";
import { deliver } from "@/queue/index.js";
@ -23,6 +22,7 @@ import {
publishToMainStream,
publishToUserStream,
UserEvent,
renderFollow,
} from "backend-rs";
import { createNotification } from "@/services/create-notification.js";
import { isDuplicateKeyValueError } from "@/misc/is-duplicate-key-value-error.js";

View file

@ -3,9 +3,9 @@ import {
publishToMainStream,
publishToUserStream,
UserEvent,
renderFollow,
} from "backend-rs";
import { renderActivity } from "@/remote/activitypub/renderer/index.js";
import renderFollow from "@/remote/activitypub/renderer/follow.js";
import { renderUndo } from "@/remote/activitypub/renderer/undo.js";
import renderReject from "@/remote/activitypub/renderer/reject.js";
import { deliver, webhookDeliver } from "@/queue/index.js";

View file

@ -1,5 +1,4 @@
import { renderActivity } from "@/remote/activitypub/renderer/index.js";
import renderFollow from "@/remote/activitypub/renderer/follow.js";
import renderReject from "@/remote/activitypub/renderer/reject.js";
import { deliver, webhookDeliver } from "@/queue/index.js";
import {
@ -7,6 +6,7 @@ import {
publishToMainStream,
publishToUserStream,
UserEvent,
renderFollow,
} from "backend-rs";
import type { ILocalUser, IRemoteUser } from "@/models/entities/user.js";
import { Users, FollowRequests, Followings } from "@/models/index.js";

View file

@ -1,8 +1,7 @@
import { renderActivity } from "@/remote/activitypub/renderer/index.js";
import renderFollow from "@/remote/activitypub/renderer/follow.js";
import renderAccept from "@/remote/activitypub/renderer/accept.js";
import { deliver } from "@/queue/index.js";
import { Event, publishToMainStream } from "backend-rs";
import { Event, publishToMainStream, renderFollow } from "backend-rs";
import { insertFollowingDoc } from "../create.js";
import type { User, CacheableUser } from "@/models/entities/user.js";
import { FollowRequests, Users } from "@/models/index.js";

View file

@ -1,8 +1,7 @@
import { renderActivity } from "@/remote/activitypub/renderer/index.js";
import renderFollow from "@/remote/activitypub/renderer/follow.js";
import { renderUndo } from "@/remote/activitypub/renderer/undo.js";
import { deliver } from "@/queue/index.js";
import { Event, publishToMainStream } from "backend-rs";
import { Event, publishToMainStream, renderFollow } from "backend-rs";
import { IdentifiableError } from "@/misc/identifiable-error.js";
import type { User } from "@/models/entities/user.js";
import { Users, FollowRequests } from "@/models/index.js";

View file

@ -1,9 +1,8 @@
import { renderActivity } from "@/remote/activitypub/renderer/index.js";
import renderFollow from "@/remote/activitypub/renderer/follow.js";
import { deliver } from "@/queue/index.js";
import type { User } from "@/models/entities/user.js";
import { Blockings, FollowRequests, Users } from "@/models/index.js";
import { Event, genIdAt, publishToMainStream } from "backend-rs";
import { Event, genIdAt, publishToMainStream, renderFollow } from "backend-rs";
import { createNotification } from "@/services/create-notification.js";
import { config } from "@/config.js";