]> Untitled Git - lemmy.git/blobdiff - src/code_migrations.rs
Implement restricted community (only mods can post) (fixes #187) (#2235)
[lemmy.git] / src / code_migrations.rs
index 1f9381986e954fd6486665d3dd8ea7ff663c5236..69e419161648456d9961e54c68d2ac7a7dc9b80d 100644 (file)
@@ -4,16 +4,13 @@ use diesel::{
   *,
 };
 use lemmy_apub::{
-  generate_apub_endpoint,
   generate_followers_url,
   generate_inbox_url,
+  generate_local_apub_endpoint,
   generate_shared_inbox_url,
+  generate_site_inbox_url,
   EndpointType,
 };
-use lemmy_db_queries::{
-  source::{comment::Comment_, post::Post_, private_message::PrivateMessage_},
-  Crud,
-};
 use lemmy_db_schema::{
   naive_now,
   source::{
@@ -22,10 +19,13 @@ use lemmy_db_schema::{
     person::{Person, PersonForm},
     post::Post,
     private_message::PrivateMessage,
+    site::{Site, SiteForm},
   },
+  traits::Crud,
 };
 use lemmy_utils::{apub::generate_actor_keypair, LemmyError};
-use log::info;
+use tracing::info;
+use url::Url;
 
 pub fn run_advanced_migrations(
   conn: &PgConnection,
@@ -38,6 +38,7 @@ pub fn run_advanced_migrations(
   private_message_updates_2020_05_05(conn, protocol_and_hostname)?;
   post_thumbnail_url_updates_2020_07_27(conn, protocol_and_hostname)?;
   apub_columns_2021_02_02(conn)?;
+  instance_actor_2022_01_28(conn, protocol_and_hostname)?;
 
   Ok(())
 }
@@ -52,7 +53,7 @@ fn user_updates_2020_04_02(
 
   // Update the actor_id, private_key, and public_key, last_refreshed_at
   let incorrect_persons = person
-    .filter(actor_id.like("http://changeme_%"))
+    .filter(actor_id.like("http://changeme%"))
     .filter(local.eq(true))
     .load::<Person>(conn)?;
 
@@ -61,13 +62,13 @@ fn user_updates_2020_04_02(
 
     let form = PersonForm {
       name: cperson.name.to_owned(),
-      actor_id: Some(generate_apub_endpoint(
+      actor_id: Some(generate_local_apub_endpoint(
         EndpointType::Person,
         &cperson.name,
         protocol_and_hostname,
       )?),
       private_key: Some(Some(keypair.private_key)),
-      public_key: Some(Some(keypair.public_key)),
+      public_key: keypair.public_key,
       last_refreshed_at: Some(naive_now()),
       ..PersonForm::default()
     };
@@ -90,13 +91,13 @@ fn community_updates_2020_04_02(
 
   // Update the actor_id, private_key, and public_key, last_refreshed_at
   let incorrect_communities = community
-    .filter(actor_id.like("http://changeme_%"))
+    .filter(actor_id.like("http://changeme%"))
     .filter(local.eq(true))
     .load::<Community>(conn)?;
 
   for ccommunity in &incorrect_communities {
     let keypair = generate_actor_keypair()?;
-    let community_actor_id = generate_apub_endpoint(
+    let community_actor_id = generate_local_apub_endpoint(
       EndpointType::Community,
       &ccommunity.name,
       protocol_and_hostname,
@@ -106,21 +107,15 @@ fn community_updates_2020_04_02(
       name: ccommunity.name.to_owned(),
       title: ccommunity.title.to_owned(),
       description: ccommunity.description.to_owned(),
-      removed: None,
-      deleted: None,
-      nsfw: None,
-      updated: None,
+      hidden: Some(false),
       actor_id: Some(community_actor_id.to_owned()),
       local: Some(ccommunity.local),
-      private_key: Some(keypair.private_key),
-      public_key: Some(keypair.public_key),
+      private_key: Some(Some(keypair.private_key)),
+      public_key: keypair.public_key,
       last_refreshed_at: Some(naive_now()),
-      published: None,
       icon: Some(ccommunity.icon.to_owned()),
       banner: Some(ccommunity.banner.to_owned()),
-      followers_url: None,
-      inbox_url: None,
-      shared_inbox_url: None,
+      ..Default::default()
     };
 
     Community::update(conn, ccommunity.id, &form)?;
@@ -141,12 +136,12 @@ fn post_updates_2020_04_03(
 
   // Update the ap_id
   let incorrect_posts = post
-    .filter(ap_id.like("http://changeme_%"))
+    .filter(ap_id.like("http://changeme%"))
     .filter(local.eq(true))
     .load::<Post>(conn)?;
 
   for cpost in &incorrect_posts {
-    let apub_id = generate_apub_endpoint(
+    let apub_id = generate_local_apub_endpoint(
       EndpointType::Post,
       &cpost.id.to_string(),
       protocol_and_hostname,
@@ -169,12 +164,12 @@ fn comment_updates_2020_04_03(
 
   // Update the ap_id
   let incorrect_comments = comment
-    .filter(ap_id.like("http://changeme_%"))
+    .filter(ap_id.like("http://changeme%"))
     .filter(local.eq(true))
     .load::<Comment>(conn)?;
 
   for ccomment in &incorrect_comments {
-    let apub_id = generate_apub_endpoint(
+    let apub_id = generate_local_apub_endpoint(
       EndpointType::Comment,
       &ccomment.id.to_string(),
       protocol_and_hostname,
@@ -197,12 +192,12 @@ fn private_message_updates_2020_05_05(
 
   // Update the ap_id
   let incorrect_pms = private_message
-    .filter(ap_id.like("http://changeme_%"))
+    .filter(ap_id.like("http://changeme%"))
     .filter(local.eq(true))
     .load::<PrivateMessage>(conn)?;
 
   for cpm in &incorrect_pms {
-    let apub_id = generate_apub_endpoint(
+    let apub_id = generate_local_apub_endpoint(
       EndpointType::PrivateMessage,
       &cpm.id.to_string(),
       protocol_and_hostname,
@@ -250,7 +245,7 @@ fn apub_columns_2021_02_02(conn: &PgConnection) -> Result<(), LemmyError> {
   {
     use lemmy_db_schema::schema::person::dsl::*;
     let persons = person
-      .filter(inbox_url.like("http://changeme_%"))
+      .filter(inbox_url.like("http://changeme%"))
       .load::<Person>(conn)?;
 
     for p in &persons {
@@ -268,7 +263,7 @@ fn apub_columns_2021_02_02(conn: &PgConnection) -> Result<(), LemmyError> {
   {
     use lemmy_db_schema::schema::community::dsl::*;
     let communities = community
-      .filter(inbox_url.like("http://changeme_%"))
+      .filter(inbox_url.like("http://changeme%"))
       .load::<Community>(conn)?;
 
     for c in &communities {
@@ -287,3 +282,29 @@ fn apub_columns_2021_02_02(conn: &PgConnection) -> Result<(), LemmyError> {
 
   Ok(())
 }
+
+/// Site object turns into an actor, so that things like instance description can be federated. This
+/// means we need to add actor columns to the site table, and initialize them with correct values.
+/// Before this point, there is only a single value in the site table which refers to the local
+/// Lemmy instance, so thats all we need to update.
+fn instance_actor_2022_01_28(
+  conn: &PgConnection,
+  protocol_and_hostname: &str,
+) -> Result<(), LemmyError> {
+  info!("Running instance_actor_2021_09_29");
+  if let Ok(site) = Site::read_local_site(conn) {
+    let key_pair = generate_actor_keypair()?;
+    let actor_id = Url::parse(protocol_and_hostname)?;
+    let site_form = SiteForm {
+      name: site.name,
+      actor_id: Some(actor_id.clone().into()),
+      last_refreshed_at: Some(naive_now()),
+      inbox_url: Some(generate_site_inbox_url(&actor_id.into())?),
+      private_key: Some(Some(key_pair.private_key)),
+      public_key: Some(key_pair.public_key),
+      ..Default::default()
+    };
+    Site::update(conn, site.id, &site_form)?;
+  }
+  Ok(())
+}