Browse files

Add an API method for editing pastes.

  • Loading branch information...
1 parent f41e4b0 commit a5076ad7d7a07a2c0bf16e2a79ead7e1a6549ec3 @Raynes Raynes committed Jan 5, 2012
Showing with 22 additions and 1 deletion.
  1. +22 −1 src/refheap/views/api.clj
23 src/refheap/views/api.clj
@@ -44,4 +44,25 @@
(let [paste (paste/paste language contents (api/string->bool private) user)]
(if (string? paste)
{:error paste}
- (api/process-paste paste)))))))
+ (api/process-paste paste)))))))
+(defpage [:post "/api/paste/edit"] {:keys [id private contents language username token]}
+ (response/json
+ (if-let [paste (paste/get-paste id)]
+ (let [user (api/validate-user username token)]
+ (cond
+ (string? user) {:error "Username or token is incorrect."}
+ (nil? (:user paste)) {:error "You can't edit anonymous pastes."}
+ (nil? user) {:error "You must be authenticated to edit pastes."}
+ (not= (:id user) (:user paste)) {:error "You can only edit pastes created by you."}
+ :else (let [paste (paste/update-paste paste
+ (or language (:language paste))
+ (or contents (:raw-contents paste))
+ (if (nil? private)
+ (:private paste)
+ (api/string->bool private))
+ user)]
+ (if (string? paste)
+ {:error paste}
+ (api/process-paste paste)))))
+ {:error "Paste does not exist."})))

0 comments on commit a5076ad

Please sign in to comment.