Got #[x] references working for 'p' tags. They sort-of work for 'e' tags too; but should probably behave a bit differently.

This commit is contained in:
Robert C. Martin 2022-06-04 12:02:04 -05:00
parent b320fee765
commit 572dc813f9
4 changed files with 160 additions and 38 deletions

View File

@ -1,6 +1,10 @@
(ns more-speech.ui.formatters-spec
(:require [speclj.core :refer :all]
[more-speech.ui.formatters :refer :all]))
[more-speech.ui.formatters :refer :all]
[more-speech.ui.swing.ui-context :refer :all]
[more-speech.nostr.util :as util]))
(defn hexify [n] (util/num32->hex-string n))
(describe "Abbreviations."
(it "abbreviates pubkeys"
@ -43,9 +47,11 @@
(should= "I found you!" (reformat-article "I found you!" 50)))
)
(describe "format header"
(describe "format header"
(it "formats an empty message"
(let [nicknames {}
event-context (atom {:nicknames nicknames})
_ (reset! ui-context {:event-context event-context})
event {:pubkey 16r1111111111111111111111111111111111111111111111111111111111111111
:created-at 1
:content ""
@ -55,37 +61,43 @@
(should= (str " 11111111111111111... " timestamp " \n") header)))
(it "formats a simple message"
(let [nicknames {}
event {:pubkey 16r1111111111111111111111111111111111111111111111111111111111111111
:created-at 1
:content "the message"
:tags []}
timestamp (format-time (event :created-at))
header (format-header nicknames event)]
(should= (str " 11111111111111111... " timestamp " the message\n") header)))
(let [nicknames {}
event-context (atom {:nicknames nicknames})
_ (reset! ui-context {:event-context event-context})
event {:pubkey 16r1111111111111111111111111111111111111111111111111111111111111111
:created-at 1
:content "the message"
:tags []}
timestamp (format-time (event :created-at))
header (format-header nicknames event)]
(should= (str " 11111111111111111... " timestamp " the message\n") header)))
(it "formats a long message with line ends."
(let [nicknames {}
event {:pubkey 16r1111111111111111111111111111111111111111111111111111111111111111
:created-at 1
:content "Four score and seven years ago
(let [nicknames {}
event-context (atom {:nicknames nicknames})
_ (reset! ui-context {:event-context event-context})
event {:pubkey 16r1111111111111111111111111111111111111111111111111111111111111111
:created-at 1
:content "Four score and seven years ago
our fathers brought forth upon this continent
a new nation concieved in liberty and dedicated to
the proposition that all men are created equal."
:tags []}
timestamp (format-time (event :created-at))
header (format-header nicknames event)]
(should= (str " 11111111111111111... " timestamp " Four score and seven years ago~our fathers brought forth upon this continent~...\n") header)))
:tags []}
timestamp (format-time (event :created-at))
header (format-header nicknames event)]
(should= (str " 11111111111111111... " timestamp " Four score and seven years ago~our fathers brought forth upon this continent~...\n") header)))
(it "formats a message with a subject"
(let [nicknames {}
event {:pubkey 16r1111111111111111111111111111111111111111111111111111111111111111
:created-at 1
:content "the message"
:tags [[:subject "the subject"]]}
timestamp (format-time (event :created-at))
header (format-header nicknames event)]
(should= (str " 11111111111111111... " timestamp " the subject|the message\n") header)))
(let [nicknames {}
event-context (atom {:nicknames nicknames})
_ (reset! ui-context {:event-context event-context})
event {:pubkey 16r1111111111111111111111111111111111111111111111111111111111111111
:created-at 1
:content "the message"
:tags [[:subject "the subject"]]}
timestamp (format-time (event :created-at))
header (format-header nicknames event)]
(should= (str " 11111111111111111... " timestamp " the subject|the message\n") header)))
)
(describe "subject and discussion tags"
@ -96,13 +108,82 @@ the proposition that all men are created equal."
(should= nil subject)))
(it "returns null if no subject tag"
(let [tags [[:p "hi"]]
subject (get-subject tags)]
(should= nil subject)))
(let [tags [[:p "hi"]]
subject (get-subject tags)]
(should= nil subject)))
(it "returns subject if found"
(let [tags [[:p "hi"] [:subject "the subject"]]
subject (get-subject tags)]
(should= "the subject" subject)))
(let [tags [[:p "hi"] [:subject "the subject"]]
subject (get-subject tags)]
(should= "the subject" subject)))
))
(describe "Replacing References"
(context "using #[n] and p tags"
(it "replaces nothing if nothing to replace"
(let [nicknames {0 "x"}
event-context (atom {:nicknames nicknames})
_ (reset! ui-context {:event-context event-context})
content "content"
event {:content content}]
(should= "content" (replace-references event))))
(it "replaces a single p reference"
(let [content "the #[0] reference"
nicknames {0 "x"}
event-context (atom {:nicknames nicknames})
_ (reset! ui-context {:event-context event-context})
event {:content content :tags [[:p (hexify 0)]]}]
(should= "the @x reference" (replace-references event))))
(it "replaces a single p reference at the start"
(let [content "#[0] reference"
nicknames {0 "x"}
event-context (atom {:nicknames nicknames})
_ (reset! ui-context {:event-context event-context})
event {:content content :tags [[:p (hexify 0)]]}]
(should= "@x reference" (replace-references event))))
(it "replaces a single p reference at the end"
(let [content "the #[0]"
nicknames {0 "x"}
event-context (atom {:nicknames nicknames})
_ (reset! ui-context {:event-context event-context})
event {:content content :tags [[:p (hexify 0)]]}]
(should= "the @x" (replace-references event))))
(it "replaces a single p reference alone"
(let [content "#[0]"
nicknames {0 "x"}
event-context (atom {:nicknames nicknames})
_ (reset! ui-context {:event-context event-context})
event {:content content :tags [[:p (hexify 0)]]}]
(should= "@x" (replace-references event))))
(it "replaces a two p references"
(let [content "the #[0] and #[1] reference"
nicknames {0 "x"
1 "y"}
event-context (atom {:nicknames nicknames})
_ (reset! ui-context {:event-context event-context})
event {:content content :tags [[:p (hexify 0)]
[:p (hexify 1)]]}]
(should= "the @x and @y reference" (replace-references event))))
(it "Replaces a p reference with an abbreviated id if not a nickname"
(let [content "#[0]"
nicknames {0 "x"}
event-context (atom {:nicknames nicknames})
_ (reset! ui-context {:event-context event-context})
event {:content content :tags [[:p "deadbeef"]]}]
(should= "@id:deadbeef" (replace-references event))))
(it "does not replace reference if there is no p tag"
(let [content "#[1]"
nicknames {0 "x"}
event-context (atom {:nicknames nicknames})
_ (reset! ui-context {:event-context event-context})
event {:content content :tags [[:p "deadbeef"]]}]
(should= "#[1]" (replace-references event))))
))

