factor/extra/webapps/planet/planet.factor

204 lines
5.3 KiB
Factor
Raw Normal View History

2008-04-20 05:19:06 -04:00
! Copyright (C) 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
2008-05-26 01:47:27 -04:00
USING: kernel accessors sequences sorting math math.order
2008-04-22 22:08:27 -04:00
calendar alarms logging concurrency.combinators namespaces
2008-05-26 01:47:27 -04:00
sequences.lib db.types db.tuples db fry locals hashtables
html.components
rss urls xml.writer
2008-05-26 01:47:27 -04:00
validators
2008-04-20 05:19:06 -04:00
http.server
furnace.actions
furnace.boilerplate
furnace.auth.login
furnace.auth ;
2008-04-20 05:19:06 -04:00
IN: webapps.planet
2008-04-25 04:23:47 -04:00
TUPLE: blog id name www-url feed-url ;
2008-04-20 05:19:06 -04:00
2008-04-22 22:08:27 -04:00
M: blog link-title name>> ;
M: blog link-href www-url>> ;
2008-04-20 05:19:06 -04:00
blog "BLOGS"
{
2008-04-28 18:38:12 -04:00
{ "id" "ID" INTEGER +db-assigned-id+ }
2008-04-20 05:19:06 -04:00
{ "name" "NAME" { VARCHAR 256 } +not-null+ }
{ "www-url" "WWWURL" { VARCHAR 256 } +not-null+ }
2008-04-25 04:23:47 -04:00
{ "feed-url" "FEEDURL" { VARCHAR 256 } +not-null+ }
2008-04-20 05:19:06 -04:00
} define-persistent
2008-05-26 01:47:27 -04:00
! TUPLE: posting < entry id ;
TUPLE: posting id title link description pub-date ;
posting "POSTINGS"
{
{ "id" "ID" INTEGER +db-assigned-id+ }
{ "title" "TITLE" { VARCHAR 256 } +not-null+ }
{ "link" "LINK" { VARCHAR 256 } +not-null+ }
{ "description" "DESCRIPTION" TEXT +not-null+ }
{ "pub-date" "DATE" TIMESTAMP +not-null+ }
} define-persistent
2008-04-20 05:19:06 -04:00
: init-blog-table blog ensure-table ;
2008-05-26 01:47:27 -04:00
: init-postings-table posting ensure-table ;
2008-04-20 05:19:06 -04:00
: <blog> ( id -- todo )
blog new
swap >>id ;
2008-04-22 22:08:27 -04:00
: blogroll ( -- seq )
2008-05-26 01:47:27 -04:00
f <blog> select-tuples
[ [ name>> ] compare ] sort ;
: postings ( -- seq )
posting new select-tuples
[ [ pub-date>> ] compare invert-comparison ] sort ;
: <edit-blogroll-action> ( -- action )
<page-action>
[ blogroll "blogroll" set-value ] >>init
"$planet-factor/admin" >>template ;
2008-05-26 01:47:27 -04:00
: <planet-action> ( -- action )
<page-action>
[
blogroll "blogroll" set-value
postings "postings" set-value
] >>init
"$planet-factor/planet" >>template ;
2008-05-26 01:47:27 -04:00
: planet-feed ( -- feed )
2008-04-20 05:19:06 -04:00
feed new
2008-04-25 04:23:47 -04:00
"Planet Factor" >>title
2008-04-20 05:19:06 -04:00
"http://planet.factorcode.org" >>link
2008-05-26 01:47:27 -04:00
postings >>entries ;
2008-04-20 05:19:06 -04:00
2008-05-26 01:47:27 -04:00
: <planet-feed-action> ( -- action )
<feed-action> [ planet-feed ] >>feed ;
2008-04-20 05:19:06 -04:00
2008-05-26 01:47:27 -04:00
:: <posting> ( entry name -- entry' )
posting new
name ": " entry title>> 3append >>title
entry link>> >>link
entry description>> >>description
entry pub-date>> >>pub-date ;
2008-04-20 05:19:06 -04:00
: fetch-feed ( url -- feed )
download-feed entries>> ;
\ fetch-feed DEBUG add-error-logging
: fetch-blogroll ( blogroll -- entries )
2008-05-26 01:47:27 -04:00
[ [ feed-url>> fetch-feed ] parallel-map ] [ [ name>> ] map ] bi
[ '[ , <posting> ] map ] 2map concat ;
2008-04-20 05:19:06 -04:00
: sort-entries ( entries -- entries' )
2008-05-26 01:47:27 -04:00
[ [ pub-date>> ] compare invert-comparison ] sort ;
2008-04-20 05:19:06 -04:00
2008-05-26 01:47:27 -04:00
: update-cached-postings ( -- )
blogroll fetch-blogroll sort-entries 8 short head [
posting new delete-tuples
[ insert-tuple ] each
] with-transaction ;
2008-04-20 05:19:06 -04:00
2008-05-26 01:47:27 -04:00
: <update-action> ( -- action )
2008-04-20 05:19:06 -04:00
<action>
[
2008-05-26 01:47:27 -04:00
update-cached-postings
URL" $planet-factor/admin" <redirect>
2008-05-26 01:47:27 -04:00
] >>submit ;
2008-04-20 05:19:06 -04:00
2008-05-26 01:47:27 -04:00
: <delete-blog-action> ( -- action )
<action>
[ validate-integer-id ] >>validate
[
"id" value <blog> delete-tuples
URL" $planet-factor/admin" <redirect>
2008-05-26 01:47:27 -04:00
] >>submit ;
: validate-blog ( -- )
{
{ "name" [ v-one-line ] }
{ "www-url" [ v-url ] }
{ "feed-url" [ v-url ] }
} validate-params ;
2008-05-26 03:54:53 -04:00
: deposit-blog-slots ( blog -- )
{ "name" "www-url" "feed-url" } deposit-slots ;
2008-05-26 01:47:27 -04:00
: <new-blog-action> ( -- action )
<page-action>
"$planet-factor/new-blog" >>template
2008-05-26 01:47:27 -04:00
[ validate-blog ] >>validate
[
f <blog>
2008-05-26 03:54:53 -04:00
[ deposit-blog-slots ]
2008-05-26 01:47:27 -04:00
[ insert-tuple ]
[
<url>
"$planet-factor/admin/edit-blog" >>path
swap id>> "id" set-query-param
<redirect>
]
2008-05-26 03:54:53 -04:00
tri
2008-05-26 01:47:27 -04:00
] >>submit ;
: <edit-blog-action> ( -- action )
<page-action>
[
validate-integer-id
"id" value <blog> select-tuple from-object
2008-05-26 01:47:27 -04:00
] >>init
2008-04-20 05:19:06 -04:00
"$planet-factor/edit-blog" >>template
2008-05-26 01:47:27 -04:00
[
validate-integer-id
validate-blog
] >>validate
2008-04-25 04:23:47 -04:00
2008-05-26 01:47:27 -04:00
[
f <blog>
2008-05-26 03:54:53 -04:00
[ deposit-blog-slots ]
2008-05-26 01:47:27 -04:00
[ update-tuple ]
[
<url>
"$planet-factor/admin" >>path
swap id>> "id" set-query-param
<redirect>
]
2008-05-26 03:54:53 -04:00
tri
2008-05-26 01:47:27 -04:00
] >>submit ;
TUPLE: planet-factor-admin < dispatcher ;
: <planet-factor-admin> ( -- responder )
planet-factor-admin new-dispatcher
<edit-blogroll-action> "blogroll" add-main-responder
<update-action> "update" add-responder
<new-blog-action> "new-blog" add-responder
<edit-blog-action> "edit-blog" add-responder
<delete-blog-action> "delete-blog" add-responder ;
2008-04-20 05:19:06 -04:00
2008-05-01 17:24:50 -04:00
SYMBOL: can-administer-planet-factor?
can-administer-planet-factor? define-capability
2008-05-26 01:47:27 -04:00
TUPLE: planet-factor < dispatcher ;
2008-04-22 22:08:27 -04:00
: <planet-factor> ( -- responder )
planet-factor new-dispatcher
2008-05-26 01:47:27 -04:00
<planet-action> "list" add-main-responder
<feed-action> "feed.xml" add-responder
<planet-factor-admin> { can-administer-planet-factor? } <protected> "admin" add-responder
2008-04-20 05:19:06 -04:00
<boilerplate>
"$planet-factor/planet-common" >>template ;
2008-04-22 22:08:27 -04:00
2008-05-26 01:47:27 -04:00
: start-update-task ( db params -- )
'[ , , [ update-cached-postings ] with-db ] 10 minutes every drop ;