lemmy_db_schema 1.0.0-beta.0

A link aggregator for the fediverse
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
use crate::{
  diesel::{BoolExpressionMethods, OptionalExtension, PgExpressionMethods, SelectableHelper},
  newtypes::{CommunityId, MultiCommunityId},
  source::{
    community::Community,
    multi_community::{
      MultiCommunity,
      MultiCommunityEntry,
      MultiCommunityEntryForm,
      MultiCommunityFollow,
      MultiCommunityFollowForm,
      MultiCommunityInsertForm,
      MultiCommunityUpdateForm,
    },
  },
  traits::ApubActor,
  utils::format_actor_url,
};
use diesel::{
  ExpressionMethods,
  QueryDsl,
  dsl::{delete, exists, insert_into, not},
  select,
  update,
};
use diesel_async::RunQueryDsl;
use lemmy_db_schema_file::{
  PersonId,
  schema::{
    community,
    instance,
    multi_community,
    multi_community_entry,
    multi_community_follow,
    person,
  },
};
use lemmy_diesel_utils::{
  connection::{DbPool, get_conn},
  dburl::DbUrl,
  traits::Crud,
  utils::functions::lower,
};
use lemmy_utils::{
  error::{LemmyErrorExt, LemmyErrorType, LemmyResult},
  settings::structs::Settings,
};
use url::Url;

const MULTI_COMMUNITY_ENTRY_LIMIT: i8 = 50;

impl Crud for MultiCommunity {
  type InsertForm = MultiCommunityInsertForm;
  type UpdateForm = MultiCommunityUpdateForm;
  type IdType = MultiCommunityId;

  async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> LemmyResult<Self> {
    let conn = &mut get_conn(pool).await?;

    insert_into(multi_community::table)
      .values(form)
      .get_result(conn)
      .await
      .with_lemmy_type(LemmyErrorType::CouldntCreate)
  }

  async fn update(
    pool: &mut DbPool<'_>,
    id: MultiCommunityId,
    form: &Self::UpdateForm,
  ) -> LemmyResult<Self> {
    let conn = &mut get_conn(pool).await?;

    update(multi_community::table.find(id))
      .set(form)
      .get_result(conn)
      .await
      .with_lemmy_type(LemmyErrorType::CouldntUpdate)
  }
}

impl MultiCommunity {
  pub async fn upsert(pool: &mut DbPool<'_>, form: &MultiCommunityInsertForm) -> LemmyResult<Self> {
    let conn = &mut get_conn(pool).await?;

    insert_into(multi_community::table)
      .values(form)
      .on_conflict(multi_community::ap_id)
      .do_update()
      .set(form)
      .get_result(conn)
      .await
      .with_lemmy_type(LemmyErrorType::CouldntUpdate)
  }

  pub async fn follow(
    pool: &mut DbPool<'_>,
    form: &MultiCommunityFollowForm,
  ) -> LemmyResult<MultiCommunityFollow> {
    let conn = &mut get_conn(pool).await?;

    insert_into(multi_community_follow::table)
      .values(form)
      .on_conflict((
        multi_community_follow::multi_community_id,
        multi_community_follow::person_id,
      ))
      .do_update()
      .set(form)
      .get_result(conn)
      .await
      .with_lemmy_type(LemmyErrorType::CouldntUpdate)
  }

  pub async fn unfollow(
    pool: &mut DbPool<'_>,
    person_id: PersonId,
    multi_community_id: MultiCommunityId,
  ) -> LemmyResult<()> {
    let conn = &mut get_conn(pool).await?;

    delete(
      multi_community_follow::table
        .filter(multi_community_follow::multi_community_id.eq(multi_community_id))
        .filter(multi_community_follow::person_id.eq(person_id)),
    )
    .execute(conn)
    .await?;

    Ok(())
  }

