]> Untitled Git - lemmy.git/blob - src/code_migrations.rs
fix submodule error
[lemmy.git] / src / code_migrations.rs
1 // This is for db migrations that require code
2 use activitypub_federation::http_signatures::generate_actor_keypair;
3 use diesel::{
4   sql_types::{Nullable, Text},
5   ExpressionMethods,
6   IntoSql,
7   QueryDsl,
8   TextExpressionMethods,
9 };
10 use diesel_async::RunQueryDsl;
11 use lemmy_api_common::{
12   lemmy_db_views::structs::SiteView,
13   utils::{
14     generate_followers_url,
15     generate_inbox_url,
16     generate_local_apub_endpoint,
17     generate_shared_inbox_url,
18     generate_site_inbox_url,
19     EndpointType,
20   },
21 };
22 use lemmy_db_schema::{
23   source::{
24     comment::{Comment, CommentUpdateForm},
25     community::{Community, CommunityUpdateForm},
26     instance::Instance,
27     local_site::{LocalSite, LocalSiteInsertForm},
28     local_site_rate_limit::{LocalSiteRateLimit, LocalSiteRateLimitInsertForm},
29     local_user::{LocalUser, LocalUserInsertForm},
30     person::{Person, PersonInsertForm, PersonUpdateForm},
31     post::{Post, PostUpdateForm},
32     private_message::{PrivateMessage, PrivateMessageUpdateForm},
33     site::{Site, SiteInsertForm, SiteUpdateForm},
34   },
35   traits::Crud,
36   utils::{get_conn, naive_now, DbPool},
37 };
38 use lemmy_utils::{error::LemmyError, settings::structs::Settings};
39 use tracing::info;
40 use url::Url;
41
42 pub async fn run_advanced_migrations(
43   pool: &mut DbPool<'_>,
44   settings: &Settings,
45 ) -> Result<(), LemmyError> {
46   let protocol_and_hostname = &settings.get_protocol_and_hostname();
47   user_updates_2020_04_02(pool, protocol_and_hostname).await?;
48   community_updates_2020_04_02(pool, protocol_and_hostname).await?;
49   post_updates_2020_04_03(pool, protocol_and_hostname).await?;
50   comment_updates_2020_04_03(pool, protocol_and_hostname).await?;
51   private_message_updates_2020_05_05(pool, protocol_and_hostname).await?;
52   post_thumbnail_url_updates_2020_07_27(pool, protocol_and_hostname).await?;
53   apub_columns_2021_02_02(pool).await?;
54   instance_actor_2022_01_28(pool, protocol_and_hostname).await?;
55   regenerate_public_keys_2022_07_05(pool).await?;
56   initialize_local_site_2022_10_10(pool, settings).await?;
57
58   Ok(())
59 }
60
61 async fn user_updates_2020_04_02(
62   pool: &mut DbPool<'_>,
63   protocol_and_hostname: &str,
64 ) -> Result<(), LemmyError> {
65   use lemmy_db_schema::schema::person::dsl::{actor_id, local, person};
66   let conn = &mut get_conn(pool).await?;
67
68   info!("Running user_updates_2020_04_02");
69
70   // Update the actor_id, private_key, and public_key, last_refreshed_at
71   let incorrect_persons = person
72     .filter(actor_id.like("http://changeme%"))
73     .filter(local.eq(true))
74     .load::<Person>(conn)
75     .await?;
76
77   for cperson in &incorrect_persons {
78     let keypair = generate_actor_keypair()?;
79
80     let form = PersonUpdateForm {
81       actor_id: Some(generate_local_apub_endpoint(
82         EndpointType::Person,
83         &cperson.name,
84         protocol_and_hostname,
85       )?),
86       private_key: Some(Some(keypair.private_key)),
87       public_key: Some(keypair.public_key),
88       last_refreshed_at: Some(naive_now()),
89       ..Default::default()
90     };
91
92     Person::update(pool, cperson.id, &form).await?;
93   }
94
95   info!("{} person rows updated.", incorrect_persons.len());
96
97   Ok(())
98 }
99
100 async fn community_updates_2020_04_02(
101   pool: &mut DbPool<'_>,
102   protocol_and_hostname: &str,
103 ) -> Result<(), LemmyError> {
104   use lemmy_db_schema::schema::community::dsl::{actor_id, community, local};
105   let conn = &mut get_conn(pool).await?;
106
107   info!("Running community_updates_2020_04_02");
108
109   // Update the actor_id, private_key, and public_key, last_refreshed_at
110   let incorrect_communities = community
111     .filter(actor_id.like("http://changeme%"))
112     .filter(local.eq(true))
113     .load::<Community>(conn)
114     .await?;
115
116   for ccommunity in &incorrect_communities {
117     let keypair = generate_actor_keypair()?;
118     let community_actor_id = generate_local_apub_endpoint(
119       EndpointType::Community,
120       &ccommunity.name,
121       protocol_and_hostname,
122     )?;
123
124     let form = CommunityUpdateForm {
125       actor_id: Some(community_actor_id.clone()),
126       private_key: Some(Some(keypair.private_key)),
127       public_key: Some(keypair.public_key),
128       last_refreshed_at: Some(naive_now()),
129       ..Default::default()
130     };
131
132     Community::update(pool, ccommunity.id, &form).await?;
133   }
134
135   info!("{} community rows updated.", incorrect_communities.len());
136
137   Ok(())
138 }
139
140 async fn post_updates_2020_04_03(
141   pool: &mut DbPool<'_>,
142   protocol_and_hostname: &str,
143 ) -> Result<(), LemmyError> {
144   use lemmy_db_schema::schema::post::dsl::{ap_id, local, post};
145   let conn = &mut get_conn(pool).await?;
146
147   info!("Running post_updates_2020_04_03");
148
149   // Update the ap_id
150   let incorrect_posts = post
151     .filter(ap_id.like("http://changeme%"))
152     .filter(local.eq(true))
153     .load::<Post>(conn)
154     .await?;
155
156   for cpost in &incorrect_posts {
157     let apub_id = generate_local_apub_endpoint(
158       EndpointType::Post,
159       &cpost.id.to_string(),
160       protocol_and_hostname,
161     )?;
162     Post::update(
163       pool,
164       cpost.id,
165       &PostUpdateForm {
166         ap_id: Some(apub_id),
167         ..Default::default()
168       },
169     )
170     .await?;
171   }
172
173   info!("{} post rows updated.", incorrect_posts.len());
174
175   Ok(())
176 }
177
178 async fn comment_updates_2020_04_03(
179   pool: &mut DbPool<'_>,
180   protocol_and_hostname: &str,
181 ) -> Result<(), LemmyError> {
182   use lemmy_db_schema::schema::comment::dsl::{ap_id, comment, local};
183   let conn = &mut get_conn(pool).await?;
184
185   info!("Running comment_updates_2020_04_03");
186
187   // Update the ap_id
188   let incorrect_comments = comment
189     .filter(ap_id.like("http://changeme%"))
190     .filter(local.eq(true))
191     .load::<Comment>(conn)
192     .await?;
193
194   for ccomment in &incorrect_comments {
195     let apub_id = generate_local_apub_endpoint(
196       EndpointType::Comment,
197       &ccomment.id.to_string(),
198       protocol_and_hostname,
199     )?;
200     Comment::update(
201       pool,
202       ccomment.id,
203       &CommentUpdateForm {
204         ap_id: Some(apub_id),
205         ..Default::default()
206       },
207     )
208     .await?;
209   }
210
211   info!("{} comment rows updated.", incorrect_comments.len());
212
213   Ok(())
214 }
215
216 async fn private_message_updates_2020_05_05(
217   pool: &mut DbPool<'_>,
218   protocol_and_hostname: &str,
219 ) -> Result<(), LemmyError> {
220   use lemmy_db_schema::schema::private_message::dsl::{ap_id, local, private_message};
221   let conn = &mut get_conn(pool).await?;
222
223   info!("Running private_message_updates_2020_05_05");
224
225   // Update the ap_id
226   let incorrect_pms = private_message
227     .filter(ap_id.like("http://changeme%"))
228     .filter(local.eq(true))
229     .load::<PrivateMessage>(conn)
230     .await?;
231
232   for cpm in &incorrect_pms {
233     let apub_id = generate_local_apub_endpoint(
234       EndpointType::PrivateMessage,
235       &cpm.id.to_string(),
236       protocol_and_hostname,
237     )?;
238     PrivateMessage::update(
239       pool,
240       cpm.id,
241       &PrivateMessageUpdateForm {
242         ap_id: Some(apub_id),
243         ..Default::default()
244       },
245     )
246     .await?;
247   }
248
249   info!("{} private message rows updated.", incorrect_pms.len());
250
251   Ok(())
252 }
253
254 async fn post_thumbnail_url_updates_2020_07_27(
255   pool: &mut DbPool<'_>,
256   protocol_and_hostname: &str,
257 ) -> Result<(), LemmyError> {
258   use lemmy_db_schema::schema::post::dsl::{post, thumbnail_url};
259   let conn = &mut get_conn(pool).await?;
260
261   info!("Running post_thumbnail_url_updates_2020_07_27");
262
263   let domain_prefix = format!("{protocol_and_hostname}/pictrs/image/",);
264
265   let incorrect_thumbnails = post.filter(thumbnail_url.not_like("http%"));
266
267   // Prepend the rows with the update
268   let res = diesel::update(incorrect_thumbnails)
269     .set(
270       thumbnail_url.eq(
271         domain_prefix
272           .into_sql::<Nullable<Text>>()
273           .concat(thumbnail_url),
274       ),
275     )
276     .get_results::<Post>(conn)
277     .await?;
278
279   info!("{} Post thumbnail_url rows updated.", res.len());
280
281   Ok(())
282 }
283
284 /// We are setting inbox and follower URLs for local and remote actors alike, because for now
285 /// all federated instances are also Lemmy and use the same URL scheme.
286 async fn apub_columns_2021_02_02(pool: &mut DbPool<'_>) -> Result<(), LemmyError> {
287   let conn = &mut get_conn(pool).await?;
288   info!("Running apub_columns_2021_02_02");
289   {
290     use lemmy_db_schema::schema::person::dsl::{inbox_url, person, shared_inbox_url};
291     let persons = person
292       .filter(inbox_url.like("http://changeme%"))
293       .load::<Person>(conn)
294       .await?;
295
296     for p in &persons {
297       let inbox_url_ = generate_inbox_url(&p.actor_id)?;
298       let shared_inbox_url_ = generate_shared_inbox_url(&p.actor_id)?;
299       diesel::update(person.find(p.id))
300         .set((
301           inbox_url.eq(inbox_url_),
302           shared_inbox_url.eq(shared_inbox_url_),
303         ))
304         .get_result::<Person>(conn)
305         .await?;
306     }
307   }
308
309   {
310     use lemmy_db_schema::schema::community::dsl::{
311       community,
312       followers_url,
313       inbox_url,
314       shared_inbox_url,
315     };
316     let communities = community
317       .filter(inbox_url.like("http://changeme%"))
318       .load::<Community>(conn)
319       .await?;
320
321     for c in &communities {
322       let followers_url_ = generate_followers_url(&c.actor_id)?;
323       let inbox_url_ = generate_inbox_url(&c.actor_id)?;
324       let shared_inbox_url_ = generate_shared_inbox_url(&c.actor_id)?;
325       diesel::update(community.find(c.id))
326         .set((
327           followers_url.eq(followers_url_),
328           inbox_url.eq(inbox_url_),
329           shared_inbox_url.eq(shared_inbox_url_),
330         ))
331         .get_result::<Community>(conn)
332         .await?;
333     }
334   }
335
336   Ok(())
337 }
338
339 /// Site object turns into an actor, so that things like instance description can be federated. This
340 /// means we need to add actor columns to the site table, and initialize them with correct values.
341 /// Before this point, there is only a single value in the site table which refers to the local
342 /// Lemmy instance, so thats all we need to update.
343 async fn instance_actor_2022_01_28(
344   pool: &mut DbPool<'_>,
345   protocol_and_hostname: &str,
346 ) -> Result<(), LemmyError> {
347   info!("Running instance_actor_2021_09_29");
348   if let Ok(site_view) = SiteView::read_local(pool).await {
349     let site = site_view.site;
350     // if site already has public key, we dont need to do anything here
351     if !site.public_key.is_empty() {
352       return Ok(());
353     }
354     let key_pair = generate_actor_keypair()?;
355     let actor_id = Url::parse(protocol_and_hostname)?;
356     let site_form = SiteUpdateForm {
357       actor_id: Some(actor_id.clone().into()),
358       last_refreshed_at: Some(naive_now()),
359       inbox_url: Some(generate_site_inbox_url(&actor_id.into())?),
360       private_key: Some(Some(key_pair.private_key)),
361       public_key: Some(key_pair.public_key),
362       ..Default::default()
363     };
364     Site::update(pool, site.id, &site_form).await?;
365   }
366   Ok(())
367 }
368
369 /// Fix for bug #2347, which can result in community/person public keys being overwritten with
370 /// empty string when the database value is updated. We go through all actors, and if the public
371 /// key field is empty, generate a new keypair. It would be possible to regenerate only the pubkey,
372 /// but thats more complicated and has no benefit, as federation is already broken for these actors.
373 /// https://github.com/LemmyNet/lemmy/issues/2347
374 async fn regenerate_public_keys_2022_07_05(pool: &mut DbPool<'_>) -> Result<(), LemmyError> {
375   let conn = &mut get_conn(pool).await?;
376   info!("Running regenerate_public_keys_2022_07_05");
377
378   {
379     // update communities with empty pubkey
380     use lemmy_db_schema::schema::community::dsl::{community, local, public_key};
381     let communities: Vec<Community> = community
382       .filter(local.eq(true))
383       .filter(public_key.eq(""))
384       .load::<Community>(conn)
385       .await?;
386     for community_ in communities {
387       info!(
388         "local community {} has empty public key field, regenerating key",
389         community_.name
390       );
391       let key_pair = generate_actor_keypair()?;
392       let form = CommunityUpdateForm {
393         public_key: Some(key_pair.public_key),
394         private_key: Some(Some(key_pair.private_key)),
395         ..Default::default()
396       };
397       Community::update(&mut conn.into(), community_.id, &form).await?;
398     }
399   }
400
401   {
402     // update persons with empty pubkey
403     use lemmy_db_schema::schema::person::dsl::{local, person, public_key};
404     let persons = person
405       .filter(local.eq(true))
406       .filter(public_key.eq(""))
407       .load::<Person>(conn)
408       .await?;
409     for person_ in persons {
410       info!(
411         "local user {} has empty public key field, regenerating key",
412         person_.name
413       );
414       let key_pair = generate_actor_keypair()?;
415       let form = PersonUpdateForm {
416         public_key: Some(key_pair.public_key),
417         private_key: Some(Some(key_pair.private_key)),
418         ..Default::default()
419       };
420       Person::update(pool, person_.id, &form).await?;
421     }
422   }
423   Ok(())
424 }
425
426 /// This ensures that your local site is initialized and exists.
427 ///
428 /// If a site already exists, the DB migration should generate a local_site row.
429 /// This will only be run for brand new sites.
430 async fn initialize_local_site_2022_10_10(
431   pool: &mut DbPool<'_>,
432   settings: &Settings,
433 ) -> Result<(), LemmyError> {
434   info!("Running initialize_local_site_2022_10_10");
435
436   // Check to see if local_site exists
437   if LocalSite::read(pool).await.is_ok() {
438     return Ok(());
439   }
440   info!("No Local Site found, creating it.");
441
442   let domain = settings
443     .get_hostname_without_port()
444     .expect("must have domain");
445
446   // Upsert this to the instance table
447   let instance = Instance::read_or_create(pool, domain).await?;
448
449   if let Some(setup) = &settings.setup {
450     let person_keypair = generate_actor_keypair()?;
451     let person_actor_id = generate_local_apub_endpoint(
452       EndpointType::Person,
453       &setup.admin_username,
454       &settings.get_protocol_and_hostname(),
455     )?;
456
457     // Register the user if there's a site setup
458     let person_form = PersonInsertForm::builder()
459       .name(setup.admin_username.clone())
460       .admin(Some(true))
461       .instance_id(instance.id)
462       .actor_id(Some(person_actor_id.clone()))
463       .private_key(Some(person_keypair.private_key))
464       .public_key(person_keypair.public_key)
465       .inbox_url(Some(generate_inbox_url(&person_actor_id)?))
466       .shared_inbox_url(Some(generate_shared_inbox_url(&person_actor_id)?))
467       .build();
468     let person_inserted = Person::create(pool, &person_form).await?;
469
470     let local_user_form = LocalUserInsertForm::builder()
471       .person_id(person_inserted.id)
472       .password_encrypted(setup.admin_password.clone())
473       .email(setup.admin_email.clone())
474       .build();
475     LocalUser::create(pool, &local_user_form).await?;
476   };
477
478   // Add an entry for the site table
479   let site_key_pair = generate_actor_keypair()?;
480   let site_actor_id = Url::parse(&settings.get_protocol_and_hostname())?;
481
482   let site_form = SiteInsertForm::builder()
483     .name(
484       settings
485         .setup
486         .clone()
487         .map(|s| s.site_name)
488         .unwrap_or_else(|| "New Site".to_string()),
489     )
490     .instance_id(instance.id)
491     .actor_id(Some(site_actor_id.clone().into()))
492     .last_refreshed_at(Some(naive_now()))
493     .inbox_url(Some(generate_site_inbox_url(&site_actor_id.into())?))
494     .private_key(Some(site_key_pair.private_key))
495     .public_key(Some(site_key_pair.public_key))
496     .build();
497   let site = Site::create(pool, &site_form).await?;
498
499   // Finally create the local_site row
500   let local_site_form = LocalSiteInsertForm::builder()
501     .site_id(site.id)
502     .site_setup(Some(settings.setup.is_some()))
503     .build();
504   let local_site = LocalSite::create(pool, &local_site_form).await?;
505
506   // Create the rate limit table
507   let local_site_rate_limit_form = LocalSiteRateLimitInsertForm::builder()
508     // TODO these have to be set, because the database defaults are too low for the federation
509     // tests to pass, and there's no way to live update the rate limits without restarting the
510     // server.
511     // This can be removed once live rate limits are enabled.
512     .message(Some(999))
513     .post(Some(999))
514     .register(Some(999))
515     .image(Some(999))
516     .comment(Some(999))
517     .search(Some(999))
518     .local_site_id(local_site.id)
519     .build();
520   LocalSiteRateLimit::create(pool, &local_site_rate_limit_form).await?;
521
522   Ok(())
523 }