(ns braid.server.events
  (:require
    [braid.server.db :as db]
    [braid.server.db.group :as group]
    [braid.server.db.thread :as thread]
    [braid.server.db.user :as user]
    [braid.server.sync-helpers :as sync-helpers]))

(defn add-user-to-recent-threads-in-group!
  [user-id group-id]
  (->> (thread/recent-threads {:user-id user-id
                               :group-id group-id
                               :num-threads 5})
      (mapcat (fn [t] (thread/user-show-thread-txn user-id (t :id))))
      (db/run-txns!)))

(defn user-join-group!
  [user-id group-id]
  (db/run-txns!
    (concat
      (group/user-add-to-group-txn user-id group-id)
      (group/user-subscribe-to-group-tags-txn user-id group-id)))
  (add-user-to-recent-threads-in-group! user-id group-id)
  (sync-helpers/broadcast-group-change group-id [:braid.client/new-user (user/user-by-id user-id)]))

(defn register-user!
  [email group-id]
  (let [[user] (db/run-txns! (user/create-user-txn {:id (db/uuid)
                                                    :email email}))]
    (user-join-group! (user :id) group-id)
    (user :id)))

(ns braid.server.events
  (:require
    [braid.server.db :as db]
    [braid.server.db.group :as group]
    [braid.server.db.thread :as thread]
    [braid.server.db.user :as user]
    [braid.server.sync-helpers :as sync-helpers]))

(defn user-join-group!
  [user-id group-id]
  (db/run-txns! (group/user-join-group-txn user-id group-id))
  (sync-helpers/broadcast-group-change
    group-id
    [:braid.client/new-user (user/user-by-id user-id)]))

(defn register-user!
  [email group-id]
  (let [[user] (db/run-txns! (user/create-user-txn {:id (db/uuid)
                                                    :email email}))]
    (user-join-group! (user :id) group-id)
    (user :id)))

Refactor user-join-group! event to just user user-join-group-txn
Refactor user-join-group! event to just user user-join-group-txn

rafd/braid,braidchat/braid,rafd/braid,braidchat/braid