  pub async fn follower_inboxes(
    pool: &mut DbPool<'_>,
    multi_community_id: MultiCommunityId,
  ) -> LemmyResult<Vec<DbUrl>> {
    let conn = &mut get_conn(pool).await?;

    multi_community_follow::table
      .inner_join(person::table)
      .filter(multi_community_follow::multi_community_id.eq(multi_community_id))
      .select(person::inbox_url)
      .distinct()
      .get_results(conn)
      .await
      .optional()?
      .ok_or(LemmyErrorType::NotFound.into())
  }

  /// Should be called in a transaction together with update() or upsert()
  pub async fn update_entries(
    pool: &mut DbPool<'_>,
    id: MultiCommunityId,
    new_communities: &Vec<CommunityId>,
  ) -> LemmyResult<(Vec<Community>, Vec<Community>, bool)> {
    let conn = &mut get_conn(pool).await?;
    if new_communities.len() >= usize::try_from(MULTI_COMMUNITY_ENTRY_LIMIT)? {
      return Err(LemmyErrorType::MultiCommunityEntryLimitReached.into());
    }

    let removed: Vec<CommunityId> = delete(
      multi_community_entry::table
        .filter(multi_community_entry::multi_community_id.eq(id))
        .filter(multi_community_entry::community_id.ne_all(new_communities)),
    )
    .returning(multi_community_entry::community_id)
    .get_results::<CommunityId>(conn)
    .await?;

    let removed: Vec<Community> = community::table
      .filter(community::id.eq_any(removed))
      .filter(not(community::local))
      .get_results(conn)
      .await?;

    let forms = new_communities
      .iter()
      .map(|community_id| MultiCommunityEntryForm {
        multi_community_id: id,
        community_id: *community_id,
      })
      .collect::<Vec<_>>();

    let added: Vec<_> = insert_into(multi_community_entry::table)
      .values(forms)
      .on_conflict_do_nothing()
      .returning(multi_community_entry::community_id)
      .get_results::<CommunityId>(conn)
      .await?;

    let added: Vec<Community> = community::table
      .filter(community::id.eq_any(added))
      .filter(not(community::local))
      .get_results(conn)
      .await?;

    // check if any local user follows the multi-comm
    let has_local_followers: bool = select(exists(
      multi_community_follow::table
        .inner_join(person::table)
        .inner_join(multi_community::table)
        .filter(person::local),
    ))
    .get_result(conn)
    .await?;

    Ok((added, removed, has_local_followers))
  }

  pub async fn read_community_ap_ids(
    pool: &mut DbPool<'_>,
    multi_name: &str,
  ) -> LemmyResult<Vec<DbUrl>> {
    let conn = &mut get_conn(pool).await?;

    multi_community::table
      .inner_join(multi_community_entry::table.inner_join(community::table))
      .filter(
        community::removed
          .or(community::deleted)
          .is_distinct_from(true),
      )
      .filter(multi_community::name.eq(multi_name))
      .select(community::ap_id)
      .get_results(conn)
      .await
      .with_lemmy_type(LemmyErrorType::NotFound)
  }
}

impl ApubActor for MultiCommunity {
  async fn read_from_apub_id(
    pool: &mut DbPool<'_>,
    object_id: &DbUrl,
  ) -> LemmyResult<Option<Self>> {
    let conn = &mut get_conn(pool).await?;
    multi_community::table
      .filter(lower(multi_community::ap_id).eq(object_id.to_lowercase()))
      .first(conn)
      .await
      .optional()
      .with_lemmy_type(LemmyErrorType::NotFound)
  }

  async fn read_from_name(
    pool: &mut DbPool<'_>,
    name: &str,
    domain: Option<&str>,
    include_deleted: bool,
  ) -> LemmyResult<Option<Self>> {
    let conn = &mut get_conn(pool).await?;
    let mut q = multi_community::table
      .inner_join(instance::table)
      .filter(lower(multi_community::name).eq(name.to_lowercase()))
      .select(MultiCommunity::as_select())
      .into_boxed();
    if !include_deleted {
      q = q.filter(multi_community::deleted.eq(false))
    }
    if let Some(domain) = domain {
      q = q.filter(lower(instance::domain).eq(domain.to_lowercase()))
    } else {
      q = q.filter(multi_community::local.eq(true))
    }
    q.first(conn)
      .await
      .optional()
      .with_lemmy_type(LemmyErrorType::NotFound)
  }

