~tmpod/remmy

794d70b411e51a5d38b9a1cad9844c6a7f409ca7 — tmpod 1 year, 4 months ago 5b24de9 task/move-to-lemmy-crates
Move to models from lemmy crates
M src/aggregates/comment.rs => src/aggregates/comment.rs +0 -14
@@ 13,17 13,3 @@

// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

use serde::Deserialize;

use crate::types::{AggregateCount, AggregateId, CommentId};

#[derive(Debug, Deserialize)]
pub struct CommentAggregates {
    pub id: AggregateId,
    pub comment_id: CommentId,
    pub score: AggregateCount,
    pub upvotes: AggregateCount,
    pub downvotes: AggregateCount,
    pub published: chrono::NaiveDateTime,
}

M src/aggregates/community.rs => src/aggregates/community.rs +0 -18
@@ 13,21 13,3 @@

// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

use serde::Deserialize;

use crate::types::{AggregateCount, AggregateId, CommunityId};

#[derive(Debug, Deserialize)]
pub struct CommunityAggregates {
    pub id: AggregateId,
    pub community_id: CommunityId,
    pub subscribers: AggregateCount,
    pub posts: AggregateCount,
    pub comments: AggregateCount,
    pub published: chrono::NaiveDateTime,
    pub users_active_day: AggregateCount,
    pub users_active_week: AggregateCount,
    pub users_active_month: AggregateCount,
    pub users_active_half_year: AggregateCount,
}

M src/aggregates/post.rs => src/aggregates/post.rs +0 -18
@@ 13,21 13,3 @@

// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

use serde::Deserialize;

use crate::types::{AggregateCount, AggregateId, PostId};

#[derive(Debug, Deserialize)]
pub struct PostAggregates {
    pub id: AggregateId,
    pub post_id: PostId,
    pub comments: AggregateCount,
    pub score: AggregateCount,
    pub upvotes: AggregateCount,
    pub downvotes: AggregateCount,
    pub stickied: bool,
    pub published: chrono::NaiveDateTime,
    pub newest_comment_time_necro: chrono::NaiveDateTime,
    pub newest_comment_time: chrono::NaiveDateTime,
}

M src/endpoints/comment.rs => src/endpoints/comment.rs +1 -20
@@ 14,23 14,4 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

use serde::{Serialize, Deserialize};

use crate::types::{CommentId, JWTAuth, LocalUserId, PostId};
use crate::views::comment::CommentView;

#[derive(Debug, Serialize)]
pub struct CreateCommentRequest {
    pub content: String,
    pub post_id: PostId,
    pub parent_id: Option<CommentId>,
    pub form_id: Option<String>,
    pub auth: JWTAuth,
}

#[derive(Debug, Deserialize)]
pub struct CreateCommentResponse {
    pub comment_view: CommentView,
    pub recipient_ids: Vec<LocalUserId>,
    pub form_id: Option<String>,
}
pub use lemmy_api_common::comment::*;

M src/endpoints/community.rs => src/endpoints/community.rs +1 -19
@@ 14,22 14,4 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

use serde::{Deserialize, Serialize};

use crate::types::{CommunityId, OnlineCount};
use crate::types::JWTAuth;
use crate::views::community::{CommunityModeratorView, CommunityView};

#[derive(Debug, Serialize)]
pub struct GetCommunityRequest {
    pub id: Option<CommunityId>,
    pub name: Option<String>,
    pub auth: Option<JWTAuth>,
}

#[derive(Debug, Deserialize)]
pub struct GetCommunityResponse {
    pub community_view: CommunityView,
    pub moderators: Vec<CommunityModeratorView>,
    pub online: OnlineCount,
}
pub use lemmy_api_common::community::*;

M src/endpoints/person.rs => src/endpoints/person.rs +1 -14
@@ 14,17 14,4 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

use serde::{Deserialize, Serialize};

use crate::types::JWTAuth;

#[derive(Debug, Serialize)]
pub struct LoginRequest {
    pub username_or_email: String,
    pub password: String,
}

#[derive(Debug, Deserialize)]
pub struct LoginResponse {
    pub jwt: JWTAuth,
}
pub use lemmy_api_common::person::*;

M src/endpoints/post.rs => src/endpoints/post.rs +2 -36
@@ 14,39 14,5 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

use serde::{Deserialize, Serialize};

use crate::types::{CommunityId, JWTAuth, PostId, OnlineCount};
use crate::views::{comment::CommentView, community::{CommunityView, CommunityModeratorView}, post::PostView};

#[derive(Debug, Serialize)]
pub struct CreatePostRequest {
    pub name: String,
    pub url: Option<String>,
    pub body: Option<String>,
    pub nsfw: Option<bool>,
    pub community_id: CommunityId,
    pub auth: JWTAuth,
    pub honeypot: Option<String>,
}

