factor/extra/webapps/planet/planet.factor

198 lines
4.8 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
db.types db.tuples db fry locals hashtables
syndication urls xml.writer validators
html.forms
html.components
2008-04-20 05:19:06 -04:00
http.server
2008-06-02 16:00:03 -04:00
http.server.dispatchers
furnace
furnace.actions
2008-06-17 01:10:46 -04:00
furnace.redirection
furnace.boilerplate
furnace.auth.login
2008-06-02 16:00:03 -04:00
furnace.auth
2008-06-05 02:56:06 -04:00
furnace.syndication ;
2008-04-20 05:19:06 -04:00
IN: webapps.planet
2008-07-08 15:26:37 -04:00
TUPLE: planet < dispatcher ;
2008-06-02 16:00:03 -04:00
2008-07-08 15:26:37 -04:00
SYMBOL: can-administer-planet?
2008-07-08 15:26:37 -04:00
can-administer-planet? define-capability
2008-07-08 15:26:37 -04:00
TUPLE: planet-admin < dispatcher ;
2008-06-02 16:00:03 -04:00
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" URL +not-null+ }
{ "feed-url" "FEEDURL" URL +not-null+ }
2008-04-20 05:19:06 -04:00
} define-persistent
TUPLE: posting < entry id ;
2008-05-26 01:47:27 -04:00
posting "POSTINGS"
{
{ "id" "ID" INTEGER +db-assigned-id+ }
{ "title" "TITLE" { VARCHAR 256 } +not-null+ }
{ "url" "LINK" URL +not-null+ }
2008-05-26 01:47:27 -04:00
{ "description" "DESCRIPTION" TEXT +not-null+ }
{ "date" "DATE" TIMESTAMP +not-null+ }
2008-05-26 01:47:27 -04:00
} define-persistent
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>> ] sort-with ;
2008-05-26 01:47:27 -04:00
: postings ( -- seq )
posting new select-tuples
[ date>> ] inv-sort-with ;
2008-05-26 01:47:27 -04:00
: <edit-blogroll-action> ( -- action )
<page-action>
[ blogroll "blogroll" set-value ] >>init
2008-07-08 15:26:37 -04:00
{ planet "admin" } >>template ;
2008-05-26 01:47:27 -04:00
: <planet-action> ( -- action )
<page-action>
[
blogroll "blogroll" set-value
postings "postings" set-value
] >>init
2008-07-08 15:26:37 -04:00
{ planet "planet" } >>template ;
2008-05-26 01:47:27 -04:00
: <planet-feed-action> ( -- action )
<feed-action>
[ "Planet Factor" ] >>title
2008-07-08 15:26:37 -04:00
[ URL" $planet" ] >>url
[ postings ] >>entries ;
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 url>> >>url
2008-05-26 01:47:27 -04:00
entry description>> >>description
entry date>> >>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
2008-09-10 23:11:40 -04:00
[ '[ _ <posting> ] map ] 2map concat ;
2008-04-20 05:19:06 -04:00
: sort-entries ( entries -- entries' )
[ date>> ] inv-sort-with ;
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
2008-07-08 15:26:37 -04:00
URL" $planet/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
2008-07-08 15:26:37 -04:00
URL" $planet/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" } to-object ;
2008-05-26 03:54:53 -04:00
2008-05-26 01:47:27 -04:00
: <new-blog-action> ( -- action )
<page-action>
2008-07-08 15:26:37 -04:00
{ planet "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 ]
2008-09-28 18:53:59 -04:00
bi
URL" $planet/admin" <redirect>
2008-05-26 01:47:27 -04:00
] >>submit ;
2008-05-26 01:47:27 -04:00
: <edit-blog-action> ( -- action )
<page-action>
2008-05-26 01:47:27 -04:00
[
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
2008-07-08 15:26:37 -04:00
{ planet "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 ]
2009-05-27 20:37:12 -04:00
[ "id" value >>id update-tuple ] bi
2008-09-30 00:29:51 -04:00
<url>
"$planet/admin" >>path
"id" value "id" set-query-param
<redirect>
2008-05-26 01:47:27 -04:00
] >>submit ;
2008-07-08 15:26:37 -04:00
: <planet-admin> ( -- responder )
planet-admin new-dispatcher
2008-09-27 12:38:20 -04:00
<edit-blogroll-action> "" add-responder
2008-05-26 01:47:27 -04:00
<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
<protected>
"administer Planet Factor" >>description
2008-07-08 15:26:37 -04:00
{ can-administer-planet? } >>capabilities ;
2008-05-01 17:24:50 -04:00
2008-07-08 15:26:37 -04:00
: <planet> ( -- responder )
planet new-dispatcher
2008-09-27 12:38:20 -04:00
<planet-action> "" add-responder
<planet-feed-action> "feed.xml" add-responder
2008-07-08 15:26:37 -04:00
<planet-admin> "admin" add-responder
2008-04-20 05:19:06 -04:00
<boilerplate>
2008-07-08 15:26:37 -04:00
{ planet "planet-common" } >>template ;
2008-04-22 22:08:27 -04:00
2008-10-02 13:51:21 -04:00
: start-update-task ( db -- )
'[ _ [ update-cached-postings ] with-db ] 10 minutes every drop ;