Browse Source

Merge branch 'master' of foxhkron/kibou into master

pull/92/head
Toromino 7 months ago
parent
commit
05c8b1ed95

+ 2
- 4
src/activitypub/routes.rs View File

@@ -5,8 +5,6 @@ use activitypub::ActivitypubMediatype;
5 5
 use activitypub::ActivitystreamsResponse;
6 6
 use activitypub::Payload;
7 7
 use activitypub::Signature;
8
-use rocket::http::Status;
9
-use serde_json;
10 8
 
11 9
 #[get("/activities/<id>")]
12 10
 pub fn activity(_media_type: ActivitypubMediatype, id: String) -> ActivitystreamsResponse {
@@ -18,8 +16,8 @@ pub fn actor(_media_type: ActivitypubMediatype, handle: String) -> Activitystrea
18 16
     ActivitystreamsResponse(ap_actor::get_json_by_preferred_username(&handle).to_string())
19 17
 }
20 18
 
21
-#[post("/actors/<id>/inbox", data = "<activity>")]
22
-pub fn actor_inbox(id: String, activity: Payload, _signature: Signature) {
19
+#[post("/actors/<_id>/inbox", data = "<activity>")]
20
+pub fn actor_inbox(_id: String, activity: Payload, _signature: Signature) {
23 21
     controller::prepare_incoming(activity.0, _signature);
24 22
 }
25 23
 

+ 15
- 10
src/bin/kibou_server.rs View File

@@ -4,16 +4,21 @@ use kibou::env;
4 4
 use kibou::rocket_app;
5 5
 
6 6
 fn main() {
7
-    let rocket_config = rocket::config::Config::build(rocket::config::Environment::active().expect("Unknown ROCKET_ENV value! (enum: {Development, Staging, Production})"))
8
-        .address(env::get_value("endpoint.host".to_string()))
9
-        .port(
10
-            env::get_value("endpoint.port".to_string())
11
-                .parse::<u16>()
12
-                .unwrap(),
13
-        )
14
-        .workers(env::get_value("endpoint.workers".to_string())
15
-                .parse::<u16>()
16
-                .unwrap_or_else(|_| 2));
7
+    let rocket_config = rocket::config::Config::build(
8
+        rocket::config::Environment::active()
9
+            .expect("Unknown ROCKET_ENV value! (enum: {Development, Staging, Production})"),
10
+    )
11
+    .address(env::get_value("endpoint.host".to_string()))
12
+    .port(
13
+        env::get_value("endpoint.port".to_string())
14
+            .parse::<u16>()
15
+            .unwrap(),
16
+    )
17
+    .workers(
18
+        env::get_value("endpoint.workers".to_string())
19
+            .parse::<u16>()
20
+            .unwrap_or_else(|_| 2),
21
+    );
17 22
 
18 23
     unsafe {
19 24
         kibou::raito_fe::BYPASS_API = &true;

+ 8
- 8
src/database/mod.rs View File

@@ -8,7 +8,7 @@ use env;
8 8
 use regex::Regex;
9 9
 use rocket::http::Status;
10 10
 use rocket::request::{self, FromRequest};
11
-use rocket::{Outcome, Request, State};
11
+use rocket::{Outcome, Request};
12 12
 
13 13
 pub type Pool = r2d2::Pool<ConnectionManager<PgConnection>>;
14 14
 pub struct PooledConnection(pub r2d2::PooledConnection<ConnectionManager<PgConnection>>);
@@ -35,13 +35,13 @@ impl std::ops::Deref for PooledConnection {
35 35
 }
36 36
 
37 37
 #[deprecated]
38
-pub fn establish_connection() -> PgConnection {
39
-    PgConnection::establish(&prepare_postgres_url()).unwrap_or_else(|_| {
40
-        panic!(format!(
41
-            "Could not connect to {url}",
42
-            url = &prepare_postgres_url()
43
-        ))
44
-    })
38
+pub fn establish_connection() -> PooledConnection {
39
+    // Originally this always established a new database connection, now this is just gonna
40
+    // return a new connection from the pool until this function can be removed completely.
41
+    //
42
+    // TODO: Remove all references of this function!
43
+
44
+    return PooledConnection(POOL.get().unwrap());
45 45
 }
46 46
 
47 47
 pub fn initialize_pool() -> Pool {

+ 5
- 2
src/env.rs View File

@@ -6,13 +6,16 @@ pub fn get_value(key: String) -> String {
6 6
         Ok(Environment::Development) => "development",
7 7
         Ok(Environment::Staging) => "staging",
8 8
         Ok(Environment::Production) => "production",
9
-        Err(_) => "development"
9
+        Err(_) => "development",
10 10
     };
11 11
 
12 12
     set_default_config_values(&mut config);
13 13
 
14 14
     config
15
-        .merge(config::File::with_name(&format!("env.{}.toml", environment)))
15
+        .merge(config::File::with_name(&format!(
16
+            "env.{}.toml",
17
+            environment
18
+        )))
16 19
         .expect("Environment config not found!");
17 20
 
18 21
     match config.get_str(&key) {

+ 1
- 1
src/kibou_api/mod.rs View File

@@ -141,7 +141,7 @@ pub fn status_build(
141 141
     let mut receipients: Vec<String> = Vec::new();
142 142
     let mut inboxes: Vec<String> = Vec::new();
143 143
     let mut tags: Vec<serde_json::Value> = Vec::new();
144
-    let mut in_reply_to_id: Option<String>;
144
+    let in_reply_to_id: Option<String>;
145 145
 
146 146
     let parsed_mentions = parse_mentions(html::to_plain_text(&content));
147 147
     direct_receipients.extend(parsed_mentions.0);

+ 1
- 8
src/mastodon_api/controller.rs View File

@@ -3,20 +3,13 @@ use activity::{
3 3
     get_activities_by_id, get_ap_object_by_id, get_ap_object_replies_by_id,
4 4
     type_exists_for_object_id,
5 5
 };
6
-use activitypub;
7 6
 use actor;
8
-use actor::get_actor_by_id;
9
-use actor::get_actor_by_uri;
10 7
 use chrono;
11 8
 use chrono::Utc;
12
-use core::borrow::Borrow;
13 9
 use database;
14 10
 use database::PooledConnection;
15
-use diesel::PgConnection;
16 11
 use env;
17 12
 use kibou_api;
18
-use lru::LruCache;
19
-use mastodon_api::routes::status;
20 13
 use mastodon_api::{
21 14
     Account, Attachment, HomeTimeline, Instance, Notification, PublicTimeline, RegistrationForm,
22 15
     Relationship, Source, Status, StatusForm, MASTODON_API_ACCOUNT_CACHE,
@@ -25,7 +18,7 @@ use mastodon_api::{
25 18
 use notification::notifications_for_actor;
26 19
 use oauth;
27 20
 use oauth::application::Application as OAuthApplication;
28
-use oauth::token::{verify_token, Token};
21
+use oauth::token::verify_token;
29 22
 use regex::Regex;
30 23
 use rocket_contrib::json;
31 24
 use rocket_contrib::json::JsonValue;

+ 266
- 222
src/mastodon_api/mod.rs View File

@@ -9,7 +9,6 @@ use actor::{count_followees, get_actor_by_uri, Actor};
9 9
 use database;
10 10
 use database::PooledConnection;
11 11
 use env;
12
-use lru::LruCache;
13 12
 use rocket::request;
14 13
 use rocket::request::FromRequest;
15 14
 use rocket::request::Request;
@@ -211,6 +210,10 @@ pub struct Tag {
211 210
     pub history: Option<serde_json::Value>,
212 211
 }
213 212
 
213
+// Entities throw an empty error if they fail serializing. Entities should only fail silently, as
214
+// these errors do not show up on the API. Furthermore should serializing a Mastodon-API entity
215
+// never panic.
216
+
214 217
 impl Account {
215 218
     pub fn from_actor(
216 219
         pooled_connection: &PooledConnection,
@@ -285,15 +288,16 @@ impl Notification {
285 288
             serde_json::from_value(activity.data);
286 289
         let pooled_connection = &PooledConnection(database::POOL.get().unwrap());
287 290
 
288
-        if activitypub_activity.is_ok() {
289
-            return Notification::from_activitystreams(
290
-                pooled_connection,
291
-                activity.id,
292
-                activitypub_activity.unwrap(),
293
-            );
294
-        } else {
295
-            return Err(());
296
-        }
291
+        match activitypub_activity {
292
+            Ok(activity_inner) => {
293
+                return Notification::from_activitystreams(
294
+                    pooled_connection,
295
+                    activity.id,
296
+                    activity_inner,
297
+                )
298
+            }
299
+            Err(_) => return Err(()),
300
+        };
297 301
     }
298 302
 
299 303
     fn from_activitystreams(
@@ -301,51 +305,61 @@ impl Notification {
301 305
         activity_id: i64,
302 306
         activity: activitypub::activity::Activity,
303 307
     ) -> Result<Self, ()> {
304
-        let account = serde_json::from_value(
308
+        let account_result: Result<Account, serde_json::Error> = serde_json::from_value(
305 309
             controller::cached_account(
306 310
                 pooled_connection,
307 311
                 Box::leak(activity.actor.to_owned().into_boxed_str()),
308 312
             )
309 313
             .into(),
310
-        )
311
-        .unwrap();
312
-        let notification_type = match activity._type.as_str() {
313
-            "Follow" => String::from("follow"),
314
-            "Create" => String::from("mention"),
315
-            "Announce" => String::from("reblog"),
316
-            "Like" => String::from("favourite"),
317
-            _ => String::from(""),
318
-        };
319
-        let mut status: Option<Status> = None;
320
-
321
-        if !notification_type.is_empty() {
322
-            if &notification_type == "reblog" || &notification_type == "favourite" {
323
-                status = serde_json::from_value(
324
-                    controller::status_by_id(
325
-                        pooled_connection,
326
-                        get_ap_object_by_id(pooled_connection, activity.object.as_str().unwrap())
327
-                            .unwrap()
328
-                            .id,
329
-                    )
330
-                    .into(),
331
-                )
332
-                .unwrap_or_else(|_| None);
333
-            } else if &notification_type == "mention" {
334
-                status = serde_json::from_value(
335
-                    controller::status_by_id(pooled_connection, activity_id).into(),
336
-                )
337
-                .unwrap();
338
-            }
314
+        );
315
+
316
+        // An activity without an actor should never occur, but if it does,
317
+        // this function needs to fail.
318
+        match account_result {
319
+            Ok(account) => {
320
+                let notification_type = match activity._type.as_str() {
321
+                    "Follow" => String::from("follow"),
322
+                    "Create" => String::from("mention"),
323
+                    "Announce" => String::from("reblog"),
324
+                    "Like" => String::from("favourite"),
325
+                    _ => String::from(""),
326
+                };
327
+                let mut status: Option<Status> = None;
328
+
329
+                if !notification_type.is_empty() {
330
+                    if &notification_type == "reblog" || &notification_type == "favourite" {
331
+                        status = serde_json::from_value(
332
+                            controller::status_by_id(
333
+                                pooled_connection,
334
+                                get_ap_object_by_id(
335
+                                    pooled_connection,
336
+                                    activity.object.as_str().unwrap(),
337
+                                )
338
+                                .unwrap()
339
+                                .id,
340
+                            )
341
+                            .into(),
342
+                        )
343
+                        .unwrap_or_else(|_| None);
344
+                    } else if &notification_type == "mention" {
345
+                        status = serde_json::from_value(
346
+                            controller::status_by_id(pooled_connection, activity_id).into(),
347
+                        )
348
+                        .unwrap_or_else(|_| None);
349
+                    }
339 350
 
340
-            return Ok(Notification {
341
-                id: activity_id.to_string(),
342
-                _type: notification_type,
343
-                created_at: activity.published,
344
-                account: account,
345
-                status: status,
346
-            });
347
-        } else {
348
-            return Err(());
351
+                    return Ok(Notification {
352
+                        id: activity_id.to_string(),
353
+                        _type: notification_type,
354
+                        created_at: activity.published,
355
+                        account: account,
356
+                        status: status,
357
+                    });
358
+                } else {
359
+                    return Err(());
360
+                }
361
+            }
362
+            Err(_) => Err(()),
349 363
         }
350 364
     }
351 365
 }
@@ -356,193 +370,223 @@ impl Status {
356 370
             serde_json::from_value(activity.data);
357 371
         let pooled_connection = &PooledConnection(database::POOL.get().unwrap());
358 372
 
359
-        if activitypub_activity.is_ok() {
360
-            return Status::from_activitystreams(
361
-                pooled_connection,
362
-                activity.id,
363
-                activitypub_activity.unwrap(),
364
-            );
365
-        } else {
366
-            return Err(());
367
-        }
373
+        match activitypub_activity {
374
+            Ok(activity_inner) => {
375
+                return Status::from_activitystreams(pooled_connection, activity.id, activity_inner)
376
+            }
377
+            Err(_) => return Err(()),
378
+        };
368 379
     }
369 380
     fn from_activitystreams(
370 381
         pooled_connection: &PooledConnection,
371 382
         activity_id: i64,
372 383
         activity: activitypub::activity::Activity,
373 384
     ) -> Result<Self, ()> {
374
-        let account: Account = serde_json::from_value(
385
+        let account_result: Result<Account, serde_json::Error> = serde_json::from_value(
375 386
             controller::cached_account(pooled_connection, &activity.actor).into(),
376
-        )
377
-        .unwrap();
378
-
379
-        let mut mentions: Vec<Mention> = Vec::new();
380
-        for actor in &activity.to {
381
-            let mention_account: Result<Account, serde_json::Error> = serde_json::from_value(
382
-                controller::cached_account(pooled_connection, &actor).into(),
383
-            );
384
-            // Just unwrapping every account would mean that an entire status fails serializing,
385
-            // because of one invalid account.
386
-            match mention_account {
387
-                Ok(mention_account) => mentions.push(Mention {
388
-                    url: mention_account.url,
389
-                    username: mention_account.username,
390
-                    acct: mention_account.acct,
391
-                    id: mention_account.id,
392
-                }),
393
-                Err(_) => (),
394
-            };
395
-        }
396
-
397
-        // The 'Public' and 'Unlisted' scope can be easily determined by the existence of
398
-        // `https://www.w3.org/ns/activitystreams#Public` in either the 'to' or 'cc' field.
399
-        //
400
-        // Note that different formats like `as:Public` have already been normalized to
401
-        // `https://www.w3.org/ns/activitystreams#Public` in activitypub::validator.
402
-        let visibility = if activity
403
-            .to
404
-            .contains(&"https://www.w3.org/ns/activitystreams#Public".to_string())
405
-        {
406
-            String::from("public")
407
-        } else if activity
408
-            .cc
409
-            .contains(&"https://www.w3.org/ns/activitystreams#Public".to_string())
410
-        {
411
-            String::from("unlisted")
412
-        // XX - This might cause issues, as the 'Followers' endpoint of remote actors might differ
413
-        // from Kibou's schema. But as of now Kibou does not keep track of that endpoint.
414
-        } else if activity.to.contains(&format!("{}/followers", account.url)) {
415
-            String::from("private")
416
-        } else {
417
-            String::from("direct")
418
-        };
419
-
420
-        match activity._type.as_str() {
421
-            "Create" => {
422
-                let inner_object: activitypub::activity::Object =
423
-                    serde_json::from_value(activity.object.clone()).unwrap();
424
-                let mut in_reply_to: Option<String> = None;
425
-                let mut in_reply_to_account: Option<String> = None;
426
-                if inner_object.inReplyTo.is_some() {
427
-                    in_reply_to = match get_ap_object_by_id(
428
-                        pooled_connection,
429
-                        &inner_object.inReplyTo.unwrap(),
430
-                    ) {
431
-                        Ok(parent_activity) => {
432
-                            in_reply_to_account = match get_actor_by_uri(
433
-                                pooled_connection,
434
-                                &parent_activity.data["actor"].as_str().unwrap(),
435
-                            ) {
436
-                                Ok(parent_actor) => Some(parent_actor.id.to_string()),
437
-                                Err(_) => None,
438
-                            };
439
-                            Some(parent_activity.id.to_string())
440
-                        }
441
-                        Err(_) => None,
387
+        );
388
+
389
+        // An activity without an actor should never occur, but if it does,
390
+        // this function needs to fail.
391
+        match account_result {
392
+            Ok(account) => {
393
+                let mut mentions: Vec<Mention> = Vec::new();
394
+                for actor in &activity.to {
395
+                    let mention_account: Result<Account, serde_json::Error> =
396
+                        serde_json::from_value(
397
+                            controller::cached_account(pooled_connection, &actor).into(),
398
+                        );
399
+                    // Just unwrapping every account would mean that an entire status fails serializing,
400
+                    // because of one invalid account.
401
+                    match mention_account {
402
+                        Ok(mention_account) => mentions.push(Mention {
403
+                            url: mention_account.url,
404
+                            username: mention_account.username,
405
+                            acct: mention_account.acct,
406
+                            id: mention_account.id,
407
+                        }),
408
+                        Err(_) => (),
442 409
                     };
443 410
                 }
444 411
 
445
-                let mut media_attachments: Vec<Attachment> = Vec::new();
446
-                match activity.object.get("attachment") {
447
-                    Some(attachments) => {
448
-                        let attachments: Vec<activitypub::Attachment> =
449
-                            serde_json::from_value(activity.object["attachment"].to_owned())
450
-                                .unwrap();
451
-
452
-                        for attachment in attachments {
453
-                            media_attachments.push(Attachment {
454
-                                id: attachment
455
-                                    .name
456
-                                    .unwrap_or_else(|| String::from("Unnamed attachment")),
457
-                                _type: String::from("image"),
458
-                                url: attachment.url.clone(),
459
-                                remote_url: Some(attachment.url.clone()),
460
-                                preview_url: attachment.url,
461
-                                text_url: None,
462
-                                meta: None,
463
-                                description: attachment.content,
464
-                            });
412
+                // The 'Public' and 'Unlisted' scope can be easily determined by the existence of
413
+                // `https://www.w3.org/ns/activitystreams#Public` in either the 'to' or 'cc' field.
414
+                //
415
+                // Note that different formats like `as:Public` have already been normalized to
416
+                // `https://www.w3.org/ns/activitystreams#Public` in activitypub::validator.
417
+                let visibility = if activity
418
+                    .to
419
+                    .contains(&"https://www.w3.org/ns/activitystreams#Public".to_string())
420
+                {
421
+                    String::from("public")
422
+                } else if activity
423
+                    .cc
424
+                    .contains(&"https://www.w3.org/ns/activitystreams#Public".to_string())
425
+                {
426
+                    String::from("unlisted")
427
+                // XX - This might cause issues, as the 'Followers' endpoint of remote actors might differ
428
+                // from Kibou's schema. But as of now Kibou does not keep track of that endpoint.
429
+                } else if activity.to.contains(&format!("{}/followers", account.url)) {
430
+                    String::from("private")
431
+                } else {
432
+                    String::from("direct")
433
+                };
434
+
435
+                match activity._type.as_str() {
436
+                    "Create" => {
437
+                        let inner_object_result: Result<
438
+                            activitypub::activity::Object,
439
+                            serde_json::Error,
440
+                        > = serde_json::from_value(activity.object.clone());
441
+                        match inner_object_result {
442
+                            Ok(inner_object) => {
443
+                                let mut in_reply_to: Option<String> = None;
444
+                                let mut in_reply_to_account: Option<String> = None;
445
+                                if inner_object.inReplyTo.is_some() {
446
+                                    in_reply_to = match get_ap_object_by_id(
447
+                                        pooled_connection,
448
+                                        &inner_object.inReplyTo.unwrap(),
449
+                                    ) {
450
+                                        Ok(parent_activity) => {
451
+                                            in_reply_to_account = match get_actor_by_uri(
452
+                                                pooled_connection,
453
+                                                &parent_activity.data["actor"].as_str().unwrap(),
454
+                                            ) {
455
+                                                Ok(parent_actor) => {
456
+                                                    Some(parent_actor.id.to_string())
457
+                                                }
458
+                                                Err(_) => None,
459
+                                            };
460
+                                            Some(parent_activity.id.to_string())
461
+                                        }
462
+                                        Err(_) => None,
463
+                                    };
464
+                                }
465
+
466
+                                let mut media_attachments: Vec<Attachment> = Vec::new();
467
+                                match activity.object.get("attachment") {
468
+                                    Some(_attachments) => {
469
+                                        let attachments: Vec<activitypub::Attachment> =
470
+                                            serde_json::from_value(
471
+                                                activity.object["attachment"].to_owned(),
472
+                                            )
473
+                                            .unwrap_or_else(|_| Vec::new());
474
+
475
+                                        for attachment in attachments {
476
+                                            media_attachments.push(Attachment {
477
+                                                id: attachment.name.unwrap_or_else(|| {
478
+                                                    String::from("Unnamed attachment")
479
+                                                }),
480
+                                                _type: String::from("image"),
481
+                                                url: attachment.url.clone(),
482
+                                                remote_url: Some(attachment.url.clone()),
483
+                                                preview_url: attachment.url,
484
+                                                text_url: None,
485
+                                                meta: None,
486
+                                                description: attachment.content,
487
+                                            });
488
+                                        }
489
+                                    }
490
+                                    None => (),
491
+                                }
492
+
493
+                                let favourites = count_ap_object_reactions_by_id(
494
+                                    pooled_connection,
495
+                                    &inner_object.id,
496
+                                    "Like",
497
+                                )
498
+                                .unwrap_or_else(|_| 0)
499
+                                    as i64;
500
+                                let reblogs = count_ap_object_reactions_by_id(
501
+                                    pooled_connection,
502
+                                    &inner_object.id,
503
+                                    "Announce",
504
+                                )
505
+                                .unwrap_or_else(|_| 0)
506
+                                    as i64;
507
+                                let replies = count_ap_object_replies_by_id(
508
+                                    pooled_connection,
509
+                                    &inner_object.id,
510
+                                )
511
+                                .unwrap_or_else(|_| 0)
512
+                                    as i64;
513
+                                return Ok(Status {
514
+                                    id: activity_id.to_string(),
515
+                                    uri: inner_object.id.clone(),
516
+                                    url: Some(inner_object.id.clone()),
517
+                                    account: account,
518
+                                    in_reply_to_id: in_reply_to,
519
+                                    in_reply_to_account_id: in_reply_to_account,
520
+                                    reblog: None,
521
+                                    content: inner_object.content,
522
+                                    created_at: inner_object.published,
523
+                                    emojis: vec![],
524
+                                    replies_count: replies,
525
+                                    reblogs_count: reblogs,
526
+                                    favourites_count: favourites,
527
+                                    reblogged: Some(false),
528
+                                    favourited: Some(false),
529
+                                    muted: None,
530
+                                    sensitive: inner_object.sensitive.unwrap_or_else(|| false),
531
+                                    spoiler_text: "".to_string(),
532
+                                    visibility: visibility,
533
+                                    media_attachments: media_attachments,
534
+                                    mentions: mentions,
535
+                                    tags: vec![],
536
+                                    application: serde_json::json!({"name": "Web", "website": null}),
537
+                                    language: None,
538
+                                    pinned: None,
539
+                                });
540
+                            }
541
+                            Err(_) => Err(()),
465 542
                         }
466 543
                     }
467
-                    None => (),
468
-                }
469
-
470
-                let favourites =
471
-                    count_ap_object_reactions_by_id(pooled_connection, &inner_object.id, "Like")
472
-                        .unwrap_or_else(|_| 0) as i64;
473
-                let reblogs = count_ap_object_reactions_by_id(
474
-                    pooled_connection,
475
-                    &inner_object.id,
476
-                    "Announce",
477
-                )
478
-                .unwrap_or_else(|_| 0) as i64;
479
-                let replies = count_ap_object_replies_by_id(pooled_connection, &inner_object.id)
480
-                    .unwrap_or_else(|_| 0) as i64;
481
-                return Ok(Status {
482
-                    id: activity_id.to_string(),
483
-                    uri: inner_object.id.clone(),
484
-                    url: Some(inner_object.id.clone()),
485
-                    account: account,
486
-                    in_reply_to_id: in_reply_to,
487
-                    in_reply_to_account_id: in_reply_to_account,
488
-                    reblog: None,
489
-                    content: inner_object.content,
490
-                    created_at: inner_object.published,
491
-                    emojis: vec![],
492
-                    replies_count: replies,
493
-                    reblogs_count: reblogs,
494
-                    favourites_count: favourites,
495
-                    reblogged: Some(false),
496
-                    favourited: Some(false),
497
-                    muted: None,
498
-                    sensitive: inner_object.sensitive.unwrap_or_else(|| false),
499
-                    spoiler_text: "".to_string(),
500
-                    visibility: visibility,
501
-                    media_attachments: media_attachments,
502
-                    mentions: mentions,
503
-                    tags: vec![],
504
-                    application: serde_json::json!({"name": "Web", "website": null}),
505
-                    language: None,
506
-                    pinned: None,
507
-                });
508
-            }
509
-            "Announce" => {
510
-                match get_ap_object_by_id(&pooled_connection, activity.object.as_str().unwrap()) {
511
-                    Ok(reblog) => {
512
-                        let serialized_reblog: Status = Status::try_from(reblog).unwrap();
513
-
514
-                        Ok(Status {
515
-                            id: activity_id.to_string(),
516
-                            uri: activity.id.clone(),
517
-                            url: Some(activity.id.clone()),
518
-                            account: account,
519
-                            in_reply_to_id: None,
520
-                            in_reply_to_account_id: None,
521
-                            reblog: Some(serde_json::to_value(serialized_reblog).unwrap()),
522
-                            content: String::from("reblog"),
523
-                            created_at: activity.published,
524
-                            emojis: vec![],
525
-                            replies_count: 0,
526
-                            reblogs_count: 0,
527
-                            favourites_count: 0,
528
-                            reblogged: Some(false),
529
-                            favourited: Some(false),
530
-                            muted: Some(false),
531
-                            sensitive: false,
532
-                            spoiler_text: String::new(),
533
-                            visibility: visibility,
534
-                            media_attachments: vec![],
535
-                            mentions: vec![],
536
-                            tags: vec![],
537
-                            application: serde_json::json!({"name": "Web", "website": null}),
538
-                            language: None,
539
-                            pinned: None,
540
-                        })
541
-                    }
542
-                    Err(_) => Err(()),
544
+                    "Announce" => match activity.object.as_str() {
545
+                        Some(object_id) => {
546
+                            match get_ap_object_by_id(&pooled_connection, object_id) {
547
+                                Ok(reblog) => match Status::try_from(reblog) {
548
+                                    Ok(serialized_reblog) => {
549
+                                        return Ok(Status {
550
+                                            id: activity_id.to_string(),
551
+                                            uri: activity.id.clone(),
552
+                                            url: Some(activity.id.clone()),
553
+                                            account: account,
554
+                                            in_reply_to_id: None,
555
+                                            in_reply_to_account_id: None,
556
+                                            reblog: Some(
557
+                                                serde_json::to_value(serialized_reblog).unwrap(),
558
+                                            ),
559
+                                            content: String::from("reblog"),
560
+                                            created_at: activity.published,
561
+                                            emojis: vec![],
562
+                                            replies_count: 0,
563
+                                            reblogs_count: 0,
564
+                                            favourites_count: 0,
565
+                                            reblogged: Some(false),
566
+                                            favourited: Some(false),
567
+                                            muted: Some(false),
568
+                                            sensitive: false,
569
+                                            spoiler_text: String::new(),
570
+                                            visibility: visibility,
571
+                                            media_attachments: vec![],
572
+                                            mentions: vec![],
573
+                                            tags: vec![],
574
+                                            application: serde_json::json!({"name": "Web", "website": null}),
575
+                                            language: None,
576
+                                            pinned: None,
577
+                                        })
578
+                                    }
579
+                                    Err(_) => Err(()),
580
+                                },
581
+                                Err(_) => Err(()),
582
+                            }
583
+                        }
584
+                        None => Err(()),
585
+                    },
586
+                    _ => Err(()),
543 587
                 }
544 588
             }
545
-            _ => return Err(()),
589
+            Err(_) => Err(()),
546 590
         }
547 591
     }
548 592
 }

+ 2
- 2
src/mastodon_api/routes.rs View File

@@ -1,5 +1,5 @@
1 1
 use database::PooledConnection;
2
-use lru::LruCache;
2
+
3 3
 use mastodon_api::controller;
4 4
 use mastodon_api::{
5 5
     parse_authorization_header, ApplicationForm, AuthorizationHeader, HomeTimeline, PublicTimeline,
@@ -7,7 +7,7 @@ use mastodon_api::{
7 7
 };
8 8
 use oauth::application::Application;
9 9
 use rocket::request::LenientForm;
10
-use rocket::State;
10
+
11 11
 use rocket_contrib::json::JsonValue;
12 12
 
13 13
 #[get("/api/v1/accounts/<id>")]

+ 1
- 1
src/raito_fe/api_controller.rs View File

@@ -1,6 +1,6 @@
1 1
 use actor;
2 2
 use database::PooledConnection;
3
-use lru::LruCache;
3
+
4 4
 use mastodon_api::{
5 5
     controller, routes, Account, AuthorizationHeader, HomeTimeline, Notification, PublicTimeline,
6 6
     RegistrationForm, Relationship, Status, StatusForm,

+ 0
- 3
src/raito_fe/mod.rs View File

@@ -7,12 +7,9 @@ use env;
7 7
 use mastodon_api;
8 8
 use rocket::outcome::Outcome;
9 9
 use rocket::request::{self, FromRequest, Request};
10
-use rocket::State;
11
-use std::collections::hash_map::IntoIter;
12 10
 use std::collections::HashMap;
13 11
 use std::fs::File;
14 12
 use std::io::prelude::*;
15
-
16 13
 pub static mut BYPASS_API: &'static bool = &false;
17 14
 pub static mut MASTODON_API_BASE_URI: &'static str = "127.0.0.1";
18 15
 

+ 2
- 6
src/raito_fe/renderer.rs View File

@@ -4,7 +4,6 @@ use chrono::prelude::*;
4 4
 use database;
5 5
 use database::PooledConnection;
6 6
 use env;
7
-use lru::LruCache;
8 7
 use mastodon_api::{Notification, RegistrationForm, Status, StatusForm};
9 8
 use raito_fe::{self, Configuration, LoginForm};
10 9
 use rocket::http::{Cookie, Cookies};
@@ -120,11 +119,9 @@ pub fn account_by_username(
120 119
     configuration: &Configuration,
121 120
     username: String,
122 121
 ) -> Template {
123
-    let database = database::establish_connection();
124
-
125 122
     let mut context = HashMap::<String, String>::new();
126 123
     context.extend(configuration.context.clone());
127
-    match actor::get_local_actor_by_preferred_username(&database, &username) {
124
+    match actor::get_local_actor_by_preferred_username(pooled_connection, &username) {
128 125
         Ok(actor) => account_by_local_id(pooled_connection, configuration, actor.id.to_string()),
129 126
         Err(_) => Template::render("raito_fe/index", context),
130 127
     }
@@ -330,7 +327,6 @@ pub fn conversation_by_uri(
330 327
     configuration: &Configuration,
331 328
     id: String,
332 329
 ) -> Template {
333
-    let database = database::establish_connection();
334 330
     let mut context = HashMap::<String, String>::new();
335 331
     let object_id = format!(
336 332
         "{}://{}/objects/{}",
@@ -340,7 +336,7 @@ pub fn conversation_by_uri(
340 336
     );
341 337
 
342 338
     context.extend(configuration.context.clone());
343
-    match activity::get_ap_object_by_id(&database, &object_id) {
339
+    match activity::get_ap_object_by_id(pooled_connection, &object_id) {
344 340
         Ok(activity) => conversation(pooled_connection, configuration, activity.id.to_string()),
345 341
         Err(_) => Template::render("raito_fe/index", context),
346 342
     }

+ 2
- 3
src/raito_fe/routes.rs View File

@@ -1,12 +1,11 @@
1
-use core::borrow::BorrowMut;
2 1
 use database::PooledConnection;
3
-use lru::LruCache;
2
+
4 3
 use mastodon_api::{RegistrationForm, StatusForm};
5 4
 use raito_fe::{renderer, Configuration, LoginForm};
6 5
 use rocket::http::Cookies;
7 6
 use rocket::request::LenientForm;
8 7
 use rocket::response::Redirect;
9
-use rocket::State;
8
+
10 9
 use rocket_contrib::templates::Template;
11 10
 
12 11
 #[get("/")]

Loading…
Cancel
Save