factor/extra/webapps/site-watcher/site-watcher.factor

134 lines
3.8 KiB
Factor
Raw Normal View History

2009-03-15 21:13:17 -04:00
! Copyright (C) 2009 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors assocs db.sqlite furnace furnace.actions furnace.alloy
2009-03-18 20:36:04 -04:00
furnace.auth furnace.auth.features.deactivate-user
furnace.auth.features.edit-profile
furnace.auth.features.recover-password
furnace.auth.features.registration furnace.auth.login
furnace.boilerplate furnace.redirection html.forms http.server
http.server.dispatchers kernel namespaces site-watcher site-watcher.db
site-watcher.private urls validators io.sockets.secure.unix.debug
io.servers.connection db db.tuples sequences ;
QUALIFIED: assocs
2009-03-15 21:13:17 -04:00
IN: webapps.site-watcher
TUPLE: site-watcher-app < dispatcher ;
CONSTANT: site-list-url URL" $site-watcher-app/"
2009-03-18 20:36:04 -04:00
: <main-action> ( -- action )
<page-action>
[
logged-in?
[ URL" $site-watcher-app/list" <redirect> ]
[ { site-watcher-app "main" } <chloe-content> ] if
] >>display ;
2009-03-15 21:13:17 -04:00
: <site-list-action> ( -- action )
<page-action>
{ site-watcher-app "site-list" } >>template
[
2009-03-18 20:36:04 -04:00
! Silly query
username watching-sites
"sites" set-value
] >>init
<protected>
"list watched sites" >>description ;
2009-03-15 21:13:17 -04:00
: <add-site-action> ( -- action )
<action>
[
2009-03-18 20:36:04 -04:00
{ { "url" [ v-url ] } } validate-params
2009-03-15 21:13:17 -04:00
] >>validate
[
2009-03-18 20:36:04 -04:00
username "url" value watch-site
2009-03-15 21:13:17 -04:00
site-list-url <redirect>
2009-03-18 20:36:04 -04:00
] >>submit
<protected>
"add a watched site" >>description ;
2009-03-15 21:13:17 -04:00
: <remove-site-action> ( -- action )
<action>
[
{ { "url" [ v-url ] } } validate-params
] >>validate
[
2009-03-18 20:36:04 -04:00
username "url" value unwatch-site
2009-03-15 21:13:17 -04:00
site-list-url <redirect>
2009-03-18 20:36:04 -04:00
] >>submit
<protected>
"remove a watched site" >>description ;
2009-03-15 21:13:17 -04:00
: <check-sites-action> ( -- action )
<action>
[
2009-03-18 20:36:04 -04:00
watch-sites
site-list-url <redirect>
] >>submit
<protected>
"check watched sites" >>description ;
: <update-notify-action> ( -- action )
<page-action>
[
username <account> select-tuple from-object
] >>init
{ site-watcher-app "update-notify" } >>template
[
{
{ "email" [ [ v-email ] v-optional ] }
{ "twitter" [ [ v-one-word ] v-optional ] }
{ "sms" [ [ v-one-line ] v-optional ] }
} validate-params
] >>validate
[
username <account> select-tuple
"email" value >>email
update-tuple
2009-03-15 21:13:17 -04:00
site-list-url <redirect>
2009-03-18 20:36:04 -04:00
] >>submit
<protected>
"update notification details" >>description ;
2009-03-15 21:13:17 -04:00
: <site-watcher-app> ( -- dispatcher )
site-watcher-app new-dispatcher
2009-03-18 20:36:04 -04:00
<main-action> "" add-responder
<site-list-action> "list" add-responder
2009-03-15 21:13:17 -04:00
<add-site-action> "add" add-responder
<remove-site-action> "remove" add-responder
2009-03-18 20:36:04 -04:00
<check-sites-action> "check" add-responder
<update-notify-action> "update-notify" add-responder ;
: <login-config> ( responder -- responder' )
"SiteWatcher" <login-realm>
"SiteWatcher" >>name
allow-registration
allow-password-recovery
allow-edit-profile
allow-deactivation ;
: <site-watcher-server> ( -- threaded-server )
<http-server>
<test-secure-config> >>secure-config
8081 >>insecure
8431 >>secure ;
: site-watcher-db ( -- db )
"resource:test.db" <sqlite-db> ;
<site-watcher-app>
<login-config>
<boilerplate> { site-watcher-app "site-watcher" } >>template
site-watcher-db <alloy>
main-responder set-global
: init-db ( -- )
site-watcher-db [
{ site account watching-site } [ ensure-table ] each
] with-db ;
: start-site-watcher ( -- )
init-db
site-watcher-db run-site-watcher
<site-watcher-server> start-server ;