Add a numerical suffix to names that are duplicates for existing pubkeys. Print longer names in the article window and article tree, and don't trim the names in the profiles file.

This commit is contained in:
Robert C. Martin 2022-07-23 09:39:21 -05:00
parent fa4acece82
commit 64a823e214
6 changed files with 49 additions and 54 deletions

View File

@ -122,7 +122,7 @@
(with-redefs [rand-int (fn [_n] 12)] (with-redefs [rand-int (fn [_n] 12)]
(let [bad-nicknames {1 "good-name" (let [bad-nicknames {1 "good-name"
2 "bad name" 2 "bad name"
3 "long-name0123456" 3 "long-name0123456789"
4 "" 4 ""
5 nil 5 nil
6 "洛奇安"}] 6 "洛奇安"}]
@ -131,7 +131,7 @@
(let [nicknames (read-string (slurp @config/nicknames-filename))] (let [nicknames (read-string (slurp @config/nicknames-filename))]
(should= {1 "good-name" (should= {1 "good-name"
2 "badname" 2 "badname"
3 "long-name012345" 3 "long-name0123456789"
4 "dud-12" 4 "dud-12"
5 "dud-12" 5 "dud-12"
6 "dudx-12"} 6 "dudx-12"}

View File

@ -3,8 +3,7 @@
[more-speech.nostr.events :refer :all] [more-speech.nostr.events :refer :all]
[more-speech.nostr.elliptic-signature :refer :all] [more-speech.nostr.elliptic-signature :refer :all]
[more-speech.nostr.util :refer :all] [more-speech.nostr.util :refer :all]
[more-speech.ui.swing.ui-context :refer :all] [more-speech.ui.swing.ui-context :refer :all]))
[more-speech.config :as config]))
(defrecord event-handler-dummy [] (defrecord event-handler-dummy []
event-handler event-handler
@ -398,17 +397,24 @@
(should= "badname" (fix-name "bad name")) (should= "badname" (fix-name "bad name"))
(should= "badname" (fix-name "bad.name"))) (should= "badname" (fix-name "bad.name")))
(it "should trim names to max length"
(let [bad-name (apply str (repeat (inc config/user-name-max-length) \x))
expected-name (apply str (repeat config/user-name-max-length \x))]
(should= expected-name (fix-name bad-name)))
)
(it "should create a random name for nils and empties" (it "should create a random name for nils and empties"
(with-redefs [rand-int (fn [_n] 12)] (with-redefs [rand-int (fn [_n] 12)]
(should= "dud-12" (fix-name "")) (should= "dud-12" (fix-name ""))))
)
) (it "should put a suffix on duplicate names."
(let [profiles {1 {:name "unclebob"}}
event-context (atom {:profiles profiles})]
(reset! ui-context {:event-context event-context})
(let [new-name (add-suffix-for-duplicate 2 "unclebob")]
(prn new-name)
(should (re-matches #"unclebob\d+" new-name)))))
(it "should put not put a suffix on previously existing names."
(let [profiles {1 {:name "unclebob"}}
event-context (atom {:profiles profiles})]
(reset! ui-context {:event-context event-context})
(let [new-name (add-suffix-for-duplicate 1 "unclebob")]
(should= "unclebob" new-name))))
) )
(describe "process-name-event" (describe "process-name-event"

View File

@ -27,4 +27,3 @@
(def user-name-pattern #"\@[\w\-]+") (def user-name-pattern #"\@[\w\-]+")
(def user-name-chars #"[\w\-]+") (def user-name-chars #"[\w\-]+")
(def user-name-max-length 15)

View File

@ -120,14 +120,14 @@
(defn process-like [event-state _event] (defn process-like [event-state _event]
event-state) event-state)
(declare fix-name) (declare fix-name add-suffix-for-duplicate)
(defn process-name-event [event-state {:keys [_id pubkey _created-at _kind _tags content _sig] :as event}] (defn process-name-event [event-state {:keys [_id pubkey _created-at _kind _tags content _sig] :as event}]
(try (try
(let [profile (json/read-str content) (let [profile (json/read-str content)
name (get profile "name" "tilt") name (get profile "name" "tilt")
about (get profile "about" "") about (get profile "about" "")
picture (get profile "picture" "") picture (get profile "picture" "")
name (fix-name name)] name (add-suffix-for-duplicate pubkey (fix-name name))]
(-> event-state (-> event-state
(update-in [:profiles] assoc pubkey {:name name (update-in [:profiles] assoc pubkey {:name name
:about about :about about
@ -410,16 +410,22 @@
(first pair) (first pair)
(recur (rest pairs)))))))) (recur (rest pairs))))))))
(defn add-suffix-for-duplicate [pubkey name]
(let [profiles (:profiles @(:event-context @ui-context))
others-profiles (dissoc profiles pubkey)
profile-vals (vals others-profiles)
dups (filter #(= name (:name %)) profile-vals)]
(if (empty? dups)
name
(str name (rand-int 1000)))))
(defn fix-name [name] (defn fix-name [name]
(if (empty? name) (if (empty? name)
(str "dud-" (rand-int 100000)) (str "dud-" (rand-int 1000000))
(let [fixed-name (apply str (let [fixed-name (apply str
(filter (filter
#(re-matches config/user-name-chars (str %)) #(re-matches config/user-name-chars (str %))
name)) name))]
fixed-name (if (> (count fixed-name) config/user-name-max-length)
(subs fixed-name 0 config/user-name-max-length)
fixed-name)]
(if (empty? fixed-name) (if (empty? fixed-name)
(str "dudx-" (rand-int 100000)) (str "dudx-" (rand-int 100000))
fixed-name)))) fixed-name))))

View File

@ -28,32 +28,16 @@
lines (map #(str ">" %) lines)] lines (map #(str ">" %) lines)]
(string/join "\n" lines))) (string/join "\n" lines)))
;(defn reformat-article [article width] (defn format-user-id
; (let [first-line-end (.indexOf article "\n") ([user-id]
; reply-line? (and (> first-line-end 0) (= \> (first article))) (format-user-id user-id 20))
; blank-line (.lastIndexOf article "\n\n" width)
; indentation (.indexOf article "\n ")
; breakable-space (.lastIndexOf article " " width)
; [break-point break-string skip]
; (cond
; reply-line? [first-line-end "\n" 1]
; (< -1 indentation width) [indentation "\n " 2]
; (>= blank-line 0) [blank-line "\n\n" 2]
; (<= (count article) width) [(count article) "" 0]
; (>= breakable-space 0) [breakable-space "\n" 1]
; :else [width "\n" 0])]
; (let [head (.substring article 0 break-point)
; head (.replaceAll head "\n" " ")
; tail (.substring article (+ skip break-point))]
; (if (empty? tail)
; head
; (str head break-string (reformat-article tail width))))))
(defn format-user-id [user-id] ([user-id length]
(let [profiles (:profiles @(:event-context @ui-context))] (let [profiles (:profiles @(:event-context @ui-context))]
(if (nil? user-id) (if (nil? user-id)
"" ""
(abbreviate (get-in profiles [user-id :name] (util/num32->hex-string user-id)) 20)))) (abbreviate (get-in profiles [user-id :name] (util/num32->hex-string user-id)) length))))
)
(declare get-subject (declare get-subject
replace-references) replace-references)
@ -136,7 +120,7 @@
(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 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`!()\[\]{};:'\".,<>?«»“”‘’]))")
(defn linkify [url] (defn linkify [url]
(str "<a href=\"" url "\">" url "</a>")) (str "<a href=\"" url "\">" url "</a>"))
(defn segment-text-url [content] (defn segment-text-url [content]
(let [url (re-find url-pattern content)] (let [url (re-find url-pattern content)]
@ -159,11 +143,11 @@
(let [segments (segment-text-url article)] (let [segments (segment-text-url article)]
(reduce (reduce
(fn [formatted-content [seg-type seg]] (fn [formatted-content [seg-type seg]]
(cond (cond
(= seg-type :text) (= seg-type :text)
(str formatted-content (str formatted-content
((comp break-newlines html-escape format-replies) seg)) ((comp break-newlines html-escape format-replies) seg))
(= seg-type :url) (= seg-type :url)
(str formatted-content (linkify seg)))) (str formatted-content (linkify seg))))
"" ""
segments))) segments)))

View File

@ -107,7 +107,7 @@
(text! article-area (formatters/reformat-article (text! article-area (formatters/reformat-article
(formatters/replace-references event))) (formatters/replace-references event)))
(text! (select main-frame [:#author-name-label]) (text! (select main-frame [:#author-name-label])
(formatters/format-user-id (:pubkey event))) (formatters/format-user-id (:pubkey event) 50))
(text! (select main-frame [:#author-id-label]) (text! (select main-frame [:#author-id-label])
(util/num32->hex-string (:pubkey event))) (util/num32->hex-string (:pubkey event)))
(text! (select main-frame [:#created-time-label]) (text! (select main-frame [:#created-time-label])