]> Untitled Git - lemmy.git/blob - src/code_migrations.rs
Store activitypub endpoints in database (#162)
[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     post::Post,
23     private_message::PrivateMessage,
24     user::{UserForm, User_},
25   },
26 };
27 use lemmy_utils::{apub::generate_actor_keypair, settings::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::user_::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_users = user_
49     .filter(actor_id.like("http://changeme_%"))
50     .filter(local.eq(true))
51     .load::<User_>(conn)?;
52
53   for cuser in &incorrect_users {
54     let keypair = generate_actor_keypair()?;
55
56     let form = UserForm {
57       name: cuser.name.to_owned(),
58       email: Some(cuser.email.to_owned()),
59       matrix_user_id: Some(cuser.matrix_user_id.to_owned()),
60       avatar: Some(cuser.avatar.to_owned()),
61       banner: Some(cuser.banner.to_owned()),
62       password_encrypted: cuser.password_encrypted.to_owned(),
63       preferred_username: Some(cuser.preferred_username.to_owned()),
64       published: Some(cuser.published),
65       updated: None,
66       admin: cuser.admin,
67       banned: Some(cuser.banned),
68       show_nsfw: cuser.show_nsfw,
69       theme: cuser.theme.to_owned(),
70       default_sort_type: cuser.default_sort_type,
71       default_listing_type: cuser.default_listing_type,
72       lang: cuser.lang.to_owned(),
73       show_avatars: cuser.show_avatars,
74       send_notifications_to_email: cuser.send_notifications_to_email,
75       actor_id: Some(generate_apub_endpoint(EndpointType::User, &cuser.name)?),
76       bio: Some(cuser.bio.to_owned()),
77       local: cuser.local,
78       private_key: Some(keypair.private_key),
79       public_key: Some(keypair.public_key),
80       last_refreshed_at: Some(naive_now()),
81       inbox_url: None,
82       shared_inbox_url: None,
83     };
84
85     User_::update(&conn, cuser.id, &form)?;
86   }
87
88   info!("{} user rows updated.", incorrect_users.len());
89
90   Ok(())
91 }
92
93 fn community_updates_2020_04_02(conn: &PgConnection) -> Result<(), LemmyError> {
94   use lemmy_db_schema::schema::community::dsl::*;
95
96   info!("Running community_updates_2020_04_02");
97
98   // Update the actor_id, private_key, and public_key, last_refreshed_at
99   let incorrect_communities = community
100     .filter(actor_id.like("http://changeme_%"))
101     .filter(local.eq(true))
102     .load::<Community>(conn)?;
103
104   for ccommunity in &incorrect_communities {
105     let keypair = generate_actor_keypair()?;
106     let community_actor_id = generate_apub_endpoint(EndpointType::Community, &ccommunity.name)?;
107
108     let form = CommunityForm {
109       name: ccommunity.name.to_owned(),
110       title: ccommunity.title.to_owned(),
111       description: ccommunity.description.to_owned(),
112       category_id: ccommunity.category_id,
113       creator_id: ccommunity.creator_id,
114       removed: None,
115       deleted: None,
116       nsfw: ccommunity.nsfw,
117       updated: None,
118       actor_id: Some(community_actor_id.to_owned()),
119       local: ccommunity.local,
120       private_key: Some(keypair.private_key),
121       public_key: Some(keypair.public_key),
122       last_refreshed_at: Some(naive_now()),
123       published: None,
124       icon: Some(ccommunity.icon.to_owned()),
125       banner: Some(ccommunity.banner.to_owned()),
126       followers_url: None,
127       inbox_url: None,
128       shared_inbox_url: None,
129     };
130
131     Community::update(&conn, ccommunity.id, &form)?;
132   }
133
134   info!("{} community rows updated.", incorrect_communities.len());
135
136   Ok(())
137 }
138
139 fn post_updates_2020_04_03(conn: &PgConnection) -> Result<(), LemmyError> {
140   use lemmy_db_schema::schema::post::dsl::*;
141
142   info!("Running post_updates_2020_04_03");
143
144   // Update the ap_id
145   let incorrect_posts = post
146     .filter(ap_id.eq("http://changeme_%"))
147     .filter(local.eq(true))
148     .load::<Post>(conn)?;
149
150   for cpost in &incorrect_posts {
151     let apub_id = generate_apub_endpoint(EndpointType::Post, &cpost.id.to_string())?;
152     Post::update_ap_id(&conn, cpost.id, apub_id)?;
153   }
154
155   info!("{} post rows updated.", incorrect_posts.len());
156
157   Ok(())
158 }
159
160 fn comment_updates_2020_04_03(conn: &PgConnection) -> 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.eq("http://changeme_%"))
168     .filter(local.eq(true))
169     .load::<Comment>(conn)?;
170
171   for ccomment in &incorrect_comments {
172     let apub_id = generate_apub_endpoint(EndpointType::Comment, &ccomment.id.to_string())?;
173     Comment::update_ap_id(&conn, ccomment.id, apub_id)?;
174   }
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::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://changeme_%"))
189     .filter(local.eq(true))
190     .load::<PrivateMessage>(conn)?;
191
192   for cpm in &incorrect_pms {
193     let apub_id = generate_apub_endpoint(EndpointType::PrivateMessage, &cpm.id.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::schema::post::dsl::*;
204
205   info!("Running post_thumbnail_url_updates_2020_07_27");
206
207   let domain_prefix = format!(
208     "{}/pictrs/image/",
209     Settings::get().get_protocol_and_hostname(),
210   );
211
212   let incorrect_thumbnails = post.filter(thumbnail_url.not_like("http%"));
213
214   // Prepend the rows with the update
215   let res = diesel::update(incorrect_thumbnails)
216     .set(
217       thumbnail_url.eq(
218         domain_prefix
219           .into_sql::<Nullable<Text>>()
220           .concat(thumbnail_url),
221       ),
222     )
223     .get_results::<Post>(conn)?;
224
225   info!("{} Post thumbnail_url rows updated.", res.len());
226
227   Ok(())
228 }
229
230 /// We are setting inbox and follower URLs for local and remote actors alike, because for now
231 /// all federated instances are also Lemmy and use the same URL scheme.
232 fn apub_columns_2021_02_02(conn: &PgConnection) -> Result<(), LemmyError> {
233   info!("Running apub_columns_2021_02_02");
234   {
235     use lemmy_db_schema::schema::user_::dsl::*;
236     let users = user_
237       .filter(inbox_url.eq("http://changeme_%"))
238       .load::<User_>(conn)?;
239
240     for u in &users {
241       let inbox_url_ = generate_inbox_url(&u.actor_id)?;
242       let shared_inbox_url_ = generate_shared_inbox_url(&u.actor_id)?;
243       diesel::update(user_.find(u.id))
244         .set((
245           inbox_url.eq(inbox_url_),
246           shared_inbox_url.eq(shared_inbox_url_),
247         ))
248         .get_result::<User_>(conn)?;
249     }
250   }
251
252   {
253     use lemmy_db_schema::schema::community::dsl::*;
254     let communities = community
255       .filter(inbox_url.eq("http://changeme_%"))
256       .load::<Community>(conn)?;
257
258     for c in &communities {
259       let followers_url_ = generate_followers_url(&c.actor_id)?;
260       let inbox_url_ = generate_inbox_url(&c.actor_id)?;
261       let shared_inbox_url_ = generate_shared_inbox_url(&c.actor_id)?;
262       diesel::update(community.find(c.id))
263         .set((
264           followers_url.eq(followers_url_),
265           inbox_url.eq(inbox_url_),
266           shared_inbox_url.eq(shared_inbox_url_),
267         ))
268         .get_result::<Community>(conn)?;
269     }
270   }
271
272   Ok(())
273 }