]> Untitled Git - lemmy.git/blob - src/code_migrations.rs
Use Url type for ap_id fields in database (fixes #1364) (#1371)
[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_db_queries::{
7   source::{comment::Comment_, post::Post_, private_message::PrivateMessage_},
8   Crud,
9 };
10 use lemmy_db_schema::{
11   naive_now,
12   source::{
13     comment::Comment,
14     community::{Community, CommunityForm},
15     post::Post,
16     private_message::PrivateMessage,
17     user::{UserForm, User_},
18   },
19 };
20 use lemmy_utils::{
21   apub::{generate_actor_keypair, make_apub_endpoint, EndpointType},
22   settings::Settings,
23   LemmyError,
24 };
25 use log::info;
26
27 pub fn run_advanced_migrations(conn: &PgConnection) -> Result<(), LemmyError> {
28   user_updates_2020_04_02(&conn)?;
29   community_updates_2020_04_02(&conn)?;
30   post_updates_2020_04_03(&conn)?;
31   comment_updates_2020_04_03(&conn)?;
32   private_message_updates_2020_05_05(&conn)?;
33   post_thumbnail_url_updates_2020_07_27(&conn)?;
34
35   Ok(())
36 }
37
38 fn user_updates_2020_04_02(conn: &PgConnection) -> Result<(), LemmyError> {
39   use lemmy_db_schema::schema::user_::dsl::*;
40
41   info!("Running user_updates_2020_04_02");
42
43   // Update the actor_id, private_key, and public_key, last_refreshed_at
44   let incorrect_users = user_
45     .filter(actor_id.like("http://changeme_%"))
46     .filter(local.eq(true))
47     .load::<User_>(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: Some(cuser.email.to_owned()),
55       matrix_user_id: Some(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: Some(cuser.preferred_username.to_owned()),
60       published: Some(cuser.published),
61       updated: None,
62       admin: cuser.admin,
63       banned: Some(cuser.banned),
64       show_nsfw: cuser.show_nsfw,
65       theme: cuser.theme.to_owned(),
66       default_sort_type: cuser.default_sort_type,
67       default_listing_type: cuser.default_listing_type,
68       lang: cuser.lang.to_owned(),
69       show_avatars: cuser.show_avatars,
70       send_notifications_to_email: cuser.send_notifications_to_email,
71       actor_id: Some(make_apub_endpoint(EndpointType::User, &cuser.name).into()),
72       bio: Some(cuser.bio.to_owned()),
73       local: cuser.local,
74       private_key: Some(keypair.private_key),
75       public_key: Some(keypair.public_key),
76       last_refreshed_at: Some(naive_now()),
77     };
78
79     User_::update(&conn, cuser.id, &form)?;
80   }
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::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("http://changeme_%"))
95     .filter(local.eq(true))
96     .load::<Community>(conn)?;
97
98   for ccommunity in &incorrect_communities {
99     let keypair = generate_actor_keypair()?;
100
101     let form = CommunityForm {
102       name: ccommunity.name.to_owned(),
103       title: ccommunity.title.to_owned(),
104       description: ccommunity.description.to_owned(),
105       category_id: ccommunity.category_id,
106       creator_id: ccommunity.creator_id,
107       removed: None,
108       deleted: None,
109       nsfw: ccommunity.nsfw,
110       updated: None,
111       actor_id: Some(make_apub_endpoint(EndpointType::Community, &ccommunity.name).into()),
112       local: ccommunity.local,
113       private_key: Some(keypair.private_key),
114       public_key: Some(keypair.public_key),
115       last_refreshed_at: Some(naive_now()),
116       published: None,
117       icon: Some(ccommunity.icon.to_owned()),
118       banner: Some(ccommunity.banner.to_owned()),
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.eq("http://changeme_%"))
137     .filter(local.eq(true))
138     .load::<Post>(conn)?;
139
140   for cpost in &incorrect_posts {
141     let apub_id = make_apub_endpoint(EndpointType::Post, &cpost.id.to_string()).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.eq("http://changeme_%"))
158     .filter(local.eq(true))
159     .load::<Comment>(conn)?;
160
161   for ccomment in &incorrect_comments {
162     let apub_id = make_apub_endpoint(EndpointType::Comment, &ccomment.id.to_string()).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.eq("http://changeme_%"))
179     .filter(local.eq(true))
180     .load::<PrivateMessage>(conn)?;
181
182   for cpm in &incorrect_pms {
183     let apub_id = make_apub_endpoint(EndpointType::PrivateMessage, &cpm.id.to_string()).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 }