Przeglądaj źródła

Merge branch 'refresh-actors' of foxhkron/kibou into master

pull/74/head
Toromino 5 miesięcy temu
rodzic
commit
b20ee8c371

+ 36
- 4
src/activitypub/actor.rs Wyświetl plik

@@ -1,10 +1,13 @@
1 1
 use activitypub::activity::Activity;
2
+use activitypub::validator;
2 3
 use actor;
4
+use chrono::Duration;
3 5
 use chrono::Utc;
4 6
 use database;
5 7
 use env;
6 8
 use serde::{Deserialize, Serialize};
7 9
 use serde_json::{self, json};
10
+use web_handler;
8 11
 
9 12
 // ActivityStreams2/AcitivityPub properties are expressed in CamelCase
10 13
 #[allow(non_snake_case)]
@@ -60,7 +63,7 @@ pub fn add_follow(account: &str, source: &str, activity_id: &str) {
60 63
         follow_data.push(new_follow_data);
61 64
 
62 65
         actor.followers["activitypub"] = serde_json::to_value(follow_data).unwrap();
63
-        actor::update_followers(&database, &mut actor);
66
+        actor::update_followers(&database, actor);
64 67
     }
65 68
 }
66 69
 
@@ -80,13 +83,41 @@ pub fn remove_follow(account: &str, source: &str) {
80 83
         follow_data.remove(index);
81 84
 
82 85
         actor.followers["activitypub"] = serde_json::to_value(follow_data).unwrap();
83
-        actor::update_followers(&database, &mut actor);
86
+        actor::update_followers(&database, actor);
84 87
     }
85 88
 }
86 89
 
87 90
 // Refetches remote actor and detects changes to icon, username, keys and summary
88
-// [TODO]
89
-pub fn refresh() {}
91
+pub fn refresh(uri: String) {
92
+    std::thread::spawn(move || {
93
+        let expiration_time: chrono::DateTime<Utc> = Utc::now() - Duration::days(2);
94
+        let database = database::establish_connection();
95
+        let actor =
96
+            actor::get_actor_by_uri(&database, &uri).expect("Actor with this URI does not exist");
97
+
98
+        if actor.modified.timestamp() <= expiration_time.timestamp() && !actor.local {
99
+            println!("Refreshing actor {}", uri);
100
+
101
+            match web_handler::fetch_remote_object(&uri.to_string()) {
102
+                Ok(object) => {
103
+                    let parsed_object: serde_json::Value = serde_json::from_str(&object).unwrap();
104
+
105
+                    match validator::validate_actor(parsed_object) {
106
+                        Ok(actor) => {
107
+                            let serialized_actor: Actor = serde_json::from_value(actor).unwrap();
108
+
109
+                            actor::update(&database, create_internal_actor(serialized_actor));
110
+                        }
111
+                        Err(_) => {
112
+                            eprintln!("Unable to refresh actor, remote object is invalid: {}", uri)
113
+                        }
114
+                    }
115
+                }
116
+                Err(_) => eprintln!("Unable to refresh actor: {}", uri),
117
+            }
118
+        }
119
+    });
120
+}
90 121
 
91 122
 pub fn get_json_by_preferred_username(preferred_username: &str) -> serde_json::Value {
92 123
     let database = database::establish_connection();
@@ -182,5 +213,6 @@ pub fn create_internal_actor(ap_actor: Actor) -> actor::Actor {
182 213
         keys: serde_json::json!({"public" : ap_actor.publicKey["publicKeyPem"]}),
183 214
         followers: serde_json::json!({"activitypub": []}),
184 215
         created: Utc::now().naive_utc(),
216
+        modified: Utc::now().naive_utc(),
185 217
     }
186 218
 }

+ 3
- 0
src/activitypub/validator.rs Wyświetl plik

@@ -1,3 +1,4 @@
1
+use activitypub;
1 2
 use activitypub::activity::Activity;
2 3
 use activitypub::activity::Object;
3 4
 use activitypub::controller::actor_exists;
