diff --git a/src/database/models/posts.rs b/src/database/models/posts.rs index 38fd469..aa5edc6 100644 --- a/src/database/models/posts.rs +++ b/src/database/models/posts.rs @@ -1,4 +1,3 @@ -use serde_json::Value; use uuid::Uuid; use sqlx::Row; use crate::database::Database; diff --git a/src/web/routes/api/webhooks/github/events/mod.rs b/src/web/routes/api/webhooks/github/events/mod.rs index 816da11..cf2b342 100644 --- a/src/web/routes/api/webhooks/github/events/mod.rs +++ b/src/web/routes/api/webhooks/github/events/mod.rs @@ -2,17 +2,16 @@ use std::{borrow::BorrowMut, sync::Mutex}; use actix_web::{web::Data, HttpResponse, HttpResponseBuilder, Result}; -use crate::database::{models::{self, tokens::Token}, Database}; +use crate::database::{models, Database}; use super::types::ReleaseEvent; -pub async fn release_handler(db: Data>, token: Token, body: ReleaseEvent, raw_body: String) -> Result { +pub async fn release_handler(db: Data>, body: ReleaseEvent, raw_body: String) -> Result { if body.action != "released" { return Ok(HttpResponse::Ok()); } - let title = format!("(New release {}:{}) {}", body.repository.full_name, body.release.tag_name, body.release.name.unwrap_or("No title provided".into())); let origin_url = body.repository.html_url.clone(); let descr = body.release.body.unwrap_or("No body provided".into()); diff --git a/src/web/routes/api/webhooks/github/mod.rs b/src/web/routes/api/webhooks/github/mod.rs index 53d551b..9f6f7cd 100644 --- a/src/web/routes/api/webhooks/github/mod.rs +++ b/src/web/routes/api/webhooks/github/mod.rs @@ -3,9 +3,9 @@ pub mod events; use std::{borrow::BorrowMut, sync::Mutex}; -use actix_web::{http::header, post, web::{self, Bytes, Data}, HttpRequest, HttpResponse, Responder, Result, Scope}; +use actix_web::{web::{self, Bytes, Data}, HttpRequest, HttpResponse, Responder, Result}; -use crate::database::{models, Database}; +use crate::database::{models::{self, Permissions}, Database}; pub async fn handler(req: HttpRequest, token: web::Path, body: Bytes, db: Data>) -> Result { @@ -23,6 +23,10 @@ pub async fn handler(req: HttpRequest, token: web::Path, body: Bytes, db return Ok(HttpResponse::Unauthorized()); }; + if !token.permissions.contains(Permissions::MAKE_POST) { + return Ok(HttpResponse::Unauthorized()); + } + let Some(event_type) = req.headers().get("X-GitHub-Event") else { log::debug!("No X-GitHub-Event header"); return Ok(HttpResponse::BadRequest()); @@ -48,7 +52,7 @@ pub async fn handler(req: HttpRequest, token: web::Path, body: Bytes, db }; match event { - types::Event::Release(body) => events::release_handler(db, token, body, json).await, + types::Event::Release(body) => events::release_handler(db, body, json).await, _ => { // dbg!(json); Ok(HttpResponse::Ok()) diff --git a/src/web/routes/mod.rs b/src/web/routes/mod.rs index 5e338ea..7c8d702 100644 --- a/src/web/routes/mod.rs +++ b/src/web/routes/mod.rs @@ -8,20 +8,20 @@ use askama::Template; use crate::{database::Database, web::templates::IndexTemplate}; -use super::templates::IndexTemplatePost; // NOTE: Not usefull to have database here but just so u know how -pub async fn index(_: Data>) -> Result { +pub async fn index(db: Data>) -> Result { + + let posts = match crate::database::models::posts::Post::get_last_n(&mut db.lock().unwrap(), 10).await { + Ok(p) => p, + _ => { + vec![] + } + }; + let html = IndexTemplate { - posts: vec![ - IndexTemplatePost { - image: String::from("/static/assets/uwu.jpg"), - title: String::from("Cutie"), - description: String::from("Yes you are ;3"), - url: String::from("https://djc.github.io/askama/template_expansion.html") - } - ], + posts, title: String::from("Very cool mcoranges website :3"), }.render().expect("Failed to render index.html"); diff --git a/src/web/templates.rs b/src/web/templates.rs index 2daf485..a17aec2 100644 --- a/src/web/templates.rs +++ b/src/web/templates.rs @@ -1,16 +1,12 @@ use askama::Template; +use crate::database::models::posts::Post; + #[derive(Debug, Clone, Template)] #[template(path = "index.html")] pub struct IndexTemplate{ pub title: String, - pub posts: Vec + pub posts: Vec } -#[derive(Debug, Clone)] -pub struct IndexTemplatePost { - pub image: String, - pub title: String, - pub description: String, - pub url: String -} + diff --git a/templates/index.html b/templates/index.html index d6560ae..e03dbae 100644 --- a/templates/index.html +++ b/templates/index.html @@ -23,10 +23,10 @@
{% for post in posts %}
- post img + post img -

{{post.title}}

-

{{post.description}}

+

{{post.title}}

+

{{post.descr}}

{% endfor %}