Compare commits

..

22 commits

Author SHA1 Message Date
fc28712e9f
fix libjens URL
Some checks reported errors
continuous-integration/drone/push Build encountered an error
2022-02-13 23:31:39 +01:00
0855af438e cli now shows meme id in list and search commands 2022-01-20 22:43:34 +01:00
676f5713ed fix upload endpoint 2022-01-09 00:40:44 +01:00
11a22df7ea bump version 2021-12-26 14:38:56 +01:00
9a4a5c5578 added rand command and aliases 2021-12-26 14:37:50 +01:00
e2ce737497 update changelog 2021-12-26 14:13:23 +01:00
e5a51be87e improve tables and remove fzf nonsense 2021-12-26 14:12:36 +01:00
6e3086c460 upgrade to new libjens to work with new api 2021-12-25 21:51:41 +01:00
506fa02cd4 add spinner
All checks were successful
continuous-integration/drone/push Build is passing
2021-08-21 22:12:26 +02:00
92e452c473 move jm_client_core to new libjens repo
Some checks failed
continuous-integration/drone/push Build is failing
https://tilera.xyz/git/LordMZTE/libjens/
2021-08-08 14:05:21 +02:00
0d3a772432 upgrade tokio
All checks were successful
continuous-integration/drone/push Build is passing
2021-06-18 21:52:35 +02:00
a5629ea741 add clear_cache function to JMClient
All checks were successful
continuous-integration/drone/push Build is passing
2021-06-18 16:20:28 +02:00
1d685eb772 memes now print out ID
All checks were successful
continuous-integration/drone/push Build is passing
2021-06-17 12:02:39 +02:00
a4d66b1fbe added errors to upload when tilera breaks the api and makes it return nonsense
All checks were successful
continuous-integration/drone/push Build is passing
2021-06-07 11:18:51 +02:00
d7c0831c85 use HRTB to get rid of DeserializeOwned trait bound in get_api_json
All checks were successful
continuous-integration/drone/push Build is passing
2021-06-02 20:27:22 +02:00
df7d39971b client is now not static anymore
All checks were successful
continuous-integration/drone/push Build is passing
2021-05-27 18:01:30 +02:00
3c395a34a7 refactors and add timestamp to meme display
All checks were successful
continuous-integration/drone/push Build is passing
2021-05-12 20:13:06 +02:00
1463a3cf0d Revert "add windows tests to CI"
All checks were successful
continuous-integration/drone/push Build is passing
I guess this isn't a good idea...

This reverts commit 831e971914.
2021-04-14 21:21:59 +02:00
831e971914 add windows tests to CI
Some checks failed
continuous-integration/drone/push Build is failing
2021-04-14 21:13:15 +02:00
8078fffc5d update to new API endpoint
All checks were successful
continuous-integration/drone/push Build is passing
2021-04-14 20:17:47 +02:00
010f6fef15 added constant for API endpoint for easy updating.
All checks were successful
continuous-integration/drone/push Build is passing
2021-04-14 20:15:31 +02:00
929c38bfed update changelog
All checks were successful
continuous-integration/drone/push Build is passing
2021-04-12 17:29:28 +02:00
21 changed files with 278 additions and 493 deletions

View file

@ -1,9 +1,4 @@
# v0.1.5
# v1.1.2
## cli
- added `--cat` option to print image to stdout
- added `--firsturl` option to print first link in search
## gui
- now exists
- fixed upload endpoint

View file

@ -2,6 +2,5 @@
members = [
"cli",
"gui",
"jm_client_core",
"tokencracker",
]

View file

