1 // This is for db migrations that require code
3 sql_types::{Nullable, Text},
7 generate_apub_endpoint,
8 generate_followers_url,
10 generate_shared_inbox_url,
13 use lemmy_db_queries::{
14 source::{comment::Comment_, post::Post_, private_message::PrivateMessage_},
17 use lemmy_db_schema::{
21 community::{Community, CommunityForm},
23 private_message::PrivateMessage,
24 user::{UserForm, User_},
27 use lemmy_utils::{apub::generate_actor_keypair, settings::Settings, LemmyError};
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)?;
42 fn user_updates_2020_04_02(conn: &PgConnection) -> Result<(), LemmyError> {
43 use lemmy_db_schema::schema::user_::dsl::*;
45 info!("Running user_updates_2020_04_02");
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)?;
53 for cuser in &incorrect_users {
54 let keypair = generate_actor_keypair()?;
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),
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()),
78 private_key: Some(keypair.private_key),
79 public_key: Some(keypair.public_key),
80 last_refreshed_at: Some(naive_now()),
82 shared_inbox_url: None,
85 User_::update(&conn, cuser.id, &form)?;
88 info!("{} user rows updated.", incorrect_users.len());
93 fn community_updates_2020_04_02(conn: &PgConnection) -> Result<(), LemmyError> {
94 use lemmy_db_schema::schema::community::dsl::*;
96 info!("Running community_updates_2020_04_02");
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)?;
104 for ccommunity in &incorrect_communities {
105 let keypair = generate_actor_keypair()?;
106 let community_actor_id = generate_apub_endpoint(EndpointType::Community, &ccommunity.name)?;
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,
116 nsfw: ccommunity.nsfw,
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()),
124 icon: Some(ccommunity.icon.to_owned()),
125 banner: Some(ccommunity.banner.to_owned()),
128 shared_inbox_url: None,
131 Community::update(&conn, ccommunity.id, &form)?;
134 info!("{} community rows updated.", incorrect_communities.len());
139 fn post_updates_2020_04_03(conn: &PgConnection) -> Result<(), LemmyError> {
140 use lemmy_db_schema::schema::post::dsl::*;
142 info!("Running post_updates_2020_04_03");
145 let incorrect_posts = post
146 .filter(ap_id.like("http://changeme_%"))
147 .filter(local.eq(true))
148 .load::<Post>(conn)?;
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)?;
155 info!("{} post rows updated.", incorrect_posts.len());
160 fn comment_updates_2020_04_03(conn: &PgConnection) -> Result<(), LemmyError> {
161 use lemmy_db_schema::schema::comment::dsl::*;
163 info!("Running comment_updates_2020_04_03");
166 let incorrect_comments = comment
167 .filter(ap_id.like("http://changeme_%"))
168 .filter(local.eq(true))
169 .load::<Comment>(conn)?;
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)?;
176 info!("{} comment rows updated.", incorrect_comments.len());
181 fn private_message_updates_2020_05_05(conn: &PgConnection) -> Result<(), LemmyError> {
182 use lemmy_db_schema::schema::private_message::dsl::*;
184 info!("Running private_message_updates_2020_05_05");
187 let incorrect_pms = private_message
188 .filter(ap_id.like("http://changeme_%"))
189 .filter(local.eq(true))
190 .load::<PrivateMessage>(conn)?;
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)?;
197 info!("{} private message rows updated.", incorrect_pms.len());
202 fn post_thumbnail_url_updates_2020_07_27(conn: &PgConnection) -> Result<(), LemmyError> {
203 use lemmy_db_schema::schema::post::dsl::*;
205 info!("Running post_thumbnail_url_updates_2020_07_27");
207 let domain_prefix = format!(
209 Settings::get().get_protocol_and_hostname(),
212 let incorrect_thumbnails = post.filter(thumbnail_url.not_like("http%"));
214 // Prepend the rows with the update
215 let res = diesel::update(incorrect_thumbnails)
219 .into_sql::<Nullable<Text>>()
220 .concat(thumbnail_url),
223 .get_results::<Post>(conn)?;
225 info!("{} Post thumbnail_url rows updated.", res.len());
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");
235 use lemmy_db_schema::schema::user_::dsl::*;
237 .filter(inbox_url.like("http://changeme_%"))
238 .load::<User_>(conn)?;
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))
245 inbox_url.eq(inbox_url_),
246 shared_inbox_url.eq(shared_inbox_url_),
248 .get_result::<User_>(conn)?;
253 use lemmy_db_schema::schema::community::dsl::*;
254 let communities = community
255 .filter(inbox_url.like("http://changeme_%"))
256 .load::<Community>(conn)?;
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))
264 followers_url.eq(followers_url_),
265 inbox_url.eq(inbox_url_),
266 shared_inbox_url.eq(shared_inbox_url_),
268 .get_result::<Community>(conn)?;