Skip to content

Commit

Permalink
Merge branch 'auto-cross-post'
Browse files Browse the repository at this point in the history
  • Loading branch information
koterpillar committed Mar 5, 2017
2 parents 5d12f90 + 7932277 commit f89e8bf
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 21 deletions.
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,11 @@ Each entry has the following format:
To obtain the authorization for cross-posting to a particular account on a
service, run `multiblog authorize <service>`.

To post all unposted articles to every configured service, run `multiblog
crosspost`. An article is considered posted if a link to it or a _newer_ article
is found in the external service account. Note that the `SITE_URL` must be set
correctly so that the link in the cross post is valid.
All new articles are cross-posted to every configured service when running the
server. An article is considered already posted if a link to it or a _newer_
article is found in the external service account. Note that the `SITE_URL` must
be set correctly so that the link in the cross post is valid; articles are not
posted if it is not set.

The following services are supported:

Expand Down Expand Up @@ -179,5 +180,3 @@ An example of the analytics file, `content/analytics.yaml`:
```yaml
google: UA-12345678-9
```

[yaml-metadata]: http://johnmacfarlane.net/pandoc/demo/example9/pandocs-markdown.html#extension-yaml_metadata_block
1 change: 1 addition & 0 deletions multiblog.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Executable multiblog
, http-conduit
, iso639
, lens
, lifted-base
, mtl
, network
, optparse-generic
Expand Down
15 changes: 10 additions & 5 deletions src/App.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,32 @@ type AppPart a = RouteT Sitemap (ServerPartT App) a
loadApp
:: String -- directory to load from
-> T.Text -- site address
-> Bool -- whether the address was explicitly specified
-> IO AppData
loadApp dataDirectory address = do
loadApp dataDirectory address isRealAddress = do
app <- loadFromDirectory dataDirectory
case app of
Left err -> error err
Right appState ->
return
appState
{ appAddress = address
, appRealAddress = isRealAddress
}

siteAddress :: IO T.Text
-- | Application address, and whether it's specified explicitly
siteAddress :: IO (T.Text, Bool)
siteAddress = do
addr <- fmap (fmap T.pack) $ lookupEnv "SITE_URL"
return $ fromMaybe "http://localhost:8000" addr
return $ case addr of
Just realAddr -> (realAddr, True)
Nothing -> ("http://localhost:8000", False)

loadAppDefault :: IO AppData
loadAppDefault = do
address <- siteAddress
(address, isRealAddress) <- siteAddress
directory <- getCurrentDirectory
loadApp directory address
loadApp directory address isRealAddress

initAppCache :: IO AppCache
initAppCache = do
Expand Down
4 changes: 3 additions & 1 deletion src/CrossPost.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{-|
Subcommand to cross-post the latest article to all external services.
Action to cross-post the latest article to all external services.
-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE FlexibleContexts #-}
Expand Down Expand Up @@ -38,7 +38,9 @@ import Types.Services

crossPost :: App ()
crossPost = do
liftIO $ putStrLn $ "Cross-posting new articles..."
runRoute $ crossPostTwitter
liftIO $ putStrLn $ "All new articles cross-posted."

crossPostTwitter
:: (MonadRoute m, URL m ~ Sitemap, MonadIO m, MonadReader AppData m)
Expand Down
9 changes: 3 additions & 6 deletions src/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ import Options.Generic.Default

import App
import Authorize
import CrossPost
import ReloadHup
import Serve

data Args
= Authorize String
| CrossPost
| Serve
| Main
deriving (Generic)

instance ParseRecord Args
Expand All @@ -27,8 +25,7 @@ main =
app <- loadAppDefault
cache <- initAppCache
runApp cache app $ do
args <- liftIO $ getRecordDefault Serve "Multiblog"
args <- liftIO $ getRecordDefault Main "Multiblog"
case args of
Authorize service -> authorize service
CrossPost -> crossPost
Serve -> serve
Main -> crossPostAndServe
2 changes: 2 additions & 0 deletions src/Models.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ instance FromJSON Analytics where
data AppData = AppData
{ appDirectory :: String
, appAddress :: T.Text
, appRealAddress :: Bool
, appArticles :: [Article]
, appMeta :: [Meta]
, appStrings :: M.Map String LanguageString
Expand All @@ -53,6 +54,7 @@ instance Default AppData where
AppData
{ appDirectory = def
, appAddress = T.empty
, appRealAddress = False
, appArticles = def
, appMeta = def
, appStrings = def
Expand Down
15 changes: 13 additions & 2 deletions src/Serve.hs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{-|
Command to serve the blog.
Action to serve the blog.
-}
module Serve where

import Control.Monad.IO.Class
import Control.Concurrent.Lifted
import Control.Exception
import Control.Monad
import Control.Monad.Reader
Expand All @@ -18,11 +18,22 @@ import Network.Socket
import System.Environment

import App
import Models
import CrossPost


crossPostAndServe :: App ()
crossPostAndServe = do
isRealAddress <- asks appRealAddress
when isRealAddress $ do
void $ fork crossPost
serve

-- Serve the site contents, handling SIGHUP
serve :: App ()
serve = do
lport <- liftIO listenPort
liftIO $ putStrLn $ "Serving on port " ++ show lport ++ "."
let conf =
nullConf
{ port = lport
Expand Down
2 changes: 1 addition & 1 deletion testsuite/Integration/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ testAddress = "http://test"
makeRequest :: TestRequest -> IO String
makeRequest req = do
happstackReq <- mkRequest req
app <- loadApp "testsuite/Integration/content" testAddress
app <- loadApp "testsuite/Integration/content" testAddress False
cache <- initAppCache
rsp <- runApp cache app $ simpleHTTP'' site happstackReq
content <- responseContent rsp
Expand Down

0 comments on commit f89e8bf

Please sign in to comment.