USING: namespaces assocs sorting sequences kernel accessors hashtables sequences.lib locals db.types db.tuples db calendar calendar.format rss xml.writer xmode.catalog http.server http.server.crud http.server.actions http.server.components http.server.components.code http.server.templating.chloe http.server.auth.login http.server.boilerplate http.server.validators http.server.forms ; IN: webapps.pastebin : ( id -- component ) modes keys natural-sort ; : pastebin-template ( name -- template ) "resource:extra/webapps/pastebin/" swap ".xml" 3append ; TUPLE: paste id summary author mode date contents annotations captcha ; paste "PASTE" { { "id" "ID" INTEGER +native-id+ } { "summary" "SUMMARY" { VARCHAR 256 } +not-null+ } { "author" "AUTHOR" { VARCHAR 256 } +not-null+ } { "mode" "MODE" { VARCHAR 256 } +not-null+ } { "date" "DATE" DATETIME +not-null+ } { "contents" "CONTENTS" TEXT +not-null+ } } define-persistent : ( id -- paste ) paste new swap >>id ; : pastes ( -- pastes ) f select-tuples ; TUPLE: annotation aid id summary author mode contents date captcha ; annotation "ANNOTATION" { { "aid" "AID" INTEGER +native-id+ } { "id" "ID" INTEGER +not-null+ } { "summary" "SUMMARY" { VARCHAR 256 } +not-null+ } { "author" "AUTHOR" { VARCHAR 256 } +not-null+ } { "mode" "MODE" { VARCHAR 256 } +not-null+ } { "date" "DATE" DATETIME +not-null+ } { "contents" "CONTENTS" TEXT +not-null+ } } define-persistent : ( id aid -- annotation ) annotation new swap >>aid swap >>id ; : fetch-annotations ( paste -- paste ) dup annotations>> [ dup id>> f select-tuples >>annotations ] unless ; : ( -- form ) "paste"
"id" hidden >>renderer add-field "aid" hidden >>renderer add-field "annotation" pastebin-template >>view-template "summary" add-field "author" add-field "mode" add-field "contents" "mode" add-field "date" add-field ; : ( -- form ) "paste" "new-annotation" pastebin-template >>edit-template "id" hidden >>renderer t >>required add-field "summary" t >>required add-field "author" t >>required add-field "mode" "factor" >>default t >>required add-field "contents" "mode" t >>required add-field "captcha" add-field ; : ( -- form ) "paste" "paste" pastebin-template >>view-template "paste-summary" pastebin-template >>summary-template "id" hidden >>renderer add-field "summary" add-field "author" add-field "mode" add-field "date" add-field "contents" "mode" add-field "annotations" +plain+ add-field ; : ( -- form ) "paste" "new-paste" pastebin-template >>edit-template "summary" t >>required add-field "author" t >>required add-field "mode" "factor" >>default t >>required add-field "contents" "mode" t >>required add-field "captcha" add-field ; : ( -- form ) "pastebin" "paste-list" pastebin-template >>view-template "pastes" +plain+ add-field ; :: ( -- action ) [let | form [ ] | [ blank-values pastes "pastes" set-value form view-form ] >>display ] ; :: ( form ctor next -- action ) { { "id" [ v-number ] } } >>get-params [ "id" get f ctor call from-tuple form set-defaults ] >>init [ form edit-form ] >>display [ f f ctor call from-tuple form validate-form values-tuple insert-tuple "id" value next ] >>submit ; : pastebin-feed-entries ( -- entries ) pastes 20 short head [ [ summary>> ] [ "$pastebin/view-paste" swap id>> "id" associate link>string ] [ date>> ] tri f swap ] map ; : pastebin-feed ( -- feed ) feed new "Factor Pastebin" >>title "http://paste.factorcode.org" >>link pastebin-feed-entries >>entries ; : ( -- action ) [ "text/xml" [ pastebin-feed feed>xml write-xml ] >>body ] >>display ; :: ( form ctor -- action ) { { "id" [ v-number ] } } >>get-params [ "id" get ctor call select-tuple fetch-annotations from-tuple ] >>init [ form view-form ] >>display ; :: ( ctor next -- action ) { { "id" [ v-number ] } } >>post-params [ "id" get ctor call delete-tuple "id" get f select-tuples [ delete-tuple ] each next f ] >>submit ; :: ( ctor next -- action ) { { "id" [ v-number ] } { "aid" [ v-number ] } } >>post-params [ "id" get "aid" get ctor call delete-tuple "id" get next ] >>submit ; :: ( form ctor next -- action ) [ f ctor call from-tuple form set-defaults ] >>init [ form edit-form ] >>display [ f ctor call from-tuple form validate-form values-tuple insert-tuple "id" value next ] >>submit ; TUPLE: pastebin < dispatcher ; : ( -- responder ) pastebin new-dispatcher "list" add-main-responder "feed.xml" add-responder [ ] "view-paste" add-responder [ ] "$pastebin/list" "delete-paste" add-responder [ ] "$pastebin/view-paste" "delete-annotation" add-responder [ ] "$pastebin/view-paste" add-responder [ now >>date ] "$pastebin/view-paste" "new-paste" add-responder [ now >>date ] "$pastebin/view-paste" "annotate" add-responder "pastebin" pastebin-template >>template ; : init-pastes-table paste ensure-table ; : init-annotations-table annotation ensure-table ;