]> Untitled Git - lemmy.git/blob - src/code_migrations.rs
Moving matrix_user_id to person table. #1438
[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       matrix_user_id: None,
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(conn: &PgConnection) -> Result<(), LemmyError> {
85   use lemmy_db_schema::schema::community::dsl::*;
86
87   info!("Running community_updates_2020_04_02");
88
89   // Update the actor_id, private_key, and public_key, last_refreshed_at
90   let incorrect_communities = community
91     .filter(actor_id.like("http://changeme_%"))
92     .filter(local.eq(true))
93     .load::<Community>(conn)?;
94
95   for ccommunity in &incorrect_communities {
96     let keypair = generate_actor_keypair()?;
97     let community_actor_id = generate_apub_endpoint(EndpointType::Community, &ccommunity.name)?;
98
99     let form = CommunityForm {
100       name: ccommunity.name.to_owned(),
101       title: ccommunity.title.to_owned(),
102       description: ccommunity.description.to_owned(),
103       creator_id: ccommunity.creator_id,
104       removed: None,
105       deleted: None,
106       nsfw: ccommunity.nsfw,
107       updated: None,
108       actor_id: Some(community_actor_id.to_owned()),
109       local: ccommunity.local,
110       private_key: Some(keypair.private_key),
111       public_key: Some(keypair.public_key),
112       last_refreshed_at: Some(naive_now()),
113       published: None,
114       icon: Some(ccommunity.icon.to_owned()),
115       banner: Some(ccommunity.banner.to_owned()),
116       followers_url: None,
117       inbox_url: None,
118       shared_inbox_url: None,
119     };
120
121     Community::update(&conn, ccommunity.id, &form)?;
122   }
123
124   info!("{} community rows updated.", incorrect_communities.len());
125
126   Ok(())
127 }
128
129 fn post_updates_2020_04_03(conn: &PgConnection) -> Result<(), LemmyError> {
130   use lemmy_db_schema::schema::post::dsl::*;
131
132   info!("Running post_updates_2020_04_03");
133
134   // Update the ap_id
135   let incorrect_posts = post
136     .filter(ap_id.like("http://changeme_%"))
137     .filter(local.eq(true))
138     .load::<Post>(conn)?;
139
140   for cpost in &incorrect_posts {
141     let apub_id = generate_apub_endpoint(EndpointType::Post, &cpost.id.to_string())?;
142     Post::update_ap_id(&conn, cpost.id, apub_id)?;
143   }
144
145   info!("{} post rows updated.", incorrect_posts.len());
146
147   Ok(())
148 }
149
150 fn comment_updates_2020_04_03(conn: &PgConnection) -> Result<(), LemmyError> {
151   use lemmy_db_schema::schema::comment::dsl::*;
152
153   info!("Running comment_updates_2020_04_03");
154
155   // Update the ap_id
156   let incorrect_comments = comment
157     .filter(ap_id.like("http://changeme_%"))
158     .filter(local.eq(true))
159     .load::<Comment>(conn)?;
160
161   for ccomment in &incorrect_comments {
162     let apub_id = generate_apub_endpoint(EndpointType::Comment, &ccomment.id.to_string())?;
163     Comment::update_ap_id(&conn, ccomment.id, apub_id)?;
164   }
165
166   info!("{} comment rows updated.", incorrect_comments.len());
167
168   Ok(())
169 }
170
171 fn private_message_updates_2020_05_05(conn: &PgConnection) -> Result<(), LemmyError> {
172   use lemmy_db_schema::schema::private_message::dsl::*;
173
174   info!("Running private_message_updates_2020_05_05");
175
176   // Update the ap_id
177   let incorrect_pms = private_message
178     .filter(ap_id.like("http://changeme_%"))
179     .filter(local.eq(true))
180     .load::<PrivateMessage>(conn)?;
181
182   for cpm in &incorrect_pms {
183     let apub_id = generate_apub_endpoint(EndpointType::PrivateMessage, &cpm.id.to_string())?;
184     PrivateMessage::update_ap_id(&conn, cpm.id, apub_id)?;
185   }
186
187   info!("{} private message rows updated.", incorrect_pms.len());
188
189   Ok(())
190 }
191
192 fn post_thumbnail_url_updates_2020_07_27(conn: &PgConnection) -> Result<(), LemmyError> {
193   use lemmy_db_schema::schema::post::dsl::*;
194
195   info!("Running post_thumbnail_url_updates_2020_07_27");
196
197   let domain_prefix = format!(
198     "{}/pictrs/image/",
199     Settings::get().get_protocol_and_hostname(),
200   );
201
202   let incorrect_thumbnails = post.filter(thumbnail_url.not_like("http%"));
203
204   // Prepend the rows with the update
205   let res = diesel::update(incorrect_thumbnails)
206     .set(
207       thumbnail_url.eq(
208         domain_prefix
209           .into_sql::<Nullable<Text>>()
210           .concat(thumbnail_url),
211       ),
212     )
213     .get_results::<Post>(conn)?;
214
215   info!("{} Post thumbnail_url rows updated.", res.len());
216
217   Ok(())
218 }
219
220 /// We are setting inbox and follower URLs for local and remote actors alike, because for now
221 /// all federated instances are also Lemmy and use the same URL scheme.
222 fn apub_columns_2021_02_02(conn: &PgConnection) -> Result<(), LemmyError> {
223   info!("Running apub_columns_2021_02_02");
224   {
225     use lemmy_db_schema::schema::person::dsl::*;
226     let persons = person
227       .filter(inbox_url.like("http://changeme_%"))
228       .load::<Person>(conn)?;
229
230     for p in &persons {
231       let inbox_url_ = generate_inbox_url(&p.actor_id)?;
232       let shared_inbox_url_ = generate_shared_inbox_url(&p.actor_id)?;
233       diesel::update(person.find(p.id))
234         .set((
235           inbox_url.eq(inbox_url_),
236           shared_inbox_url.eq(shared_inbox_url_),
237         ))
238         .get_result::<Person>(conn)?;
239     }
240   }
241
242   {
243     use lemmy_db_schema::schema::community::dsl::*;
244     let communities = community
245       .filter(inbox_url.like("http://changeme_%"))
246       .load::<Community>(conn)?;
247
248     for c in &communities {
249       let followers_url_ = generate_followers_url(&c.actor_id)?;
250       let inbox_url_ = generate_inbox_url(&c.actor_id)?;
251       let shared_inbox_url_ = generate_shared_inbox_url(&c.actor_id)?;
252       diesel::update(community.find(c.id))
253         .set((
254           followers_url.eq(followers_url_),
255           inbox_url.eq(inbox_url_),
256           shared_inbox_url.eq(shared_inbox_url_),
257         ))
258         .get_result::<Community>(conn)?;
259     }
260   }
261
262   Ok(())
263 }