]> Untitled Git - lemmy.git/blob - src/code_migrations.rs
Merge branch 'main' into split_user_table
[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_apub_endpoint,
8   generate_followers_url,
9   generate_inbox_url,
10   generate_shared_inbox_url,
11   EndpointType,
12 };
13 use lemmy_db_queries::{
14   source::{comment::Comment_, post::Post_, private_message::PrivateMessage_},
15   Crud,
16 };
17 use lemmy_db_schema::{
18   naive_now,
19   source::{
20     comment::Comment,
21     community::{Community, CommunityForm},
22     person::{Person, PersonForm},
23     post::Post,
24     private_message::PrivateMessage,
25   },
26 };
27 use lemmy_utils::{apub::generate_actor_keypair, settings::structs::Settings, LemmyError};
28 use log::info;
29
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)?;
38
39   Ok(())
40 }
41
42 fn user_updates_2020_04_02(conn: &PgConnection) -> Result<(), LemmyError> {
43   use lemmy_db_schema::schema::person::dsl::*;
44
45   info!("Running user_updates_2020_04_02");
46
47   // Update the actor_id, private_key, and public_key, last_refreshed_at
48   let incorrect_persons = person
49     .filter(actor_id.like("http://changeme_%"))
50     .filter(local.eq(true))
51     .load::<Person>(conn)?;
52
53   for cperson in &incorrect_persons {
54     let keypair = generate_actor_keypair()?;
55
56     let form = PersonForm {
57       name: cperson.name.to_owned(),
58       avatar: None,
59       banner: None,
60       preferred_username: None,
61       published: None,
62       updated: None,
63       banned: None,
64       deleted: None,
65       actor_id: Some(generate_apub_endpoint(EndpointType::Person, &cperson.name)?),
66       bio: None,
67       local: None,
68       private_key: Some(Some(keypair.private_key)),
69       public_key: Some(Some(keypair.public_key)),
70       last_refreshed_at: Some(naive_now()),
71       inbox_url: None,
72       shared_inbox_url: None,
73     };
74
75     Person::update(&conn, cperson.id, &form)?;
76   }
77
78   info!("{} person rows updated.", incorrect_persons.len());
79
80   Ok(())
81 }
82
83 fn community_updates_2020_04_02(conn: &PgConnection) -> Result<(), LemmyError> {
84   use lemmy_db_schema::schema::community::dsl::*;
85
86   info!("Running community_updates_2020_04_02");
87
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)?;
93
94   for ccommunity in &incorrect_communities {
95     let keypair = generate_actor_keypair()?;
96     let community_actor_id = generate_apub_endpoint(EndpointType::Community, &ccommunity.name)?;
97
98     let form = CommunityForm {
99       name: ccommunity.name.to_owned(),
100       title: ccommunity.title.to_owned(),
101       description: ccommunity.description.to_owned(),
102       creator_id: ccommunity.creator_id,
103       removed: None,
104       deleted: None,
105       nsfw: ccommunity.nsfw,
106       updated: None,
107       actor_id: Some(community_actor_id.to_owned()),
108       local: ccommunity.local,
109       private_key: Some(keypair.private_key),
110       public_key: Some(keypair.public_key),
111       last_refreshed_at: Some(naive_now()),
112       published: None,
113       icon: Some(ccommunity.icon.to_owned()),
114       banner: Some(ccommunity.banner.to_owned()),
115       followers_url: None,
116       inbox_url: None,
117       shared_inbox_url: None,
118     };
119
120     Community::update(&conn, ccommunity.id, &form)?;
121   }
122
123   info!("{} community rows updated.", incorrect_communities.len());
124
125   Ok(())
126 }
127
128 fn post_updates_2020_04_03(conn: &PgConnection) -> Result<(), LemmyError> {
129   use lemmy_db_schema::schema::post::dsl::*;
130
131   info!("Running post_updates_2020_04_03");
132
133   // Update the ap_id
134   let incorrect_posts = post
135     .filter(ap_id.like("http://changeme_%"))
136     .filter(local.eq(true))
137     .load::<Post>(conn)?;
138
139   for cpost in &incorrect_posts {
140     let apub_id = generate_apub_endpoint(EndpointType::Post, &cpost.id.to_string())?;
141     Post::update_ap_id(&conn, cpost.id, apub_id)?;
142   }
143
144   info!("{} post rows updated.", incorrect_posts.len());
145
146   Ok(())
147 }
148
149 fn comment_updates_2020_04_03(conn: &PgConnection) -> Result<(), LemmyError> {
150   use lemmy_db_schema::schema::comment::dsl::*;
151
152   info!("Running comment_updates_2020_04_03");
153
154   // Update the ap_id
155   let incorrect_comments = comment
156     .filter(ap_id.like("http://changeme_%"))
157     .filter(local.eq(true))
158     .load::<Comment>(conn)?;
159
160   for ccomment in &incorrect_comments {
161     let apub_id = generate_apub_endpoint(EndpointType::Comment, &ccomment.id.to_string())?;
162     Comment::update_ap_id(&conn, ccomment.id, apub_id)?;
163   }
164
165   info!("{} comment rows updated.", incorrect_comments.len());
166
167   Ok(())
168 }
169
170 fn private_message_updates_2020_05_05(conn: &PgConnection) -> Result<(), LemmyError> {
171   use lemmy_db_schema::schema::private_message::dsl::*;
172
173   info!("Running private_message_updates_2020_05_05");
174
175   // Update the ap_id
176   let incorrect_pms = private_message
177     .filter(ap_id.like("http://changeme_%"))
178     .filter(local.eq(true))
179     .load::<PrivateMessage>(conn)?;
180
181   for cpm in &incorrect_pms {
182     let apub_id = generate_apub_endpoint(EndpointType::PrivateMessage, &cpm.id.to_string())?;
183     PrivateMessage::update_ap_id(&conn, cpm.id, apub_id)?;
184   }
185
186   info!("{} private message rows updated.", incorrect_pms.len());
187
188   Ok(())
189 }
190
191 fn post_thumbnail_url_updates_2020_07_27(conn: &PgConnection) -> Result<(), LemmyError> {
192   use lemmy_db_schema::schema::post::dsl::*;
193
194   info!("Running post_thumbnail_url_updates_2020_07_27");
195
196   let domain_prefix = format!(
197     "{}/pictrs/image/",
198     Settings::get().get_protocol_and_hostname(),
199   );
200
201   let incorrect_thumbnails = post.filter(thumbnail_url.not_like("http%"));
202
203   // Prepend the rows with the update
204   let res = diesel::update(incorrect_thumbnails)
205     .set(
206       thumbnail_url.eq(
207         domain_prefix
208           .into_sql::<Nullable<Text>>()
209           .concat(thumbnail_url),
210       ),
211     )
212     .get_results::<Post>(conn)?;
213
214   info!("{} Post thumbnail_url rows updated.", res.len());
215
216   Ok(())
217 }
218
219 /// We are setting inbox and follower URLs for local and remote actors alike, because for now
220 /// all federated instances are also Lemmy and use the same URL scheme.
221 fn apub_columns_2021_02_02(conn: &PgConnection) -> Result<(), LemmyError> {
222   info!("Running apub_columns_2021_02_02");
223   {
224     use lemmy_db_schema::schema::person::dsl::*;
225     let persons = person
226       .filter(inbox_url.like("http://changeme_%"))
227       .load::<Person>(conn)?;
228
229     for p in &persons {
230       let inbox_url_ = generate_inbox_url(&p.actor_id)?;
231       let shared_inbox_url_ = generate_shared_inbox_url(&p.actor_id)?;
232       diesel::update(person.find(p.id))
233         .set((
234           inbox_url.eq(inbox_url_),
235           shared_inbox_url.eq(shared_inbox_url_),
236         ))
237         .get_result::<Person>(conn)?;
238     }
239   }
240
241   {
242     use lemmy_db_schema::schema::community::dsl::*;
243     let communities = community
244       .filter(inbox_url.like("http://changeme_%"))
245       .load::<Community>(conn)?;
246
247     for c in &communities {
248       let followers_url_ = generate_followers_url(&c.actor_id)?;
249       let inbox_url_ = generate_inbox_url(&c.actor_id)?;
250       let shared_inbox_url_ = generate_shared_inbox_url(&c.actor_id)?;
251       diesel::update(community.find(c.id))
252         .set((
253           followers_url.eq(followers_url_),
254           inbox_url.eq(inbox_url_),
255           shared_inbox_url.eq(shared_inbox_url_),
256         ))
257         .get_result::<Community>(conn)?;
258     }
259   }
260
261   Ok(())
262 }