-
Notifications
You must be signed in to change notification settings - Fork 27
/
user_api.clj
125 lines (105 loc) · 3.7 KB
/
user_api.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
(ns castra-notify-chat.user-api
(:refer-clojure :exclude [assert])
(:require
[castra.core :refer [ex defrpc *session*]]
[notify.notification-api :refer :all]))
(defmacro assert [expr & [msg]]
`(when-not ~expr (throw (ex (or ~msg "Server error.") {:from ::assert}))))
(def users-atom (atom {"able" {:pass "able"}
"baker" {:pass "baker"}
"charlie" {:pass "charlie"}}))
(def user-sessions (atom {}))
(defn notify-user! [user type value]
(let [session-id (get-in @user-sessions [user :session-id])]
(when session-id
(add-notification! session-id type value))))
(defn notify-group! [group type value]
(let [f (first group)]
(when f
(notify-user! f type value)
(recur (next group) type value))))
(defn notify-users! [type value]
(notify-group! (keys @user-sessions) type value))
(comment defn notify-users! [type value]
(let [ks (keys @user-session)]
(loop [ks]
(let [f (first ks)]
(when f
(notify-user! f type value)
(recur (next ks)))))))
(defn activate-user! [user]
(let [session-id (get-session-id)]
(swap! user-sessions assoc-in [user :session-id] session-id)
(notify-users! :activate-user user)))
(defn deactivate-user! [user session-id]
(notify-users! :deactivate-user user)
(swap! user-sessions dissoc user))
(defn get-pass [users user] (get-in users [user :pass]))
(defn login? []
(:user @*session*))
(defn do-login! [user]
(try
(if-let [other-id (get-in @user-sessions [user :session-id])]
(deactivate-user! user other-id))
(let [old-user (:user @*session*)]
(if old-user
(deactivate-user! old-user (get-session-id))))
(swap! *session*
(fn [session]
(assoc session :user user)))
(activate-user! user)
(catch Exception e
(.printStackTrace e)
(throw (ex (str "server error: " (.getMessage e)) {})))))
(defn login! [user pass]
(assert (= pass (get-pass @users-atom user)) "Bad username/password.")
(do-login! user))
(defn register! [user pass1 pass2]
(assert (= pass1 pass2) "Passwords don't match.")
(swap! users-atom (fn [users]
(assert (not (get users user)) "Username not available.")
(assoc users user {:pass pass1})))
(do-login! user))
(defn logoff! []
(try
(let [user (:user @*session*)]
(if user
(let [session-id (get-in @user-sessions [user :session-id])]
(if session-id
(if (= session-id (get-session-id))
(do
(deactivate-user! user session-id)
(swap! *session* dissoc :user))
nil)
nil))
nil))
(catch Exception e
(.printStackTrace e)
(throw (ex (str "server error: " (.getMessage e)) {})))))
(defn session-startup! []
(let [user (:user @*session*)]
(if user
(let [session-id (get-in @user-sessions [user :session-id])]
(if (= session-id (get-session-id))
user
(do
(swap! *session* dissoc :user)
nil)))
nil)))
(defrpc session-startup [last-id]
{:rpc/pre [(identify-session!)]}
(if-let [user (session-startup!)]
{:user user :buddies (into #{} (keys @user-sessions))}
{}))
(defrpc login [user pass]
{:rpc/pre [(get-session-id)
(login! user pass)]}
{:user user :buddies (into #{} (keys @user-sessions))})
(defrpc register [user pass1 pass2]
{:rpc/pre [(get-session-id)
(register! user pass1 pass2)]}
{:user user :buddies (into #{} (keys @user-sessions))})
(defrpc logoff [t]
{:rpc/pre [(get-session-id)
(logoff!)]}
{})