factor/extra/webapps/pastebin/pastebin.factor

245 lines
5.8 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-09-23 15:06:49 -04:00
calendar calendar.format math.parser math.order syndication urls
xml.writer xmode.catalog validators
html.forms
2008-06-02 16:00:03 -04:00
html.components
html.templates.chloe
2008-04-25 04:23:47 -04:00
http.server
2008-06-02 16:00:03 -04:00
http.server.dispatchers
http.server.redirection
furnace
furnace.actions
2008-06-17 01:10:46 -04:00
furnace.redirection
furnace.auth
furnace.auth.login
2008-06-02 16:00:03 -04:00
furnace.boilerplate
2008-06-05 02:56:06 -04:00
furnace.syndication ;
2008-04-25 04:23:47 -04:00
IN: webapps.pastebin
2008-06-02 16:00:03 -04:00
TUPLE: pastebin < dispatcher ;
SYMBOL: can-delete-pastes?
can-delete-pastes? define-capability
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
GENERIC: entity-url ( entity -- url )
M: entity feed-entry-title summary>> ;
M: entity feed-entry-date date>> ;
M: entity feed-entry-url entity-url ;
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 )
2008-09-23 15:06:49 -04:00
f <paste> select-tuples
[ [ date>> ] compare ] sort
reverse ;
2008-04-25 04:23:47 -04:00
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
2008-05-26 01:47:27 -04:00
: paste ( id -- paste )
[ <paste> select-tuple ]
[ f <annotation> select-tuples ]
bi >>annotations ;
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
: pastebin-url ( -- url )
URL" $pastebin/list" ;
2008-04-25 04:23:47 -04:00
: paste-url ( id -- url )
"$pastebin/paste" >url swap "id" set-query-param ;
M: paste entity-url
id>> paste-url ;
2008-04-25 04:23:47 -04:00
: annotation-url ( parent id -- url )
"$pastebin/paste" >url
swap number>string >>anchor
swap "id" set-query-param ;
2008-04-25 04:23:47 -04:00
M: annotation entity-url
[ parent>> ] [ id>> ] bi annotation-url ;
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
2008-06-02 16:00:03 -04:00
{ pastebin "pastebin" } >>template ;
2008-04-25 04:23:47 -04:00
2008-05-26 01:47:27 -04:00
: <pastebin-feed-action> ( -- action )
<feed-action>
[ pastebin-url ] >>url
[ "Factor Pastebin" ] >>title
2008-09-23 15:06:49 -04:00
[ pastes ] >>entries ;
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-object
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" [
"parent" set-value
2008-05-26 01:47:27 -04:00
mode-names "modes" set-value
"factor" "mode" set-value
] nest-form
2008-05-26 01:47:27 -04:00
] >>init
2008-04-25 04:23:47 -04:00
2008-06-02 16:00:03 -04:00
{ pastebin "paste" } >>template ;
2008-04-25 04:23:47 -04:00
2008-05-26 01:47:27 -04:00
: <paste-feed-action> ( -- action )
<feed-action>
[ validate-integer-id ] >>init
[ "id" value paste-url ] >>url
[ "Paste " "id" value number>string append ] >>title
[ "id" value f <annotation> select-tuples ] >>entries ;
2008-05-26 01:47:27 -04:00
: 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" } to-object ;
2008-05-26 03:54:53 -04:00
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-06-02 16:00:03 -04:00
{ pastebin "new-paste" } >>template
2008-05-26 01:47:27 -04:00
[
mode-names "modes" set-value
validate-entity
] >>validate
2008-05-26 01:47:27 -04:00
[
2008-05-26 01:47:27 -04:00
f <paste>
[ deposit-entity-slots ]
2008-05-26 01:47:27 -04:00
[ insert-tuple ]
[ id>> paste-url <redirect> ]
2008-05-26 03:54:53 -04:00
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
[
[
"id" value <paste> delete-tuples
"id" value f <annotation> delete-tuples
] with-transaction
URL" $pastebin/list" <redirect>
] >>submit
<protected>
"delete pastes" >>description
{ can-delete-pastes? } >>capabilities ;
2008-04-25 04:23:47 -04:00
2008-05-26 01:47:27 -04:00
! ! !
! ANNOTATIONS
! ! !
: <new-annotation-action> ( -- action )
<action>
[
mode-names "modes" set-value
{ { "parent" [ v-integer ] } } validate-params
validate-entity
] >>validate
2008-04-25 04:23:47 -04:00
[
"parent" value f <annotation>
[ deposit-entity-slots ]
[ insert-tuple ]
[ entity-url <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>> paste-url <redirect> ]
2008-05-26 01:47:27 -04:00
bi
] >>submit
2008-04-25 04:23:47 -04:00
<protected>
"delete annotations" >>description
{ can-delete-pastes? } >>capabilities ;
2008-05-01 17:24:50 -04:00
2008-04-25 04:23:47 -04:00
: <pastebin> ( -- responder )
pastebin new-dispatcher
2008-09-27 12:38:20 -04:00
<pastebin-action> "" add-responder
2008-05-26 01:47:27 -04:00
<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> "delete-paste" add-responder
2008-05-26 01:47:27 -04:00
<new-annotation-action> "new-annotation" add-responder
<delete-annotation-action> "delete-annotation" add-responder
2008-04-25 04:23:47 -04:00
<boilerplate>
2008-06-02 16:00:03 -04:00
{ pastebin "pastebin-common" } >>template ;