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 sequences.lib db.types db.tuples db combinators
5 calendar calendar.format math.parser syndication urls xml.writer
6 xmode.catalog validators
10 http.server.dispatchers
11 http.server.redirection
20 TUPLE: pastebin < dispatcher ;
26 TUPLE: entity id summary author mode date contents ;
30 { "id" "ID" INTEGER +db-assigned-id+ }
31 { "summary" "SUMMARY" { VARCHAR 256 } +not-null+ }
32 { "author" "AUTHOR" { VARCHAR 256 } +not-null+ }
33 { "mode" "MODE" { VARCHAR 256 } +not-null+ }
34 { "date" "DATE" DATETIME +not-null+ }
35 { "contents" "CONTENTS" TEXT +not-null+ }
38 GENERIC: entity-url ( entity -- url )
40 M: entity feed-entry-title summary>> ;
42 M: entity feed-entry-date date>> ;
44 M: entity feed-entry-url entity-url ;
46 TUPLE: paste < entity annotations ;
48 \ paste "PASTES" { } define-persistent
50 : <paste> ( id -- paste )
54 : pastes ( -- pastes )
55 f <paste> select-tuples ;
57 TUPLE: annotation < entity parent ;
59 annotation "ANNOTATIONS"
61 { "parent" "PARENT" INTEGER +not-null+ }
64 : <annotation> ( parent id -- annotation )
69 : paste ( id -- paste )
70 [ <paste> select-tuple ]
71 [ f <annotation> select-tuples ]
78 : pastebin-url ( -- url )
79 URL" $pastebin/list" ;
81 : paste-url ( id -- url )
82 "$pastebin/paste" >url swap "id" set-query-param ;
87 : annotation-url ( parent id -- url )
88 "$pastebin/paste" >url
89 swap number>string >>anchor
90 swap "id" set-query-param ;
92 M: annotation entity-url
93 [ parent>> ] [ id>> ] bi annotation-url ;
99 : <pastebin-action> ( -- action )
101 [ pastes "pastes" set-value ] >>init
102 { pastebin "pastebin" } >>template ;
104 : <pastebin-feed-action> ( -- action )
106 [ pastebin-url ] >>url
107 [ "Factor Pastebin" ] >>title
108 [ pastes <reversed> ] >>entries ;
114 : <paste-action> ( -- action )
118 "id" value paste from-object
123 mode-names "modes" set-value
124 "factor" "mode" set-value
128 { pastebin "paste" } >>template ;
130 : <paste-feed-action> ( -- action )
132 [ validate-integer-id ] >>init
133 [ "id" value paste-url ] >>url
134 [ "Paste " "id" value number>string append ] >>title
135 [ "id" value f <annotation> select-tuples ] >>entries ;
137 : validate-entity ( -- )
139 { "summary" [ v-one-line ] }
140 { "author" [ v-one-line ] }
141 { "mode" [ v-mode ] }
142 { "contents" [ v-required ] }
143 { "captcha" [ v-captcha ] }
146 : deposit-entity-slots ( tuple -- )
148 { "summary" "author" "mode" "contents" } deposit-slots ;
150 : <new-paste-action> ( -- action )
153 "factor" "mode" set-value
154 mode-names "modes" set-value
157 { pastebin "new-paste" } >>template
159 [ mode-names "modes" set-value ] >>validate
165 [ deposit-entity-slots ]
167 [ id>> paste-url <redirect> ]
171 : <delete-paste-action> ( -- action )
173 [ validate-integer-id ] >>validate
176 "id" value <paste> delete-tuples
177 "id" value f <annotation> delete-tuples
178 URL" $pastebin/list" <redirect>
185 : <new-annotation-action> ( -- action )
188 { { "parent" [ v-integer ] } } validate-params
193 "parent" value f <annotation>
194 [ deposit-entity-slots ]
196 [ entity-url <redirect> ]
200 : <delete-annotation-action> ( -- action )
202 [ { { "id" [ v-number ] } } validate-params ] >>validate
205 f "id" value <annotation> select-tuple
207 [ parent>> paste-url <redirect> ]
211 SYMBOL: can-delete-pastes?
213 can-delete-pastes? define-capability
215 : <pastebin> ( -- responder )
216 pastebin new-dispatcher
217 <pastebin-action> "list" add-main-responder
218 <pastebin-feed-action> "list.atom" add-responder
219 <paste-action> "paste" add-responder
220 <paste-feed-action> "paste.atom" add-responder
221 <new-paste-action> "new-paste" add-responder
222 <delete-paste-action> <protected>
223 "delete pastes" >>description
224 { can-delete-pastes? } >>capabilities "delete-paste" add-responder
225 <new-annotation-action> "new-annotation" add-responder
226 <delete-annotation-action> <protected>
227 "delete annotations" >>description
228 { can-delete-pastes? } >>capabilities "delete-annotation" add-responder
230 { pastebin "pastebin-common" } >>template ;
232 : init-pastes-table ( -- ) \ paste ensure-table ;
234 : init-annotations-table ( -- ) annotation ensure-table ;