This repository has been archived by the owner on Oct 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
user_connection.clj
173 lines (135 loc) · 6.54 KB
/
user_connection.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
;
; Copyright 2017 Fintech Open Source Foundation
; SPDX-License-Identifier: Apache-2.0
;
; Licensed under the Apache License, Version 2.0 (the "License");
; you may not use this file except in compliance with the License.
; You may obtain a copy of the License at
;
; http://www.apache.org/licenses/LICENSE-2.0
;
; Unless required by applicable law or agreed to in writing, software
; distributed under the License is distributed on an "AS IS" BASIS,
; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
; See the License for the specific language governing permissions and
; limitations under the License.
;
(ns clj-symphony.user-connection
"Operations related to user connections and requests for those connections.
In Symphony, a user connection is an explicitly established relationship
between users in different pods."
(:require [clj-symphony.user :as syu]))
(def user-connection-states
"The set of possible user connections states in Symphony, as keywords."
(set (map #(keyword (str %)) (org.symphonyoss.symphony.clients.model.SymUserConnection$Status/values))))
(defn userconnectionobj->map
"Converts a `org.symphonyoss.symphony.clients.model.SymUserConnection` object
into a map with these keys:
| Key | Description |
|-----------------------|---------------------------------------------------------------------|
| `:user-id` | The id of the user to whom the request was made. |
| `:status` | The current status of the request (see [[user-connection-states]]). |
| `:first-request-date` | The date a connection was first requested. |
| `:update-date` | The date the last connection request was initiated. |
| `:request-count` | The total number of connection requests made. |
"
[^org.symphonyoss.symphony.clients.model.SymUserConnection uc]
(if uc
{
:user-id (.getUserId uc)
:status (if-let [status (.getStatus uc)]
(keyword (str status)))
:first-request-date (if-let [first-requested-at-epoch (.getFirstRequestedAt uc)]
(java.util.Date. first-requested-at-epoch))
:update-date (if-let [updated-at-epoch (.getUpdatedAt uc)]
(java.util.Date. updated-at-epoch))
:request-count (.getRequestCounter uc)
}))
(defn user-connectionsobjs
"Returns all `org.symphonyoss.symphony.clients.model.SymUserConnection`
objects for the authenticated user."
[^org.symphonyoss.client.SymphonyClient c]
(.getAllConnections (.getConnectionsClient c)))
(defn user-connections
"Returns all user connections for the authenticated user, as a lazy sequence
of maps (see [[userconnectionobj->map]] for details)."
[c]
(map userconnectionobj->map (user-connectionsobjs c)))
(defn accepted-requestsobjs
"Returns all accepted user connection requests as
`org.symphonyoss.symphony.clients.model.SymUserConnection` objects for the
authenticated user."
[^org.symphonyoss.client.SymphonyClient c]
(.getAcceptedRequests (.getConnectionsClient c)))
(defn accepted-requests
"Returns all accepted user connection requests for the authenticated user, as
a lazy sequence of maps (see [[userconnectionobj->map]] for details)."
[c]
(map userconnectionobj->map (accepted-requestsobjs c)))
(defn pending-requestsobjs
"Returns all pending user connection requests as
`org.symphonyoss.symphony.clients.model.SymUserConnection` objects for the
authenticated user."
[^org.symphonyoss.client.SymphonyClient c]
(.getPendingRequests (.getConnectionsClient c)))
(defn pending-requests
"Returns all pending user connection requests for the authenticated user, as a
lazy sequence of maps (see [[userconnectionobj->map]] for details)."
[c]
(map userconnectionobj->map (pending-requestsobjs c)))
(defn rejected-requestsobjs
"Returns all rejected user connection requests as
`org.symphonyoss.symphony.clients.model.SymUserConnection` objects for the
authenticated user."
[^org.symphonyoss.client.SymphonyClient c]
(.getRejectedRequests (.getConnectionsClient c)))
(defn rejected-requests
"Returns all rejected user connection requests for the authenticated user, as
a lazy sequence of maps (see [[userconnectionobj->map]] for details)."
[c]
(map userconnectionobj->map (rejected-requestsobjs c)))
(defn incoming-requestsobjs
"Returns all incoming user connection requests as
`org.symphonyoss.symphony.clients.model.SymUserConnection` objects for the
authenticated user."
[^org.symphonyoss.client.SymphonyClient c]
(.getIncomingRequests (.getConnectionsClient c)))
(defn incoming-requests
"Returns all incoming user connection requests for the authenticated user, as
a lazy sequence of maps (see [[userconnectionobj->map]] for details)."
[c]
(map userconnectionobj->map (incoming-requestsobjs c)))
(defn- ^org.symphonyoss.symphony.clients.model.SymUserConnectionRequest build-connection-requestobj
[u]
(org.symphonyoss.symphony.clients.model.SymUserConnectionRequest.
(doto (org.symphonyoss.symphony.clients.model.SymUserConnection.)
(.setUserId (syu/user-id u)))))
(defn send-connection-request!
"Sends a connection request to the given user."
[^org.symphonyoss.client.SymphonyClient c u]
(.sendConnectionRequest (.getConnectionsClient c) (build-connection-requestobj u))
nil)
(defn accept-connection-request!
"Accepts a connection request from the given user."
[^org.symphonyoss.client.SymphonyClient c u]
(.acceptConnectionRequest (.getConnectionsClient c) (build-connection-requestobj u))
nil)
(defn reject-connection-request!
"Rejects a connection request from the given user."
[^org.symphonyoss.client.SymphonyClient c u]
(.rejectConnectionRequest (.getConnectionsClient c) (build-connection-requestobj u))
nil)
(defn accept-all-connection-requests!
"Convenience method that unconditionally accepts all incoming user connection
requests, returning the number accepted."
[c]
(let [incoming-requests (incoming-requests c)]
(doall (map (partial accept-connection-request! c) incoming-requests))
(count incoming-requests)))
(defn reject-all-connection-requests!
"Convenience method that unconditionally rejects all incoming user connection
requests, returning the number rejected."
[c]
(let [incoming-requests (incoming-requests c)]
(doall (map (partial reject-connection-request! c) incoming-requests))
(count incoming-requests)))