3 #[derive(EnumString, ToString, Debug, Serialize, Deserialize)]
4 pub enum PostListingType {
10 // The faked schema since diesel doesn't do views
15 url -> Nullable<Text>,
16 body -> Nullable<Text>,
21 published -> Timestamp,
22 updated -> Nullable<Timestamp>,
26 banned_from_community -> Bool,
28 creator_name -> Varchar,
29 community_name -> Varchar,
30 community_removed -> Bool,
31 community_deleted -> Bool,
32 community_nsfw -> Bool,
33 number_of_comments -> BigInt,
38 user_id -> Nullable<Int4>,
39 my_vote -> Nullable<Int4>,
40 subscribed -> Nullable<Bool>,
41 read -> Nullable<Bool>,
42 saved -> Nullable<Bool>,
47 Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, QueryableByName, Clone,
49 #[table_name = "post_view"]
53 pub url: Option<String>,
54 pub body: Option<String>,
56 pub community_id: i32,
59 pub published: chrono::NaiveDateTime,
60 pub updated: Option<chrono::NaiveDateTime>,
64 pub banned_from_community: bool,
66 pub creator_name: String,
67 pub community_name: String,
68 pub community_removed: bool,
69 pub community_deleted: bool,
70 pub community_nsfw: bool,
71 pub number_of_comments: i64,
76 pub user_id: Option<i32>,
77 pub my_vote: Option<i32>,
78 pub subscribed: Option<bool>,
79 pub read: Option<bool>,
80 pub saved: Option<bool>,
86 type_: PostListingType,
88 for_community_id: Option<i32>,
89 for_creator_id: Option<i32>,
90 search_term: Option<String>,
91 url_search: Option<String>,
92 my_user_id: Option<i32>,
98 ) -> Result<Vec<Self>, Error> {
99 use super::post_view::post_view::dsl::*;
101 let (limit, offset) = limit_and_offset(page, limit);
103 let mut query = post_view.into_boxed();
105 if let Some(for_creator_id) = for_creator_id {
106 query = query.filter(creator_id.eq(for_creator_id));
109 if let Some(search_term) = search_term {
110 query = query.filter(name.ilike(fuzzy_search(&search_term)));
113 if let Some(url_search) = url_search {
114 query = query.filter(url.eq(url_search));
117 if let Some(for_community_id) = for_community_id {
118 query = query.filter(community_id.eq(for_community_id));
119 query = query.then_order_by(stickied.desc());
122 // TODO these are wrong, bc they'll only show saved for your logged in user, not theirs
124 query = query.filter(saved.eq(true));
128 query = query.filter(read.eq(false));
132 PostListingType::Subscribed => {
133 query = query.filter(subscribed.eq(true));
138 // The view lets you pass a null user_id, if you're not logged in
139 if let Some(my_user_id) = my_user_id {
140 query = query.filter(user_id.eq(my_user_id));
142 query = query.filter(user_id.is_null());
147 .filter(nsfw.eq(false))
148 .filter(community_nsfw.eq(false));
152 SortType::Hot => query
153 .then_order_by(hot_rank.desc())
154 .then_order_by(published.desc()),
155 SortType::New => query.then_order_by(published.desc()),
156 SortType::TopAll => query.then_order_by(score.desc()),
157 SortType::TopYear => query
158 .filter(published.gt(now - 1.years()))
159 .then_order_by(score.desc()),
160 SortType::TopMonth => query
161 .filter(published.gt(now - 1.months()))
162 .then_order_by(score.desc()),
163 SortType::TopWeek => query
164 .filter(published.gt(now - 1.weeks()))
165 .then_order_by(score.desc()),
166 SortType::TopDay => query
167 .filter(published.gt(now - 1.days()))
168 .then_order_by(score.desc()),
174 .filter(removed.eq(false))
175 .filter(deleted.eq(false))
176 .filter(community_removed.eq(false))
177 .filter(community_deleted.eq(false));
179 query.load::<Self>(conn)
185 my_user_id: Option<i32>,
186 ) -> Result<Self, Error> {
187 use super::post_view::post_view::dsl::*;
188 use diesel::prelude::*;
190 let mut query = post_view.into_boxed();
192 query = query.filter(id.eq(from_post_id));
194 if let Some(my_user_id) = my_user_id {
195 query = query.filter(user_id.eq(my_user_id));
197 query = query.filter(user_id.is_null());
200 query.first::<Self>(conn)
206 use super::super::community::*;
207 use super::super::post::*;
208 use super::super::user::*;
212 let conn = establish_connection();
214 let user_name = "tegan".to_string();
215 let community_name = "test_community_3".to_string();
216 let post_name = "test post 3".to_string();
218 let new_user = UserForm {
219 name: user_name.to_owned(),
220 fedi_name: "rrf".into(),
221 preferred_username: None,
222 password_encrypted: "nope".into(),
228 theme: "darkly".into(),
231 let inserted_user = User_::create(&conn, &new_user).unwrap();
233 let new_community = CommunityForm {
234 name: community_name.to_owned(),
235 title: "nada".to_owned(),
237 creator_id: inserted_user.id,
245 let inserted_community = Community::create(&conn, &new_community).unwrap();
247 let new_post = PostForm {
248 name: post_name.to_owned(),
251 creator_id: inserted_user.id,
252 community_id: inserted_community.id,
261 let inserted_post = Post::create(&conn, &new_post).unwrap();
263 let post_like_form = PostLikeForm {
264 post_id: inserted_post.id,
265 user_id: inserted_user.id,
269 let inserted_post_like = PostLike::like(&conn, &post_like_form).unwrap();
271 let expected_post_like = PostLike {
272 id: inserted_post_like.id,
273 post_id: inserted_post.id,
274 user_id: inserted_user.id,
275 published: inserted_post_like.published,
279 let post_like_form = PostLikeForm {
280 post_id: inserted_post.id,
281 user_id: inserted_user.id,
285 // the non user version
286 let expected_post_listing_no_user = PostView {
289 id: inserted_post.id,
290 name: post_name.to_owned(),
293 creator_id: inserted_user.id,
294 creator_name: user_name.to_owned(),
296 banned_from_community: false,
297 community_id: inserted_community.id,
302 community_name: community_name.to_owned(),
303 community_removed: false,
304 community_deleted: false,
305 community_nsfw: false,
306 number_of_comments: 0,
311 published: inserted_post.published,
319 let expected_post_listing_with_user = PostView {
320 user_id: Some(inserted_user.id),
322 id: inserted_post.id,
323 name: post_name.to_owned(),
330 creator_id: inserted_user.id,
331 creator_name: user_name.to_owned(),
333 banned_from_community: false,
334 community_id: inserted_community.id,
335 community_name: community_name.to_owned(),
336 community_removed: false,
337 community_deleted: false,
338 community_nsfw: false,
339 number_of_comments: 0,
344 published: inserted_post.published,
352 let read_post_listings_with_user = PostView::list(
354 PostListingType::Community,
356 Some(inserted_community.id),
360 Some(inserted_user.id),
368 let read_post_listings_no_user = PostView::list(
370 PostListingType::Community,
372 Some(inserted_community.id),
384 let read_post_listing_no_user = PostView::read(&conn, inserted_post.id, None).unwrap();
385 let read_post_listing_with_user =
386 PostView::read(&conn, inserted_post.id, Some(inserted_user.id)).unwrap();
388 let like_removed = PostLike::remove(&conn, &post_like_form).unwrap();
389 let num_deleted = Post::delete(&conn, inserted_post.id).unwrap();
390 Community::delete(&conn, inserted_community.id).unwrap();
391 User_::delete(&conn, inserted_user.id).unwrap();
395 expected_post_listing_with_user,
396 read_post_listings_with_user[0]
398 assert_eq!(expected_post_listing_with_user, read_post_listing_with_user);
399 assert_eq!(1, read_post_listings_with_user.len());
402 assert_eq!(expected_post_listing_no_user, read_post_listings_no_user[0]);
403 assert_eq!(expected_post_listing_no_user, read_post_listing_no_user);
404 assert_eq!(1, read_post_listings_no_user.len());
406 // assert_eq!(expected_post, inserted_post);
407 // assert_eq!(expected_post, updated_post);
408 assert_eq!(expected_post_like, inserted_post_like);
409 assert_eq!(1, like_removed);
410 assert_eq!(1, num_deleted);