@@ -32,6 +33,7 @@ pub fn validate_activity(
32 33
 
33 34
     let valid_actor = if activity.get("actor").is_some() {
34 35
         if actor_exists(activity["actor"].as_str().unwrap()) {
36
+            activitypub::actor::refresh(activity["actor"].as_str().unwrap().to_string());
35 37
             true
36 38
         } else {
37 39
             fetch_object_by_id(activity["actor"].as_str().unwrap().to_string());
@@ -98,6 +100,7 @@ pub fn validate_object(
98 100
 
99 101
     let valid_actor = if object.get("attributedTo").is_some() {
100 102
         if actor_exists(object["attributedTo"].as_str().unwrap()) {
103
+            activitypub::actor::refresh(object["attributedTo"].as_str().unwrap().to_string());
101 104
             true
102 105
         } else {
103 106
             fetch_object_by_id(object["attributedTo"].as_str().unwrap().to_string());

+ 18
- 4
src/actor.rs Wyświetl plik

@@ -31,6 +31,7 @@ pub struct Actor {
31 31
     pub local: bool,
32 32
     pub keys: serde_json::Value,
33 33
     pub created: NaiveDateTime,
34
+    pub modified: NaiveDateTime,
34 35
 }
35 36
 
36 37
 impl Actor {
@@ -130,6 +131,7 @@ fn serialize_actor(sql_actor: QueryActor) -> Actor {
130 131
         local: sql_actor.local,
131 132
         followers: sql_actor.followers,
132 133
         created: sql_actor.created,
134
+        modified: sql_actor.modified,
133 135
     }
134 136
 }
135 137
 
@@ -235,17 +237,29 @@ pub fn create_actor(db_connection: &PgConnection, actor: &mut Actor) {
235 237
 /// Tests for this function are at `tests/actor.rs`
236 238
 /// - delete_local_actor()
237 239
 /// - delete_remote_actor()
238
-pub fn delete(db_connection: &PgConnection, actor: &mut Actor) {
240
+pub fn delete(db_connection: &PgConnection, actor: Actor) {
239 241
     diesel::delete(actors.filter(actor_uri.eq(&actor.actor_uri)))
240 242
         .execute(db_connection)
241
-        .expect("Error deleting user");
243
+        .expect("Could not delete actor");
242 244
 }
243 245
 
244
-pub fn update_followers(db_connection: &PgConnection, actor: &mut Actor) {
246
+pub fn update(db_connection: &PgConnection, actor: Actor) {
247
+    diesel::update(actors.filter(actor_uri.eq(&actor.actor_uri)))
248
+        .set((
249
+            username.eq(&actor.username),
250
+            summary.eq(&actor.summary),
251
+            icon.eq(&actor.icon),
252
+            keys.eq(&actor.keys),
253
+        ))
254
+        .execute(db_connection)
255
+        .expect("Could not update actor");
256
+}
257
+
258
+pub fn update_followers(db_connection: &PgConnection, actor: Actor) {
245 259
     diesel::update(actors.filter(actor_uri.eq(&actor.actor_uri)))
246 260
         .set(followers.eq(&actor.followers))
247 261
         .execute(db_connection)
248
-        .expect("Error updating followers");
262
+        .expect("Error occured while updating followers");
249 263
 }
250 264
 
251 265
 pub fn get_actor_by_acct(

+ 1
- 0
src/bin/create_user.rs Wyświetl plik

@@ -58,6 +58,7 @@ fn main() {
58 58
         local: true,
59 59
         keys: serde_json::json!({}),
60 60
         created: Utc::now().naive_utc(),
61
+        modified: Utc::now().naive_utc(),
61 62
     };
62 63
 
63 64
     actor::create_actor(&database, &mut new_actor)

+ 1
- 0
src/mastodon_api/controller.rs Wyświetl plik

@@ -87,6 +87,7 @@ pub fn account_create(form: &RegistrationForm) -> Option<Token> {
87 87
             local: true,
88 88
             keys: serde_json::json!({}),
89 89
             created: Utc::now().naive_utc(),
90
+            modified: Utc::now().naive_utc(),
90 91
         };
91 92
 
92 93
         actor::create_actor(&database, &mut new_actor);

+ 3
- 1
src/tests/utils.rs Wyświetl plik

@@ -18,6 +18,7 @@ pub fn create_local_test_actor(username: &str) -> actor::Actor {
18 18
         local: true,
19 19
         followers: serde_json::json!({"activitypub": []}),
20 20
         created: Utc::now().naive_utc(),
21
+        modified: Utc::now().naive_utc(),
21 22
     };
22 23
 
23 24
     actor::create_actor(&database, &mut test_actor);
@@ -29,7 +30,7 @@ pub fn create_local_test_actor(username: &str) -> actor::Actor {
29 30
 
30 31
 pub fn delete_test_actor(mut actor: actor::Actor) {
31 32
     let database = database::establish_connection();
32
-    actor::delete(&database, &mut actor);
33
+    actor::delete(&database, actor);
33 34
 }
34 35
 
35 36
 pub fn create_remote_test_actor(username: &str) -> actor::Actor {
@@ -48,6 +49,7 @@ pub fn create_remote_test_actor(username: &str) -> actor::Actor {
48 49
         local: false,
49 50
         followers: serde_json::json!({"activitypub": []}),
50 51
         created: Utc::now().naive_utc(),
52
+        modified: Utc::now().naive_utc(),
51 53
     };
52 54
 
53 55
     actor::create_actor(&database, &mut test_actor);

Ładowanie…
Anuluj
Zapisz