Skip to content

Commit

Permalink
Merge pull request #1626 from akvo/1625-allow-admins-approve-chat-pri…
Browse files Browse the repository at this point in the history
…vate-channel-join-requests

1625 allow admins approve chat private channel join requests
  • Loading branch information
joseAyudarte91 authored Oct 5, 2023
2 parents fb1719b + 2043a3c commit bd95b57
Show file tree
Hide file tree
Showing 7 changed files with 249 additions and 92 deletions.
26 changes: 18 additions & 8 deletions backend/resources/gpml/config.edn
Original file line number Diff line number Diff line change
Expand Up @@ -615,17 +615,25 @@
:handler #ig/ref :gpml.handler.chat/get-all-channels
:parameters #ig/ref :gpml.handler.chat/get-all-channels-params}}]
["/private"
{:get {:summary "Get all private channels in the server"
:middleware [#ig/ref :gpml.auth/auth-middleware
#ig/ref :gpml.auth/auth-required]
:swagger {:tags ["chat"]}
:handler #ig/ref :gpml.handler.chat/get-private-channels}
:post {:summary "Send private channel invitation request"
[""
{:get {:summary "Get all private channels in the server"
:middleware [#ig/ref :gpml.auth/auth-middleware
#ig/ref :gpml.auth/auth-required]
:swagger {:tags ["chat"]}
:handler #ig/ref :gpml.handler.chat/send-private-channel-invitation-request
:parameters #ig/ref :gpml.handler.chat/send-private-channel-invitation-request-params}}]
:handler #ig/ref :gpml.handler.chat/get-private-channels}
:post {:summary "Send private channel invitation request"
:middleware [#ig/ref :gpml.auth/auth-middleware
#ig/ref :gpml.auth/auth-required]
:swagger {:tags ["chat"]}
:handler #ig/ref :gpml.handler.chat/send-private-channel-invitation-request
:parameters #ig/ref :gpml.handler.chat/send-private-channel-invitation-request-params}}]
["/add-user"
{:post {:summary "Adds user to private channel"
:middleware [#ig/ref :gpml.auth/auth-middleware
#ig/ref :gpml.auth/auth-required]
:swagger {:tags ["chat"]}
:handler #ig/ref :gpml.handler.chat/add-user-to-private-channel
:parameters #ig/ref :gpml.handler.chat/add-user-to-private-channel-params}}]]
["/public"
{:get {:summary "Get all public channels in the server"
:middleware [#ig/ref :gpml.auth/auth-middleware
Expand Down Expand Up @@ -899,6 +907,8 @@
:gpml.handler.chat/get-user-joined-channels #ig/ref :gpml.config/common
:gpml.handler.chat/send-private-channel-invitation-request #ig/ref :gpml.config/common
:gpml.handler.chat/send-private-channel-invitation-request-params {}
:gpml.handler.chat/add-user-to-private-channel #ig/ref :gpml.config/common
:gpml.handler.chat/add-user-to-private-channel-params {}
:gpml.handler.chat/remove-user-from-channel #ig/ref :gpml.config/common
:gpml.handler.chat/remove-user-from-channel-params {}

Expand Down
26 changes: 25 additions & 1 deletion backend/src/gpml/boundary/adapter/chat/rocket_chat/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,28 @@
:reason :exception
:error-details {:msg (ex-message t)}})))

(defn- add-user-to-private-channel*
[{:keys [logger api-key api-user-id] :as adapter} user-id channel-id]
(try
(let [{:keys [status body]}
(http-client/do-request logger
{:url (build-api-endpoint-url adapter "/groups.invite")
:method :post
:body (json/->json {:roomId channel-id :userId user-id})
:headers (get-auth-headers api-key api-user-id)
:as :json-keyword-keys})]
(if (<= 200 status 299)
{:success? true}
{:success? false
:reason :failed-to-add-user-to-private-channel
:error-details body}))
(catch Throwable t
(log logger :error :failed-to-add-user-to-private-channel {:exception-message (ex-message t)
:stack-trace (map str (.getStackTrace t))})
{:success? false
:reason :exception
:error-details {:msg (ex-message t)}})))

(defrecord RocketChat [api-domain-url api-url-path api-key api-user-id logger]
port/Chat
(create-user-account [this user]
Expand Down Expand Up @@ -414,4 +436,6 @@
(get-user-joined-channels [this user-id]
(get-user-joined-channels* this user-id))
(remove-user-from-channel [this user-id channel-id channel-type]
(remove-user-from-channel* this user-id channel-id channel-type)))
(remove-user-from-channel* this user-id channel-id channel-type))
(add-user-to-private-channel [this user-id channel-id]
(add-user-to-private-channel* this user-id channel-id)))
3 changes: 2 additions & 1 deletion backend/src/gpml/boundary/port/chat.clj
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
[this user-id]
[this user-id opts])
(get-user-joined-channels [this user-id])
(remove-user-from-channel [this user-id channel-id channel-type]))
(remove-user-from-channel [this user-id channel-id channel-type])
(add-user-to-private-channel [this user-id channel-id]))
69 changes: 69 additions & 0 deletions backend/src/gpml/handler/chat.clj
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
(ns gpml.handler.chat
(:require [camel-snake-kebab.core :refer [->snake_case]]
[camel-snake-kebab.extras :as cske]
[duct.logger :refer [log]]
[gpml.db.stakeholder :as db.stakeholder]
[gpml.domain.types :as dom.types]
[gpml.handler.resource.permission :as h.r.permission]
[gpml.handler.responses :as r]
[gpml.service.chat :as srv.chat]
[gpml.util.email :as email]
[integrant.core :as ig]))

(def ^:private channel-types
Expand All @@ -21,13 +24,42 @@

(def ^:private send-private-channel-invitation-request-params-schema
[:map
[:channel_id
{:optional false
:swagger {:description "The channel id"
:type "string"
:allowEmptyValue false}}
[:string {:min 1}]]
[:channel_name
{:optional false
:swagger {:description "The channel name"
:type "string"
:allowEmptyValue false}}
[:string {:min 1}]]])

(def ^:private add-user-to-private-channel-params-schema
[:map
[:channel_id
{:optional false
:swagger {:description "The channel id"
:type "string"
:allowEmptyValue false}}
[:string {:min 1}]]
[:channel_name
{:optional false
:swagger {:description "The channel name"
:type "string"
:allowEmptyValue false}}
[:string {:min 1}]]
[:user_id
{:optional false
:swagger {:description "The user's identifier in GPML"
:type "integer"
:allowEmptyValue false}}
[:fn
{:error/message "Not a valid user identifier. It should be a positive integer."}
pos-int?]]])

(def ^:private get-all-channels-params-schema
[:map
[:name
Expand Down Expand Up @@ -131,9 +163,11 @@
:root-context? true})
(r/forbidden {:message "Unauthorized"})
(let [channel-name (get-in parameters [:body :channel_name])
channel-id (get-in parameters [:body :channel_id])
result (srv.chat/send-private-channel-invitation-request
config
user
channel-id
channel-name)]
(if (:success? result)
(r/ok {})
Expand All @@ -150,6 +184,24 @@
(r/ok {})
(r/server-error (dissoc result :success?)))))

(defn- add-user-to-private-channel
[{:keys [db mailjet-config] :as config} parameters]
(let [{:keys [channel_id channel_name user_id]} (:body parameters)
target-user (db.stakeholder/get-stakeholder-by-id (:spec db) {:id user_id})]
(if (seq target-user)
(let [result (srv.chat/add-user-to-private-channel config
(:chat_account_id target-user)
channel_id)]
(if (:success? result)
(do
(email/notify-user-about-chat-private-channel-invitation-request-accepted
mailjet-config
target-user
channel_name)
(r/ok {}))
(r/server-error (dissoc result :success?))))
(r/server-error {:reason :user-not-found}))))

(defmethod ig/init-key :gpml.handler.chat/post
[_ config]
(fn [req]
Expand Down Expand Up @@ -197,6 +249,23 @@
[_ _]
{:body send-private-channel-invitation-request-params-schema})

(defmethod ig/init-key :gpml.handler.chat/add-user-to-private-channel-params
[_ _]
{:body add-user-to-private-channel-params-schema})

(defmethod ig/init-key :gpml.handler.chat/add-user-to-private-channel
[_ {:keys [logger] :as config}]
(fn [{parameters :parameters user :user}]
(if (h.r.permission/super-admin? config (:id user))
(try
(add-user-to-private-channel config parameters)
(catch Throwable t
(log logger :error ::failed-to-add-user-to-private-channel {:exception-message (ex-message t)})
(let [response {:success? false
:reason :could-not-add-user-to-private-channel}]
(r/server-error (assoc-in response [:error-details :error] (ex-message t))))))
(r/forbidden {:message "Unauthorized"}))))

(defmethod ig/init-key :gpml.handler.chat/remove-user-from-channel
[_ config]
(fn [req]
Expand Down
9 changes: 8 additions & 1 deletion backend/src/gpml/service/chat.clj
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,18 @@
channel-id
channel-type))

(defn add-user-to-private-channel
[{:keys [chat-adapter]} chat-account-id channel-id]
(chat/add-user-to-private-channel chat-adapter
chat-account-id
channel-id))

(defn send-private-channel-invitation-request
[{:keys [db mailjet-config]} user channel-name]
[{:keys [db mailjet-config]} user channel-id channel-name]
(let [super-admins (db.rbac-util/get-super-admins-details (:spec db) {})]
(util.email/notify-admins-new-chat-private-channel-invitation-request
mailjet-config
super-admins
user
channel-id
channel-name)))
8 changes: 6 additions & 2 deletions backend/src/gpml/util.clj
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
(ns gpml.util
(:require [clojure.string :as str]
[clojure.walk :as w]
[gpml.util.regular-expressions :as util.regex])
[gpml.util.regular-expressions])
(:import [java.io File]
[java.net URL]
[java.net URL URLEncoder]
[java.util Base64]
[java.util UUID]))

Expand Down Expand Up @@ -182,3 +182,7 @@
[email]
(and string?
(re-matches gpml.util.regular-expressions/email-re email)))

(defn encode-url-param
[^String param]
(URLEncoder/encode param "utf-8"))
Loading

0 comments on commit bd95b57

Please sign in to comment.