mirror of
https://github.com/simplex-chat/simplex-chat.git
synced 2026-04-04 22:05:55 +00:00
50 lines
1.5 KiB
Haskell
50 lines
1.5 KiB
Haskell
{-# LANGUAGE DuplicateRecordFields #-}
|
|
{-# LANGUAGE NamedFieldPuns #-}
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
{-# LANGUAGE TypeApplications #-}
|
|
|
|
module Simplex.Chat.Bot.KnownContacts where
|
|
|
|
import qualified Data.Attoparsec.ByteString.Char8 as A
|
|
import Data.Int (Int64)
|
|
import Data.Text (Text)
|
|
import qualified Data.Text as T
|
|
import Data.Text.Encoding (encodeUtf8)
|
|
import Options.Applicative
|
|
import Simplex.Chat.Library.Commands (displayNameP)
|
|
import Simplex.Messaging.Parsers (parseAll)
|
|
|
|
data KnownContact = KnownContact
|
|
{ contactId :: Int64,
|
|
localDisplayName :: Text
|
|
}
|
|
deriving (Eq)
|
|
|
|
data KnownGroup = KnownGroup
|
|
{ groupId :: Int64,
|
|
localDisplayName :: Text
|
|
}
|
|
|
|
knownContactNames :: [KnownContact] -> Text
|
|
knownContactNames = T.intercalate ", " . map (("@" <>) . (\KnownContact {localDisplayName = n} -> n))
|
|
|
|
parseKnownContacts :: ReadM [KnownContact]
|
|
parseKnownContacts = eitherReader $ parseAll knownContactsP . encodeUtf8 . T.pack
|
|
|
|
knownContactsP :: A.Parser [KnownContact]
|
|
knownContactsP = contactP `A.sepBy1` A.char ','
|
|
where
|
|
contactP = do
|
|
contactId <- A.decimal <* A.char ':'
|
|
localDisplayName <- displayNameP
|
|
pure KnownContact {contactId, localDisplayName}
|
|
|
|
parseKnownGroup :: ReadM KnownGroup
|
|
parseKnownGroup = eitherReader $ parseAll knownGroupP . encodeUtf8 . T.pack
|
|
|
|
knownGroupP :: A.Parser KnownGroup
|
|
knownGroupP = do
|
|
groupId <- A.decimal <* A.char ':'
|
|
localDisplayName <- displayNameP
|
|
pure KnownGroup {groupId, localDisplayName}
|