<commit_before>{-# LANGUAGE QuasiQuotes, TemplateHaskell, TypeFamilies, MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
module Foundation
    ( Haskellers (..)
    , HaskellersMessage (..)
    , Route (..)
    , resourcesHaskellers
    , Handler
    , Widget
    , maybeAuth
    , requireAuth
    , maybeAuth'
    , maybeAuthId
    , requireAuth'
    , module Yesod
    , module Settings
    , module Model
    , login
    , Profile (..)
    , userR
    , getDebugR
    , prettyTime
    , prettyDay
    , addTeamNews
    , humanReadableTimeDiff
    , userFullName
    ) where

#define debugRunDB debugRunDBInner __FILE__ __LINE__

import Yesod hiding (Route)
import Yesod.Static
import Yesod.Auth
import Yesod.Auth.OpenId
import Yesod.Auth.Facebook
import Yesod.Message
import Data.Char (isSpace)
import qualified Settings
import System.Directory
import qualified Data.ByteString.Lazy as L
import Database.Persist.GenericSql
import Settings (hamletFile, cassiusFile, juliusFile, widgetFile, hostname)
import Model hiding (userFullName)
import qualified Model
import StaticFiles (logo_png, jquery_ui_css, google_gif, yahoo_gif,
                    facebook_gif, background_png, browserid_png,
                    buttons_png, reset_css, hslogo_16_png)
import Yesod.Form.Jquery
import Yesod.Form.Nic
import Data.IORef (IORef)
import qualified Data.Set as Set

import Control.Concurrent.STM
import System.IO.Unsafe
import qualified Data.Map as Map

import Data.Time
import System.Locale
import Text.Jasmine
import Control.Monad (unless)
import Data.Text (Text, pack, unpack)
import qualified Data.Text as T
import Blaze.ByteString.Builder.Char.Utf8 (fromText)
import Data.Monoid (mappend)
import Network.HTTP.Types (encodePath, queryTextToQuery)
import Text.Hamlet (HtmlUrlI18n, ihamletFile)
import qualified Data.Text.Read
import Data.Maybe (fromJust)
import Web.Authenticate.BrowserId (checkAssertion)
import Network.HTTP.Conduit (Manager)
import Data.Conduit (runResourceT)
import Facebook (Credentials (..))

-- | The site argument for your application. This can be a good place to
-- keep settings and values requiring initialization before your application
-- starts running, such as database connections. Every handler will have
-- access to the data present here.
data Haskellers = Haskellers
    { getStatic :: Static -- ^ Settings for static file serving.
    , connPool :: Settings.ConnectionPool -- ^ Database connection pool.
    , homepageProfiles :: IORef ([Profile], Int)
    , publicProfiles :: IORef [Profile]
    , theApproot :: Text
    , httpManager :: Manager
    }

data Profile = Profile
    { profileUserId :: UserId
    , profileName :: Text
    , profileEmail :: Text
    , profileUser :: User
    , profileSkills :: Set.Set SkillId
    , profileUsername :: Maybe Username
    }
  deriving Show

prettyTime :: UTCTime -> String
prettyTime = formatTime defaultTimeLocale "%B %e, %Y %r"

mkMessage "Haskellers" "messages" "en"

-- This is where we define all of the routes in our application. For a full
-- explanation of the syntax, please see:
-- http://docs.yesodweb.com/book/web-routes-quasi/
--
-- This function does three things:
--
-- * Creates the route datatype Route Haskellers. Every valid URL in your
--   application can be represented as a value of this type.
-- * Creates the associated type:
--       type instance Route Haskellers = Route Haskellers
-- * Creates the value resourcesHaskellers which contains information on the
--   resources declared below. This is used in Controller.hs by the call to
--   mkYesodDispatch
--
-- What this function does *not* do is create a YesodSite instance for
-- Haskellers. Creating that instance requires all of the handler functions
-- for our application to be in scope. However, the handler functions
-- usually require access to the Route Haskellers datatype. Therefore, we
-- split these actions into two functions and place them in separate files.
mkYesodData "Haskellers" $(parseRoutesFile "routes")

maybeAuth' :: GHandler s Haskellers (Maybe ((UserId, User), Maybe Username))
maybeAuth' = do
    x <- maybeAuth
    case x of
        Nothing -> return Nothing
        Just (Entity uid u) -> do
            y <- runDB $ getBy $ UniqueUsernameUser uid
            return $ Just ((uid, u), fmap entityVal y)

requireAuth' :: GHandler s Haskellers ((UserId, User), Maybe Username)
requireAuth' = do
    Entity uid u <- requireAuth
    y <- runDB $ getBy $ UniqueUsernameUser uid
    return ((uid, u), fmap entityVal y)

-- Please see the documentation for the Yesod typeclass. There are a number
-- of settings which can be configured by overriding methods here.
instance Yesod Haskellers where
    joinPath _ ar pieces qs' =
        fromText ar
        `mappend` encodePath pieces' (queryTextToQuery qs)
      where
        qs = map (\(x, y) -> (x, if T.null y then Nothing else Just y)) qs'
        pieces'
            | pieces == ["auth", "page", "openid", "complete"] = ["auth", "page", "openid", "complete", ""] -- For Google, it remembers the old OpenIDs
            | otherwise = pieces
    cleanPath _ ["page", "openid", "complete", ""] = Right ["page", "openid", "complete"]
    cleanPath _ s =
        if corrected == s
            then Right s
            else Left corrected
      where
        corrected = filter (not . T.null) s

    approot = ApprootMaster theApproot

    defaultLayout widget = do
        mmsg <- getMessage
        ma <- maybeAuth'
        y <- getYesod<commit_msg>Fix OpenID<commit_after>{-# LANGUAGE QuasiQuotes, TemplateHaskell, TypeFamilies, MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
module Foundation
    ( Haskellers (..)
    , HaskellersMessage (..)
    , Route (..)
    , resourcesHaskellers
    , Handler
    , Widget
    , maybeAuth
    , requireAuth
    , maybeAuth'
    , maybeAuthId
    , requireAuth'
    , module Yesod
    , module Settings
    , module Model
    , login
    , Profile (..)
    , userR
    , getDebugR
    , prettyTime
    , prettyDay
    , addTeamNews
    , humanReadableTimeDiff
    , userFullName
    ) where

#define debugRunDB debugRunDBInner __FILE__ __LINE__

import Yesod hiding (Route)
import Yesod.Static
import Yesod.Auth
import Yesod.Auth.OpenId
import Yesod.Auth.Facebook
import Yesod.Message
import Data.Char (isSpace)
import qualified Settings
import System.Directory
import qualified Data.ByteString.Lazy as L
import Database.Persist.GenericSql
import Settings (hamletFile, cassiusFile, juliusFile, widgetFile, hostname)
import Model hiding (userFullName)
import qualified Model
import StaticFiles (logo_png, jquery_ui_css, google_gif, yahoo_gif,
                    facebook_gif, background_png, browserid_png,
                    buttons_png, reset_css, hslogo_16_png)
import Yesod.Form.Jquery
import Yesod.Form.Nic
import Data.IORef (IORef)
import qualified Data.Set as Set

import Control.Concurrent.STM
import System.IO.Unsafe
import qualified Data.Map as Map

import Data.Time
import System.Locale
import Text.Jasmine
import Control.Monad (unless)
import Data.Text (Text, pack, unpack)
import qualified Data.Text as T
import Blaze.ByteString.Builder.Char.Utf8 (fromText)
import Data.Monoid (mappend)
import Network.HTTP.Types (encodePath, queryTextToQuery)
import Text.Hamlet (HtmlUrlI18n, ihamletFile)
import qualified Data.Text.Read
import Data.Maybe (fromJust)
import Web.Authenticate.BrowserId (checkAssertion)
import Network.HTTP.Conduit (Manager)
import Data.Conduit (runResourceT)
import Facebook (Credentials (..))

-- | The site argument for your application. This can be a good place to
-- keep settings and values requiring initialization before your application
-- starts running, such as database connections. Every handler will have
-- access to the data present here.
data Haskellers = Haskellers
    { getStatic :: Static -- ^ Settings for static file serving.
    , connPool :: Settings.ConnectionPool -- ^ Database connection pool.
    , homepageProfiles :: IORef ([Profile], Int)
    , publicProfiles :: IORef [Profile]
    , theApproot :: Text
    , httpManager :: Manager
    }

data Profile = Profile
    { profileUserId :: UserId
    , profileName :: Text
    , profileEmail :: Text
    , profileUser :: User
    , profileSkills :: Set.Set SkillId
    , profileUsername :: Maybe Username
    }
  deriving Show

prettyTime :: UTCTime -> String
prettyTime = formatTime defaultTimeLocale "%B %e, %Y %r"

mkMessage "Haskellers" "messages" "en"

-- This is where we define all of the routes in our application. For a full
-- explanation of the syntax, please see:
-- http://docs.yesodweb.com/book/web-routes-quasi/
--
-- This function does three things:
--
-- * Creates the route datatype Route Haskellers. Every valid URL in your
--   application can be represented as a value of this type.
-- * Creates the associated type:
--       type instance Route Haskellers = Route Haskellers
-- * Creates the value resourcesHaskellers which contains information on the
--   resources declared below. This is used in Controller.hs by the call to
--   mkYesodDispatch
--
-- What this function does *not* do is create a YesodSite instance for
-- Haskellers. Creating that instance requires all of the handler functions
-- for our application to be in scope. However, the handler functions
-- usually require access to the Route Haskellers datatype. Therefore, we
-- split these actions into two functions and place them in separate files.
mkYesodData "Haskellers" $(parseRoutesFile "routes")

maybeAuth' :: GHandler s Haskellers (Maybe ((UserId, User), Maybe Username))
maybeAuth' = do
    x <- maybeAuth
    case x of
        Nothing -> return Nothing
        Just (Entity uid u) -> do
            y <- runDB $ getBy $ UniqueUsernameUser uid
            return $ Just ((uid, u), fmap entityVal y)

requireAuth' :: GHandler s Haskellers ((UserId, User), Maybe Username)
requireAuth' = do
    Entity uid u <- requireAuth
    y <- runDB $ getBy $ UniqueUsernameUser uid
    return ((uid, u), fmap entityVal y)

-- Please see the documentation for the Yesod typeclass. There are a number
-- of settings which can be configured by overriding methods here.
instance Yesod Haskellers where
    joinPath _ ar pieces qs' =
        fromText ar
        `mappend` encodePath pieces' (queryTextToQuery qs)
      where
        qs = map (\(x, y) -> (x, if T.null y then Nothing else Just y)) qs'
        pieces'
            | pieces == ["auth", "page", "openid", "complete"] = ["auth", "page", "openid", "complete", ""] -- For Google, it remembers the old OpenIDs
            | otherwise = pieces
    cleanPath _ ["auth", "page", "openid", "complete", ""] = Right ["auth", "page", "openid", "complete"]
    cleanPath _ s =
        if corrected == s
            then Right s
            else Left corrected
      where
        corrected = filter (not . T.null) s

    approot = ApprootMaster theApproot

    defaultLayout widget = do
        mmsg <- getMessage
        ma <- maybeAuth'
        y <- getYesod
{'fraction_non_alphanumeric': 0.09050832208726946, 'fraction_numerical': 0.000899685110211426, 'mean_word_length': 3.233054074638233, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2676709', 'n_tokens_mistral': 3213, 'n_tokens_neox': 3144, 'n_words': 1158}