<commit_before>(ns gratefulplace.db.transactions
  (:require [datomic.api :as d]
            [gratefulplace.db.maprules :as mr]
            [gratefulplace.db.query :as db]
            [gratefulplace.models.mailer :as mailer]
            [flyingmachine.cartographer.core :as c])
  (:use gratefulplace.utils))

(defn create-post
  [params]
  (let [{:keys [topic-id author-id]} params
        post-tempid (d/tempid :db.part/user -1)
        now (now)
        post (remove-nils-from-map {:post/content (:content params)
                                    :post/topic topic-id
                                    :post/created-at now
                                    :content/author author-id
                                    :db/id post-tempid})
        watches (db/all :watch/topic [:watch/topic topic-id])]

    (future
      (doseq [watch watches]
        (let [user (c/mapify (:watch/user watch) mr/ent->user)]
          (if (and
               (:receive-watch-notifications user)
               (not= author-id (:id user)))
            (mailer/send-post-notification user params)))))
    
    {:result (db/t [post
                    {:db/id topic-id :topic/last-posted-to-at now}
                    [:increment-watch-count topic-id author-id]])
     :tempid post-tempid}))

(defn update-post
  [params]
  (fn [_] (db/t [(c/mapify params mr/post->txdata)])))

(defn create-topic
  [params]
  (let [topic-tempid (d/tempid :db.part/user -1)
        post-tempid (d/tempid :db.part/user -2)
        author-id (:author-id params)
        topic (remove-nils-from-map {:topic/title (:title params)
                                     :topic/first-post post-tempid
                                     :topic/last-posted-to-at (now)
                                     :content/author author-id
                                     :content/deleted false
                                     :db/id topic-tempid})
        post {:post/content (:content params)
              :post/topic topic-tempid
              :post/created-at (now)
              :content/author author-id
              :db/id post-tempid}]
<commit_msg>run transaction first so emails don't get sent if there's an error<commit_after>(ns gratefulplace.db.transactions
  (:require [datomic.api :as d]
            [gratefulplace.db.maprules :as mr]
            [gratefulplace.db.query :as db]
            [gratefulplace.models.mailer :as mailer]
            [flyingmachine.cartographer.core :as c])
  (:use gratefulplace.utils))

(defn create-post
  [params]
  (let [{:keys [topic-id author-id]} params
        post-tempid (d/tempid :db.part/user -1)
        now (now)
        post (remove-nils-from-map {:post/content (:content params)
                                    :post/topic topic-id
                                    :post/created-at now
                                    :content/author author-id
                                    :db/id post-tempid})
        watches (db/all :watch/topic [:watch/topic topic-id])
        result (db/t [post
                      {:db/id topic-id :topic/last-posted-to-at now}
                      [:increment-watch-count topic-id author-id]])]

    ;; TODO find a better home for this
    (future
      (doseq [watch watches]
        (let [user (c/mapify (:watch/user watch) mr/ent->user)]
          (if (and
               (:receive-watch-notifications user)
               (not= author-id (:id user)))
            (mailer/send-post-notification user params)))))
    
    {:result result
     :tempid post-tempid}))

(defn update-post
  [params]
  (fn [_] (db/t [(c/mapify params mr/post->txdata)])))

(defn create-topic
  [params]
  (let [topic-tempid (d/tempid :db.part/user -1)
        post-tempid (d/tempid :db.part/user -2)
        author-id (:author-id params)
        topic (remove-nils-from-map {:topic/title (:title params)
                                     :topic/first-post post-tempid
                                     :topic/last-posted-to-at (now)
                                     :content/author author-id
                                     :content/deleted false
                                     :db/id topic-tempid})
        post {:post/content (:content params)
              :post/topic topic-tempid
              :post/created-at (now)
              :content/author author-id
              :db/id post-tempid}]

{'fraction_non_alphanumeric': 0.12262640128117136, 'fraction_numerical': 0.0013726835964310226, 'mean_word_length': 1.4370122630992197, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5002306', 'n_tokens_mistral': 1325, 'n_tokens_neox': 1271, 'n_words': 290}