1 ! Copyright (C) 2007, 2008 Slava Pestov
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: namespaces assocs sorting sequences kernel accessors
4 hashtables db.types db.tuples db combinators
5 calendar calendar.format math.parser math.order syndication urls
6 xml.writer xmode.catalog validators
11 http.server.dispatchers
12 http.server.redirection
22 TUPLE: pastebin < dispatcher ;
24 SYMBOL: can-delete-pastes?
26 can-delete-pastes? define-capability
32 TUPLE: entity id summary author mode date contents ;
36 { "id" "ID" INTEGER +db-assigned-id+ }
37 { "summary" "SUMMARY" { VARCHAR 256 } +not-null+ }
38 { "author" "AUTHOR" { VARCHAR 256 } +not-null+ }
39 { "mode" "MODE" { VARCHAR 256 } +not-null+ }
40 { "date" "DATE" DATETIME +not-null+ }
41 { "contents" "CONTENTS" TEXT +not-null+ }
44 GENERIC: entity-url ( entity -- url )
46 M: entity feed-entry-title summary>> ;
48 M: entity feed-entry-date date>> ;
50 M: entity feed-entry-url entity-url ;
52 TUPLE: paste < entity annotations ;
54 \ paste "PASTES" { } define-persistent
56 : <paste> ( id -- paste )
60 : pastes ( -- pastes )
61 f <paste> select-tuples
65 TUPLE: annotation < entity parent ;
67 annotation "ANNOTATIONS"
69 { "parent" "PARENT" INTEGER +not-null+ }
72 : <annotation> ( parent id -- annotation )
77 : paste ( id -- paste )
78 [ <paste> select-tuple ]
79 [ f <annotation> select-tuples ]
86 CONSTANT: pastebin-url URL" $pastebin/"
88 : paste-url ( id -- url )
89 "$pastebin/paste" >url swap "id" set-query-param ;
94 : annotation-url ( parent id -- url )
95 "$pastebin/paste" >url
96 swap number>string >>anchor
97 swap "id" set-query-param ;
99 M: annotation entity-url
100 [ parent>> ] [ id>> ] bi annotation-url ;
106 : <pastebin-action> ( -- action )
108 [ pastes "pastes" set-value ] >>init
109 { pastebin "pastebin" } >>template ;
111 : <pastebin-feed-action> ( -- action )
113 [ pastebin-url ] >>url
114 [ "Factor Pastebin" ] >>title
115 [ pastes ] >>entries ;
121 : <paste-action> ( -- action )
125 "id" value paste from-object
130 mode-names "modes" set-value
131 "factor" "mode" set-value
135 { pastebin "paste" } >>template ;
137 : <paste-feed-action> ( -- action )
139 [ validate-integer-id ] >>init
140 [ "id" value paste-url ] >>url
141 [ "Paste " "id" value number>string append ] >>title
142 [ "id" value f <annotation> select-tuples ] >>entries ;
144 : validate-entity ( -- )
146 { "summary" [ v-one-line ] }
147 { "author" [ v-one-line ] }
148 { "mode" [ v-mode ] }
149 { "contents" [ v-required ] }
150 { "captcha" [ v-captcha ] }
153 : deposit-entity-slots ( tuple -- )
155 { "summary" "author" "mode" "contents" } to-object ;
157 : <new-paste-action> ( -- action )
160 "factor" "mode" set-value
161 mode-names "modes" set-value
164 { pastebin "new-paste" } >>template
167 mode-names "modes" set-value
173 [ deposit-entity-slots ]
175 [ id>> paste-url <redirect> ]
179 : <delete-paste-action> ( -- action )
182 [ validate-integer-id ] >>validate
186 "id" value <paste> delete-tuples
187 "id" value f <annotation> delete-tuples
189 pastebin-url <redirect>
193 "delete pastes" >>description
194 { can-delete-pastes? } >>capabilities ;
200 : <new-annotation-action> ( -- action )
203 mode-names "modes" set-value
204 { { "parent" [ v-integer ] } } validate-params
209 "parent" value f <annotation>
210 [ deposit-entity-slots ]
212 [ entity-url <redirect> ]
216 : <delete-annotation-action> ( -- action )
219 [ { { "id" [ v-number ] } } validate-params ] >>validate
222 f "id" value <annotation> select-tuple
224 [ parent>> paste-url <redirect> ]
229 "delete annotations" >>description
230 { can-delete-pastes? } >>capabilities ;
232 : <pastebin> ( -- responder )
233 pastebin new-dispatcher
234 <pastebin-action> "" add-responder
235 <pastebin-feed-action> "list.atom" add-responder
236 <paste-action> "paste" add-responder
237 <paste-feed-action> "paste.atom" add-responder
238 <new-paste-action> "new-paste" add-responder
239 <delete-paste-action> "delete-paste" add-responder
240 <new-annotation-action> "new-annotation" add-responder
241 <delete-annotation-action> "delete-annotation" add-responder
243 { pastebin "pastebin-common" } >>template ;