Browse Source

Implement nodeinfo and new environment variables

pull/19/head
Toromino 3 months ago
parent
commit
ca05d14913
Signed by: Foxhkron <foxhkron@toromino.de> GPG Key ID: BF85EE6A14C3CE36
4 changed files with 131 additions and 1 deletions
  1. 2
    0
      env.toml.sample
  2. 8
    1
      src/lib.rs
  3. 1
    0
      src/well_known/mod.rs
  4. 120
    0
      src/well_known/nodeinfo.rs

+ 2
- 0
env.toml.sample View File

@@ -19,3 +19,5 @@ port = 8000
19 19
 
20 20
 [node]
21 21
 name = "Kibou"
22
+description = "A Kibou instance"
23
+registrations_enabled = true

+ 8
- 1
src/lib.rs View File

@@ -38,5 +38,12 @@ pub fn rocket_app(config: rocket::config::Config) -> rocket::Rocket {
38 38
                 activitypub::routes::inbox
39 39
             ],
40 40
         )
41
-        .mount("/", routes![well_known::webfinger::webfinger])
41
+        .mount(
42
+            "/",
43
+            routes![
44
+                well_known::nodeinfo::nodeinfo_v2,
45
+                well_known::nodeinfo::nodeinfo_v2_1,
46
+                well_known::webfinger::webfinger
47
+            ],
48
+        )
42 49
 }

+ 1
- 0
src/well_known/mod.rs View File

@@ -1 +1,2 @@
1
+pub mod nodeinfo;
1 2
 pub mod webfinger;

+ 120
- 0
src/well_known/nodeinfo.rs View File

@@ -0,0 +1,120 @@
1
+use activity::count_local_ap_notes;
2
+use actor::count_local_actors;
3
+use database;
4
+use env;
5
+use rocket_contrib::json;
6
+use rocket_contrib::json::JsonValue;
7
+
8
+#[get("/.well-known/nodeinfo")]
9
+pub fn nodeinfo() -> JsonValue {
10
+    json!({
11
+           "links": [
12
+           {
13
+               "rel": "http://nodeinfo.diaspora.software/ns/schema/2.0",
14
+               "href": format!("{}://{}/nodeinfo/2.0.json",
15
+               env::get_value(String::from("endpoint.base_scheme")),
16
+               env::get_value(String::from("endpoint.base_domain")))
17
+           },
18
+           {
19
+               "rel": "http://nodeinfo.diaspora.software/ns/schema/2.1",
20
+               "href": format!("{}://{}/nodeinfo/2.1.json",
21
+               env::get_value(String::from("endpoint.base_scheme")),
22
+               env::get_value(String::from("endpoint.base_domain")))
23
+           }]
24
+    })
25
+}
26
+
27
+// NoteInfo protocol version 2.0 according to the schema at
28
+// http://nodeinfo.diaspora.software/ns/schema/2.0
29
+#[get("/nodeinfo/2.0.json")]
30
+pub fn nodeinfo_v2() -> JsonValue {
31
+    json!({
32
+        "version": "2.0",
33
+        "software": {
34
+            "version": format!("{}-testing",env!("CARGO_PKG_VERSION")),
35
+            "name": env!("CARGO_PKG_NAME")
36
+        },
37
+        "protocols": [
38
+        "activitypub"
39
+        ],
40
+        "nodeName": env::get_value(String::from("node.name")),
41
+        "nodeDescription": env::get_value(String::from("node.description")),
42
+        "services":{
43
+            "outbound": [
44
+
45
+            ],
46
+            "inbound": [
47
+
48
+            ]
49
+        },
50
+        "openRegistrations": get_open_registrations(),
51
+        "usage": {
52
+            "users": {
53
+                "total": get_total_users()
54
+            },
55
+            "localPosts": get_local_posts()
56
+        },
57
+        "metadata": {
58
+
59
+        },
60
+        "features": [
61
+            "webfinger"
62
+        ]
63
+    })
64
+}
65
+
66
+// NoteInfo protocol version 2.1 according to the schema at
67
+// http://nodeinfo.diaspora.software/ns/schema/2.1
68
+#[get("/nodeinfo/2.1.json")]
69
+pub fn nodeinfo_v2_1() -> JsonValue {
70
+    json!({
71
+        "version": "2.1",
72
+        "software": {
73
+            "version": format!("{}-testing",env!("CARGO_PKG_VERSION")),
74
+            "name": env!("CARGO_PKG_NAME"),
75
+            "repository": "https://git.cybre.club/kibouproject/kibou"
76
+        },
77
+        "protocols": [
78
+        "activitypub"
79
+        ],
80
+        "nodeName": env::get_value(String::from("node.name")),
81
+        "nodeDescription": env::get_value(String::from("node.description")),
82
+        "services":{
83
+            "outbound": [
84
+
85
+            ],
86
+            "inbound": [
87
+
88
+            ]
89
+        },
90
+        "openRegistrations": get_open_registrations(),
91
+        "usage": {
92
+            "users": {
93
+                "total": get_total_users()
94
+            },
95
+            "localPosts": get_local_posts()
96
+        },
97
+        "metadata": {
98
+
99
+        },
100
+        "features": [
101
+            "webfinger"
102
+        ]
103
+    })
104
+}
105
+
106
+fn get_local_posts() -> usize {
107
+    let database = database::establish_connection();
108
+    count_local_ap_notes(&database).unwrap_or_else(|_| 0)
109
+}
110
+
111
+fn get_open_registrations() -> bool {
112
+    env::get_value(String::from("node.registrations_enabled"))
113
+        .parse::<bool>()
114
+        .unwrap_or_else(|_| false)
115
+}
116
+
117
+fn get_total_users() -> usize {
118
+    let database = database::establish_connection();
119
+    count_local_actors(&database).unwrap_or_else(|_| 0)
120
+}

Loading…
Cancel
Save