caracal 0.4.0

Nostr client for Gemini
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
mod bookmarks;
mod community;
mod contacts;
mod dm;
mod following;
mod hashtags;
mod inbox;
mod manual;
mod mute;
mod nostr_db;
mod notes;
mod nsite;
mod outbox;
mod polls;
mod profile;
mod remote_signer;
mod response;
mod search;
mod sec;
mod settings;
mod www;

mod route_prelude;

pub use response::*;

pub use bookmarks::{
    bookmark_public_add, public_bookmarks, public_bookmarks_delete_event,
};
pub use community::{
    nostriches_list_all, nostriches_list_recent, nostriches_search,
};
pub use contacts::contact_list_index;
pub use dm::{dm_all, person_dm_conversation, person_send_dm};
pub use following::{follow, person_follow, person_unfollow};
pub use hashtags::{hashtags_index, notes_by_hashtag};
pub use inbox::inbox;
pub use manual::manual_root;
pub use mute::{person_mute, person_unmute};
pub use nostr_db::db_purge;
pub use notes::{
    draft_body_append, draft_edit, draft_longform_body_append,
    draft_longform_delete_last_line, draft_longform_edit, draft_longform_new,
    draft_longform_new_with_id, draft_longform_post, draft_longform_preview,
    draft_longform_set, draft_new, draft_post, draft_upload_file, note_delete,
    note_react_custom, note_react_select, note_reply, note_repost, note_thread,
    post_note,
};
pub use nsite::nsites_index;
pub use outbox::outbox;
pub use polls::{poll_details, poll_respond, polls_list};
pub use profile::{
    profile_edit, profile_index, profile_init_reset, profile_upload,
};
pub use remote_signer::{
    nostr_connect_bunker, nostr_connect_client_initiated, nostr_connect_index,
};
pub use search::search_content;
pub use settings::{
    relay_modify, relays_add_from_popular, relays_settings, settings_index,
    ui_settings, ui_settings_chlocale,
};
pub use www::route_www_get;

use crate::aska::WindTemplate;
use crate::emoji::*;
use crate::nostru;
use crate::rendering;
use crate::user::{CaracalUser, lookup_user, lookup_user_mut};
use crate::util::fingerprint;

use urlencoding::{decode, encode};

use askama::Template;
use colored::*;

use openssl::nid::Nid;
use windmark_titanesque::{context::RouteContext, response::Response};

use chrono::DateTime;
use nostr_sdk::prelude::*;

use mdiu::Gemtext;
use mdiu::{Block, Content, Level, Markup};

use openssl::x509::X509;

use rust_i18n::t;

use std::future::IntoFuture;

rust_i18n::i18n!("locales");

#[derive(Template, Clone)]
#[template(path = "index.gmi", escape = "txt")]
struct IndexTemplate<'a> {
    connect_uri: Option<&'a NostrConnectURI>,
    connect_relays: Option<Vec<String>>,
    profile_metadata: Option<Metadata>,
}

#[derive(Template, Clone)]
#[template(path = "index_nocert.gmi", escape = "txt")]
struct IndexNoCertTemplate {}

#[derive(Template, Clone)]
#[template(path = "about.gmi", escape = "txt")]
struct AboutTemplate {}

#[derive(Template)]
#[template(path = "feed.gmi", escape = "txt")]
pub struct FeedTemplate {
    b1: Block,
    edoc: Vec<Block>,
}

#[derive(Template)]
#[template(path = "person_index.gmi", escape = "txt")]
pub struct PersonIndexTemplate {
    npub: String,
    metadata: Option<Metadata>,
    notes_blocks: Option<Vec<Block>>,
    payment_targets: Option<Event>,
}

pub fn gem(blocks: &[Block]) -> String {
    <Gemtext>::markup(blocks)
}

/// Formats a [`Timestamp`] to a human format
pub fn humanize_ts(ts: &Timestamp) -> String {
    if let Ok(dt) =
        DateTime::parse_from_rfc3339(ts.to_human_datetime().as_str())
    {
        format!("{}", dt.format("%a %b %e %Y, %T"))
    } else {
        String::from("Invalid date")
    }
}

/// Main route handler
/// If the user has a certificate, call the route
/// Otherwise, redirect to the "no cert" page to tell the user to create a cert
pub async fn guardian<R>(
    ctx: RouteContext,
    mut handler: impl FnMut(RouteContext, &'static mut CaracalUser) -> R
    + Send
    + Sync
    + 'static,
) -> Response
where
    R: IntoFuture<Output = Response> + Send + 'static,
{
    if let Some(cert) = &ctx.certificate {
        let Some(fingerprint) = fingerprint(cert) else {
            return Response::temporary_failure(
                "Could not load your certificate",
            );
        };

        // Print the request as gemtext
        println!(
            "{} {} {}",
            "=>".green(),
            "From:".red(),
            fingerprint.yellow()
        );
        println!("{} {}", "=>".yellow(), ctx.url.path().blue());

        let Some(user) = lookup_user_mut(fingerprint) else {
            return Response::temporary_failure("Cannot find user");
        };

        let subject = cert.subject_name();

        if let Some(org) = subject.entries_by_nid(Nid::ORGANIZATIONNAME).next()
        {
            let Ok(org) = org.data().as_utf8() else {
                return Response::temporary_failure(
                    "Failed to retrieve organization field in cert",
                );
            };

            // If the cert's "Organization" field = nostr, use Nostr Connect
            if org.to_string().to_lowercase() == "nostr" {
                if user.connect_uri.is_none()
                    && !ctx.url.path().starts_with("/connect")
                {
                    // No connect URI: render the nostr connect homepage (no redirect)
                    nostr_connect_index(ctx, user).await
                } else {
                    handler(ctx, user).await
                }
            } else {
                handler(ctx, user).await
            }
        } else {
            let resp = handler(ctx, user).await;

            match resp.status {
                10 => println!("{}", resp.status.to_string().blue()),
                20..=22 => println!("{}", resp.status.to_string().green()),
                _ => println!("{}", resp.status.to_string().red()),
            }

            resp
        }
    } else {
        Response::success(WindTemplate::render(IndexNoCertTemplate {}))
    }
}

pub async fn ugate(certificate: Option<X509>) -> Option<&'static CaracalUser> {
    if let Some(cert) = certificate {
        match fingerprint(&cert) {
            Some(fp) => lookup_user(fp),
            None => None,
        }
    } else {
        None
    }
}

