2020-04-03 04:12:05 +00:00
|
|
|
// This is for db migrations that require code
|
2020-07-28 15:42:40 +00:00
|
|
|
use diesel::{
|
|
|
|
sql_types::{Nullable, Text},
|
|
|
|
*,
|
|
|
|
};
|
2021-02-04 16:34:58 +00:00
|
|
|
use lemmy_apub::{
|
|
|
|
generate_followers_url,
|
|
|
|
generate_inbox_url,
|
2021-10-25 16:09:21 +00:00
|
|
|
generate_local_apub_endpoint,
|
2021-02-04 16:34:58 +00:00
|
|
|
generate_shared_inbox_url,
|
2022-02-07 19:23:12 +00:00
|
|
|
generate_site_inbox_url,
|
2021-02-04 16:34:58 +00:00
|
|
|
EndpointType,
|
|
|
|
};
|
2020-12-18 17:27:25 +00:00
|
|
|
use lemmy_db_schema::{
|
2020-07-10 18:15:41 +00:00
|
|
|
naive_now,
|
2020-12-13 17:04:42 +00:00
|
|
|
source::{
|
|
|
|
comment::Comment,
|
|
|
|
community::{Community, CommunityForm},
|
2021-03-11 04:43:11 +00:00
|
|
|
person::{Person, PersonForm},
|
2020-12-13 17:04:42 +00:00
|
|
|
post::Post,
|
|
|
|
private_message::PrivateMessage,
|
2022-02-07 19:23:12 +00:00
|
|
|
site::{Site, SiteForm},
|
2020-12-13 17:04:42 +00:00
|
|
|
},
|
2021-10-16 13:33:38 +00:00
|
|
|
traits::Crud,
|
2020-05-16 14:04:08 +00:00
|
|
|
};
|
2021-09-22 15:57:09 +00:00
|
|
|
use lemmy_utils::{apub::generate_actor_keypair, LemmyError};
|
2021-11-23 12:16:47 +00:00
|
|
|
use tracing::info;
|
2022-02-07 19:23:12 +00:00
|
|
|
use url::Url;
|
2020-04-03 04:12:05 +00:00
|
|
|
|
2021-09-22 15:57:09 +00:00
|
|
|
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)?;
|
2021-07-05 16:07:26 +00:00
|
|
|
apub_columns_2021_02_02(conn)?;
|
2022-02-07 19:23:12 +00:00
|
|
|
instance_actor_2022_01_28(conn, protocol_and_hostname)?;
|
2020-07-01 12:54:29 +00:00
|
|
|
|
2020-04-03 04:12:05 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-09-22 15:57:09 +00:00
|
|
|
fn user_updates_2020_04_02(
|
|
|
|
conn: &PgConnection,
|
|
|
|
protocol_and_hostname: &str,
|
|
|
|
) -> Result<(), LemmyError> {
|
2021-03-11 04:43:11 +00:00
|
|
|
use lemmy_db_schema::schema::person::dsl::*;
|
2020-04-03 04:12:05 +00:00
|
|
|
|
|
|
|
info!("Running user_updates_2020_04_02");
|
|
|
|
|
|
|
|
// Update the actor_id, private_key, and public_key, last_refreshed_at
|
2021-03-11 04:43:11 +00:00
|
|
|
let incorrect_persons = person
|
2022-04-13 11:13:29 +00:00
|
|
|
.filter(actor_id.like("http://changeme%"))
|
2020-04-03 04:12:05 +00:00
|
|
|
.filter(local.eq(true))
|
2021-03-11 04:43:11 +00:00
|
|
|
.load::<Person>(conn)?;
|
2020-04-03 04:12:05 +00:00
|
|
|
|
2021-03-11 04:43:11 +00:00
|
|
|
for cperson in &incorrect_persons {
|
2020-04-19 11:44:44 +00:00
|
|
|
let keypair = generate_actor_keypair()?;
|
2020-04-03 04:12:05 +00:00
|
|
|
|
2021-03-11 04:43:11 +00:00
|
|
|
let form = PersonForm {
|
|
|
|
name: cperson.name.to_owned(),
|
2021-10-25 16:09:21 +00:00
|
|
|
actor_id: Some(generate_local_apub_endpoint(
|
2021-09-22 15:57:09 +00:00
|
|
|
EndpointType::Person,
|
|
|
|
&cperson.name,
|
|
|
|
protocol_and_hostname,
|
|
|
|
)?),
|
2021-03-11 04:43:11 +00:00
|
|
|
private_key: Some(Some(keypair.private_key)),
|
2021-11-22 15:10:18 +00:00
|
|
|
public_key: keypair.public_key,
|
2020-04-03 04:12:05 +00:00
|
|
|
last_refreshed_at: Some(naive_now()),
|
2021-03-20 20:59:07 +00:00
|
|
|
..PersonForm::default()
|
2020-04-03 04:12:05 +00:00
|
|
|
};
|
|
|
|
|
2021-07-05 16:07:26 +00:00
|
|
|
Person::update(conn, cperson.id, &form)?;
|
2020-04-03 04:12:05 +00:00
|
|
|
}
|
|
|
|
|
2021-03-11 04:43:11 +00:00
|
|
|
info!("{} person rows updated.", incorrect_persons.len());
|
2020-04-03 04:12:05 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-09-22 15:57:09 +00:00
|
|
|
fn community_updates_2020_04_02(
|
|
|
|
conn: &PgConnection,
|
|
|
|
protocol_and_hostname: &str,
|
|
|
|
) -> Result<(), LemmyError> {
|
2020-12-18 16:17:21 +00:00
|
|
|
use lemmy_db_schema::schema::community::dsl::*;
|
2020-04-03 04:12:05 +00:00
|
|
|
|
|
|
|
info!("Running community_updates_2020_04_02");
|
|
|
|
|
|
|
|
// Update the actor_id, private_key, and public_key, last_refreshed_at
|
|
|
|
let incorrect_communities = community
|
2022-04-13 11:13:29 +00:00
|
|
|
.filter(actor_id.like("http://changeme%"))
|
2020-04-03 04:12:05 +00:00
|
|
|
.filter(local.eq(true))
|
|
|
|
.load::<Community>(conn)?;
|
|
|
|
|
|
|
|
for ccommunity in &incorrect_communities {
|
2020-04-19 11:44:44 +00:00
|
|
|
let keypair = generate_actor_keypair()?;
|
2021-10-25 16:09:21 +00:00
|
|
|
let community_actor_id = generate_local_apub_endpoint(
|
2021-09-22 15:57:09 +00:00
|
|
|
EndpointType::Community,
|
|
|
|
&ccommunity.name,
|
|
|
|
protocol_and_hostname,
|
|
|
|
)?;
|
2020-04-03 04:12:05 +00:00
|
|
|
|
|
|
|
let form = CommunityForm {
|
|
|
|
name: ccommunity.name.to_owned(),
|
|
|
|
title: ccommunity.title.to_owned(),
|
|
|
|
description: ccommunity.description.to_owned(),
|
|
|
|
removed: None,
|
|
|
|
deleted: None,
|
2021-03-20 20:59:07 +00:00
|
|
|
nsfw: None,
|
2020-04-03 04:12:05 +00:00
|
|
|
updated: None,
|
2022-02-18 02:30:47 +00:00
|
|
|
hidden: Some(false),
|
2021-02-04 16:34:58 +00:00
|
|
|
actor_id: Some(community_actor_id.to_owned()),
|
2021-03-20 20:59:07 +00:00
|
|
|
local: Some(ccommunity.local),
|
2021-11-22 15:10:18 +00:00
|
|
|
private_key: Some(Some(keypair.private_key)),
|
|
|
|
public_key: keypair.public_key,
|
2020-04-03 04:12:05 +00:00
|
|
|
last_refreshed_at: Some(naive_now()),
|
2020-04-07 16:47:19 +00:00
|
|
|
published: None,
|
2020-08-05 16:03:46 +00:00
|
|
|
icon: Some(ccommunity.icon.to_owned()),
|
|
|
|
banner: Some(ccommunity.banner.to_owned()),
|
2021-02-04 16:34:58 +00:00
|
|
|
followers_url: None,
|
|
|
|
inbox_url: None,
|
|
|
|
shared_inbox_url: None,
|
2020-04-03 04:12:05 +00:00
|
|
|
};
|
|
|
|
|
2021-07-05 16:07:26 +00:00
|
|
|
Community::update(conn, ccommunity.id, &form)?;
|
2020-04-03 04:12:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
info!("{} community rows updated.", incorrect_communities.len());
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-04-04 00:04:57 +00:00
|
|
|
|
2021-09-22 15:57:09 +00:00
|
|
|
fn post_updates_2020_04_03(
|
|
|
|
conn: &PgConnection,
|
|
|
|
protocol_and_hostname: &str,
|
|
|
|
) -> Result<(), LemmyError> {
|
2020-12-18 16:17:21 +00:00
|
|
|
use lemmy_db_schema::schema::post::dsl::*;
|
2020-04-04 00:04:57 +00:00
|
|
|
|
|
|
|
info!("Running post_updates_2020_04_03");
|
|
|
|
|
|
|
|
// Update the ap_id
|
|
|
|
let incorrect_posts = post
|
2022-04-13 11:13:29 +00:00
|
|
|
.filter(ap_id.like("http://changeme%"))
|
2020-04-04 00:04:57 +00:00
|
|
|
.filter(local.eq(true))
|
|
|
|
.load::<Post>(conn)?;
|
|
|
|
|
|
|
|
for cpost in &incorrect_posts {
|
2021-10-25 16:09:21 +00:00
|
|
|
let apub_id = generate_local_apub_endpoint(
|
2021-09-22 15:57:09 +00:00
|
|
|
EndpointType::Post,
|
|
|
|
&cpost.id.to_string(),
|
|
|
|
protocol_and_hostname,
|
|
|
|
)?;
|
2021-07-05 16:07:26 +00:00
|
|
|
Post::update_ap_id(conn, cpost.id, apub_id)?;
|
2020-04-04 00:04:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
info!("{} post rows updated.", incorrect_posts.len());
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-09-22 15:57:09 +00:00
|
|
|
fn comment_updates_2020_04_03(
|
|
|
|
conn: &PgConnection,
|
|
|
|
protocol_and_hostname: &str,
|
|
|
|
) -> Result<(), LemmyError> {
|
2020-12-18 16:17:21 +00:00
|
|
|
use lemmy_db_schema::schema::comment::dsl::*;
|
2020-04-04 00:04:57 +00:00
|
|
|
|
|
|
|
info!("Running comment_updates_2020_04_03");
|
|
|
|
|
|
|
|
// Update the ap_id
|
|
|
|
let incorrect_comments = comment
|
2022-04-13 11:13:29 +00:00
|
|
|
.filter(ap_id.like("http://changeme%"))
|
2020-04-04 00:04:57 +00:00
|
|
|
.filter(local.eq(true))
|
|
|
|
.load::<Comment>(conn)?;
|
|
|
|
|
|
|
|
for ccomment in &incorrect_comments {
|
2021-10-25 16:09:21 +00:00
|
|
|
let apub_id = generate_local_apub_endpoint(
|
2021-09-22 15:57:09 +00:00
|
|
|
EndpointType::Comment,
|
|
|
|
&ccomment.id.to_string(),
|
|
|
|
protocol_and_hostname,
|
|
|
|
)?;
|
2021-07-05 16:07:26 +00:00
|
|
|
Comment::update_ap_id(conn, ccomment.id, apub_id)?;
|
2020-04-04 00:04:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
info!("{} comment rows updated.", incorrect_comments.len());
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-05-06 02:06:24 +00:00
|
|
|
|
2021-09-22 15:57:09 +00:00
|
|
|
fn private_message_updates_2020_05_05(
|
|
|
|
conn: &PgConnection,
|
|
|
|
protocol_and_hostname: &str,
|
|
|
|
) -> Result<(), LemmyError> {
|
2020-12-18 16:17:21 +00:00
|
|
|
use lemmy_db_schema::schema::private_message::dsl::*;
|
2020-05-06 02:06:24 +00:00
|
|
|
|
|
|
|
info!("Running private_message_updates_2020_05_05");
|
|
|
|
|
|
|
|
// Update the ap_id
|
|
|
|
let incorrect_pms = private_message
|
2022-04-13 11:13:29 +00:00
|
|
|
.filter(ap_id.like("http://changeme%"))
|
2020-05-06 02:06:24 +00:00
|
|
|
.filter(local.eq(true))
|
|
|
|
.load::<PrivateMessage>(conn)?;
|
|
|
|
|
|
|
|
for cpm in &incorrect_pms {
|
2021-10-25 16:09:21 +00:00
|
|
|
let apub_id = generate_local_apub_endpoint(
|
2021-09-22 15:57:09 +00:00
|
|
|
EndpointType::PrivateMessage,
|
|
|
|
&cpm.id.to_string(),
|
|
|
|
protocol_and_hostname,
|
|
|
|
)?;
|
2021-07-05 16:07:26 +00:00
|
|
|
PrivateMessage::update_ap_id(conn, cpm.id, apub_id)?;
|
2020-05-06 02:06:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
info!("{} private message rows updated.", incorrect_pms.len());
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-07-28 15:42:40 +00:00
|
|
|
|
2021-09-22 15:57:09 +00:00
|
|
|
fn post_thumbnail_url_updates_2020_07_27(
|
|
|
|
conn: &PgConnection,
|
|
|
|
protocol_and_hostname: &str,
|
|
|
|
) -> Result<(), LemmyError> {
|
2020-12-18 16:17:21 +00:00
|
|
|
use lemmy_db_schema::schema::post::dsl::*;
|
2020-07-28 15:42:40 +00:00
|
|
|
|
|
|
|
info!("Running post_thumbnail_url_updates_2020_07_27");
|
|
|
|
|
2021-09-22 15:57:09 +00:00
|
|
|
let domain_prefix = format!("{}/pictrs/image/", protocol_and_hostname,);
|
2020-07-28 15:42:40 +00:00
|
|
|
|
|
|
|
let incorrect_thumbnails = post.filter(thumbnail_url.not_like("http%"));
|
|
|
|
|
|
|
|
// Prepend the rows with the update
|
|
|
|
let res = diesel::update(incorrect_thumbnails)
|
|
|
|
.set(
|
|
|
|
thumbnail_url.eq(
|
|
|
|
domain_prefix
|
|
|
|
.into_sql::<Nullable<Text>>()
|
|
|
|
.concat(thumbnail_url),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.get_results::<Post>(conn)?;
|
|
|
|
|
|
|
|
info!("{} Post thumbnail_url rows updated.", res.len());
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-02-04 16:34:58 +00:00
|
|
|
|
|
|
|
/// 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");
|
|
|
|
{
|
2021-03-11 04:43:11 +00:00
|
|
|
use lemmy_db_schema::schema::person::dsl::*;
|
|
|
|
let persons = person
|
2022-04-13 11:13:29 +00:00
|
|
|
.filter(inbox_url.like("http://changeme%"))
|
2021-03-11 04:43:11 +00:00
|
|
|
.load::<Person>(conn)?;
|
2021-02-04 16:34:58 +00:00
|
|
|
|
2021-03-11 04:43:11 +00:00
|
|
|
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))
|
2021-02-04 16:34:58 +00:00
|
|
|
.set((
|
|
|
|
inbox_url.eq(inbox_url_),
|
|
|
|
shared_inbox_url.eq(shared_inbox_url_),
|
|
|
|
))
|
2021-03-11 04:43:11 +00:00
|
|
|
.get_result::<Person>(conn)?;
|
2021-02-04 16:34:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
use lemmy_db_schema::schema::community::dsl::*;
|
|
|
|
let communities = community
|
2022-04-13 11:13:29 +00:00
|
|
|
.filter(inbox_url.like("http://changeme%"))
|
2021-02-04 16:34:58 +00:00
|
|
|
.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(())
|
|
|
|
}
|
2022-02-07 19:23:12 +00:00
|
|
|
|
|
|
|
/// 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(())
|
|
|
|
}
|