From ae3fccf7012905830c995e2f7b939bb242c8c44c Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Wed, 18 Dec 2019 01:59:47 +0100 Subject: [PATCH] Implement webfinger (fixes #149) --- server/src/db/community.rs | 4 +++ server/src/db/user.rs | 20 ++++++----- server/src/feeds.rs | 6 ++-- server/src/lib.rs | 1 + server/src/main.rs | 6 +++- server/src/settings.rs | 1 - server/src/webfinger.rs | 69 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 94 insertions(+), 13 deletions(-) create mode 100644 server/src/webfinger.rs diff --git a/server/src/db/community.rs b/server/src/db/community.rs index 57b962d13..74579535e 100644 --- a/server/src/db/community.rs +++ b/server/src/db/community.rs @@ -68,6 +68,10 @@ impl Community { .filter(name.eq(community_name)) .first::(conn) } + + pub fn get_community_url(community_name: &str) -> String { + format!("https://{}/c/{}", Settings::get().hostname, community_name) + } } #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)] diff --git a/server/src/db/user.rs b/server/src/db/user.rs index 3d3865e8d..21407e58c 100644 --- a/server/src/db/user.rs +++ b/server/src/db/user.rs @@ -132,23 +132,27 @@ impl User_ { .unwrap() } + pub fn find_by_username(conn: &PgConnection, username: &str) -> Result { + user_.filter(name.eq(username)).first::(conn) + } + + pub fn find_by_email(conn: &PgConnection, from_email: &str) -> Result { + user_.filter(email.eq(from_email)).first::(conn) + } + pub fn find_by_email_or_username( conn: &PgConnection, username_or_email: &str, ) -> Result { if is_email_regex(username_or_email) { - user_ - .filter(email.eq(username_or_email)) - .first::(conn) + User_::find_by_email(conn, username_or_email) } else { - user_ - .filter(name.eq(username_or_email)) - .first::(conn) + User_::find_by_username(conn, username_or_email) } } - pub fn find_by_email(conn: &PgConnection, from_email: &str) -> Result { - user_.filter(email.eq(from_email)).first::(conn) + pub fn get_user_profile_url(username: &str) -> String { + format!("https://{}/u/{}", Settings::get().hostname, username) } pub fn find_by_jwt(conn: &PgConnection, jwt: &str) -> Result { diff --git a/server/src/feeds.rs b/server/src/feeds.rs index 5337edcf9..c735688be 100644 --- a/server/src/feeds.rs +++ b/server/src/feeds.rs @@ -110,8 +110,8 @@ fn get_feed_user(sort_type: &SortType, user_name: String) -> Result Result) -> HttpResponse { + // NOTE: Calling the parameter "account" maybe doesn't really make sense, but should give us the + // best compatibility with existing implementations. We could also support an alternative name + // like "group", and encourage others to use that. + let community_identifier = info.resource.replace("acct:", ""); + let split_identifier: Vec<&str> = community_identifier.split("@").collect(); + let community_name = split_identifier[0]; + // It looks like Mastodon does not return webfinger requests for users from other instances, so we + // don't do that either. + if split_identifier.len() != 2 || split_identifier[1] != Settings::get().hostname { + return HttpResponse::NotFound().finish(); + } + + // Make sure the requested community exists. + let conn = establish_connection(); + match Community::read_from_name(&conn, community_name.to_owned()) { + Err(_) => return HttpResponse::NotFound().finish(), + Ok(c) => c, + }; + + let community_url = Community::get_community_url(&community_name); + + let json = json!({ + "subject": info.resource, + "aliases": [ + community_url, + ], + "links": [ + { + "rel": "http://webfinger.net/rel/profile-page", + "type": "text/html", + "href": community_url + }, + { + "rel": "self", + "type": "application/activity+json", + "href": community_url // Yes this is correct, this link doesn't include the `.json` extension + } + // TODO: this also needs to return the subscribe link once that's implemented + //{ + // "rel": "http://ostatus.org/schema/1.0/subscribe", + // "template": "https://my_instance.com/authorize_interaction?uri={uri}" + //} + ] + }); + return HttpResponse::Ok() + .content_type("application/activity+json") + .body(json.to_string()); +}