]> Untitled Git - lemmy.git/blob - server/src/code_migrations.rs
Merge remote-tracking branch 'weblate/main' into main
[lemmy.git] / server / src / code_migrations.rs
1 // This is for db migrations that require code
2 use crate::LemmyError;
3 use diesel::{
4   sql_types::{Nullable, Text},
5   *,
6 };
7 use lemmy_db::{
8   comment::Comment,
9   community::{Community, CommunityForm},
10   naive_now,
11   post::Post,
12   private_message::PrivateMessage,
13   user::{UserForm, User_},
14   Crud,
15 };
16 use lemmy_utils::{
17   generate_actor_keypair,
18   get_apub_protocol_string,
19   make_apub_endpoint,
20   settings::Settings,
21   EndpointType,
22 };
23 use log::info;
24
25 pub fn run_advanced_migrations(conn: &PgConnection) -> Result<(), LemmyError> {
26   user_updates_2020_04_02(&conn)?;
27   community_updates_2020_04_02(&conn)?;
28   post_updates_2020_04_03(&conn)?;
29   comment_updates_2020_04_03(&conn)?;
30   private_message_updates_2020_05_05(&conn)?;
31   post_thumbnail_url_updates_2020_07_27(&conn)?;
32
33   Ok(())
34 }
35
36 fn user_updates_2020_04_02(conn: &PgConnection) -> Result<(), LemmyError> {
37   use lemmy_db::schema::user_::dsl::*;
38
39   info!("Running user_updates_2020_04_02");
40
41   // Update the actor_id, private_key, and public_key, last_refreshed_at
42   let incorrect_users = user_
43     .filter(actor_id.like("changeme_%"))
44     .filter(local.eq(true))
45     .load::<User_>(conn)?;
46
47   sql_query("alter table user_ disable trigger refresh_user").execute(conn)?;
48
49   for cuser in &incorrect_users {
50     let keypair = generate_actor_keypair()?;
51
52     let form = UserForm {
53       name: cuser.name.to_owned(),
54       email: cuser.email.to_owned(),
55       matrix_user_id: cuser.matrix_user_id.to_owned(),
56       avatar: Some(cuser.avatar.to_owned()),
57       banner: Some(cuser.banner.to_owned()),
58       password_encrypted: cuser.password_encrypted.to_owned(),
59       preferred_username: cuser.preferred_username.to_owned(),
60       updated: None,
61       admin: cuser.admin,
62       banned: cuser.banned,
63       show_nsfw: cuser.show_nsfw,
64       theme: cuser.theme.to_owned(),
65       default_sort_type: cuser.default_sort_type,
66       default_listing_type: cuser.default_listing_type,
67       lang: cuser.lang.to_owned(),
68       show_avatars: cuser.show_avatars,
69       send_notifications_to_email: cuser.send_notifications_to_email,
70       actor_id: make_apub_endpoint(EndpointType::User, &cuser.name).to_string(),
71       bio: cuser.bio.to_owned(),
72       local: cuser.local,
73       private_key: Some(keypair.private_key),
74       public_key: Some(keypair.public_key),
75       last_refreshed_at: Some(naive_now()),
76     };
77
78     User_::update(&conn, cuser.id, &form)?;
79   }
80
81   sql_query("alter table user_ enable trigger refresh_user").execute(conn)?;
82
83   info!("{} user rows updated.", incorrect_users.len());
84
85   Ok(())
86 }
87
88 fn community_updates_2020_04_02(conn: &PgConnection) -> Result<(), LemmyError> {
89   use lemmy_db::schema::community::dsl::*;
90
91   info!("Running community_updates_2020_04_02");
92
93   // Update the actor_id, private_key, and public_key, last_refreshed_at
94   let incorrect_communities = community
95     .filter(actor_id.like("changeme_%"))
96     .filter(local.eq(true))
97     .load::<Community>(conn)?;
98
99   sql_query("alter table community disable trigger refresh_community").execute(conn)?;
100
101   for ccommunity in &incorrect_communities {
102     let keypair = generate_actor_keypair()?;
103
104     let form = CommunityForm {
105       name: ccommunity.name.to_owned(),
106       title: ccommunity.title.to_owned(),
107       description: ccommunity.description.to_owned(),
108       category_id: ccommunity.category_id,
109       creator_id: ccommunity.creator_id,
110       removed: None,
111       deleted: None,
112       nsfw: ccommunity.nsfw,
113       updated: None,
114       actor_id: make_apub_endpoint(EndpointType::Community, &ccommunity.name).to_string(),
115       local: ccommunity.local,
116       private_key: Some(keypair.private_key),
117       public_key: Some(keypair.public_key),
118       last_refreshed_at: Some(naive_now()),
119       published: None,
120       icon: Some(ccommunity.icon.to_owned()),
121       banner: Some(ccommunity.banner.to_owned()),
122     };
123
124     Community::update(&conn, ccommunity.id, &form)?;
125   }
126
127   sql_query("alter table community enable trigger refresh_community").execute(conn)?;
128
129   info!("{} community rows updated.", incorrect_communities.len());
130
131   Ok(())
132 }
133
134 fn post_updates_2020_04_03(conn: &PgConnection) -> Result<(), LemmyError> {
135   use lemmy_db::schema::post::dsl::*;
136
137   info!("Running post_updates_2020_04_03");
138
139   // Update the ap_id
140   let incorrect_posts = post
141     .filter(ap_id.eq("http://fake.com"))
142     .filter(local.eq(true))
143     .load::<Post>(conn)?;
144
145   sql_query("alter table post disable trigger refresh_post").execute(conn)?;
146
147   for cpost in &incorrect_posts {
148     let apub_id = make_apub_endpoint(EndpointType::Post, &cpost.id.to_string()).to_string();
149     Post::update_ap_id(&conn, cpost.id, apub_id)?;
150   }
151
152   info!("{} post rows updated.", incorrect_posts.len());
153
154   sql_query("alter table post enable trigger refresh_post").execute(conn)?;
155
156   Ok(())
157 }
158
159 fn comment_updates_2020_04_03(conn: &PgConnection) -> Result<(), LemmyError> {
160   use lemmy_db::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.eq("http://fake.com"))
167     .filter(local.eq(true))
168     .load::<Comment>(conn)?;
169
170   sql_query("alter table comment disable trigger refresh_comment").execute(conn)?;
171
172   for ccomment in &incorrect_comments {
173     let apub_id = make_apub_endpoint(EndpointType::Comment, &ccomment.id.to_string()).to_string();
174     Comment::update_ap_id(&conn, ccomment.id, apub_id)?;
175   }
176
177   sql_query("alter table comment enable trigger refresh_comment").execute(conn)?;
178
179   info!("{} comment rows updated.", incorrect_comments.len());
180
181   Ok(())
182 }
183
184 fn private_message_updates_2020_05_05(conn: &PgConnection) -> Result<(), LemmyError> {
185   use lemmy_db::schema::private_message::dsl::*;
186
187   info!("Running private_message_updates_2020_05_05");
188
189   // Update the ap_id
190   let incorrect_pms = private_message
191     .filter(ap_id.eq("http://fake.com"))
192     .filter(local.eq(true))
193     .load::<PrivateMessage>(conn)?;
194
195   for cpm in &incorrect_pms {
196     let apub_id = make_apub_endpoint(EndpointType::PrivateMessage, &cpm.id.to_string()).to_string();
197     PrivateMessage::update_ap_id(&conn, cpm.id, apub_id)?;
198   }
199
200   info!("{} private message rows updated.", incorrect_pms.len());
201
202   Ok(())
203 }
204
205 fn post_thumbnail_url_updates_2020_07_27(conn: &PgConnection) -> Result<(), LemmyError> {
206   use lemmy_db::schema::post::dsl::*;
207
208   info!("Running post_thumbnail_url_updates_2020_07_27");
209
210   let domain_prefix = format!(
211     "{}://{}/pictrs/image/",
212     get_apub_protocol_string(),
213     Settings::get().hostname
214   );
215
216   let incorrect_thumbnails = post.filter(thumbnail_url.not_like("http%"));
217
218   // Prepend the rows with the update
219   let res = diesel::update(incorrect_thumbnails)
220     .set(
221       thumbnail_url.eq(
222         domain_prefix
223           .into_sql::<Nullable<Text>>()
224           .concat(thumbnail_url),
225       ),
226     )
227     .get_results::<Post>(conn)?;
228
229   info!("{} Post thumbnail_url rows updated.", res.len());
230
231   Ok(())
232 }