  fn actor_url(&self, settings: &Settings) -> LemmyResult<Url> {
    let domain = self
      .ap_id
      .inner()
      .domain()
      .ok_or(LemmyErrorType::NotFound)?;

    format_actor_url(&self.name, domain, 'm', settings)
  }

  fn generate_local_actor_url(name: &str, settings: &Settings) -> LemmyResult<DbUrl> {
    let domain = settings.get_protocol_and_hostname();
    Ok(Url::parse(&format!("{domain}/m/{name}"))?.into())
  }
}

impl MultiCommunityEntry {
  pub async fn create(pool: &mut DbPool<'_>, form: &MultiCommunityEntryForm) -> LemmyResult<Self> {
    let conn = &mut get_conn(pool).await?;

    insert_into(multi_community_entry::table)
      .values(form)
      .get_result(conn)
      .await
      .with_lemmy_type(LemmyErrorType::CouldntCreate)
  }

  pub async fn delete(pool: &mut DbPool<'_>, form: &MultiCommunityEntryForm) -> LemmyResult<usize> {
    let conn = &mut get_conn(pool).await?;

    delete(
      multi_community_entry::table
        .filter(multi_community_entry::multi_community_id.eq(form.multi_community_id))
        .filter(multi_community_entry::community_id.eq(form.community_id)),
    )
    .execute(conn)
    .await
    .with_lemmy_type(LemmyErrorType::Deleted)
  }

  /// Make sure you aren't trying to insert more communities than the entry limit allows.
  pub async fn check_entry_limit(
    pool: &mut DbPool<'_>,
    multi_community_id: MultiCommunityId,
  ) -> LemmyResult<()> {
    let conn = &mut get_conn(pool).await?;

    let count: i64 = multi_community_entry::table
      .filter(multi_community_entry::multi_community_id.eq(multi_community_id))
      .count()
      .get_result(conn)
      .await?;

    if count >= MULTI_COMMUNITY_ENTRY_LIMIT.into() {
      Err(LemmyErrorType::MultiCommunityEntryLimitReached.into())
    } else {
      Ok(())
    }
  }

  pub async fn community_used_in_multiple(
    pool: &mut DbPool<'_>,
    form: &MultiCommunityEntryForm,
  ) -> LemmyResult<bool> {
    let conn = &mut get_conn(pool).await?;

    select(exists(
      multi_community_entry::table
        .filter(multi_community_entry::multi_community_id.ne(form.multi_community_id))
        .filter(multi_community_entry::community_id.eq(form.community_id)),
    ))
    .get_result(conn)
    .await
    .with_lemmy_type(LemmyErrorType::NotFound)
  }

  pub async fn list_community_ids(
    pool: &mut DbPool<'_>,
    id: MultiCommunityId,
  ) -> LemmyResult<Vec<CommunityId>> {
    let conn = &mut get_conn(pool).await?;

    multi_community_entry::table
      .filter(multi_community_entry::multi_community_id.eq(id))
      .select(multi_community_entry::community_id)
      .get_results(conn)
      .await
      .with_lemmy_type(LemmyErrorType::NotFound)
  }
}

#[cfg(test)]
mod tests {
  use super::*;
  use crate::source::{
    community::{Community, CommunityInsertForm},
    instance::Instance,
    multi_community::{MultiCommunity, MultiCommunityInsertForm},
    person::{Person, PersonInsertForm},
  };
  use lemmy_db_schema_file::enums::CommunityFollowerState;
  use lemmy_diesel_utils::{connection::build_db_pool_for_tests, traits::Crud};
  use lemmy_utils::error::LemmyResult;
  use pretty_assertions::assert_eq;
  use serial_test::serial;

  struct Data {
    multi: MultiCommunity,
    instance: Instance,
    community: Community,
    person: Person,
  }

