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