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