factor/extra/webapps/pastebin/pastebin.factor

250 lines
6.3 KiB
Factor
Raw Normal View History

2008-05-26 01:47:27 -04:00
! Copyright (C) 2007, 2008 Slava Pestov
! See http://factorcode.org/license.txt for BSD license.
2008-04-25 04:23:47 -04:00
USING: namespaces assocs sorting sequences kernel accessors
2008-05-26 03:54:53 -04:00
hashtables sequences.lib db.types db.tuples db combinators
2008-05-26 01:47:27 -04:00
calendar calendar.format math.parser rss xml.writer
xmode.catalog validators html.components html.templates.chloe
2008-04-25 04:23:47 -04:00
http.server
http.server.actions
2008-05-01 17:24:50 -04:00
http.server.auth
http.server.auth.login
2008-05-26 01:47:27 -04:00
http.server.boilerplate ;
2008-04-25 04:23:47 -04:00
IN: webapps.pastebin
2008-05-26 01:47:27 -04:00
! ! !
! DOMAIN MODEL
! ! !
2008-04-25 04:23:47 -04:00
TUPLE: entity id summary author mode date contents ;
2008-04-25 04:23:47 -04:00
entity f
2008-04-25 04:23:47 -04:00
{
2008-04-28 18:38:12 -04:00
{ "id" "ID" INTEGER +db-assigned-id+ }
2008-04-25 04:23:47 -04:00
{ "summary" "SUMMARY" { VARCHAR 256 } +not-null+ }
{ "author" "AUTHOR" { VARCHAR 256 } +not-null+ }
{ "mode" "MODE" { VARCHAR 256 } +not-null+ }
{ "date" "DATE" DATETIME +not-null+ }
2008-04-25 04:23:47 -04:00
{ "contents" "CONTENTS" TEXT +not-null+ }
} define-persistent
TUPLE: paste < entity annotations ;
\ paste "PASTES" { } define-persistent
2008-04-25 04:23:47 -04:00
: <paste> ( id -- paste )
2008-05-26 01:47:27 -04:00
\ paste new
2008-04-25 04:23:47 -04:00
swap >>id ;
: pastes ( -- pastes )
f <paste> select-tuples ;
TUPLE: annotation < entity parent ;
2008-04-25 04:23:47 -04:00
annotation "ANNOTATIONS"
2008-04-25 04:23:47 -04:00
{
{ "parent" "PARENT" INTEGER +not-null+ }
2008-04-25 04:23:47 -04:00
} define-persistent
: <annotation> ( parent id -- annotation )
2008-04-25 04:23:47 -04:00
annotation new
swap >>id
swap >>parent ;
2008-04-25 04:23:47 -04:00
: fetch-annotations ( paste -- paste )
dup annotations>> [
dup id>> f <annotation> select-tuples >>annotations
] unless ;
2008-05-26 01:47:27 -04:00
: paste ( id -- paste )
<paste> select-tuple fetch-annotations ;
2008-04-25 04:23:47 -04:00
2008-05-26 01:47:27 -04:00
: <id-redirect> ( id next -- response )
swap "id" associate <standard-redirect> ;
2008-04-25 04:23:47 -04:00
2008-05-26 01:47:27 -04:00
! ! !
! LINKS, ETC
! ! !
2008-04-25 04:23:47 -04:00
2008-05-26 01:47:27 -04:00
: pastebin-link ( -- url )
"$pastebin/list" f link>string ;
2008-04-25 04:23:47 -04:00
2008-05-26 01:47:27 -04:00
GENERIC: entity-link ( entity -- url )
2008-04-25 04:23:47 -04:00
2008-05-26 01:47:27 -04:00
M: paste entity-link
id>> "id" associate "$pastebin/paste" swap link>string ;
2008-04-25 04:23:47 -04:00
2008-05-26 01:47:27 -04:00
M: annotation entity-link
[ parent>> "parent" associate "$pastebin/paste" swap link>string ]
[ id>> number>string "#" prepend ] bi
2008-05-26 01:47:27 -04:00
append ;
2008-04-25 04:23:47 -04:00
2008-05-26 01:47:27 -04:00
: pastebin-template ( name -- template )
"resource:extra/webapps/pastebin/" swap ".xml" 3append <chloe> ;
2008-04-25 04:23:47 -04:00
2008-05-26 01:47:27 -04:00
! ! !
! PASTE LIST
! ! !
2008-04-25 04:23:47 -04:00
2008-05-26 01:47:27 -04:00
: <pastebin-action> ( -- action )
<page-action>
[ pastes "pastes" set-value ] >>init
"pastebin" pastebin-template >>template ;
2008-04-25 04:23:47 -04:00
2008-05-26 01:47:27 -04:00
: pastebin-feed-entries ( seq -- entries )
<reversed> 20 short head [
entry new
swap
[ summary>> >>title ]
[ date>> >>pub-date ]
[ entity-link >>link ]
tri
2008-04-25 04:23:47 -04:00
] map ;
: pastebin-feed ( -- feed )
feed new
"Factor Pastebin" >>title
2008-05-26 01:47:27 -04:00
pastebin-link >>link
pastes pastebin-feed-entries >>entries ;
2008-04-25 04:23:47 -04:00
2008-05-26 01:47:27 -04:00
: <pastebin-feed-action> ( -- action )
<feed-action> [ pastebin-feed ] >>feed ;
2008-04-25 04:23:47 -04:00
2008-05-26 01:47:27 -04:00
! ! !
! PASTES
! ! !
2008-04-25 04:23:47 -04:00
2008-05-26 01:47:27 -04:00
: <paste-action> ( -- action )
<page-action>
[
validate-integer-id
"id" value paste from-tuple
2008-04-25 04:23:47 -04:00
2008-05-26 03:54:53 -04:00
"id" value
2008-05-26 01:47:27 -04:00
"new-annotation" [
2008-05-26 03:54:53 -04:00
"id" set-value
2008-05-26 01:47:27 -04:00
mode-names "modes" set-value
"factor" "mode" set-value
] nest-values
] >>init
2008-04-25 04:23:47 -04:00
2008-05-26 01:47:27 -04:00
"paste" pastebin-template >>template ;
2008-04-25 04:23:47 -04:00
2008-05-26 01:47:27 -04:00
: paste-feed-entries ( paste -- entries )
fetch-annotations annotations>> pastebin-feed-entries ;
: paste-feed ( paste -- feed )
feed new
swap
[ "Paste #" swap id>> number>string append >>title ]
[ entity-link >>link ]
[ paste-feed-entries >>entries ]
tri ;
: <paste-feed-action> ( -- action )
<feed-action>
[ validate-integer-id ] >>init
[ "id" value paste annotations>> paste-feed ] >>feed ;
: validate-entity ( -- )
2008-05-26 03:54:53 -04:00
{
{ "summary" [ v-one-line ] }
{ "author" [ v-one-line ] }
{ "mode" [ v-mode ] }
{ "contents" [ v-required ] }
{ "captcha" [ v-captcha ] }
} validate-params ;
: deposit-entity-slots ( tuple -- )
2008-05-26 03:54:53 -04:00
now >>date
{ "summary" "author" "mode" "contents" } deposit-slots ;
2008-05-26 01:47:27 -04:00
: <new-paste-action> ( -- action )
<page-action>
2008-04-25 04:23:47 -04:00
[
2008-05-26 01:47:27 -04:00
"factor" "mode" set-value
mode-names "modes" set-value
] >>init
2008-04-25 04:23:47 -04:00
2008-05-26 01:47:27 -04:00
"new-paste" pastebin-template >>template
2008-04-25 04:23:47 -04:00
2008-05-26 01:47:27 -04:00
[
validate-entity
2008-05-26 01:47:27 -04:00
f <paste>
[ deposit-entity-slots ]
2008-05-26 01:47:27 -04:00
[ insert-tuple ]
2008-05-26 03:54:53 -04:00
[ id>> "$pastebin/paste" <id-redirect> ]
tri
2008-04-25 04:23:47 -04:00
] >>submit ;
2008-05-26 01:47:27 -04:00
: <delete-paste-action> ( -- action )
2008-04-25 04:23:47 -04:00
<action>
2008-05-26 01:47:27 -04:00
[ validate-integer-id ] >>validate
2008-04-25 04:23:47 -04:00
[
2008-05-26 01:47:27 -04:00
"id" value <paste> delete-tuples
"id" value f <annotation> delete-tuples
"$pastebin/list" f <permanent-redirect>
2008-04-25 04:23:47 -04:00
] >>submit ;
2008-05-26 01:47:27 -04:00
! ! !
! ANNOTATIONS
! ! !
: <new-annotation-action> ( -- action )
2008-05-26 03:54:53 -04:00
<page-action>
[
{ { "id" [ v-integer ] } } validate-params
"id" value "$pastebin/paste" <id-redirect>
] >>display
2008-05-26 03:54:53 -04:00
[
{ { "id" [ v-integer ] } } validate-params
validate-entity
] >>validate
2008-04-25 04:23:47 -04:00
[
"id" value f <annotation>
[ deposit-entity-slots ]
[ insert-tuple ]
[
! Add anchor here
parent>> "$pastebin/paste" <id-redirect>
]
tri
2008-05-26 01:47:27 -04:00
] >>submit ;
2008-04-25 04:23:47 -04:00
2008-05-26 01:47:27 -04:00
: <delete-annotation-action> ( -- action )
<action>
[ { { "id" [ v-number ] } } validate-params ] >>validate
2008-04-25 04:23:47 -04:00
2008-05-26 01:47:27 -04:00
[
f "id" value <annotation> select-tuple
2008-05-26 01:47:27 -04:00
[ delete-tuples ]
[ parent>> "$pastebin/paste" <id-redirect> ]
2008-05-26 01:47:27 -04:00
bi
2008-04-25 04:23:47 -04:00
] >>submit ;
TUPLE: pastebin < dispatcher ;
2008-05-01 17:24:50 -04:00
SYMBOL: can-delete-pastes?
can-delete-pastes? define-capability
2008-04-25 04:23:47 -04:00
: <pastebin> ( -- responder )
pastebin new-dispatcher
2008-05-26 01:47:27 -04:00
<pastebin-action> "list" add-main-responder
<pastebin-feed-action> "list.atom" add-responder
<paste-action> "paste" add-responder
<paste-feed-action> "paste.atom" add-responder
<new-paste-action> "new-paste" add-responder
<delete-paste-action> { can-delete-pastes? } <protected> "delete-paste" add-responder
<new-annotation-action> "new-annotation" add-responder
<delete-annotation-action> { can-delete-pastes? } <protected> "delete-annotation" add-responder
2008-04-25 04:23:47 -04:00
<boilerplate>
2008-05-26 01:47:27 -04:00
"pastebin-common" pastebin-template >>template ;
2008-04-25 04:23:47 -04:00
2008-05-26 01:47:27 -04:00
: init-pastes-table \ paste ensure-table ;
2008-04-25 04:23:47 -04:00
: init-annotations-table annotation ensure-table ;