Skip to content

Commit

Permalink
ahhhhhhhh fur wisla
Browse files Browse the repository at this point in the history
  • Loading branch information
Dromader2137 committed May 28, 2024
1 parent 09713d1 commit ac4ee4b
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 81 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
#### /api/get/posts/by-user/{id}/{limit}/{offset}
- Get: 200 (PostList) / 404 ("User not found")
- Note: Only from user {id}
#### /api/get/posts/by-tag/{tag}/{limit}/{offset}
- Get: 200 (PostList) / 404 ("Tag not found")
- Note: Posts with tag {tag}
#### /api/get/posts/by-id/{id}/{limit}/{offset}
- Get: 200 (Post) / 404 ("Post not found")
- Note: Post with id {id}
Expand All @@ -38,6 +35,9 @@ Post {
date: i64
body: string (max 2048 chars)
likes: i64
user_name: string
display_name: string
pfp_image: string
}
```
```
Expand Down
151 changes: 80 additions & 71 deletions src/api_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::types::*;
use crate::auth::*;
use bytes::BufMut;
use futures::{StreamExt, TryStreamExt};
use tracing::Instrument;
use urlencoding::decode;

use tokio_rusqlite::params;
Expand All @@ -18,9 +19,10 @@ pub async fn get_posts_by_user(user_id: i64, limit: i64, offset: i64) -> Result<
.await
.unwrap();
let query = "
SELECT posts.*, users.user_name
SELECT posts.*, users.user_name, users.display_name, images.image_file
FROM posts
JOIN users ON users.user_id=posts.user_id
LEFT JOIN images ON users.pfp_id=images.image_id
WHERE users.user_id = ?
ORDER BY posts.date DESC
LIMIT ? OFFSET ?";
Expand Down Expand Up @@ -54,68 +56,13 @@ pub async fn get_posts_by_user(user_id: i64, limit: i64, offset: i64) -> Result<
body: row.get(3).unwrap(),
likes: row.get(4).unwrap(),
user_name: row.get(5).unwrap(),
});
}
Ok(post_vec)
})
.await
.unwrap();

let post = PostList { post_list };
Ok(warp::reply::with_status(
warp::reply::json(&post),
warp::http::StatusCode::OK,
))
}

