feat: migrate to lemmy v0.19 (closes #33)

This commit is contained in:
Bnyro
2024-01-06 11:30:55 +01:00
parent 7f8d62cc98
commit c069b15cd8
23 changed files with 221 additions and 3409 deletions
Generated
+140 -896
View File
File diff suppressed because it is too large Load Diff
+1 -1
View File
@@ -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"
+3 -20
View File
@@ -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,
@@ -17,7 +15,6 @@ pub fn create_comment(
post_id,
content,
parent_id,
auth: settings::get_current_account().jwt.unwrap(),
..Default::default()
};
super::post("/comment", &params)
@@ -25,11 +22,7 @@ pub fn create_comment(
// 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)
}
@@ -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)
@@ -50,17 +42,12 @@ 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)
}
@@ -68,10 +55,6 @@ 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
View File
@@ -5,7 +5,6 @@ use lemmy_api_common::{
};
use super::search;
use crate::settings;
pub fn fetch_communities(
page: i64,
@@ -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()
};
-5
View File
@@ -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()
};
@@ -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)
}
@@ -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)
+11 -15
View File
@@ -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>()?;
@@ -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![]),
}
}
+37 -5
View File
@@ -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;
@@ -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);
@@ -37,12 +40,31 @@ 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>
@@ -50,7 +72,12 @@ 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>
@@ -58,5 +85,10 @@ 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()
}
+2 -13
View File
@@ -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)
}
+5 -23
View File
@@ -1,4 +1,3 @@
use crate::settings;
use itertools::Itertools;
use lemmy_api_common::{
comment::{GetComments, GetCommentsResponse},
@@ -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)
@@ -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()
};
@@ -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)
@@ -87,7 +83,6 @@ 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)
@@ -95,11 +90,7 @@ pub fn edit_post(
// 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)
}
@@ -107,34 +98,25 @@ 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
View File
@@ -4,8 +4,6 @@ use lemmy_api_common::{
post::{GetPosts, GetPostsResponse},
};
use crate::settings;
pub fn list_posts(
page: i64,
community_name: Option<String>,
@@ -17,7 +15,6 @@ pub fn list_posts(
type_: listing_type,
sort: sort_type,
community_name,
auth: settings::get_current_account().jwt,
..Default::default()
};
-3
View File
@@ -11,7 +11,6 @@ pub fn create_private_message(
recipient_id: PersonId,
) -> std::result::Result<PrivateMessageResponse, reqwest::Error> {
let params = CreatePrivateMessage {
auth: crate::settings::get_current_account().jwt.unwrap(),
recipient_id,
content,
};
@@ -23,7 +22,6 @@ pub fn edit_private_message(
private_message_id: PrivateMessageId,
) -> std::result::Result<PrivateMessageResponse, reqwest::Error> {
let params = EditPrivateMessage {
auth: crate::settings::get_current_account().jwt.unwrap(),
private_message_id,
content,
};
@@ -37,7 +35,6 @@ pub fn list_private_messages(
let params = GetPrivateMessages {
unread_only: Some(unread_only),
page: Some(page),
auth: crate::settings::get_current_account().jwt.unwrap(),
..Default::default()
};
super::get("/private_message/list", &params)
-3
View File
@@ -3,8 +3,6 @@ use lemmy_api_common::{
site::{Search, SearchResponse},
};
use crate::settings;
pub fn fetch_search(
page: i64,
query: String,
@@ -15,7 +13,6 @@ pub fn fetch_search(
sort: Some(SortType::TopMonth),
page: Some(page),
type_: search_type,
auth: settings::get_current_account().jwt,
..Default::default()
};
+2 -7
View File
@@ -1,12 +1,7 @@
use lemmy_api_common::site::{GetSite, GetSiteResponse};
use crate::settings;
use lemmy_api_common::site::GetSiteResponse;
pub fn fetch_site() -> std::result::Result<GetSiteResponse, reqwest::Error> {
let params = GetSite {
auth: settings::get_current_account().jwt,
};
super::get("/site", &params)
super::get("/site", &())
}
pub fn default_site_info() -> GetSiteResponse {
+2 -15
View File
@@ -3,12 +3,9 @@ use lemmy_api_common::{
person::{
BlockPerson, BlockPersonResponse, GetPersonDetails, GetPersonDetailsResponse,
GetPersonMentions, GetPersonMentionsResponse, GetReplies, GetRepliesResponse,
MarkAllAsRead,
},
};
use crate::settings;
pub fn get_user(
id: PersonId,
page: i64,
@@ -17,7 +14,6 @@ pub fn get_user(
let params = GetPersonDetails {
page: Some(page),
person_id: Some(id),
auth: settings::get_current_account().jwt,
saved_only: Some(saved_only),
..Default::default()
};
@@ -29,11 +25,7 @@ pub fn block_user(
person_id: PersonId,
block: bool,
) -> std::result::Result<BlockPersonResponse, reqwest::Error> {
let params = BlockPerson {
person_id,
block,
auth: settings::get_current_account().jwt.unwrap(),
};
let params = BlockPerson { person_id, block };
super::post("/user/block", &params)
}
@@ -47,7 +39,6 @@ pub fn get_mentions(
unread_only: bool,
) -> std::result::Result<GetPersonMentionsResponse, reqwest::Error> {
let params = GetPersonMentions {
auth: settings::get_current_account().jwt.unwrap(),
unread_only: Some(unread_only),
page: Some(page),
sort: Some(CommentSortType::New),
@@ -61,7 +52,6 @@ pub fn get_replies(
unread_only: bool,
) -> std::result::Result<GetRepliesResponse, reqwest::Error> {
let params = GetReplies {
auth: settings::get_current_account().jwt.unwrap(),
page: Some(page),
unread_only: Some(unread_only),
sort: Some(CommentSortType::New),
@@ -71,8 +61,5 @@ pub fn get_replies(
}
pub fn mark_all_as_read() -> std::result::Result<GetRepliesResponse, reqwest::Error> {
let params = MarkAllAsRead {
auth: settings::get_current_account().jwt.unwrap(),
};
super::post("/user/mark_all_as_read", &params)
super::post("/user/mark_all_as_read", &())
}
+3 -3
View File
@@ -1,5 +1,5 @@
use gtk::prelude::*;
use lemmy_api_common::lemmy_db_schema::source::instance::Instance;
use lemmy_api_common::site::InstanceWithFederationState;
use relm4::{factory::FactoryVecDeque, prelude::*};
use crate::{api, settings};
@@ -14,7 +14,7 @@ pub struct InstancesPage {
#[derive(Debug)]
pub enum InstancesPageInput {
FetchInstances,
DoneFetchInstances(Vec<Instance>),
DoneFetchInstances(Vec<InstanceWithFederationState>),
SetInstance(String),
}
@@ -133,7 +133,7 @@ impl SimpleComponent for InstancesPage {
InstancesPageInput::DoneFetchInstances(instances) => {
self.instances.guard().clear();
for instance in instances {
self.instances.guard().push_back(instance);
self.instances.guard().push_back(instance.instance);
}
}
InstancesPageInput::SetInstance(instance_url) => {
+1
View File
@@ -102,6 +102,7 @@ impl SimpleComponent for LoginPage {
let mut account = settings::get_current_account();
account.jwt = Some(token);
settings::update_current_account(account.clone());
if let Ok(site) = api::site::fetch_site() {
let user = site.my_user.unwrap().local_user_view.person;
account.name = user.name;
-4
View File
@@ -15,8 +15,6 @@ pub struct VotingStats {
downvotes: i64,
score: i64,
own_vote: Option<i16>,
#[allow(dead_code)]
id: i32,
post_id: Option<i32>,
comment_id: Option<i32>,
}
@@ -28,7 +26,6 @@ impl VotingStats {
downvotes: counts.downvotes,
own_vote: my_vote,
post_id: Some(counts.post_id.0),
id: counts.id,
score: counts.score,
comment_id: None,
}
@@ -40,7 +37,6 @@ impl VotingStats {
downvotes: counts.downvotes,
own_vote: my_vote,
post_id: None,
id: counts.id,
score: counts.score,
comment_id: Some(counts.comment_id.0),
}
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+1 -854
View File
File diff suppressed because one or more lines are too long
+1 -379
View File
File diff suppressed because one or more lines are too long
+2 -3
View File
@@ -21,9 +21,8 @@ pub fn markdown_to_pango_markup(text: String) -> String {
html2pango::markup_html(&markdown::to_html(&text)).unwrap_or(text)
}
pub fn format_elapsed_time(time: chrono::NaiveDateTime) -> String {
pub fn format_elapsed_time(time: chrono::DateTime<chrono::Utc>) -> String {
let formatter = timeago::Formatter::new();
let current_time = chrono::Utc::now();
let published = time.and_utc();
formatter.convert_chrono(published, current_time)
formatter.convert_chrono(time, current_time)
}
+8
View File
@@ -0,0 +1,8 @@
#!/bin/bash
BASE_URL="https://lemmy.ml/api/v3"
curl "$BASE_URL/user?username=kzhe@lemmy.zip" > src/examples/person.json
curl "$BASE_URL/community?name=asklemmy" > src/examples/community.json
curl "$BASE_URL/post?id=10133939" > src/examples/post.json
curl "$BASE_URL/site" > src/examples/site.json