Make it possible to choose an avatar

This commit is contained in:
Bat 2018-09-03 13:04:17 +01:00
parent 95ba99a6be
commit e2e7d10929
3 changed files with 15 additions and 1 deletions

View File

@ -491,6 +491,13 @@ impl User {
} }
}) })
} }
pub fn set_avatar(&self, conn: &PgConnection, id: i32) {
diesel::update(self)
.set(users::avatar_id.eq(id))
.execute(conn)
.expect("Couldn't update user avatar");
}
} }
impl<'a, 'r> FromRequest<'a, 'r> for User { impl<'a, 'r> FromRequest<'a, 'r> for User {

View File

@ -68,6 +68,7 @@ fn main() {
routes::medias::upload, routes::medias::upload,
routes::medias::details, routes::medias::details,
routes::medias::delete, routes::medias::delete,
routes::medias::set_avatar,
routes::medias::static_files, routes::medias::static_files,
routes::notifications::paginated_notifications, routes::notifications::paginated_notifications,

View File

@ -104,8 +104,14 @@ fn delete(id: i32, _user: User, conn: DbConn) -> Redirect {
Redirect::to(uri!(list)) Redirect::to(uri!(list))
} }
#[get("/medias/<id>/avatar")]
fn set_avatar(id: i32, user: User, conn: DbConn) -> Redirect {
let media = Media::get(&*conn, id).expect("Media to delete not found");
user.set_avatar(&*conn, media.id);
Redirect::to(uri!(details: id = id))
}
#[get("/static/media/<file..>", rank = 1)] #[get("/static/media/<file..>", rank = 1)]
fn static_files(file: PathBuf) -> Option<NamedFile> { fn static_files(file: PathBuf) -> Option<NamedFile> {
NamedFile::open(Path::new("media/").join(file)).ok() NamedFile::open(Path::new("media/").join(file)).ok()
} }