]> Untitled Git - lemmy.git/blob - src/code_migrations.rs
4c0becfe951202dc7b55ed3fe48bfb4947dd7380
[lemmy.git] / src / code_migrations.rs
1 // This is for db migrations that require code
2 use diesel::{
3   sql_types::{Nullable, Text},
4   *,
5 };
6 use lemmy_apub::{
7   generate_followers_url,
8   generate_inbox_url,
9   generate_local_apub_endpoint,
10   generate_shared_inbox_url,
11   generate_site_inbox_url,
12   EndpointType,
13 };
14 use lemmy_db_schema::{
15   naive_now,
16   source::{
17     comment::Comment,
18     community::{Community, CommunityForm},
19     person::{Person, PersonForm},
20     post::Post,
21     private_message::PrivateMessage,
22     site::{Site, SiteForm},
23   },
24   traits::Crud,
25 };
26 use lemmy_utils::{apub::generate_actor_keypair, LemmyError};
27 use tracing::info;
28 use url::Url;
29
30 pub fn run_advanced_migrations(
31   conn: &PgConnection,
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)?;
42
43   Ok(())
44 }
45
46 fn user_updates_2020_04_02(
47   conn: &PgConnection,
48   protocol_and_hostname: &str,
49 ) -> Result<(), LemmyError> {
50   use lemmy_db_schema::schema::person::dsl::*;
51
52   info!("Running user_updates_2020_04_02");
53
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)?;
59
60   for cperson in &incorrect_persons {
61     let keypair = generate_actor_keypair()?;
62
63     let form = PersonForm {
64       name: cperson.name.to_owned(),
65       actor_id: Some(generate_local_apub_endpoint(
66         EndpointType::Person,
67         &cperson.name,
68         protocol_and_hostname,
69       )?),
70       private_key: Some(Some(keypair.private_key)),
71       public_key: keypair.public_key,
72       last_refreshed_at: Some(naive_now()),
73       ..PersonForm::default()
74     };
75
76     Person::update(conn, cperson.id, &form)?;
77   }
78
79   info!("{} person rows updated.", incorrect_persons.len());
80
81   Ok(())
82 }
83
84 fn community_updates_2020_04_02(
85   conn: &PgConnection,
86   protocol_and_hostname: &str,
87 ) -> Result<(), LemmyError> {
88   use lemmy_db_schema::schema::community::dsl::*;
89
90   info!("Running community_updates_2020_04_02");
91
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)?;
97
98   for ccommunity in &incorrect_communities {
99     let keypair = generate_actor_keypair()?;
100     let community_actor_id = generate_local_apub_endpoint(
101       EndpointType::Community,
102       &ccommunity.name,
103       protocol_and_hostname,
104     )?;
105
106     let form = CommunityForm {
107       name: ccommunity.name.to_owned(),
108       title: ccommunity.title.to_owned(),
109       description: ccommunity.description.to_owned(),
110       removed: None,
111       deleted: None,
112       nsfw: None,
113       updated: None,
114       hidden: Some(false),
115       actor_id: Some(community_actor_id.to_owned()),
116       local: Some(ccommunity.local),
117       private_key: Some(Some(keypair.private_key)),
118       public_key: keypair.public_key,
119       last_refreshed_at: Some(naive_now()),
120       published: None,
121       icon: Some(ccommunity.icon.to_owned()),
122       banner: Some(ccommunity.banner.to_owned()),
123       followers_url: None,
124       inbox_url: None,
125       shared_inbox_url: None,
126     };
127
128     Community::update(conn, ccommunity.id, &form)?;
129   }
130
131   info!("{} community rows updated.", incorrect_communities.len());
132
133   Ok(())
134 }
135
136 fn post_updates_2020_04_03(
137   conn: &PgConnection,
138   protocol_and_hostname: &str,
139 ) -> Result<(), LemmyError> {
140   use lemmy_db_schema::schema::post::dsl::*;
141
142   info!("Running post_updates_2020_04_03");
143
144   // Update the ap_id
145   let incorrect_posts = post
146     .filter(ap_id.like("http://changeme_%"))
147     .filter(local.eq(true))
148     .load::<Post>(conn)?;
149
150   for cpost in &incorrect_posts {
151     let apub_id = generate_local_apub_endpoint(
152       EndpointType::Post,
153       &cpost.id.to_string(),
154       protocol_and_hostname,
155     )?;
156     Post::update_ap_id(conn, cpost.id, apub_id)?;
157   }
158
159   info!("{} post rows updated.", incorrect_posts.len());
160
161   Ok(())
162 }
163
164 fn comment_updates_2020_04_03(
165   conn: &PgConnection,
166   protocol_and_hostname: &str,
167 ) -> Result<(), LemmyError> {
168   use lemmy_db_schema::schema::comment::dsl::*;
169
170   info!("Running comment_updates_2020_04_03");
171
172   // Update the ap_id
173   let incorrect_comments = comment
174     .filter(ap_id.like("http://changeme_%"))
175     .filter(local.eq(true))
176     .load::<Comment>(conn)?;
177
178   for ccomment in &incorrect_comments {
179     let apub_id = generate_local_apub_endpoint(
180       EndpointType::Comment,
181       &ccomment.id.to_string(),
182       protocol_and_hostname,
183     )?;
184     Comment::update_ap_id(conn, ccomment.id, apub_id)?;
185   }
186
187   info!("{} comment rows updated.", incorrect_comments.len());
188
189   Ok(())
190 }
191
192 fn private_message_updates_2020_05_05(
193   conn: &PgConnection,
194   protocol_and_hostname: &str,
195 ) -> Result<(), LemmyError> {
196   use lemmy_db_schema::schema::private_message::dsl::*;
197
198   info!("Running private_message_updates_2020_05_05");
199
200   // Update the ap_id
201   let incorrect_pms = private_message
202     .filter(ap_id.like("http://changeme_%"))
203     .filter(local.eq(true))
204     .load::<PrivateMessage>(conn)?;
205
206   for cpm in &incorrect_pms {
207     let apub_id = generate_local_apub_endpoint(
208       EndpointType::PrivateMessage,
209       &cpm.id.to_string(),
210       protocol_and_hostname,
211     )?;
212     PrivateMessage::update_ap_id(conn, cpm.id, apub_id)?;
213   }
214
215   info!("{} private message rows updated.", incorrect_pms.len());
216
217   Ok(())
218 }
219
220 fn post_thumbnail_url_updates_2020_07_27(
221   conn: &PgConnection,
222   protocol_and_hostname: &str,
223 ) -> Result<(), LemmyError> {
224   use lemmy_db_schema::schema::post::dsl::*;
225
226   info!("Running post_thumbnail_url_updates_2020_07_27");
227
228   let domain_prefix = format!("{}/pictrs/image/", protocol_and_hostname,);
229
230   let incorrect_thumbnails = post.filter(thumbnail_url.not_like("http%"));
231
232   // Prepend the rows with the update
233   let res = diesel::update(incorrect_thumbnails)
234     .set(
235       thumbnail_url.eq(
236         domain_prefix
237           .into_sql::<Nullable<Text>>()
238           .concat(thumbnail_url),
239       ),
240     )
241     .get_results::<Post>(conn)?;
242
243   info!("{} Post thumbnail_url rows updated.", res.len());
244
245   Ok(())
246 }
247
248 /// We are setting inbox and follower URLs for local and remote actors alike, because for now
249 /// all federated instances are also Lemmy and use the same URL scheme.
250 fn apub_columns_2021_02_02(conn: &PgConnection) -> Result<(), LemmyError> {
251   info!("Running apub_columns_2021_02_02");
252   {
253     use lemmy_db_schema::schema::person::dsl::*;
254     let persons = person
255       .filter(inbox_url.like("http://changeme_%"))
256       .load::<Person>(conn)?;
257
258     for p in &persons {
259       let inbox_url_ = generate_inbox_url(&p.actor_id)?;
260       let shared_inbox_url_ = generate_shared_inbox_url(&p.actor_id)?;
261       diesel::update(person.find(p.id))
262         .set((
263           inbox_url.eq(inbox_url_),
264           shared_inbox_url.eq(shared_inbox_url_),
265         ))
266         .get_result::<Person>(conn)?;
267     }
268   }
269
270   {
271     use lemmy_db_schema::schema::community::dsl::*;
272     let communities = community
273       .filter(inbox_url.like("http://changeme_%"))
274       .load::<Community>(conn)?;
275
276     for c in &communities {
277       let followers_url_ = generate_followers_url(&c.actor_id)?;
278       let inbox_url_ = generate_inbox_url(&c.actor_id)?;
279       let shared_inbox_url_ = generate_shared_inbox_url(&c.actor_id)?;
280       diesel::update(community.find(c.id))
281         .set((
282           followers_url.eq(followers_url_),
283           inbox_url.eq(inbox_url_),
284           shared_inbox_url.eq(shared_inbox_url_),
285         ))
286         .get_result::<Community>(conn)?;
287     }
288   }
289
290   Ok(())
291 }
292
293 /// Site object turns into an actor, so that things like instance description can be federated. This
294 /// means we need to add actor columns to the site table, and initialize them with correct values.
295 /// Before this point, there is only a single value in the site table which refers to the local
296 /// Lemmy instance, so thats all we need to update.
297 fn instance_actor_2022_01_28(
298   conn: &PgConnection,
299   protocol_and_hostname: &str,
300 ) -> Result<(), LemmyError> {
301   info!("Running instance_actor_2021_09_29");
302   if let Ok(site) = Site::read_local_site(conn) {
303     let key_pair = generate_actor_keypair()?;
304     let actor_id = Url::parse(protocol_and_hostname)?;
305     let site_form = SiteForm {
306       name: site.name,
307       actor_id: Some(actor_id.clone().into()),
308       last_refreshed_at: Some(naive_now()),
309       inbox_url: Some(generate_site_inbox_url(&actor_id.into())?),
310       private_key: Some(Some(key_pair.private_key)),
311       public_key: Some(key_pair.public_key),
312       ..Default::default()
313     };
314     Site::update(conn, site.id, &site_form)?;
315   }
316   Ok(())
317 }