View File

@ -0,0 +1,8 @@
(ns more-speech.ui.swing.article-panel-spec
(:require [speclj.core :refer :all]
[more-speech.ui.swing.article-panel :refer :all]
[more-speech.ui.swing.ui-context :refer :all]
[more-speech.nostr.util :as util]))
(defn hexify [n] (util/num32->hex-string n))

View File

@ -2,7 +2,8 @@
(:require [clojure.string :as string]
[more-speech.nostr.util :as util]
[more-speech.nostr.events :as events]
[more-speech.ui.config :as config])
[more-speech.ui.config :as config]
[more-speech.ui.swing.ui-context :refer :all])
(:import (java.util Date)
(java.text SimpleDateFormat)))
@ -46,12 +47,14 @@
""
(abbreviate (get nicknames user-id (util/num32->hex-string user-id)) 20)))
(declare get-subject)
(declare get-subject
replace-references)
(defn format-header [nicknames {:keys [pubkey created-at content tags] :as event}]
(defn format-header [nicknames {:keys [pubkey created-at tags] :as event}]
(if (nil? event)
"nil"
(let [name (format-user-id nicknames pubkey)
(let [content (replace-references event)
name (format-user-id nicknames pubkey)
time (format-time created-at)
subject (get-subject tags)
[reply-id _ _] (events/get-references event)
@ -75,4 +78,32 @@
))
)
)
)
(declare lookup-reference)
(defn replace-references [event]
(let [padded-content (str " " (:content event) " ")
nicknames (:nicknames @(:event-context @ui-context))
pattern #"\#\[\d+\]"
references (re-seq pattern padded-content)
segments (string/split padded-content pattern)
referents (mapv (partial lookup-reference nicknames event) references)
referents (conj referents " ")
]
(string/trim (apply str (interleave segments referents)))))
(defn lookup-reference [nicknames event reference]
(let [ref-string (re-find #"\d+" reference)
index (Integer/parseInt ref-string)
tags (:tags event)]
(if (>= index (count tags))
reference
(let [id-string (-> tags (nth index) second)
id (util/hex-string->num id-string)
name (get nicknames id)
name (if (nil? name)
(str "id:" (abbreviate id-string 8))
name)]
(str "@" name))))
)

View File

@ -100,7 +100,9 @@
subject-label (select main-frame [:#subject-label])]
(swing-util/clear-popup relays-popup)
(config! relays-popup :items (:relays event))
(text! article-area (formatters/reformat-article (:content event) config/article-width))
(text! article-area (formatters/reformat-article
(formatters/replace-references event)
config/article-width))
(text! (select main-frame [:#author-name-label])
(format-user (:pubkey event)))
(text! (select main-frame [:#author-id-label])