2020-04-03 04:12:05 +00:00
|
|
|
// This is for db migrations that require code
|
2023-03-21 15:03:05 +00:00
|
|
|
use activitypub_federation::http_signatures::generate_actor_keypair;
|
2020-07-28 15:42:40 +00:00
|
|
|
use diesel::{
|
|
|
|
sql_types::{Nullable, Text},
|
2022-11-09 10:05:00 +00:00
|
|
|
ExpressionMethods,
|
|
|
|
IntoSql,
|
|
|
|
QueryDsl,
|
|
|
|
TextExpressionMethods,
|
2020-07-28 15:42:40 +00:00
|
|
|
};
|
2022-11-09 10:05:00 +00:00
|
|
|
use diesel_async::RunQueryDsl;
|
2022-11-26 20:47:13 +00:00
|
|
|
use lemmy_api_common::{
|
|
|
|
lemmy_db_views::structs::SiteView,
|
2022-11-28 14:29:33 +00:00
|
|
|
utils::{
|
|
|
|
generate_followers_url,
|
|
|
|
generate_inbox_url,
|
|
|
|
generate_local_apub_endpoint,
|
|
|
|
generate_shared_inbox_url,
|
|
|
|
generate_site_inbox_url,
|
|
|
|
EndpointType,
|
|
|
|
},
|
2021-02-04 16:34:58 +00:00
|
|
|
};
|
2020-12-18 17:27:25 +00:00
|
|
|
use lemmy_db_schema::{
|
2020-12-13 17:04:42 +00:00
|
|
|
source::{
|
2022-10-27 09:24:07 +00:00
|
|
|
comment::{Comment, CommentUpdateForm},
|
|
|
|
community::{Community, CommunityUpdateForm},
|
|
|
|
instance::Instance,
|
|
|
|
local_site::{LocalSite, LocalSiteInsertForm},
|
|
|
|
local_site_rate_limit::{LocalSiteRateLimit, LocalSiteRateLimitInsertForm},
|
|
|
|
local_user::{LocalUser, LocalUserInsertForm},
|
|
|
|
person::{Person, PersonInsertForm, PersonUpdateForm},
|
|
|
|
post::{Post, PostUpdateForm},
|
|
|
|
private_message::{PrivateMessage, PrivateMessageUpdateForm},
|
|
|
|
site::{Site, SiteInsertForm, SiteUpdateForm},
|
2020-12-13 17:04:42 +00:00
|
|
|
},
|
2021-10-16 13:33:38 +00:00
|
|
|
traits::Crud,
|
2022-11-09 10:05:00 +00:00
|
|
|
utils::{get_conn, naive_now, DbPool},
|
2020-05-16 14:04:08 +00:00
|
|
|
};
|
2022-10-27 09:24:07 +00:00
|
|
|
use lemmy_utils::{error::LemmyError, settings::structs::Settings};
|
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
|
|
|
|
2023-07-11 13:09:59 +00:00
|
|
|
pub async fn run_advanced_migrations(
|
|
|
|
pool: &mut DbPool<'_>,
|
|
|
|
settings: &Settings,
|
|
|
|
) -> Result<(), LemmyError> {
|
2022-10-27 09:24:07 +00:00
|
|
|
let protocol_and_hostname = &settings.get_protocol_and_hostname();
|
2022-11-09 10:05:00 +00:00
|
|
|
user_updates_2020_04_02(pool, protocol_and_hostname).await?;
|
|
|
|
community_updates_2020_04_02(pool, protocol_and_hostname).await?;
|
|
|
|
post_updates_2020_04_03(pool, protocol_and_hostname).await?;
|
|
|
|
comment_updates_2020_04_03(pool, protocol_and_hostname).await?;
|
|
|
|
private_message_updates_2020_05_05(pool, protocol_and_hostname).await?;
|
|
|
|
post_thumbnail_url_updates_2020_07_27(pool, protocol_and_hostname).await?;
|
|
|
|
apub_columns_2021_02_02(pool).await?;
|
|
|
|
instance_actor_2022_01_28(pool, protocol_and_hostname).await?;
|
|
|
|
regenerate_public_keys_2022_07_05(pool).await?;
|
|
|
|
initialize_local_site_2022_10_10(pool, settings).await?;
|
2020-07-01 12:54:29 +00:00
|
|
|
|
2020-04-03 04:12:05 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn user_updates_2020_04_02(
|
2023-07-11 13:09:59 +00:00
|
|
|
pool: &mut DbPool<'_>,
|
2021-09-22 15:57:09 +00:00
|
|
|
protocol_and_hostname: &str,
|
|
|
|
) -> Result<(), LemmyError> {
|
2022-11-19 04:33:54 +00:00
|
|
|
use lemmy_db_schema::schema::person::dsl::{actor_id, local, person};
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
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))
|
2022-11-09 10:05:00 +00:00
|
|
|
.load::<Person>(conn)
|
|
|
|
.await?;
|
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
|
|
|
|
2023-08-08 09:41:41 +00:00
|
|
|
let form = PersonUpdateForm {
|
|
|
|
actor_id: Some(generate_local_apub_endpoint(
|
2021-09-22 15:57:09 +00:00
|
|
|
EndpointType::Person,
|
|
|
|
&cperson.name,
|
|
|
|
protocol_and_hostname,
|
2023-08-08 09:41:41 +00:00
|
|
|
)?),
|
|
|
|
private_key: Some(Some(keypair.private_key)),
|
|
|
|
public_key: Some(keypair.public_key),
|
|
|
|
last_refreshed_at: Some(naive_now()),
|
|
|
|
..Default::default()
|
|
|
|
};
|
2020-04-03 04:12:05 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
Person::update(pool, cperson.id, &form).await?;
|
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(())
|
|
|
|
}
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn community_updates_2020_04_02(
|
2023-07-11 13:09:59 +00:00
|
|
|
pool: &mut DbPool<'_>,
|
2021-09-22 15:57:09 +00:00
|
|
|
protocol_and_hostname: &str,
|
|
|
|
) -> Result<(), LemmyError> {
|
2022-11-19 04:33:54 +00:00
|
|
|
use lemmy_db_schema::schema::community::dsl::{actor_id, community, local};
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
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))
|
2022-11-09 10:05:00 +00:00
|
|
|
.load::<Community>(conn)
|
|
|
|
.await?;
|
2020-04-03 04:12:05 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2023-08-08 09:41:41 +00:00
|
|
|
let form = CommunityUpdateForm {
|
|
|
|
actor_id: Some(community_actor_id.clone()),
|
|
|
|
private_key: Some(Some(keypair.private_key)),
|
|
|
|
public_key: Some(keypair.public_key),
|
|
|
|
last_refreshed_at: Some(naive_now()),
|
|
|
|
..Default::default()
|
|
|
|
};
|
2020-04-03 04:12:05 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
Community::update(pool, ccommunity.id, &form).await?;
|
2020-04-03 04:12:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
info!("{} community rows updated.", incorrect_communities.len());
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-04-04 00:04:57 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn post_updates_2020_04_03(
|
2023-07-11 13:09:59 +00:00
|
|
|
pool: &mut DbPool<'_>,
|
2021-09-22 15:57:09 +00:00
|
|
|
protocol_and_hostname: &str,
|
|
|
|
) -> Result<(), LemmyError> {
|
2022-11-19 04:33:54 +00:00
|
|
|
use lemmy_db_schema::schema::post::dsl::{ap_id, local, post};
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
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))
|
2022-11-09 10:05:00 +00:00
|
|
|
.load::<Post>(conn)
|
|
|
|
.await?;
|
2020-04-04 00:04:57 +00:00
|
|
|
|
|
|
|
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,
|
|
|
|
)?;
|
2022-10-27 09:24:07 +00:00
|
|
|
Post::update(
|
2022-11-09 10:05:00 +00:00
|
|
|
pool,
|
2022-10-27 09:24:07 +00:00
|
|
|
cpost.id,
|
2023-08-08 09:41:41 +00:00
|
|
|
&PostUpdateForm {
|
|
|
|
ap_id: Some(apub_id),
|
|
|
|
..Default::default()
|
|
|
|
},
|
2022-11-09 10:05:00 +00:00
|
|
|
)
|
|
|
|
.await?;
|
2020-04-04 00:04:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
info!("{} post rows updated.", incorrect_posts.len());
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn comment_updates_2020_04_03(
|
2023-07-11 13:09:59 +00:00
|
|
|
pool: &mut DbPool<'_>,
|
2021-09-22 15:57:09 +00:00
|
|
|
protocol_and_hostname: &str,
|
|
|
|
) -> Result<(), LemmyError> {
|
2022-11-19 04:33:54 +00:00
|
|
|
use lemmy_db_schema::schema::comment::dsl::{ap_id, comment, local};
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
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))
|
2022-11-09 10:05:00 +00:00
|
|
|
.load::<Comment>(conn)
|
|
|
|
.await?;
|
2020-04-04 00:04:57 +00:00
|
|
|
|
|
|
|
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,
|
|
|
|
)?;
|
2022-10-27 09:24:07 +00:00
|
|
|
Comment::update(
|
2022-11-09 10:05:00 +00:00
|
|
|
pool,
|
2022-10-27 09:24:07 +00:00
|
|
|
ccomment.id,
|
2023-08-08 09:41:41 +00:00
|
|
|
&CommentUpdateForm {
|
|
|
|
ap_id: Some(apub_id),
|
|
|
|
..Default::default()
|
|
|
|
},
|
2022-11-09 10:05:00 +00:00
|
|
|
)
|
|
|
|
.await?;
|
2020-04-04 00:04:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
info!("{} comment rows updated.", incorrect_comments.len());
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-05-06 02:06:24 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn private_message_updates_2020_05_05(
|
2023-07-11 13:09:59 +00:00
|
|
|
pool: &mut DbPool<'_>,
|
2021-09-22 15:57:09 +00:00
|
|
|
protocol_and_hostname: &str,
|
|
|
|
) -> Result<(), LemmyError> {
|
2022-11-19 04:33:54 +00:00
|
|
|
use lemmy_db_schema::schema::private_message::dsl::{ap_id, local, private_message};
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
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))
|
2022-11-09 10:05:00 +00:00
|
|
|
.load::<PrivateMessage>(conn)
|
|
|
|
.await?;
|
2020-05-06 02:06:24 +00:00
|
|
|
|
|
|
|
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,
|
|
|
|
)?;
|
2022-10-27 09:24:07 +00:00
|
|
|
PrivateMessage::update(
|
2022-11-09 10:05:00 +00:00
|
|
|
pool,
|
2022-10-27 09:24:07 +00:00
|
|
|
cpm.id,
|
2023-08-08 09:41:41 +00:00
|
|
|
&PrivateMessageUpdateForm {
|
|
|
|
ap_id: Some(apub_id),
|
|
|
|
..Default::default()
|
|
|
|
},
|
2022-11-09 10:05:00 +00:00
|
|
|
)
|
|
|
|
.await?;
|
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
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn post_thumbnail_url_updates_2020_07_27(
|
2023-07-11 13:09:59 +00:00
|
|
|
pool: &mut DbPool<'_>,
|
2021-09-22 15:57:09 +00:00
|
|
|
protocol_and_hostname: &str,
|
|
|
|
) -> Result<(), LemmyError> {
|
2022-11-19 04:33:54 +00:00
|
|
|
use lemmy_db_schema::schema::post::dsl::{post, thumbnail_url};
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2020-07-28 15:42:40 +00:00
|
|
|
|
|
|
|
info!("Running post_thumbnail_url_updates_2020_07_27");
|
|
|
|
|
2023-01-30 19:17:24 +00:00
|
|
|
let domain_prefix = format!("{protocol_and_hostname}/pictrs/image/",);
|
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),
|
|
|
|
),
|
|
|
|
)
|
2022-11-09 10:05:00 +00:00
|
|
|
.get_results::<Post>(conn)
|
|
|
|
.await?;
|
2020-07-28 15:42:40 +00:00
|
|
|
|
|
|
|
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.
|
2023-07-11 13:09:59 +00:00
|
|
|
async fn apub_columns_2021_02_02(pool: &mut DbPool<'_>) -> Result<(), LemmyError> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2021-02-04 16:34:58 +00:00
|
|
|
info!("Running apub_columns_2021_02_02");
|
|
|
|
{
|
2022-11-19 04:33:54 +00:00
|
|
|
use lemmy_db_schema::schema::person::dsl::{inbox_url, person, shared_inbox_url};
|
2021-03-11 04:43:11 +00:00
|
|
|
let persons = person
|
2022-04-13 11:13:29 +00:00
|
|
|
.filter(inbox_url.like("http://changeme%"))
|
2022-11-09 10:05:00 +00:00
|
|
|
.load::<Person>(conn)
|
|
|
|
.await?;
|
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_),
|
|
|
|
))
|
2022-11-09 10:05:00 +00:00
|
|
|
.get_result::<Person>(conn)
|
|
|
|
.await?;
|
2021-02-04 16:34:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2022-11-19 04:33:54 +00:00
|
|
|
use lemmy_db_schema::schema::community::dsl::{
|
|
|
|
community,
|
|
|
|
followers_url,
|
|
|
|
inbox_url,
|
|
|
|
shared_inbox_url,
|
|
|
|
};
|
2021-02-04 16:34:58 +00:00
|
|
|
let communities = community
|
2022-04-13 11:13:29 +00:00
|
|
|
.filter(inbox_url.like("http://changeme%"))
|
2022-11-09 10:05:00 +00:00
|
|
|
.load::<Community>(conn)
|
|
|
|
.await?;
|
2021-02-04 16:34:58 +00:00
|
|
|
|
|
|
|
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_),
|
|
|
|
))
|
2022-11-09 10:05:00 +00:00
|
|
|
.get_result::<Community>(conn)
|
|
|
|
.await?;
|
2021-02-04 16:34:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn instance_actor_2022_01_28(
|
2023-07-11 13:09:59 +00:00
|
|
|
pool: &mut DbPool<'_>,
|
2022-02-07 19:23:12 +00:00
|
|
|
protocol_and_hostname: &str,
|
|
|
|
) -> Result<(), LemmyError> {
|
|
|
|
info!("Running instance_actor_2021_09_29");
|
2022-11-09 10:05:00 +00:00
|
|
|
if let Ok(site_view) = SiteView::read_local(pool).await {
|
2022-10-27 09:24:07 +00:00
|
|
|
let site = site_view.site;
|
2022-07-11 18:25:33 +00:00
|
|
|
// if site already has public key, we dont need to do anything here
|
|
|
|
if !site.public_key.is_empty() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
2022-02-07 19:23:12 +00:00
|
|
|
let key_pair = generate_actor_keypair()?;
|
|
|
|
let actor_id = Url::parse(protocol_and_hostname)?;
|
2023-08-08 09:41:41 +00:00
|
|
|
let site_form = SiteUpdateForm {
|
|
|
|
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()
|
|
|
|
};
|
2022-11-09 10:05:00 +00:00
|
|
|
Site::update(pool, site.id, &site_form).await?;
|
2022-02-07 19:23:12 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-07-11 18:25:33 +00:00
|
|
|
|
|
|
|
/// Fix for bug #2347, which can result in community/person public keys being overwritten with
|
|
|
|
/// empty string when the database value is updated. We go through all actors, and if the public
|
|
|
|
/// key field is empty, generate a new keypair. It would be possible to regenerate only the pubkey,
|
|
|
|
/// but thats more complicated and has no benefit, as federation is already broken for these actors.
|
|
|
|
/// https://github.com/LemmyNet/lemmy/issues/2347
|
2023-07-11 13:09:59 +00:00
|
|
|
async fn regenerate_public_keys_2022_07_05(pool: &mut DbPool<'_>) -> Result<(), LemmyError> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
2022-07-11 18:25:33 +00:00
|
|
|
info!("Running regenerate_public_keys_2022_07_05");
|
|
|
|
|
|
|
|
{
|
|
|
|
// update communities with empty pubkey
|
2022-11-19 04:33:54 +00:00
|
|
|
use lemmy_db_schema::schema::community::dsl::{community, local, public_key};
|
2022-07-11 18:25:33 +00:00
|
|
|
let communities: Vec<Community> = community
|
|
|
|
.filter(local.eq(true))
|
|
|
|
.filter(public_key.eq(""))
|
2022-11-09 10:05:00 +00:00
|
|
|
.load::<Community>(conn)
|
|
|
|
.await?;
|
2022-07-11 18:25:33 +00:00
|
|
|
for community_ in communities {
|
|
|
|
info!(
|
|
|
|
"local community {} has empty public key field, regenerating key",
|
|
|
|
community_.name
|
|
|
|
);
|
|
|
|
let key_pair = generate_actor_keypair()?;
|
2023-08-08 09:41:41 +00:00
|
|
|
let form = CommunityUpdateForm {
|
|
|
|
public_key: Some(key_pair.public_key),
|
|
|
|
private_key: Some(Some(key_pair.private_key)),
|
|
|
|
..Default::default()
|
|
|
|
};
|
2023-07-11 13:09:59 +00:00
|
|
|
Community::update(&mut conn.into(), community_.id, &form).await?;
|
2022-07-11 18:25:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// update persons with empty pubkey
|
2022-11-19 04:33:54 +00:00
|
|
|
use lemmy_db_schema::schema::person::dsl::{local, person, public_key};
|
2022-07-11 18:25:33 +00:00
|
|
|
let persons = person
|
|
|
|
.filter(local.eq(true))
|
|
|
|
.filter(public_key.eq(""))
|
2022-11-09 10:05:00 +00:00
|
|
|
.load::<Person>(conn)
|
|
|
|
.await?;
|
2022-07-11 18:25:33 +00:00
|
|
|
for person_ in persons {
|
|
|
|
info!(
|
|
|
|
"local user {} has empty public key field, regenerating key",
|
|
|
|
person_.name
|
|
|
|
);
|
|
|
|
let key_pair = generate_actor_keypair()?;
|
2023-08-08 09:41:41 +00:00
|
|
|
let form = PersonUpdateForm {
|
|
|
|
public_key: Some(key_pair.public_key),
|
|
|
|
private_key: Some(Some(key_pair.private_key)),
|
|
|
|
..Default::default()
|
|
|
|
};
|
2022-11-09 10:05:00 +00:00
|
|
|
Person::update(pool, person_.id, &form).await?;
|
2022-07-11 18:25:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-10-27 09:24:07 +00:00
|
|
|
|
|
|
|
/// This ensures that your local site is initialized and exists.
|
|
|
|
///
|
|
|
|
/// If a site already exists, the DB migration should generate a local_site row.
|
|
|
|
/// This will only be run for brand new sites.
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn initialize_local_site_2022_10_10(
|
2023-07-11 13:09:59 +00:00
|
|
|
pool: &mut DbPool<'_>,
|
2022-10-27 09:24:07 +00:00
|
|
|
settings: &Settings,
|
|
|
|
) -> Result<(), LemmyError> {
|
|
|
|
info!("Running initialize_local_site_2022_10_10");
|
|
|
|
|
|
|
|
// Check to see if local_site exists
|
2022-11-09 10:05:00 +00:00
|
|
|
if LocalSite::read(pool).await.is_ok() {
|
2022-10-27 09:24:07 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
info!("No Local Site found, creating it.");
|
|
|
|
|
|
|
|
let domain = settings
|
|
|
|
.get_hostname_without_port()
|
|
|
|
.expect("must have domain");
|
|
|
|
|
|
|
|
// Upsert this to the instance table
|
2023-03-01 02:36:57 +00:00
|
|
|
let instance = Instance::read_or_create(pool, domain).await?;
|
2022-10-27 09:24:07 +00:00
|
|
|
|
|
|
|
if let Some(setup) = &settings.setup {
|
|
|
|
let person_keypair = generate_actor_keypair()?;
|
|
|
|
let person_actor_id = generate_local_apub_endpoint(
|
|
|
|
EndpointType::Person,
|
|
|
|
&setup.admin_username,
|
|
|
|
&settings.get_protocol_and_hostname(),
|
|
|
|
)?;
|
|
|
|
|
|
|
|
// Register the user if there's a site setup
|
|
|
|
let person_form = PersonInsertForm::builder()
|
2022-11-19 04:33:54 +00:00
|
|
|
.name(setup.admin_username.clone())
|
2022-10-27 09:24:07 +00:00
|
|
|
.admin(Some(true))
|
|
|
|
.instance_id(instance.id)
|
|
|
|
.actor_id(Some(person_actor_id.clone()))
|
|
|
|
.private_key(Some(person_keypair.private_key))
|
|
|
|
.public_key(person_keypair.public_key)
|
|
|
|
.inbox_url(Some(generate_inbox_url(&person_actor_id)?))
|
|
|
|
.shared_inbox_url(Some(generate_shared_inbox_url(&person_actor_id)?))
|
|
|
|
.build();
|
2022-11-09 10:05:00 +00:00
|
|
|
let person_inserted = Person::create(pool, &person_form).await?;
|
2022-10-27 09:24:07 +00:00
|
|
|
|
|
|
|
let local_user_form = LocalUserInsertForm::builder()
|
|
|
|
.person_id(person_inserted.id)
|
2022-11-19 04:33:54 +00:00
|
|
|
.password_encrypted(setup.admin_password.clone())
|
|
|
|
.email(setup.admin_email.clone())
|
2022-10-27 09:24:07 +00:00
|
|
|
.build();
|
2022-11-09 10:05:00 +00:00
|
|
|
LocalUser::create(pool, &local_user_form).await?;
|
2022-10-27 09:24:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Add an entry for the site table
|
|
|
|
let site_key_pair = generate_actor_keypair()?;
|
|
|
|
let site_actor_id = Url::parse(&settings.get_protocol_and_hostname())?;
|
|
|
|
|
|
|
|
let site_form = SiteInsertForm::builder()
|
|
|
|
.name(
|
|
|
|
settings
|
|
|
|
.setup
|
2022-11-19 04:33:54 +00:00
|
|
|
.clone()
|
2022-10-27 09:24:07 +00:00
|
|
|
.map(|s| s.site_name)
|
|
|
|
.unwrap_or_else(|| "New Site".to_string()),
|
|
|
|
)
|
|
|
|
.instance_id(instance.id)
|
|
|
|
.actor_id(Some(site_actor_id.clone().into()))
|
|
|
|
.last_refreshed_at(Some(naive_now()))
|
|
|
|
.inbox_url(Some(generate_site_inbox_url(&site_actor_id.into())?))
|
|
|
|
.private_key(Some(site_key_pair.private_key))
|
|
|
|
.public_key(Some(site_key_pair.public_key))
|
|
|
|
.build();
|
2022-11-09 10:05:00 +00:00
|
|
|
let site = Site::create(pool, &site_form).await?;
|
2022-10-27 09:24:07 +00:00
|
|
|
|
|
|
|
// Finally create the local_site row
|
|
|
|
let local_site_form = LocalSiteInsertForm::builder()
|
|
|
|
.site_id(site.id)
|
|
|
|
.site_setup(Some(settings.setup.is_some()))
|
|
|
|
.build();
|
2022-11-09 10:05:00 +00:00
|
|
|
let local_site = LocalSite::create(pool, &local_site_form).await?;
|
2022-10-27 09:24:07 +00:00
|
|
|
|
|
|
|
// Create the rate limit table
|
|
|
|
let local_site_rate_limit_form = LocalSiteRateLimitInsertForm::builder()
|
|
|
|
// TODO these have to be set, because the database defaults are too low for the federation
|
|
|
|
// tests to pass, and there's no way to live update the rate limits without restarting the
|
|
|
|
// server.
|
|
|
|
// This can be removed once live rate limits are enabled.
|
|
|
|
.message(Some(999))
|
|
|
|
.post(Some(999))
|
|
|
|
.register(Some(999))
|
|
|
|
.image(Some(999))
|
|
|
|
.comment(Some(999))
|
|
|
|
.search(Some(999))
|
|
|
|
.local_site_id(local_site.id)
|
|
|
|
.build();
|
2022-11-09 10:05:00 +00:00
|
|
|
LocalSiteRateLimit::create(pool, &local_site_rate_limit_form).await?;
|
2022-10-27 09:24:07 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|