<commit_before>(ns braid.core.server.sync
  (:require
    [clojure.set :refer [difference intersection]]
    [clojure.string :as string]
    [environ.core :refer [env]]
    [mount.core :refer [defstate]]
    [taoensso.timbre :as timbre :refer [debugf]]
    [taoensso.truss :refer [have]]
    [braid.core.hooks :as hooks]
    [braid.core.common.schema :as schema]
    [braid.core.common.util :as util :refer [valid-nickname? valid-tag-name?]]
    [braid.core.server.db :as db]
    [braid.core.server.db.group :as group]
    [braid.core.server.db.invitation :as invitation]
    [braid.core.server.db.message :as message]
    [braid.core.server.db.tag :as tag]
    [braid.core.server.db.thread :as thread]
    [braid.core.server.db.user :as user]
    [braid.core.server.digest :as digest]
    [braid.core.server.events :as events]
    [braid.core.server.invite :as invites]
    [braid.core.server.socket :refer [chsk-send! connected-uids]]
    [braid.core.server.sync-handler :as sync-handler :refer [event-msg-handler]]
    [braid.core.server.sync-helpers :as helpers :refer [broadcast-group-change]]
    [braid.core.server.util :refer [valid-url?]]))

(defn user-can-delete-message?
  [user-id message-id]
  (or (= user-id (message/message-author message-id))
      (group/user-is-group-admin?
        user-id
        (message/message-group message-id))))

(defn broadcast-new-user-to-group
  [user-id group-id]
  (broadcast-group-change
    group-id
    [:braid.client/new-user [(user/user-by-id user-id) group-id]]))

;; Handlers

(defmethod event-msg-handler :chsk/ws-ping
  [ev-msg])
  ; Do nothing, just avoid unhandled event message


(defmethod event-msg-handler :chsk/uidport-open
  [{:as ev-msg :keys [user-id]}]
  (doseq [{group-id :id} (group/user-groups user-id)]
    (broadcast-group-change group-id [:braid.client/user-connected [group-id user-id]])))

(defmethod event-msg-handler :chsk/uidport-close
  [{:as ev-msg :keys [user-id]}]
  (doseq [{group-id :id} (group/user-groups user-id)]
    (broadcast-group-change group-id [:braid.client/user-disconnected [group-id user-id]])))

(defonce new-message-callbacks (hooks/register! (atom []) [fn?]))

(defmethod event-msg-handler :braid.server/new-message
  [{:as ev-msg :keys [?data ?reply-fn user-id]}]<commit_msg>Remove unused function<commit_after>(ns braid.core.server.sync
  (:require
    [clojure.set :refer [difference intersection]]
    [clojure.string :as string]
    [environ.core :refer [env]]
    [mount.core :refer [defstate]]
    [taoensso.timbre :as timbre :refer [debugf]]
    [taoensso.truss :refer [have]]
    [braid.core.hooks :as hooks]
    [braid.core.common.schema :as schema]
    [braid.core.common.util :as util :refer [valid-nickname? valid-tag-name?]]
    [braid.core.server.db :as db]
    [braid.core.server.db.group :as group]
    [braid.core.server.db.invitation :as invitation]
    [braid.core.server.db.message :as message]
    [braid.core.server.db.tag :as tag]
    [braid.core.server.db.thread :as thread]
    [braid.core.server.db.user :as user]
    [braid.core.server.digest :as digest]
    [braid.core.server.events :as events]
    [braid.core.server.invite :as invites]
    [braid.core.server.socket :refer [chsk-send! connected-uids]]
    [braid.core.server.sync-handler :as sync-handler :refer [event-msg-handler]]
    [braid.core.server.sync-helpers :as helpers :refer [broadcast-group-change]]
    [braid.core.server.util :refer [valid-url?]]))

(defn user-can-delete-message?
  [user-id message-id]
  (or (= user-id (message/message-author message-id))
      (group/user-is-group-admin?
        user-id
        (message/message-group message-id))))

;; Handlers

(defmethod event-msg-handler :chsk/ws-ping
  [ev-msg])
  ; Do nothing, just avoid unhandled event message


(defmethod event-msg-handler :chsk/uidport-open
  [{:as ev-msg :keys [user-id]}]
  (doseq [{group-id :id} (group/user-groups user-id)]
    (broadcast-group-change group-id [:braid.client/user-connected [group-id user-id]])))

(defmethod event-msg-handler :chsk/uidport-close
  [{:as ev-msg :keys [user-id]}]
  (doseq [{group-id :id} (group/user-groups user-id)]
    (broadcast-group-change group-id [:braid.client/user-disconnected [group-id user-id]])))

(defonce new-message-callbacks (hooks/register! (atom []) [fn?]))

(defmethod event-msg-handler :braid.server/new-message
  [{:as ev-msg :keys [?data ?reply-fn user-id]}]
{'fraction_non_alphanumeric': 0.16855974557019537, 'fraction_numerical': 0.0, 'mean_word_length': 3.620146904512067, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6487602', 'n_tokens_mistral': 1640, 'n_tokens_neox': 1650, 'n_words': 321}