Compare commits

..

25 Commits

Author SHA1 Message Date
ef3820a2e1 User Flairs 2020-12-20 11:29:23 -08:00
1678245750 Add Sorting to Short Links 2020-12-20 09:10:37 -08:00
3594b6d41f Fix CSS and CSP 2020-12-19 22:25:00 -08:00
a754d42b9e Enforce Content Security Policy 2020-12-19 21:49:10 -08:00
c7e0234d33 Fix comment hover color 2020-12-19 21:44:30 -08:00
11a9ff53e4 Update README 2020-12-19 21:44:07 -08:00
7b8f694c8c Basic Nested & Collapsible Comments 2020-12-19 19:54:46 -08:00
19dc7de3c5 Update README.md 2020-12-18 16:24:09 -08:00
cd29cfbf29 Update Readme
Specify lack of Windows/MacOS binaries, update AUR installation method, correct Docker comparison
2020-12-16 13:01:11 -08:00
d0ec1fcc43 Update v0.1.11 2020-12-14 16:35:38 -08:00
75bc170eba Rewrite URL Dispatch 2020-12-14 16:35:04 -08:00
148d87fb45 Add Elsewhere Links 2020-12-12 08:57:23 -08:00
5219c919af Refactor Last Commit 2020-12-11 20:36:25 -08:00
5bda103356 Fix Post URL Colors 2020-12-11 20:36:06 -08:00
81274e35d7 Fix Post Body Links 2020-12-08 11:31:01 -08:00
e1962c7b66 Fix Header 2020-12-08 09:58:36 -08:00
528fe15819 Add million support 2020-12-07 11:36:05 -08:00
8509f6e22d Merge branch 'master' of https://github.com/spikecodes/libreddit 2020-12-07 11:20:33 -08:00
77886579f4 Link Post Titles 2020-12-07 11:20:24 -08:00
4f5ba35ddb Merge pull request #6 from Scoder12/feature/improve-actions
Use rust-cache action and prettify workflow yml
2020-12-07 11:07:22 -08:00
c738300bc4 Use rust-cache action and prettify
Run `prettier` on the workflow file
2020-12-07 11:05:00 -08:00
293a4d5c50 Merge pull request #5 from Scoder12/master
Add number format utility
2020-12-07 11:00:38 -08:00
312d162c09 Fix mistakes 2020-12-07 10:53:22 -08:00
9f19d729d1 Add number format utility 2020-12-07 10:32:46 -08:00
6794f7d6ba Show and Log Version 2020-12-05 21:29:25 -08:00
17 changed files with 326 additions and 249 deletions

View File

@ -2,25 +2,28 @@ name: Rust
on:
push:
branches: [ master ]
branches: [master]
pull_request:
branches: [ master ]
branches: [master]
env:
CARGO_TERM_COLOR: always
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Build
run: cargo build --release
- uses: actions/upload-artifact@v2.2.1
name: Upload a Build Artifact
with:
name: libreddit
path: target/release/libreddit
- uses: actions/checkout@v2
- name: Cache Packages
uses: Swatinem/rust-cache@v1.0.1
- name: Build
run: cargo build --release
- uses: actions/upload-artifact@v2.2.1
name: Upload a Build Artifact
with:
name: libreddit
path: target/release/libreddit

122
Cargo.lock generated
View File