#[derive(Debug, Deserialize)]
pub struct CreatePostResponse {
    pub post_view: PostView,
}


#[derive(Debug, Serialize)]
pub struct GetPostRequest {
    pub id: PostId,
    pub auth: Option<JWTAuth>,
}

#[derive(Debug, Deserialize)]
pub struct GetPostResponse {
  pub post_view: PostView,
  pub community_view: CommunityView,
  pub comments: Vec<CommentView>,
  pub moderators: Vec<CommunityModeratorView>,
  pub online: OnlineCount,
}
pub use lemmy_api_common::post::PostResponse as CreatePostResponse;
pub use lemmy_api_common::post::*;

M src/entities/comment.rs => src/entities/comment.rs +0 -21
@@ 13,24 13,3 @@

// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

use serde::Deserialize;

use crate::types::{CommentId, PersonId, PostId};

#[derive(Debug, Deserialize)]
pub struct Comment {
    pub id: CommentId,
    pub creator_id: PersonId,
    pub post_id: PostId,
    pub parent_id: Option<CommentId>,
    pub content: String,
    pub removed: bool,
    pub read: bool,
    pub published: chrono::NaiveDateTime,
    pub updated: Option<chrono::NaiveDateTime>,
    pub deleted: bool,
    pub ap_id: url::Url,
    pub local: bool,
}


M src/entities/community.rs => src/entities/community.rs +0 -21
@@ 13,24 13,3 @@

// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

use serde::Deserialize;

use crate::types::CommunityId;

#[derive(Debug, Deserialize)]
pub struct Community {
    pub id: CommunityId,
    pub name: String,
    pub title: String,
    pub description: Option<String>,
    pub removed: bool,
    pub published: chrono::NaiveDateTime,
    pub updated: Option<chrono::NaiveDateTime>,
    pub deleted: bool,
    pub nsfw: bool,
    pub actor_id: url::Url,
    pub local: bool,
    pub icon: Option<url::Url>,
    pub banner: Option<url::Url>,
}

M src/entities/person.rs => src/entities/person.rs +1 -24
@@ 14,27 14,4 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

use serde::Deserialize;

use crate::types::PersonId;

#[derive(Debug, Deserialize)]
pub struct Person {
    pub id: PersonId,
    pub name: String,
    pub display_name: Option<String>,
    pub avatar: Option<url::Url>,
    pub banned: bool,
    pub published: chrono::NaiveDateTime,
    pub updated: Option<chrono::NaiveDateTime>,
    pub actor_id: url::Url,
    pub bio: Option<String>,
    pub local: bool,
    pub banner: Option<url::Url>,
    pub deleted: bool,
    pub inbox_url: url::Url,
    pub shared_inbox_url: Option<url::Url>,
    pub matrix_user_id: Option<String>,
    pub admin: bool,
    pub bot_account: bool,
}
pub use lemmy_api_common::person::*;

M src/entities/post.rs => src/entities/post.rs +0 -27
@@ 13,30 13,3 @@

// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

use serde::Deserialize;

use crate::types::{CommunityId, PersonId, PostId};

#[derive(Debug, Deserialize)]
pub struct Post {
    pub id: PostId,
    pub name: String,
    pub url: Option<url::Url>,
    pub body: Option<String>,
    pub creator_id: PersonId,
    pub community_id: CommunityId,
    pub removed: bool,
    pub locked: bool,
    pub published: chrono::NaiveDateTime,
    pub updated: Option<chrono::NaiveDateTime>,
    pub deleted: bool,
    pub nsfw: bool,
    pub stickied: bool,
    pub embed_title: Option<String>,
    pub embed_description: Option<String>,
    pub embed_html: Option<String>,
    pub thumbnail_url: Option<url::Url>,
    pub ap_id: url::Url,
    pub local: bool,
}

M src/http.rs => src/http.rs +21 -18
@@ 15,7 15,7 @@
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

use reqwest::{Client, Method, Url};
use serde::{Serialize, de::DeserializeOwned}; 
use serde::{de::DeserializeOwned, Serialize};

use crate::endpoints::*;



@@ 26,10 26,14 @@ pub struct HttpClient {
    http: Client,
}