pub async fn get_posts_by_tag(tag: String, limit: i64, offset: i64) -> Result<impl warp::Reply, warp::Rejection> {
let connection = tokio_rusqlite::Connection::open("projekt-db")
.await
.unwrap();
let tag_id = match get_tag_by_name(&connection, tag.clone()).await {
Ok(val) => val,
Err(_) => {
let r = "Tag not found";
return Ok(warp::reply::with_status(
warp::reply::json(&r),
warp::http::StatusCode::NOT_FOUND,
));
}
};

let timestamp = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_secs() as i64;
let query = format!(
"
SELECT posts.*, users.user_name
FROM posts
JOIN posts_tags
ON posts.post_id = posts_tags.post_id
JOIN users
ON posts.user_id = users.user_id
WHERE posts_tags.tag_id = ?
AND posts.user_id NOT IN
(SELECT user_id FROM bans WHERE is_active = 1 AND expires_on > {})
ORDER BY posts.date DESC
LIMIT ? OFFSET ?
",
timestamp
);
let post_list = connection
.call(move |conn| {
let mut statement = conn.prepare(&query).unwrap();
let mut rows = statement.query(params![tag_id, limit, offset]).unwrap();
let mut post_vec: Vec<Post> = Vec::new();
while let Ok(Some(row)) = rows.next() {
post_vec.push(Post {
post_id: row.get(0).unwrap(),
user_id: row.get(1).unwrap(),
date: row.get(2).unwrap(),
body: row.get(3).unwrap(),
likes: row.get(4).unwrap(),
user_name: row.get(5).unwrap(),
display_name: row.get(6).unwrap(),
pfp_image: {
match row.get(7) {
Ok(val) => val,
Err(_) => "".to_string()
}
}
});
}
Ok(post_vec)
Expand Down Expand Up @@ -145,10 +92,11 @@ pub async fn get_posts_from_search(phrase: String, limit: i64, offset: i64, date
let phrase_cpy = "%".to_string() + &decoded_phrase + "%";
let query = format!(
"
SELECT posts.*, users.user_name
SELECT posts.*, users.user_name, users.display_name, images.image_file
FROM posts
JOIN users
ON posts.user_id = users.user_id
LEFT JOIN images ON users.pfp_id=images.image_id
WHERE posts.body LIKE ?
AND posts.user_id NOT IN
(SELECT user_id FROM bans WHERE is_active = 1 AND expires_on > {})
Expand All @@ -171,6 +119,13 @@ pub async fn get_posts_from_search(phrase: String, limit: i64, offset: i64, date
body: row.get(3).unwrap(),
likes: row.get(4).unwrap(),
user_name: row.get(5).unwrap(),
display_name: row.get(6).unwrap(),
pfp_image: {
match row.get(7) {
Ok(val) => val,
Err(_) => "".to_string()
}
}
});
}
Ok(post_vec)
Expand Down Expand Up @@ -251,9 +206,10 @@ pub async fn get_posts(limit: i64, offset: i64) -> Result<impl warp::Reply, warp
.as_secs() as i64;
let query = format!(
"
SELECT posts.*, users.user_name
SELECT posts.*, users.user_name, users.display_name, images.image_file
FROM posts
JOIN users ON posts.user_id = users.user_id
LEFT JOIN images ON users.pfp_id=images.image_id
WHERE
posts.user_id NOT IN (SELECT user_id FROM bans WHERE is_active = 1 AND expires_on > {})
ORDER BY posts.date DESC
Expand All @@ -274,6 +230,13 @@ pub async fn get_posts(limit: i64, offset: i64) -> Result<impl warp::Reply, warp
body: row.get(3).unwrap(),
likes: row.get(4).unwrap(),
user_name: row.get(5).unwrap(),
display_name: row.get(6).unwrap(),
pfp_image: {
match row.get(7) {
Ok(val) => val,
Err(_) => "".to_string()
}
}
});
}
Ok(post_vec)
Expand All @@ -298,9 +261,10 @@ pub async fn get_posts_top(limit: i64, offset: i64, date_from: i64) -> Result<im
.as_secs() as i64;
let query = format!(
"
SELECT posts.*, users.user_name
SELECT posts.*, users.user_name, users.display_name, images.image_file
FROM posts
JOIN users ON posts.user_id = users.user_id
LEFT JOIN images ON users.pfp_id=images.image_id
WHERE
posts.user_id NOT IN (SELECT user_id FROM bans WHERE is_active = 1 AND expires_on > {})
AND posts.date > ?
Expand All @@ -322,6 +286,13 @@ pub async fn get_posts_top(limit: i64, offset: i64, date_from: i64) -> Result<im
body: row.get(3).unwrap(),
likes: row.get(4).unwrap(),
user_name: row.get(5).unwrap(),
display_name: row.get(6).unwrap(),
pfp_image: {
match row.get(7) {
Ok(val) => val,
Err(_) => "".to_string()
}
}
});
}
Ok(post_vec)
Expand All @@ -346,9 +317,10 @@ pub async fn get_posts_bottom(limit: i64, offset: i64, date_from: i64) -> Result
.as_secs() as i64;
let query = format!(
"
SELECT posts.*, users.user_name
SELECT posts.*, users.user_name, users.display_name, images.image_file
FROM posts
JOIN users ON posts.user_id = users.user_id
LEFT JOIN images ON users.pfp_id=images.image_id
WHERE
posts.user_id NOT IN (SELECT user_id FROM bans WHERE is_active = 1 AND expires_on > {})
AND posts.date > ?
Expand All @@ -370,6 +342,13 @@ pub async fn get_posts_bottom(limit: i64, offset: i64, date_from: i64) -> Result
body: row.get(3).unwrap(),
likes: row.get(4).unwrap(),
user_name: row.get(5).unwrap(),
display_name: row.get(6).unwrap(),
pfp_image: {
match row.get(7) {
Ok(val) => val,
Err(_) => "".to_string()
}
}
});
}
Ok(post_vec)
Expand All @@ -394,9 +373,10 @@ pub async fn get_posts_trending(limit: i64, offset: i64, date_from: i64) -> Resu
.as_secs() as i64;
let query = format!(
"
SELECT posts.*, users.user_name
SELECT posts.*, users.user_name, users.display_name, images.image_file
FROM posts
JOIN users ON posts.user_id = users.user_id
LEFT JOIN images ON users.pfp_id=images.image_id
WHERE
posts.user_id NOT IN (SELECT user_id FROM bans WHERE is_active = 1 AND expires_on > {})
AND posts.date > ?
Expand All @@ -418,6 +398,13 @@ pub async fn get_posts_trending(limit: i64, offset: i64, date_from: i64) -> Resu
body: row.get(3).unwrap(),
likes: row.get(4).unwrap(),
user_name: row.get(5).unwrap(),
display_name: row.get(6).unwrap(),
pfp_image: {
match row.get(7) {
Ok(val) => val,
Err(_) => "".to_string()
}
}
});
}
Ok(post_vec)
Expand All @@ -435,10 +422,11 @@ pub async fn get_posts_trending(limit: i64, offset: i64, date_from: i64) -> Resu
pub async fn get_comments_from_post(post_id: i64) -> Result<impl warp::Reply, warp::Rejection> {
let connection = tokio_rusqlite::Connection::open("projekt-db").await.unwrap();
let query = "
SELECT comments.*, users.user_name
SELECT comments.*, users.user_name, users.display_name, images.image_file
FROM comments
JOIN users
ON users.user_id = comments.user_id
LEFT JOIN images ON users.pfp_id=images.image_id
WHERE comments.post_id = ?
";

Expand All @@ -463,7 +451,14 @@ pub async fn get_comments_from_post(post_id: i64) -> Result<impl warp::Reply, wa
user_id: row.get(2).unwrap(),
body: row.get(3).unwrap(),
date: row.get(4).unwrap(),
user_name: row.get(5).unwrap()
user_name: row.get(5).unwrap(),
display_name: row.get(6).unwrap(),
pfp_image: {
match row.get(7) {
Ok(val) => val,
Err(_) => "".to_string()
}
}
}
);
}
Expand Down Expand Up @@ -570,9 +565,12 @@ pub async fn get_post_by_id(post_id: i64) -> Result<impl warp::Reply, warp::Reje
let connection = tokio_rusqlite::Connection::open("projekt-db")
.await
.unwrap();
let query = "SELECT posts.*, users.user_name FROM posts
let query = "
SELECT posts.*, users.user_name, users.display_name, images.image_file
FROM posts
JOIN users
ON posts.user_id = users.user_id
LEFT JOIN images ON users.pfp_id=images.image_id
WHERE posts.post_id = ?";

