1 // This is for db migrations that require code
3 sql_types::{Nullable, Text},
7 generate_followers_url,
9 generate_local_apub_endpoint,
10 generate_shared_inbox_url,
13 use lemmy_db_schema::{
17 community::{Community, CommunityForm},
18 person::{Person, PersonForm},
20 private_message::PrivateMessage,
24 use lemmy_utils::{apub::generate_actor_keypair, LemmyError};
27 pub fn run_advanced_migrations(
29 protocol_and_hostname: &str,
30 ) -> Result<(), LemmyError> {
31 user_updates_2020_04_02(conn, protocol_and_hostname)?;
32 community_updates_2020_04_02(conn, protocol_and_hostname)?;
33 post_updates_2020_04_03(conn, protocol_and_hostname)?;
34 comment_updates_2020_04_03(conn, protocol_and_hostname)?;
35 private_message_updates_2020_05_05(conn, protocol_and_hostname)?;
36 post_thumbnail_url_updates_2020_07_27(conn, protocol_and_hostname)?;
37 apub_columns_2021_02_02(conn)?;
42 fn user_updates_2020_04_02(
44 protocol_and_hostname: &str,
45 ) -> Result<(), LemmyError> {
46 use lemmy_db_schema::schema::person::dsl::*;
48 info!("Running user_updates_2020_04_02");
50 // Update the actor_id, private_key, and public_key, last_refreshed_at
51 let incorrect_persons = person
52 .filter(actor_id.like("http://changeme_%"))
53 .filter(local.eq(true))
54 .load::<Person>(conn)?;
56 for cperson in &incorrect_persons {
57 let keypair = generate_actor_keypair()?;
59 let form = PersonForm {
60 name: cperson.name.to_owned(),
61 actor_id: Some(generate_local_apub_endpoint(
64 protocol_and_hostname,
66 private_key: Some(Some(keypair.private_key)),
67 public_key: keypair.public_key,
68 last_refreshed_at: Some(naive_now()),
69 ..PersonForm::default()
72 Person::update(conn, cperson.id, &form)?;
75 info!("{} person rows updated.", incorrect_persons.len());
80 fn community_updates_2020_04_02(
82 protocol_and_hostname: &str,
83 ) -> Result<(), LemmyError> {
84 use lemmy_db_schema::schema::community::dsl::*;
86 info!("Running community_updates_2020_04_02");
88 // Update the actor_id, private_key, and public_key, last_refreshed_at
89 let incorrect_communities = community
90 .filter(actor_id.like("http://changeme_%"))
91 .filter(local.eq(true))
92 .load::<Community>(conn)?;
94 for ccommunity in &incorrect_communities {
95 let keypair = generate_actor_keypair()?;
96 let community_actor_id = generate_local_apub_endpoint(
97 EndpointType::Community,
99 protocol_and_hostname,
102 let form = CommunityForm {
103 name: ccommunity.name.to_owned(),
104 title: ccommunity.title.to_owned(),
105 description: ccommunity.description.to_owned(),
110 actor_id: Some(community_actor_id.to_owned()),
111 local: Some(ccommunity.local),
112 private_key: Some(Some(keypair.private_key)),
113 public_key: keypair.public_key,
114 last_refreshed_at: Some(naive_now()),
116 icon: Some(ccommunity.icon.to_owned()),
117 banner: Some(ccommunity.banner.to_owned()),
120 shared_inbox_url: None,
123 Community::update(conn, ccommunity.id, &form)?;
126 info!("{} community rows updated.", incorrect_communities.len());
131 fn post_updates_2020_04_03(
133 protocol_and_hostname: &str,
134 ) -> Result<(), LemmyError> {
135 use lemmy_db_schema::schema::post::dsl::*;
137 info!("Running post_updates_2020_04_03");
140 let incorrect_posts = post
141 .filter(ap_id.like("http://changeme_%"))
142 .filter(local.eq(true))
143 .load::<Post>(conn)?;
145 for cpost in &incorrect_posts {
146 let apub_id = generate_local_apub_endpoint(
148 &cpost.id.to_string(),
149 protocol_and_hostname,
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(
161 protocol_and_hostname: &str,
162 ) -> Result<(), LemmyError> {
163 use lemmy_db_schema::schema::comment::dsl::*;
165 info!("Running comment_updates_2020_04_03");
168 let incorrect_comments = comment
169 .filter(ap_id.like("http://changeme_%"))
170 .filter(local.eq(true))
171 .load::<Comment>(conn)?;
173 for ccomment in &incorrect_comments {
174 let apub_id = generate_local_apub_endpoint(
175 EndpointType::Comment,
176 &ccomment.id.to_string(),
177 protocol_and_hostname,
179 Comment::update_ap_id(conn, ccomment.id, apub_id)?;
182 info!("{} comment rows updated.", incorrect_comments.len());
187 fn private_message_updates_2020_05_05(
189 protocol_and_hostname: &str,
190 ) -> Result<(), LemmyError> {
191 use lemmy_db_schema::schema::private_message::dsl::*;
193 info!("Running private_message_updates_2020_05_05");
196 let incorrect_pms = private_message
197 .filter(ap_id.like("http://changeme_%"))
198 .filter(local.eq(true))
199 .load::<PrivateMessage>(conn)?;
201 for cpm in &incorrect_pms {
202 let apub_id = generate_local_apub_endpoint(
203 EndpointType::PrivateMessage,
205 protocol_and_hostname,
207 PrivateMessage::update_ap_id(conn, cpm.id, apub_id)?;
210 info!("{} private message rows updated.", incorrect_pms.len());
215 fn post_thumbnail_url_updates_2020_07_27(
217 protocol_and_hostname: &str,
218 ) -> Result<(), LemmyError> {
219 use lemmy_db_schema::schema::post::dsl::*;
221 info!("Running post_thumbnail_url_updates_2020_07_27");
223 let domain_prefix = format!("{}/pictrs/image/", protocol_and_hostname,);
225 let incorrect_thumbnails = post.filter(thumbnail_url.not_like("http%"));
227 // Prepend the rows with the update
228 let res = diesel::update(incorrect_thumbnails)
232 .into_sql::<Nullable<Text>>()
233 .concat(thumbnail_url),
236 .get_results::<Post>(conn)?;
238 info!("{} Post thumbnail_url rows updated.", res.len());
243 /// We are setting inbox and follower URLs for local and remote actors alike, because for now
244 /// all federated instances are also Lemmy and use the same URL scheme.
245 fn apub_columns_2021_02_02(conn: &PgConnection) -> Result<(), LemmyError> {
246 info!("Running apub_columns_2021_02_02");
248 use lemmy_db_schema::schema::person::dsl::*;
250 .filter(inbox_url.like("http://changeme_%"))
251 .load::<Person>(conn)?;
254 let inbox_url_ = generate_inbox_url(&p.actor_id)?;
255 let shared_inbox_url_ = generate_shared_inbox_url(&p.actor_id)?;
256 diesel::update(person.find(p.id))
258 inbox_url.eq(inbox_url_),
259 shared_inbox_url.eq(shared_inbox_url_),
261 .get_result::<Person>(conn)?;
266 use lemmy_db_schema::schema::community::dsl::*;
267 let communities = community
268 .filter(inbox_url.like("http://changeme_%"))
269 .load::<Community>(conn)?;
271 for c in &communities {
272 let followers_url_ = generate_followers_url(&c.actor_id)?;
273 let inbox_url_ = generate_inbox_url(&c.actor_id)?;
274 let shared_inbox_url_ = generate_shared_inbox_url(&c.actor_id)?;
275 diesel::update(community.find(c.id))
277 followers_url.eq(followers_url_),
278 inbox_url.eq(inbox_url_),
279 shared_inbox_url.eq(shared_inbox_url_),
281 .get_result::<Community>(conn)?;