1 // This is for db migrations that require code
3 sql_types::{Nullable, Text},
7 generate_apub_endpoint,
8 generate_followers_url,
10 generate_shared_inbox_url,
13 use lemmy_db_queries::{
14 source::{comment::Comment_, post::Post_, private_message::PrivateMessage_},
17 use lemmy_db_schema::{
21 community::{Community, CommunityForm},
23 private_message::PrivateMessage,
24 user::{UserForm, User_},
27 use lemmy_utils::{apub::generate_actor_keypair, settings::structs::Settings, LemmyError};
30 pub fn run_advanced_migrations(conn: &PgConnection) -> Result<(), LemmyError> {
31 user_updates_2020_04_02(&conn)?;
32 community_updates_2020_04_02(&conn)?;
33 post_updates_2020_04_03(&conn)?;
34 comment_updates_2020_04_03(&conn)?;
35 private_message_updates_2020_05_05(&conn)?;
36 post_thumbnail_url_updates_2020_07_27(&conn)?;
37 apub_columns_2021_02_02(&conn)?;
42 fn user_updates_2020_04_02(conn: &PgConnection) -> Result<(), LemmyError> {
43 use lemmy_db_schema::schema::user_::dsl::*;
45 info!("Running user_updates_2020_04_02");
47 // Update the actor_id, private_key, and public_key, last_refreshed_at
48 let incorrect_users = user_
49 .filter(actor_id.like("http://changeme_%"))
50 .filter(local.eq(true))
51 .load::<User_>(conn)?;
53 for cuser in &incorrect_users {
54 let keypair = generate_actor_keypair()?;
57 name: cuser.name.to_owned(),
58 email: Some(cuser.email.to_owned()),
59 matrix_user_id: Some(cuser.matrix_user_id.to_owned()),
60 avatar: Some(cuser.avatar.to_owned()),
61 banner: Some(cuser.banner.to_owned()),
62 password_encrypted: cuser.password_encrypted.to_owned(),
63 preferred_username: Some(cuser.preferred_username.to_owned()),
64 published: Some(cuser.published),
67 banned: Some(cuser.banned),
68 show_nsfw: cuser.show_nsfw,
69 theme: cuser.theme.to_owned(),
70 default_sort_type: cuser.default_sort_type,
71 default_listing_type: cuser.default_listing_type,
72 lang: cuser.lang.to_owned(),
73 show_avatars: cuser.show_avatars,
74 send_notifications_to_email: cuser.send_notifications_to_email,
75 actor_id: Some(generate_apub_endpoint(EndpointType::Person, &cuser.name)?),
76 bio: Some(cuser.bio.to_owned()),
78 private_key: Some(keypair.private_key),
79 public_key: Some(keypair.public_key),
80 last_refreshed_at: Some(naive_now()),
82 shared_inbox_url: None,
85 User_::update(&conn, cuser.id, &form)?;
88 info!("{} user rows updated.", incorrect_users.len());
93 fn community_updates_2020_04_02(conn: &PgConnection) -> Result<(), LemmyError> {
94 use lemmy_db_schema::schema::community::dsl::*;
96 info!("Running community_updates_2020_04_02");
98 // Update the actor_id, private_key, and public_key, last_refreshed_at
99 let incorrect_communities = community
100 .filter(actor_id.like("http://changeme_%"))
101 .filter(local.eq(true))
102 .load::<Community>(conn)?;
104 for ccommunity in &incorrect_communities {
105 let keypair = generate_actor_keypair()?;
106 let community_actor_id = generate_apub_endpoint(EndpointType::Community, &ccommunity.name)?;
108 let form = CommunityForm {
109 name: ccommunity.name.to_owned(),
110 title: ccommunity.title.to_owned(),
111 description: ccommunity.description.to_owned(),
112 creator_id: ccommunity.creator_id,
115 nsfw: ccommunity.nsfw,
117 actor_id: Some(community_actor_id.to_owned()),
118 local: ccommunity.local,
119 private_key: Some(keypair.private_key),
120 public_key: Some(keypair.public_key),
121 last_refreshed_at: Some(naive_now()),
123 icon: Some(ccommunity.icon.to_owned()),
124 banner: Some(ccommunity.banner.to_owned()),
127 shared_inbox_url: None,
130 Community::update(&conn, ccommunity.id, &form)?;
133 info!("{} community rows updated.", incorrect_communities.len());
138 fn post_updates_2020_04_03(conn: &PgConnection) -> Result<(), LemmyError> {
139 use lemmy_db_schema::schema::post::dsl::*;
141 info!("Running post_updates_2020_04_03");
144 let incorrect_posts = post
145 .filter(ap_id.like("http://changeme_%"))
146 .filter(local.eq(true))
147 .load::<Post>(conn)?;
149 for cpost in &incorrect_posts {
150 let apub_id = generate_apub_endpoint(EndpointType::Post, &cpost.id.to_string())?;
151 Post::update_ap_id(&conn, cpost.id, apub_id)?;
154 info!("{} post rows updated.", incorrect_posts.len());
159 fn comment_updates_2020_04_03(conn: &PgConnection) -> Result<(), LemmyError> {
160 use lemmy_db_schema::schema::comment::dsl::*;
162 info!("Running comment_updates_2020_04_03");
165 let incorrect_comments = comment
166 .filter(ap_id.like("http://changeme_%"))
167 .filter(local.eq(true))
168 .load::<Comment>(conn)?;
170 for ccomment in &incorrect_comments {
171 let apub_id = generate_apub_endpoint(EndpointType::Comment, &ccomment.id.to_string())?;
172 Comment::update_ap_id(&conn, ccomment.id, apub_id)?;
175 info!("{} comment rows updated.", incorrect_comments.len());
180 fn private_message_updates_2020_05_05(conn: &PgConnection) -> Result<(), LemmyError> {
181 use lemmy_db_schema::schema::private_message::dsl::*;
183 info!("Running private_message_updates_2020_05_05");
186 let incorrect_pms = private_message
187 .filter(ap_id.like("http://changeme_%"))
188 .filter(local.eq(true))
189 .load::<PrivateMessage>(conn)?;
191 for cpm in &incorrect_pms {
192 let apub_id = generate_apub_endpoint(EndpointType::PrivateMessage, &cpm.id.to_string())?;
193 PrivateMessage::update_ap_id(&conn, cpm.id, apub_id)?;
196 info!("{} private message rows updated.", incorrect_pms.len());
201 fn post_thumbnail_url_updates_2020_07_27(conn: &PgConnection) -> Result<(), LemmyError> {
202 use lemmy_db_schema::schema::post::dsl::*;
204 info!("Running post_thumbnail_url_updates_2020_07_27");
206 let domain_prefix = format!(
208 Settings::get().get_protocol_and_hostname(),
211 let incorrect_thumbnails = post.filter(thumbnail_url.not_like("http%"));
213 // Prepend the rows with the update
214 let res = diesel::update(incorrect_thumbnails)
218 .into_sql::<Nullable<Text>>()
219 .concat(thumbnail_url),
222 .get_results::<Post>(conn)?;
224 info!("{} Post thumbnail_url rows updated.", res.len());
229 /// We are setting inbox and follower URLs for local and remote actors alike, because for now
230 /// all federated instances are also Lemmy and use the same URL scheme.
231 fn apub_columns_2021_02_02(conn: &PgConnection) -> Result<(), LemmyError> {
232 info!("Running apub_columns_2021_02_02");
234 use lemmy_db_schema::schema::user_::dsl::*;
236 .filter(inbox_url.like("http://changeme_%"))
237 .load::<User_>(conn)?;
240 let inbox_url_ = generate_inbox_url(&u.actor_id)?;
241 let shared_inbox_url_ = generate_shared_inbox_url(&u.actor_id)?;
242 diesel::update(user_.find(u.id))
244 inbox_url.eq(inbox_url_),
245 shared_inbox_url.eq(shared_inbox_url_),
247 .get_result::<User_>(conn)?;
252 use lemmy_db_schema::schema::community::dsl::*;
253 let communities = community
254 .filter(inbox_url.like("http://changeme_%"))
255 .load::<Community>(conn)?;
257 for c in &communities {
258 let followers_url_ = generate_followers_url(&c.actor_id)?;
259 let inbox_url_ = generate_inbox_url(&c.actor_id)?;
260 let shared_inbox_url_ = generate_shared_inbox_url(&c.actor_id)?;
261 diesel::update(community.find(c.id))
263 followers_url.eq(followers_url_),
264 inbox_url.eq(inbox_url_),
265 shared_inbox_url.eq(shared_inbox_url_),
267 .get_result::<Community>(conn)?;