2021-06-25 18:18:24 +01:00
|
|
|
{-# LANGUAGE ConstraintKinds #-}
|
2021-07-12 19:00:03 +01:00
|
|
|
{-# LANGUAGE DuplicateRecordFields #-}
|
2021-06-25 18:18:24 +01:00
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
|
|
|
|
{-# LANGUAGE LambdaCase #-}
|
2021-07-12 19:00:03 +01:00
|
|
|
{-# LANGUAGE NamedFieldPuns #-}
|
2021-06-25 18:18:24 +01:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
|
2021-07-05 20:05:07 +01:00
|
|
|
module Simplex.Chat.View
|
2021-06-25 18:18:24 +01:00
|
|
|
( printToView,
|
|
|
|
|
showInvitation,
|
2021-07-04 18:42:24 +01:00
|
|
|
showChatError,
|
2021-06-25 18:18:24 +01:00
|
|
|
showContactDeleted,
|
|
|
|
|
showContactConnected,
|
|
|
|
|
showContactDisconnected,
|
|
|
|
|
showReceivedMessage,
|
|
|
|
|
showSentMessage,
|
2021-07-12 19:00:03 +01:00
|
|
|
showGroupCreated,
|
2021-07-16 07:40:55 +01:00
|
|
|
showSentGroupInvitation,
|
|
|
|
|
showReceivedGroupInvitation,
|
|
|
|
|
showConnectedGroupMember,
|
|
|
|
|
showUserConnectedToGroup,
|
2021-06-26 20:20:33 +01:00
|
|
|
safeDecodeUtf8,
|
2021-06-25 18:18:24 +01:00
|
|
|
)
|
|
|
|
|
where
|
|
|
|
|
|
|
|
|
|
import Control.Monad.IO.Unlift
|
|
|
|
|
import Control.Monad.Reader
|
|
|
|
|
import Data.ByteString.Char8 (ByteString)
|
2021-07-16 07:40:55 +01:00
|
|
|
import Data.Composition ((.:))
|
2021-07-04 18:42:24 +01:00
|
|
|
import Data.Text (Text)
|
2021-06-25 18:18:24 +01:00
|
|
|
import qualified Data.Text as T
|
|
|
|
|
import Data.Time.Clock (DiffTime, UTCTime)
|
|
|
|
|
import Data.Time.Format (defaultTimeLocale, formatTime)
|
|
|
|
|
import Data.Time.LocalTime (TimeZone, ZonedTime, getCurrentTimeZone, getZonedTime, localDay, localTimeOfDay, timeOfDayToTime, utcToLocalTime, zonedTimeToLocalTime)
|
|
|
|
|
import Simplex.Chat.Controller
|
|
|
|
|
import Simplex.Chat.Markdown
|
2021-07-12 19:00:03 +01:00
|
|
|
import Simplex.Chat.Store (StoreError (..))
|
2021-06-25 18:18:24 +01:00
|
|
|
import Simplex.Chat.Styled
|
2021-07-05 20:05:07 +01:00
|
|
|
import Simplex.Chat.Terminal (printToTerminal)
|
2021-07-04 18:42:24 +01:00
|
|
|
import Simplex.Chat.Types
|
2021-07-05 20:05:07 +01:00
|
|
|
import Simplex.Chat.Util (safeDecodeUtf8)
|
2021-06-25 18:18:24 +01:00
|
|
|
import Simplex.Messaging.Agent.Protocol
|
|
|
|
|
import System.Console.ANSI.Types
|
|
|
|
|
|
|
|
|
|
type ChatReader m = (MonadUnliftIO m, MonadReader ChatController m)
|
|
|
|
|
|
2021-07-05 19:54:44 +01:00
|
|
|
showInvitation :: ChatReader m => SMPQueueInfo -> m ()
|
|
|
|
|
showInvitation = printToView . invitation
|
2021-06-25 18:18:24 +01:00
|
|
|
|
2021-07-04 18:42:24 +01:00
|
|
|
showChatError :: ChatReader m => ChatError -> m ()
|
|
|
|
|
showChatError = printToView . chatError
|
2021-06-25 18:18:24 +01:00
|
|
|
|
2021-07-14 20:11:41 +01:00
|
|
|
showContactDeleted :: ChatReader m => ContactName -> m ()
|
2021-06-25 18:18:24 +01:00
|
|
|
showContactDeleted = printToView . contactDeleted
|
|
|
|
|
|
2021-07-16 07:40:55 +01:00
|
|
|
showContactConnected :: ChatReader m => Contact -> m ()
|
2021-06-25 18:18:24 +01:00
|
|
|
showContactConnected = printToView . contactConnected
|
|
|
|
|
|
2021-07-14 20:11:41 +01:00
|
|
|
showContactDisconnected :: ChatReader m => ContactName -> m ()
|
2021-06-25 18:18:24 +01:00
|
|
|
showContactDisconnected = printToView . contactDisconnected
|
|
|
|
|
|
2021-07-14 20:11:41 +01:00
|
|
|
showReceivedMessage :: ChatReader m => ContactName -> UTCTime -> Text -> MsgIntegrity -> m ()
|
2021-06-25 18:18:24 +01:00
|
|
|
showReceivedMessage c utcTime msg mOk = printToView =<< liftIO (receivedMessage c utcTime msg mOk)
|
|
|
|
|
|
2021-07-14 20:11:41 +01:00
|
|
|
showSentMessage :: ChatReader m => ContactName -> ByteString -> m ()
|
2021-06-25 18:18:24 +01:00
|
|
|
showSentMessage c msg = printToView =<< liftIO (sentMessage c msg)
|
|
|
|
|
|
2021-07-16 07:40:55 +01:00
|
|
|
showGroupCreated :: ChatReader m => Group -> m ()
|
2021-07-12 19:00:03 +01:00
|
|
|
showGroupCreated = printToView . groupCreated
|
|
|
|
|
|
2021-07-16 07:40:55 +01:00
|
|
|
showSentGroupInvitation :: ChatReader m => Group -> ContactName -> m ()
|
|
|
|
|
showSentGroupInvitation = printToView .: sentGroupInvitation
|
|
|
|
|
|
|
|
|
|
showReceivedGroupInvitation :: ChatReader m => Group -> ContactName -> m ()
|
|
|
|
|
showReceivedGroupInvitation = printToView .: receivedGroupInvitation
|
|
|
|
|
|
|
|
|
|
showConnectedGroupMember :: ChatReader m => GroupName -> ContactName -> m ()
|
|
|
|
|
showConnectedGroupMember = printToView .: connectedGroupMember
|
|
|
|
|
|
|
|
|
|
showUserConnectedToGroup :: ChatReader m => GroupName -> m ()
|
|
|
|
|
showUserConnectedToGroup = printToView . userConnectedToGroup
|
|
|
|
|
|
2021-07-05 19:54:44 +01:00
|
|
|
invitation :: SMPQueueInfo -> [StyledString]
|
|
|
|
|
invitation qInfo =
|
|
|
|
|
[ "pass this invitation to your contact (via another channel): ",
|
2021-06-25 18:18:24 +01:00
|
|
|
"",
|
2021-07-12 19:00:03 +01:00
|
|
|
(plain . serializeSmpQueueInfo) qInfo,
|
2021-06-25 18:18:24 +01:00
|
|
|
"",
|
2021-07-16 07:40:55 +01:00
|
|
|
"and ask them to connect: " <> highlight' "/c <invitation_above>"
|
2021-06-25 18:18:24 +01:00
|
|
|
]
|
|
|
|
|
|
2021-07-14 20:11:41 +01:00
|
|
|
contactDeleted :: ContactName -> [StyledString]
|
2021-06-25 18:18:24 +01:00
|
|
|
contactDeleted c = [ttyContact c <> " is deleted"]
|
|
|
|
|
|
2021-07-16 07:40:55 +01:00
|
|
|
contactConnected :: Contact -> [StyledString]
|
|
|
|
|
contactConnected ct = [ttyFullContact ct <> " is connected"]
|
2021-06-25 18:18:24 +01:00
|
|
|
|
2021-07-14 20:11:41 +01:00
|
|
|
contactDisconnected :: ContactName -> [StyledString]
|
2021-06-25 18:18:24 +01:00
|
|
|
contactDisconnected c = ["disconnected from " <> ttyContact c <> " - restart chat"]
|
|
|
|
|
|
2021-07-16 07:40:55 +01:00
|
|
|
groupCreated :: Group -> [StyledString]
|
|
|
|
|
groupCreated g@Group {localDisplayName} =
|
|
|
|
|
[ "group " <> ttyFullGroup g <> " is created",
|
|
|
|
|
"use " <> highlight ("/a #" <> localDisplayName <> " <name>") <> " to add members"
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
sentGroupInvitation :: Group -> ContactName -> [StyledString]
|
|
|
|
|
sentGroupInvitation g c = ["invitation to join the group " <> ttyFullGroup g <> " sent to " <> ttyContact c]
|
|
|
|
|
|
|
|
|
|
receivedGroupInvitation :: Group -> ContactName -> [StyledString]
|
|
|
|
|
receivedGroupInvitation g@Group {localDisplayName} c =
|
|
|
|
|
[ ttyContact c <> " invites you to join the group " <> ttyFullGroup g,
|
|
|
|
|
"use " <> highlight ("/j #" <> localDisplayName) <> " to accept"
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
connectedGroupMember :: GroupName -> ContactName -> [StyledString]
|
|
|
|
|
connectedGroupMember g c = [ttyContact c <> " joined the group " <> ttyGroup g]
|
|
|
|
|
|
|
|
|
|
userConnectedToGroup :: GroupName -> [StyledString]
|
|
|
|
|
userConnectedToGroup g = ["you joined the group " <> ttyGroup g]
|
2021-07-12 19:00:03 +01:00
|
|
|
|
2021-07-14 20:11:41 +01:00
|
|
|
receivedMessage :: ContactName -> UTCTime -> Text -> MsgIntegrity -> IO [StyledString]
|
2021-06-25 18:18:24 +01:00
|
|
|
receivedMessage c utcTime msg mOk = do
|
|
|
|
|
t <- formatUTCTime <$> getCurrentTimeZone <*> getZonedTime
|
|
|
|
|
pure $ prependFirst (t <> " " <> ttyFromContact c) (msgPlain msg) ++ showIntegrity mOk
|
|
|
|
|
where
|
|
|
|
|
formatUTCTime :: TimeZone -> ZonedTime -> StyledString
|
|
|
|
|
formatUTCTime localTz currentTime =
|
|
|
|
|
let localTime = utcToLocalTime localTz utcTime
|
|
|
|
|
format =
|
|
|
|
|
if (localDay localTime < localDay (zonedTimeToLocalTime currentTime))
|
|
|
|
|
&& (timeOfDayToTime (localTimeOfDay localTime) > (6 * 60 * 60 :: DiffTime))
|
|
|
|
|
then "%m-%d" -- if message is from yesterday or before and 6 hours has passed since midnight
|
|
|
|
|
else "%H:%M"
|
|
|
|
|
in styleTime $ formatTime defaultTimeLocale format localTime
|
|
|
|
|
showIntegrity :: MsgIntegrity -> [StyledString]
|
|
|
|
|
showIntegrity MsgOk = []
|
|
|
|
|
showIntegrity (MsgError err) = msgError $ case err of
|
|
|
|
|
MsgSkipped fromId toId ->
|
|
|
|
|
"skipped message ID " <> show fromId
|
|
|
|
|
<> if fromId == toId then "" else ".." <> show toId
|
|
|
|
|
MsgBadId msgId -> "unexpected message ID " <> show msgId
|
|
|
|
|
MsgBadHash -> "incorrect message hash"
|
|
|
|
|
MsgDuplicate -> "duplicate message ID"
|
|
|
|
|
msgError :: String -> [StyledString]
|
|
|
|
|
msgError s = [styled (Colored Red) s]
|
|
|
|
|
|
2021-07-14 20:11:41 +01:00
|
|
|
sentMessage :: ContactName -> ByteString -> IO [StyledString]
|
2021-06-25 18:18:24 +01:00
|
|
|
sentMessage c msg = do
|
|
|
|
|
time <- formatTime defaultTimeLocale "%H:%M" <$> getZonedTime
|
2021-07-04 18:42:24 +01:00
|
|
|
pure $ prependFirst (styleTime time <> " " <> ttyToContact c) (msgPlain $ safeDecodeUtf8 msg)
|
2021-06-25 18:18:24 +01:00
|
|
|
|
|
|
|
|
prependFirst :: StyledString -> [StyledString] -> [StyledString]
|
|
|
|
|
prependFirst s [] = [s]
|
|
|
|
|
prependFirst s (s' : ss) = (s <> s') : ss
|
|
|
|
|
|
2021-07-04 18:42:24 +01:00
|
|
|
msgPlain :: Text -> [StyledString]
|
|
|
|
|
msgPlain = map styleMarkdownText . T.lines
|
|
|
|
|
|
|
|
|
|
chatError :: ChatError -> [StyledString]
|
|
|
|
|
chatError = \case
|
2021-07-16 07:40:55 +01:00
|
|
|
ChatError err -> case err of
|
|
|
|
|
CEGroupDuplicateMember c -> ["contact " <> ttyContact c <> " is already in the group"]
|
|
|
|
|
CEGroupDuplicateMemberId -> ["cannot add member - duplicate member ID"]
|
|
|
|
|
CEGroupRole -> ["insufficient role for this group command"]
|
|
|
|
|
CEGroupNotJoined g -> ["you did not join this group, use " <> highlight ("/join #" <> g)]
|
|
|
|
|
CEGroupMemberNotReady -> ["you cannot invite other members yet, try later"]
|
|
|
|
|
CEGroupInternal s -> ["chat group bug: " <> plain s]
|
|
|
|
|
-- e -> ["chat error: " <> plain (show e)]
|
2021-07-12 19:00:03 +01:00
|
|
|
ChatErrorStore err -> case err of
|
2021-07-14 20:11:41 +01:00
|
|
|
SEDuplicateName -> ["this display name is already used by user, contact or group"]
|
2021-07-12 19:00:03 +01:00
|
|
|
SEContactNotFound c -> ["no contact " <> ttyContact c]
|
|
|
|
|
SEContactNotReady c -> ["contact " <> ttyContact c <> " is not active yet"]
|
2021-07-16 07:40:55 +01:00
|
|
|
SEGroupNotFound g -> ["no group " <> ttyGroup g]
|
|
|
|
|
SEGroupAlreadyJoined -> ["you already joined this group"]
|
2021-07-12 19:00:03 +01:00
|
|
|
e -> ["chat db error: " <> plain (show e)]
|
2021-07-05 19:54:44 +01:00
|
|
|
ChatErrorAgent err -> case err of
|
|
|
|
|
-- CONN e -> case e of
|
|
|
|
|
-- -- TODO replace with ChatErrorContact errors, these errors should never happen
|
|
|
|
|
-- NOT_FOUND -> ["no contact " <> ttyContact c]
|
|
|
|
|
-- DUPLICATE -> ["contact " <> ttyContact c <> " already exists"]
|
|
|
|
|
-- SIMPLEX -> ["contact " <> ttyContact c <> " did not accept invitation yet"]
|
2021-07-04 18:42:24 +01:00
|
|
|
e -> ["smp agent error: " <> plain (show e)]
|
2021-07-16 07:40:55 +01:00
|
|
|
ChatErrorMessage e -> ["chat message error: " <> plain (show e)]
|
2021-06-25 18:18:24 +01:00
|
|
|
|
|
|
|
|
printToView :: (MonadUnliftIO m, MonadReader ChatController m) => [StyledString] -> m ()
|
|
|
|
|
printToView s = asks chatTerminal >>= liftIO . (`printToTerminal` s)
|
|
|
|
|
|
2021-07-14 20:11:41 +01:00
|
|
|
ttyContact :: ContactName -> StyledString
|
2021-07-04 18:42:24 +01:00
|
|
|
ttyContact = styled (Colored Green)
|
2021-06-25 18:18:24 +01:00
|
|
|
|
2021-07-16 07:40:55 +01:00
|
|
|
ttyFullContact :: Contact -> StyledString
|
|
|
|
|
ttyFullContact Contact {localDisplayName, profile = Profile {fullName}} =
|
|
|
|
|
ttyContact localDisplayName <> optFullName localDisplayName fullName
|
|
|
|
|
|
2021-07-14 20:11:41 +01:00
|
|
|
ttyToContact :: ContactName -> StyledString
|
2021-07-07 22:46:38 +01:00
|
|
|
ttyToContact c = styled (Colored Cyan) $ "@" <> c <> " "
|
2021-06-25 18:18:24 +01:00
|
|
|
|
2021-07-14 20:11:41 +01:00
|
|
|
ttyFromContact :: ContactName -> StyledString
|
2021-07-04 18:42:24 +01:00
|
|
|
ttyFromContact c = styled (Colored Yellow) $ c <> "> "
|
2021-06-25 18:18:24 +01:00
|
|
|
|
2021-07-14 20:11:41 +01:00
|
|
|
ttyGroup :: GroupName -> StyledString
|
2021-07-12 19:00:03 +01:00
|
|
|
ttyGroup g = styled (Colored Blue) $ "#" <> g
|
2021-06-25 18:18:24 +01:00
|
|
|
|
2021-07-16 07:40:55 +01:00
|
|
|
ttyFullGroup :: Group -> StyledString
|
|
|
|
|
ttyFullGroup Group {localDisplayName, groupProfile = GroupProfile {fullName}} =
|
|
|
|
|
ttyGroup localDisplayName <> optFullName localDisplayName fullName
|
|
|
|
|
|
|
|
|
|
optFullName :: Text -> Text -> StyledString
|
|
|
|
|
optFullName localDisplayName fullName
|
|
|
|
|
| localDisplayName == fullName = ""
|
|
|
|
|
| otherwise = plain (" (" <> fullName <> ")")
|
|
|
|
|
|
|
|
|
|
highlight :: StyledFormat a => a -> StyledString
|
|
|
|
|
highlight = styled (Colored Cyan)
|
|
|
|
|
|
|
|
|
|
highlight' :: String -> StyledString
|
|
|
|
|
highlight' = highlight
|
|
|
|
|
|
2021-07-04 18:42:24 +01:00
|
|
|
-- ttyFromGroup :: Group -> Contact -> StyledString
|
|
|
|
|
-- ttyFromGroup (Group g) (Contact a) = styled (Colored Yellow) $ "#" <> g <> " " <> a <> "> "
|
2021-06-25 18:18:24 +01:00
|
|
|
|
|
|
|
|
styleTime :: String -> StyledString
|
|
|
|
|
styleTime = Styled [SetColor Foreground Vivid Black]
|