Skip to content

Commit

Permalink
Handle post edits with no content changes (#259)
Browse files Browse the repository at this point in the history
* Send warning on web when user submits empty edit

* Do not update back end on receiving empty edit

* Simplify back-end tests
  • Loading branch information
flybot-nam-nguyenhoai authored Aug 4, 2023
1 parent 99e2e7e commit 5ac35f2
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 10 deletions.
19 changes: 16 additions & 3 deletions client/common/src/flybot/client/common/db/event.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,23 @@
(rf/reg-event-fx
:evt.post.form/send-post
(fn [{:keys [db]} _]
(let [user-id (-> db :app/user :user/id)
post (-> db :form/fields (valid/prepare-post user-id) (valid/validate valid/post-schema-create))]
(if (:errors post)
(let [content-keys [:post/css-class
:post/md-content
:post/image-beside
:post/default-order]
user-id (-> db :app/user :user/id)
post (-> db :form/fields (valid/prepare-post user-id) (valid/validate valid/post-schema-create))
existing-post (get-in db [:app/posts (:post/id post)])]
(cond
(:errors post)
{:fx [[:dispatch [:evt.notif/set-notif :error/form "Form Input Error" (valid/error-msg post)]]]}
(= (select-keys post content-keys)
(select-keys existing-post content-keys))
{:fx [[:dispatch [:evt.notif/set-notif
:warning
"Post unchanged"
(client.utils/post->title post)]]]}
:else
{:http-xhrio (merge http-xhrio-default
{:headers (when MOBILE? {:cookie (:user/cookie db)})
:params {:posts
Expand Down
13 changes: 13 additions & 0 deletions client/web/test/flybot/client/web/core/db_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,19 @@
(select-keys @notification [:notification/type
:notification/title]))))

;;---------- EMPTY EDIT IS IGNORED
(testing "Empty edit (content unchanged):"
(rf/dispatch [:evt.post.form/set-field
:post/md-content (:post/md-content s/post-1)])
(rf/dispatch [:evt.post.form/send-post])
(testing "Warning notification sent."
(is (= #:notification{:type :warning
:title "Post unchanged"
:body "Some content 1"}
(dissoc @notification :notification/id))))
(testing "Form not cleared."
(is @p1-form)))

;;---------- SEND POST SUCCESS
;; Mock success http request
;; Change md-content in the form
Expand Down
4 changes: 3 additions & 1 deletion common/test/flybot/common/test_sample_data.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
#inst "2023-01-04"
#inst "2023-01-05"
#inst "2023-01-06"
#inst "2023-01-07"])
#inst "2023-01-07"
#inst "2023-01-08"])

;;---------- Users ----------

Expand Down Expand Up @@ -59,6 +60,7 @@
(def post-1-edit-date (nth dates 4))
(def post-2-create-date (nth dates 5))
(def post-3-create-date (nth dates 6))
(def post-3-edit-date (nth dates 7))

(def post-1
#:post{:id post-1-id
Expand Down
19 changes: 15 additions & 4 deletions server/src/flybot/server/core/handler/operation.clj
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,34 @@
(defn add-post
"Add the post to the DB with only the author/editor IDs included.
Returns the post with the full author/editor profiles included."
[db post]
(let [author-id (-> post :post/author :user/id)
[db {:post/keys [id page] :as post}]
(let [content-keys [:post/page
:post/css-class
:post/md-content
:post/image-beside
:post/default-order]
existing-post (db/get-post db id)
author-id (-> post :post/author :user/id)
editor-id (-> post :post/last-editor :user/id)
author (db/get-user db author-id)
editor (db/get-user db editor-id)
full-post (cond-> (assoc post :post/author author)
editor (assoc :post/last-editor editor))
page (:post/page post)
posts (-> db
(db/get-all-posts-of page)
(utils/update-post-orders-with post :new-post))]
(if (and editor-id (not= author-id editor-id) (not (admin? editor)))
(cond
(and editor-id (not= author-id editor-id) (not (admin? editor)))
{:error {:type :user/cannot-edit-post
:author-id author-id
:editor-id editor-id
:required-role :admin
:current-role :editor}}
(and existing-post
(= (select-keys post content-keys)
(select-keys existing-post content-keys)))
{:response existing-post}
:else
{:response full-post
:effects {:db {:payload posts}}})))

Expand Down
4 changes: 3 additions & 1 deletion server/test/flybot/server/core/handler/operation_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@
[(assoc post-in :post/default-order 2)]}}}
(sut/add-post (d/db db-conn) post-in)))))
(testing "User is admin and edits post of other so returns new post."
(let [post-in s/post-1
(let [post-in (assoc s/post-1
:post/md-content "# Edited content 1")
post-out (assoc s/post-1
:post/md-content "# Edited content 1"
:post/author s/alice-user
:post/last-editor s/bob-user)]
(is (= {:response post-out
Expand Down
42 changes: 41 additions & 1 deletion server/test/flybot/server/core/handler_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,47 @@
:post/default-order 1}
{:post/id s/post-1-id
:post/default-order 0}}
(-> resp :body :posts :all set)))))))
(-> resp :body :posts :all set)))))
(testing "Submitting an empty edit is a no-op:"
(let [post-in (assoc s/post-3
:post/last-editor {:user/id s/bob-id}
:post/last-edit-date s/post-3-edit-date
:post/default-order 2)
post-out (assoc s/post-3
:post/author s/bob-user
:post/default-order 2)
new-post-resp
(http-request "/pattern"
{:posts
{(list :new-post :with [post-in])
{:post/id '?
:post/page '?
:post/author {:user/id '?
:user/email '?
:user/name '?
:user/picture '?
:user/roles [{:role/name '?
:role/date-granted '?}]}
:post/creation-date '?
:post/last-editor {:user/id '?}
:post/last-edit-date '?
:post/md-content '?
:post/default-order '?}}})
get-post-resp
(http-request "/pattern"
{:posts
{(list :post :with [s/post-3-id])
{:post/id '?
:post/last-editor {:user/id '?}
:post/last-edit-date '?}}})]
(testing "Response is the existing post."
(is (= post-out
(-> new-post-resp :body :posts :new-post))))
(testing "Post does not have a editor or edit date."
(is (empty?
(-> get-post-resp :body :posts :post
(select-keys [:post/last-editor
:post/last-edit-date])))))))))
(testing "Execute a request for a delete post."
(with-redefs [auth/has-permission? (constantly true)]
(let [resp (http-request "/pattern"
Expand Down

0 comments on commit 5ac35f2

Please sign in to comment.