  async fn setup(pool: &mut DbPool<'_>) -> LemmyResult<Data> {
    let instance = Instance::read_or_create(pool, "my_domain.tld").await?;

    let form = PersonInsertForm::test_form(instance.id, "bobby");
    let person = Person::create(pool, &form).await?;

    let form = CommunityInsertForm::new(
      instance.id,
      "TIL".into(),
      "nada".to_owned(),
      "pubkey".to_string(),
    );
    let community = Community::create(pool, &form).await?;

    let form =
      MultiCommunityInsertForm::new(person.id, instance.id, "multi".to_string(), String::new());
    let multi = MultiCommunity::create(pool, &form).await?;
    assert_eq!(form.creator_id, multi.creator_id);
    assert_eq!(form.name, multi.name);

    Ok(Data {
      multi,
      instance,
      community,
      person,
    })
  }

  #[tokio::test]
  #[serial]
  async fn test_counts() -> LemmyResult<()> {
    let pool = &build_db_pool_for_tests();
    let pool = &mut pool.into();
    let data = setup(pool).await?;

    // Make sure there are no counts in the current multi.
    assert_eq!(0, data.multi.subscribers);
    assert_eq!(0, data.multi.subscribers_local);
    assert_eq!(0, data.multi.communities);

    // Insert a community entry
    let entry_form = MultiCommunityEntryForm {
      multi_community_id: data.multi.id,
      community_id: data.community.id,
    };
    MultiCommunityEntry::create(pool, &entry_form).await?;

    let after_entry_insert = MultiCommunity::read(pool, data.multi.id).await?;
    assert_eq!(1, after_entry_insert.communities);

    MultiCommunityEntry::delete(pool, &entry_form).await?;
    let after_entry_delete = MultiCommunity::read(pool, data.multi.id).await?;
    assert_eq!(0, after_entry_delete.communities);

    let pending_follow_form = MultiCommunityFollowForm {
      multi_community_id: data.multi.id,
      person_id: data.person.id,
      follow_state: CommunityFollowerState::Pending,
    };
    MultiCommunity::follow(pool, &pending_follow_form).await?;
    let after_pending_follow = MultiCommunity::read(pool, data.multi.id).await?;
    // Should be 0, since its a pending follow, not approved
    assert_eq!(0, after_pending_follow.subscribers);
    assert_eq!(0, after_pending_follow.subscribers_local);

    // Unfollow (deletes the row), the count should not decrement
    MultiCommunity::unfollow(pool, data.person.id, data.multi.id).await?;
    let after_unfollow = MultiCommunity::read(pool, data.multi.id).await?;
    assert_eq!(0, after_unfollow.subscribers);
    assert_eq!(0, after_unfollow.subscribers_local);

    let accepted_follow_form = MultiCommunityFollowForm {
      multi_community_id: data.multi.id,
      person_id: data.person.id,
      follow_state: CommunityFollowerState::Accepted,
    };
    MultiCommunity::follow(pool, &accepted_follow_form).await?;
    let after_accepted_follow = MultiCommunity::read(pool, data.multi.id).await?;
    assert_eq!(1, after_accepted_follow.subscribers);
    assert_eq!(1, after_accepted_follow.subscribers_local);

    Instance::delete(pool, data.instance.id).await?;

    Ok(())
  }

  #[tokio::test]
  #[serial]
  async fn test_multi_community_apub() -> LemmyResult<()> {
    let pool = &build_db_pool_for_tests();
    let pool = &mut pool.into();
    let data = setup(pool).await?;

    let multi_read_apub_empty =
      MultiCommunity::read_community_ap_ids(pool, &data.multi.name).await?;
    assert!(multi_read_apub_empty.is_empty());

    let multi_entries = vec![data.community.id];
    MultiCommunity::update_entries(pool, data.multi.id, &multi_entries).await?;

    let multi_read_apub = MultiCommunity::read_community_ap_ids(pool, &data.multi.name).await?;
    assert_eq!(vec![data.community.ap_id], multi_read_apub);

    Instance::delete(pool, data.instance.id).await?;

    Ok(())
  }
}