]> Untitled Git - lemmy.git/blobdiff - src/code_migrations.rs
Making public key required. Fixes #1934
[lemmy.git] / src / code_migrations.rs
index 2afdfabda1c0e1849317caceb2b53b72f4b2302a..b7c43508be15e2103c8d0083dc812cf757e57392 100644 (file)
@@ -3,204 +3,208 @@ use diesel::{
   sql_types::{Nullable, Text},
   *,
 };
-use lemmy_db::{
-  source::{
-    comment::Comment_,
-    community::{Community, CommunityForm},
-    post::Post_,
-    private_message::PrivateMessage,
-  },
-  Crud,
+use lemmy_apub::{
+  generate_followers_url,
+  generate_inbox_url,
+  generate_local_apub_endpoint,
+  generate_shared_inbox_url,
+  EndpointType,
 };
 use lemmy_db_schema::{
   naive_now,
   source::{
     comment::Comment,
+    community::{Community, CommunityForm},
+    person::{Person, PersonForm},
     post::Post,
-    user::{UserForm, User_},
+    private_message::PrivateMessage,
   },
+  traits::Crud,
 };
-use lemmy_utils::{
-  apub::{generate_actor_keypair, make_apub_endpoint, EndpointType},
-  settings::Settings,
-  LemmyError,
-};
+use lemmy_utils::{apub::generate_actor_keypair, LemmyError};
 use log::info;
 
-pub fn run_advanced_migrations(conn: &PgConnection) -> Result<(), LemmyError> {
-  user_updates_2020_04_02(&conn)?;
-  community_updates_2020_04_02(&conn)?;
-  post_updates_2020_04_03(&conn)?;
-  comment_updates_2020_04_03(&conn)?;
-  private_message_updates_2020_05_05(&conn)?;
-  post_thumbnail_url_updates_2020_07_27(&conn)?;
+pub fn run_advanced_migrations(
+  conn: &PgConnection,
+  protocol_and_hostname: &str,
+) -> Result<(), LemmyError> {
+  user_updates_2020_04_02(conn, protocol_and_hostname)?;
+  community_updates_2020_04_02(conn, protocol_and_hostname)?;
+  post_updates_2020_04_03(conn, protocol_and_hostname)?;
+  comment_updates_2020_04_03(conn, protocol_and_hostname)?;
+  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)?;
 
   Ok(())
 }
 
-fn user_updates_2020_04_02(conn: &PgConnection) -> Result<(), LemmyError> {
-  use lemmy_db_schema::schema::user_::dsl::*;
+fn user_updates_2020_04_02(
+  conn: &PgConnection,
+  protocol_and_hostname: &str,
+) -> Result<(), LemmyError> {
+  use lemmy_db_schema::schema::person::dsl::*;
 
   info!("Running user_updates_2020_04_02");
 
   // Update the actor_id, private_key, and public_key, last_refreshed_at
-  let incorrect_users = user_
-    .filter(actor_id.like("changeme_%"))
+  let incorrect_persons = person
+    .filter(actor_id.like("http://changeme_%"))
     .filter(local.eq(true))
-    .load::<User_>(conn)?;
-
-  sql_query("alter table user_ disable trigger refresh_user").execute(conn)?;
+    .load::<Person>(conn)?;
 
-  for cuser in &incorrect_users {
+  for cperson in &incorrect_persons {
     let keypair = generate_actor_keypair()?;
 
-    let form = UserForm {
-      name: cuser.name.to_owned(),
-      email: Some(cuser.email.to_owned()),
-      matrix_user_id: Some(cuser.matrix_user_id.to_owned()),
-      avatar: Some(cuser.avatar.to_owned()),
-      banner: Some(cuser.banner.to_owned()),
-      password_encrypted: cuser.password_encrypted.to_owned(),
-      preferred_username: Some(cuser.preferred_username.to_owned()),
-      published: Some(cuser.published),
-      updated: None,
-      admin: cuser.admin,
-      banned: Some(cuser.banned),
-      show_nsfw: cuser.show_nsfw,
-      theme: cuser.theme.to_owned(),
-      default_sort_type: cuser.default_sort_type,
-      default_listing_type: cuser.default_listing_type,
-      lang: cuser.lang.to_owned(),
-      show_avatars: cuser.show_avatars,
-      send_notifications_to_email: cuser.send_notifications_to_email,
-      actor_id: Some(make_apub_endpoint(EndpointType::User, &cuser.name).to_string()),
-      bio: Some(cuser.bio.to_owned()),
-      local: cuser.local,
-      private_key: Some(keypair.private_key),
-      public_key: Some(keypair.public_key),
+    let form = PersonForm {
+      name: cperson.name.to_owned(),
+      actor_id: Some(generate_local_apub_endpoint(
+        EndpointType::Person,
+        &cperson.name,
+        protocol_and_hostname,
+      )?),
+      private_key: Some(Some(keypair.private_key)),
+      public_key: keypair.public_key,
       last_refreshed_at: Some(naive_now()),
+      ..PersonForm::default()
     };
 
-    User_::update(&conn, cuser.id, &form)?;
+    Person::update(conn, cperson.id, &form)?;
   }
 
-  sql_query("alter table user_ enable trigger refresh_user").execute(conn)?;
-
-  info!("{} user rows updated.", incorrect_users.len());
+  info!("{} person rows updated.", incorrect_persons.len());
 
   Ok(())
 }
 
-fn community_updates_2020_04_02(conn: &PgConnection) -> Result<(), LemmyError> {
+fn community_updates_2020_04_02(
+  conn: &PgConnection,
+  protocol_and_hostname: &str,
+) -> Result<(), LemmyError> {
   use lemmy_db_schema::schema::community::dsl::*;
 
   info!("Running 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("changeme_%"))
+    .filter(actor_id.like("http://changeme_%"))
     .filter(local.eq(true))
     .load::<Community>(conn)?;
 
-  sql_query("alter table community disable trigger refresh_community").execute(conn)?;
-
   for ccommunity in &incorrect_communities {
     let keypair = generate_actor_keypair()?;
+    let community_actor_id = generate_local_apub_endpoint(
+      EndpointType::Community,
+      &ccommunity.name,
+      protocol_and_hostname,
+    )?;
 
     let form = CommunityForm {
       name: ccommunity.name.to_owned(),
       title: ccommunity.title.to_owned(),
       description: ccommunity.description.to_owned(),
-      category_id: ccommunity.category_id,
-      creator_id: ccommunity.creator_id,
       removed: None,
       deleted: None,
-      nsfw: ccommunity.nsfw,
+      nsfw: None,
       updated: None,
-      actor_id: Some(make_apub_endpoint(EndpointType::Community, &ccommunity.name).to_string()),
-      local: ccommunity.local,
-      private_key: Some(keypair.private_key),
-      public_key: Some(keypair.public_key),
+      actor_id: Some(community_actor_id.to_owned()),
+      local: Some(ccommunity.local),
+      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,
     };
 
-    Community::update(&conn, ccommunity.id, &form)?;
+    Community::update(conn, ccommunity.id, &form)?;
   }
 
-  sql_query("alter table community enable trigger refresh_community").execute(conn)?;
-
   info!("{} community rows updated.", incorrect_communities.len());
 
   Ok(())
 }
 
-fn post_updates_2020_04_03(conn: &PgConnection) -> Result<(), LemmyError> {
+fn post_updates_2020_04_03(
+  conn: &PgConnection,
+  protocol_and_hostname: &str,
+) -> Result<(), LemmyError> {
   use lemmy_db_schema::schema::post::dsl::*;
 
   info!("Running post_updates_2020_04_03");
 
   // Update the ap_id
   let incorrect_posts = post
-    .filter(ap_id.eq("changeme_%"))
+    .filter(ap_id.like("http://changeme_%"))
     .filter(local.eq(true))
     .load::<Post>(conn)?;
 
-  sql_query("alter table post disable trigger refresh_post").execute(conn)?;
-
   for cpost in &incorrect_posts {
-    let apub_id = make_apub_endpoint(EndpointType::Post, &cpost.id.to_string()).to_string();
-    Post::update_ap_id(&conn, cpost.id, apub_id)?;
+    let apub_id = generate_local_apub_endpoint(
+      EndpointType::Post,
+      &cpost.id.to_string(),
+      protocol_and_hostname,
+    )?;
+    Post::update_ap_id(conn, cpost.id, apub_id)?;
   }
 
   info!("{} post rows updated.", incorrect_posts.len());
 
-  sql_query("alter table post enable trigger refresh_post").execute(conn)?;
-
   Ok(())
 }
 
-fn comment_updates_2020_04_03(conn: &PgConnection) -> Result<(), LemmyError> {
+fn comment_updates_2020_04_03(
+  conn: &PgConnection,
+  protocol_and_hostname: &str,
+) -> Result<(), LemmyError> {
   use lemmy_db_schema::schema::comment::dsl::*;
 
   info!("Running comment_updates_2020_04_03");
 
   // Update the ap_id
   let incorrect_comments = comment
-    .filter(ap_id.eq("changeme_%"))
+    .filter(ap_id.like("http://changeme_%"))
     .filter(local.eq(true))
     .load::<Comment>(conn)?;
 
-  sql_query("alter table comment disable trigger refresh_comment").execute(conn)?;
-
   for ccomment in &incorrect_comments {
-    let apub_id = make_apub_endpoint(EndpointType::Comment, &ccomment.id.to_string()).to_string();
-    Comment::update_ap_id(&conn, ccomment.id, apub_id)?;
+    let apub_id = generate_local_apub_endpoint(
+      EndpointType::Comment,
+      &ccomment.id.to_string(),
+      protocol_and_hostname,
+    )?;
+    Comment::update_ap_id(conn, ccomment.id, apub_id)?;
   }
 
-  sql_query("alter table comment enable trigger refresh_comment").execute(conn)?;
-
   info!("{} comment rows updated.", incorrect_comments.len());
 
   Ok(())
 }
 
-fn private_message_updates_2020_05_05(conn: &PgConnection) -> Result<(), LemmyError> {
+fn private_message_updates_2020_05_05(
+  conn: &PgConnection,
+  protocol_and_hostname: &str,
+) -> Result<(), LemmyError> {
   use lemmy_db_schema::schema::private_message::dsl::*;
 
   info!("Running private_message_updates_2020_05_05");
 
   // Update the ap_id
   let incorrect_pms = private_message
-    .filter(ap_id.eq("changeme_%"))
+    .filter(ap_id.like("http://changeme_%"))
     .filter(local.eq(true))
     .load::<PrivateMessage>(conn)?;
 
   for cpm in &incorrect_pms {
-    let apub_id = make_apub_endpoint(EndpointType::PrivateMessage, &cpm.id.to_string()).to_string();
-    PrivateMessage::update_ap_id(&conn, cpm.id, apub_id)?;
+    let apub_id = generate_local_apub_endpoint(
+      EndpointType::PrivateMessage,
+      &cpm.id.to_string(),
+      protocol_and_hostname,
+    )?;
+    PrivateMessage::update_ap_id(conn, cpm.id, apub_id)?;
   }
 
   info!("{} private message rows updated.", incorrect_pms.len());
@@ -208,15 +212,15 @@ fn private_message_updates_2020_05_05(conn: &PgConnection) -> Result<(), LemmyEr
   Ok(())
 }
 
-fn post_thumbnail_url_updates_2020_07_27(conn: &PgConnection) -> Result<(), LemmyError> {
+fn post_thumbnail_url_updates_2020_07_27(
+  conn: &PgConnection,
+  protocol_and_hostname: &str,
+) -> Result<(), LemmyError> {
   use lemmy_db_schema::schema::post::dsl::*;
 
   info!("Running post_thumbnail_url_updates_2020_07_27");
 
-  let domain_prefix = format!(
-    "{}/pictrs/image/",
-    Settings::get().get_protocol_and_hostname(),
-  );
+  let domain_prefix = format!("{}/pictrs/image/", protocol_and_hostname,);
 
   let incorrect_thumbnails = post.filter(thumbnail_url.not_like("http%"));
 
@@ -235,3 +239,48 @@ fn post_thumbnail_url_updates_2020_07_27(conn: &PgConnection) -> Result<(), Lemm
 
   Ok(())
 }
+
+/// We are setting inbox and follower URLs for local and remote actors alike, because for now
+/// all federated instances are also Lemmy and use the same URL scheme.
+fn apub_columns_2021_02_02(conn: &PgConnection) -> Result<(), LemmyError> {
+  info!("Running apub_columns_2021_02_02");
+  {
+    use lemmy_db_schema::schema::person::dsl::*;
+    let persons = person
+      .filter(inbox_url.like("http://changeme_%"))
+      .load::<Person>(conn)?;
+
+    for p in &persons {
+      let inbox_url_ = generate_inbox_url(&p.actor_id)?;
+      let shared_inbox_url_ = generate_shared_inbox_url(&p.actor_id)?;
+      diesel::update(person.find(p.id))
+        .set((
+          inbox_url.eq(inbox_url_),
+          shared_inbox_url.eq(shared_inbox_url_),
+        ))
+        .get_result::<Person>(conn)?;
+    }
+  }
+
+  {
+    use lemmy_db_schema::schema::community::dsl::*;
+    let communities = community
+      .filter(inbox_url.like("http://changeme_%"))
+      .load::<Community>(conn)?;
+
+    for c in &communities {
+      let followers_url_ = generate_followers_url(&c.actor_id)?;
+      let inbox_url_ = generate_inbox_url(&c.actor_id)?;
+      let shared_inbox_url_ = generate_shared_inbox_url(&c.actor_id)?;
+      diesel::update(community.find(c.id))
+        .set((
+          followers_url.eq(followers_url_),
+          inbox_url.eq(inbox_url_),
+          shared_inbox_url.eq(shared_inbox_url_),
+        ))
+        .get_result::<Community>(conn)?;
+    }
+  }
+
+  Ok(())
+}