let post = connection
Expand All @@ -588,6 +586,13 @@ pub async fn get_post_by_id(post_id: i64) -> Result<impl warp::Reply, warp::Reje
body: row.get(3).unwrap(),
likes: row.get(4).unwrap(),
user_name: row.get(5).unwrap(),
display_name: row.get(6).unwrap(),
pfp_image: {
match row.get(7) {
Ok(val) => val,
Err(_) => "".to_string()
}
}
};
} else {
post = Post {
Expand All @@ -597,6 +602,8 @@ pub async fn get_post_by_id(post_id: i64) -> Result<impl warp::Reply, warp::Reje
body: "".to_string(),
likes: 0,
user_name: "".to_string(),
display_name: "".to_string(),
pfp_image: "".to_string(),
};
}
Ok(post)
Expand Down Expand Up @@ -890,6 +897,8 @@ pub async fn post(
body: request.body,
likes: 0,
user_name: "".to_string(),
display_name: "".to_string(),
pfp_image: "".to_string()
},
request.tags,
)
Expand Down
5 changes: 0 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ pub fn routes() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejecti
.and(warp::path!("api" / "get" / "posts" / "by-user" / i64 / i64 / i64))
.and_then(get_posts_by_user);

let get_posts_by_tag = warp::get()
.and(warp::path!("api" / "get" / "posts" / "by-tag" / String / i64 / i64))
.and_then(get_posts_by_tag);

let get_post_by_id = warp::get()
.and(warp::path!("api" / "get" / "posts" / "by-id" / i64))
.and_then(get_post_by_id);
Expand Down Expand Up @@ -202,7 +198,6 @@ pub fn routes() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejecti
.or(get_post_by_id)
.or(ban)
.or(unban)
.or(get_posts_by_tag)
.or(get_tags_from_post)
.or(react)
.or(get_profile_by_id)
Expand Down
8 changes: 6 additions & 2 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ pub struct Post {
pub date: i64,
pub body: String,
pub likes: i64,
pub user_name: String
pub user_name: String,
pub display_name: String,
pub pfp_image: String,
}

#[derive(Debug, Deserialize, Serialize, Clone)]
Expand All @@ -24,7 +26,9 @@ pub struct Comment {
pub user_id: i64,
pub body: String,
pub date: i64,
pub user_name: String
pub user_name: String,
pub display_name: String,
pub pfp_image: String,
}

#[derive(Debug, Deserialize, Serialize, Clone)]
Expand Down

0 comments on commit ac4ee4b

Please sign in to comment.