]> Untitled Git - lemmy.git/blob - server/src/code_migrations.rs
Translated using Weblate (Italian)
[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: cuser.avatar.to_owned(),
57       password_encrypted: cuser.password_encrypted.to_owned(),
58       preferred_username: cuser.preferred_username.to_owned(),
59       updated: None,
60       admin: cuser.admin,
61       banned: cuser.banned,
62       show_nsfw: cuser.show_nsfw,
63       theme: cuser.theme.to_owned(),
64       default_sort_type: cuser.default_sort_type,
65       default_listing_type: cuser.default_listing_type,
66       lang: cuser.lang.to_owned(),
67       show_avatars: cuser.show_avatars,
68       send_notifications_to_email: cuser.send_notifications_to_email,
69       actor_id: make_apub_endpoint(EndpointType::User, &cuser.name).to_string(),
70       bio: cuser.bio.to_owned(),
71       local: cuser.local,
72       private_key: Some(keypair.private_key),
73       public_key: Some(keypair.public_key),
74       last_refreshed_at: Some(naive_now()),
75     };
76
77     User_::update(&conn, cuser.id, &form)?;
78   }
79
80   sql_query("alter table user_ enable trigger refresh_user").execute(conn)?;
81
82   info!("{} user rows updated.", incorrect_users.len());
83
84   Ok(())
85 }
86
87 fn community_updates_2020_04_02(conn: &PgConnection) -> Result<(), LemmyError> {
88   use lemmy_db::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("changeme_%"))
95     .filter(local.eq(true))
96     .load::<Community>(conn)?;
97
98   sql_query("alter table community disable trigger refresh_community").execute(conn)?;
99
100   for ccommunity in &incorrect_communities {
101     let keypair = generate_actor_keypair()?;
102
103     let form = CommunityForm {
104       name: ccommunity.name.to_owned(),
105       title: ccommunity.title.to_owned(),
106       description: ccommunity.description.to_owned(),
107       category_id: ccommunity.category_id,
108       creator_id: ccommunity.creator_id,
109       removed: None,
110       deleted: None,
111       nsfw: ccommunity.nsfw,
112       updated: None,
113       actor_id: make_apub_endpoint(EndpointType::Community, &ccommunity.name).to_string(),
114       local: ccommunity.local,
115       private_key: Some(keypair.private_key),
116       public_key: Some(keypair.public_key),
117       last_refreshed_at: Some(naive_now()),
118       published: None,
119     };
120
121     Community::update(&conn, ccommunity.id, &form)?;
122   }
123
124   sql_query("alter table community enable trigger refresh_community").execute(conn)?;
125
126   info!("{} community rows updated.", incorrect_communities.len());
127
128   Ok(())
129 }
130
131 fn post_updates_2020_04_03(conn: &PgConnection) -> Result<(), LemmyError> {
132   use lemmy_db::schema::post::dsl::*;
133
134   info!("Running post_updates_2020_04_03");
135
136   // Update the ap_id
137   let incorrect_posts = post
138     .filter(ap_id.eq("http://fake.com"))
139     .filter(local.eq(true))
140     .load::<Post>(conn)?;
141
142   sql_query("alter table post disable trigger refresh_post").execute(conn)?;
143
144   for cpost in &incorrect_posts {
145     let apub_id = make_apub_endpoint(EndpointType::Post, &cpost.id.to_string()).to_string();
146     Post::update_ap_id(&conn, cpost.id, apub_id)?;
147   }
148
149   info!("{} post rows updated.", incorrect_posts.len());
150
151   sql_query("alter table post enable trigger refresh_post").execute(conn)?;
152
153   Ok(())
154 }
155
156 fn comment_updates_2020_04_03(conn: &PgConnection) -> Result<(), LemmyError> {
157   use lemmy_db::schema::comment::dsl::*;
158
159   info!("Running comment_updates_2020_04_03");
160
161   // Update the ap_id
162   let incorrect_comments = comment
163     .filter(ap_id.eq("http://fake.com"))
164     .filter(local.eq(true))
165     .load::<Comment>(conn)?;
166
167   sql_query("alter table comment disable trigger refresh_comment").execute(conn)?;
168
169   for ccomment in &incorrect_comments {
170     let apub_id = make_apub_endpoint(EndpointType::Comment, &ccomment.id.to_string()).to_string();
171     Comment::update_ap_id(&conn, ccomment.id, apub_id)?;
172   }
173
174   sql_query("alter table comment enable trigger refresh_comment").execute(conn)?;
175
176   info!("{} comment rows updated.", incorrect_comments.len());
177
178   Ok(())
179 }
180
181 fn private_message_updates_2020_05_05(conn: &PgConnection) -> Result<(), LemmyError> {
182   use lemmy_db::schema::private_message::dsl::*;
183
184   info!("Running private_message_updates_2020_05_05");
185
186   // Update the ap_id
187   let incorrect_pms = private_message
188     .filter(ap_id.eq("http://fake.com"))
189     .filter(local.eq(true))
190     .load::<PrivateMessage>(conn)?;
191
192   for cpm in &incorrect_pms {
193     let apub_id = make_apub_endpoint(EndpointType::PrivateMessage, &cpm.id.to_string()).to_string();
194     PrivateMessage::update_ap_id(&conn, cpm.id, apub_id)?;
195   }
196
197   info!("{} private message rows updated.", incorrect_pms.len());
198
199   Ok(())
200 }
201
202 fn post_thumbnail_url_updates_2020_07_27(conn: &PgConnection) -> Result<(), LemmyError> {
203   use lemmy_db::schema::post::dsl::*;
204
205   info!("Running post_thumbnail_url_updates_2020_07_27");
206
207   let domain_prefix = format!(
208     "{}://{}/pictrs/image/",
209     get_apub_protocol_string(),
210     Settings::get().hostname
211   );
212
213   let incorrect_thumbnails = post.filter(thumbnail_url.not_like("http%"));
214
215   // Prepend the rows with the update
216   let res = diesel::update(incorrect_thumbnails)
217     .set(
218       thumbnail_url.eq(
219         domain_prefix
220           .into_sql::<Nullable<Text>>()
221           .concat(thumbnail_url),
222       ),
223     )
224     .get_results::<Post>(conn)?;
225
226   info!("{} Post thumbnail_url rows updated.", res.len());
227
228   Ok(())
229 }