3 #[derive(EnumString,ToString,Debug, Serialize, Deserialize)]
4 pub enum PostListingType {
5 All, Subscribed, Community
8 // The faked schema since diesel doesn't do views
13 url -> Nullable<Text>,
14 body -> Nullable<Text>,
19 published -> Timestamp,
20 updated -> Nullable<Timestamp>,
22 creator_name -> Varchar,
23 community_name -> Varchar,
24 community_removed -> Bool,
25 community_deleted -> Bool,
26 number_of_comments -> BigInt,
31 user_id -> Nullable<Int4>,
32 my_vote -> Nullable<Int4>,
33 subscribed -> Nullable<Bool>,
34 read -> Nullable<Bool>,
35 saved -> Nullable<Bool>,
40 #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize,QueryableByName,Clone)]
41 #[table_name="post_view"]
45 pub url: Option<String>,
46 pub body: Option<String>,
48 pub community_id: i32,
51 pub published: chrono::NaiveDateTime,
52 pub updated: Option<chrono::NaiveDateTime>,
54 pub creator_name: String,
55 pub community_name: String,
56 pub community_removed: bool,
57 pub community_deleted: bool,
58 pub number_of_comments: i64,
63 pub user_id: Option<i32>,
64 pub my_vote: Option<i32>,
65 pub subscribed: Option<bool>,
66 pub read: Option<bool>,
67 pub saved: Option<bool>,
71 pub fn list(conn: &PgConnection,
72 type_: PostListingType,
74 for_community_id: Option<i32>,
75 for_creator_id: Option<i32>,
76 search_term: Option<String>,
77 my_user_id: Option<i32>,
82 ) -> Result<Vec<Self>, Error> {
83 use super::post_view::post_view::dsl::*;
85 let (limit, offset) = limit_and_offset(page, limit);
87 let mut query = post_view.into_boxed();
89 if let Some(for_community_id) = for_community_id {
90 query = query.filter(community_id.eq(for_community_id));
93 if let Some(for_creator_id) = for_creator_id {
94 query = query.filter(creator_id.eq(for_creator_id));
97 if let Some(search_term) = search_term {
98 query = query.filter(name.ilike(fuzzy_search(&search_term)));
101 // TODO these are wrong, bc they'll only show saved for your logged in user, not theirs
103 query = query.filter(saved.eq(true));
107 query = query.filter(read.eq(false));
111 PostListingType::Subscribed => {
112 query = query.filter(subscribed.eq(true));
117 // The view lets you pass a null user_id, if you're not logged in
118 if let Some(my_user_id) = my_user_id {
119 query = query.filter(user_id.eq(my_user_id));
121 query = query.filter(user_id.is_null());
125 SortType::Hot => query.order_by(hot_rank.desc()),
126 SortType::New => query.order_by(published.desc()),
127 SortType::TopAll => query.order_by(score.desc()),
128 SortType::TopYear => query
129 .filter(published.gt(now - 1.years()))
130 .order_by(score.desc()),
131 SortType::TopMonth => query
132 .filter(published.gt(now - 1.months()))
133 .order_by(score.desc()),
134 SortType::TopWeek => query
135 .filter(published.gt(now - 1.weeks()))
136 .order_by(score.desc()),
137 SortType::TopDay => query
138 .filter(published.gt(now - 1.days()))
139 .order_by(score.desc())
145 .filter(removed.eq(false))
146 .filter(deleted.eq(false))
147 .filter(community_removed.eq(false))
148 .filter(community_deleted.eq(false));
150 query.load::<Self>(conn)
154 pub fn read(conn: &PgConnection, from_post_id: i32, my_user_id: Option<i32>) -> Result<Self, Error> {
156 use super::post_view::post_view::dsl::*;
157 use diesel::prelude::*;
159 let mut query = post_view.into_boxed();
161 query = query.filter(id.eq(from_post_id));
163 if let Some(my_user_id) = my_user_id {
164 query = query.filter(user_id.eq(my_user_id));
166 query = query.filter(user_id.is_null());
169 query.first::<Self>(conn)
178 use super::super::community::*;
179 use super::super::user::*;
180 use super::super::post::*;
183 let conn = establish_connection();
185 let user_name = "tegan".to_string();
186 let community_name = "test_community_3".to_string();
187 let post_name = "test post 3".to_string();
189 let new_user = UserForm {
190 name: user_name.to_owned(),
191 fedi_name: "rrf".into(),
192 preferred_username: None,
193 password_encrypted: "nope".into(),
200 let inserted_user = User_::create(&conn, &new_user).unwrap();
202 let new_community = CommunityForm {
203 name: community_name.to_owned(),
204 title: "nada".to_owned(),
206 creator_id: inserted_user.id,
213 let inserted_community = Community::create(&conn, &new_community).unwrap();
215 let new_post = PostForm {
216 name: post_name.to_owned(),
219 creator_id: inserted_user.id,
220 community_id: inserted_community.id,
227 let inserted_post = Post::create(&conn, &new_post).unwrap();
229 let post_like_form = PostLikeForm {
230 post_id: inserted_post.id,
231 user_id: inserted_user.id,
235 let inserted_post_like = PostLike::like(&conn, &post_like_form).unwrap();
237 let expected_post_like = PostLike {
238 id: inserted_post_like.id,
239 post_id: inserted_post.id,
240 user_id: inserted_user.id,
241 published: inserted_post_like.published,
245 let post_like_form = PostLikeForm {
246 post_id: inserted_post.id,
247 user_id: inserted_user.id,
251 // the non user version
252 let expected_post_listing_no_user = PostView {
255 id: inserted_post.id,
256 name: post_name.to_owned(),
259 creator_id: inserted_user.id,
260 creator_name: user_name.to_owned(),
261 community_id: inserted_community.id,
265 community_name: community_name.to_owned(),
266 community_removed: false,
267 community_deleted: false,
268 number_of_comments: 0,
273 published: inserted_post.published,
280 let expected_post_listing_with_user = PostView {
281 user_id: Some(inserted_user.id),
283 id: inserted_post.id,
284 name: post_name.to_owned(),
290 creator_id: inserted_user.id,
291 creator_name: user_name.to_owned(),
292 community_id: inserted_community.id,
293 community_name: community_name.to_owned(),
294 community_removed: false,
295 community_deleted: false,
296 number_of_comments: 0,
301 published: inserted_post.published,
309 let read_post_listings_with_user = PostView::list(&conn,
310 PostListingType::Community,
311 &SortType::New, Some(inserted_community.id),
314 Some(inserted_user.id),
319 let read_post_listings_no_user = PostView::list(&conn,
320 PostListingType::Community,
322 Some(inserted_community.id),
330 let read_post_listing_no_user = PostView::read(&conn, inserted_post.id, None).unwrap();
331 let read_post_listing_with_user = PostView::read(&conn, inserted_post.id, Some(inserted_user.id)).unwrap();
333 let like_removed = PostLike::remove(&conn, &post_like_form).unwrap();
334 let num_deleted = Post::delete(&conn, inserted_post.id).unwrap();
335 Community::delete(&conn, inserted_community.id).unwrap();
336 User_::delete(&conn, inserted_user.id).unwrap();
339 assert_eq!(expected_post_listing_with_user, read_post_listings_with_user[0]);
340 assert_eq!(expected_post_listing_with_user, read_post_listing_with_user);
341 assert_eq!(1, read_post_listings_with_user.len());
344 assert_eq!(expected_post_listing_no_user, read_post_listings_no_user[0]);
345 assert_eq!(expected_post_listing_no_user, read_post_listing_no_user);
346 assert_eq!(1, read_post_listings_no_user.len());
348 // assert_eq!(expected_post, inserted_post);
349 // assert_eq!(expected_post, updated_post);
350 assert_eq!(expected_post_like, inserted_post_like);
351 assert_eq!(1, like_removed);
352 assert_eq!(1, num_deleted);