1 // This is for db migrations that require code
2 use super::comment::Comment;
3 use super::community::{Community, CommunityForm};
5 use super::user::{UserForm, User_};
7 use crate::apub::{gen_keypair_str, make_apub_endpoint, EndpointType};
11 pub fn run_advanced_migrations(conn: &PgConnection) -> Result<(), Error> {
12 user_updates_2020_04_02(conn)?;
13 community_updates_2020_04_02(conn)?;
14 post_updates_2020_04_03(conn)?;
15 comment_updates_2020_04_03(conn)?;
20 fn user_updates_2020_04_02(conn: &PgConnection) -> Result<(), Error> {
21 use crate::schema::user_::dsl::*;
23 info!("Running user_updates_2020_04_02");
25 // Update the actor_id, private_key, and public_key, last_refreshed_at
26 let incorrect_users = user_
27 .filter(actor_id.eq("changeme"))
28 .filter(local.eq(true))
29 .load::<User_>(conn)?;
31 for cuser in &incorrect_users {
32 let (user_public_key, user_private_key) = gen_keypair_str();
35 name: cuser.name.to_owned(),
36 email: cuser.email.to_owned(),
37 matrix_user_id: cuser.matrix_user_id.to_owned(),
38 avatar: cuser.avatar.to_owned(),
39 password_encrypted: cuser.password_encrypted.to_owned(),
40 preferred_username: cuser.preferred_username.to_owned(),
44 show_nsfw: cuser.show_nsfw,
45 theme: cuser.theme.to_owned(),
46 default_sort_type: cuser.default_sort_type,
47 default_listing_type: cuser.default_listing_type,
48 lang: cuser.lang.to_owned(),
49 show_avatars: cuser.show_avatars,
50 send_notifications_to_email: cuser.send_notifications_to_email,
51 actor_id: make_apub_endpoint(EndpointType::User, &cuser.name).to_string(),
52 bio: cuser.bio.to_owned(),
54 private_key: Some(user_private_key),
55 public_key: Some(user_public_key),
56 last_refreshed_at: Some(naive_now()),
59 User_::update(&conn, cuser.id, &form)?;
62 info!("{} user rows updated.", incorrect_users.len());
67 fn community_updates_2020_04_02(conn: &PgConnection) -> Result<(), Error> {
68 use crate::schema::community::dsl::*;
70 info!("Running community_updates_2020_04_02");
72 // Update the actor_id, private_key, and public_key, last_refreshed_at
73 let incorrect_communities = community
74 .filter(actor_id.eq("changeme"))
75 .filter(local.eq(true))
76 .load::<Community>(conn)?;
78 for ccommunity in &incorrect_communities {
79 let (community_public_key, community_private_key) = gen_keypair_str();
81 let form = CommunityForm {
82 name: ccommunity.name.to_owned(),
83 title: ccommunity.title.to_owned(),
84 description: ccommunity.description.to_owned(),
85 category_id: ccommunity.category_id,
86 creator_id: ccommunity.creator_id,
89 nsfw: ccommunity.nsfw,
91 actor_id: make_apub_endpoint(EndpointType::Community, &ccommunity.name).to_string(),
92 local: ccommunity.local,
93 private_key: Some(community_private_key),
94 public_key: Some(community_public_key),
95 last_refreshed_at: Some(naive_now()),
99 Community::update(&conn, ccommunity.id, &form)?;
102 info!("{} community rows updated.", incorrect_communities.len());
107 fn post_updates_2020_04_03(conn: &PgConnection) -> Result<(), Error> {
108 use crate::schema::post::dsl::*;
110 info!("Running post_updates_2020_04_03");
113 let incorrect_posts = post
114 .filter(ap_id.eq("changeme"))
115 .filter(local.eq(true))
116 .load::<Post>(conn)?;
118 for cpost in &incorrect_posts {
119 Post::update_ap_id(&conn, cpost.id)?;
122 info!("{} post rows updated.", incorrect_posts.len());
127 fn comment_updates_2020_04_03(conn: &PgConnection) -> Result<(), Error> {
128 use crate::schema::comment::dsl::*;
130 info!("Running comment_updates_2020_04_03");
133 let incorrect_comments = comment
134 .filter(ap_id.eq("changeme"))
135 .filter(local.eq(true))
136 .load::<Comment>(conn)?;
138 for ccomment in &incorrect_comments {
139 Comment::update_ap_id(&conn, ccomment.id)?;
142 info!("{} comment rows updated.", incorrect_comments.len());