Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added forking. Closes #11.

  • Loading branch information...
commit 9a2da56ab371ce6167537cd77c3e66de230074da 1 parent 8d6a5c4
@Raynes Raynes authored
View
9 src/refheap/models/api.clj
@@ -1,6 +1,7 @@
(ns refheap.models.api
(:require [somnium.congomongo :as mongo]
[refheap.models.users :as users]
+ [refheap.models.paste :as pastes]
[noir.response :as response])
(:import java.util.UUID))
@@ -38,6 +39,11 @@
(dissoc :_id))
"User or token not valid.")))
+(defn id->paste-id [paste]
+ (if-let [fork (:fork paste)]
+ (assoc paste :fork (or (:paste-id (pastes/get-paste-by-id fork)) "deleted"))
+ paste))
+
(defn process-paste
"Select and rename keys to make pastes suitable for api consumption."
[paste]
@@ -46,7 +52,8 @@
(assoc :user (when-let [user (:user paste)]
(:username (users/get-user-by-id user))))
(assoc :url (str "http://refheap.com/paste/" (:paste-id paste)))
- (dissoc :id :_id :raw-contents :summary)))
+ (dissoc :id :_id :raw-contents :summary)
+ id->paste-id))
(defn string->bool [s]
(case s
View
20 src/refheap/models/paste.clj
@@ -166,7 +166,7 @@
:dir "resources/pygments"
:in text)))
-(defn paste-map [paste-id id user language contents date private]
+(defn paste-map [paste-id id user language contents date private fork]
{:paste-id (str paste-id)
:id id
:user (:id user)
@@ -187,7 +187,8 @@
(if (= \newline (last contents))
lines
(inc lines)))
- :contents (pygmentize language contents true)})
+ :contents (pygmentize language contents true)
+ :fork fork})
(defn validate [contents]
(cond
@@ -200,7 +201,7 @@
(defn paste
"Create a new paste."
- [language contents private user]
+ [language contents private user & [fork]]
(let [validated (validate contents)]
(if-let [error (:error validated)]
error
@@ -214,7 +215,8 @@
language
(:contents validated)
(format/unparse (format/formatters :date-time) (time/now))
- private))]
+ private
+ fork))]
(if private
(let [new (assoc result :paste-id (str (:_id result)))]
(mongo/update! :pastes result new)
@@ -228,6 +230,13 @@
:pastes
:where {:paste-id id}))
+(defn get-paste-by-id
+ "Get a paste by its :id key (which is the same regardless of being public or private."
+ [id]
+ (mongo/fetch-one
+ :pastes
+ :where {:id id}))
+
(defn update-paste
"Update an existing paste."
[old language contents private user]
@@ -246,7 +255,8 @@
language
(:contents validated)
(:date old)
- private)]
+ private
+ (:fork old))]
(mongo/update! :pastes old paste)
paste))))
View
24 src/refheap/views/paste.clj
@@ -41,7 +41,7 @@
[:div.clear]))
(defn show-paste-page [id]
- (when-let [{:keys [lines private user contents language date]} (paste/get-paste id)]
+ (when-let [{:keys [lines private user contents language date fork] :as all} (paste/get-paste id)]
(layout
(list
[:div.floater
@@ -50,16 +50,25 @@
[:span.info "Lines: " lines]
(when private
[:span {:class "info private"} "Private"])
- [:span#last.info "Pasted by "
+ [:span#last.info
+ (if fork "Forked by " "Pasted by ")
(if user
(let [user (:username (users/get-user-by-id user))]
(ph/link-to (str "/users/" user) user))
"anonymous")
+ (when fork
+ (list
+ " from "
+ (if-let [paste (:paste-id (paste/get-paste-by-id fork))]
+ (ph/link-to (str "/paste/" paste) paste)
+ "[deleted]")))
" on "
(date-string date)
[:div#edit
(ph/link-to (str "/paste/" id "/raw") "raw")
(ph/link-to (str "/paste/" id "/fullscreen") "maximize")
+ (when-not (= user (:id (session/get :user)))
+ (ph/link-to (str "/paste/" id "/fork") "fork"))
(when (and user (= user (:id (session/get :user))))
(list
[:a {:href (str "/paste/" id "/edit")} "edit"]
@@ -121,6 +130,17 @@
(when (= (:user paste) (:id (session/get :user)))
(create-paste-page nil paste))))
+(defpage "/paste/:id/fork" {:keys [id]}
+ (let [user (:id (session/get :user))
+ paste (paste/get-paste id)]
+ (when (and user paste (not= (:user paste) user))
+ (let [forked (paste/paste (:language paste)
+ (:raw-contents paste)
+ (:private paste)
+ (session/get :user)
+ (:id paste))]
+ (redirect (str "/paste/" (:paste-id forked)))))))
+
(defpage "/paste/:id/delete" {:keys [id]}
(when-let [user (:user (paste/get-paste id))]
(when (= user (:id (session/get :user)))
Please sign in to comment.
Something went wrong with that request. Please try again.