From: Felix Ableitner Date: Wed, 18 Dec 2019 00:59:47 +0000 (+0100) Subject: Implement webfinger (fixes #149) X-Git-Url: http://these/git/ui/assets/static/gitweb.css?a=commitdiff_plain;h=3d9f7d28d69bf077258c1c8e0803be79a12e426a;p=lemmy.git Implement webfinger (fixes #149) --- diff --git a/server/src/db/community.rs b/server/src/db/community.rs index 57b962d1..74579535 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 3d3865e8..21407e58 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 5337edcf..c735688b 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()); +}