From ecb757bcaf7cedfe8eb925f0824b7b308c6a1b93 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Fri, 13 Jan 2023 19:48:19 +0100 Subject: [PATCH] :tada: Move user feedback http handler to RPC command method --- backend/src/app/http/feedback.clj | 81 ------------------- backend/src/app/rpc.clj | 1 + backend/src/app/rpc/commands/feedback.clj | 56 +++++++++++++ frontend/src/app/main/repo.cljs | 9 --- .../src/app/main/ui/settings/feedback.cljs | 2 +- 5 files changed, 58 insertions(+), 91 deletions(-) delete mode 100644 backend/src/app/http/feedback.clj create mode 100644 backend/src/app/rpc/commands/feedback.clj diff --git a/backend/src/app/http/feedback.clj b/backend/src/app/http/feedback.clj deleted file mode 100644 index e602b533b..000000000 --- a/backend/src/app/http/feedback.clj +++ /dev/null @@ -1,81 +0,0 @@ -;; This Source Code Form is subject to the terms of the Mozilla Public -;; License, v. 2.0. If a copy of the MPL was not distributed with this -;; file, You can obtain one at http://mozilla.org/MPL/2.0/. -;; -;; Copyright (c) KALEIDOS INC - -(ns app.http.feedback - "A general purpose feedback module." - (:require - [app.common.data :as d] - [app.common.exceptions :as ex] - [app.common.spec :as us] - [app.config :as cf] - [app.db :as db] - [app.emails :as eml] - [app.http.session :as-alias session] - [app.rpc.queries.profile :as profile] - [app.worker :as wrk] - [clojure.spec.alpha :as s] - [integrant.core :as ig] - [promesa.core :as p] - [promesa.exec :as px] - [yetti.request :as yrq] - [yetti.response :as yrs])) - -(declare ^:private send-feedback) -(declare ^:private handler) - -(defmethod ig/pre-init-spec ::handler [_] - (s/keys :req-un [::db/pool ::wrk/executor])) - -(defmethod ig/init-key ::handler - [_ {:keys [executor] :as cfg}] - (let [enabled? (contains? cf/flags :user-feedback)] - (if enabled? - (fn [request respond raise] - (-> (px/submit! executor #(handler cfg request)) - (p/then' respond) - (p/catch raise))) - (fn [_ _ raise] - (raise (ex/error :type :validation - :code :feedback-disabled - :hint "feedback module is disabled")))))) - -(defn- handler - [{:keys [pool] :as cfg} {:keys [::session/profile-id] :as request}] - (let [ftoken (cf/get :feedback-token ::no-token) - token (yrq/get-header request "x-feedback-token") - params (d/merge (:params request) - (:body-params request))] - (cond - (uuid? profile-id) - (let [profile (profile/retrieve-profile-data pool profile-id) - params (assoc params :from (:email profile))] - (send-feedback pool profile params)) - - (= token ftoken) - (send-feedback cfg nil params)) - - (yrs/response 204))) - -(s/def ::content ::us/string) -(s/def ::from ::us/email) -(s/def ::subject ::us/string) -(s/def ::feedback - (s/keys :req-un [::from ::subject ::content])) - -(defn- send-feedback - [pool profile params] - (let [params (us/conform ::feedback params) - destination (cf/get :feedback-destination)] - (eml/send! {::eml/conn pool - ::eml/factory eml/feedback - :from destination - :to destination - :profile profile - :reply-to (:from params) - :email (:from params) - :subject (:subject params) - :content (:content params)}) - nil)) diff --git a/backend/src/app/rpc.clj b/backend/src/app/rpc.clj index a78601865..257637c25 100644 --- a/backend/src/app/rpc.clj +++ b/backend/src/app/rpc.clj @@ -328,6 +328,7 @@ 'app.rpc.commands.access-token 'app.rpc.commands.audit 'app.rpc.commands.auth + 'app.rpc.commands.feedback 'app.rpc.commands.binfile 'app.rpc.commands.comments 'app.rpc.commands.demo diff --git a/backend/src/app/rpc/commands/feedback.clj b/backend/src/app/rpc/commands/feedback.clj new file mode 100644 index 000000000..6f2f83f98 --- /dev/null +++ b/backend/src/app/rpc/commands/feedback.clj @@ -0,0 +1,56 @@ +;; This Source Code Form is subject to the terms of the Mozilla Public +;; License, v. 2.0. If a copy of the MPL was not distributed with this +;; file, You can obtain one at http://mozilla.org/MPL/2.0/. +;; +;; Copyright (c) KALEIDOS INC + +(ns app.rpc.commands.feedback + "A general purpose feedback module." + (:require + [app.common.exceptions :as ex] + [app.common.spec :as us] + [app.config :as cf] + [app.db :as db] + [app.emails :as eml] + [app.rpc :as-alias rpc] + [app.rpc.doc :as-alias doc] + [app.rpc.queries.profile :as profile] + [app.util.services :as sv] + [clojure.spec.alpha :as s])) + +(declare ^:private send-feedback!) + +(s/def ::content ::us/string) +(s/def ::from ::us/email) +(s/def ::subject ::us/string) + +(s/def ::send-user-feedback + (s/keys :req [::rpc/profile-id] + :req-un [::subject + ::content])) + +(sv/defmethod ::send-user-feedback + {::doc/added "1.18"} + [{:keys [::db/pool]} {:keys [::rpc/profile-id] :as params}] + (when-not (contains? cf/flags :user-feedback) + (ex/raise :type :restriction + :code :feedback-disabled + :hint "feedback not enabled")) + + (let [profile (profile/get-profile pool profile-id)] + (send-feedback! pool profile params) + nil)) + +(defn- send-feedback! + [pool profile params] + (let [dest (cf/get :feedback-destination)] + (eml/send! {::eml/conn pool + ::eml/factory eml/feedback + :from dest + :to dest + :profile profile + :reply-to (:email profile) + :email (:email profile) + :subject (:subject params) + :content (:content params)}) + nil)) diff --git a/frontend/src/app/main/repo.cljs b/frontend/src/app/main/repo.cljs index 55c2e3976..b5b67fefc 100644 --- a/frontend/src/app/main/repo.cljs +++ b/frontend/src/app/main/repo.cljs @@ -164,15 +164,6 @@ (rx/map http/conditional-decode-transit) (rx/mapcat handle-response)))) -(defmethod command :send-feedback - [_ params] - (->> (http/send! {:method :post - :uri (u/join @cf/public-uri "api/feedback") - :credentials "include" - :body (http/transit-data params)}) - (rx/map http/conditional-decode-transit) - (rx/mapcat handle-response))) - (defn- send-export [{:keys [blob?] :as params}] (->> (http/send! {:method :post diff --git a/frontend/src/app/main/ui/settings/feedback.cljs b/frontend/src/app/main/ui/settings/feedback.cljs index 10f1d88ac..e198088e6 100644 --- a/frontend/src/app/main/ui/settings/feedback.cljs +++ b/frontend/src/app/main/ui/settings/feedback.cljs @@ -55,7 +55,7 @@ (fn [form _] (reset! loading true) (let [data (:clean-data @form)] - (->> (rp/command! :send-feedback data) + (->> (rp/command! :send-user-feedback data) (rx/subs on-succes on-error)))))] [:& fm/form {:class "feedback-form"