@ -93,7 +93,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4ca8ce00b267af8ccebbd647de0d61e0674b6e61185cc7a592ff88772bed655"
dependencies = [
"quote 1.0.7",
"syn 1.0.53",
"syn 1.0.54",
]
[[package]]
@ -267,7 +267,7 @@ checksum = "ad26f77093333e0e7c6ffe54ebe3582d908a104e448723eec6d43d08b07143fb"
dependencies = [
"proc-macro2 1.0.24",
"quote 1.0.7",
"syn 1.0.53",
"syn 1.0.54",
]
[[package]]
@ -338,6 +338,17 @@ dependencies = [
"toml",
]
[[package]]
name = "async-recursion"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5444eec77a9ec2bfe4524139e09195862e981400c4358d3b760cae634e4c4ee"
dependencies = [
"proc-macro2 1.0.24",
"quote 1.0.7",
"syn 1.0.54",
]
[[package]]
name = "async-trait"
version = "0.1.42"
@ -346,7 +357,7 @@ checksum = "8d3a45e77e34375a7923b1e8febb049bb011f064714a8e17a1a616fef01da13d"
dependencies = [
"proc-macro2 1.0.24",
"quote 1.0.7",
"syn 1.0.53",
"syn 1.0.54",
]
[[package]]
@ -505,21 +516,11 @@ dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "console_error_panic_hook"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8d976903543e0c48546a91908f21588a680a8c8f984df9a5d69feccb2b2a211"
dependencies = [
"cfg-if 0.1.10",
"wasm-bindgen",
]
[[package]]
name = "const_fn"
version = "0.4.3"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c478836e029dcef17fb47c89023448c64f781a046e0300e257ad8225ae59afab"
checksum = "cd51eab21ab4fd6a3bf889e2d0958c0a6e3a61ad04260325e919e652a2a62826"
[[package]]
name = "cookie"
@ -561,7 +562,7 @@ checksum = "41cb0e6161ad61ed084a36ba71fbba9e3ac5aee3606fb607fe08da6acbcf3d8c"
dependencies = [
"proc-macro2 1.0.24",
"quote 1.0.7",
"syn 1.0.53",
"syn 1.0.54",
]
[[package]]
@ -603,7 +604,7 @@ dependencies = [
"heck",
"proc-macro2 1.0.24",
"quote 1.0.7",
"syn 1.0.53",
"syn 1.0.54",
]
[[package]]
@ -695,7 +696,7 @@ dependencies = [
"proc-macro-hack",
"proc-macro2 1.0.24",
"quote 1.0.7",
"syn 1.0.53",
"syn 1.0.54",
]
[[package]]
@ -835,9 +836,9 @@ dependencies = [
[[package]]
name = "http"
version = "0.2.1"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "28d569972648b2c512421b5f2a405ad6ac9666547189d0c5477a3f200f3e02f9"
checksum = "84129d298a6d57d246960ff8eb831ca4af3f96d29e2e28848dae275408658e26"
dependencies = [
"bytes",
"fnv",
@ -925,9 +926,9 @@ dependencies = [
[[package]]
name = "indexmap"
version = "1.6.0"
version = "1.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "55e2e4c765aa53a0424761bf9f41aa7a6ac1efa87238f59560640e27fca028f2"
checksum = "4fb1fa934250de4de8aef298d81c729a7d33d8c239daa3a7575e6b92bfc7313b"
dependencies = [
"autocfg",
"hashbrown",
@ -1008,16 +1009,17 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.80"
version = "0.2.81"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4d58d1b70b004888f764dfbf6a26a3b0342a1632d33968e4a179d8011c760614"
checksum = "1482821306169ec4d07f6aca392a4681f66c75c9918aa49641a2595db64053cb"
[[package]]
name = "libreddit"
version = "0.1.10"
version = "0.2.1"
dependencies = [
"actix-web",
"askama",
"async-recursion",
"base64 0.13.0",
"chrono",
"pulldown-cmark",
@ -1147,9 +1149,9 @@ dependencies = [
[[package]]
name = "net2"
version = "0.2.36"
version = "0.2.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d7cf75f38f16cb05ea017784dc6dbfd354f76c223dba37701734c4f5a9337d02"
checksum = "391630d12b68002ae1e25e8f974306474966550ad82dac6886fb8910c19568ae"
dependencies = [
"cfg-if 0.1.10",
"libc",
@ -1270,7 +1272,7 @@ checksum = "65ad2ae56b6abe3a1ee25f15ee605bacadb9a764edaba9c2bf4103800d4a1895"
dependencies = [
"proc-macro2 1.0.24",
"quote 1.0.7",
"syn 1.0.53",
"syn 1.0.54",
]
[[package]]
@ -1281,7 +1283,7 @@ checksum = "f8e8d2bf0b23038a4424865103a4df472855692821aab4e4f5c3312d461d9e5f"
dependencies = [
"proc-macro2 1.0.24",
"quote 1.0.7",
"syn 1.0.53",
"syn 1.0.54",
]
[[package]]
@ -1441,9 +1443,9 @@ checksum = "3b181ba2dcf07aaccad5448e8ead58db5b742cf85dfe035e2227f137a539a189"
[[package]]
name = "reqwest"
version = "0.10.9"
version = "0.10.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb15d6255c792356a0f578d8a645c677904dc02e862bebe2ecc18e0c01b9a0ce"
checksum = "0718f81a8e14c4dbb3b34cf23dc6aaf9ab8a0dfec160c534b3dbca1aaa21f47c"
dependencies = [
"base64 0.13.0",
"bytes",
@ -1470,7 +1472,6 @@ dependencies = [
"url",
"wasm-bindgen",
"wasm-bindgen-futures",
"wasm-bindgen-test",
"web-sys",
"webpki-roots",
"winreg 0.7.0",
@ -1535,12 +1536,6 @@ version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e"
[[package]]
name = "scoped-tls"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea6a9290e3c9cf0f18145ef7ffa62d68ee0bf5fcd651017e586dc7fd5da448c2"
[[package]]
name = "scopeguard"
version = "1.1.0"
@ -1589,7 +1584,7 @@ checksum = "c84d3526699cd55261af4b941e4e725444df67aa4f9e6a3564f18030d12672df"
dependencies = [
"proc-macro2 1.0.24",
"quote 1.0.7",
"syn 1.0.53",
"syn 1.0.54",
]
[[package]]
@ -1657,13 +1652,12 @@ checksum = "ae524f056d7d770e174287294f562e95044c68e88dec909a00d2094805db9d75"
[[package]]
name = "socket2"
version = "0.3.17"
version = "0.3.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2c29947abdee2a218277abeca306f25789c938e500ea5a9d4b12a5a504466902"
checksum = "97e0e9fd577458a4f61fb91fcb559ea2afecc54c934119421f9f5d3d5b1a1057"
dependencies = [
"cfg-if 1.0.0",
"libc",
"redox_syscall",
"winapi 0.3.9",
]
@ -1706,7 +1700,7 @@ dependencies = [
"quote 1.0.7",
"serde",
"serde_derive",
"syn 1.0.53",
"syn 1.0.54",
]
[[package]]
@ -1722,7 +1716,7 @@ dependencies = [
"serde_derive",
"serde_json",
"sha1",
"syn 1.0.53",
"syn 1.0.54",
]
[[package]]
@ -1744,9 +1738,9 @@ dependencies = [
[[package]]
name = "syn"
version = "1.0.53"
version = "1.0.54"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8833e20724c24de12bbaba5ad230ea61c3eafb05b881c7c9d3cfe8638b187e68"
checksum = "9a2af957a63d6bd42255c359c93d9bfdb97076bd3b820897ce55ffbfbf107f44"
dependencies = [
"proc-macro2 1.0.24",
"quote 1.0.7",
@ -1770,7 +1764,7 @@ checksum = "9ba20f23e85b10754cd195504aebf6a27e2e6cbe28c17778a0c930724628dd56"
dependencies = [
"proc-macro2 1.0.24",
"quote 1.0.7",
"syn 1.0.53",
"syn 1.0.54",
]
[[package]]
@ -1837,7 +1831,7 @@ dependencies = [
"proc-macro2 1.0.24",
"quote 1.0.7",
"standback",
"syn 1.0.53",
"syn 1.0.54",
]
[[package]]
@ -1857,9 +1851,9 @@ checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c"
[[package]]
name = "tokio"
version = "0.2.23"
version = "0.2.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a6d7ad61edd59bfcc7e80dababf0f4aed2e6d5e0ba1659356ae889752dfc12ff"
checksum = "099837d3464c16a808060bb3f02263b412f6fafcb5d01c533d309985fbeebe48"
dependencies = [
"bytes",
"fnv",
@ -2126,7 +2120,7 @@ dependencies = [
"log",
"proc-macro2 1.0.24",
"quote 1.0.7",
"syn 1.0.53",
"syn 1.0.54",
"wasm-bindgen-shared",
]
@ -2160,7 +2154,7 @@ checksum = "b5a48c72f299d80557c7c62e37e7225369ecc0c963964059509fbafe917c7549"
dependencies = [
"proc-macro2 1.0.24",
"quote 1.0.7",
"syn 1.0.53",
"syn 1.0.54",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
@ -2171,30 +2165,6 @@ version = "0.2.69"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e7811dd7f9398f14cc76efd356f98f03aa30419dea46aa810d71e819fc97158"
[[package]]
name = "wasm-bindgen-test"
version = "0.3.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0355fa0c1f9b792a09b6dcb6a8be24d51e71e6d74972f9eb4a44c4c004d24a25"
dependencies = [
"console_error_panic_hook",
"js-sys",
"scoped-tls",
"wasm-bindgen",
"wasm-bindgen-futures",
"wasm-bindgen-test-macro",
]
[[package]]
name = "wasm-bindgen-test-macro"
version = "0.3.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "27e07b46b98024c2ba2f9e83a10c2ef0515f057f2da299c1762a2017de80438b"
dependencies = [
"proc-macro2 1.0.24",
"quote 1.0.7",
]
[[package]]
name = "web-sys"
version = "0.3.46"

View File

@ -3,7 +3,7 @@ name = "libreddit"
description = " Alternative private front-end to Reddit"
license = "AGPL-3.0"
repository = "https://github.com/spikecodes/libreddit"
version = "0.1.10"
version = "0.2.1"
authors = ["spikecodes <19519553+spikecodes@users.noreply.github.com>"]
edition = "2018"
@ -20,3 +20,4 @@ serde = "1.0.117"
serde_json = "1.0"
pulldown-cmark = "0.8.0"
chrono = "0.4.19"
async-recursion = "0.3.1"

View File

@ -7,7 +7,7 @@ Libre + Reddit = Libreddit
- 🚀 Fast: written in Rust for blazing fast speeds and safety
- ☁️ Light: no javascript, no ads, no tracking
- 🕵 Private: all requests are proxied through the server, including media
- 🔒 Safe: does not rely on Reddit's OAuth-requiring APIs
- 🔒 Safe: does not rely on Reddit OAuth or require a Reddit API Key
- 📱 Responsive: works great on mobile!
Think Invidious but for Reddit. Watch your cat videos without being watched.
@ -18,25 +18,30 @@ Think Invidious but for Reddit. Watch your cat videos without being watched.
## About
### Elsewhere
Find Libreddit on...
- 💬 Matrix: [#libreddit:matrix.org](https://matrix.to/#/#libreddit:matrix.org)
- 🐋 Docker: [spikecodes/libreddit](https://hub.docker.com/r/spikecodes/libreddit)
- :octocat: GitHub: [spikecodes/libreddit](https://github.com/spikecodes/libreddit)
- 🦊 GitLab: [spikecodes/libreddit](https://gitlab.com/spikecodes/libreddit)
### Info
Libreddit hopes to provide an easier way to browse Reddit, without the ads, trackers and bloat. Libreddit was inspired by other alternative front-ends to popular services such as [Invidious](https://github.com/iv-org/invidious) for YouTube, [Nitter](https://github.com/zedeus/nitter) for Twitter, and [Bibliogram](https://sr.ht/~cadence/bibliogram/) for Instagram.
Libreddit currently implements most of Reddit's functionalities but still lacks a few features that are being worked on below.
### In Progress
- Nested comments
- User flairs
- Searching
- Multireddits
### How does it compare to Teddit?
Teddit is another awesome open source project designed to provide an alternative frontend to Reddit. There is no connection (AFAIK) between the two and you're welcome to use whichever one you favor. Competition fosters innovation and Teddit's release has motivated me to build Libreddit into an even more polished product.
Teddit is another awesome open source project designed to provide an alternative frontend to Reddit. There is no connection between the two and you're welcome to use whichever one you favor. Competition fosters innovation and Teddit's release has motivated me to build Libreddit into an even more polished product.
If you are looking to compare, the biggest differences I have noticed are:
- Libreddit is themed around Reddit's redesign whereas Teddit appears to stick much closer to Reddit's old design. This may suit some users better as design is always subjective.
- Libreddit is written in Rust for speed and memory safety. It uses Actix Web, which was [benchmarked as the fastest web server for single queries](https://www.techempower.com/benchmarks/#hw=ph&test=db).
- Unlike Teddit (at the time of writing this), Libreddit does not require a Reddit API key to host.
- Currently, Teddit does not support hosting on Docker whereas Libreddit does.
- Libreddit lacks certain functionality that Teddit offers, primarily: nested comments, configuration & multireddits. If these are features that you need, Teddit may suit your needs better.
## Instances
@ -69,7 +74,7 @@ docker run -d --name libreddit -p 80:8080 spikecodes/libreddit
### C) AUR
Libreddit is available from the Arch User Repository as [`libreddit-git`](https://aur.archlinux.org/packages/libreddit-git).
For ArchLinux users, Libreddit is available from the AUR as [`libreddit-git`](https://aur.archlinux.org/packages/libreddit-git).
Install:
```
@ -79,6 +84,7 @@ yay -S libreddit-git
### D) GitHub Releases
If you're on Linux and none of these methods work for you, you can grab a Linux binary from [the newest release](https://github.com/spikecodes/libreddit/releases/latest).
Currently Libreddit does not have Windows or MacOS binaries but those will be available soon.
## Deploy an Instance

View File

@ -1,5 +1,5 @@
// Import Crates
use actix_web::{get, App, HttpResponse, HttpServer};
use actix_web::{get, middleware::NormalizePath, web, App, HttpResponse, HttpServer};
// Reference local files
mod popular;
@ -10,12 +10,10 @@ mod user;
mod utils;
// Create Services
#[get("/style.css")]
async fn style() -> HttpResponse {
HttpResponse::Ok().content_type("text/css").body(include_str!("../static/style.css"))
}
#[get("/robots.txt")]
async fn robots() -> HttpResponse {
HttpResponse::Ok().body(include_str!("../static/robots.txt"))
}
@ -40,25 +38,28 @@ async fn main() -> std::io::Result<()> {
}
// start http server
println!("Running Libreddit on {}!", address.clone());
println!("Running Libreddit v{} on {}!", env!("CARGO_PKG_VERSION"), address.clone());
HttpServer::new(|| {
App::new()
// TRAILING SLASH MIDDLEWARE
.wrap(NormalizePath::default())
// GENERAL SERVICES
.service(style)
.service(favicon)
.service(robots)
.route("/style.css/", web::get().to(style))
.route("/favicon.ico/", web::get().to(|| HttpResponse::Ok()))
.route("/robots.txt/", web::get().to(robots))
// PROXY SERVICE
.service(proxy::handler)
// POST SERVICES
.service(post::short)
.service(post::page)
// SUBREDDIT SERVICES
.service(subreddit::page)
// POPULAR SERVICES
.service(popular::page)
.route("/proxy/{url:.*}/", web::get().to(proxy::handler))
// USER SERVICES
.service(user::page)
.route("/u/{username}/", web::get().to(user::page))
.route("/user/{username}/", web::get().to(user::page))
// SUBREDDIT SERVICES
.route("/r/{sub}/", web::get().to(subreddit::page))
// POPULAR SERVICES
.route("/", web::get().to(popular::page))
// POST SERVICES
.route("/{id:.{5,6}}/", web::get().to(post::short))
.route("/r/{sub}/comments/{id}/{title}/", web::get().to(post::page))
})
.bind(address.clone())
.expect(format!("Cannot bind to the address: {}", address).as_str())

View File

@ -1,6 +1,6 @@
// CRATES
use crate::utils::{fetch_posts, ErrorTemplate, Params, Post};
use actix_web::{get, http::StatusCode, web, HttpResponse, Result};
use actix_web::{http::StatusCode, web, HttpResponse, Result};
use askama::Template;
// STRUCTS
@ -50,7 +50,6 @@ async fn render(sub_name: String, sort: Option<String>, ends: (Option<String>, O
}
// SERVICES
#[get("/")]
pub async fn page(params: web::Query<Params>) -> Result<HttpResponse> {
render("popular".to_string(), params.sort.clone(), (params.before.clone(), params.after.clone())).await
}

View File

@ -1,6 +1,9 @@
// CRATES
use crate::utils::{format_url, request, val, Comment, ErrorTemplate, Flair, Params, Post};
use actix_web::{get, http::StatusCode, web, HttpResponse, Result};
use crate::utils::{format_num, format_url, request, val, Comment, ErrorTemplate, Flair, Params, Post};
use actix_web::{http::StatusCode, web, HttpResponse, Result};
use async_recursion::async_recursion;
use askama::Template;
use chrono::{TimeZone, Utc};
use pulldown_cmark::{html, Options, Parser};
@ -14,12 +17,15 @@ struct PostTemplate {
sort: String,
}
async fn render(id: String, sort: String) -> Result<HttpResponse> {
async fn render(id: String, sort: Option<String>) -> Result<HttpResponse> {
// Log the post ID being fetched
dbg!(&id);
// Handling sort paramater
let sorting: String = sort.unwrap_or("confidence".to_string());
// Build the Reddit JSON API url
let url: String = format!("https://reddit.com/{}.json?sort={}", id, sort);
let url: String = format!("https://reddit.com/{}.json?sort={}", id, sorting);
// Send a request to the url, receive JSON in response
let req = request(url).await;
@ -45,7 +51,7 @@ async fn render(id: String, sort: String) -> Result<HttpResponse> {
let s = PostTemplate {
comments: comments.unwrap(),
post: post.unwrap(),
sort: sort,
sort: sorting,
}
.render()
.unwrap();
@ -53,17 +59,12 @@ async fn render(id: String, sort: String) -> Result<HttpResponse> {
}
// SERVICES
#[get("/{id}")]
async fn short(web::Path(id): web::Path<String>) -> Result<HttpResponse> {
render(id.to_string(), "confidence".to_string()).await
pub async fn short(web::Path(id): web::Path<String>, params: web::Query<Params>) -> Result<HttpResponse> {
render(id, params.sort.clone()).await
}
#[get("/r/{sub}/comments/{id}/{title}/")]
async fn page(web::Path((_sub, id)): web::Path<(String, String)>, params: web::Query<Params>) -> Result<HttpResponse> {
match &params.sort {
Some(sort) => render(id, sort.to_string()).await,
None => render(id, "confidence".to_string()).await,
}
pub async fn page(web::Path((_sub, id)): web::Path<(String, String)>, params: web::Query<Params>) -> Result<HttpResponse> {
render(id, params.sort.clone()).await
}
// UTILITIES
@ -114,8 +115,13 @@ async fn parse_post(json: serde_json::Value) -> Result<Post, &'static str> {
community: val(post_data, "subreddit").await,
body: markdown_to_html(post_data["data"]["selftext"].as_str().unwrap()).await,
author: val(post_data, "author").await,
author_flair: Flair(
val(post_data, "author_flair_text").await,
val(post_data, "author_flair_background_color").await,
val(post_data, "author_flair_text_color").await,
),
url: val(post_data, "permalink").await,
score: if score > 1000 { format!("{}k", score / 1000) } else { score.to_string() },
score: format_num(score),
post_type: media.0,
media: media.1,
time: Utc.timestamp(unix_time, 0).format("%b %e %Y %H:%M UTC").to_string(),
@ -127,32 +133,47 @@ async fn parse_post(json: serde_json::Value) -> Result<Post, &'static str> {
} else {
"white".to_string()
},
),
)
};
Ok(post)
}
// COMMENTS
#[async_recursion]
async fn parse_comments(json: serde_json::Value) -> Result<Vec<Comment>, &'static str> {
// Separate the comment JSON into a Vector of comments
let comment_data = json["data"]["children"].as_array().unwrap();
let mut comments: Vec<Comment> = Vec::new();
// For each comment, retrieve the values to build a Comment object
for comment in comment_data.iter() {
let unix_time: i64 = comment["data"]["created_utc"].as_f64().unwrap_or(0.0).round() as i64;
if unix_time == 0 {
continue;
}
let score = comment["data"]["score"].as_i64().unwrap_or(0);
let body = markdown_to_html(comment["data"]["body"].as_str().unwrap_or("")).await;
// if comment["data"]["replies"].is_object() {
// let replies = parse_comments(comment["data"]["replies"].clone()).await.unwrap();
// }
let replies: Vec<Comment> = if comment["data"]["replies"].is_object() {
parse_comments(comment["data"]["replies"].clone()).await.unwrap_or(Vec::new())
} else {
Vec::new()
};
comments.push(Comment {
body: body,
author: val(comment, "author").await,
score: if score > 1000 { format!("{}k", score / 1000) } else { score.to_string() },
score: format_num(score),
time: Utc.timestamp(unix_time, 0).format("%b %e %Y %H:%M UTC").to_string(),
replies: replies,
flair: Flair(
val(comment, "author_flair_text").await,
val(comment, "author_flair_background_color").await,
val(comment, "author_flair_text_color").await,
),
});
}

View File

@ -1,10 +1,9 @@
use actix_web::{client::Client, get, web, Error, HttpResponse, Result};
use actix_web::{client::Client, web, Error, HttpResponse, Result};
#[cfg(feature = "proxy")]
use base64::decode;
#[get("/imageproxy/{url:.*}")]
async fn handler(web::Path(url): web::Path<String>) -> Result<HttpResponse> {
pub async fn handler(web::Path(url): web::Path<String>) -> Result<HttpResponse> {
if cfg!(feature = "proxy") {
let media: String;

View File

@ -1,7 +1,8 @@
// CRATES
use crate::utils::{fetch_posts, format_url, request, val, ErrorTemplate, Params, Post, Subreddit};
use actix_web::{get, http::StatusCode, web, HttpResponse, Result};
use crate::utils::{fetch_posts, format_num, format_url, request, val, ErrorTemplate, Params, Post, Subreddit};
use actix_web::{http::StatusCode, web, HttpResponse, Result};
use askama::Template;
use std::convert::TryInto;
// STRUCTS
#[derive(Template)]
@ -15,8 +16,7 @@ struct SubredditTemplate {
// SERVICES
#[allow(dead_code)]
#[get("/r/{sub}")]
async fn page(web::Path(sub): web::Path<String>, params: web::Query<Params>) -> Result<HttpResponse> {
pub async fn page(web::Path(sub): web::Path<String>, params: web::Query<Params>) -> Result<HttpResponse> {
render(sub, params.sort.clone(), (params.before.clone(), params.after.clone())).await
}
@ -89,8 +89,8 @@ async fn subreddit(sub: &String) -> Result<Subreddit, &'static str> {
title: val(&res, "title").await,
description: val(&res, "public_description").await,
icon: format_url(val(&res, "icon_img").await.as_str()).await,
members: if members > 1000 { format!("{}k", members / 1000) } else { members.to_string() },
active: if active > 1000 { format!("{}k", active / 1000) } else { active.to_string() },
members: format_num(members.try_into().unwrap()),
active: format_num(active.try_into().unwrap()),
};
Ok(sub)

View File

@ -1,6 +1,6 @@
// CRATES
use crate::utils::{fetch_posts, nested_val, request, ErrorTemplate, Params, Post, User};
use actix_web::{get, http::StatusCode, web, HttpResponse, Result};
use actix_web::{http::StatusCode, web, HttpResponse, Result};
use askama::Template;
// STRUCTS
@ -39,8 +39,7 @@ async fn render(username: String, sort: String) -> Result<HttpResponse> {
}
// SERVICES
#[get("/u/{username}")]
async fn page(web::Path(username): web::Path<String>, params: web::Query<Params>) -> Result<HttpResponse> {
pub async fn page(web::Path(username): web::Path<String>, params: web::Query<Params>) -> Result<HttpResponse> {
match &params.sort {
Some(sort) => render(username, sort.to_string()).await,
None => render(username, "hot".to_string()).await,

View File

@ -22,6 +22,7 @@ pub struct Post {
pub community: String,
pub body: String,
pub author: String,
pub author_flair: Flair,
pub url: String,
pub score: String,
pub post_type: String,
@ -35,8 +36,10 @@ pub struct Post {
pub struct Comment {
pub body: String,
pub author: String,
pub flair: Flair,
pub score: String,
pub time: String,
pub replies: Vec<Comment>,
}
#[allow(dead_code)]
@ -76,17 +79,27 @@ pub struct ErrorTemplate {
}
//
// URL HANDLING
// FORMATTING
//
pub async fn format_url(url: &str) -> String {
#[cfg(feature = "proxy")]
return "/imageproxy/".to_string() + encode(url).as_str();
return "/proxy/".to_string() + encode(url).as_str();
#[cfg(not(feature = "proxy"))]
return url.to_string();
}
pub fn format_num(num: i64) -> String {
if num > 1000000 {
format!("{}m", num / 1000000)
} else if num > 1000 {
format!("{}k", num / 1000)
} else {
num.to_string()
}
}
//
// JSON PARSING
//
@ -136,7 +149,12 @@ pub async fn fetch_posts(url: String, fallback_title: String) -> Result<(Vec<Pos
community: val(post, "subreddit").await,
body: val(post, "body").await,
author: val(post, "author").await,
score: if score > 1000 { format!("{}k", score / 1000) } else { score.to_string() },
author_flair: Flair(
val(post, "author_flair_text").await,
val(post, "author_flair_background_color").await,
val(post, "author_flair_text_color").await,
),
score: format_num(score),
post_type: "link".to_string(),
media: img,
url: val(post, "permalink").await,

View File

@ -1,5 +1,14 @@
/* General */
:root {
--background: #0F0F0F;
--foreground: #222;
--outside: #1F1F1F;
--post: #161616;
--highlighted: #333;
--black-contrast: 0 1px 3px rgba(0,0,0,0.5);
}
* {
transition: 0.2s all;
margin: 0px;
@ -8,15 +17,15 @@
font-weight: normal;
}
html {
background: black;
body {
background: var(--background);
}
header {
display: flex;
justify-content: space-between;
color: aqua;
background: #151515;
background: var(--outside);
padding: 15px;
font-weight: bold;
font-size: 20px;
@ -53,12 +62,14 @@ a:not(.post_right):hover {
text-decoration: underline;
}
span {
color: aqua;
#about {
padding-top: 20px;
background: #151515;
}
#about {
background: #151515;
#version {
color: white;
opacity: 25%;
}
/* Subreddit */
@ -123,7 +134,8 @@ span {
}
#sort > div, footer > a {
background: #151515;
box-shadow: var(--black-contrast);
background: var(--outside);
color: lightgrey;
border-radius: 5px;
margin-right: 5px;
@ -138,32 +150,24 @@ span {
}
#sort > div:hover {
background: #222;
background: var(--foreground);
}
/* Post */
.post {
border-radius: 5px;
background: #151515;
background: var(--post);
box-shadow: var(--black-contrast);
display: flex;
}
.post.highlighted {
border: 2px solid #555;
background: #222;
}
.post.highlighted > .post_left {
background: #333;
}
.post:hover {
background: #222;
background: var(--foreground);
}
.post:hover > .post_left {
background: #333;
background: var(--highlighted);
}
.post_left, .post_right {
@ -174,20 +178,20 @@ span {
.post_left {
text-align: center;
background: #222;
background: var(--foreground);
border-radius: 5px 0px 0px 5px;
min-width: 50px;
padding: 5px;
}
.datetime {
float: right;
}
.post_subreddit {
font-weight: bold;
}
.post_title {
font-size: 18px;
}
.post_score {
margin-top: 1em;
color: aqua;
@ -203,10 +207,6 @@ span {
margin: 5px;
}
.post_right > a {
color: aqua;
}
.post_right > p {
opacity: 0.75;
font-size: 16px;
@ -231,11 +231,15 @@ span {
margin-top: 1.5em;
}
.post_body > a {
.post_body a {
text-decoration: underline;
color: aqua;
}
#post_url {
color: aqua;
}
.post_thumbnail {
object-fit: cover;
width: auto;
@ -249,7 +253,7 @@ span {
display: none;
}
small {
.post_flair {
background: aqua;
color: black;
padding: 5px;
@ -262,13 +266,10 @@ small {
/* Comment */
.comment {
margin-top: 1em;
border-radius: 5px;
display: flex;
border: 2px solid #222;
}
.comment:hover {
background: #111;
font-size: 15px;
}
.comment_left, .comment_right {
@ -279,18 +280,32 @@ small {
.comment_left {
text-align: center;
min-width: 50px;
padding: 5px;
align-items: flex-end;
padding: 5px 0px;
align-items: center;
}
.comment_title {
font-size: 20px;
}
.comment_author {
opacity: 0.9;
}
.author_flair {
background: var(--highlighted);
color: white;
padding: 5px;
margin-right: 5px;
border-radius: 5px;
font-size: 12px;
font-weight: bold;
}
.comment_upvote {
margin-top: 0.5em;
border-radius: 5px 5px 0px 0px;
background: #222;
background: var(--foreground);
width: 40px;
padding: 10px 0px 0px 0px;
}
@ -301,27 +316,23 @@ small {
.comment_score {
color: aqua;
background: #222;
width: 40px;
padding: 5px 0px 10px 0px;
border-radius: 0px 0px 5px 5px;
background: var(--foreground);
min-width: 40px;
border-radius: 5px;
padding: 10px 0px;
}
.comment_right {
word-wrap: anywhere;
padding: 10px 25px 10px 10px;
padding: 10px 25px 10px 5px;
flex-grow: 1;
flex-shrink: 1;
}
.comment_right > * {
.comment_data > * {
margin: 5px;
}
.comment_right > p {
opacity: 0.75;
}
.comment_image {
max-width: 500px;
align-self: center;
@ -346,24 +357,58 @@ small {
color: aqua;
}
::marker {
color: aqua;
}
.reply {
margin-top: 0;
margin-left: 2em;
}
.datetime {
opacity: 0.75;
}
.line {
width: 2px;
height: 100%;
background: var(--foreground);
}
.post.comment {
background: #000;
border: 2px solid #222;
border: 2px solid var(--foreground);
}
.post.comment > .post_left {
background: black;
}
/* Code */
pre {
background: var(--outside);
padding: 20px;
margin-top: 10px;
border-radius: 5px;
box-shadow: var(--black-contrast);
}
code {
font-family: monospace;
font-size: 14px;
}
/* Tables */
table {
border: 3px #333 solid;
border: 3px var(--highlighted) solid;
border-spacing: 0rem;
}
td, th {
border: 1px #333 solid;
border: 1px var(--highlighted) solid;
padding: 0.5em;
}

View File

@ -3,6 +3,7 @@
<head>
{% block head %}
<title>{% block title %}{% endblock %}</title>
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; style-src 'self' 'unsafe-inline';">
<meta name="description" content="View on Libreddit, an alternative private front-end to Reddit.">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
@ -18,14 +19,14 @@
{% endblock %}
</head>
<body>
{% block body %}
{% block header %}
<header>
<a href="/"><span id="lib">lib</span>reddit.</a>
<a href="/"><span id="lib">lib</span>reddit. <span id="version">v{{ env!("CARGO_PKG_VERSION") }}</span></a>
<a id="github" href="https://github.com/spikecodes/libreddit">GITHUB</a>
</header>
{% endblock %}
{% block body %}
<main>
{% block content %}
{% endblock %}

View File

@ -15,14 +15,15 @@
<div class="post_right">
<h4>
<b><a class="post_subreddit" href="/r/{{ post.community }}">r/{{ post.community }}</a></b>
&bull;
Posted by
<a class="post_author" href="/u/{{ post.author }}">u/{{ post.author }}</a>
&bull; <a class="post_author" href="/u/{{ post.author }}">u/{{ post.author }}</a>
{% if post.author_flair.0 != "" %}
<small class="author_flair">{{ post.author_flair.0 }}</small>
{% endif %}
<span class="datetime" style="float: right;">{{ post.time }}</span>
</h4>
<h3 class="post_title">
{% if post.flair.0 != "" %}
<small style="color:{{ post.flair.2 }}; background:{{ post.flair.1 }}">{{ post.flair.0 }}</small>
<small class="post_flair" style="color:{{ post.flair.2 }}; background:{{ post.flair.1 }}">{{ post.flair.0 }}</small>
{% endif %}
<a href="{{ post.url }}">{{ post.title }}</a>
</h3>

View File

@ -4,6 +4,26 @@
{% call super() %}
<meta name="author" content="u/{{ post.author }}">
{% endblock %}
{% macro comment(item) -%}
<div class="comment">
<div class="comment_left">
<h3 class="comment_score">{{ item.score }}</h3>
<div class="line"></div>
</div>
<details class="comment_right" open>
<summary class="comment_data">
<a class="comment_author" href="/u/{{ item.author }}">u/{{ item.author }}</a>
{% if item.flair.0 != "" %}
<small class="author_flair">{{ item.flair.0 }}</small>
{% endif %}
&bull; <span class="datetime">{{ item.time }}</span>
</summary>
<h4 class="comment_body">{{ item.body }}</h4>
{%- endmacro %}
{% block content %}
<div class="post highlighted">
<div class="post_left">
@ -13,22 +33,24 @@
<h4>
<b><a class="post_subreddit" href="/r/{{ post.community }}">r/{{ post.community }}</a></b>
&bull;
Posted by
<a class="post_author" href="/u/{{ post.author }}">u/{{ post.author }}</a>
{% if post.author_flair.0 != "" %}
<small class="author_flair">{{ post.author_flair.0 }}</small>
{% endif %}
<span class="datetime">{{ post.time }}</span>
</h4>
<h3 class="post_title">
<a href="{{ post.url }}" class="post_title">
{{ post.title }}
{% if post.flair.0 != "" %}
<small style="color:{{ post.flair.2 }}; background:{{ post.flair.1 }}">{{ post.flair.0 }}</small>
<small class="post_flair" style="color:{{ post.flair.2 }}; background:{{ post.flair.1 }}">{{ post.flair.0 }}</small>
{% endif %}
</h3>
</a>
{% if post.post_type == "image" %}
<img class="post_media" src="{{ post.media }}"/>
{% else if post.post_type == "video" %}
<video class="post_media" src="{{ post.media }}" controls autoplay loop>
{% else if post.post_type == "link" %}
<a href="{{ post.media }}">{{ post.media }}</a>
<a id="post_url" href="{{ post.media }}">{{ post.media }}</a>
{% endif %}
<h4 class="post_body">{{ post.body }}</h4>
</div>
@ -40,19 +62,21 @@
<div id="sort_controversial"><a href="?sort=controversial">Controversial</a></div>
<div id="sort_old"><a href="?sort=old">Old</a></div>
</div>
{% for comment in comments %}
<div class="comment">
<div class="comment_left">
<div class="comment_upvote"></div>
<h3 class="comment_score">{{ comment.score }}</h3>
{% for c in comments -%}
<div class="thread">
{% call comment(c) %}
<div class="replies">
{% for reply in c.replies %}
{% call comment(reply) %}
<div class="replies">
{% for response in reply.replies %}
{% call comment(response) %}</details></div>
{% endfor %}
</div></details></div>
{% endfor %}
</div></details></div>
</div>
<div class="comment_right">
<h4>
Posted by <a class="comment_author" href="/u/{{ comment.author }}">u/{{ comment.author }}</a>
<span class="datetime">{{ comment.time }}</span>
</h4>
<h4 class="comment_body">{{ comment.body }}</h4>
</div>
</div><br>
{% endfor %}
{%- endfor %}
{% endblock %}

View File

@ -1,12 +1,6 @@
{% extends "base.html" %}
{% block title %}r/{{ sub.name }}: {{ sub.description }}{% endblock %}
{% block body %}
{% block header %}
<header>
<a href="/"><span id="lib">lib</span>reddit.</a>
<a id="github" href="https://github.com/spikecodes/libreddit">GITHUB</a>
</header>
{% endblock %}
<div id="about">
<div class="subreddit">
<div class="subreddit_left">
@ -33,14 +27,15 @@
<div class="post_right">
<h4>
<b><a class="post_subreddit" href="/r/{{ post.community }}">r/{{ sub.name }}</a></b>
&bull;
Posted by
<a class="post_author" href="/u/{{ post.author }}">u/{{ post.author }}</a>
&bull; <a class="post_author" href="/u/{{ post.author }}">u/{{ post.author }}</a>
{% if post.author_flair.0 != "" %}
<small class="author_flair">{{ post.author_flair.0 }}</small>
{% endif %}
<span class="datetime">{{ post.time }}</span>
</h4>
<h3 class="post_title">
{% if post.flair.0 != "" %}
<small style="color:{{ post.flair.2 }}; background:{{ post.flair.1 }}">{{ post.flair.0 }}</small>
<small class="post_flair" style="color:{{ post.flair.2 }}; background:{{ post.flair.1 }}">{{ post.flair.0 }}</small>
{% endif %}
<a href="{{ post.url }}">{{ post.title }}</a>
</h3>

View File

@ -1,12 +1,6 @@
{% extends "base.html" %}
{% block title %}Libreddit: u/{{ user.name }}{% endblock %}
{% block body %}
{% block header %}
<header>
<a href="/"><span id="lib">lib</span>reddit.</a>
<a id="github" href="https://github.com/spikecodes/libreddit">GITHUB</a>
</header>
{% endblock %}
<div id="about">
<div class="user">
<div class="user_left">
@ -33,16 +27,17 @@
<div class="post_right">
<h4>
<b><a class="post_subreddit" href="/r/{{ post.community }}">r/{{ post.community }}</a></b>
&bull;
Posted by
<a class="post_author" href="/u/{{ post.author }}">u/{{ post.author }}</a>
&bull; <a class="post_author" href="/u/{{ post.author }}">u/{{ post.author }}</a>
{% if post.author_flair.0 != "" %}
<small class="author_flair">{{ post.author_flair.0 }}</small>
{% endif %}
<span class="datetime" style="float: right;">{{ post.time }}</span>
</h4>
<h3 class="post_title">
{% if post.flair.0 == "Comment" %}
{% else if post.flair.0 == "" %}
{% else %}
<small style="color:{{ post.flair.2 }}; background:{{ post.flair.1 }}">{{ post.flair.0 }}</small>
<small class="post_flair" style="color:{{ post.flair.2 }}; background:{{ post.flair.1 }}">{{ post.flair.0 }}</small>
{% endif %}
<a href="{{ post.url }}">{{ post.title }}</a>
</h3>
@ -52,7 +47,6 @@
{% else %}
<div class="comment">
<div class="comment_left">
<div class="comment_upvote"></div>
<h3 class="comment_score">{{ post.score }}</h3>
</div>
<div class="comment_right">