<commit_before>(ns pc.http.datomic
  (:require [cheshire.core :as json]
            [clojure.core.async :as async]
            [clojure.tools.logging :as log]
            [clj-time.core :as time]
            [org.httpkit.client :as http]
            [pc.http.datomic-common :as common]
            [pc.http.sente :as sente]
            [pc.datomic :as pcd]
            [pc.profile :as profile]
            [datomic.api :refer [db q] :as d])
  (:import java.util.UUID))

(defn entity-id-request [eid-count]
  (cond (not (number? eid-count))
        {:status 400 :body (pr-str {:error "count is required and should be a number"})}
        (< 100 eid-count)
        {:status 400 :body (pr-str {:error "You can only ask for 100 entity ids"})}
        :else
        {:status 200 :body (pr-str {:entity-ids (pcd/generate-eids (pcd/conn) eid-count)})}))

(defn get-annotations [transaction]
  (let [txid (-> transaction :tx-data first :tx)]
    (->> txid (d/entity (:db-after transaction)) (#(select-keys % [:document/id :session/uuid :cust/uuid])))))

(defn handle-precursor-pings [document-id datoms]
  (if-let [ping-datoms (seq (filter #(and (= :chat/body (:a %))
                                            (re-find #"@prcrsr" (:v %)))
                                      datoms))]
    (doseq [datom ping-datoms
            :let [message (format "<https://prcrsr.com/document/%s|%s>: %s"
                                  document-id document-id (:v datom))]]
      (http/post "https://hooks.slack.com/services/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"
                 {:form-params {"payload" (json/encode {:text message})}}))
    (when (and (first (filter #(= :chat/body (:a %)) datoms))
               (= 1 (count (get @sente/document-subs document-id))))
      (let [message (format "<https://prcrsr.com/document/%s|%s> is typing messages to himself: \n %s"
                            document-id document-id (:v (first (filter #(= :chat/body (:a %)) datoms))))]
        (http/post "https://hooks.slack.com/services/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"
                   {:form-params {"payload" (json/encode {:text message})}})))))

(defn notify-subscribers [transaction]
  (def myt transaction)
  (let [annotations (get-annotations transaction)]
    (when (:document/id annotations)
      (when-let [public-datoms (->> transaction
                                    :tx-data
                                    (filter (partial common/public? (:db-before transaction)))
                                    (map (partial common/datom-read-api (:db-after transaction)))
                                    seq)]
        (sente/notify-transaction (merge {:tx-data public-datoms}
                                         annotations))
        (when (profile/prod?)
          (handle-precursor-pings (:document/id annotations) public-datoms))))))<commit_msg>make backend detect danny and daniel too<commit_after>(ns pc.http.datomic
  (:require [cheshire.core :as json]
            [clojure.core.async :as async]
            [clojure.tools.logging :as log]
            [clj-time.core :as time]
            [org.httpkit.client :as http]
            [pc.http.datomic-common :as common]
            [pc.http.sente :as sente]
            [pc.datomic :as pcd]
            [pc.profile :as profile]
            [datomic.api :refer [db q] :as d])
  (:import java.util.UUID))

(defn entity-id-request [eid-count]
  (cond (not (number? eid-count))
        {:status 400 :body (pr-str {:error "count is required and should be a number"})}
        (< 100 eid-count)
        {:status 400 :body (pr-str {:error "You can only ask for 100 entity ids"})}
        :else
        {:status 200 :body (pr-str {:entity-ids (pcd/generate-eids (pcd/conn) eid-count)})}))

(defn get-annotations [transaction]
  (let [txid (-> transaction :tx-data first :tx)]
    (->> txid (d/entity (:db-after transaction)) (#(select-keys % [:document/id :session/uuid :cust/uuid])))))

(defn handle-precursor-pings [document-id datoms]
  (if-let [ping-datoms (seq (filter #(and (= :chat/body (:a %))
                                            (re-find #"(?i)@prcrsr|@danny|@daniel" (:v %)))
                                      datoms))]
    (doseq [datom ping-datoms
            :let [message (format "<https://prcrsr.com/document/%s|%s>: %s"
                                  document-id document-id (:v datom))]]
      (http/post "https://hooks.slack.com/services/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"
                 {:form-params {"payload" (json/encode {:text message})}}))
    (when (and (first (filter #(= :chat/body (:a %)) datoms))
               (= 1 (count (get @sente/document-subs document-id))))
      (let [message (format "<https://prcrsr.com/document/%s|%s> is typing messages to himself: \n %s"
                            document-id document-id (:v (first (filter #(= :chat/body (:a %)) datoms))))]
        (http/post "https://hooks.slack.com/services/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"
                   {:form-params {"payload" (json/encode {:text message})}})))))

(defn notify-subscribers [transaction]
  (def myt transaction)
  (let [annotations (get-annotations transaction)]
    (when (:document/id annotations)
      (when-let [public-datoms (->> transaction
                                    :tx-data
                                    (filter (partial common/public? (:db-before transaction)))
                                    (map (partial common/datom-read-api (:db-after transaction)))
                                    seq)]
        (sente/notify-transaction (merge {:tx-data public-datoms}
                                         annotations))
        (when (profile/prod?)
          (handle-precursor-pings (:document/id annotations) public-datoms))))))
{'fraction_non_alphanumeric': 0.1554520358868185, 'fraction_numerical': 0.011732229123533472, 'mean_word_length': 1.9958656330749354, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '886884', 'n_tokens_mistral': 1929, 'n_tokens_neox': 1812, 'n_words': 425}