Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions sources/multi.suwayomi/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions sources/multi.suwayomi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2024"
aidoku = { git = "https://github.com/Aidoku/aidoku-rs.git", version = "0.3.0", features = ["json"] }
serde = { version = "1.0", features = ["derive"], default-features = false }
serde_json = { version = "1.0.143", default-features = false, features = ["alloc"] }
base64 = { version = "0.22", default-features = false, features = ["alloc"] }

[dev-dependencies]
aidoku = { git = "https://github.com/Aidoku/aidoku-rs.git", features = ["test"] }
Expand Down
25 changes: 24 additions & 1 deletion sources/multi.suwayomi/res/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,30 @@
"returnKeyType": 9,
"autocorrectionDisabled": true,
"refreshes": ["content"]
},
{
"type": "select",
"key": "authMode",
"title": "Auth Mode",
"values": ["auto", "none", "basic_auth", "simple_login"],
"titles": [
"Auto",
"None",
"Basic Auth",
"Simple Login"
],
"default": "auto",
"refreshes": ["content"]
},
{
"type": "login",
"method": "basic",
"key": "credentials",
"title": "LOGIN",
"requires": "baseUrl",
"refreshes": ["content", "listings"]
}
]
],
"footer": "Enter your Suwayomi server URL and optionally login with your credentials."
}
]
2 changes: 1 addition & 1 deletion sources/multi.suwayomi/res/source.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"info": {
"id": "multi.suwayomi",
"name": "Suwayomi",
"version": 3,
"version": 4,
"url": "https://github.com/Suwayomi",
"contentRating": 0,
"languages": [
Expand Down
145 changes: 138 additions & 7 deletions sources/multi.suwayomi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,104 @@ use crate::models::{
};
use aidoku::imports::std::send_partial_result;
use aidoku::{
AidokuError, BaseUrlProvider, Chapter, DynamicListings, FilterValue, Listing, ListingProvider,
Manga, MangaPageResult, Page, PageContent, Result, Source,
AidokuError, BaseUrlProvider, BasicLoginHandler, Chapter, DynamicListings, FilterValue,
Listing, ListingProvider, Manga, MangaPageResult, Page, PageContent, Result, Source,
alloc::{String, Vec},
imports::net::Request,
prelude::*,
};
use alloc::string::ToString;
use alloc::vec;
use base64::{Engine, engine::general_purpose::STANDARD};

struct Suwayomi;

impl Suwayomi {
fn send_graphql_post_request(
&self,
base_url: &str,
body: String,
) -> core::result::Result<Request, aidoku::imports::net::RequestError> {
let req = Request::post(format!("{base_url}/api/graphql"))?
.header("Content-Type", "application/json")
.body(body);
Ok(req)
}

fn send_basic_auth_request(
&self,
base_url: &str,
user: &str,
pass: &str,
body: String,
) -> core::result::Result<Request, aidoku::imports::net::RequestError> {
let auth = STANDARD.encode(format!("{user}:{pass}"));
let req = self
.send_graphql_post_request(base_url, body)?
.header("Authorization", &format!("Basic {auth}"));
Ok(req)
}

fn send_form_login_request(
&self,
base_url: &str,
user: &str,
pass: &str,
) -> core::result::Result<Request, aidoku::imports::net::RequestError> {
let form = format!(
"user={}&pass={}",
aidoku::helpers::uri::encode_uri_component(user),
aidoku::helpers::uri::encode_uri_component(pass)
);
let req = Request::post(format!("{base_url}/login.html"))?
.header("Content-Type", "application/x-www-form-urlencoded")
.body(form);
Ok(req)
}

fn graphql_request<T>(&self, body: serde_json::Value) -> Result<GraphQLResponse<T>>
where
T: serde::de::DeserializeOwned,
{
let base_url = settings::get_base_url()?;
Request::post(format!("{base_url}/api/graphql"))?
.header("Content-Type", "application/json")
.body(body.to_string())
.json_owned::<GraphQLResponse<T>>()
let auth_mode = settings::get_auth_mode();
let body_str = body.to_string();

let send_req = |with_basic: bool| -> Result<GraphQLResponse<T>> {
let request = if with_basic && let Some((user, pass)) = settings::get_credentials() {
self.send_basic_auth_request(&base_url, &user, &pass, body_str.clone())?
} else {
self.send_graphql_post_request(&base_url, body_str.clone())?
};
request.json_owned::<GraphQLResponse<T>>()
};

let do_login_html = || -> Result<()> {
if let Some((user, pass)) = settings::get_credentials() {
let _ = self
.send_form_login_request(&base_url, &user, &pass)?
.send()
.ok();
}
Ok(())
};

match auth_mode.as_str() {
"none" => send_req(false),
"basic_auth" => send_req(true),
"simple_login" => {
do_login_html()?;
send_req(false)
}
_ => {
let resp = send_req(true);
if resp.is_err() {
do_login_html()?;
return send_req(true);
}
resp
}
}
}

fn execute_query<T>(
Expand Down Expand Up @@ -271,4 +348,58 @@ impl BaseUrlProvider for Suwayomi {
}
}

register_source!(Suwayomi, ListingProvider, BaseUrlProvider, DynamicListings);
impl BasicLoginHandler for Suwayomi {
fn handle_basic_login(&self, _key: String, username: String, password: String) -> Result<bool> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks like code mostly duplicated from graphql_request. could you refactor this a bit?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heyo. I'm still learning rust, but I think I got what you're saying. Do the changes make sense?

let base_url = settings::get_base_url()?;
let auth_mode = settings::get_auth_mode();

let send_basic_req = || {
let body = serde_json::json!({
"operationName": graphql::GraphQLQuery::CATEGORIES.operation_name,
"query": graphql::GraphQLQuery::CATEGORIES.query,
});
self.send_basic_auth_request(&base_url, &username, &password, body.to_string())?
.send()
};

let send_form_req = || {
self.send_form_login_request(&base_url, &username, &password)?
.send()
};

match auth_mode.as_str() {
"none" => Ok(true),
"basic_auth" => {
let resp = send_basic_req()?;
Ok(resp.status_code() == 200)
}
"simple_login" => {
let resp = send_form_req()?;
Ok(resp.status_code() == 200)
}
_ => {
// auto: try basic auth first
if let Ok(resp) = send_basic_req()
&& resp.status_code() == 200
{
return Ok(true);
}
// try form login next
if let Ok(resp) = send_form_req()
&& resp.status_code() == 200
{
return Ok(true);
}
Ok(false)
}
}
}
}

register_source!(
Suwayomi,
ListingProvider,
BaseUrlProvider,
DynamicListings,
BasicLoginHandler
);
29 changes: 23 additions & 6 deletions sources/multi.suwayomi/src/settings.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
use aidoku::{AidokuError, alloc::string::String, imports::defaults::defaults_get, prelude::bail};
use aidoku::{Result, alloc::String, imports::defaults::defaults_get, prelude::*};

const BASE_URL_KEY: &str = "baseUrl";
const AUTH_MODE_KEY: &str = "authMode";
const USERNAME_KEY: &str = "credentials.username";
const PASSWORD_KEY: &str = "credentials.password";

pub fn get_base_url() -> Result<String, AidokuError> {
let base_url = defaults_get::<String>(BASE_URL_KEY);
match base_url {
Some(url) if !url.is_empty() => Ok(url),
_ => bail!("Base Url not configured"),
pub fn get_base_url() -> Result<String> {
let url: String = defaults_get::<String>(BASE_URL_KEY).ok_or(error!("Missing baseUrl"))?;
Ok(url.trim_end_matches('/').into())
}

pub fn get_auth_mode() -> String {
match defaults_get::<String>(AUTH_MODE_KEY) {
Some(v) if !v.is_empty() => v,
_ => "auto".into(),
}
}

pub fn get_credentials() -> Option<(String, String)> {
let user: String = defaults_get::<String>(USERNAME_KEY)?;
let pass: String = defaults_get::<String>(PASSWORD_KEY)?;

if user.is_empty() {
return None;
}
Some((user, pass))
}
Loading