From aa5fa11218cba1de2ec6a25fe27bab5a221eb5d3 Mon Sep 17 00:00:00 2001 From: Bat Date: Mon, 3 Sep 2018 12:17:59 +0100 Subject: [PATCH] Add support for avatars, and fetch remote ones --- .../down.sql | 2 ++ .../2018-09-03-102510_users_add_avatar/up.sql | 2 ++ plume-models/src/medias.rs | 25 ++++++++++++++++- plume-models/src/schema.rs | 2 +- plume-models/src/users.rs | 27 ++++++++++++++----- templates/users/header.html.tera | 6 ++++- 6 files changed, 54 insertions(+), 10 deletions(-) create mode 100644 migrations/2018-09-03-102510_users_add_avatar/down.sql create mode 100644 migrations/2018-09-03-102510_users_add_avatar/up.sql diff --git a/migrations/2018-09-03-102510_users_add_avatar/down.sql b/migrations/2018-09-03-102510_users_add_avatar/down.sql new file mode 100644 index 00000000..cf822ecd --- /dev/null +++ b/migrations/2018-09-03-102510_users_add_avatar/down.sql @@ -0,0 +1,2 @@ +-- This file should undo anything in `up.sql` +ALTER TABLE users DROP COLUMN avatar_id; diff --git a/migrations/2018-09-03-102510_users_add_avatar/up.sql b/migrations/2018-09-03-102510_users_add_avatar/up.sql new file mode 100644 index 00000000..5a97db2a --- /dev/null +++ b/migrations/2018-09-03-102510_users_add_avatar/up.sql @@ -0,0 +1,2 @@ +-- Your SQL goes here +ALTER TABLE users ADD COLUMN avatar_id INTEGER REFERENCES medias(id) ON DELETE CASCADE; diff --git a/plume-models/src/medias.rs b/plume-models/src/medias.rs index 55fa9911..c78edbd9 100644 --- a/plume-models/src/medias.rs +++ b/plume-models/src/medias.rs @@ -64,11 +64,34 @@ impl Media { } pub fn url(&self, conn: &PgConnection) -> String { - ap_url(format!("{}/static/{}", Instance::get_local(conn).unwrap().public_domain, self.file_path)) + if self.is_remote { + self.remote_url.clone().unwrap_or(String::new()) + } else { + ap_url(format!("{}/static/{}", Instance::get_local(conn).unwrap().public_domain, self.file_path)) + } } pub fn delete(&self, conn: &PgConnection) { fs::remove_file(self.file_path.as_str()).expect("Couldn't delete media from disk"); diesel::delete(self).execute(conn).expect("Couldn't remove media from DB"); } + + pub fn save_remote(conn: &PgConnection, url: String) -> Media { + Media::insert(conn, NewMedia { + file_path: String::new(), + alt_text: String::new(), + is_remote: true, + remote_url: Some(url), + sensitive: false, + content_warning: None, + owner_id: 1 // It will be owned by the admin during an instant, but set_owner will be called just after + }) + } + + pub fn set_owner(&self, conn: &PgConnection, id: i32) { + diesel::update(self) + .set(medias::owner_id.eq(id)) + .execute(conn) + .expect("Couldn't update Media.owner_id"); + } } diff --git a/plume-models/src/schema.rs b/plume-models/src/schema.rs index 6fa2dc74..eacc19cc 100644 --- a/plume-models/src/schema.rs +++ b/plume-models/src/schema.rs @@ -155,6 +155,7 @@ table! { public_key -> Text, shared_inbox_url -> Nullable, followers_endpoint -> Varchar, + avatar_id -> Nullable, } } @@ -165,7 +166,6 @@ joinable!(comments -> posts (post_id)); joinable!(comments -> users (author_id)); joinable!(likes -> posts (post_id)); joinable!(likes -> users (user_id)); -joinable!(medias -> users (owner_id)); joinable!(mentions -> comments (comment_id)); joinable!(mentions -> posts (post_id)); joinable!(mentions -> users (mentioned_id)); diff --git a/plume-models/src/users.rs b/plume-models/src/users.rs index ad0373b7..de2ebc10 100644 --- a/plume-models/src/users.rs +++ b/plume-models/src/users.rs @@ -37,6 +37,7 @@ use blog_authors::BlogAuthor; use follows::Follow; use instance::*; use likes::Like; +use medias::Media; use post_authors::PostAuthor; use posts::Post; use reshares::Reshare; @@ -64,7 +65,8 @@ pub struct User { pub private_key: Option, pub public_key: String, pub shared_inbox_url: Option, - pub followers_endpoint: String + pub followers_endpoint: String, + pub avatar_id: Option, } #[derive(Insertable)] @@ -83,7 +85,8 @@ pub struct NewUser { pub private_key: Option, pub public_key: String, pub shared_inbox_url: Option, - pub followers_endpoint: String + pub followers_endpoint: String, + pub avatar_id: Option, } const USER_PREFIX: &'static str = "@"; @@ -195,7 +198,11 @@ impl User { }) } }; - User::insert(conn, NewUser { + + let avatar = Media::save_remote(conn, acct.object.object_props.icon_image().expect("User::from_activity: icon error") + .object_props.url_string().expect("User::from_activity: icon.url error")); + + let user = User::insert(conn, NewUser { username: acct.object.ap_actor_props.preferred_username_string().expect("User::from_activity: preferredUsername error"), display_name: acct.object.object_props.name_string().expect("User::from_activity: name error"), outbox_url: acct.object.ap_actor_props.outbox_string().expect("User::from_activity: outbox error"), @@ -211,8 +218,12 @@ impl User { private_key: None, shared_inbox_url: acct.object.ap_actor_props.endpoints_endpoint() .and_then(|e| e.shared_inbox_string()).ok(), - followers_endpoint: acct.object.ap_actor_props.followers_string().expect("User::from_activity: followers error") - }) + followers_endpoint: acct.object.ap_actor_props.followers_string().expect("User::from_activity: followers error"), + avatar_id: Some(avatar.id) + }); + avatar.set_owner(conn, user.id); + + user } pub fn hash_pass(pass: String) -> String { @@ -238,7 +249,7 @@ impl User { if self.inbox_url.len() == 0 { diesel::update(self) .set(users::inbox_url.eq(instance.compute_box(USER_PREFIX, self.username.clone(), "inbox"))) - .get_result::(conn).expect("Couldn't update inbox URL"); + .get_result::(conn).expect("Couldn't update inbox URL"); } if self.ap_url.len() == 0 { @@ -438,6 +449,7 @@ impl User { } else { json!(self.get_fqn(conn)) }; + json["avatar"] = json!(self.avatar_id.and_then(|id| Media::get(conn, id).map(|m| m.url(conn))).unwrap_or("/static/default-avatar.png".to_string())); json } @@ -556,7 +568,8 @@ impl NewUser { public_key: String::from_utf8(pub_key).unwrap(), private_key: Some(String::from_utf8(priv_key).unwrap()), shared_inbox_url: None, - followers_endpoint: String::from("") + followers_endpoint: String::from(""), + avatar_id: None }) } } diff --git a/templates/users/header.html.tera b/templates/users/header.html.tera index 75818955..5c2c3582 100644 --- a/templates/users/header.html.tera +++ b/templates/users/header.html.tera @@ -1,4 +1,8 @@
+
+ {{ +
+

{{ user.name }} @@ -7,7 +11,7 @@ {% if user.is_admin %} {{ "Admin" | _ }} {% endif %} - + {% if is_self %} {{ "It is you" | _ }} {% endif %}