|
2 | 2 | (:require [org.httpkit.server :refer [with-channel on-close on-receive send!]]
|
3 | 3 | [chatb0x.user :refer :all]
|
4 | 4 | [cheshire.core :refer [generate-string]]
|
| 5 | + [cemerick.friend :as friend] |
5 | 6 | [digest :refer [md5]]
|
6 | 7 | [clojure.string :as str]))
|
7 | 8 |
|
|
76 | 77 | ;; Agent visitor handling
|
77 | 78 | (defn send-msg2 [client1 client2 msg]
|
78 | 79 | (do (if client1
|
79 |
| - (println "Sending client1: " msg) |
80 |
| - (send! client1 msg false)) |
| 80 | + (do (println "Sending client1: " client1 msg) |
| 81 | + (send! client1 msg false))) |
81 | 82 | (if client2
|
82 |
| - (println "Sending client2" msg) |
83 |
| - (send! client2 msg false)))) |
| 83 | + (do (println "Sending client2" client2 msg) |
| 84 | + (send! client2 msg false))))) |
84 | 85 |
|
85 | 86 | (defn msg-init [client1 client2]
|
86 | 87 | "Send address of opposite end to both clients"
|
87 |
| - (send! client1 (generate-string {:channel client2}) false) |
88 |
| - (send! client2 (generate-string {:channel client1}) false)) |
| 88 | + (send! client1 (pr-str {:channel client2}) false) |
| 89 | + (send! client2 (pr-str {:channel client1}) false)) |
89 | 90 |
|
90 | 91 | (defn msg-text [sender data]
|
91 | 92 | (let [agent (get-agent sender)
|
92 | 93 | visitor (get-visitor data)
|
93 | 94 | text (get-text data)]
|
94 |
| - (println "In msg-text") |
95 |
| - (send-msg2 agent visitor (generate-string {:ch-visitor (:ch-visitor data) :message text})))) |
| 95 | + (println "In msg-text" agent visitor) |
| 96 | + (send-msg2 agent visitor (pr-str {:ch-visitor (:ch-visitor data) :message text})))) |
96 | 97 |
|
97 | 98 | (defn msg-close [client]
|
98 | 99 | (let [agent (get-agent client)
|
|
101 | 102 | (do (println "websockets: agent closed, send closed message to all visitors" (get-agent-visitors agent))
|
102 | 103 | (doseq [visitor (get-agent-visitors agent)]
|
103 | 104 | (println "Sending msg to visitor " visitor)
|
104 |
| - (send! visitor (generate-string {:agent agent :visitor visitor :message text}) false))) |
| 105 | + (send! visitor (pr-str {:agent agent :visitor visitor :message text}) false))) |
105 | 106 | (do (println "websockets: visitor closed, send closed message to the agent" client agent)
|
106 |
| - (if (= agent nil) (send! agent (generate-string {:agent agent :visitor client :message text}) false)) |
| 107 | + (if (= agent nil) (send! agent (pr-str {:agent agent :visitor client :message text}) false)) |
107 | 108 | (println "barbazbop")))))
|
108 | 109 |
|
109 | 110 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
114 | 115 | (with-channel req channel
|
115 | 116 | ;; CONNECT
|
116 | 117 | (println req)
|
117 |
| - (if (contains? (get-in req [:session :cemerick.friend/identity :authentications nil :roles]) :chatb0x.user/agent) |
| 118 | + (if (friend/authorized? #{:chatb0x.user/agent} (friend/identity req)) |
118 | 119 | (do (println "Agent connected: " channel) ;; Agent
|
119 | 120 | (swap! ds-clients assoc channel {:name nil :gravatar-url (calc-gravatar (get-in req [:session :cemerick.friend/identity :authentications nil :username])) :room nil}) ;; Add to ds-clients
|
120 | 121 | (swap! ds-agents assoc channel {}))
|
|
123 | 124 | gravatar-url (calc-gravatar (get-in req [:session :cemerick.friend/identity :authentications nil :username]))]
|
124 | 125 | (if (= ch-agent nil)
|
125 | 126 | (println "ERROR EVENTUALLY!!! no agents on, ignoring visitor!")
|
126 |
| - (do (println "Visitor connected: " channel) |
| 127 | + (do (println "Visitor connected: " ch-visitor) |
127 | 128 | (swap! ds-clients assoc ch-visitor {:name nil :gravatar-url gravatar-url :room nil}) ;; Add to ds-clients
|
128 | 129 | (ds-agents-add-visitor ch-agent ch-visitor)
|
129 | 130 | (ds-visitors-add ch-visitor ch-agent)
|
130 |
| - (send! ch-agent (generate-string {:ch-visitor (str ch-visitor) :gravatar-url gravatar-url}) false) |
131 |
| - ;; (send! ch-visitor (generate-string {:gravatar-url gravatar-url}) false) |
| 131 | + (println "sending this message over!!" (pr-str {:ch-visitor (str ch-visitor) :gravatar-url gravatar-url})) |
| 132 | + (send! ch-agent (pr-str {:ch-visitor (str ch-visitor) :gravatar-url gravatar-url}) false) |
| 133 | + ;; (send! ch-visitor (pr-str {:gravatar-url gravatar-url}) false) |
132 | 134 | ))))
|
133 | 135 | ;; RECEIVE
|
134 | 136 | (on-receive channel (fn [data]
|
|
147 | 149 | (let [client-filter-fn (fn [room] (fn [client] (if (= room (:room (val client))) true false)))
|
148 | 150 | clients-in-room (fn [room clients] (filter (client-filter-fn room) clients))
|
149 | 151 | channels-to-room (keys (clients-in-room room @comment-clients))
|
150 |
| - message-string (generate-string message-map)] |
| 152 | + message-string (pr-str message-map)] |
151 | 153 | (when (seq channels-to-room)
|
152 | 154 | (println "sending message: " message-map "to" (count channels-to-room) "channels")
|
153 | 155 | (doseq [channel channels-to-room]
|
|
0 commit comments