Skip to content

Commit

Permalink
refactor!: update routes (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwaa committed Feb 6, 2024
1 parent 3b9b590 commit 3a1da13
Show file tree
Hide file tree
Showing 16 changed files with 42 additions and 46 deletions.
2 changes: 1 addition & 1 deletion crates/api_apub/src/activities/activity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@ pub async fn redirect(
// ActivitiesRedirect { activity_id }: ActivitiesRedirect,
Path(activity_id): Path<String>,
) -> impl IntoResponse {
Redirect::permanent(&format!("/a/{activity_id}")).into_response()
Redirect::permanent(&format!("/activities/{activity_id}")).into_response()
}
4 changes: 2 additions & 2 deletions crates/api_apub/src/activities/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ pub fn routes() -> Router {
Router::new()
// .typed_get(activity::handler)
// .typed_get(activity::redirect)
.route("/a/:activity", get(activity::handler))
.route("/activities/:activity", get(activity::redirect))
.route("/activities/:activity", get(activity::handler))
.route("/a/:activity", get(activity::redirect))
}
4 changes: 2 additions & 2 deletions crates/api_apub/src/posts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ pub fn routes() -> Router {
// .typed_get(object::handler)
// .typed_get(object::redirect)
.route("/notice/*notice", get(notice::notice))
.route("/o/*object", get(post::handler))
.route("/objects/*object", get(post::redirect))
.route("/posts/*post", get(post::handler))
.route("/p/*post", get(post::redirect))
}
2 changes: 1 addition & 1 deletion crates/api_apub/src/posts/notice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub async fn notice(Path(base64_url): Path<String>) -> impl IntoResponse {
|_| Err(AppError::not_found("Record", &base64_url)),
|utf8_url| match String::from_utf8(utf8_url) {
Ok(url) if url.starts_with("https://") => {
Ok(Redirect::permanent(&format!("/o/{url}")).into_response())
Ok(Redirect::permanent(&format!("/posts/{url}")).into_response())
}
_ => Err(AppError::not_found("Record", &base64_url)),
},
Expand Down
12 changes: 6 additions & 6 deletions crates/api_apub/src/posts/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ use sea_orm::EntityTrait;
#[debug_handler]
pub async fn handler(
// Objects { object }: Objects,
Path(object): Path<String>,
Path(post_id): Path<String>,
data: Data<AppData>,
) -> Result<FederationJson<WithContext<Note>>, AppError> {
tracing::info!("Reading object {}", object);
tracing::info!("Reading post {}", post_id);

let object_url = hatsu_utils::url::generate_object_url(data.domain(), object)?;
let post_url = hatsu_utils::url::generate_post_url(data.domain(), post_id)?;

match Post::find_by_id(&object_url.to_string())
match Post::find_by_id(&post_url.to_string())
.one(&data.conn)
.await?
{
Expand All @@ -55,7 +55,7 @@ pub async fn handler(
#[debug_handler]
pub async fn redirect(
// ObjectsRedirect { object }: ObjectsRedirect
Path(object): Path<String>,
Path(post_id): Path<String>,
) -> impl IntoResponse {
Redirect::permanent(&format!("/o/{object}")).into_response()
Redirect::permanent(&format!("/posts/{post_id}")).into_response()
}
16 changes: 8 additions & 8 deletions crates/api_apub/src/users/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ pub fn routes() -> Router {
// .typed_get(user_followers::redirect)
// .typed_get(user_outbox::handler)
// .typed_get(user_outbox::redirect)
.route("/u/:user", get(user::handler))
.route("/users/:user", get(user::redirect))
.route("/u/:user/followers", get(user_followers::handler))
.route("/users/:user/followers", get(user_followers::redirect))
.route("/u/:user/following", get(user_following::handler))
.route("/users/:user/following", get(user_following::redirect))
.route("/u/:user/outbox", get(user_outbox::handler))
.route("/users/:user/outbox", get(user_outbox::redirect))
.route("/u/:user", get(user::redirect))
.route("/u/:user/followers", get(user_followers::redirect))
.route("/u/:user/following", get(user_following::redirect))
.route("/u/:user/outbox", get(user_outbox::redirect))
.route("/u/:user/inbox", post(user_inbox::handler))
.route("/users/:user", get(user::handler))
.route("/users/:user/followers", get(user_followers::handler))
.route("/users/:user/following", get(user_following::handler))
.route("/users/:user/outbox", get(user_outbox::handler))
.route("/users/:user/inbox", post(user_inbox::handler))
}
3 changes: 1 addition & 2 deletions crates/api_apub/src/users/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ pub async fn handler(
Path(name): Path<String>,
data: Data<AppData>,
) -> Result<FederationJson<WithContext<Service>>, AppError> {
// let id = format!("https://{}/u/{}", data.domain(), &name);
let url = hatsu_utils::url::generate_user_url(data.domain(), &name)?;
// "@context": [
// "https://www.w3.org/ns/activitystreams",
Expand Down Expand Up @@ -64,5 +63,5 @@ pub async fn redirect(
// UsersRedirect { name }: UsersRedirect,
Path(name): Path<String>,
) -> impl IntoResponse {
Redirect::permanent(&format!("/u/{name}")).into_response()
Redirect::permanent(&format!("/users/{name}")).into_response()
}
2 changes: 1 addition & 1 deletion crates/api_apub/src/users/user_followers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,5 @@ pub async fn redirect(
// UsersFollowersRedirect { name }: UsersFollowersRedirect,
Path(name): Path<String>,
) -> impl IntoResponse {
Redirect::permanent(&format!("/u/{name}/followers")).into_response()
Redirect::permanent(&format!("/users/{name}/followers")).into_response()
}
2 changes: 1 addition & 1 deletion crates/api_apub/src/users/user_following.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,5 @@ pub async fn redirect(
// UsersFollowingRedirect { name }: UsersFollowingRedirect,
Path(name): Path<String>,
) -> impl IntoResponse {
Redirect::permanent(&format!("/u/{name}/followers")).into_response()
Redirect::permanent(&format!("/users/{name}/followers")).into_response()
}
2 changes: 1 addition & 1 deletion crates/api_apub/src/users/user_outbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,5 @@ pub async fn redirect(
// UsersOutboxRedirect { name }: UsersOutboxRedirect,
Path(name): Path<String>,
) -> impl IntoResponse {
Redirect::permanent(&format!("/u/{name}/outbox")).into_response()
Redirect::permanent(&format!("/users/{name}/outbox")).into_response()
}
2 changes: 1 addition & 1 deletion crates/apub/src/activities/following/accept_follow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub struct AcceptFollow {
/// <https://github.com/LemmyNet/activitypub-federation-rust/blob/7bb17f21d59b0aed6126d8a8a0cd60897cb02e6d/examples/local_federation/activities/accept.rs>
impl AcceptFollow {
pub async fn new(follow: Follow, data: &Data<AppData>) -> Result<WithContext<Self>, AppError> {
// 被关注者(本地账号),https://{}/u/{}
// 被关注者(本地账号),https://{}/users/{}
let user: ApubUser = follow.object.dereference_local(data).await?;
// 关注者
let person = follow.actor.clone().dereference(data).await?;
Expand Down
17 changes: 7 additions & 10 deletions crates/apub/src/collections/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ pub fn generate_collection_page_url(collection_id: &Url, page: u64) -> Result<Ur
pub struct Collection {
#[serde(rename = "type")]
kind: OrderedCollectionType,
// example: https://hatsu.local/u/example.com/collection
// example: https://hatsu.local/users/example.com/collection
id: Url,

// example: https://hatsu.local/u/example.com/collection?page=1
// example: https://hatsu.local/users/example.com/collection?page=1
first: Url,
// example: https://hatsu.local/u/example.com/collection?page=64
// example: https://hatsu.local/users/example.com/collection?page=64
#[serde(skip_serializing_if = "Option::is_none")]
last: Option<Url>,

Expand All @@ -33,19 +33,16 @@ pub struct Collection {
pub struct CollectionPage<T> {
#[serde(rename = "type")]
kind: OrderedCollectionPageType,
// example: https://hatsu.local/u/example.com/collection?page=2
// example: https://hatsu.local/users/example.com/collection?page=2
id: Url,

// example: https://hatsu.local/u/example.com/collection?page=1
// example: https://hatsu.local/users/example.com/collection?page=1
#[serde(skip_serializing_if = "Option::is_none")]
prev: Option<Url>,
// example: https://hatsu.local/u/example.com/collection?page=3
// example: https://hatsu.local/users/example.com/collection?page=3
#[serde(skip_serializing_if = "Option::is_none")]
next: Option<Url>,

// example: https://hatsu.local/u/example.com/collection
// example: https://hatsu.local/users/example.com/collection
part_of: Url,

// collection item list
ordered_items: Vec<T>,
// collection count
Expand Down
14 changes: 7 additions & 7 deletions crates/utils/src/url/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ pub fn absolutize_relative_url(relative_url: &str, domain: &str) -> Result<Url,

/// 创建一个 Activity URL
///
/// Example: <https://hatsu.local/a/80075c4e-a4e1-4b29-8b89-417cf7339be9>
/// Example: <https://hatsu.local/activities/80075c4e-a4e1-4b29-8b89-417cf7339be9>
pub fn generate_activity_url(domain: &str, id: Option<String>) -> Result<Url, AppError> {
Ok(Url::parse(&format!(
"https://{}/a/{}",
"https://{}/activities/{}",
domain,
id.unwrap_or_else(|| Uuid::now_v7().to_string())
))?)
}

/// 创建一个 Object URL
/// 创建一个 Post URL
///
/// Example: <https://hatsu.local/o/https://example.com/foo/bar>
/// Example: <https://hatsu.local/post/https://example.com/foo/bar>
pub fn generate_object_url(domain: &str, id: String) -> Result<Url, AppError> {
match id {
id if id.starts_with("https://") => Ok(Url::parse(&format!("https://{domain}/o/{id}",))?),
id if id.starts_with("https://") => Ok(Url::parse(&format!("https://{domain}/post/{id}",))?),
_ => Err(AppError::new(
format!("Invalid Object ID {id}"),
serde_json::from_str("Object ID need to starts with https://")?,
Expand All @@ -40,10 +40,10 @@ pub fn generate_object_url(domain: &str, id: String) -> Result<Url, AppError> {

/// 创建一个 User URL
///
/// Example: <https://hatsu.local/u/example.com>
/// Example: <https://hatsu.local/user/example.com>
pub fn generate_user_url(domain: &str, id: &str) -> Result<Url, AppError> {
match id {
id if !id.starts_with("https://") => Ok(Url::parse(&format!("https://{domain}/u/{id}",))?),
id if !id.starts_with("https://") => Ok(Url::parse(&format!("https://{domain}/user/{id}",))?),
_ => Err(AppError::new(
format!("Invalid User ID {id}"),
serde_json::from_str("User ID cannot starts with https://")?,
Expand Down
2 changes: 1 addition & 1 deletion docs/src/users/redirecting-with-aoba.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const instance = new URL('https://hatsu.local')

// https://example.com/.well-known/* => https://hatsu.local/.well-known/*
app.use('/.well-known/*'hatsuWellKnown({ instance }))
// https://example.com/posts/foo => https://hatsu.local/o/https://example.com/posts/foo
// https://example.com/posts/foo => https://hatsu.local/posts/https://example.com/posts/foo
app.use('/posts/*', hatsuObject({ instance }))
```

Expand Down
2 changes: 1 addition & 1 deletion docs/src/users/redirecting-with-static-files-and-markup.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ That's it! For `https://example.com/foo/bar`, just add the following tag to the
> Replace `hatsu.local` with your Hatsu instance.
```html
<link rel="alternate" type="application/activity+json" href="https://hatsu.local/o/https://example.com/foo/bar" />
<link rel="alternate" type="application/activity+json" href="https://hatsu.local/posts/https://example.com/foo/bar" />
```
2 changes: 1 addition & 1 deletion docs/src/users/redirecting.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ There are two types of redirects required by Hatsu:

2. Requests accept of type `application/activity+json`, redirecting them to make your page searchable.
- before: `https://example.com/foo/bar`
- after: `https://hatsu.local/o/https://example.com/foo/bar`
- after: `https://hatsu.local/posts/https://example.com/foo/bar`

There are many ways to redirect them and you can pick one you like:

Expand Down

0 comments on commit 3a1da13

Please sign in to comment.