pub async fn ugate_mut(
    certificate: Option<X509>,
) -> Option<&'static mut CaracalUser> {
    if let Some(cert) = certificate {
        match fingerprint(&cert) {
            Some(fp) => lookup_user_mut(fp),
            None => None,
        }
    } else {
        None
    }
}

pub async fn ugate_m(ctx: &RouteContext) -> Option<&'static mut CaracalUser> {
    match &ctx.certificate {
        Some(cert) => match fingerprint(cert) {
            Some(fp) => lookup_user_mut(fp),
            None => None,
        },
        None => None,
    }
}

pub async fn about(_ctx: RouteContext) -> Response {
    Response::success(WindTemplate::render(AboutTemplate {}))
}

pub async fn index(
    _ctx: RouteContext,
    user: &'static mut CaracalUser,
) -> Response {
    let Ok(signer) = user.client.signer().await else {
        return Response::temporary_failure("Signer error!");
    };

    let Ok(pubkey) = signer.get_public_key().await else {
        return Response::temporary_redirect("/connect");
    };

    let profile_metadata =
        nostru::get_user_metadata(&user.client, pubkey).await;

    let connect_relays = user
        .connect_uri
        .as_ref()
        .map(|uri| uri.relays().iter().map(|r| r.to_string()).collect());

    Response::success(WindTemplate::render(IndexTemplate {
        profile_metadata,
        connect_uri: user.connect_uri.as_ref(),
        connect_relays,
    }))
}

pub async fn feed(
    ctx: RouteContext,
    user: &'static mut CaracalUser,
) -> Response {
    let params = ctx.parameters.clone();
    let default = String::from("500");
    let limit_str = params.get("limit").unwrap_or(&default);

    let limit = match limit_str.parse::<usize>() {
        Ok(result) => result,
        Err(_error) => default.parse::<usize>().unwrap(),
    };

    let Ok(mut contacts) = user.contact_list_pubkeys().await else {
        return Response::temporary_failure("Failed to fetch contact list");
    };

    let mute_list = user.mute_list().await;

    // Remove muted public keys
    contacts.retain(|pk| !mute_list.public_keys.contains(pk));

    let filter = Filter::new()
        .kind(Kind::TextNote)
        .kind(Kind::LongFormTextNote)
        .authors(contacts)
        .limit(limit);

    let Ok(events) = user.client.database().query(filter).await else {
        return Response::temporary_failure("db error");
    };

    let mut doc = vec![];

    for event in events.into_iter() {
        let usermeta =
            nostru::get_user_metadata(&user.client, event.pubkey).await;

        match rendering::render_note(
            &user.client,
            &event,
            usermeta,
            rendering::NoteRenderOptions::default()
                | rendering::NoteRenderOptions::SHOW_HASHTAGS,
        )
        .await
        {
            Ok(mut blocks) => {
                doc.append(&mut blocks);
            }
            Err(_err) => {
                doc.push(Block::Text(
                    Content::new("Cannot process note").unwrap(),
                ));
            }
        }
    }
    let tmpl = FeedTemplate {
        edoc: doc,
        b1: Block::Heading(Level::One, "caracal!".parse().unwrap()),
    };

    Response::success(WindTemplate::render(tmpl))
}

pub async fn person_profile(
    ctx: RouteContext,
    user: &'static mut CaracalUser,
) -> Response {
    let params = ctx.parameters.clone();

    let Some(npub) = params.get("npub") else {
        return resp_invalid_params();
    };

    let Ok(pubkey) = PublicKey::parse(npub) else {
        return resp_invalid_params();
    };

    let usermeta = nostru::get_user_metadata(&user.client, pubkey).await;

    let _ = user
        .client
        .subscribe(nostru::notes_filter().author(pubkey).limit(500), None)
        .await;

    // Payment targets
    let Ok(pt_events) = user
        .fetch_quick(
            Filter::new()
                .kind(Kind::Custom(10133))
                .author(pubkey)
                .limit(1),
        )
        .await
    else {
        return Response::temporary_failure("Could not fetch payment targets");
    };

    let payment_targets = pt_events.first_owned();

    let notes: Option<Vec<Block>> = match user
        .client
        .database()
        .query(nostru::notes_filter().author(pubkey).limit(300))
        .await
    {
        Ok(events) => (rendering::render_notes(
            events,
            &user.client,
            None,
            None,
            rendering::NoteRenderOptions::default(),
        )
        .await)
            .ok(),
        Err(_error) => None,
    };

    let template = PersonIndexTemplate {
        npub: pubkey.to_bech32().unwrap(),
        metadata: usermeta,
        notes_blocks: notes,
        payment_targets,
    };

    Response::success(WindTemplate::render(template))
}