2021-01-23 00:53:36 +03:00
|
|
|
{-# LANGUAGE DeriveGeneric #-}
|
|
|
|
|
|
|
|
module Command.Telemetry.User
|
2021-04-28 18:36:00 +03:00
|
|
|
( UserSignature (..),
|
|
|
|
readOrCreateUserSignatureFile,
|
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
import Command.Telemetry.Common (TelemetryCacheDir)
|
|
|
|
import qualified Data.UUID.V4 as UUID
|
2021-07-03 12:00:01 +03:00
|
|
|
import StrongPath (Abs, Dir, File', Path', relfile)
|
2021-04-28 18:36:00 +03:00
|
|
|
import qualified StrongPath as SP
|
|
|
|
import qualified System.Directory as SD
|
2021-01-23 00:53:36 +03:00
|
|
|
|
|
|
|
-- Random, non-identifyable UUID used to represent user in analytics.
|
2021-04-28 18:36:00 +03:00
|
|
|
newtype UserSignature = UserSignature {_userSignatureValue :: String} deriving (Show)
|
2021-01-23 00:53:36 +03:00
|
|
|
|
2021-07-03 12:00:01 +03:00
|
|
|
readOrCreateUserSignatureFile :: Path' Abs (Dir TelemetryCacheDir) -> IO UserSignature
|
2021-01-23 00:53:36 +03:00
|
|
|
readOrCreateUserSignatureFile telemetryCacheDirPath = do
|
2021-04-28 18:36:00 +03:00
|
|
|
let filePath = getUserSignatureFilePath telemetryCacheDirPath
|
2021-07-03 12:00:01 +03:00
|
|
|
let filePathFP = SP.fromAbsFile filePath
|
2021-04-28 18:36:00 +03:00
|
|
|
fileExists <- SD.doesFileExist filePathFP
|
|
|
|
UserSignature
|
|
|
|
<$> if fileExists
|
|
|
|
then readFile filePathFP
|
|
|
|
else do
|
|
|
|
userSignature <- show <$> UUID.nextRandom
|
|
|
|
writeFile filePathFP userSignature
|
|
|
|
return userSignature
|
2021-01-23 00:53:36 +03:00
|
|
|
|
2021-07-03 12:00:01 +03:00
|
|
|
getUserSignatureFilePath :: Path' Abs (Dir TelemetryCacheDir) -> Path' Abs File'
|
|
|
|
getUserSignatureFilePath telemetryCacheDir = telemetryCacheDir SP.</> [relfile|signature|]
|