<commit_before>{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}

module Types
    ( ReviewPost (..)
    , ThumbState (..)
    , Settings   (..)
    ) where

import Data.Semigroup
import Data.Text
import GHC.Generics

#ifdef ghcjs_HOST_OS
import JavaScript.JSON.Types.Internal ()
import JavaScript.JSON.Types.Instances
import JavaScript.JSON.Types.Generic ()
#else
import Data.Aeson
#endif

-- | Left-biased choice on foldables
orElse :: Foldable t => t a -> t a -> t a
x `orElse` y = if Prelude.null x
                 then y
                 else x

orElseText :: Text -> Text -> Text
x `orElseText` y = if Data.Text.null x
                     then y
                     else x

data ThumbState = None | ThumbUp | ThumbDown deriving Generic
instance ToJSON ThumbState
instance FromJSON ThumbState

data ReviewPost = ReviewPost {
    thumb             :: !ThumbState
  , reviewPostComment :: !Text
  } deriving Generic
instance ToJSON   ReviewPost
instance FromJSON ReviewPost

data Settings = Settings {
    loggingUrl              :: Maybe Text -- ^ WebSocket URL to send user analytics to
  , luciUrl                 :: Maybe Text -- ^ WebSocket URL to connect to Luci
  , getSubmissionGeoJsonUrl :: Maybe Text -- ^ URL to GET geoJSON for current submission
  , postSubmissionUrl       :: Maybe Text -- ^ URL for students to POST their new submission to
  , viewMode                :: Text       -- TODO: use proper type from qua-server
  , viewUrl                 :: Text       -- ^ URL of current qua-viewer page
  } deriving Generic
instance ToJSON    Settings
instance FromJSON  Settings
instance Semigroup Settings where
  (<>) s1 s2 = Settings {
                 loggingUrl              = orElse (loggingUrl s1) (loggingUrl s2)
               , luciUrl                 = orElse (luciUrl s1) (luciUrl s2)
               , getSubmissionGeoJsonUrl = orElse (getSubmissionGeoJsonUrl s1) (getSubmissionGeoJsonUrl s2)
               , postSubmissionUrl       = orElse (postSubmissionUrl s1) (postSubmissionUrl s2)
               , viewMode                = orElseText (viewMode s1) (viewMode s2)
               , viewUrl                 = orElseText (viewUrl s1) (viewUrl s2)
               }
instance Monoid Settings where
  mappend = (<>)
  mempty = Settings {
             loggingUrl              = Nothing
           , luciUrl                 = Nothing
           , getSubmissionGeoJsonUrl = Nothing
           , postSubmissionUrl       = Nothing
           , viewMode                = ""
           , viewUrl                 = ""
           }<commit_msg>Types: Text -> Text/JSSTring<commit_after>{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}

module Types
    ( ReviewPost (..)
    , ThumbState (..)
    , ReviewSettings (..)
    , Settings   (..)
    ) where

import Data.Semigroup
import GHC.Generics

#ifdef ghcjs_HOST_OS
import Data.JSString
import JavaScript.JSON.Types.Internal ()
import JavaScript.JSON.Types.Instances
import JavaScript.JSON.Types.Generic ()

type QuaText = JSString
emptyQuaText = Data.JSString.null

#else

import Data.Aeson
import Data.Text

type QuaText = Text
emptyQuaText = Data.Text.null
#endif

-- | Left-biased choice on foldables
orElse :: Foldable t => t a -> t a -> t a
x `orElse` y = if Prelude.null x
                 then y
                 else x

orElseText :: QuaText -> QuaText -> QuaText
x `orElseText` y = if emptyQuaText x
                     then y
                     else x

data ThumbState = None | ThumbUp | ThumbDown deriving Generic
instance ToJSON ThumbState
instance FromJSON ThumbState

data ReviewPost = ReviewPost {
    thumb             :: !ThumbState
  , reviewPostComment :: !QuaText
  } deriving Generic
instance ToJSON   ReviewPost
instance FromJSON ReviewPost

data Settings = Settings {
    loggingUrl              :: Maybe QuaText -- ^ WebSocket URL to send user analytics to
  , luciUrl                 :: Maybe QuaText -- ^ WebSocket URL to connect to Luci
  , getSubmissionGeoJsonUrl :: Maybe QuaText -- ^ URL to GET geoJSON for current submission
  , postSubmissionUrl       :: Maybe QuaText -- ^ URL for students to POST their new submission to
  , reviewSettingsUrl       :: Maybe QuaText
  , viewMode                :: QuaText       -- TODO: use proper type
  , viewUrl                 :: QuaText       -- ^ URL of current qua-viewer page
  } deriving Generic
instance ToJSON    Settings
instance FromJSON  Settings
instance Semigroup Settings where
  (<>) s1 s2 = Settings {
                 loggingUrl              = orElse (loggingUrl s1) (loggingUrl s2)
               , luciUrl                 = orElse (luciUrl s1) (luciUrl s2)
               , getSubmissionGeoJsonUrl = orElse (getSubmissionGeoJsonUrl s1) (getSubmissionGeoJsonUrl s2)
               , postSubmissionUrl       = orElse (postSubmissionUrl s1) (postSubmissionUrl s2)
               , reviewSettingsUrl       = orElse (reviewSettingsUrl s1) (reviewSettingsUrl s2)
               , viewMode                = orElseText (viewMode s1) (viewMode s2)
               , viewUrl                 = orElseText (viewUrl s1) (viewUrl s2)
               }
instance Monoid Settings where
  mappend = (<>)
  mempty = Settings {
             loggingUrl              = Nothing
           , luciUrl                 = Nothing
           , getSubmissionGeoJsonUrl = Nothing
           , postSubmissionUrl       = Nothing
           , reviewSettingsUrl       = Nothing
           , viewMode                = ""
           , viewUrl                 = ""
           }

data ReviewSettings = ReviewSettings {
    postReviewUrl :: QuaText
  } deriving Generic
instance ToJSON    ReviewSettings
instance FromJSON  ReviewSettings
{'fraction_non_alphanumeric': 0.075836820083682, 'fraction_numerical': 0.005230125523012552, 'mean_word_length': 1.9511316872427984, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2110796', 'n_tokens_mistral': 1629, 'n_tokens_neox': 1573, 'n_words': 483}