Skip to content

Commit

Permalink
Adds hashtag tags (:t "tag")
Browse files Browse the repository at this point in the history
  • Loading branch information
unclebob committed Jul 15, 2023
1 parent ee893ce commit a9bdc5e
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 66 deletions.
14 changes: 12 additions & 2 deletions spec/more_speech/nostr/event_composers_spec.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
[more-speech.nostr.event-composers :refer :all]
[more-speech.nostr.event-composers :refer :all]
[more-speech.nostr.event-dispatcher :refer :all]
[more-speech.nostr.events :as events]
[more-speech.nostr.events :refer :all]
[more-speech.nostr.util :as util]
[more-speech.nostr.util :refer :all]
Expand Down Expand Up @@ -230,7 +231,7 @@
)

(declare event-tags)
(describe "Emplacing references"
(describe "Replacements, emplacements, and other edits."
(with-stubs)
(setup-db-mem)
(redefs-around [util/get-now (stub :get-now {:return 1000})])
Expand Down Expand Up @@ -300,7 +301,16 @@
content "hello @01234567abc."]
(should= ["hello @01234567abc." [[:e "blah"]]]
(emplace-references content tags))
(should-be-nil (gateway/get-profile @db 0x01234567abc))))))
(should-be-nil (gateway/get-profile @db 0x01234567abc))))
)

(context "creating hashtags tags"
(it "adds hashtag tags"
(let [composed-body (compose-text-event-body "subject" "text #tag1 text #tag2 text #TAG1" nil)
t-tags (set (events/get-tag composed-body :t))]
(should= #{["tag1"] ["tag2"]} t-tags)))
)
)

(describe "find-user-id"
(setup-db-mem)
Expand Down
140 changes: 76 additions & 64 deletions src/more_speech/nostr/event_composers.clj
Original file line number Diff line number Diff line change
Expand Up @@ -178,69 +178,81 @@
[encrypted-content 4])))
[content 1]))

(defn compose-text-event
([subject text]
(compose-text-event subject text nil))

([subject text reply-to-or-nil]
(let [root (get-reply-root reply-to-or-nil)
tags (concat (make-event-reference-tags reply-to-or-nil root)
(make-people-reference-tags reply-to-or-nil)
(make-subject-tag subject)
[[:client (str "more-speech - " config/version)]])
[content tags] (emplace-references text tags)
[content kind] (encrypt-if-direct-message content tags)
body {:kind kind
:tags tags
:content content}]
(body->event body))))

(defn send-event [event]
(let [send-chan (get-mem :send-chan)]
(async/>!! send-chan [:event event])))

(defn compose-and-send-text-event [source-event-or-nil subject message]
(let [reply-to-or-nil (:id source-event-or-nil)
event (compose-text-event subject message reply-to-or-nil)]
(send-event event)))

(defn compose-recommended-server-event [url]
(let [body {:kind 2
:tags []
:content url}]
(body->event body)))
(defn make-hashtag-tags [text]
(let [words (string/split text #"\s")
hashtags (filter #(.startsWith % "#") words)
hashtags (map #(subs % 1) hashtags)
hashtags (map #(.toLowerCase %) hashtags)
tags (map #(vector :t %) (set hashtags))]
tags))

(defn remove-arguments [url]
(re-find config/relay-pattern url))

(defn compose-and-send-metadata-event []
(send-event (compose-metadata-event)))

(defn compose-and-send-metadata-and-relay-recommendations []
(send-event (compose-metadata-event))
(let [relays (get-mem :relays)
server-urls (filter #(:write (get relays %)) (keys relays))
server-urls (map remove-arguments server-urls)]
(log-pr 2 'server-urls server-urls)
(future
(doseq [url server-urls]
(Thread/sleep 5000)
(send-event (compose-recommended-server-event url))))))

(defn compose-and-send-contact-list [contact-list]
(send-event (compose-contact-list contact-list)))

(defn compose-reaction-event [subject-event polarity]
(let [id (:id subject-event)
pubkey (:pubkey subject-event)
ep-tags (filter #(or (= :p (first %)) (= :e (first %))) (:tags subject-event))
tags (concat ep-tags [[:e (hexify id)] [:p (hexify pubkey)]])
body {:kind 7
:tags tags
:content polarity}]
body))
(defn compose-text-event-body [subject text reply-to-or-nil]
(let [root (get-reply-root reply-to-or-nil)
tags (concat (make-event-reference-tags reply-to-or-nil root)
(make-people-reference-tags reply-to-or-nil)
(make-subject-tag subject)
(make-hashtag-tags text)
[[:client (str "more-speech - " config/version)]])
[content tags] (emplace-references text tags)
[content kind] (encrypt-if-direct-message content tags)]
{:kind kind
:tags tags
:content content}))

(defn compose-text-event
([subject text]
(compose-text-event subject text nil))

([subject text reply-to-or-nil]
(body->event
(compose-text-event-body subject text reply-to-or-nil))))

(defn send-event [event]
(let [send-chan (get-mem :send-chan)]
(async/>!! send-chan [:event event])))

(defn compose-and-send-text-event [source-event-or-nil subject message]
(let [reply-to-or-nil (:id source-event-or-nil)
event (compose-text-event subject message reply-to-or-nil)]
(send-event event)))

(defn compose-recommended-server-event [url]
(let [body {:kind 2
:tags []
:content url}]
(body->event body)))

(defn remove-arguments [url]
(re-find config/relay-pattern url))

(defn compose-and-send-metadata-event []
(send-event (compose-metadata-event)))

(defn compose-and-send-metadata-and-relay-recommendations []
(send-event (compose-metadata-event))
(let [relays (get-mem :relays)
server-urls (filter #(:write (get relays %)) (keys relays))
server-urls (map remove-arguments server-urls)]
(log-pr 2 'server-urls server-urls)
(future
(doseq [url server-urls]
(Thread/sleep 5000)
(send-event (compose-recommended-server-event url))))))

(defn compose-and-send-contact-list [contact-list]
(send-event (compose-contact-list contact-list)))

(defn compose-reaction-event [subject-event polarity]
(let [id (:id subject-event)
pubkey (:pubkey subject-event)
ep-tags (filter #(or (= :p (first %)) (= :e (first %))) (:tags subject-event))
tags (concat ep-tags [[:e (hexify id)] [:p (hexify pubkey)]])
body {:kind 7
:tags tags
:content polarity}]
body))

(defn compose-and-send-reaction-event [subject-event polarity]
(send-event
(body->event
(compose-reaction-event subject-event polarity))))
(defn compose-and-send-reaction-event [subject-event polarity]
(send-event
(body->event
(compose-reaction-event subject-event polarity))))

0 comments on commit a9bdc5e

Please sign in to comment.