Add support for avatars, and fetch remote ones
This commit is contained in:
parent
a357dae45d
commit
aa5fa11218
2
migrations/2018-09-03-102510_users_add_avatar/down.sql
Normal file
2
migrations/2018-09-03-102510_users_add_avatar/down.sql
Normal file
@ -0,0 +1,2 @@
|
||||
-- This file should undo anything in `up.sql`
|
||||
ALTER TABLE users DROP COLUMN avatar_id;
|
2
migrations/2018-09-03-102510_users_add_avatar/up.sql
Normal file
2
migrations/2018-09-03-102510_users_add_avatar/up.sql
Normal file
@ -0,0 +1,2 @@
|
||||
-- Your SQL goes here
|
||||
ALTER TABLE users ADD COLUMN avatar_id INTEGER REFERENCES medias(id) ON DELETE CASCADE;
|
@ -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");
|
||||
}
|
||||
}
|
||||
|
@ -155,6 +155,7 @@ table! {
|
||||
public_key -> Text,
|
||||
shared_inbox_url -> Nullable<Varchar>,
|
||||
followers_endpoint -> Varchar,
|
||||
avatar_id -> Nullable<Int4>,
|
||||
}
|
||||
}
|
||||
|
||||
@ -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));
|
||||
|
@ -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<String>,
|
||||
pub public_key: String,
|
||||
pub shared_inbox_url: Option<String>,
|
||||
pub followers_endpoint: String
|
||||
pub followers_endpoint: String,
|
||||
pub avatar_id: Option<i32>,
|
||||
}
|
||||
|
||||
#[derive(Insertable)]
|
||||
@ -83,7 +85,8 @@ pub struct NewUser {
|
||||
pub private_key: Option<String>,
|
||||
pub public_key: String,
|
||||
pub shared_inbox_url: Option<String>,
|
||||
pub followers_endpoint: String
|
||||
pub followers_endpoint: String,
|
||||
pub avatar_id: Option<i32>,
|
||||
}
|
||||
|
||||
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 {
|
||||
@ -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
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,8 @@
|
||||
<div class="user">
|
||||
<div>
|
||||
<img src="{{ user.avatar }}" alt="{{ "{{ name}}'s avatar'" | _(name=user.name) }}">
|
||||
</div>
|
||||
|
||||
<h1>
|
||||
{{ user.name }}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user