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,
11 generate_site_inbox_url,
14 use lemmy_db_schema::{
18 community::{Community, CommunityForm},
19 person::{Person, PersonForm},
21 private_message::PrivateMessage,
22 site::{Site, SiteForm},
26 use lemmy_utils::{apub::generate_actor_keypair, LemmyError};
30 pub fn run_advanced_migrations(
32 protocol_and_hostname: &str,
33 ) -> Result<(), LemmyError> {
34 user_updates_2020_04_02(conn, protocol_and_hostname)?;
35 community_updates_2020_04_02(conn, protocol_and_hostname)?;
36 post_updates_2020_04_03(conn, protocol_and_hostname)?;
37 comment_updates_2020_04_03(conn, protocol_and_hostname)?;
38 private_message_updates_2020_05_05(conn, protocol_and_hostname)?;
39 post_thumbnail_url_updates_2020_07_27(conn, protocol_and_hostname)?;
40 apub_columns_2021_02_02(conn)?;
41 instance_actor_2022_01_28(conn, protocol_and_hostname)?;
46 fn user_updates_2020_04_02(
48 protocol_and_hostname: &str,
49 ) -> Result<(), LemmyError> {
50 use lemmy_db_schema::schema::person::dsl::*;
52 info!("Running user_updates_2020_04_02");
54 // Update the actor_id, private_key, and public_key, last_refreshed_at
55 let incorrect_persons = person
56 .filter(actor_id.like("http://changeme_%"))
57 .filter(local.eq(true))
58 .load::<Person>(conn)?;
60 for cperson in &incorrect_persons {
61 let keypair = generate_actor_keypair()?;
63 let form = PersonForm {
64 name: cperson.name.to_owned(),
65 actor_id: Some(generate_local_apub_endpoint(
68 protocol_and_hostname,
70 private_key: Some(Some(keypair.private_key)),
71 public_key: keypair.public_key,
72 last_refreshed_at: Some(naive_now()),
73 ..PersonForm::default()
76 Person::update(conn, cperson.id, &form)?;
79 info!("{} person rows updated.", incorrect_persons.len());
84 fn community_updates_2020_04_02(
86 protocol_and_hostname: &str,
87 ) -> Result<(), LemmyError> {
88 use lemmy_db_schema::schema::community::dsl::*;
90 info!("Running community_updates_2020_04_02");
92 // Update the actor_id, private_key, and public_key, last_refreshed_at
93 let incorrect_communities = community
94 .filter(actor_id.like("http://changeme_%"))
95 .filter(local.eq(true))
96 .load::<Community>(conn)?;
98 for ccommunity in &incorrect_communities {
99 let keypair = generate_actor_keypair()?;
100 let community_actor_id = generate_local_apub_endpoint(
101 EndpointType::Community,
103 protocol_and_hostname,
106 let form = CommunityForm {
107 name: ccommunity.name.to_owned(),
108 title: ccommunity.title.to_owned(),
109 description: ccommunity.description.to_owned(),
114 actor_id: Some(community_actor_id.to_owned()),
115 local: Some(ccommunity.local),
116 private_key: Some(Some(keypair.private_key)),
117 public_key: keypair.public_key,
118 last_refreshed_at: Some(naive_now()),
120 icon: Some(ccommunity.icon.to_owned()),
121 banner: Some(ccommunity.banner.to_owned()),
124 shared_inbox_url: None,
127 Community::update(conn, ccommunity.id, &form)?;
130 info!("{} community rows updated.", incorrect_communities.len());
135 fn post_updates_2020_04_03(
137 protocol_and_hostname: &str,
138 ) -> 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_local_apub_endpoint(
152 &cpost.id.to_string(),
153 protocol_and_hostname,
155 Post::update_ap_id(conn, cpost.id, apub_id)?;
158 info!("{} post rows updated.", incorrect_posts.len());
163 fn comment_updates_2020_04_03(
165 protocol_and_hostname: &str,
166 ) -> Result<(), LemmyError> {
167 use lemmy_db_schema::schema::comment::dsl::*;
169 info!("Running comment_updates_2020_04_03");
172 let incorrect_comments = comment
173 .filter(ap_id.like("http://changeme_%"))
174 .filter(local.eq(true))
175 .load::<Comment>(conn)?;
177 for ccomment in &incorrect_comments {
178 let apub_id = generate_local_apub_endpoint(
179 EndpointType::Comment,
180 &ccomment.id.to_string(),
181 protocol_and_hostname,
183 Comment::update_ap_id(conn, ccomment.id, apub_id)?;
186 info!("{} comment rows updated.", incorrect_comments.len());
191 fn private_message_updates_2020_05_05(
193 protocol_and_hostname: &str,
194 ) -> Result<(), LemmyError> {
195 use lemmy_db_schema::schema::private_message::dsl::*;
197 info!("Running private_message_updates_2020_05_05");
200 let incorrect_pms = private_message
201 .filter(ap_id.like("http://changeme_%"))
202 .filter(local.eq(true))
203 .load::<PrivateMessage>(conn)?;
205 for cpm in &incorrect_pms {
206 let apub_id = generate_local_apub_endpoint(
207 EndpointType::PrivateMessage,
209 protocol_and_hostname,
211 PrivateMessage::update_ap_id(conn, cpm.id, apub_id)?;
214 info!("{} private message rows updated.", incorrect_pms.len());
219 fn post_thumbnail_url_updates_2020_07_27(
221 protocol_and_hostname: &str,
222 ) -> Result<(), LemmyError> {
223 use lemmy_db_schema::schema::post::dsl::*;
225 info!("Running post_thumbnail_url_updates_2020_07_27");
227 let domain_prefix = format!("{}/pictrs/image/", protocol_and_hostname,);
229 let incorrect_thumbnails = post.filter(thumbnail_url.not_like("http%"));
231 // Prepend the rows with the update
232 let res = diesel::update(incorrect_thumbnails)
236 .into_sql::<Nullable<Text>>()
237 .concat(thumbnail_url),
240 .get_results::<Post>(conn)?;
242 info!("{} Post thumbnail_url rows updated.", res.len());
247 /// We are setting inbox and follower URLs for local and remote actors alike, because for now
248 /// all federated instances are also Lemmy and use the same URL scheme.
249 fn apub_columns_2021_02_02(conn: &PgConnection) -> Result<(), LemmyError> {
250 info!("Running apub_columns_2021_02_02");
252 use lemmy_db_schema::schema::person::dsl::*;
254 .filter(inbox_url.like("http://changeme_%"))
255 .load::<Person>(conn)?;
258 let inbox_url_ = generate_inbox_url(&p.actor_id)?;
259 let shared_inbox_url_ = generate_shared_inbox_url(&p.actor_id)?;
260 diesel::update(person.find(p.id))
262 inbox_url.eq(inbox_url_),
263 shared_inbox_url.eq(shared_inbox_url_),
265 .get_result::<Person>(conn)?;
270 use lemmy_db_schema::schema::community::dsl::*;
271 let communities = community
272 .filter(inbox_url.like("http://changeme_%"))
273 .load::<Community>(conn)?;
275 for c in &communities {
276 let followers_url_ = generate_followers_url(&c.actor_id)?;
277 let inbox_url_ = generate_inbox_url(&c.actor_id)?;
278 let shared_inbox_url_ = generate_shared_inbox_url(&c.actor_id)?;
279 diesel::update(community.find(c.id))
281 followers_url.eq(followers_url_),
282 inbox_url.eq(inbox_url_),
283 shared_inbox_url.eq(shared_inbox_url_),
285 .get_result::<Community>(conn)?;
292 /// Site object turns into an actor, so that things like instance description can be federated. This
293 /// means we need to add actor columns to the site table, and initialize them with correct values.
294 /// Before this point, there is only a single value in the site table which refers to the local
295 /// Lemmy instance, so thats all we need to update.
296 fn instance_actor_2022_01_28(
298 protocol_and_hostname: &str,
299 ) -> Result<(), LemmyError> {
300 info!("Running instance_actor_2021_09_29");
301 if let Ok(site) = Site::read_local_site(conn) {
302 let key_pair = generate_actor_keypair()?;
303 let actor_id = Url::parse(protocol_and_hostname)?;
304 let site_form = SiteForm {
306 actor_id: Some(actor_id.clone().into()),
307 last_refreshed_at: Some(naive_now()),
308 inbox_url: Some(generate_site_inbox_url(&actor_id.into())?),
309 private_key: Some(Some(key_pair.private_key)),
310 public_key: Some(key_pair.public_key),
313 Site::update(conn, site.id, &site_form)?;