I THINK THIS IS IT

This commit is contained in:
Gvidas Juknevičius 2024-03-30 19:31:39 +02:00
parent 6b7fd18171
commit 953eb9dabb
Signed by: MCorange
GPG Key ID: 12B1346D720B7FBB
6 changed files with 26 additions and 28 deletions

View File

@ -1,4 +1,3 @@
use serde_json::Value;
use uuid::Uuid; use uuid::Uuid;
use sqlx::Row; use sqlx::Row;
use crate::database::Database; use crate::database::Database;

View File

@ -2,17 +2,16 @@ use std::{borrow::BorrowMut, sync::Mutex};
use actix_web::{web::Data, HttpResponse, HttpResponseBuilder, Result}; 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; use super::types::ReleaseEvent;
pub async fn release_handler(db: Data<Mutex<Database>>, token: Token, body: ReleaseEvent, raw_body: String) -> Result<HttpResponseBuilder> { pub async fn release_handler(db: Data<Mutex<Database>>, body: ReleaseEvent, raw_body: String) -> Result<HttpResponseBuilder> {
if body.action != "released" { if body.action != "released" {
return Ok(HttpResponse::Ok()); 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 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 origin_url = body.repository.html_url.clone();
let descr = body.release.body.unwrap_or("No body provided".into()); let descr = body.release.body.unwrap_or("No body provided".into());

View File

@ -3,9 +3,9 @@ pub mod events;
use std::{borrow::BorrowMut, sync::Mutex}; 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<String>, body: Bytes, db: Data<Mutex<Database>>) -> Result<impl Responder> { pub async fn handler(req: HttpRequest, token: web::Path<String>, body: Bytes, db: Data<Mutex<Database>>) -> Result<impl Responder> {
@ -23,6 +23,10 @@ pub async fn handler(req: HttpRequest, token: web::Path<String>, body: Bytes, db
return Ok(HttpResponse::Unauthorized()); 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 { let Some(event_type) = req.headers().get("X-GitHub-Event") else {
log::debug!("No X-GitHub-Event header"); log::debug!("No X-GitHub-Event header");
return Ok(HttpResponse::BadRequest()); return Ok(HttpResponse::BadRequest());
@ -48,7 +52,7 @@ pub async fn handler(req: HttpRequest, token: web::Path<String>, body: Bytes, db
}; };
match event { 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); // dbg!(json);
Ok(HttpResponse::Ok()) Ok(HttpResponse::Ok())

View File

@ -8,20 +8,20 @@ use askama::Template;
use crate::{database::Database, web::templates::IndexTemplate}; use crate::{database::Database, web::templates::IndexTemplate};
use super::templates::IndexTemplatePost;
// NOTE: Not usefull to have database here but just so u know how // NOTE: Not usefull to have database here but just so u know how
pub async fn index(_: Data<Mutex<Database>>) -> Result<impl Responder> { pub async fn index(db: Data<Mutex<Database>>) -> Result<impl Responder> {
let html = IndexTemplate {
posts: vec![ let posts = match crate::database::models::posts::Post::get_last_n(&mut db.lock().unwrap(), 10).await {
IndexTemplatePost { Ok(p) => p,
image: String::from("/static/assets/uwu.jpg"), _ => {
title: String::from("Cutie"), vec![]
description: String::from("Yes you are ;3"),
url: String::from("https://djc.github.io/askama/template_expansion.html")
} }
], };
let html = IndexTemplate {
posts,
title: String::from("Very cool mcoranges website :3"), title: String::from("Very cool mcoranges website :3"),
}.render().expect("Failed to render index.html"); }.render().expect("Failed to render index.html");

View File

@ -1,16 +1,12 @@
use askama::Template; use askama::Template;
use crate::database::models::posts::Post;
#[derive(Debug, Clone, Template)] #[derive(Debug, Clone, Template)]
#[template(path = "index.html")] #[template(path = "index.html")]
pub struct IndexTemplate{ pub struct IndexTemplate{
pub title: String, pub title: String,
pub posts: Vec<IndexTemplatePost> pub posts: Vec<Post>
} }
#[derive(Debug, Clone)]
pub struct IndexTemplatePost {
pub image: String,
pub title: String,
pub description: String,
pub url: String
}

View File

@ -23,10 +23,10 @@
<div class="box" id="content-pane"> <div class="box" id="content-pane">
{% for post in posts %} {% for post in posts %}
<div class="post"> <div class="post">
<a href="{{post.url}}"><img src="{{post.image}}" alt="post img"></a> <a href="{{post.origin_url}}"><img src="{{post.img_url}}" alt="post img"></a>
<span> <span>
<a href="{{post.url}}"><h3>{{post.title}}</h3></a> <a href="{{post.origin_url}}"><h3>{{post.title}}</h3></a>
<p>{{post.description}}</p> <p>{{post.descr}}</p>
</span> </span>
</div> </div>
{% endfor %} {% endfor %}