]> Untitled Git - lemmy.git/blob - src/code_migrations.rs
Implement restricted community (only mods can post) (fixes #187) (#2235)
[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_followers_url,
8   generate_inbox_url,
9   generate_local_apub_endpoint,
10   generate_shared_inbox_url,
11   generate_site_inbox_url,
12   EndpointType,
13 };
14 use lemmy_db_schema::{
15   naive_now,
16   source::{
17     comment::Comment,
18     community::{Community, CommunityForm},
19     person::{Person, PersonForm},
20     post::Post,
21     private_message::PrivateMessage,
22     site::{Site, SiteForm},
23   },
24   traits::Crud,
25 };
26 use lemmy_utils::{apub::generate_actor_keypair, LemmyError};
27 use tracing::info;
28 use url::Url;
29
30 pub fn run_advanced_migrations(
31   conn: &PgConnection,
32   protocol_and_hostname: &str,
33 ) -> Result<(), LemmyError> {
34   user_updates_2020_04_02(conn, protocol_and_hostname)?;
35   community_updates_2020_04_02(conn, protocol_and_hostname)?;
36   post_updates_2020_04_03(conn, protocol_and_hostname)?;
37   comment_updates_2020_04_03(conn, protocol_and_hostname)?;
38   private_message_updates_2020_05_05(conn, protocol_and_hostname)?;
39   post_thumbnail_url_updates_2020_07_27(conn, protocol_and_hostname)?;
40   apub_columns_2021_02_02(conn)?;
41   instance_actor_2022_01_28(conn, protocol_and_hostname)?;
42
43   Ok(())
44 }
45
46 fn user_updates_2020_04_02(
47   conn: &PgConnection,
48   protocol_and_hostname: &str,
49 ) -> Result<(), LemmyError> {
50   use lemmy_db_schema::schema::person::dsl::*;
51
52   info!("Running user_updates_2020_04_02");
53
54   // Update the actor_id, private_key, and public_key, last_refreshed_at
55   let incorrect_persons = person
56     .filter(actor_id.like("http://changeme%"))
57     .filter(local.eq(true))
58     .load::<Person>(conn)?;
59
60   for cperson in &incorrect_persons {
61     let keypair = generate_actor_keypair()?;
62
63     let form = PersonForm {
64       name: cperson.name.to_owned(),
65       actor_id: Some(generate_local_apub_endpoint(
66         EndpointType::Person,
67         &cperson.name,
68         protocol_and_hostname,
69       )?),
70       private_key: Some(Some(keypair.private_key)),
71       public_key: keypair.public_key,
72       last_refreshed_at: Some(naive_now()),
73       ..PersonForm::default()
74     };
75
76     Person::update(conn, cperson.id, &form)?;
77   }
78
79   info!("{} person rows updated.", incorrect_persons.len());
80
81   Ok(())
82 }
83
84 fn community_updates_2020_04_02(
85   conn: &PgConnection,
86   protocol_and_hostname: &str,
87 ) -> 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     let community_actor_id = generate_local_apub_endpoint(
101       EndpointType::Community,
102       &ccommunity.name,
103       protocol_and_hostname,
104     )?;
105
106     let form = CommunityForm {
107       name: ccommunity.name.to_owned(),
108       title: ccommunity.title.to_owned(),
109       description: ccommunity.description.to_owned(),
110       hidden: Some(false),
111       actor_id: Some(community_actor_id.to_owned()),
112       local: Some(ccommunity.local),
113       private_key: Some(Some(keypair.private_key)),
114       public_key: keypair.public_key,
115       last_refreshed_at: Some(naive_now()),
116       icon: Some(ccommunity.icon.to_owned()),
117       banner: Some(ccommunity.banner.to_owned()),
118       ..Default::default()
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(
130   conn: &PgConnection,
131   protocol_and_hostname: &str,
132 ) -> Result<(), LemmyError> {
133   use lemmy_db_schema::schema::post::dsl::*;
134
135   info!("Running post_updates_2020_04_03");
136
137   // Update the ap_id
138   let incorrect_posts = post
139     .filter(ap_id.like("http://changeme%"))
140     .filter(local.eq(true))
141     .load::<Post>(conn)?;
142
143   for cpost in &incorrect_posts {
144     let apub_id = generate_local_apub_endpoint(
145       EndpointType::Post,
146       &cpost.id.to_string(),
147       protocol_and_hostname,
148     )?;
149     Post::update_ap_id(conn, cpost.id, apub_id)?;
150   }
151
152   info!("{} post rows updated.", incorrect_posts.len());
153
154   Ok(())
155 }
156
157 fn comment_updates_2020_04_03(
158   conn: &PgConnection,
159   protocol_and_hostname: &str,
160 ) -> Result<(), LemmyError> {
161   use lemmy_db_schema::schema::comment::dsl::*;
162
163   info!("Running comment_updates_2020_04_03");
164
165   // Update the ap_id
166   let incorrect_comments = comment
167     .filter(ap_id.like("http://changeme%"))
168     .filter(local.eq(true))
169     .load::<Comment>(conn)?;
170
171   for ccomment in &incorrect_comments {
172     let apub_id = generate_local_apub_endpoint(
173       EndpointType::Comment,
174       &ccomment.id.to_string(),
175       protocol_and_hostname,
176     )?;
177     Comment::update_ap_id(conn, ccomment.id, apub_id)?;
178   }
179
180   info!("{} comment rows updated.", incorrect_comments.len());
181
182   Ok(())
183 }
184
185 fn private_message_updates_2020_05_05(
186   conn: &PgConnection,
187   protocol_and_hostname: &str,
188 ) -> Result<(), LemmyError> {
189   use lemmy_db_schema::schema::private_message::dsl::*;
190
191   info!("Running private_message_updates_2020_05_05");
192
193   // Update the ap_id
194   let incorrect_pms = private_message
195     .filter(ap_id.like("http://changeme%"))
196     .filter(local.eq(true))
197     .load::<PrivateMessage>(conn)?;
198
199   for cpm in &incorrect_pms {
200     let apub_id = generate_local_apub_endpoint(
201       EndpointType::PrivateMessage,
202       &cpm.id.to_string(),
203       protocol_and_hostname,
204     )?;
205     PrivateMessage::update_ap_id(conn, cpm.id, apub_id)?;
206   }
207
208   info!("{} private message rows updated.", incorrect_pms.len());
209
210   Ok(())
211 }
212
213 fn post_thumbnail_url_updates_2020_07_27(
214   conn: &PgConnection,
215   protocol_and_hostname: &str,
216 ) -> Result<(), LemmyError> {
217   use lemmy_db_schema::schema::post::dsl::*;
218
219   info!("Running post_thumbnail_url_updates_2020_07_27");
220
221   let domain_prefix = format!("{}/pictrs/image/", protocol_and_hostname,);
222
223   let incorrect_thumbnails = post.filter(thumbnail_url.not_like("http%"));
224
225   // Prepend the rows with the update
226   let res = diesel::update(incorrect_thumbnails)
227     .set(
228       thumbnail_url.eq(
229         domain_prefix
230           .into_sql::<Nullable<Text>>()
231           .concat(thumbnail_url),
232       ),
233     )
234     .get_results::<Post>(conn)?;
235
236   info!("{} Post thumbnail_url rows updated.", res.len());
237
238   Ok(())
239 }
240
241 /// We are setting inbox and follower URLs for local and remote actors alike, because for now
242 /// all federated instances are also Lemmy and use the same URL scheme.
243 fn apub_columns_2021_02_02(conn: &PgConnection) -> Result<(), LemmyError> {
244   info!("Running apub_columns_2021_02_02");
245   {
246     use lemmy_db_schema::schema::person::dsl::*;
247     let persons = person
248       .filter(inbox_url.like("http://changeme%"))
249       .load::<Person>(conn)?;
250
251     for p in &persons {
252       let inbox_url_ = generate_inbox_url(&p.actor_id)?;
253       let shared_inbox_url_ = generate_shared_inbox_url(&p.actor_id)?;
254       diesel::update(person.find(p.id))
255         .set((
256           inbox_url.eq(inbox_url_),
257           shared_inbox_url.eq(shared_inbox_url_),
258         ))
259         .get_result::<Person>(conn)?;
260     }
261   }
262
263   {
264     use lemmy_db_schema::schema::community::dsl::*;
265     let communities = community
266       .filter(inbox_url.like("http://changeme%"))
267       .load::<Community>(conn)?;
268
269     for c in &communities {
270       let followers_url_ = generate_followers_url(&c.actor_id)?;
271       let inbox_url_ = generate_inbox_url(&c.actor_id)?;
272       let shared_inbox_url_ = generate_shared_inbox_url(&c.actor_id)?;
273       diesel::update(community.find(c.id))
274         .set((
275           followers_url.eq(followers_url_),
276           inbox_url.eq(inbox_url_),
277           shared_inbox_url.eq(shared_inbox_url_),
278         ))
279         .get_result::<Community>(conn)?;
280     }
281   }
282
283   Ok(())
284 }
285
286 /// Site object turns into an actor, so that things like instance description can be federated. This
287 /// means we need to add actor columns to the site table, and initialize them with correct values.
288 /// Before this point, there is only a single value in the site table which refers to the local
289 /// Lemmy instance, so thats all we need to update.
290 fn instance_actor_2022_01_28(
291   conn: &PgConnection,
292   protocol_and_hostname: &str,
293 ) -> Result<(), LemmyError> {
294   info!("Running instance_actor_2021_09_29");
295   if let Ok(site) = Site::read_local_site(conn) {
296     let key_pair = generate_actor_keypair()?;
297     let actor_id = Url::parse(protocol_and_hostname)?;
298     let site_form = SiteForm {
299       name: site.name,
300       actor_id: Some(actor_id.clone().into()),
301       last_refreshed_at: Some(naive_now()),
302       inbox_url: Some(generate_site_inbox_url(&actor_id.into())?),
303       private_key: Some(Some(key_pair.private_key)),
304       public_key: Some(key_pair.public_key),
305       ..Default::default()
306     };
307     Site::update(conn, site.id, &site_form)?;
308   }
309   Ok(())
310 }