Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
feat: migrate to lemmy v0.19 (closes #33)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed Jan 6, 2024
1 parent 7f8d62c commit c069b15
Show file tree
Hide file tree
Showing 23 changed files with 223 additions and 3,411 deletions.
1,040 changes: 142 additions & 898 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ relm4-components = { version = "0.6.2", features = ["web"] }
reqwest = { version = "0.11", features = ["json", "blocking", "multipart"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
lemmy_api_common = "0.18"
lemmy_api_common = "0.19"
markdown = "0.3"
html2pango = "0.5"
rand = "0.8"
Expand Down
23 changes: 3 additions & 20 deletions src/api/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ use lemmy_api_common::{
lemmy_db_schema::newtypes::{CommentId, PostId},
};

use crate::settings;

pub fn create_comment(
post_id: PostId,
content: String,
Expand All @@ -17,19 +15,14 @@ pub fn create_comment(
post_id,
content,
parent_id,
auth: settings::get_current_account().jwt.unwrap(),
..Default::default()
};
super::post("/comment", &params)
}

// see posts.rs for possible score parameters
pub fn like_comment(comment_id: CommentId, score: i16) -> Result<CommentResponse, reqwest::Error> {
let params = CreateCommentLike {
comment_id,
score,
auth: settings::get_current_account().jwt.unwrap(),
};
let params = CreateCommentLike { comment_id, score };
super::post("/comment/like", &params)
}

Expand All @@ -40,7 +33,6 @@ pub fn edit_comment(
let params = EditComment {
content: Some(body),
comment_id,
auth: settings::get_current_account().jwt.unwrap(),
..Default::default()
};
super::put("/post", &params)
Expand All @@ -50,28 +42,19 @@ pub fn delete_comment(comment_id: CommentId) -> Result<CommentResponse, reqwest:
let params = DeleteComment {
comment_id,
deleted: true,
auth: settings::get_current_account().jwt.unwrap(),
};
super::post("/comment/delete", &params)
}

pub fn save_comment(comment_id: CommentId, save: bool) -> Result<CommentResponse, reqwest::Error> {
let params = SaveComment {
auth: settings::get_current_account().jwt.unwrap(),
comment_id,
save,
};
let params = SaveComment { comment_id, save };
super::put("/comment/save", &params)
}

pub fn report_comment(
comment_id: CommentId,
reason: String,
) -> Result<CommentReportResponse, reqwest::Error> {
let params = CreateCommentReport {
comment_id,
reason,
auth: settings::get_current_account().jwt.unwrap(),
};
let params = CreateCommentReport { comment_id, reason };
super::post("/comment/report", &params)
}
2 changes: 0 additions & 2 deletions src/api/communities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use lemmy_api_common::{
};

use super::search;
use crate::settings;

pub fn fetch_communities(
page: i64,
Expand All @@ -17,7 +16,6 @@ pub fn fetch_communities(
type_: listing_type,
sort: Some(SortType::TopMonth),
page: Some(page),
auth: settings::get_current_account().jwt,
..Default::default()
};

Expand Down
5 changes: 0 additions & 5 deletions src/api/community.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@ use lemmy_api_common::{
lemmy_db_schema::newtypes::CommunityId,
};

use crate::settings;

pub fn get_community(id: CommunityId) -> std::result::Result<GetCommunityResponse, reqwest::Error> {
let params = GetCommunity {
id: Some(id),
auth: settings::get_current_account().jwt,
..Default::default()
};

Expand All @@ -25,7 +22,6 @@ pub fn follow_community(
let params = FollowCommunity {
community_id,
follow,
auth: settings::get_current_account().jwt.unwrap(),
};
super::post("/community/follow", &params)
}
Expand All @@ -41,7 +37,6 @@ pub fn block_community(
let params = BlockCommunity {
community_id,
block,
auth: settings::get_current_account().jwt.unwrap(),
};

super::post("/community/block", &params)
Expand Down
26 changes: 11 additions & 15 deletions src/api/instances.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
use crate::settings;
use lemmy_api_common::{
lemmy_db_schema::source::instance::Instance,
site::{GetFederatedInstances, GetFederatedInstancesResponse},
};

pub fn fetch_instances(query_filter: &str) -> std::result::Result<Vec<Instance>, reqwest::Error> {
// TODO: Update code to use the Instance views from lemmy 0.18.0
let params = GetFederatedInstances {
auth: settings::get_current_account().jwt,
};
use lemmy_api_common::site::{GetFederatedInstancesResponse, InstanceWithFederationState};

pub fn fetch_instances(
query_filter: &str,
) -> std::result::Result<Vec<InstanceWithFederationState>, reqwest::Error> {
// we fetch the instances from the official instance because the instance is likely unset on first startup
let instances = super::CLIENT
.get("https://lemmy.ml/api/v3/federated_instances".to_owned())
.query(&params)
.send()?
.json::<GetFederatedInstancesResponse>()?;

Expand All @@ -23,11 +15,15 @@ pub fn fetch_instances(query_filter: &str) -> std::result::Result<Vec<Instance>,
.linked
.iter()
.filter(|instance| {
instance.software == Some("lemmy".to_owned())
&& instance.domain.clone().contains(&lowercase_query_filter)
instance.instance.software == Some("lemmy".to_owned())
&& instance
.instance
.domain
.clone()
.contains(&lowercase_query_filter)
})
.cloned()
.collect::<Vec<Instance>>()),
.collect::<Vec<InstanceWithFederationState>>()),
None => Ok(vec![]),
}
}
42 changes: 37 additions & 5 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use serde::{de::DeserializeOwned, Serialize};

use crate::{config, settings::get_current_account};
use crate::{
config,
settings::{self, get_current_account},
};

pub mod auth;
pub mod comment;
Expand All @@ -19,7 +22,7 @@ pub mod user;
static API_VERSION: &str = "v3";

use relm4::once_cell::sync::Lazy;
use reqwest::blocking::Client;
use reqwest::{blocking::Client, header::HeaderMap, header::HeaderValue};

pub static CLIENT: Lazy<Client> = Lazy::new(|| {
let user_agent = format!("{}/{}", config::NAME, config::VERSION);
Expand All @@ -37,26 +40,55 @@ fn get_url(path: &str) -> String {
format!("{}{}", get_api_url(), path)
}

fn get_auth_header() -> HeaderMap<HeaderValue> {
let mut headers = HeaderMap::new();

if let Some(jwt) = settings::get_current_account().jwt {
let auth_string = "Bearer ".to_string() + &jwt.into_inner();
headers.insert(
"Authorization",
HeaderValue::from_str(&auth_string).unwrap(),
);
}

headers
}

fn get<T, Params>(path: &str, params: &Params) -> Result<T, reqwest::Error>
where
T: DeserializeOwned,
Params: Serialize + std::fmt::Debug,
{
CLIENT.get(get_url(path)).query(&params).send()?.json()
CLIENT
.get(get_url(path))
.headers(get_auth_header())
.query(&params)
.send()?
.json()
}

fn post<T, Params>(path: &str, params: &Params) -> Result<T, reqwest::Error>
where
T: DeserializeOwned,
Params: Serialize + std::fmt::Debug,
{
CLIENT.post(get_url(path)).json(&params).send()?.json()
CLIENT
.post(get_url(path))
.headers(get_auth_header())
.json(&params)
.send()?
.json()
}

fn put<T, Params>(path: &str, params: &Params) -> Result<T, reqwest::Error>
where
T: DeserializeOwned,
Params: Serialize + std::fmt::Debug,
{
CLIENT.put(get_url(path)).json(&params).send()?.json()
CLIENT
.put(get_url(path))
.headers(get_auth_header())
.json(&params)
.send()?
.json()
}
15 changes: 2 additions & 13 deletions src/api/moderation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,22 @@ use lemmy_api_common::{
comment::{CommentResponse, RemoveComment},
lemmy_db_schema::newtypes::{CommentId, PostId},
post::{PostResponse, RemovePost},
sensitive::Sensitive,
};

pub fn remove_post(
post_id: i32,
reason: String,
auth: Sensitive<String>,
) -> Result<PostResponse, reqwest::Error> {
pub fn remove_post(post_id: i32, reason: String) -> Result<PostResponse, reqwest::Error> {
let params = RemovePost {
post_id: PostId(post_id),
removed: true,
reason: Some(reason),
auth,
};
super::post("/post/remove", &params)
}

pub fn remove_comment(
comment_id: i32,
reason: String,
auth: Sensitive<String>,
) -> Result<CommentResponse, reqwest::Error> {
pub fn remove_comment(comment_id: i32, reason: String) -> Result<CommentResponse, reqwest::Error> {
let params = RemoveComment {
comment_id: CommentId(comment_id),
removed: true,
reason: Some(reason),
auth,
};
super::post("/comment/remove", &params)
}
28 changes: 5 additions & 23 deletions src/api/post.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::settings;
use itertools::Itertools;
use lemmy_api_common::{
comment::{GetComments, GetCommentsResponse},
Expand All @@ -18,7 +17,6 @@ pub fn get_post(id: PostId) -> Result<GetPostResponse, reqwest::Error> {
let params = GetPost {
id: Some(id),
comment_id: None,
auth: settings::get_current_account().jwt,
};

super::get("/post", &params)
Expand All @@ -30,7 +28,6 @@ pub fn get_comments(post_id: PostId) -> Result<Vec<CommentView>, reqwest::Error>
sort: Some(CommentSortType::Hot),
type_: Some(ListingType::All),
max_depth: Some(8),
auth: settings::get_current_account().jwt,
..Default::default()
};

Expand Down Expand Up @@ -70,7 +67,6 @@ pub fn create_post(
body: Some(body),
url,
community_id: CommunityId(community_id),
auth: settings::get_current_account().jwt.unwrap(),
..Default::default()
};
super::post("/post", &params)
Expand All @@ -87,54 +83,40 @@ pub fn edit_post(
body: Some(body),
url,
post_id: PostId(post_id),
auth: settings::get_current_account().jwt.unwrap(),
..Default::default()
};
super::put("/post", &params)
}

// for score, use 1 to upvote, -1 to vote down and 0 to reset the user's voting
pub fn like_post(post_id: PostId, score: i16) -> Result<PostResponse, reqwest::Error> {
let params = CreatePostLike {
post_id,
score,
auth: settings::get_current_account().jwt.unwrap(),
};
let params = CreatePostLike { post_id, score };
super::post("/post/like", &params)
}

pub fn delete_post(post_id: PostId) -> Result<PostResponse, reqwest::Error> {
let params = DeletePost {
post_id,
deleted: true,
auth: settings::get_current_account().jwt.unwrap(),
};
super::post("/post/delete", &params)
}

pub fn save_post(post_id: PostId, save: bool) -> Result<PostResponse, reqwest::Error> {
let params = SavePost {
auth: settings::get_current_account().jwt.unwrap(),
post_id,
save,
};
let params = SavePost { post_id, save };
super::put("/post/save", &params)
}

pub fn report_post(post_id: PostId, reason: String) -> Result<PostReportResponse, reqwest::Error> {
let params = CreatePostReport {
post_id,
reason,
auth: settings::get_current_account().jwt.unwrap(),
};
let params = CreatePostReport { post_id, reason };
super::post("/post/report", &params)
}

pub fn mark_post_as_read(post_id: PostId, read: bool) -> Result<PostResponse, reqwest::Error> {
let params = MarkPostAsRead {
post_id,
post_id: Some(post_id),
read,
auth: settings::get_current_account().jwt.unwrap(),
..Default::default()
};
super::post("/post/mark_as_read", &params)
}
3 changes: 0 additions & 3 deletions src/api/posts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ use lemmy_api_common::{
post::{GetPosts, GetPostsResponse},
};

use crate::settings;

pub fn list_posts(
page: i64,
community_name: Option<String>,
Expand All @@ -17,7 +15,6 @@ pub fn list_posts(
type_: listing_type,
sort: sort_type,
community_name,
auth: settings::get_current_account().jwt,
..Default::default()
};

Expand Down
Loading

0 comments on commit c069b15

Please sign in to comment.