handle incoming nostr: tokens and turn them into appropriate links or @references. Ugh.

This commit is contained in:
Robert C. Martin 2023-04-22 22:27:08 -05:00
parent a3c00964a9
commit f71f79b7b9
4 changed files with 36 additions and 32 deletions

View File

@ -247,24 +247,14 @@
(it "returns a list of :text and :url and :namereference segments"
(should= [[:text "Hey "] [:namereference "@bob"] [:text " Check this "] [:url "http://nostr.com"] [:text " It's cool"]]
(segment-article "Hey @bob Check this http://nostr.com It's cool"))
(should= [[:namereference "npub1qq"] [:text " "]
[:namereference "@npub1qq"] [:text " "]
[:nostrnamereference "nostr:npub1qq"] [:text " "]
[:nostrnamereference "@nostr:npub1qq"]]
(segment-article "npub1qq @npub1qq nostr:npub1qq @nostr:npub1qq"))
(should= [[:notereference "note1qq"] [:text " "]
[:notereference "@note1qq"] [:text " "]
[:nostrnotereference "nostr:note1qq"] [:text " "]
[:nostrnotereference "@nostr:note1qq"]]
(segment-article "note1qq @note1qq nostr:note1qq @nostr:note1qq")))
(should= [[:nostrnamereference "nostr:npub1qq"] [:text " "] [:nostrprofilereference "nostr:nprofile1qq"]]
(segment-article "nostr:npub1qq nostr:nprofile1qq"))
(should= [[:nostrnotereference "nostr:note1qq"] [:text " "] [:nostreventreference "nostr:nevent1qq"]]
(segment-article "nostr:note1qq nostr:nevent1qq")))
(it "extracts text from segments"
(should= "name" (extract-reference "@name"))
(should= "npub1qq" (extract-reference "npub1qq"))
(should= "x" (extract-reference "nostr:x"))
(should= "x" (extract-reference "@nostr:x"))
)
(should= "x" (extract-reference "nostr:x")))
)
(describe "Format article"

View File

@ -70,10 +70,11 @@
;; https://daringfireball.net/2010/07/improved_regex_for_matching_urls
(def url-pattern #"(?i)\b(?:(?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(?:(?:[^\s()<>]+|(?:\(?:[^\s()<>]+\)))*\))+(?:\(?:(?:[^\s()<>]+|(?:\(?:[^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))")
(def nostr-note-reference-pattern #"(?:\@)?nostr:note1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]+")
(def note-reference-pattern #"(?:\@)?note1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]+")
(def nostr-user-reference-pattern #"(?:\@)?nostr:npub1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]+")
(def user-reference-pattern #"(?:\@[\w\-]+)|(?:\@)?(?:npub1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]+)")
(def nostr-note-reference-pattern #"nostr:note1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]+")
(def nostr-event-reference-pattern #"nostr:nevent1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]+")
(def nostr-user-reference-pattern #"nostr:npub1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]+")
(def nostr-profile-reference-pattern #"nostr:nprofile1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]+")
(def user-reference-pattern #"@[\w\-]+")
(def id-reference-pattern #"\@[0-9a-f]{64}")
(def email-pattern #"[\w.-]+@[\w.-]+")

View File

@ -1,13 +1,14 @@
(ns more-speech.ui.formatters
(:require [clojure.string :as string]
[more-speech.logger.default :refer [log-pr]]
[more-speech.nostr.util :as util :refer [hexify]]
[more-speech.mem :refer :all]
[more-speech.nostr.events :as events]
[more-speech.nostr.contact-list :as contact-list]
[more-speech.ui.formatter-util :refer :all]
[more-speech.bech32 :as bech32]
[more-speech.config :as config :refer [get-db]]
[more-speech.db.gateway :as gateway]))
[more-speech.db.gateway :as gateway]
[more-speech.logger.default :refer [log-pr]]
[more-speech.mem :refer :all]
[more-speech.nostr.contact-list :as contact-list]
[more-speech.nostr.events :as events]
[more-speech.nostr.util :as util :refer [hexify]]
[more-speech.ui.formatter-util :refer :all]))
(defn format-user-id
([user-id]
@ -190,11 +191,11 @@
([content segments]
(let [patterns [[:nostrnotereference config/nostr-note-reference-pattern]
[:nostreventreference config/nostr-event-reference-pattern]
[:nostrnamereference config/nostr-user-reference-pattern]
[:notereference config/note-reference-pattern]
[:nostrprofilereference config/nostr-profile-reference-pattern]
[:idreference config/id-reference-pattern]
[:namereference config/user-reference-pattern]
[:url config/url-pattern]]
pattern (apply combine-patterns patterns)
group-names (map first patterns)]
@ -227,10 +228,16 @@
(defn extract-reference [s]
(cond
(.startsWith s "nostr:") (subs s 6)
(.startsWith s "@nostr:") (subs s 7)
(.startsWith s "@") (subs s 1)
:else s))
(defn get-author-name [npub]
(let [id (bech32/address->number npub)
profile (gateway/get-profile (get-db) id)]
(if (nil? profile)
npub
(:name profile))))
(defn reformat-article-into-html [article]
(let [segments (segment-article article)]
(reduce
@ -249,13 +256,18 @@
(= seg-type :url)
(str formatted-content (linkify seg))
(or (= seg-type :namereference) (= seg-type :nostrnamereference))
(= seg-type :namereference)
(str formatted-content (ms-linkify "ms-namereference" (extract-reference seg)))
(or (= seg-type :nostrnamereference)
(= seg-type :nostrprofilereference))
(str formatted-content (ms-linkify "ms-namereference" (get-author-name (extract-reference seg))))
(= seg-type :idreference)
(str formatted-content (ms-linkify "ms-idreference" (subs seg 1)))
(or (= seg-type :notereference) (= seg-type :nostrnotereference))
(or (= seg-type :nostrnotereference)
(= seg-type :nostreventreference))
(str formatted-content (ms-linkify "ms-notereference" (extract-reference seg)))
(= seg-type :img)

View File

@ -327,7 +327,8 @@
(.startsWith subject "@")
(composers/find-user-id (subs subject 1))
(.startsWith subject "npub")
(or (.startsWith subject "npub1")
(.startsWith subject "nprofile1"))
(bech32/address->number subject)
:else