// TODO: investigate if there's a way to easily split this into multiple parts, to avoid having a
// massive block with every single API endpoint.
impl HttpClient {
    pub fn new(url: Url) -> Self {
        let mut url = url.clone();
        url.path_segments_mut().expect("URL can't be base").extend(&["api", API_VERSION]);
        url.path_segments_mut()
            .expect("URL can't be base")
            .extend(&["api", API_VERSION]);

        Self {
            api_base_url: url,


@@ 57,8 61,7 @@ impl HttpClient {
        let mut url = self.api_base_url.clone();
        url.path_segments_mut().unwrap().extend(endpoint);

        let mut builder = self.http
            .request(method.clone(), url);
        let mut builder = self.http.request(method.clone(), url);

        builder = match method {
            Method::GET => builder.query(&form),


@@ 74,12 77,7 @@ impl HttpClient {
            .await
    }

    async fn request<M, R, E>(
        &self,
        method: Method,
        endpoint: E,
        form: M,
    ) -> reqwest::Result<R>
    async fn request<M, R, E>(&self, method: Method, endpoint: E, form: M) -> reqwest::Result<R>
    where
        M: Serialize,
        R: DeserializeOwned,


@@ 89,10 87,9 @@ impl HttpClient {
        let mut url = self.api_base_url.clone();
        url.path_segments_mut().unwrap().extend(endpoint);

        let mut builder = self.http
            .request(method.clone(), url);
        let mut builder = self.http.request(method.clone(), url);

        // This is what lemmy-ui does. 
        // This is what lemmy-ui does.
        // See https://github.com/LemmyNet/lemmy-js-client/blob/e88a992d25c7c9af85124df03dca53d0e6cd6a29/src/http.ts#L656
        builder = match method {
            Method::GET => builder.query(&form),


@@ 108,23 105,29 @@ impl HttpClient {
            .await
    }

    pub async fn login(&self, form: person::LoginRequest) -> reqwest::Result<person::LoginResponse> {
    pub async fn login(&self, form: person::Login) -> reqwest::Result<person::LoginResponse> {
        self.request(Method::POST, &["user", "login"], form).await
    }

    pub async fn get_community(&self, form: community::GetCommunityRequest) -> reqwest::Result<community::GetCommunityResponse> {
    pub async fn get_community(
        &self,
        form: community::GetCommunity,
    ) -> reqwest::Result<community::GetCommunityResponse> {
        self.request(Method::GET, &["community"], form).await
    }

    pub async fn get_post(&self, form: post::GetPostRequest) -> reqwest::Result<post::GetPostResponse> {
    pub async fn get_post(&self, form: post::GetPost) -> reqwest::Result<post::GetPostResponse> {
        self.request(Method::GET, &["post"], form).await
    }

    pub async fn create_post(&self, form: post::CreatePostRequest) -> reqwest::Result<post::CreatePostResponse> {
    pub async fn create_post(&self, form: post::CreatePost) -> reqwest::Result<post::PostResponse> {
        self.request(Method::POST, &["post"], form).await
    }

    pub async fn create_comment(&self, form: comment::CreateCommentRequest) -> reqwest::Result<comment::CreateCommentResponse> {
    pub async fn create_comment(
        &self,
        form: comment::CreateComment,
    ) -> reqwest::Result<comment::CommentResponse> {
        self.request(Method::POST, &["comment"], form).await
    }
}

M src/views/comment.rs => src/views/comment.rs +0 -21
@@ 13,24 13,3 @@

// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

use serde::Deserialize;

use crate::entities::{comment::Comment, community::Community, person::Person, post::Post};
use crate::aggregates::comment::CommentAggregates;
use crate::types::VoteCount;

#[derive(Debug, Deserialize)]
pub struct CommentView {
    pub comment: Comment,
    pub creator: Person,
    pub recipient: Option<serde_json::Value>,
    pub post: Post,
    pub community: Community,
    pub counts: CommentAggregates,
    pub creator_banned_from_community: bool,
    pub subscribed: bool,
    pub saved: bool,
    pub creator_blocked: bool,
    pub my_vote: Option<VoteCount>,
}

M src/views/community.rs => src/views/community.rs +0 -19
@@ 13,22 13,3 @@

// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

use serde::Deserialize;

use crate::aggregates::community::CommunityAggregates;
use crate::entities::{community::Community, person::Person};

#[derive(Debug, Deserialize)]
pub struct CommunityView {
    pub community: Community,
    pub subscribed: bool,
    pub blocked: bool,
    pub counts: CommunityAggregates,
}

#[derive(Debug, Deserialize)]
pub struct CommunityModeratorView {
    pub community: Community,
    pub moderator: Person,
}

M src/views/post.rs => src/views/post.rs +0 -20
@@ 13,23 13,3 @@

// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

use serde::Deserialize;

use crate::aggregates::post::PostAggregates;
use crate::entities::{community::Community, person::Person, post::Post};
use crate::types::VoteCount;

#[derive(Debug, Deserialize)]
pub struct PostView {
    pub post: Post,
    pub creator: Person,
    pub community: Community,
    pub creator_banned_from_community: bool,
    pub counts: PostAggregates,
    pub subscribed: bool,
    pub saved: bool,
    pub creator_blocked: bool,
    pub read: bool,
    pub my_vote: Option<VoteCount>,
}