Compare commits

...

2 Commits

Author SHA1 Message Date
emilis f4aae98656 moved timeline from own page to index 2022-12-19 21:56:51 +00:00
emilis be7c415980 wip timeline 2022-12-19 21:23:37 +00:00
7 changed files with 214 additions and 16 deletions

View File

@ -14,7 +14,8 @@ use crate::svc::auth::{AuthError, Claims};
use super::{
servek::{Server, ServerError},
CreateProfileRequest, LoginRequest, NavType, Notification, Redirect, WithNav,
CreateProfileRequest, LoginRequest, NavType, Note, Notification, Profile, Redirect, Timeline,
WithNav,
};
use rust_embed::RustEmbed;
@ -41,6 +42,21 @@ impl Server {
.fallback(routing::get(Self::handler_404))
}
fn get_auth(&self, cookies: Cookies) -> Result<Claims, ServerError> {
match cookies.get(AUTH_COOKIE_NAME) {
Some(cookie) => self
.auth
.get_claims(cookie.value().to_owned())
.map_err(|e| {
if e.expired() {
cookies.remove(Cookie::new(AUTH_COOKIE_NAME, ""));
}
e.into()
}),
None => Err(ServerError::NotLoggedIn),
}
}
fn from_cookies(&self, cookies: Cookies) -> Result<WithNav<Option<Claims>>, ServerError> {
const LOGGED_OUT: Result<WithNav<Option<Claims>>, ServerError> = Ok(WithNav {
obj: None,
@ -68,10 +84,49 @@ impl Server {
Extension(srv): Extension<Server>,
cookies: Cookies,
) -> Result<impl IntoResponse, ServerError> {
Ok((
StatusCode::OK,
response::Html(srv.hb.render("index", &srv.from_cookies(cookies)?)?),
))
// TODO: show actual posts, these are hardcoded currently to check
// out how they look
let profile1 = Box::new(Profile {
username: "@emilis@puff.place".to_string(),
display_name: "dusty rusty".to_string(),
avatar_url: "https://puff.place/files/thumbnail-501ec5ba-37d6-4163-8e8d-3478a689660d"
.to_string(),
});
let profile2 = Box::new(Profile::new("@emilk@another.place".to_string()));
let note1 = Note::new(profile1.clone(), Some("hey there goods".to_string()));
let note2 = Note::new(
profile2,
Some("@emilis@puff.place u dont got shit".to_string()),
);
let note3 = Note::new(profile1, Some("bithces :".to_string()));
match srv.get_auth(cookies) {
Ok(claims) => Ok((
StatusCode::OK,
response::Html(srv.hb.render(
"index",
&WithNav::new(
Timeline {
show_postbox: true,
notes: vec![note1, note2, note3],
},
NavType::LoggedIn,
),
)?),
)
.into_response()),
Err(e) => match e {
ServerError::NotLoggedIn => Ok((
StatusCode::OK,
response::Html(
srv.hb
.render("index", &WithNav::new((), NavType::LoggedOut))?,
),
)
.into_response()),
_ => Err(e),
},
}
}
async fn favicon() -> impl IntoResponse {

View File

@ -2,14 +2,53 @@ use axum::response::{Html, IntoResponse};
use serde::{Deserialize, Serialize};
use tower_cookies::{Cookie, Cookies};
use crate::svc::{
auth::{Auth, AuthError, Claims},
profiles::User,
};
use crate::svc::auth::{AuthError, Claims};
mod html;
pub mod servek;
#[derive(Debug, Clone, Serialize)]
struct Timeline {
pub show_postbox: bool,
pub notes: Vec<Note>,
}
#[derive(Debug, Clone, Serialize)]
struct Note {
pub profile: Box<Profile>,
pub content: Option<String>,
}
impl Note {
fn new(profile: Box<Profile>, content: Option<String>) -> Self {
Self { profile, content }
}
}
#[derive(Debug, Clone, Serialize)]
struct Profile {
pub display_name: String,
pub username: String,
pub avatar_url: String,
}
impl Profile {
fn new(username: String) -> Self {
let display_name = username
.strip_prefix('@')
.unwrap()
.split_once('@')
.unwrap()
.0
.to_string();
Profile {
display_name,
username,
avatar_url: "/favicon.svg".to_string(),
}
}
}
#[derive(Debug, Clone, Serialize)]
struct Notification {
pub message: String,

View File

@ -79,6 +79,7 @@ impl Server {
pub(super) enum ServerError {
Internal(String),
NotFound,
NotLoggedIn,
BadRequest(String),
}
@ -126,6 +127,7 @@ impl IntoResponse for ServerError {
)
.into_response(),
ServerError::BadRequest(err) => (StatusCode::BAD_REQUEST, err).into_response(),
ServerError::NotLoggedIn => (StatusCode::UNAUTHORIZED, "unauthorized").into_response(),
}
}
}

View File

@ -103,8 +103,8 @@ pub enum AuthError {
}
impl AuthError {
pub fn expired(self) -> bool {
self == Self::Expired
pub fn expired(&self) -> bool {
*self == Self::Expired
}
}

View File

@ -72,7 +72,7 @@ a {
font-weight: bold;
}
.avatar {
.profile-avatar {
width: 128px;
height: 128px;
border: 3px solid rebeccapurple;
@ -152,10 +152,91 @@ nav ul li {
}
nav>ul>li>a {
/* color: #aaa; */
/* background-color:#FF0; */
display: block;
line-height: 2em;
padding: 0.5em 0.5em;
text-decoration: none;
}
.avatar {
width: 42px;
height: 42px;
}
button {
background-color: rgba(102, 51, 153, 0.75);
color: rgb(204, 179, 230);
border-color: rgba(102, 51, 153, 0.25);
border-style: initial;
font-size: large;
}
#postbox {
/* border: 3px solid rebeccapurple; */
/* padding: 5px; */
margin: 0 0 30px 0;
height: fit-content;
display: flex;
/* text-align: left; */
}
#timeline {
margin: 0;
padding: 0;
}
#timeline li {
display: grid;
list-style-type: none;
width: 85vw;
}
#post-content {
color: rgb(204, 179, 230);
background-color: rebeccapurple;
border-width: 0px;
height: 60px;
width: 85vw;
}
.note {
display: flex;
}
.content {
display: inline-flex;
flex-direction: column;
}
div>p {
font-size: small;
margin-left: 2px;
margin-right: 2px;
}
.display-name {
font-weight: bold;
}
.username {
color: rgba(102, 51, 153, 0.5);
}
.user-info {
display: inline-flex;
margin-top: 0px;
}
#timeline-box {
color: rgb(204, 179, 230);
display: flex;
flex-direction: column;
}
.note:nth-child(even) {
background: rgba(102, 51, 153, 0.25);
}
.note:nth-child(odd) {
background: rgba(102, 51, 153, 0.125);
}

View File

@ -9,7 +9,28 @@
<body>
{{> (lookup this "nav_type") }}
<h1>hi {{obj.username}}</h1>
<div class="main" id="timeline-box">
{{#if obj.show_postbox}}
<div id="postbox">
<textarea id="post-content" placeholder="im tosti"></textarea>
<button>send</button>
</div>
{{/if}}
<div id="timeline">
{{#each obj.notes}}
<div class="note">
<img class="avatar" src="{{this.profile.avatar_url}}" alt="example" />
<div class="content">
<div class="user-info">
<p class="display-name">{{this.profile.display_name}}</p>
<p class="username">{{this.profile.username}}</p>
</div>
<p class="post-text">{{this.content}}</p>
</div>
</div>
{{/each}}
</div>
</div>
</body>
</html>

View File

@ -12,7 +12,7 @@
<div class="main">
<div class="profile">
<div class="profile-header">
<img class="avatar" src="{{obj.user.avatar_uri}}" alt="{{obj.user.username}}'s avatar" />
<img class="profile-avatar" src="{{obj.user.avatar_uri}}" alt="{{obj.user.username}}'s avatar" />
<p class="name">{{obj.user.display_name}}</p>
</div>
<div class="profile-bio">