|
@@ -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
|
}
|