@ -1,6 +1,6 @@
[package]
name = "cli"
version = "0.1.6"
version = "1.1.2"
authors = ["LordMZTE <lord@mzte.de>"]
edition = "2018"
license = "GPL-3.0"
@ -16,18 +16,23 @@ name = "jm"
path = "src/main.rs"
[dependencies]
jm_client_core = { path = "../jm_client_core" }
anyhow = "1.0.34"
clap = "2.33.3"
env_logger = "0.8.2"
anyhow = "1.0.52"
chrono = "0.4.19"
clap = "3.0.10"
comfy-table = "5.0.0"
env_logger = "0.9.0"
fuzzy-matcher = "0.3.7"
log = "0.4.11"
opener = "0.4.1"
structopt = "0.3.21"
term-table = "1.3.0"
indicatif = "0.16.2"
libjens = { git = "https://git.tilera.org/LordMZTE/libjens.git", rev = "1.1.0" }
log = "0.4.14"
opener = "0.5.0"
reqwest = { version = "0.11.9", features = ["stream", "multipart"] }
serde_json = "1.0.75"
structopt = "0.3.26"
term_size = "0.3.2"
tokio = { version = "0.2.23", features = ["macros", "fs", "process"] }
url = "2.2.0"
reqwest = { version = "0.10", features = ["stream"] }
tokio = { version = "1.15.0", features = ["macros", "fs", "process", "rt-multi-thread"] }
tokio-util = { version = "0.6.9", features = ["codec"] }
url = "2.2.2"
[features]

View file

@ -1,29 +0,0 @@
use crate::util::IntoTableRow;
use jm_client_core::api::{Category, Meme, User};
use term_table::{row::Row, table_cell::TableCell};
impl IntoTableRow for Category {
fn into_table_row(&self) -> Row<'_> {
Row::new(vec![TableCell::new(&self.id), TableCell::new(&self.name)])
}
}
impl IntoTableRow for Meme {
fn into_table_row(&self) -> Row<'_> {
Row::new(vec![
TableCell::new(&self.link),
TableCell::new(&self.category),
TableCell::new(&self.user),
])
}
}
impl IntoTableRow for User {
fn into_table_row(&self) -> Row<'_> {
Row::new(vec![
TableCell::new(&self.name),
TableCell::new(&self.get_id().map(String::as_ref).unwrap_or("[No ID]")),
TableCell::new(&self.dayuploads),
])
}
}

View file

