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},
|
|
|
|
*,
|
|
|
|
};
|
2020-07-10 18:15:41 +00:00
|
|
|
use lemmy_db::{
|
2020-12-21 13:38:34 +00:00
|
|
|
source::{comment::Comment_, post::Post_, private_message::PrivateMessage_},
|
2020-07-10 18:15:41 +00:00
|
|
|
Crud,
|
2020-05-16 14:04:08 +00:00
|
|
|
};
|
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},
|
|
|
|
post::Post,
|
|
|
|
private_message::PrivateMessage,
|
|
|
|
user::{UserForm, User_},
|
|
|
|
},
|
2020-05-16 14:04:08 +00:00
|
|
|
};
|
2020-07-28 15:42:40 +00:00
|
|
|
use lemmy_utils::{
|
2020-09-25 15:33:00 +00:00
|
|
|
apub::{generate_actor_keypair, make_apub_endpoint, EndpointType},
|
2020-07-28 15:42:40 +00:00
|
|
|
settings::Settings,
|
2020-09-01 14:25:34 +00:00
|
|
|
LemmyError,
|
2020-07-28 15:42:40 +00:00
|
|
|
};
|
2020-04-03 04:12:05 +00:00
|
|
|
use log::info;
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
pub fn run_advanced_migrations(conn: &PgConnection) -> Result<(), LemmyError> {
|
2020-06-27 01:12:41 +00:00
|
|
|
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)?;
|
2020-07-28 15:42:40 +00:00
|
|
|
post_thumbnail_url_updates_2020_07_27(&conn)?;
|
2020-07-01 12:54:29 +00:00
|
|
|
|
2020-04-03 04:12:05 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
fn user_updates_2020_04_02(conn: &PgConnection) -> Result<(), LemmyError> {
|
2020-12-18 16:17:21 +00:00
|
|
|
use lemmy_db_schema::schema::user_::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
|
|
|
|
let incorrect_users = user_
|
2020-07-19 17:12:42 +00:00
|
|
|
.filter(actor_id.like("changeme_%"))
|
2020-04-03 04:12:05 +00:00
|
|
|
.filter(local.eq(true))
|
|
|
|
.load::<User_>(conn)?;
|
|
|
|
|
|
|
|
for cuser in &incorrect_users {
|
2020-04-19 11:44:44 +00:00
|
|
|
let keypair = generate_actor_keypair()?;
|
2020-04-03 04:12:05 +00:00
|
|
|
|
|
|
|
let form = UserForm {
|
|
|
|
name: cuser.name.to_owned(),
|
2020-08-09 02:36:29 +00:00
|
|
|
email: Some(cuser.email.to_owned()),
|
2020-09-25 15:16:49 +00:00
|
|
|
matrix_user_id: Some(cuser.matrix_user_id.to_owned()),
|
2020-08-05 16:03:46 +00:00
|
|
|
avatar: Some(cuser.avatar.to_owned()),
|
|
|
|
banner: Some(cuser.banner.to_owned()),
|
2020-04-03 04:12:05 +00:00
|
|
|
password_encrypted: cuser.password_encrypted.to_owned(),
|
2020-09-25 15:16:49 +00:00
|
|
|
preferred_username: Some(cuser.preferred_username.to_owned()),
|
2020-09-18 11:04:12 +00:00
|
|
|
published: Some(cuser.published),
|
2020-04-03 04:12:05 +00:00
|
|
|
updated: None,
|
|
|
|
admin: cuser.admin,
|
2020-11-09 14:29:36 +00:00
|
|
|
banned: Some(cuser.banned),
|
2020-04-03 04:12:05 +00:00
|
|
|
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,
|
2020-08-31 13:48:02 +00:00
|
|
|
actor_id: Some(make_apub_endpoint(EndpointType::User, &cuser.name).to_string()),
|
2020-09-25 15:16:49 +00:00
|
|
|
bio: Some(cuser.bio.to_owned()),
|
2020-04-03 04:12:05 +00:00
|
|
|
local: cuser.local,
|
2020-04-18 18:54:20 +00:00
|
|
|
private_key: Some(keypair.private_key),
|
|
|
|
public_key: Some(keypair.public_key),
|
2020-04-03 04:12:05 +00:00
|
|
|
last_refreshed_at: Some(naive_now()),
|
|
|
|
};
|
|
|
|
|
|
|
|
User_::update(&conn, cuser.id, &form)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("{} user rows updated.", incorrect_users.len());
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
fn community_updates_2020_04_02(conn: &PgConnection) -> 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
|
2020-07-19 17:12:42 +00:00
|
|
|
.filter(actor_id.like("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()?;
|
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(),
|
|
|
|
category_id: ccommunity.category_id,
|
|
|
|
creator_id: ccommunity.creator_id,
|
|
|
|
removed: None,
|
|
|
|
deleted: None,
|
|
|
|
nsfw: ccommunity.nsfw,
|
|
|
|
updated: None,
|
2020-08-31 13:48:02 +00:00
|
|
|
actor_id: Some(make_apub_endpoint(EndpointType::Community, &ccommunity.name).to_string()),
|
2020-04-03 04:12:05 +00:00
|
|
|
local: ccommunity.local,
|
2020-04-18 18:54:20 +00:00
|
|
|
private_key: Some(keypair.private_key),
|
|
|
|
public_key: Some(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()),
|
2020-04-03 04:12:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Community::update(&conn, ccommunity.id, &form)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
info!("{} community rows updated.", incorrect_communities.len());
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-04-04 00:04:57 +00:00
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
fn post_updates_2020_04_03(conn: &PgConnection) -> 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
|
2020-08-31 13:48:02 +00:00
|
|
|
.filter(ap_id.eq("changeme_%"))
|
2020-04-04 00:04:57 +00:00
|
|
|
.filter(local.eq(true))
|
|
|
|
.load::<Post>(conn)?;
|
|
|
|
|
|
|
|
for cpost in &incorrect_posts {
|
2020-07-10 18:15:41 +00:00
|
|
|
let apub_id = make_apub_endpoint(EndpointType::Post, &cpost.id.to_string()).to_string();
|
|
|
|
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(())
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
fn comment_updates_2020_04_03(conn: &PgConnection) -> 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
|
2020-08-31 13:48:02 +00:00
|
|
|
.filter(ap_id.eq("changeme_%"))
|
2020-04-04 00:04:57 +00:00
|
|
|
.filter(local.eq(true))
|
|
|
|
.load::<Comment>(conn)?;
|
|
|
|
|
|
|
|
for ccomment in &incorrect_comments {
|
2020-07-10 18:15:41 +00:00
|
|
|
let apub_id = make_apub_endpoint(EndpointType::Comment, &ccomment.id.to_string()).to_string();
|
|
|
|
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
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
fn private_message_updates_2020_05_05(conn: &PgConnection) -> 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
|
2020-08-31 13:48:02 +00:00
|
|
|
.filter(ap_id.eq("changeme_%"))
|
2020-05-06 02:06:24 +00:00
|
|
|
.filter(local.eq(true))
|
|
|
|
.load::<PrivateMessage>(conn)?;
|
|
|
|
|
|
|
|
for cpm in &incorrect_pms {
|
2020-07-10 18:15:41 +00:00
|
|
|
let apub_id = make_apub_endpoint(EndpointType::PrivateMessage, &cpm.id.to_string()).to_string();
|
|
|
|
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
|
|
|
|
|
|
|
fn post_thumbnail_url_updates_2020_07_27(conn: &PgConnection) -> 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");
|
|
|
|
|
|
|
|
let domain_prefix = format!(
|
2020-09-25 15:33:00 +00:00
|
|
|
"{}/pictrs/image/",
|
|
|
|
Settings::get().get_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(())
|
|
|
|
}
|