]> Untitled Git - lemmy.git/blob - crates/db_views/src/post_view.rs
Tag posts and comments with language (fixes #440) (#2269)
[lemmy.git] / crates / db_views / src / post_view.rs
1 use crate::structs::PostView;
2 use diesel::{dsl::*, pg::Pg, result::Error, *};
3 use lemmy_db_schema::{
4   aggregates::structs::PostAggregates,
5   newtypes::{CommunityId, DbUrl, LocalUserId, PersonId, PostId},
6   schema::{
7     community,
8     community_block,
9     community_follower,
10     community_person_ban,
11     language,
12     local_user_language,
13     person,
14     person_block,
15     post,
16     post_aggregates,
17     post_like,
18     post_read,
19     post_saved,
20   },
21   source::{
22     community::{Community, CommunityFollower, CommunityPersonBan, CommunitySafe},
23     language::Language,
24     person::{Person, PersonSafe},
25     person_block::PersonBlock,
26     post::{Post, PostRead, PostSaved},
27   },
28   traits::{ToSafe, ViewToVec},
29   utils::{functions::hot_rank, fuzzy_search, limit_and_offset},
30   ListingType,
31   SortType,
32 };
33 use tracing::debug;
34 use typed_builder::TypedBuilder;
35
36 type PostViewTuple = (
37   Post,
38   PersonSafe,
39   CommunitySafe,
40   Option<CommunityPersonBan>,
41   PostAggregates,
42   Option<CommunityFollower>,
43   Option<PostSaved>,
44   Option<PostRead>,
45   Option<PersonBlock>,
46   Option<i16>,
47   Language,
48 );
49
50 impl PostView {
51   pub fn read(
52     conn: &PgConnection,
53     post_id: PostId,
54     my_person_id: Option<PersonId>,
55   ) -> Result<Self, Error> {
56     // The left join below will return None in this case
57     let person_id_join = my_person_id.unwrap_or(PersonId(-1));
58     let (
59       post,
60       creator,
61       community,
62       creator_banned_from_community,
63       counts,
64       follower,
65       saved,
66       read,
67       creator_blocked,
68       post_like,
69       language,
70     ) = post::table
71       .find(post_id)
72       .inner_join(person::table)
73       .inner_join(community::table)
74       .left_join(
75         community_person_ban::table.on(
76           post::community_id
77             .eq(community_person_ban::community_id)
78             .and(community_person_ban::person_id.eq(post::creator_id))
79             .and(
80               community_person_ban::expires
81                 .is_null()
82                 .or(community_person_ban::expires.gt(now)),
83             ),
84         ),
85       )
86       .inner_join(post_aggregates::table)
87       .left_join(
88         community_follower::table.on(
89           post::community_id
90             .eq(community_follower::community_id)
91             .and(community_follower::person_id.eq(person_id_join)),
92         ),
93       )
94       .left_join(
95         post_saved::table.on(
96           post::id
97             .eq(post_saved::post_id)
98             .and(post_saved::person_id.eq(person_id_join)),
99         ),
100       )
101       .left_join(
102         post_read::table.on(
103           post::id
104             .eq(post_read::post_id)
105             .and(post_read::person_id.eq(person_id_join)),
106         ),
107       )
108       .left_join(
109         person_block::table.on(
110           post::creator_id
111             .eq(person_block::target_id)
112             .and(person_block::person_id.eq(person_id_join)),
113         ),
114       )
115       .left_join(
116         post_like::table.on(
117           post::id
118             .eq(post_like::post_id)
119             .and(post_like::person_id.eq(person_id_join)),
120         ),
121       )
122       .inner_join(language::table)
123       .select((
124         post::all_columns,
125         Person::safe_columns_tuple(),
126         Community::safe_columns_tuple(),
127         community_person_ban::all_columns.nullable(),
128         post_aggregates::all_columns,
129         community_follower::all_columns.nullable(),
130         post_saved::all_columns.nullable(),
131         post_read::all_columns.nullable(),
132         person_block::all_columns.nullable(),
133         post_like::score.nullable(),
134         language::all_columns,
135       ))
136       .first::<PostViewTuple>(conn)?;
137
138     // If a person is given, then my_vote, if None, should be 0, not null
139     // Necessary to differentiate between other person's votes
140     let my_vote = if my_person_id.is_some() && post_like.is_none() {
141       Some(0)
142     } else {
143       post_like
144     };
145
146     Ok(PostView {
147       post,
148       creator,
149       community,
150       creator_banned_from_community: creator_banned_from_community.is_some(),
151       counts,
152       subscribed: CommunityFollower::to_subscribed_type(&follower),
153       saved: saved.is_some(),
154       read: read.is_some(),
155       creator_blocked: creator_blocked.is_some(),
156       my_vote,
157       language,
158     })
159   }
160 }
161
162 #[derive(TypedBuilder)]
163 #[builder(field_defaults(default))]
164 pub struct PostQuery<'a> {
165   #[builder(!default)]
166   conn: &'a PgConnection,
167   listing_type: Option<ListingType>,
168   sort: Option<SortType>,
169   creator_id: Option<PersonId>,
170   community_id: Option<CommunityId>,
171   community_actor_id: Option<DbUrl>,
172   my_person_id: Option<PersonId>,
173   my_local_user_id: Option<LocalUserId>,
174   search_term: Option<String>,
175   url_search: Option<String>,
176   show_nsfw: Option<bool>,
177   show_bot_accounts: Option<bool>,
178   show_read_posts: Option<bool>,
179   saved_only: Option<bool>,
180   page: Option<i64>,
181   limit: Option<i64>,
182 }
183
184 impl<'a> PostQuery<'a> {
185   pub fn list(self) -> Result<Vec<PostView>, Error> {
186     use diesel::dsl::*;
187
188     // The left join below will return None in this case
189     let person_id_join = self.my_person_id.unwrap_or(PersonId(-1));
190     let local_user_id_join = self.my_local_user_id.unwrap_or(LocalUserId(-1));
191
192     let mut query = post::table
193       .inner_join(person::table)
194       .inner_join(community::table)
195       .left_join(
196         community_person_ban::table.on(
197           post::community_id
198             .eq(community_person_ban::community_id)
199             .and(community_person_ban::person_id.eq(post::creator_id))
200             .and(
201               community_person_ban::expires
202                 .is_null()
203                 .or(community_person_ban::expires.gt(now)),
204             ),
205         ),
206       )
207       .inner_join(post_aggregates::table)
208       .left_join(
209         community_follower::table.on(
210           post::community_id
211             .eq(community_follower::community_id)
212             .and(community_follower::person_id.eq(person_id_join)),
213         ),
214       )
215       .left_join(
216         post_saved::table.on(
217           post::id
218             .eq(post_saved::post_id)
219             .and(post_saved::person_id.eq(person_id_join)),
220         ),
221       )
222       .left_join(
223         post_read::table.on(
224           post::id
225             .eq(post_read::post_id)
226             .and(post_read::person_id.eq(person_id_join)),
227         ),
228       )
229       .left_join(
230         person_block::table.on(
231           post::creator_id
232             .eq(person_block::target_id)
233             .and(person_block::person_id.eq(person_id_join)),
234         ),
235       )
236       .left_join(
237         community_block::table.on(
238           community::id
239             .eq(community_block::community_id)
240             .and(community_block::person_id.eq(person_id_join)),
241         ),
242       )
243       .left_join(
244         post_like::table.on(
245           post::id
246             .eq(post_like::post_id)
247             .and(post_like::person_id.eq(person_id_join)),
248         ),
249       )
250       .inner_join(language::table)
251       .left_join(
252         local_user_language::table.on(
253           post::language_id
254             .eq(local_user_language::language_id)
255             .and(local_user_language::local_user_id.eq(local_user_id_join)),
256         ),
257       )
258       .select((
259         post::all_columns,
260         Person::safe_columns_tuple(),
261         Community::safe_columns_tuple(),
262         community_person_ban::all_columns.nullable(),
263         post_aggregates::all_columns,
264         community_follower::all_columns.nullable(),
265         post_saved::all_columns.nullable(),
266         post_read::all_columns.nullable(),
267         person_block::all_columns.nullable(),
268         post_like::score.nullable(),
269         language::all_columns,
270       ))
271       .into_boxed();
272
273     if let Some(listing_type) = self.listing_type {
274       match listing_type {
275         ListingType::Subscribed => {
276           query = query.filter(community_follower::person_id.is_not_null())
277         }
278         ListingType::Local => {
279           query = query.filter(community::local.eq(true)).filter(
280             community::hidden
281               .eq(false)
282               .or(community_follower::person_id.eq(person_id_join)),
283           );
284         }
285         ListingType::All => {
286           query = query.filter(
287             community::hidden
288               .eq(false)
289               .or(community_follower::person_id.eq(person_id_join)),
290           )
291         }
292       }
293     }
294
295     if let Some(community_id) = self.community_id {
296       query = query
297         .filter(post::community_id.eq(community_id))
298         .then_order_by(post_aggregates::stickied.desc());
299     }
300
301     if let Some(community_actor_id) = self.community_actor_id {
302       query = query
303         .filter(community::actor_id.eq(community_actor_id))
304         .then_order_by(post_aggregates::stickied.desc());
305     }
306
307     if let Some(url_search) = self.url_search {
308       query = query.filter(post::url.eq(url_search));
309     }
310
311     if let Some(search_term) = self.search_term {
312       let searcher = fuzzy_search(&search_term);
313       query = query.filter(
314         post::name
315           .ilike(searcher.to_owned())
316           .or(post::body.ilike(searcher)),
317       );
318     }
319
320     // If its for a specific person, show the removed / deleted
321     if let Some(creator_id) = self.creator_id {
322       query = query.filter(post::creator_id.eq(creator_id));
323     }
324
325     if !self.show_nsfw.unwrap_or(false) {
326       query = query
327         .filter(post::nsfw.eq(false))
328         .filter(community::nsfw.eq(false));
329     };
330
331     if !self.show_bot_accounts.unwrap_or(true) {
332       query = query.filter(person::bot_account.eq(false));
333     };
334
335     if self.saved_only.unwrap_or(false) {
336       query = query.filter(post_saved::id.is_not_null());
337     }
338     // Only hide the read posts, if the saved_only is false. Otherwise ppl with the hide_read
339     // setting wont be able to see saved posts.
340     else if !self.show_read_posts.unwrap_or(true) {
341       query = query.filter(post_read::id.is_null());
342     }
343
344     // Filter out the rows with missing languages
345     if self.my_local_user_id.is_some() {
346       query = query.filter(local_user_language::id.is_not_null());
347     }
348
349     // Don't show blocked communities or persons
350     if self.my_person_id.is_some() {
351       query = query.filter(community_block::person_id.is_null());
352       query = query.filter(person_block::person_id.is_null());
353     }
354
355     query = match self.sort.unwrap_or(SortType::Hot) {
356       SortType::Active => query
357         .then_order_by(
358           hot_rank(
359             post_aggregates::score,
360             post_aggregates::newest_comment_time_necro,
361           )
362           .desc(),
363         )
364         .then_order_by(post_aggregates::newest_comment_time_necro.desc()),
365       SortType::Hot => query
366         .then_order_by(hot_rank(post_aggregates::score, post_aggregates::published).desc())
367         .then_order_by(post_aggregates::published.desc()),
368       SortType::New => query.then_order_by(post_aggregates::published.desc()),
369       SortType::Old => query.then_order_by(post_aggregates::published.asc()),
370       SortType::NewComments => query.then_order_by(post_aggregates::newest_comment_time.desc()),
371       SortType::MostComments => query
372         .then_order_by(post_aggregates::comments.desc())
373         .then_order_by(post_aggregates::published.desc()),
374       SortType::TopAll => query
375         .then_order_by(post_aggregates::score.desc())
376         .then_order_by(post_aggregates::published.desc()),
377       SortType::TopYear => query
378         .filter(post_aggregates::published.gt(now - 1.years()))
379         .then_order_by(post_aggregates::score.desc())
380         .then_order_by(post_aggregates::published.desc()),
381       SortType::TopMonth => query
382         .filter(post_aggregates::published.gt(now - 1.months()))
383         .then_order_by(post_aggregates::score.desc())
384         .then_order_by(post_aggregates::published.desc()),
385       SortType::TopWeek => query
386         .filter(post_aggregates::published.gt(now - 1.weeks()))
387         .then_order_by(post_aggregates::score.desc())
388         .then_order_by(post_aggregates::published.desc()),
389       SortType::TopDay => query
390         .filter(post_aggregates::published.gt(now - 1.days()))
391         .then_order_by(post_aggregates::score.desc())
392         .then_order_by(post_aggregates::published.desc()),
393     };
394
395     let (limit, offset) = limit_and_offset(self.page, self.limit)?;
396
397     query = query
398       .limit(limit)
399       .offset(offset)
400       .filter(post::removed.eq(false))
401       .filter(post::deleted.eq(false))
402       .filter(community::removed.eq(false))
403       .filter(community::deleted.eq(false));
404
405     debug!("Post View Query: {:?}", debug_query::<Pg, _>(&query));
406
407     let res = query.load::<PostViewTuple>(self.conn)?;
408
409     Ok(PostView::from_tuple_to_vec(res))
410   }
411 }
412
413 impl ViewToVec for PostView {
414   type DbTuple = PostViewTuple;
415   fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
416     items
417       .into_iter()
418       .map(|a| Self {
419         post: a.0,
420         creator: a.1,
421         community: a.2,
422         creator_banned_from_community: a.3.is_some(),
423         counts: a.4,
424         subscribed: CommunityFollower::to_subscribed_type(&a.5),
425         saved: a.6.is_some(),
426         read: a.7.is_some(),
427         creator_blocked: a.8.is_some(),
428         my_vote: a.9,
429         language: a.10,
430       })
431       .collect::<Vec<Self>>()
432   }
433 }
434
435 #[cfg(test)]
436 mod tests {
437   use crate::post_view::{PostQuery, PostView};
438   use diesel::PgConnection;
439   use lemmy_db_schema::{
440     aggregates::structs::PostAggregates,
441     newtypes::LanguageId,
442     source::{
443       community::*,
444       community_block::{CommunityBlock, CommunityBlockForm},
445       language::Language,
446       local_user::{LocalUser, LocalUserForm},
447       local_user_language::LocalUserLanguage,
448       person::*,
449       person_block::{PersonBlock, PersonBlockForm},
450       post::*,
451     },
452     traits::{Blockable, Crud, Likeable},
453     utils::establish_unpooled_connection,
454     SortType,
455     SubscribedType,
456   };
457   use serial_test::serial;
458
459   struct Data {
460     inserted_person: Person,
461     inserted_blocked_person: Person,
462     inserted_bot: Person,
463     inserted_community: Community,
464     inserted_post: Post,
465   }
466
467   fn init_data(conn: &PgConnection) -> Data {
468     let person_name = "tegan".to_string();
469
470     let new_person = PersonForm {
471       name: person_name.to_owned(),
472       public_key: Some("pubkey".to_string()),
473       ..PersonForm::default()
474     };
475
476     let inserted_person = Person::create(conn, &new_person).unwrap();
477
478     let new_bot = PersonForm {
479       name: "mybot".to_string(),
480       bot_account: Some(true),
481       public_key: Some("pubkey".to_string()),
482       ..PersonForm::default()
483     };
484
485     let inserted_bot = Person::create(conn, &new_bot).unwrap();
486
487     let new_community = CommunityForm {
488       name: "test_community_3".to_string(),
489       title: "nada".to_owned(),
490       public_key: Some("pubkey".to_string()),
491       ..CommunityForm::default()
492     };
493
494     let inserted_community = Community::create(conn, &new_community).unwrap();
495
496     // Test a person block, make sure the post query doesn't include their post
497     let blocked_person = PersonForm {
498       name: person_name,
499       public_key: Some("pubkey".to_string()),
500       ..PersonForm::default()
501     };
502
503     let inserted_blocked_person = Person::create(conn, &blocked_person).unwrap();
504
505     let post_from_blocked_person = PostForm {
506       name: "blocked_person_post".to_string(),
507       creator_id: inserted_blocked_person.id,
508       community_id: inserted_community.id,
509       language_id: Some(LanguageId(1)),
510       ..PostForm::default()
511     };
512
513     Post::create(conn, &post_from_blocked_person).unwrap();
514
515     // block that person
516     let person_block = PersonBlockForm {
517       person_id: inserted_person.id,
518       target_id: inserted_blocked_person.id,
519     };
520
521     PersonBlock::block(conn, &person_block).unwrap();
522
523     // A sample post
524     let new_post = PostForm {
525       name: "test post 3".to_string(),
526       creator_id: inserted_person.id,
527       community_id: inserted_community.id,
528       language_id: Some(LanguageId(47)),
529       ..PostForm::default()
530     };
531
532     let inserted_post = Post::create(conn, &new_post).unwrap();
533
534     let new_bot_post = PostForm {
535       name: "test bot post".to_string(),
536       creator_id: inserted_bot.id,
537       community_id: inserted_community.id,
538       ..PostForm::default()
539     };
540
541     let _inserted_bot_post = Post::create(conn, &new_bot_post).unwrap();
542
543     Data {
544       inserted_person,
545       inserted_blocked_person,
546       inserted_bot,
547       inserted_community,
548       inserted_post,
549     }
550   }
551
552   fn cleanup(data: Data, conn: &PgConnection) {
553     let num_deleted = Post::delete(conn, data.inserted_post.id).unwrap();
554     Community::delete(conn, data.inserted_community.id).unwrap();
555     Person::delete(conn, data.inserted_person.id).unwrap();
556     Person::delete(conn, data.inserted_bot.id).unwrap();
557     Person::delete(conn, data.inserted_blocked_person.id).unwrap();
558     assert_eq!(1, num_deleted);
559   }
560
561   fn expected_post_listing(data: &Data, conn: &PgConnection) -> PostView {
562     let (inserted_person, inserted_community, inserted_post) = (
563       &data.inserted_person,
564       &data.inserted_community,
565       &data.inserted_post,
566     );
567     let agg = PostAggregates::read(conn, inserted_post.id).unwrap();
568
569     PostView {
570       post: Post {
571         id: inserted_post.id,
572         name: inserted_post.name.clone(),
573         creator_id: inserted_person.id,
574         url: None,
575         body: None,
576         published: inserted_post.published,
577         updated: None,
578         community_id: inserted_community.id,
579         removed: false,
580         deleted: false,
581         locked: false,
582         stickied: false,
583         nsfw: false,
584         embed_title: None,
585         embed_description: None,
586         embed_video_url: None,
587         thumbnail_url: None,
588         ap_id: inserted_post.ap_id.to_owned(),
589         local: true,
590         language_id: LanguageId(47),
591       },
592       my_vote: None,
593       creator: PersonSafe {
594         id: inserted_person.id,
595         name: inserted_person.name.clone(),
596         display_name: None,
597         published: inserted_person.published,
598         avatar: None,
599         actor_id: inserted_person.actor_id.to_owned(),
600         local: true,
601         admin: false,
602         bot_account: false,
603         banned: false,
604         deleted: false,
605         bio: None,
606         banner: None,
607         updated: None,
608         inbox_url: inserted_person.inbox_url.to_owned(),
609         shared_inbox_url: None,
610         matrix_user_id: None,
611         ban_expires: None,
612       },
613       creator_banned_from_community: false,
614       community: CommunitySafe {
615         id: inserted_community.id,
616         name: inserted_community.name.clone(),
617         icon: None,
618         removed: false,
619         deleted: false,
620         nsfw: false,
621         actor_id: inserted_community.actor_id.to_owned(),
622         local: true,
623         title: "nada".to_owned(),
624         description: None,
625         updated: None,
626         banner: None,
627         hidden: false,
628         posting_restricted_to_mods: false,
629         published: inserted_community.published,
630       },
631       counts: PostAggregates {
632         id: agg.id,
633         post_id: inserted_post.id,
634         comments: 0,
635         score: 0,
636         upvotes: 0,
637         downvotes: 0,
638         stickied: false,
639         published: agg.published,
640         newest_comment_time_necro: inserted_post.published,
641         newest_comment_time: inserted_post.published,
642       },
643       subscribed: SubscribedType::NotSubscribed,
644       read: false,
645       saved: false,
646       creator_blocked: false,
647       language: Language {
648         id: LanguageId(47),
649         code: "fr".to_string(),
650         name: "Français".to_string(),
651       },
652     }
653   }
654
655   #[test]
656   #[serial]
657   fn post_listing_with_person() {
658     let conn = establish_unpooled_connection();
659     let data = init_data(&conn);
660
661     let read_post_listing = PostQuery::builder()
662       .conn(&conn)
663       .sort(Some(SortType::New))
664       .community_id(Some(data.inserted_community.id))
665       .show_bot_accounts(Some(false))
666       .my_person_id(Some(data.inserted_person.id))
667       .build()
668       .list()
669       .unwrap();
670
671     let post_listing_single_with_person =
672       PostView::read(&conn, data.inserted_post.id, Some(data.inserted_person.id)).unwrap();
673
674     let mut expected_post_listing_with_user = expected_post_listing(&data, &conn);
675
676     // Should be only one person, IE the bot post, and blocked should be missing
677     assert_eq!(1, read_post_listing.len());
678
679     assert_eq!(expected_post_listing_with_user, read_post_listing[0]);
680     expected_post_listing_with_user.my_vote = Some(0);
681     assert_eq!(
682       expected_post_listing_with_user,
683       post_listing_single_with_person
684     );
685
686     let post_listings_with_bots = PostQuery::builder()
687       .conn(&conn)
688       .sort(Some(SortType::New))
689       .community_id(Some(data.inserted_community.id))
690       .show_bot_accounts(Some(true))
691       .my_person_id(Some(data.inserted_person.id))
692       .build()
693       .list()
694       .unwrap();
695     // should include bot post which has "undetermined" language
696     assert_eq!(2, post_listings_with_bots.len());
697
698     cleanup(data, &conn);
699   }
700
701   #[test]
702   #[serial]
703   fn post_listing_no_person() {
704     let conn = establish_unpooled_connection();
705     let data = init_data(&conn);
706
707     let read_post_listing_multiple_no_person = PostQuery::builder()
708       .conn(&conn)
709       .sort(Some(SortType::New))
710       .community_id(Some(data.inserted_community.id))
711       .build()
712       .list()
713       .unwrap();
714
715     let read_post_listing_single_no_person =
716       PostView::read(&conn, data.inserted_post.id, None).unwrap();
717
718     let expected_post_listing_no_person = expected_post_listing(&data, &conn);
719
720     // Should be 2 posts, with the bot post, and the blocked
721     assert_eq!(3, read_post_listing_multiple_no_person.len());
722
723     assert_eq!(
724       expected_post_listing_no_person,
725       read_post_listing_multiple_no_person[1]
726     );
727     assert_eq!(
728       expected_post_listing_no_person,
729       read_post_listing_single_no_person
730     );
731
732     cleanup(data, &conn);
733   }
734
735   #[test]
736   #[serial]
737   fn post_listing_block_community() {
738     let conn = establish_unpooled_connection();
739     let data = init_data(&conn);
740
741     let community_block = CommunityBlockForm {
742       person_id: data.inserted_person.id,
743       community_id: data.inserted_community.id,
744     };
745     CommunityBlock::block(&conn, &community_block).unwrap();
746
747     let read_post_listings_with_person_after_block = PostQuery::builder()
748       .conn(&conn)
749       .sort(Some(SortType::New))
750       .community_id(Some(data.inserted_community.id))
751       .show_bot_accounts(Some(true))
752       .my_person_id(Some(data.inserted_person.id))
753       .build()
754       .list()
755       .unwrap();
756     // Should be 0 posts after the community block
757     assert_eq!(0, read_post_listings_with_person_after_block.len());
758
759     CommunityBlock::unblock(&conn, &community_block).unwrap();
760     cleanup(data, &conn);
761   }
762
763   #[test]
764   #[serial]
765   fn post_listing_like() {
766     let conn = establish_unpooled_connection();
767     let data = init_data(&conn);
768
769     let post_like_form = PostLikeForm {
770       post_id: data.inserted_post.id,
771       person_id: data.inserted_person.id,
772       score: 1,
773     };
774
775     let inserted_post_like = PostLike::like(&conn, &post_like_form).unwrap();
776
777     let expected_post_like = PostLike {
778       id: inserted_post_like.id,
779       post_id: data.inserted_post.id,
780       person_id: data.inserted_person.id,
781       published: inserted_post_like.published,
782       score: 1,
783     };
784     assert_eq!(expected_post_like, inserted_post_like);
785
786     let like_removed =
787       PostLike::remove(&conn, data.inserted_person.id, data.inserted_post.id).unwrap();
788     assert_eq!(1, like_removed);
789     cleanup(data, &conn);
790   }
791
792   #[test]
793   #[serial]
794   fn post_listing_person_language() {
795     let conn = establish_unpooled_connection();
796     let data = init_data(&conn);
797
798     let spanish_id = Language::read_id_from_code(&conn, "es").unwrap();
799     let post_spanish = PostForm {
800       name: "asffgdsc".to_string(),
801       creator_id: data.inserted_person.id,
802       community_id: data.inserted_community.id,
803       language_id: Some(spanish_id),
804       ..PostForm::default()
805     };
806
807     Post::create(&conn, &post_spanish).unwrap();
808
809     let my_person_form = PersonForm {
810       name: "Reverie Toiba".to_string(),
811       public_key: Some("pubkey".to_string()),
812       ..PersonForm::default()
813     };
814     let my_person = Person::create(&conn, &my_person_form).unwrap();
815     let local_user_form = LocalUserForm {
816       person_id: Some(my_person.id),
817       password_encrypted: Some("".to_string()),
818       ..Default::default()
819     };
820     let local_user = LocalUser::create(&conn, &local_user_form).unwrap();
821
822     // Update the users languages to all
823     LocalUserLanguage::update_user_languages(&conn, None, local_user.id).unwrap();
824
825     let post_listings_all = PostQuery::builder()
826       .conn(&conn)
827       .sort(Some(SortType::New))
828       .show_bot_accounts(Some(true))
829       .my_person_id(Some(my_person.id))
830       .my_local_user_id(Some(local_user.id))
831       .build()
832       .list()
833       .unwrap();
834
835     // no language filters specified, all posts should be returned
836     assert_eq!(4, post_listings_all.len());
837
838     let french_id = Language::read_id_from_code(&conn, "fr").unwrap();
839     LocalUserLanguage::update_user_languages(&conn, Some(vec![french_id]), local_user.id).unwrap();
840
841     let post_listing_french = PostQuery::builder()
842       .conn(&conn)
843       .sort(Some(SortType::New))
844       .show_bot_accounts(Some(true))
845       .my_person_id(Some(my_person.id))
846       .my_local_user_id(Some(local_user.id))
847       .build()
848       .list()
849       .unwrap();
850
851     // only one french language post should be returned
852     assert_eq!(1, post_listing_french.len());
853     assert_eq!(french_id, post_listing_french[0].post.language_id);
854
855     let undetermined_id = Language::read_id_from_code(&conn, "und").unwrap();
856     LocalUserLanguage::update_user_languages(
857       &conn,
858       Some(vec![french_id, undetermined_id]),
859       local_user.id,
860     )
861     .unwrap();
862     let post_listings_french_und = PostQuery::builder()
863       .conn(&conn)
864       .sort(Some(SortType::New))
865       .show_bot_accounts(Some(true))
866       .my_person_id(Some(my_person.id))
867       .my_local_user_id(Some(local_user.id))
868       .build()
869       .list()
870       .unwrap();
871
872     // french post and undetermined language post should be returned
873     assert_eq!(2, post_listings_french_und.len());
874     assert_eq!(
875       undetermined_id,
876       post_listings_french_und[0].post.language_id
877     );
878     assert_eq!(french_id, post_listings_french_und[1].post.language_id);
879
880     cleanup(data, &conn);
881   }
882 }