3 schema::{post, post_like, post_read, post_saved},
9 use chrono::NaiveDateTime;
10 use diesel::{ExpressionMethods, PgConnection, QueryDsl, RunQueryDsl};
11 use lemmy_apub_lib::traits::ApubObject;
12 use lemmy_utils::LemmyError;
13 use serde::{Deserialize, Serialize};
16 #[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
17 #[table_name = "post"]
21 pub url: Option<DbUrl>,
22 pub body: Option<String>,
23 pub creator_id: PersonId,
24 pub community_id: CommunityId,
27 pub published: chrono::NaiveDateTime,
28 pub updated: Option<chrono::NaiveDateTime>,
32 pub embed_title: Option<String>,
33 pub embed_description: Option<String>,
34 pub embed_html: Option<String>,
35 pub thumbnail_url: Option<DbUrl>,
40 #[derive(Insertable, AsChangeset, Default)]
41 #[table_name = "post"]
44 pub creator_id: PersonId,
45 pub community_id: CommunityId,
46 pub nsfw: Option<bool>,
47 pub url: Option<DbUrl>,
48 pub body: Option<String>,
49 pub removed: Option<bool>,
50 pub locked: Option<bool>,
51 pub published: Option<chrono::NaiveDateTime>,
52 pub updated: Option<chrono::NaiveDateTime>,
53 pub deleted: Option<bool>,
54 pub stickied: Option<bool>,
55 pub embed_title: Option<String>,
56 pub embed_description: Option<String>,
57 pub embed_html: Option<String>,
58 pub thumbnail_url: Option<DbUrl>,
59 pub ap_id: Option<DbUrl>,
60 pub local: Option<bool>,
63 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
65 #[table_name = "post_like"]
69 pub person_id: PersonId,
71 pub published: chrono::NaiveDateTime,
74 #[derive(Insertable, AsChangeset, Clone)]
75 #[table_name = "post_like"]
76 pub struct PostLikeForm {
78 pub person_id: PersonId,
82 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
84 #[table_name = "post_saved"]
85 pub struct PostSaved {
88 pub person_id: PersonId,
89 pub published: chrono::NaiveDateTime,
92 #[derive(Insertable, AsChangeset)]
93 #[table_name = "post_saved"]
94 pub struct PostSavedForm {
96 pub person_id: PersonId,
99 #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
101 #[table_name = "post_read"]
102 pub struct PostRead {
105 pub person_id: PersonId,
106 pub published: chrono::NaiveDateTime,
109 #[derive(Insertable, AsChangeset)]
110 #[table_name = "post_read"]
111 pub struct PostReadForm {
113 pub person_id: PersonId,
116 impl ApubObject for Post {
117 type DataType = PgConnection;
119 fn last_refreshed_at(&self) -> Option<NaiveDateTime> {
123 fn read_from_apub_id(conn: &PgConnection, object_id: Url) -> Result<Option<Self>, LemmyError> {
124 use crate::schema::post::dsl::*;
125 let object_id: DbUrl = object_id.into();
126 Ok(post.filter(ap_id.eq(object_id)).first::<Self>(conn).ok())
129 fn delete(self, conn: &PgConnection) -> Result<(), LemmyError> {
130 use crate::schema::post::dsl::*;
131 diesel::update(post.find(self.id))
132 .set((deleted.eq(true), updated.eq(naive_now())))
133 .get_result::<Self>(conn)?;