@ -1,19 +1,17 @@
use crate::util::{self, IntoTableRow};
use jm_client_core::util::api;
use reqwest::Client;
use crate::table::{list_table, JMTableEntry, TableExt};
use libjens::{api::Category, JMClient};
pub async fn run(http: &Client) -> anyhow::Result<()> {
pub async fn run(client: &JMClient) -> anyhow::Result<()> {
// clone required, because for sorting the immutable reference will not work
let mut cats = api::cats(http).await?.clone();
let mut cats = client.get_cats().await?.as_ref().clone();
cats.sort_by(|a, b| a.id.cmp(&b.id));
let mut table = util::list_table();
for cat in &cats {
table.add_row(cat.into_table_row());
}
println!("{}", table.render());
println!(
"{}",
list_table()
.type_header::<Category>()
.add_rows(cats.into_iter().map(JMTableEntry))
);
Ok(())
}

View file

@ -1,87 +1,52 @@
use anyhow::{Context, Result};
use reqwest::Client;
use std::{
io::Write,
process::{Command, Stdio},
use crate::{
table::{list_table, JMTableEntry, TableExt},
util,
};
use crate::util::IntoTableRow;
use jm_client_core::util::{self, api, MemeSorting};
use anyhow::Result;
use libjens::{api::Meme, util::MemeSorting, JMClient};
pub async fn run(
http: &Client,
client: &JMClient,
cat: Option<String>,
user: Option<String>,
sorting: Option<MemeSorting>,
fzf: bool,
) -> Result<()> {
// This needs to be done so both users, memes and categories will be requested
// at once
let (memes, ..) = tokio::try_join!(
api::memes(
http,
cat.as_ref().map(String::from),
user.as_ref().map(String::from)
),
async { client.get_memes().await.map_err(|e| e.into()) },
async {
if let Some(c) = cat.as_ref() {
util::assert_category_exists(http, c).await
util::assert_category_exists(client, c).await
} else {
Ok(())
}
},
async {
if let Some(u) = user.as_ref() {
util::assert_user_exists(http, u).await
util::assert_user_exists(client, u).await
} else {
Ok(())
}
},
)?;
let mut memes = memes.iter().collect::<Vec<_>>();
let mut memes = memes
.iter()
.filter(|m| cat.as_ref().map(|c| &m.category == c).unwrap_or(true))
.filter(|m| user.as_ref().map(|u| &m.user == u).unwrap_or(true))
.collect::<Vec<_>>();
if let Some(s) = sorting {
s.sort_with(&mut memes);
}
let mut table = crate::util::list_table();
for m in memes.iter() {
table.add_row(m.into_table_row());
}
let table_str = table.render();
if fzf {
let mut child = Command::new("fzf")
.args(&["--delimiter", "\\t", "--with-nth", "2"])
.stdout(Stdio::piped())
.stdin(Stdio::piped())
.spawn()
.context("Failed to spawn FZF")?;
let stdin = child.stdin.as_mut().context("could not get FZF stdin")?;
for (idx, line) in table_str.lines().enumerate() {
stdin
.write(format!("{}\t{}\n", idx, line).as_bytes())
.context("Failed to write to FZF")?;
}
let out = child.wait_with_output()?;
let out_str = String::from_utf8(out.stdout).context("FZF output is invalid UTF-8")?;
let idx = out_str
.split('\t')
.next()
.and_then(|s| s.parse::<usize>().ok())
.context("Failed to parse FZF output")?;
let meme = memes
.get(idx)
.context("Falied to retrieve meme FZF returned")?;
println!("{}", meme.link);
} else {
println!("{}", table_str);
}
println!(
"{}",
list_table()
.type_header::<Meme>()
.add_rows(memes.into_iter().cloned().map(JMTableEntry))
);
Ok(())
}

View file

@ -1,5 +1,6 @@
pub mod cats;
pub mod list;
pub mod rand;
pub mod search;
pub mod up;
pub mod users;

21
cli/src/commands/rand.rs Normal file
View file

@ -0,0 +1,21 @@
use anyhow::Result;
use chrono::{Local, TimeZone};
use libjens::JMClient;
pub async fn run(client: &JMClient) -> Result<()> {
let meme = client.get_random().await?;
println!(
"\
Link: {}
Category: {}
User: {}
Timestamp: {}",
meme.link,
meme.category,
meme.user,
Local.timestamp(meme.timestamp, 0).format("%F %R")
);
Ok(())
}

View file

@ -1,13 +1,15 @@
use anyhow::{Context, Result};
use log::info;
use reqwest::Client;
use std::io::Write;
use crate::util::IntoTableRow;
use jm_client_core::util::{self, api};
use crate::{
table::{list_table, JMTableEntry, TableExt},
util,
};
use libjens::{api::Meme, JMClient};
pub async fn run(
http: &Client,
client: &JMClient,
query: String,
user: Option<String>,
category: Option<String>,
@ -15,21 +17,17 @@ pub async fn run(
cat: bool,
) -> Result<()> {
let (memes, ..) = tokio::try_join!(
api::memes(
http,
category.as_ref().map(String::clone),
user.as_ref().map(String::clone),
),
async { client.get_memes().await.map_err(|e| e.into()) },
async {
if let Some(u) = user.as_ref() {
util::assert_user_exists(http, u).await
util::assert_user_exists(client, u).await
} else {
Ok(())
}
},
async {
if let Some(c) = category.as_ref() {
util::assert_category_exists(http, c).await
util::assert_category_exists(client, c).await
} else {
Ok(())
}
@ -40,7 +38,12 @@ pub async fn run(
info!("Starting search with query '{}'", query);
for meme in memes.iter() {
let memes = memes
.iter()
.filter(|m| category.as_ref().map(|c| &m.category == c).unwrap_or(true))
.filter(|m| user.as_ref().map(|u| &m.user == u).unwrap_or(true));
for meme in memes {
let file_name = meme.file_name()?;
if let Some(score) = fuzzy_matcher::clangd::fuzzy_match(file_name, &query) {
info!("Found matching meme '{}' with score {}", file_name, score);
@ -50,14 +53,11 @@ pub async fn run(
matches.sort_by(|a, b| b.1.cmp(&a.1));
let res = match (firsturl, cat) {
(false, false) => {
let mut table = crate::util::list_table();
for m in matches {
table.add_row(m.0.into_table_row());
}
table.render().into_bytes()
},
(false, false) => list_table()
.type_header::<Meme>()
.add_rows(matches.into_iter().map(|(m, _)| JMTableEntry(m.clone())))
.to_string()
.into_bytes(),
(true, _) => matches
.first()
@ -68,7 +68,7 @@ pub async fn run(
.into_bytes(),
(_, true) => {
let url = &matches.first().context("No results found")?.0.link;
http.get(url).send().await?.bytes().await?.to_vec()
client.http.get(url).send().await?.bytes().await?.to_vec()
},
};

View file

@ -1,36 +1,56 @@
use crate::util::open_link;
use anyhow::Result;
use jm_client_core::{api::UpResp, util};
use crate::util;
use anyhow::{bail, Result};
use indicatif::{ProgressBar, ProgressStyle};
use libjens::{api::UpResp, JMClient};
use log::info;
use reqwest::{
multipart::{Form, Part},
Body,
Client,
};
use tokio::{fs::File, io::reader_stream};
use tokio::fs::File;
use tokio_util::codec::{BytesCodec, FramedRead};
pub async fn run(
http: &Client,
client: &JMClient,
token: String,
path: String,
name: String,
category: String,
open: bool,
) -> Result<()> {
util::assert_category_exists(http, &category).await?;
util::assert_category_exists(client, &category).await?;
let res = http
.post("https://data.tilera.xyz/api/jensmemes/upload")
let spinner = ProgressBar::new_spinner();
spinner.enable_steady_tick(50);
spinner.set_style(ProgressStyle::default_spinner().tick_strings(&[
"🗎 JM",
" 🗎 JM",
" 🗎 JM",
" 🗎 JM",
" 🗎 JM",
" 🗎 JM",
" 🗎 JM",
" 🗎JM",
"▪▪▪▪▪▪▪▪▪▪",
]));
spinner.set_message("Uploading...");
let res = client
.http
.post("https://api.tilera.xyz/jensmemes/v1/upload")
.multipart(
Form::new()
.text("category", category)
.text("token", token)
.part(
"file",
Part::stream(Body::wrap_stream(reader_stream({
info!("Opening file {}", &path);
File::open(path).await?
})))
Part::stream(Body::wrap_stream(FramedRead::new(
{
info!("Opening file {}", &path);
File::open(path).await?
},
BytesCodec::new(),
)))
.file_name(name),
),
)
@ -38,7 +58,18 @@ pub async fn run(
.await?;
let status = res.status();
let res = util::api::try_deserialize_api_reponse::<UpResp>(&res.bytes().await?)?;
// TODO move into JMClient
let bytes = &res.bytes().await?;
spinner.finish_with_message("Done!");
let res = if let Ok(res) = serde_json::from_slice::<UpResp>(bytes) {
res
} else if let Ok(s) = std::str::from_utf8(bytes) {
bail!("Server responded with unexpected response: {}", s);
} else {
bail!("Server responded with invalid utf8 bytes: {:?}", bytes);
};
println!("Server responded with code {}", status);
@ -48,7 +79,7 @@ pub async fn run(
for f in res.files {
if open {
open_link(&f).await?;
util::open_link(&f).await?;
} else {
println!("{}", f);
}

View file

@ -1,17 +1,17 @@
use crate::util::IntoTableRow;
use anyhow::Result;
use jm_client_core::util::api;
use reqwest::Client;
use libjens::{api::User, JMClient};
pub async fn run(http: &Client) -> Result<()> {
let users = api::users(http).await?;
let mut table = crate::util::list_table();
use crate::table::{list_table, JMTableEntry, TableExt};
for u in users {
table.add_row(u.into_table_row())
}
pub async fn run(client: &JMClient) -> Result<()> {
let users = client.get_users().await?;
println!("{}", table.render());
println!(
"{}",
list_table()
.type_header::<User>()
.add_rows(users.iter().cloned().map(JMTableEntry))
);
Ok(())
}

View file

@ -1,10 +1,9 @@
use anyhow::Result;
use jm_client_core::util::MemeSorting;
use reqwest::Client;
use libjens::{util::MemeSorting, JMClient};
use structopt::StructOpt;
mod api;
mod commands;
mod table;
mod util;
#[derive(StructOpt)]
@ -40,10 +39,10 @@ enum Cmd {
open: bool,
},
#[structopt(about = "lists the available categories")]
#[structopt(about = "lists the available categories", alias = "c")]
Cats,
#[structopt(about = "searches for a meme")]
#[structopt(about = "searches for a meme", alias = "s")]
Search {
query: String,
@ -75,16 +74,16 @@ enum Cmd {
#[structopt(
long,
short,
help = "how to sort the results. can be id, user, category or link"
help = "how to sort the results. can be id, user, category, timestamp or link"
)]
sort: Option<MemeSorting>,
#[structopt(long, short, help = "search memes with FZF")]
fzf: bool,
},
#[structopt(about = "Lists all users")]
#[structopt(about = "Lists all users", alias = "u")]
Users,
#[structopt(about = "Gets a random meme", alias = "r")]
Rand,
}
#[tokio::main]
@ -92,7 +91,7 @@ async fn main() -> Result<()> {
env_logger::init();
let Opts { cmd } = Opts::from_args();
let http = Client::new();
let client = JMClient::new();
match cmd {
Cmd::Up {
@ -103,23 +102,23 @@ async fn main() -> Result<()> {
open,
} => {
let name = name.unwrap_or_else(|| file.clone());
commands::up::run(&http, token, file, name, category, open).await?;
commands::up::run(&client, token, file, name, category, open).await?;
},
Cmd::Cats => commands::cats::run(&http).await?,
Cmd::Cats => commands::cats::run(&client).await?,
Cmd::Search {
query,
user,
category,
firsturl,
cat,
} => commands::search::run(&http, query, user, category, firsturl, cat).await?,
} => commands::search::run(&client, query, user, category, firsturl, cat).await?,
Cmd::List {
category,
user,
sort,
fzf,
} => commands::list::run(&http, category, user, sort, fzf).await?,
Cmd::Users => commands::users::run(&http).await?,
} => commands::list::run(&client, category, user, sort).await?,
Cmd::Users => commands::users::run(&client).await?,
Cmd::Rand => commands::rand::run(&client).await?,
}
Ok(())

81
cli/src/table.rs Normal file
View file

@ -0,0 +1,81 @@
use chrono::{Local, TimeZone};
use comfy_table::{presets::UTF8_NO_BORDERS, ContentArrangement, Row, Table};
use libjens::api::{Category, Meme, User};
pub fn list_table() -> Table {
let mut table = Table::new();
table
.load_preset(UTF8_NO_BORDERS)
.set_content_arrangement(ContentArrangement::Dynamic);
table
}
pub trait TableHeader {
fn header() -> Row;
}
macro_rules! impl_table_header {
($t:ident, $e:expr) => {
impl TableHeader for $t {
fn header() -> Row {
$e.into()
}
}
};
}
impl_table_header!(User, vec!["Name", "ID"]);
impl_table_header!(Category, vec!["Name", "ID"]);
impl_table_header!(Meme, vec!["Link", "Category", "User", "Timestamp", "ID"]);
/// a newtype wrapper to convert libjens types to table rows
pub struct JMTableEntry<T>(pub T);
impl From<JMTableEntry<User>> for Row {
fn from(e: JMTableEntry<User>) -> Self {
vec![e.0.name, e.0.id.unwrap_or_else(String::new)].into()
}
}
impl From<JMTableEntry<Category>> for Row {
fn from(e: JMTableEntry<Category>) -> Self {
vec![e.0.name, e.0.id].into()
}
}
impl From<JMTableEntry<Meme>> for Row {
fn from(e: JMTableEntry<Meme>) -> Self {
vec![
e.0.link,
e.0.category,
e.0.user,
Local
.timestamp(e.0.timestamp, 0)
.format("%F %R")
.to_string(),
e.0.id,
]
.into()
}
}
pub trait TableExt {
fn add_rows<T: Into<Row>>(&mut self, iter: impl IntoIterator<Item = T>) -> &mut Self;
fn type_header<T: TableHeader>(&mut self) -> &mut Self;
}
impl TableExt for Table {
fn add_rows<T: Into<Row>>(&mut self, iter: impl IntoIterator<Item = T>) -> &mut Self {
for i in iter.into_iter() {
self.add_row(i);
}
self
}
fn type_header<T: TableHeader>(&mut self) -> &mut Self {
self.set_header(T::header())
}
}

View file

@ -1,6 +1,26 @@
use term_table::{Table, TableBuilder, TableStyle};
use anyhow::bail;
use libjens::JMClient;
use tokio::process::Command;
pub const NO_SUCH_CATEGORY_ERROR: &str = "The given Category does not exist!";
pub const NO_SUCH_USER_ERROR: &str = "The given User does not exist!";
pub async fn assert_category_exists(client: &JMClient, cat: &str) -> anyhow::Result<()> {
if !client.get_cats().await?.iter().any(|c| c.id == cat) {
bail!(NO_SUCH_CATEGORY_ERROR);
}
Ok(())
}
pub async fn assert_user_exists(client: &JMClient, user: &str) -> anyhow::Result<()> {
if !client.get_users().await?.iter().any(|u| u.name == user) {
bail!(NO_SUCH_USER_ERROR);
}
Ok(())
}
pub async fn open_link(url: &str) -> anyhow::Result<()> {
match std::env::var_os("BROWSER") {
Some(browser) => {
@ -11,17 +31,3 @@ pub async fn open_link(url: &str) -> anyhow::Result<()> {
Ok(())
}
/// returns an empty table with the correct format settings for lists
pub fn list_table<'a>() -> Table<'a> {
TableBuilder::new()
.style(TableStyle::simple())
.separate_rows(false)
.has_top_boarder(false)
.has_bottom_boarder(false)
.build()
}
pub trait IntoTableRow {
fn into_table_row(&self) -> term_table::row::Row;
}

View file

@ -16,6 +16,6 @@ path = "src/main.rs"
[dependencies]
anyhow = "1.0.40"
druid = "0.7.0"
jm_client_core = { path = "../jm_client_core" }
tokio = { version = "0.2.23", features = ["macros"] }
libjens = { git = "https://git.tilera.org/LordMZTE/libjens.git", rev = "d4a8b3" }
reqwest = "0.10"
tokio = { version = "0.2.23", features = ["macros"] }

View file

@ -11,8 +11,7 @@ use druid::{
WidgetExt,
WindowDesc,
};
use jm_client_core::api::Meme;
use reqwest::Client;
use libjens::{api::Meme, JMClient};
pub(crate) mod util;
@ -20,8 +19,9 @@ const LIST_COLS: &[(&str, f64)] = &[("Link", 1000.), ("User", 50.)];
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let http = Client::new();
let mut memes = jm_client_core::util::api::memes(&http, None, None)
let client = JMClient::new();
let mut memes = client
.get_memes()
.await?
.iter()
.map(|m| EqData(m.clone()))

View file

@ -1,20 +0,0 @@
[package]
name = "jm_client_core"
version = "0.1.6"
authors = ["LordMZTE <lord@mzte.de>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
anyhow = "1.0.34"
env_logger = "0.8.2"
lazy_static = "1.4.0"
log = "0.4.11"
once_cell = "1.5.2"
reqwest = "0.10.9"
serde = { version = "1.0.117", features = ["derive"] }
serde_json = "1.0.60"
thiserror = "1.0.23"
tokio = { version = "0.2.23", features = ["macros", "fs"] }
url = "2.2.0"

View file

@ -1,64 +0,0 @@
use anyhow::{anyhow, Result};
use serde::Deserialize;
#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct UpResp {
pub files: Vec<String>,
}
#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct CatsResp {
pub categories: Vec<Category>,
}
#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct UsersResp {
pub users: Vec<User>,
}
#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct MemesResp {
pub memes: Vec<Meme>,
}
#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct Category {
pub id: String,
pub name: String,
}
#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct Meme {
pub id: String,
pub link: String,
pub path: String,
pub category: String,
pub user: String,
}
impl Meme {
pub fn file_name(&self) -> Result<&str> {
self.path
.split('/')
.last()
.ok_or_else(|| anyhow!("failed to get file name. server response invalid"))
}
}
#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct User {
pub name: String,
pub id: Option<String>,
pub tokenhash: Option<String>,
pub userdir: Option<String>,
pub dayuploads: String,
}
impl User {
pub fn get_id(&self) -> Option<&String> {
self.id
.as_ref()
.or(self.tokenhash.as_ref())
.or(self.userdir.as_ref())
}
}

View file

@ -1,2 +0,0 @@
pub mod api;
pub mod util;

View file

@ -1,201 +0,0 @@
use crate::api::Meme;
use anyhow::bail;
use reqwest::Client;
use std::str::FromStr;
#[macro_export]
macro_rules! init_once_cell {
($cell:ident, $init_fn:expr) => {
match $cell.get() {
Some(x) => x,
None => {
let x = $init_fn;
$cell.get_or_init(|| x)
},
}
};
}
pub mod consts {
pub const NO_SUCH_CATEGORY_ERROR: &str = "The given Category does not exist!";
pub const NO_SUCH_USER_ERROR: &str = "The given User does not exist!";
}
/// ways to communicyte with the JM API
pub mod api {
use crate::api::{Category, CatsResp, Meme, MemesResp, User, UsersResp};
use anyhow::Result;
use lazy_static::lazy_static;
use log::info;
use once_cell::sync::OnceCell;
use reqwest::Client;
use std::{
collections::HashMap,
sync::{Arc, Mutex},
};
use thiserror::Error;
use url::Url;
// cached api responses
static USERS: OnceCell<Vec<User>> = OnceCell::new();
static CATS: OnceCell<Vec<Category>> = OnceCell::new();
lazy_static! {
// is this type long enough yet?
static ref MEMES: Mutex<HashMap<(Option<String>, Option<String>), Arc<Vec<Meme>>>> =
Mutex::new(HashMap::new());
}
#[derive(Error, Debug)]
#[error("Error Deserializing JSON api response: \n{json}\nError: {error}")]
pub struct ApiDeserializeError {
pub json: String,
#[source]
pub error: serde_json::Error,
}
pub async fn cats(http: &Client) -> Result<&Vec<Category>> {
Ok(init_once_cell!(CATS, {
info!("Requesting categories from server");
let res = http
.get("https://data.tilera.xyz/api/jensmemes/categories")
.send()
.await?;
let cats = try_deserialize_api_reponse::<CatsResp>(&res.bytes().await?)?;
cats.categories.into_iter().collect()
}))
}
pub async fn memes<'a>(
http: &Client,
cat_filter: Option<String>,
usr_filter: Option<String>,
) -> Result<Arc<Vec<Meme>>> {
let filters = (cat_filter, usr_filter);
if let Some(m) = MEMES.lock().unwrap().get(&filters) {
return Ok(m.clone());
}
let mut url = Url::options().parse("https://data.tilera.xyz/api/jensmemes/memes")?;
let mut pairs = url.query_pairs_mut();
if let Some(cat) = filters.0.as_ref() {
pairs.append_pair("category", cat);
}
if let Some(usr) = filters.1.as_ref() {
pairs.append_pair("user", usr);
}
// drop required in order to move the URL into the request
drop(pairs);
info!("Requesting memes from server");
let res = http.get(url).send().await?;
let memes = try_deserialize_api_reponse::<MemesResp>(&res.bytes().await?)?;
Ok(MEMES
.lock()
.unwrap()
.entry(filters)
.or_insert(Arc::new(memes.memes))
.clone())
}
pub async fn users(http: &Client) -> Result<&Vec<User>> {
Ok(init_once_cell!(USERS, {
info!("Requesting users from server");
let res = http
.get("https://data.tilera.xyz/api/jensmemes/users")
.send()
.await?;
let users = try_deserialize_api_reponse::<UsersResp>(&res.bytes().await?)?;
users.users
}))
}
/// tries to deserialize `json` as `T`, and if it fails converts the error
/// to a `ApiDeserializeError`
pub fn try_deserialize_api_reponse<'a, T: serde::Deserialize<'a>>(json: &'a [u8]) -> Result<T> {
let result = serde_json::from_slice::<T>(&json);
result.map_err(|error| match std::str::from_utf8(json) {
Err(e) => e.into(),
Ok(json) => ApiDeserializeError {
json: json.into(),
error,
}
.into(),
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn api_deserialize_error() {
let incorrect_json = r#"
{
"foo": "bar"
}
"#;
let result = try_deserialize_api_reponse::<UsersResp>(incorrect_json.as_bytes());
println!("{}", result.unwrap_err());
}
}
}
pub enum MemeSorting {
Id,
Link,
Category,
User,
}
impl MemeSorting {
pub fn sort_with(&self, memes: &mut [&Meme]) {
macro_rules! sort {
($list:ident, $field:ident) => {
$list.sort_by(|a, b| {
a.$field
.to_ascii_lowercase()
.cmp(&b.$field.to_ascii_lowercase())
});
};
}
match self {
Self::Id => sort!(memes, id),
Self::Link => sort!(memes, link),
Self::Category => sort!(memes, category),
Self::User => sort!(memes, user),
}
}
}
impl FromStr for MemeSorting {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_ref() {
"id" => Ok(Self::Id),
"link" => Ok(Self::Link),
"category" => Ok(Self::Category),
"user" => Ok(Self::User),
_ => bail!("Invalid Meme sorting! options are id, link, category and user!"),
}
}
}
pub async fn assert_user_exists(http: &Client, user: &str) -> anyhow::Result<()> {
if !api::users(http).await?.iter().any(|u| u.name == user) {
bail!(consts::NO_SUCH_USER_ERROR);
}
Ok(())
}
pub async fn assert_category_exists(http: &Client, cat: &str) -> anyhow::Result<()> {
if !api::cats(http).await?.iter().any(|c| c.id == cat) {
bail!(consts::NO_SUCH_CATEGORY_ERROR);
}
Ok(())
}

View file

@ -16,10 +16,10 @@ name = "jmtoken"
path = "src/main.rs"
[dependencies]
anyhow = "1.0.34"
clap = "2.33.3"
anyhow = "1.0.52"
clap = "2.34.0"
md5 = "0.7.0"
reqwest = "0.10.8"
serde = { version = "1.0.117", features = ["derive"] }
serde_json = "1.0.59"
tokio = { version = "~0.2", features = ["full"] }
reqwest = "0.11.8"
serde = { version = "1.0.132", features = ["derive"] }
serde_json = "1.0.73"
tokio = { version = "1.15.0", features = ["full"] }