{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RecordWildCards #-}
module Matterhorn.Types
  ( ConnectionStatus(..)
  , HelpTopic(..)
  , ProgramOutput(..)
  , MHEvent(..)
  , InternalEvent(..)
  , StartupStateInfo(..)
  , MHError(..)
  , CPUUsagePolicy(..)
  , SemEq(..)
  , handleEventWith
  , getServerBaseUrl
  , serverBaseUrl
  , ConnectionInfo(..)
  , SidebarUpdate(..)
  , PendingChannelChange(..)
  , ViewMessageWindowTab(..)
  , clearChannelUnreadStatus
  , ChannelListSorting(..)
  , ThreadOrientation(..)
  , ChannelListOrientation(..)
  , channelListEntryUserId
  , userIdsFromZipper
  , entryIsDMEntry
  , ciHostname
  , ciPort
  , ciUrlPath
  , ciUsername
  , ciOTPToken
  , ciPassword
  , ciType
  , ciAccessToken
  , ChannelTopicDialogState(..)
  , channelTopicDialogEditor
  , channelTopicDialogFocus

  , resultToWidget

  , Config(..)
  , configUserL
  , configHostL
  , configTeamL
  , configPortL
  , configUrlPathL
  , configPassL
  , configTokenL
  , configTimeFormatL
  , configDateFormatL
  , configThemeL
  , configThemeCustomizationFileL
  , configSmartBacktickL
  , configSmartEditingL
  , configURLOpenCommandL
  , configURLOpenCommandInteractiveL
  , configActivityNotifyCommandL
  , configActivityNotifyVersionL
  , configActivityBellL
  , configTruncateVerbatimBlocksL
  , configChannelListSortingL
  , configShowMessageTimestampsL
  , configShowBackgroundL
  , configShowMessagePreviewL
  , configShowChannelListL
  , configShowExpandedChannelTopicsL
  , configEnableAspellL
  , configAspellDictionaryL
  , configUnsafeUseHTTPL
  , configValidateServerCertificateL
  , configChannelListWidthL
  , configLogMaxBufferSizeL
  , configShowOlderEditsL
  , configShowTypingIndicatorL
  , configSendTypingNotificationsL
  , configAbsPathL
  , configUserKeysL
  , configHyperlinkingModeL
  , configSyntaxDirsL
  , configDirectChannelExpirationDaysL
  , configCpuUsagePolicyL
  , configDefaultAttachmentPathL
  , configChannelListOrientationL
  , configThreadOrientationL
  , configMouseModeL
  , configShowLastOpenThreadL

  , NotificationVersion(..)
  , PasswordSource(..)
  , TokenSource(..)
  , MatchType(..)
  , Mode(..)
  , ChannelSelectPattern(..)
  , PostListContents(..)
  , AuthenticationException(..)
  , BackgroundInfo(..)
  , RequestChan
  , UserFetch(..)
  , writeBChan
  , InternalTheme(..)

  , attrNameToConfig

  , sortTeams
  , matchesTeam
  , mkTeamZipper
  , mkTeamZipperFromIds
  , teamZipperIds
  , mkChannelZipperList
  , ChannelListGroup(..)
  , nonDMChannelListGroupUnread

  , ThreadInterface
  , ChannelMessageInterface

  , threadInterface
  , unsafeThreadInterface
  , maybeThreadInterface
  , threadInterfaceEmpty
  , threadInterfaceDeleteWhere
  , modifyThreadMessages
  , modifyEachThreadMessage

  , trimChannelSigil

  , ChannelSelectState(..)
  , channelSelectMatches
  , channelSelectInput
  , emptyChannelSelectState

  , TeamState(..)
  , tsFocus
  , tsPendingChannelChange
  , tsRecentChannel
  , tsReturnChannel
  , tsTeam
  , tsChannelSelectState
  , tsViewedMessage
  , tsPostListWindow
  , tsUserListWindow
  , tsChannelListWindow
  , tsNotifyPrefs
  , tsChannelTopicDialog
  , tsReactionEmojiListWindow
  , tsThemeListWindow
  , tsChannelListSorting
  , tsThreadInterface
  , tsMessageInterfaceFocus

  , teamMode
  , teamModes
  , getTeamMode

  , MessageInterfaceFocus(..)
  , messageInterfaceFocusNext
  , messageInterfaceFocusPrev

  , channelEditor
  , channelMessageSelect

  , ChatState
  , newState

  , withCurrentChannel
  , withCurrentChannel'
  , withCurrentTeam

  , csTeamZipper
  , csTeams
  , csTeam
  , csChannelListOrientation
  , csResources
  , csLastMouseDownEvent
  , csGlobalEditState
  , csVerbatimTruncateSetting
  , csCurrentChannelId
  , csCurrentTeamId
  , csPostMap
  , csUsers
  , csHiddenChannelGroups
  , csConnectionStatus
  , csWorkerIsBusy
  , csChannel
  , csChannelMessages
  , csChannelMessageInterface
  , maybeChannelMessageInterface
  , csChannels
  , csClientConfig
  , csInputHistory
  , csMe
  , timeZone
  , whenMode
  , pushMode
  , pushMode'
  , popMode
  , replaceMode

  , GlobalEditState(..)
  , emptyGlobalEditState
  , gedYankBuffer

  , PostListWindowState(..)
  , postListSelected
  , postListPosts

  , UserSearchScope(..)
  , ChannelSearchScope(..)

  , ListWindowState(..)
  , listWindowSearchResults
  , listWindowSearchInput
  , listWindowSearchScope
  , listWindowSearching
  , listWindowEnterHandler
  , listWindowNewList
  , listWindowFetchResults
  , listWindowRecordCount

  , getUsers

  , ChatResources(..)
  , crUserPreferences
  , crEventQueue
  , crTheme
  , crStatusUpdateChan
  , crSubprocessLog
  , crWebsocketActionChan
  , crWebsocketThreadId
  , crRequestQueue
  , crFlaggedPosts
  , crConn
  , crConfiguration
  , crSyntaxMap
  , crLogManager
  , crSpellChecker
  , crEmoji
  , getSession
  , getResourceSession

  , specialUserMentions

  , applyTeamOrder
  , refreshTeamZipper

  , UserPreferences(UserPreferences)
  , userPrefShowJoinLeave
  , userPrefFlaggedPostList
  , userPrefGroupChannelPrefs
  , userPrefDirectChannelPrefs
  , userPrefTeammateNameDisplayMode
  , userPrefTeamOrder
  , userPrefFavoriteChannelPrefs
  , dmChannelShowPreference
  , groupChannelShowPreference
  , favoriteChannelPreference

  , defaultUserPreferences
  , setUserPreferences

  , WebsocketAction(..)

  , Cmd(..)
  , commandName
  , CmdArgs(..)

  , MH
  , runMHEvent
  , scheduleUserFetches
  , scheduleUserStatusFetches
  , getScheduledUserFetches
  , getScheduledUserStatusFetches
  , mh
  , generateUUID
  , generateUUID_IO
  , mhSuspendAndResume
  , mhHandleEventLensed
  , mhHandleEventLensed'
  , mhContinueWithoutRedraw
  , St.gets
  , mhError

  , mhLog
  , mhGetIOLogger
  , ioLogWithManager
  , LogContext(..)
  , withLogContext
  , withLogContextChannelId
  , getLogContext
  , LogMessage(..)
  , LogCommand(..)
  , LogCategory(..)

  , LogManager(..)
  , startLoggingToFile
  , stopLoggingToFile
  , requestLogSnapshot
  , requestLogDestination
  , sendLogMessage

  , requestQuit
  , getMessageForPostId
  , getParentMessage
  , getReplyRootMessage
  , withChannel
  , withChannelOrDefault
  , userList
  , resetAutocomplete
  , isMine
  , setUserStatus
  , myUser
  , myUsername
  , myUserId
  , usernameForUserId
  , userByUsername
  , userByNickname
  , channelIdByChannelName
  , channelIdByUsername
  , userById
  , allUserIds
  , addNewUser
  , useNickname
  , useNickname'
  , displayNameForUserId
  , displayNameForUser
  , raiseInternalEvent
  , getNewMessageCutoff
  , getEditedMessageCutoff

  , HighlightSet(..)
  , UserSet
  , ChannelSet
  , getHighlightSet
  , emptyHSet

  , moveLeft
  , moveRight

  , module Matterhorn.Types.Core
  , module Matterhorn.Types.Channels
  , module Matterhorn.Types.EditState
  , module Matterhorn.Types.Messages
  , module Matterhorn.Types.MessageInterface
  , module Matterhorn.Types.TabbedWindow
  , module Matterhorn.Types.Posts
  , module Matterhorn.Types.Users
  )
where

import           Prelude ()
import           Matterhorn.Prelude

import           GHC.Stack ( HasCallStack )

import qualified Brick
import           Brick ( EventM, Next, Widget(..), Size(..), Result )
import           Brick.Focus ( FocusRing )
import           Brick.Themes ( Theme )
import           Brick.Main ( invalidateCache, invalidateCacheEntry )
import           Brick.AttrMap ( AttrMap )
import qualified Brick.BChan as BCH
import           Brick.Forms (Form)
import           Brick.Widgets.Edit ( Editor, editor )
import           Brick.Widgets.List ( List )
import           Control.Concurrent ( ThreadId )
import           Control.Concurrent.Async ( Async )
import qualified Control.Concurrent.STM as STM
import           Control.Exception ( SomeException )
import qualified Control.Monad.Fail as MHF
import qualified Control.Monad.State as St
import qualified Control.Monad.Reader as R
import qualified Data.Set as Set
import qualified Data.Foldable as F
import           Data.Function ( on )
import qualified Data.Kind as K
import           Data.Maybe ( fromJust )
import           Data.Ord ( comparing )
import qualified Data.HashMap.Strict as HM
import           Data.List ( sortBy, elemIndex, partition )
import qualified Data.Sequence as Seq
import qualified Data.Text as T
import           Data.Time.Clock ( getCurrentTime, addUTCTime )
import           Data.UUID ( UUID )
import qualified Data.Vector as Vec
import qualified Graphics.Vty as Vty
import           Lens.Micro.Platform ( at, makeLenses, lens, (^?!), (.=)
                                     , (%=), (%~), (.~), _Just, Traversal', to
                                     , SimpleGetter, filtered, traversed, singular
                                     )
import           Network.Connection ( HostNotResolved, HostCannotConnect )
import           Skylighting.Types ( SyntaxMap )
import           System.Exit ( ExitCode )
import           System.Random ( randomIO )
import           Text.Aspell ( Aspell )

import           Network.Mattermost ( ConnectionData )
import           Network.Mattermost.Exceptions
import           Network.Mattermost.Lenses
import           Network.Mattermost.Types
import           Network.Mattermost.Types.Config
import           Network.Mattermost.WebSocket ( WebsocketEvent, WebsocketActionResponse )

import           Matterhorn.Constants ( normalChannelSigil )
import           Matterhorn.InputHistory
import           Matterhorn.Emoji
import           Matterhorn.Types.Common
import           Matterhorn.Types.Core
import           Matterhorn.Types.Channels
import           Matterhorn.Types.EditState
import           Matterhorn.Types.KeyEvents
import           Matterhorn.Types.Messages
import           Matterhorn.Types.MessageInterface
import           Matterhorn.Types.NonemptyStack
import           Matterhorn.Types.Posts
import           Matterhorn.Types.RichText ( TeamBaseURL(..), TeamURLName(..) )
import           Matterhorn.Types.TabbedWindow
import           Matterhorn.Types.Users
import qualified Matterhorn.Zipper as Z


-- * Configuration

-- | A notification version for the external notifier
data NotificationVersion =
    NotifyV1
    | NotifyV2
    deriving (NotificationVersion -> NotificationVersion -> Bool
(NotificationVersion -> NotificationVersion -> Bool)
-> (NotificationVersion -> NotificationVersion -> Bool)
-> Eq NotificationVersion
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: NotificationVersion -> NotificationVersion -> Bool
$c/= :: NotificationVersion -> NotificationVersion -> Bool
== :: NotificationVersion -> NotificationVersion -> Bool
$c== :: NotificationVersion -> NotificationVersion -> Bool
Eq, ReadPrec [NotificationVersion]
ReadPrec NotificationVersion
Int -> ReadS NotificationVersion
ReadS [NotificationVersion]
(Int -> ReadS NotificationVersion)
-> ReadS [NotificationVersion]
-> ReadPrec NotificationVersion
-> ReadPrec [NotificationVersion]
-> Read NotificationVersion
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [NotificationVersion]
$creadListPrec :: ReadPrec [NotificationVersion]
readPrec :: ReadPrec NotificationVersion
$creadPrec :: ReadPrec NotificationVersion
readList :: ReadS [NotificationVersion]
$creadList :: ReadS [NotificationVersion]
readsPrec :: Int -> ReadS NotificationVersion
$creadsPrec :: Int -> ReadS NotificationVersion
Read, Int -> NotificationVersion -> ShowS
[NotificationVersion] -> ShowS
NotificationVersion -> String
(Int -> NotificationVersion -> ShowS)
-> (NotificationVersion -> String)
-> ([NotificationVersion] -> ShowS)
-> Show NotificationVersion
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [NotificationVersion] -> ShowS
$cshowList :: [NotificationVersion] -> ShowS
show :: NotificationVersion -> String
$cshow :: NotificationVersion -> String
showsPrec :: Int -> NotificationVersion -> ShowS
$cshowsPrec :: Int -> NotificationVersion -> ShowS
Show)

-- | A user password is either given to us directly, or a command
-- which we execute to find the password.
data PasswordSource =
    PasswordString Text
    | PasswordCommand Text
    deriving (PasswordSource -> PasswordSource -> Bool
(PasswordSource -> PasswordSource -> Bool)
-> (PasswordSource -> PasswordSource -> Bool) -> Eq PasswordSource
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PasswordSource -> PasswordSource -> Bool
$c/= :: PasswordSource -> PasswordSource -> Bool
== :: PasswordSource -> PasswordSource -> Bool
$c== :: PasswordSource -> PasswordSource -> Bool
Eq, ReadPrec [PasswordSource]
ReadPrec PasswordSource
Int -> ReadS PasswordSource
ReadS [PasswordSource]
(Int -> ReadS PasswordSource)
-> ReadS [PasswordSource]
-> ReadPrec PasswordSource
-> ReadPrec [PasswordSource]
-> Read PasswordSource
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [PasswordSource]
$creadListPrec :: ReadPrec [PasswordSource]
readPrec :: ReadPrec PasswordSource
$creadPrec :: ReadPrec PasswordSource
readList :: ReadS [PasswordSource]
$creadList :: ReadS [PasswordSource]
readsPrec :: Int -> ReadS PasswordSource
$creadsPrec :: Int -> ReadS PasswordSource
Read, Int -> PasswordSource -> ShowS
[PasswordSource] -> ShowS
PasswordSource -> String
(Int -> PasswordSource -> ShowS)
-> (PasswordSource -> String)
-> ([PasswordSource] -> ShowS)
-> Show PasswordSource
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PasswordSource] -> ShowS
$cshowList :: [PasswordSource] -> ShowS
show :: PasswordSource -> String
$cshow :: PasswordSource -> String
showsPrec :: Int -> PasswordSource -> ShowS
$cshowsPrec :: Int -> PasswordSource -> ShowS
Show)

-- | An access token source.
data TokenSource =
    TokenString Text
    | TokenCommand Text
    deriving (TokenSource -> TokenSource -> Bool
(TokenSource -> TokenSource -> Bool)
-> (TokenSource -> TokenSource -> Bool) -> Eq TokenSource
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TokenSource -> TokenSource -> Bool
$c/= :: TokenSource -> TokenSource -> Bool
== :: TokenSource -> TokenSource -> Bool
$c== :: TokenSource -> TokenSource -> Bool
Eq, ReadPrec [TokenSource]
ReadPrec TokenSource
Int -> ReadS TokenSource
ReadS [TokenSource]
(Int -> ReadS TokenSource)
-> ReadS [TokenSource]
-> ReadPrec TokenSource
-> ReadPrec [TokenSource]
-> Read TokenSource
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [TokenSource]
$creadListPrec :: ReadPrec [TokenSource]
readPrec :: ReadPrec TokenSource
$creadPrec :: ReadPrec TokenSource
readList :: ReadS [TokenSource]
$creadList :: ReadS [TokenSource]
readsPrec :: Int -> ReadS TokenSource
$creadsPrec :: Int -> ReadS TokenSource
Read, Int -> TokenSource -> ShowS
[TokenSource] -> ShowS
TokenSource -> String
(Int -> TokenSource -> ShowS)
-> (TokenSource -> String)
-> ([TokenSource] -> ShowS)
-> Show TokenSource
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TokenSource] -> ShowS
$cshowList :: [TokenSource] -> ShowS
show :: TokenSource -> String
$cshow :: TokenSource -> String
showsPrec :: Int -> TokenSource -> ShowS
$cshowsPrec :: Int -> TokenSource -> ShowS
Show)

-- | The type of channel list group headings. Integer arguments indicate
-- total number of channels in the group that have unread activity.
data ChannelListGroup =
    ChannelListGroup { ChannelListGroup -> ChannelListGroupLabel
channelListGroupLabel :: ChannelListGroupLabel
                     , ChannelListGroup -> Int
channelListGroupUnread :: Int
                     , ChannelListGroup -> Bool
channelListGroupCollapsed :: Bool
                     , ChannelListGroup -> Int
channelListGroupEntries :: Int
                     }
                     deriving (ChannelListGroup -> ChannelListGroup -> Bool
(ChannelListGroup -> ChannelListGroup -> Bool)
-> (ChannelListGroup -> ChannelListGroup -> Bool)
-> Eq ChannelListGroup
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ChannelListGroup -> ChannelListGroup -> Bool
$c/= :: ChannelListGroup -> ChannelListGroup -> Bool
== :: ChannelListGroup -> ChannelListGroup -> Bool
$c== :: ChannelListGroup -> ChannelListGroup -> Bool
Eq, Int -> ChannelListGroup -> ShowS
[ChannelListGroup] -> ShowS
ChannelListGroup -> String
(Int -> ChannelListGroup -> ShowS)
-> (ChannelListGroup -> String)
-> ([ChannelListGroup] -> ShowS)
-> Show ChannelListGroup
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ChannelListGroup] -> ShowS
$cshowList :: [ChannelListGroup] -> ShowS
show :: ChannelListGroup -> String
$cshow :: ChannelListGroup -> String
showsPrec :: Int -> ChannelListGroup -> ShowS
$cshowsPrec :: Int -> ChannelListGroup -> ShowS
Show)

nonDMChannelListGroupUnread :: ChannelListGroup -> Int
nonDMChannelListGroupUnread :: ChannelListGroup -> Int
nonDMChannelListGroupUnread ChannelListGroup
g =
    case ChannelListGroup -> ChannelListGroupLabel
channelListGroupLabel ChannelListGroup
g of
        ChannelListGroupLabel
ChannelGroupDirectMessages -> Int
0
        ChannelListGroupLabel
_ -> ChannelListGroup -> Int
channelListGroupUnread ChannelListGroup
g

data ChannelListSorting =
    ChannelListSortDefault
    | ChannelListSortUnreadFirst
    deriving (ChannelListSorting -> ChannelListSorting -> Bool
(ChannelListSorting -> ChannelListSorting -> Bool)
-> (ChannelListSorting -> ChannelListSorting -> Bool)
-> Eq ChannelListSorting
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ChannelListSorting -> ChannelListSorting -> Bool
$c/= :: ChannelListSorting -> ChannelListSorting -> Bool
== :: ChannelListSorting -> ChannelListSorting -> Bool
$c== :: ChannelListSorting -> ChannelListSorting -> Bool
Eq, Int -> ChannelListSorting -> ShowS
[ChannelListSorting] -> ShowS
ChannelListSorting -> String
(Int -> ChannelListSorting -> ShowS)
-> (ChannelListSorting -> String)
-> ([ChannelListSorting] -> ShowS)
-> Show ChannelListSorting
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ChannelListSorting] -> ShowS
$cshowList :: [ChannelListSorting] -> ShowS
show :: ChannelListSorting -> String
$cshow :: ChannelListSorting -> String
showsPrec :: Int -> ChannelListSorting -> ShowS
$cshowsPrec :: Int -> ChannelListSorting -> ShowS
Show, Eq ChannelListSorting
Eq ChannelListSorting
-> (ChannelListSorting -> ChannelListSorting -> Ordering)
-> (ChannelListSorting -> ChannelListSorting -> Bool)
-> (ChannelListSorting -> ChannelListSorting -> Bool)
-> (ChannelListSorting -> ChannelListSorting -> Bool)
-> (ChannelListSorting -> ChannelListSorting -> Bool)
-> (ChannelListSorting -> ChannelListSorting -> ChannelListSorting)
-> (ChannelListSorting -> ChannelListSorting -> ChannelListSorting)
-> Ord ChannelListSorting
ChannelListSorting -> ChannelListSorting -> Bool
ChannelListSorting -> ChannelListSorting -> Ordering
ChannelListSorting -> ChannelListSorting -> ChannelListSorting
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: ChannelListSorting -> ChannelListSorting -> ChannelListSorting
$cmin :: ChannelListSorting -> ChannelListSorting -> ChannelListSorting
max :: ChannelListSorting -> ChannelListSorting -> ChannelListSorting
$cmax :: ChannelListSorting -> ChannelListSorting -> ChannelListSorting
>= :: ChannelListSorting -> ChannelListSorting -> Bool
$c>= :: ChannelListSorting -> ChannelListSorting -> Bool
> :: ChannelListSorting -> ChannelListSorting -> Bool
$c> :: ChannelListSorting -> ChannelListSorting -> Bool
<= :: ChannelListSorting -> ChannelListSorting -> Bool
$c<= :: ChannelListSorting -> ChannelListSorting -> Bool
< :: ChannelListSorting -> ChannelListSorting -> Bool
$c< :: ChannelListSorting -> ChannelListSorting -> Bool
compare :: ChannelListSorting -> ChannelListSorting -> Ordering
$ccompare :: ChannelListSorting -> ChannelListSorting -> Ordering
$cp1Ord :: Eq ChannelListSorting
Ord)

-- | This is how we represent the user's configuration. Most fields
-- correspond to configuration file settings (see Config.hs) but some
-- are for internal book-keeping purposes only.
data Config =
    Config { Config -> Maybe Text
configUser :: Maybe Text
           -- ^ The username to use when connecting.
           , Config -> Maybe Text
configHost :: Maybe Text
           -- ^ The hostname to use when connecting.
           , Config -> Maybe Text
configTeam :: Maybe Text
           -- ^ The team name to use when connecting.
           , Config -> Int
configPort :: Int
           -- ^ The port to use when connecting.
           , Config -> Maybe Text
configUrlPath :: Maybe Text
           -- ^ The server path to use when connecting.
           , Config -> Maybe PasswordSource
configPass :: Maybe PasswordSource
           -- ^ The password source to use when connecting.
           , Config -> Maybe TokenSource
configToken :: Maybe TokenSource
           -- ^ The token source to use when connecting.
           , Config -> Maybe Text
configTimeFormat :: Maybe Text
           -- ^ The format string for timestamps.
           , Config -> Maybe Text
configDateFormat :: Maybe Text
           -- ^ The format string for dates.
           , Config -> Maybe Text
configTheme :: Maybe Text
           -- ^ The name of the theme to use.
           , Config -> Maybe Text
configThemeCustomizationFile :: Maybe Text
           -- ^ The path to the theme customization file, if any.
           , Config -> Bool
configSmartBacktick :: Bool
           -- ^ Whether to enable smart quoting characters.
           , Config -> Bool
configSmartEditing :: Bool
           -- ^ Whether to enable smart editing behaviors.
           , Config -> Maybe Text
configURLOpenCommand :: Maybe Text
           -- ^ The command to use to open URLs.
           , Config -> Bool
configURLOpenCommandInteractive :: Bool
           -- ^ Whether the URL-opening command is interactive (i.e.
           -- whether it should be given control of the terminal).
           , Config -> Maybe Text
configActivityNotifyCommand :: Maybe T.Text
           -- ^ The command to run for activity notifications.
           , Config -> NotificationVersion
configActivityNotifyVersion :: NotificationVersion
           -- ^ The activity notifier version.
           , Config -> Bool
configActivityBell :: Bool
           -- ^ Whether to ring the terminal bell on activity.
           , Config -> Maybe Int
configTruncateVerbatimBlocks :: Maybe Int
           -- ^ Whether to truncate verbatim (and code) blocks past a
           -- reasonable number of lines.
           , Config -> Bool
configShowMessageTimestamps :: Bool
           -- ^ Whether to show timestamps on messages.
           , Config -> BackgroundInfo
configShowBackground :: BackgroundInfo
           -- ^ Whether to show async background worker thread info.
           , Config -> Bool
configShowMessagePreview :: Bool
           -- ^ Whether to show the message preview area.
           , Config -> Bool
configShowChannelList :: Bool
           -- ^ Whether to show the channel list.
           , Config -> Bool
configShowExpandedChannelTopics :: Bool
           -- ^ Whether to show expanded channel topics.
           , Config -> Bool
configEnableAspell :: Bool
           -- ^ Whether to enable Aspell spell checking.
           , Config -> Maybe Text
configAspellDictionary :: Maybe Text
           -- ^ A specific Aspell dictionary name to use.
           , Config -> Bool
configUnsafeUseHTTP :: Bool
           -- ^ Whether to permit an insecure HTTP connection.
           , Config -> Bool
configValidateServerCertificate :: Bool
           -- ^ Whether to validate TLS certificates.
           , Config -> Int
configChannelListWidth :: Int
           -- ^ The width, in columns, of the channel list sidebar.
           , Config -> Int
configLogMaxBufferSize :: Int
           -- ^ The maximum size, in log entries, of the internal log
           -- message buffer.
           , Config -> Bool
configShowOlderEdits :: Bool
           -- ^ Whether to highlight the edit indicator on edits made
           -- prior to the beginning of the current session.
           , Config -> ChannelListSorting
configChannelListSorting :: ChannelListSorting
           -- ^ How to sort channels in each channel list group
           , Config -> Bool
configShowTypingIndicator :: Bool
           -- ^ Whether to show the typing indicator when other users
           -- are typing
           , Config -> Bool
configSendTypingNotifications :: Bool
           -- Whether to send typing notifications to other users.
           , Config -> Maybe String
configAbsPath :: Maybe FilePath
           -- ^ A book-keeping field for the absolute path to the
           -- configuration. (Not a user setting.)
           , Config -> KeyConfig
configUserKeys :: KeyConfig
           -- ^ The user's keybinding configuration.
           , Config -> Bool
configHyperlinkingMode :: Bool
           -- ^ Whether to enable terminal hyperlinking mode.
           , Config -> [String]
configSyntaxDirs :: [FilePath]
           -- ^ The search path for syntax description XML files.
           , Config -> Int
configDirectChannelExpirationDays :: Int
           -- ^ The number of days to show a user in the channel menu after a direct
           -- message with them.
           , Config -> CPUUsagePolicy
configCpuUsagePolicy :: CPUUsagePolicy
           -- ^ The CPU usage policy for the application.
           , Config -> Maybe String
configDefaultAttachmentPath :: Maybe FilePath
           -- ^ The default path for browsing attachments
           , Config -> ChannelListOrientation
configChannelListOrientation :: ChannelListOrientation
           -- ^ The orientation of the channel list.
           , Config -> ThreadOrientation
configThreadOrientation :: ThreadOrientation
           -- ^ The orientation of the thread window relative to the
           -- main channel message window.
           , Config -> Bool
configMouseMode :: Bool
           -- ^ Whether to enable mouse support in matterhorn
           , Config -> Bool
configShowLastOpenThread :: Bool
           -- ^ Whether to re-open a thread that was open the last time
           -- Matterhorn quit
           } deriving (Config -> Config -> Bool
(Config -> Config -> Bool)
-> (Config -> Config -> Bool) -> Eq Config
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Config -> Config -> Bool
$c/= :: Config -> Config -> Bool
== :: Config -> Config -> Bool
$c== :: Config -> Config -> Bool
Eq, Int -> Config -> ShowS
[Config] -> ShowS
Config -> String
(Int -> Config -> ShowS)
-> (Config -> String) -> ([Config] -> ShowS) -> Show Config
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Config] -> ShowS
$cshowList :: [Config] -> ShowS
show :: Config -> String
$cshow :: Config -> String
showsPrec :: Int -> Config -> ShowS
$cshowsPrec :: Int -> Config -> ShowS
Show)

-- | The policy for CPU usage.
--
-- The idea is that Matterhorn can benefit from using multiple CPUs,
-- but the exact number is application-determined. We expose this policy
-- setting to the user in the configuration.
data CPUUsagePolicy =
    SingleCPU
    -- ^ Constrain the application to use one CPU.
    | MultipleCPUs
    -- ^ Permit the usage of multiple CPUs (the exact number is
    -- determined by the application).
    deriving (CPUUsagePolicy -> CPUUsagePolicy -> Bool
(CPUUsagePolicy -> CPUUsagePolicy -> Bool)
-> (CPUUsagePolicy -> CPUUsagePolicy -> Bool) -> Eq CPUUsagePolicy
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CPUUsagePolicy -> CPUUsagePolicy -> Bool
$c/= :: CPUUsagePolicy -> CPUUsagePolicy -> Bool
== :: CPUUsagePolicy -> CPUUsagePolicy -> Bool
$c== :: CPUUsagePolicy -> CPUUsagePolicy -> Bool
Eq, Int -> CPUUsagePolicy -> ShowS
[CPUUsagePolicy] -> ShowS
CPUUsagePolicy -> String
(Int -> CPUUsagePolicy -> ShowS)
-> (CPUUsagePolicy -> String)
-> ([CPUUsagePolicy] -> ShowS)
-> Show CPUUsagePolicy
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CPUUsagePolicy] -> ShowS
$cshowList :: [CPUUsagePolicy] -> ShowS
show :: CPUUsagePolicy -> String
$cshow :: CPUUsagePolicy -> String
showsPrec :: Int -> CPUUsagePolicy -> ShowS
$cshowsPrec :: Int -> CPUUsagePolicy -> ShowS
Show)

-- | The state of the UI diagnostic indicator for the async worker
-- thread.
data BackgroundInfo =
    Disabled
    -- ^ Disable (do not show) the indicator.
    | Active
    -- ^ Show the indicator when the thread is working.
    | ActiveCount
    -- ^ Show the indicator when the thread is working, but include the
    -- thread's work queue length.
    deriving (BackgroundInfo -> BackgroundInfo -> Bool
(BackgroundInfo -> BackgroundInfo -> Bool)
-> (BackgroundInfo -> BackgroundInfo -> Bool) -> Eq BackgroundInfo
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: BackgroundInfo -> BackgroundInfo -> Bool
$c/= :: BackgroundInfo -> BackgroundInfo -> Bool
== :: BackgroundInfo -> BackgroundInfo -> Bool
$c== :: BackgroundInfo -> BackgroundInfo -> Bool
Eq, Int -> BackgroundInfo -> ShowS
[BackgroundInfo] -> ShowS
BackgroundInfo -> String
(Int -> BackgroundInfo -> ShowS)
-> (BackgroundInfo -> String)
-> ([BackgroundInfo] -> ShowS)
-> Show BackgroundInfo
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [BackgroundInfo] -> ShowS
$cshowList :: [BackgroundInfo] -> ShowS
show :: BackgroundInfo -> String
$cshow :: BackgroundInfo -> String
showsPrec :: Int -> BackgroundInfo -> ShowS
$cshowsPrec :: Int -> BackgroundInfo -> ShowS
Show)

data UserPreferences =
    UserPreferences { UserPreferences -> Bool
_userPrefShowJoinLeave :: Bool
                    , UserPreferences -> Seq FlaggedPost
_userPrefFlaggedPostList :: Seq FlaggedPost
                    , UserPreferences -> HashMap ChannelId Bool
_userPrefGroupChannelPrefs :: HashMap ChannelId Bool
                    , UserPreferences -> HashMap UserId Bool
_userPrefDirectChannelPrefs :: HashMap UserId Bool
                    , UserPreferences -> HashMap ChannelId Bool
_userPrefFavoriteChannelPrefs :: HashMap ChannelId Bool
                    , UserPreferences -> Maybe TeammateNameDisplayMode
_userPrefTeammateNameDisplayMode :: Maybe TeammateNameDisplayMode
                    , UserPreferences -> Maybe [TeamId]
_userPrefTeamOrder :: Maybe [TeamId]
                    }

hasUnread' :: ClientChannel -> Bool
hasUnread' :: ClientChannel -> Bool
hasUnread' ClientChannel
chan = Bool -> Maybe Bool -> Bool
forall a. a -> Maybe a -> a
fromMaybe Bool
False (Maybe Bool -> Bool) -> Maybe Bool -> Bool
forall a b. (a -> b) -> a -> b
$ do
    let info :: ChannelInfo
info = ClientChannel -> ChannelInfo
_ccInfo ClientChannel
chan
    ServerTime
lastViewTime <- ChannelInfo -> Maybe ServerTime
_cdViewed ChannelInfo
info
    Bool -> Maybe Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> Maybe Bool) -> Bool -> Maybe Bool
forall a b. (a -> b) -> a -> b
$ ChannelInfo -> Int
_cdMentionCount ChannelInfo
info Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 Bool -> Bool -> Bool
||
             (Bool -> Bool
not (ClientChannel -> Bool
isMuted ClientChannel
chan) Bool -> Bool -> Bool
&&
              (((ChannelInfo -> ServerTime
_cdUpdated ChannelInfo
info) ServerTime -> ServerTime -> Bool
forall a. Ord a => a -> a -> Bool
> ServerTime
lastViewTime) Bool -> Bool -> Bool
||
               (Maybe ServerTime -> Bool
forall a. Maybe a -> Bool
isJust (Maybe ServerTime -> Bool) -> Maybe ServerTime -> Bool
forall a b. (a -> b) -> a -> b
$ ChannelInfo -> Maybe ServerTime
_cdEditedMessageThreshold ChannelInfo
info)))

mkChannelZipperList :: ChannelListSorting
                    -> UTCTime
                    -> Config
                    -> TeamId
                    -> Maybe ClientConfig
                    -> UserPreferences
                    -> HM.HashMap TeamId (Set ChannelListGroupLabel)
                    -> ClientChannels
                    -> Users
                    -> [(ChannelListGroup, [ChannelListEntry])]
mkChannelZipperList :: ChannelListSorting
-> UTCTime
-> Config
-> TeamId
-> Maybe ClientConfig
-> UserPreferences
-> HashMap TeamId (Set ChannelListGroupLabel)
-> ClientChannels
-> Users
-> [(ChannelListGroup, [ChannelListEntry])]
mkChannelZipperList ChannelListSorting
sorting UTCTime
now Config
config TeamId
tId Maybe ClientConfig
cconfig UserPreferences
prefs HashMap TeamId (Set ChannelListGroupLabel)
hidden ClientChannels
cs Users
us =
    let ([ChannelListEntry]
privFavs, [ChannelListEntry]
privEntries) = [ChannelListEntry] -> ([ChannelListEntry], [ChannelListEntry])
partitionFavorites ([ChannelListEntry] -> ([ChannelListEntry], [ChannelListEntry]))
-> [ChannelListEntry] -> ([ChannelListEntry], [ChannelListEntry])
forall a b. (a -> b) -> a -> b
$ TeamId
-> UserPreferences -> ClientChannels -> Type -> [ChannelListEntry]
getChannelEntriesByType TeamId
tId UserPreferences
prefs ClientChannels
cs Type
Private
        ([ChannelListEntry]
normFavs, [ChannelListEntry]
normEntries) = [ChannelListEntry] -> ([ChannelListEntry], [ChannelListEntry])
partitionFavorites ([ChannelListEntry] -> ([ChannelListEntry], [ChannelListEntry]))
-> [ChannelListEntry] -> ([ChannelListEntry], [ChannelListEntry])
forall a b. (a -> b) -> a -> b
$ TeamId
-> UserPreferences -> ClientChannels -> Type -> [ChannelListEntry]
getChannelEntriesByType TeamId
tId UserPreferences
prefs ClientChannels
cs Type
Ordinary
        ([ChannelListEntry]
dmFavs,   [ChannelListEntry]
dmEntries)   = [ChannelListEntry] -> ([ChannelListEntry], [ChannelListEntry])
partitionFavorites ([ChannelListEntry] -> ([ChannelListEntry], [ChannelListEntry]))
-> [ChannelListEntry] -> ([ChannelListEntry], [ChannelListEntry])
forall a b. (a -> b) -> a -> b
$ UTCTime
-> Config
-> Maybe ClientConfig
-> UserPreferences
-> Users
-> ClientChannels
-> [ChannelListEntry]
getDMChannelEntries UTCTime
now Config
config Maybe ClientConfig
cconfig UserPreferences
prefs Users
us ClientChannels
cs
        favEntries :: [ChannelListEntry]
favEntries              = [ChannelListEntry]
privFavs [ChannelListEntry] -> [ChannelListEntry] -> [ChannelListEntry]
forall a. Semigroup a => a -> a -> a
<> [ChannelListEntry]
normFavs [ChannelListEntry] -> [ChannelListEntry] -> [ChannelListEntry]
forall a. Semigroup a => a -> a -> a
<> [ChannelListEntry]
dmFavs
        isHidden :: ChannelListGroupLabel -> Bool
isHidden ChannelListGroupLabel
label =
            case TeamId
-> HashMap TeamId (Set ChannelListGroupLabel)
-> Maybe (Set ChannelListGroupLabel)
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HM.lookup TeamId
tId HashMap TeamId (Set ChannelListGroupLabel)
hidden of
                Maybe (Set ChannelListGroupLabel)
Nothing -> Bool
False
                Just Set ChannelListGroupLabel
s -> ChannelListGroupLabel -> Set ChannelListGroupLabel -> Bool
forall a. Ord a => a -> Set a -> Bool
Set.member ChannelListGroupLabel
label Set ChannelListGroupLabel
s
    in [ let unread :: Int
unread = [ChannelListEntry] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([ChannelListEntry] -> Int) -> [ChannelListEntry] -> Int
forall a b. (a -> b) -> a -> b
$ (ChannelListEntry -> Bool)
-> [ChannelListEntry] -> [ChannelListEntry]
forall a. (a -> Bool) -> [a] -> [a]
filter ChannelListEntry -> Bool
channelListEntryUnread [ChannelListEntry]
favEntries
             coll :: Bool
coll = ChannelListGroupLabel -> Bool
isHidden ChannelListGroupLabel
ChannelGroupFavoriteChannels
         in ( ChannelListGroupLabel -> Int -> Bool -> Int -> ChannelListGroup
ChannelListGroup ChannelListGroupLabel
ChannelGroupFavoriteChannels Int
unread Bool
coll ([ChannelListEntry] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [ChannelListEntry]
favEntries)
            , if Bool
coll then [ChannelListEntry]
forall a. Monoid a => a
mempty else ChannelListSorting -> [ChannelListEntry] -> [ChannelListEntry]
sortChannelListEntries ChannelListSorting
sorting [ChannelListEntry]
favEntries
            )
       , let unread :: Int
unread = [ChannelListEntry] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([ChannelListEntry] -> Int) -> [ChannelListEntry] -> Int
forall a b. (a -> b) -> a -> b
$ (ChannelListEntry -> Bool)
-> [ChannelListEntry] -> [ChannelListEntry]
forall a. (a -> Bool) -> [a] -> [a]
filter ChannelListEntry -> Bool
channelListEntryUnread [ChannelListEntry]
normEntries
             coll :: Bool
coll = ChannelListGroupLabel -> Bool
isHidden ChannelListGroupLabel
ChannelGroupPublicChannels
         in ( ChannelListGroupLabel -> Int -> Bool -> Int -> ChannelListGroup
ChannelListGroup ChannelListGroupLabel
ChannelGroupPublicChannels Int
unread Bool
coll ([ChannelListEntry] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [ChannelListEntry]
normEntries)
            , if Bool
coll then [ChannelListEntry]
forall a. Monoid a => a
mempty else ChannelListSorting -> [ChannelListEntry] -> [ChannelListEntry]
sortChannelListEntries ChannelListSorting
sorting [ChannelListEntry]
normEntries
            )
       , let unread :: Int
unread = [ChannelListEntry] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([ChannelListEntry] -> Int) -> [ChannelListEntry] -> Int
forall a b. (a -> b) -> a -> b
$ (ChannelListEntry -> Bool)
-> [ChannelListEntry] -> [ChannelListEntry]
forall a. (a -> Bool) -> [a] -> [a]
filter ChannelListEntry -> Bool
channelListEntryUnread [ChannelListEntry]
privEntries
             coll :: Bool
coll = ChannelListGroupLabel -> Bool
isHidden ChannelListGroupLabel
ChannelGroupPrivateChannels
         in ( ChannelListGroupLabel -> Int -> Bool -> Int -> ChannelListGroup
ChannelListGroup ChannelListGroupLabel
ChannelGroupPrivateChannels Int
unread Bool
coll ([ChannelListEntry] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [ChannelListEntry]
privEntries)
            , if Bool
coll then [ChannelListEntry]
forall a. Monoid a => a
mempty else ChannelListSorting -> [ChannelListEntry] -> [ChannelListEntry]
sortChannelListEntries ChannelListSorting
sorting [ChannelListEntry]
privEntries
            )
       , let unread :: Int
unread = [ChannelListEntry] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([ChannelListEntry] -> Int) -> [ChannelListEntry] -> Int
forall a b. (a -> b) -> a -> b
$ (ChannelListEntry -> Bool)
-> [ChannelListEntry] -> [ChannelListEntry]
forall a. (a -> Bool) -> [a] -> [a]
filter ChannelListEntry -> Bool
channelListEntryUnread [ChannelListEntry]
dmEntries
             coll :: Bool
coll = ChannelListGroupLabel -> Bool
isHidden ChannelListGroupLabel
ChannelGroupDirectMessages
         in ( ChannelListGroupLabel -> Int -> Bool -> Int -> ChannelListGroup
ChannelListGroup ChannelListGroupLabel
ChannelGroupDirectMessages Int
unread Bool
coll ([ChannelListEntry] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [ChannelListEntry]
dmEntries)
            , if Bool
coll then [ChannelListEntry]
forall a. Monoid a => a
mempty else [ChannelListEntry] -> [ChannelListEntry]
sortDMChannelListEntries [ChannelListEntry]
dmEntries
            )
       ]

sortChannelListEntries :: ChannelListSorting -> [ChannelListEntry] -> [ChannelListEntry]
sortChannelListEntries :: ChannelListSorting -> [ChannelListEntry] -> [ChannelListEntry]
sortChannelListEntries ChannelListSorting
ChannelListSortDefault =
    (ChannelListEntry -> ChannelListEntry -> Ordering)
-> [ChannelListEntry] -> [ChannelListEntry]
forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy ((ChannelListEntry -> (Bool, Text))
-> ChannelListEntry -> ChannelListEntry -> Ordering
forall a b. Ord a => (b -> a) -> b -> b -> Ordering
comparing (\ChannelListEntry
c -> (ChannelListEntry -> Bool
channelListEntryMuted ChannelListEntry
c, ChannelListEntry -> Text
channelListEntrySortValue ChannelListEntry
c)))
sortChannelListEntries ChannelListSorting
ChannelListSortUnreadFirst =
    (ChannelListEntry -> ChannelListEntry -> Ordering)
-> [ChannelListEntry] -> [ChannelListEntry]
forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy ((ChannelListEntry -> Bool)
-> ChannelListEntry -> ChannelListEntry -> Ordering
forall a b. Ord a => (b -> a) -> b -> b -> Ordering
comparing (Bool -> Bool
not (Bool -> Bool)
-> (ChannelListEntry -> Bool) -> ChannelListEntry -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ChannelListEntry -> Bool
channelListEntryUnread)) ([ChannelListEntry] -> [ChannelListEntry])
-> ([ChannelListEntry] -> [ChannelListEntry])
-> [ChannelListEntry]
-> [ChannelListEntry]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
    ChannelListSorting -> [ChannelListEntry] -> [ChannelListEntry]
sortChannelListEntries ChannelListSorting
ChannelListSortDefault

sortDMChannelListEntries :: [ChannelListEntry] -> [ChannelListEntry]
sortDMChannelListEntries :: [ChannelListEntry] -> [ChannelListEntry]
sortDMChannelListEntries = (ChannelListEntry -> ChannelListEntry -> Ordering)
-> [ChannelListEntry] -> [ChannelListEntry]
forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy ChannelListEntry -> ChannelListEntry -> Ordering
compareDMChannelListEntries

partitionFavorites :: [ChannelListEntry] -> ([ChannelListEntry], [ChannelListEntry])
partitionFavorites :: [ChannelListEntry] -> ([ChannelListEntry], [ChannelListEntry])
partitionFavorites = (ChannelListEntry -> Bool)
-> [ChannelListEntry] -> ([ChannelListEntry], [ChannelListEntry])
forall a. (a -> Bool) -> [a] -> ([a], [a])
partition ChannelListEntry -> Bool
channelListEntryFavorite

getChannelEntriesByType :: TeamId -> UserPreferences -> ClientChannels -> Type -> [ChannelListEntry]
getChannelEntriesByType :: TeamId
-> UserPreferences -> ClientChannels -> Type -> [ChannelListEntry]
getChannelEntriesByType TeamId
tId UserPreferences
prefs ClientChannels
cs Type
ty =
    let matches :: (ChannelId, ClientChannel) -> Bool
matches (ChannelId
_, ClientChannel
info) = ClientChannel
infoClientChannel -> Getting Type ClientChannel Type -> Type
forall s a. s -> Getting a s a -> a
^.(ChannelInfo -> Const Type ChannelInfo)
-> ClientChannel -> Const Type ClientChannel
Lens' ClientChannel ChannelInfo
ccInfo((ChannelInfo -> Const Type ChannelInfo)
 -> ClientChannel -> Const Type ClientChannel)
-> ((Type -> Const Type Type)
    -> ChannelInfo -> Const Type ChannelInfo)
-> Getting Type ClientChannel Type
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Type -> Const Type Type) -> ChannelInfo -> Const Type ChannelInfo
Lens' ChannelInfo Type
cdType Type -> Type -> Bool
forall a. Eq a => a -> a -> Bool
== Type
ty Bool -> Bool -> Bool
&&
                            ClientChannel
infoClientChannel
-> Getting (Maybe TeamId) ClientChannel (Maybe TeamId)
-> Maybe TeamId
forall s a. s -> Getting a s a -> a
^.(ChannelInfo -> Const (Maybe TeamId) ChannelInfo)
-> ClientChannel -> Const (Maybe TeamId) ClientChannel
Lens' ClientChannel ChannelInfo
ccInfo((ChannelInfo -> Const (Maybe TeamId) ChannelInfo)
 -> ClientChannel -> Const (Maybe TeamId) ClientChannel)
-> ((Maybe TeamId -> Const (Maybe TeamId) (Maybe TeamId))
    -> ChannelInfo -> Const (Maybe TeamId) ChannelInfo)
-> Getting (Maybe TeamId) ClientChannel (Maybe TeamId)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Maybe TeamId -> Const (Maybe TeamId) (Maybe TeamId))
-> ChannelInfo -> Const (Maybe TeamId) ChannelInfo
Lens' ChannelInfo (Maybe TeamId)
cdTeamId Maybe TeamId -> Maybe TeamId -> Bool
forall a. Eq a => a -> a -> Bool
== TeamId -> Maybe TeamId
forall a. a -> Maybe a
Just TeamId
tId
        pairs :: [(ChannelId, ClientChannel)]
pairs = ((ChannelId, ClientChannel) -> Bool)
-> ClientChannels -> [(ChannelId, ClientChannel)]
filteredChannels (ChannelId, ClientChannel) -> Bool
matches ClientChannels
cs
        entries :: [ChannelListEntry]
entries = (ChannelId, ClientChannel) -> ChannelListEntry
mkEntry ((ChannelId, ClientChannel) -> ChannelListEntry)
-> [(ChannelId, ClientChannel)] -> [ChannelListEntry]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [(ChannelId, ClientChannel)]
pairs
        mkEntry :: (ChannelId, ClientChannel) -> ChannelListEntry
mkEntry (ChannelId
cId, ClientChannel
ch) = ChannelListEntry :: ChannelId
-> ChannelListEntryType
-> Bool
-> Text
-> Bool
-> Bool
-> ChannelListEntry
ChannelListEntry { channelListEntryChannelId :: ChannelId
channelListEntryChannelId = ChannelId
cId
                                             , channelListEntryType :: ChannelListEntryType
channelListEntryType = ChannelListEntryType
CLChannel
                                             , channelListEntryMuted :: Bool
channelListEntryMuted = ClientChannel -> Bool
isMuted ClientChannel
ch
                                             , channelListEntryUnread :: Bool
channelListEntryUnread = ClientChannel -> Bool
hasUnread' ClientChannel
ch
                                             , channelListEntrySortValue :: Text
channelListEntrySortValue = ClientChannel
chClientChannel -> Getting Text ClientChannel Text -> Text
forall s a. s -> Getting a s a -> a
^.(ChannelInfo -> Const Text ChannelInfo)
-> ClientChannel -> Const Text ClientChannel
Lens' ClientChannel ChannelInfo
ccInfo((ChannelInfo -> Const Text ChannelInfo)
 -> ClientChannel -> Const Text ClientChannel)
-> ((Text -> Const Text Text)
    -> ChannelInfo -> Const Text ChannelInfo)
-> Getting Text ClientChannel Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Text -> Const Text Text) -> ChannelInfo -> Const Text ChannelInfo
Lens' ChannelInfo Text
cdDisplayName((Text -> Const Text Text)
 -> ChannelInfo -> Const Text ChannelInfo)
-> ((Text -> Const Text Text) -> Text -> Const Text Text)
-> (Text -> Const Text Text)
-> ChannelInfo
-> Const Text ChannelInfo
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Text -> Text) -> SimpleGetter Text Text
forall s a. (s -> a) -> SimpleGetter s a
to Text -> Text
T.toLower
                                             , channelListEntryFavorite :: Bool
channelListEntryFavorite = UserPreferences -> ChannelId -> Bool
isFavorite UserPreferences
prefs ChannelId
cId
                                             }
    in [ChannelListEntry]
entries

getDMChannelEntries :: UTCTime
                    -> Config
                    -> Maybe ClientConfig
                    -> UserPreferences
                    -> Users
                    -> ClientChannels
                    -> [ChannelListEntry]
getDMChannelEntries :: UTCTime
-> Config
-> Maybe ClientConfig
-> UserPreferences
-> Users
-> ClientChannels
-> [ChannelListEntry]
getDMChannelEntries UTCTime
now Config
config Maybe ClientConfig
cconfig UserPreferences
prefs Users
us ClientChannels
cs =
    let oneOnOneDmChans :: [ChannelListEntry]
oneOnOneDmChans = UTCTime
-> Config
-> Maybe ClientConfig
-> UserPreferences
-> Users
-> ClientChannels
-> [ChannelListEntry]
getSingleDMChannelEntries UTCTime
now Config
config Maybe ClientConfig
cconfig UserPreferences
prefs Users
us ClientChannels
cs
        groupChans :: [ChannelListEntry]
groupChans = UTCTime
-> Config
-> UserPreferences
-> ClientChannels
-> [ChannelListEntry]
getGroupDMChannelEntries UTCTime
now Config
config UserPreferences
prefs ClientChannels
cs
    in [ChannelListEntry]
groupChans [ChannelListEntry] -> [ChannelListEntry] -> [ChannelListEntry]
forall a. Semigroup a => a -> a -> a
<> [ChannelListEntry]
oneOnOneDmChans

compareDMChannelListEntries :: ChannelListEntry -> ChannelListEntry -> Ordering
compareDMChannelListEntries :: ChannelListEntry -> ChannelListEntry -> Ordering
compareDMChannelListEntries ChannelListEntry
e1 ChannelListEntry
e2 =
    let u1 :: Bool
u1 = ChannelListEntry -> Bool
channelListEntryUnread ChannelListEntry
e1
        u2 :: Bool
u2 = ChannelListEntry -> Bool
channelListEntryUnread ChannelListEntry
e2
        n1 :: Text
n1 = ChannelListEntry -> Text
channelListEntrySortValue ChannelListEntry
e1
        n2 :: Text
n2 = ChannelListEntry -> Text
channelListEntrySortValue ChannelListEntry
e2
    in if Bool
u1 Bool -> Bool -> Bool
forall a. Eq a => a -> a -> Bool
== Bool
u2
       then Text -> Text -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Text
n1 Text
n2
       else if Bool
u1 Bool -> Bool -> Bool
&& Bool -> Bool
not Bool
u2
            then Ordering
LT
            else Ordering
GT

useNickname' :: Maybe ClientConfig -> UserPreferences -> Bool
useNickname' :: Maybe ClientConfig -> UserPreferences -> Bool
useNickname' Maybe ClientConfig
clientConfig UserPreferences
prefs =
    let serverSetting :: Maybe Bool
serverSetting = case Maybe ClientConfig
clientConfigMaybe ClientConfig
-> Getting
     (First TeammateNameDisplayMode)
     (Maybe ClientConfig)
     TeammateNameDisplayMode
-> Maybe TeammateNameDisplayMode
forall s a. s -> Getting (First a) s a -> Maybe a
^?(ClientConfig
 -> Const (First TeammateNameDisplayMode) ClientConfig)
-> Maybe ClientConfig
-> Const (First TeammateNameDisplayMode) (Maybe ClientConfig)
forall a a'. Traversal (Maybe a) (Maybe a') a a'
_Just((ClientConfig
  -> Const (First TeammateNameDisplayMode) ClientConfig)
 -> Maybe ClientConfig
 -> Const (First TeammateNameDisplayMode) (Maybe ClientConfig))
-> ((TeammateNameDisplayMode
     -> Const (First TeammateNameDisplayMode) TeammateNameDisplayMode)
    -> ClientConfig
    -> Const (First TeammateNameDisplayMode) ClientConfig)
-> Getting
     (First TeammateNameDisplayMode)
     (Maybe ClientConfig)
     TeammateNameDisplayMode
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(ClientConfig -> TeammateNameDisplayMode)
-> SimpleGetter ClientConfig TeammateNameDisplayMode
forall s a. (s -> a) -> SimpleGetter s a
to ClientConfig -> TeammateNameDisplayMode
clientConfigTeammateNameDisplay of
            Just TeammateNameDisplayMode
TMNicknameOrFullname -> Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
True
            Maybe TeammateNameDisplayMode
_                         -> Maybe Bool
forall a. Maybe a
Nothing
        accountSetting :: Maybe Bool
accountSetting = (TeammateNameDisplayMode -> TeammateNameDisplayMode -> Bool
forall a. Eq a => a -> a -> Bool
== TeammateNameDisplayMode
TMNicknameOrFullname) (TeammateNameDisplayMode -> Bool)
-> Maybe TeammateNameDisplayMode -> Maybe Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (UserPreferences -> Maybe TeammateNameDisplayMode
_userPrefTeammateNameDisplayMode UserPreferences
prefs)
        fallback :: Bool
fallback = Bool
False
    in Bool -> Maybe Bool -> Bool
forall a. a -> Maybe a -> a
fromMaybe Bool
fallback (Maybe Bool -> Bool) -> Maybe Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Maybe Bool
accountSetting Maybe Bool -> Maybe Bool -> Maybe Bool
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Maybe Bool
serverSetting

displayNameForUser :: UserInfo -> Maybe ClientConfig -> UserPreferences -> Text
displayNameForUser :: UserInfo -> Maybe ClientConfig -> UserPreferences -> Text
displayNameForUser UserInfo
u Maybe ClientConfig
clientConfig UserPreferences
prefs
    | Maybe ClientConfig -> UserPreferences -> Bool
useNickname' Maybe ClientConfig
clientConfig UserPreferences
prefs =
        Text -> Maybe Text -> Text
forall a. a -> Maybe a -> a
fromMaybe (UserInfo
uUserInfo -> Getting Text UserInfo Text -> Text
forall s a. s -> Getting a s a -> a
^.Getting Text UserInfo Text
Lens' UserInfo Text
uiName) (UserInfo
uUserInfo
-> Getting (Maybe Text) UserInfo (Maybe Text) -> Maybe Text
forall s a. s -> Getting a s a -> a
^.Getting (Maybe Text) UserInfo (Maybe Text)
Lens' UserInfo (Maybe Text)
uiNickName)
    | Bool
otherwise =
        UserInfo
uUserInfo -> Getting Text UserInfo Text -> Text
forall s a. s -> Getting a s a -> a
^.Getting Text UserInfo Text
Lens' UserInfo Text
uiName

getGroupDMChannelEntries :: UTCTime
                         -> Config
                         -> UserPreferences
                         -> ClientChannels
                         -> [ChannelListEntry]
getGroupDMChannelEntries :: UTCTime
-> Config
-> UserPreferences
-> ClientChannels
-> [ChannelListEntry]
getGroupDMChannelEntries UTCTime
now Config
config UserPreferences
prefs ClientChannels
cs =
    let matches :: (ChannelId, ClientChannel) -> Bool
matches (ChannelId
_, ClientChannel
info) = ClientChannel
infoClientChannel -> Getting Type ClientChannel Type -> Type
forall s a. s -> Getting a s a -> a
^.(ChannelInfo -> Const Type ChannelInfo)
-> ClientChannel -> Const Type ClientChannel
Lens' ClientChannel ChannelInfo
ccInfo((ChannelInfo -> Const Type ChannelInfo)
 -> ClientChannel -> Const Type ClientChannel)
-> ((Type -> Const Type Type)
    -> ChannelInfo -> Const Type ChannelInfo)
-> Getting Type ClientChannel Type
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Type -> Const Type Type) -> ChannelInfo -> Const Type ChannelInfo
Lens' ChannelInfo Type
cdType Type -> Type -> Bool
forall a. Eq a => a -> a -> Bool
== Type
Group Bool -> Bool -> Bool
&&
                            ClientChannel
infoClientChannel
-> Getting (Maybe TeamId) ClientChannel (Maybe TeamId)
-> Maybe TeamId
forall s a. s -> Getting a s a -> a
^.(ChannelInfo -> Const (Maybe TeamId) ChannelInfo)
-> ClientChannel -> Const (Maybe TeamId) ClientChannel
Lens' ClientChannel ChannelInfo
ccInfo((ChannelInfo -> Const (Maybe TeamId) ChannelInfo)
 -> ClientChannel -> Const (Maybe TeamId) ClientChannel)
-> ((Maybe TeamId -> Const (Maybe TeamId) (Maybe TeamId))
    -> ChannelInfo -> Const (Maybe TeamId) ChannelInfo)
-> Getting (Maybe TeamId) ClientChannel (Maybe TeamId)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Maybe TeamId -> Const (Maybe TeamId) (Maybe TeamId))
-> ChannelInfo -> Const (Maybe TeamId) ChannelInfo
Lens' ChannelInfo (Maybe TeamId)
cdTeamId Maybe TeamId -> Maybe TeamId -> Bool
forall a. Eq a => a -> a -> Bool
== Maybe TeamId
forall a. Maybe a
Nothing Bool -> Bool -> Bool
&&
                            UTCTime -> Config -> UserPreferences -> ClientChannel -> Bool
groupChannelShouldAppear UTCTime
now Config
config UserPreferences
prefs ClientChannel
info
    in ((ChannelId, ClientChannel) -> ChannelListEntry)
-> [(ChannelId, ClientChannel)] -> [ChannelListEntry]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(ChannelId
cId, ClientChannel
ch) -> ChannelListEntry :: ChannelId
-> ChannelListEntryType
-> Bool
-> Text
-> Bool
-> Bool
-> ChannelListEntry
ChannelListEntry { channelListEntryChannelId :: ChannelId
channelListEntryChannelId = ChannelId
cId
                                            , channelListEntryType :: ChannelListEntryType
channelListEntryType = ChannelListEntryType
CLGroupDM
                                            , channelListEntryMuted :: Bool
channelListEntryMuted = ClientChannel -> Bool
isMuted ClientChannel
ch
                                            , channelListEntryUnread :: Bool
channelListEntryUnread = ClientChannel -> Bool
hasUnread' ClientChannel
ch
                                            , channelListEntrySortValue :: Text
channelListEntrySortValue = ClientChannel
chClientChannel -> Getting Text ClientChannel Text -> Text
forall s a. s -> Getting a s a -> a
^.(ChannelInfo -> Const Text ChannelInfo)
-> ClientChannel -> Const Text ClientChannel
Lens' ClientChannel ChannelInfo
ccInfo((ChannelInfo -> Const Text ChannelInfo)
 -> ClientChannel -> Const Text ClientChannel)
-> ((Text -> Const Text Text)
    -> ChannelInfo -> Const Text ChannelInfo)
-> Getting Text ClientChannel Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Text -> Const Text Text) -> ChannelInfo -> Const Text ChannelInfo
Lens' ChannelInfo Text
cdDisplayName
                                            , channelListEntryFavorite :: Bool
channelListEntryFavorite = UserPreferences -> ChannelId -> Bool
isFavorite UserPreferences
prefs ChannelId
cId
                                            }) ([(ChannelId, ClientChannel)] -> [ChannelListEntry])
-> [(ChannelId, ClientChannel)] -> [ChannelListEntry]
forall a b. (a -> b) -> a -> b
$
       ((ChannelId, ClientChannel) -> Bool)
-> ClientChannels -> [(ChannelId, ClientChannel)]
filteredChannels (ChannelId, ClientChannel) -> Bool
matches ClientChannels
cs

getSingleDMChannelEntries :: UTCTime
                          -> Config
                          -> Maybe ClientConfig
                          -> UserPreferences
                          -> Users
                          -> ClientChannels
                          -> [ChannelListEntry]
getSingleDMChannelEntries :: UTCTime
-> Config
-> Maybe ClientConfig
-> UserPreferences
-> Users
-> ClientChannels
-> [ChannelListEntry]
getSingleDMChannelEntries UTCTime
now Config
config Maybe ClientConfig
cconfig UserPreferences
prefs Users
us ClientChannels
cs =
    let mapping :: [(UserId, ChannelId)]
mapping = ClientChannels -> [(UserId, ChannelId)]
allDmChannelMappings ClientChannels
cs
        mappingWithUserInfo :: [ChannelListEntry]
mappingWithUserInfo = [Maybe ChannelListEntry] -> [ChannelListEntry]
forall a. [Maybe a] -> [a]
catMaybes ([Maybe ChannelListEntry] -> [ChannelListEntry])
-> [Maybe ChannelListEntry] -> [ChannelListEntry]
forall a b. (a -> b) -> a -> b
$ (UserId, ChannelId) -> Maybe ChannelListEntry
getInfo ((UserId, ChannelId) -> Maybe ChannelListEntry)
-> [(UserId, ChannelId)] -> [Maybe ChannelListEntry]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [(UserId, ChannelId)]
mapping
        getInfo :: (UserId, ChannelId) -> Maybe ChannelListEntry
getInfo (UserId
uId, ChannelId
cId) = do
            ClientChannel
c <- ChannelId -> ClientChannels -> Maybe ClientChannel
findChannelById ChannelId
cId ClientChannels
cs
            UserInfo
u <- UserId -> Users -> Maybe UserInfo
findUserById UserId
uId Users
us
            case UserInfo
uUserInfo -> Getting Bool UserInfo Bool -> Bool
forall s a. s -> Getting a s a -> a
^.Getting Bool UserInfo Bool
Lens' UserInfo Bool
uiDeleted of
                Bool
True -> Maybe ChannelListEntry
forall a. Maybe a
Nothing
                Bool
False ->
                    if UTCTime -> Config -> UserPreferences -> ClientChannel -> Bool
dmChannelShouldAppear UTCTime
now Config
config UserPreferences
prefs ClientChannel
c
                    then ChannelListEntry -> Maybe ChannelListEntry
forall (m :: * -> *) a. Monad m => a -> m a
return (ChannelListEntry :: ChannelId
-> ChannelListEntryType
-> Bool
-> Text
-> Bool
-> Bool
-> ChannelListEntry
ChannelListEntry { channelListEntryChannelId :: ChannelId
channelListEntryChannelId = ChannelId
cId
                                                  , channelListEntryType :: ChannelListEntryType
channelListEntryType = UserId -> ChannelListEntryType
CLUserDM UserId
uId
                                                  , channelListEntryMuted :: Bool
channelListEntryMuted = ClientChannel -> Bool
isMuted ClientChannel
c
                                                  , channelListEntryUnread :: Bool
channelListEntryUnread = ClientChannel -> Bool
hasUnread' ClientChannel
c
                                                  , channelListEntrySortValue :: Text
channelListEntrySortValue = UserInfo -> Maybe ClientConfig -> UserPreferences -> Text
displayNameForUser UserInfo
u Maybe ClientConfig
cconfig UserPreferences
prefs
                                                  , channelListEntryFavorite :: Bool
channelListEntryFavorite = UserPreferences -> ChannelId -> Bool
isFavorite UserPreferences
prefs ChannelId
cId
                                                  })
                    else Maybe ChannelListEntry
forall a. Maybe a
Nothing
    in [ChannelListEntry]
mappingWithUserInfo

-- | Return whether the specified channel has been marked as a favorite
-- channel.
isFavorite :: UserPreferences -> ChannelId -> Bool
isFavorite :: UserPreferences -> ChannelId -> Bool
isFavorite UserPreferences
prefs ChannelId
cId = UserPreferences -> ChannelId -> Maybe Bool
favoriteChannelPreference UserPreferences
prefs ChannelId
cId Maybe Bool -> Maybe Bool -> Bool
forall a. Eq a => a -> a -> Bool
== Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
True

-- Always show a DM channel if it has unread activity or has been marked
-- as a favorite.
--
-- If it has no unread activity and if the preferences explicitly say to
-- hide it, hide it.
--
-- Otherwise, only show it if at least one of the other conditions are
-- met (see 'or' below).
dmChannelShouldAppear :: UTCTime -> Config -> UserPreferences -> ClientChannel -> Bool
dmChannelShouldAppear :: UTCTime -> Config -> UserPreferences -> ClientChannel -> Bool
dmChannelShouldAppear UTCTime
now Config
config UserPreferences
prefs ClientChannel
c =
    let ndays :: Int
ndays = Config -> Int
configDirectChannelExpirationDays Config
config
        localCutoff :: UTCTime
localCutoff = NominalDiffTime -> UTCTime -> UTCTime
addUTCTime (NominalDiffTime
nominalDay NominalDiffTime -> NominalDiffTime -> NominalDiffTime
forall a. Num a => a -> a -> a
* (-(Int -> NominalDiffTime
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
ndays))) UTCTime
now
        cutoff :: ServerTime
cutoff = UTCTime -> ServerTime
ServerTime UTCTime
localCutoff
        updated :: ServerTime
updated = ClientChannel
cClientChannel
-> Getting ServerTime ClientChannel ServerTime -> ServerTime
forall s a. s -> Getting a s a -> a
^.(ChannelInfo -> Const ServerTime ChannelInfo)
-> ClientChannel -> Const ServerTime ClientChannel
Lens' ClientChannel ChannelInfo
ccInfo((ChannelInfo -> Const ServerTime ChannelInfo)
 -> ClientChannel -> Const ServerTime ClientChannel)
-> ((ServerTime -> Const ServerTime ServerTime)
    -> ChannelInfo -> Const ServerTime ChannelInfo)
-> Getting ServerTime ClientChannel ServerTime
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(ServerTime -> Const ServerTime ServerTime)
-> ChannelInfo -> Const ServerTime ChannelInfo
Lens' ChannelInfo ServerTime
cdUpdated
        uId :: UserId
uId = Maybe UserId -> UserId
forall a. HasCallStack => Maybe a -> a
fromJust (Maybe UserId -> UserId) -> Maybe UserId -> UserId
forall a b. (a -> b) -> a -> b
$ ClientChannel
cClientChannel
-> Getting (Maybe UserId) ClientChannel (Maybe UserId)
-> Maybe UserId
forall s a. s -> Getting a s a -> a
^.(ChannelInfo -> Const (Maybe UserId) ChannelInfo)
-> ClientChannel -> Const (Maybe UserId) ClientChannel
Lens' ClientChannel ChannelInfo
ccInfo((ChannelInfo -> Const (Maybe UserId) ChannelInfo)
 -> ClientChannel -> Const (Maybe UserId) ClientChannel)
-> ((Maybe UserId -> Const (Maybe UserId) (Maybe UserId))
    -> ChannelInfo -> Const (Maybe UserId) ChannelInfo)
-> Getting (Maybe UserId) ClientChannel (Maybe UserId)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Maybe UserId -> Const (Maybe UserId) (Maybe UserId))
-> ChannelInfo -> Const (Maybe UserId) ChannelInfo
Lens' ChannelInfo (Maybe UserId)
cdDMUserId
        cId :: ChannelId
cId = ClientChannel
cClientChannel
-> Getting ChannelId ClientChannel ChannelId -> ChannelId
forall s a. s -> Getting a s a -> a
^.(ChannelInfo -> Const ChannelId ChannelInfo)
-> ClientChannel -> Const ChannelId ClientChannel
Lens' ClientChannel ChannelInfo
ccInfo((ChannelInfo -> Const ChannelId ChannelInfo)
 -> ClientChannel -> Const ChannelId ClientChannel)
-> ((ChannelId -> Const ChannelId ChannelId)
    -> ChannelInfo -> Const ChannelId ChannelInfo)
-> Getting ChannelId ClientChannel ChannelId
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(ChannelId -> Const ChannelId ChannelId)
-> ChannelInfo -> Const ChannelId ChannelInfo
Lens' ChannelInfo ChannelId
cdChannelId
    in if UserPreferences -> ChannelId -> Bool
isFavorite UserPreferences
prefs ChannelId
cId
       then Bool
True
       else (if ClientChannel -> Bool
hasUnread' ClientChannel
c Bool -> Bool -> Bool
|| Bool -> (UTCTime -> Bool) -> Maybe UTCTime -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False (UTCTime -> UTCTime -> Bool
forall a. Ord a => a -> a -> Bool
>= UTCTime
localCutoff) (ClientChannel
cClientChannel
-> Getting (Maybe UTCTime) ClientChannel (Maybe UTCTime)
-> Maybe UTCTime
forall s a. s -> Getting a s a -> a
^.(ChannelInfo -> Const (Maybe UTCTime) ChannelInfo)
-> ClientChannel -> Const (Maybe UTCTime) ClientChannel
Lens' ClientChannel ChannelInfo
ccInfo((ChannelInfo -> Const (Maybe UTCTime) ChannelInfo)
 -> ClientChannel -> Const (Maybe UTCTime) ClientChannel)
-> ((Maybe UTCTime -> Const (Maybe UTCTime) (Maybe UTCTime))
    -> ChannelInfo -> Const (Maybe UTCTime) ChannelInfo)
-> Getting (Maybe UTCTime) ClientChannel (Maybe UTCTime)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Maybe UTCTime -> Const (Maybe UTCTime) (Maybe UTCTime))
-> ChannelInfo -> Const (Maybe UTCTime) ChannelInfo
Lens' ChannelInfo (Maybe UTCTime)
cdSidebarShowOverride)
             then Bool
True
             else case UserPreferences -> UserId -> Maybe Bool
dmChannelShowPreference UserPreferences
prefs UserId
uId of
                    Just Bool
False -> Bool
False
                    Maybe Bool
_ -> [Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
or [
                                -- The channel was updated recently enough
                                ServerTime
updated ServerTime -> ServerTime -> Bool
forall a. Ord a => a -> a -> Bool
>= ServerTime
cutoff
                            ])

-- Always show a group DM channel if it has unread activity or has been
-- marked as a favorite.
--
-- If it has no unread activity and if the preferences explicitly say to
-- hide it, hide it.
--
-- Otherwise, only show it if at least one of the other conditions are
-- met (see 'or' below).
groupChannelShouldAppear :: UTCTime -> Config -> UserPreferences -> ClientChannel -> Bool
groupChannelShouldAppear :: UTCTime -> Config -> UserPreferences -> ClientChannel -> Bool
groupChannelShouldAppear UTCTime
now Config
config UserPreferences
prefs ClientChannel
c =
    let ndays :: Int
ndays = Config -> Int
configDirectChannelExpirationDays Config
config
        localCutoff :: UTCTime
localCutoff = NominalDiffTime -> UTCTime -> UTCTime
addUTCTime (NominalDiffTime
nominalDay NominalDiffTime -> NominalDiffTime -> NominalDiffTime
forall a. Num a => a -> a -> a
* (-(Int -> NominalDiffTime
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
ndays))) UTCTime
now
        cutoff :: ServerTime
cutoff = UTCTime -> ServerTime
ServerTime UTCTime
localCutoff
        updated :: ServerTime
updated = ClientChannel
cClientChannel
-> Getting ServerTime ClientChannel ServerTime -> ServerTime
forall s a. s -> Getting a s a -> a
^.(ChannelInfo -> Const ServerTime ChannelInfo)
-> ClientChannel -> Const ServerTime ClientChannel
Lens' ClientChannel ChannelInfo
ccInfo((ChannelInfo -> Const ServerTime ChannelInfo)
 -> ClientChannel -> Const ServerTime ClientChannel)
-> ((ServerTime -> Const ServerTime ServerTime)
    -> ChannelInfo -> Const ServerTime ChannelInfo)
-> Getting ServerTime ClientChannel ServerTime
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(ServerTime -> Const ServerTime ServerTime)
-> ChannelInfo -> Const ServerTime ChannelInfo
Lens' ChannelInfo ServerTime
cdUpdated
        cId :: ChannelId
cId = ClientChannel
cClientChannel
-> Getting ChannelId ClientChannel ChannelId -> ChannelId
forall s a. s -> Getting a s a -> a
^.(ChannelInfo -> Const ChannelId ChannelInfo)
-> ClientChannel -> Const ChannelId ClientChannel
Lens' ClientChannel ChannelInfo
ccInfo((ChannelInfo -> Const ChannelId ChannelInfo)
 -> ClientChannel -> Const ChannelId ClientChannel)
-> ((ChannelId -> Const ChannelId ChannelId)
    -> ChannelInfo -> Const ChannelId ChannelInfo)
-> Getting ChannelId ClientChannel ChannelId
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(ChannelId -> Const ChannelId ChannelId)
-> ChannelInfo -> Const ChannelId ChannelInfo
Lens' ChannelInfo ChannelId
cdChannelId
    in if UserPreferences -> ChannelId -> Bool
isFavorite UserPreferences
prefs ChannelId
cId
       then Bool
True
       else (if ClientChannel -> Bool
hasUnread' ClientChannel
c Bool -> Bool -> Bool
|| Bool -> (UTCTime -> Bool) -> Maybe UTCTime -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False (UTCTime -> UTCTime -> Bool
forall a. Ord a => a -> a -> Bool
>= UTCTime
localCutoff) (ClientChannel
cClientChannel
-> Getting (Maybe UTCTime) ClientChannel (Maybe UTCTime)
-> Maybe UTCTime
forall s a. s -> Getting a s a -> a
^.(ChannelInfo -> Const (Maybe UTCTime) ChannelInfo)
-> ClientChannel -> Const (Maybe UTCTime) ClientChannel
Lens' ClientChannel ChannelInfo
ccInfo((ChannelInfo -> Const (Maybe UTCTime) ChannelInfo)
 -> ClientChannel -> Const (Maybe UTCTime) ClientChannel)
-> ((Maybe UTCTime -> Const (Maybe UTCTime) (Maybe UTCTime))
    -> ChannelInfo -> Const (Maybe UTCTime) ChannelInfo)
-> Getting (Maybe UTCTime) ClientChannel (Maybe UTCTime)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Maybe UTCTime -> Const (Maybe UTCTime) (Maybe UTCTime))
-> ChannelInfo -> Const (Maybe UTCTime) ChannelInfo
Lens' ChannelInfo (Maybe UTCTime)
cdSidebarShowOverride)
             then Bool
True
             else case UserPreferences -> ChannelId -> Maybe Bool
groupChannelShowPreference UserPreferences
prefs ChannelId
cId of
                    Just Bool
False -> Bool
False
                    Maybe Bool
_ -> [Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
or [
                                -- The channel was updated recently enough
                                ServerTime
updated ServerTime -> ServerTime -> Bool
forall a. Ord a => a -> a -> Bool
>= ServerTime
cutoff
                            ])

dmChannelShowPreference :: UserPreferences -> UserId -> Maybe Bool
dmChannelShowPreference :: UserPreferences -> UserId -> Maybe Bool
dmChannelShowPreference UserPreferences
ps UserId
uId = UserId -> HashMap UserId Bool -> Maybe Bool
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HM.lookup UserId
uId (UserPreferences -> HashMap UserId Bool
_userPrefDirectChannelPrefs UserPreferences
ps)

groupChannelShowPreference :: UserPreferences -> ChannelId -> Maybe Bool
groupChannelShowPreference :: UserPreferences -> ChannelId -> Maybe Bool
groupChannelShowPreference UserPreferences
ps ChannelId
cId = ChannelId -> HashMap ChannelId Bool -> Maybe Bool
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HM.lookup ChannelId
cId (UserPreferences -> HashMap ChannelId Bool
_userPrefGroupChannelPrefs UserPreferences
ps)

favoriteChannelPreference :: UserPreferences -> ChannelId -> Maybe Bool
favoriteChannelPreference :: UserPreferences -> ChannelId -> Maybe Bool
favoriteChannelPreference UserPreferences
ps ChannelId
cId = ChannelId -> HashMap ChannelId Bool -> Maybe Bool
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HM.lookup ChannelId
cId (UserPreferences -> HashMap ChannelId Bool
_userPrefFavoriteChannelPrefs UserPreferences
ps)

-- | Types that provide a "semantically equal" operation. Two values may
-- be semantically equal even if they are not equal according to Eq if,
-- for example, they are equal on the basis of some fields that are more
-- pertinent than others.
class (Show a, Eq a, Ord a) => SemEq a where
    semeq :: a -> a -> Bool

instance SemEq Name where
    semeq :: Name -> Name -> Bool
semeq (ClickableURL Maybe MessageId
mId1 Name
r1 Int
_ LinkTarget
t1) (ClickableURL Maybe MessageId
mId2 Name
r2 Int
_ LinkTarget
t2) = Maybe MessageId
mId1 Maybe MessageId -> Maybe MessageId -> Bool
forall a. Eq a => a -> a -> Bool
== Maybe MessageId
mId2 Bool -> Bool -> Bool
&& LinkTarget
t1 LinkTarget -> LinkTarget -> Bool
forall a. Eq a => a -> a -> Bool
== LinkTarget
t2 Bool -> Bool -> Bool
&& Name
r1 Name -> Name -> Bool
forall a. Eq a => a -> a -> Bool
== Name
r2
    semeq (ClickableUsername Maybe MessageId
mId1 Name
r1 Int
_ Text
n) (ClickableUsername Maybe MessageId
mId2 Name
r2 Int
_ Text
n2) = Maybe MessageId
mId1 Maybe MessageId -> Maybe MessageId -> Bool
forall a. Eq a => a -> a -> Bool
== Maybe MessageId
mId2 Bool -> Bool -> Bool
&& Text
n Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
n2 Bool -> Bool -> Bool
&& Name
r1 Name -> Name -> Bool
forall a. Eq a => a -> a -> Bool
== Name
r2
    semeq Name
a Name
b = Name
a Name -> Name -> Bool
forall a. Eq a => a -> a -> Bool
== Name
b

instance SemEq a => SemEq (Maybe a) where
    semeq :: Maybe a -> Maybe a -> Bool
semeq Maybe a
Nothing Maybe a
Nothing = Bool
True
    semeq (Just a
a) (Just a
b) = a
a a -> a -> Bool
forall a. SemEq a => a -> a -> Bool
`semeq` a
b
    semeq Maybe a
_ Maybe a
_ = Bool
False

-- | The sum type of exceptions we expect to encounter on authentication
-- failure. We encode them explicitly here so that we can print them in
-- a more user-friendly manner than just 'show'.
data AuthenticationException =
    ConnectError HostCannotConnect
    | ResolveError HostNotResolved
    | AuthIOError IOError
    | LoginError LoginFailureException
    | MattermostServerError MattermostError
    | OtherAuthError SomeException
    deriving (Int -> AuthenticationException -> ShowS
[AuthenticationException] -> ShowS
AuthenticationException -> String
(Int -> AuthenticationException -> ShowS)
-> (AuthenticationException -> String)
-> ([AuthenticationException] -> ShowS)
-> Show AuthenticationException
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [AuthenticationException] -> ShowS
$cshowList :: [AuthenticationException] -> ShowS
show :: AuthenticationException -> String
$cshow :: AuthenticationException -> String
showsPrec :: Int -> AuthenticationException -> ShowS
$cshowsPrec :: Int -> AuthenticationException -> ShowS
Show)

-- | Our 'ConnectionInfo' contains exactly as much information as is
-- necessary to start a connection with a Mattermost server. This is
-- built up during interactive authentication and then is used to log
-- in.
--
-- If the access token field is non-empty, that value is used and the
-- username and password values are ignored.
data ConnectionInfo =
    ConnectionInfo { ConnectionInfo -> Text
_ciHostname :: Text
                   , ConnectionInfo -> Int
_ciPort     :: Int
                   , ConnectionInfo -> Text
_ciUrlPath  :: Text
                   , ConnectionInfo -> Text
_ciUsername :: Text
                   , ConnectionInfo -> Maybe Text
_ciOTPToken :: Maybe Text
                   , ConnectionInfo -> Text
_ciPassword :: Text
                   , ConnectionInfo -> Text
_ciAccessToken :: Text
                   , ConnectionInfo -> ConnectionType
_ciType     :: ConnectionType
                   }

-- | We want to continue referring to posts by their IDs, but we don't
-- want to have to synthesize new valid IDs for messages from the client
-- itself (like error messages or informative client responses). To that
-- end, a PostRef can be either a PostId or a newly-generated client ID.
data PostRef
    = MMId PostId
    | CLId Int
    deriving (PostRef -> PostRef -> Bool
(PostRef -> PostRef -> Bool)
-> (PostRef -> PostRef -> Bool) -> Eq PostRef
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PostRef -> PostRef -> Bool
$c/= :: PostRef -> PostRef -> Bool
== :: PostRef -> PostRef -> Bool
$c== :: PostRef -> PostRef -> Bool
Eq, Int -> PostRef -> ShowS
[PostRef] -> ShowS
PostRef -> String
(Int -> PostRef -> ShowS)
-> (PostRef -> String) -> ([PostRef] -> ShowS) -> Show PostRef
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PostRef] -> ShowS
$cshowList :: [PostRef] -> ShowS
show :: PostRef -> String
$cshow :: PostRef -> String
showsPrec :: Int -> PostRef -> ShowS
$cshowsPrec :: Int -> PostRef -> ShowS
Show)

-- ** Channel-matching types

data ChannelSelectPattern = CSP MatchType Text
                          | CSPAny
                          deriving (ChannelSelectPattern -> ChannelSelectPattern -> Bool
(ChannelSelectPattern -> ChannelSelectPattern -> Bool)
-> (ChannelSelectPattern -> ChannelSelectPattern -> Bool)
-> Eq ChannelSelectPattern
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ChannelSelectPattern -> ChannelSelectPattern -> Bool
$c/= :: ChannelSelectPattern -> ChannelSelectPattern -> Bool
== :: ChannelSelectPattern -> ChannelSelectPattern -> Bool
$c== :: ChannelSelectPattern -> ChannelSelectPattern -> Bool
Eq, Int -> ChannelSelectPattern -> ShowS
[ChannelSelectPattern] -> ShowS
ChannelSelectPattern -> String
(Int -> ChannelSelectPattern -> ShowS)
-> (ChannelSelectPattern -> String)
-> ([ChannelSelectPattern] -> ShowS)
-> Show ChannelSelectPattern
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ChannelSelectPattern] -> ShowS
$cshowList :: [ChannelSelectPattern] -> ShowS
show :: ChannelSelectPattern -> String
$cshow :: ChannelSelectPattern -> String
showsPrec :: Int -> ChannelSelectPattern -> ShowS
$cshowsPrec :: Int -> ChannelSelectPattern -> ShowS
Show)

data MatchType =
    Prefix
    | Suffix
    | Infix
    | Equal
    | PrefixDMOnly
    | PrefixNonDMOnly
    deriving (MatchType -> MatchType -> Bool
(MatchType -> MatchType -> Bool)
-> (MatchType -> MatchType -> Bool) -> Eq MatchType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: MatchType -> MatchType -> Bool
$c/= :: MatchType -> MatchType -> Bool
== :: MatchType -> MatchType -> Bool
$c== :: MatchType -> MatchType -> Bool
Eq, Int -> MatchType -> ShowS
[MatchType] -> ShowS
MatchType -> String
(Int -> MatchType -> ShowS)
-> (MatchType -> String)
-> ([MatchType] -> ShowS)
-> Show MatchType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [MatchType] -> ShowS
$cshowList :: [MatchType] -> ShowS
show :: MatchType -> String
$cshow :: MatchType -> String
showsPrec :: Int -> MatchType -> ShowS
$cshowsPrec :: Int -> MatchType -> ShowS
Show)

-- * Application State Values

data ProgramOutput =
    ProgramOutput { ProgramOutput -> String
program :: FilePath
                  , ProgramOutput -> [String]
programArgs :: [String]
                  , ProgramOutput -> String
programStdout :: String
                  , ProgramOutput -> String
programStderr :: String
                  , ProgramOutput -> ExitCode
programExitCode :: ExitCode
                  }

defaultUserPreferences :: UserPreferences
defaultUserPreferences :: UserPreferences
defaultUserPreferences =
    UserPreferences :: Bool
-> Seq FlaggedPost
-> HashMap ChannelId Bool
-> HashMap UserId Bool
-> HashMap ChannelId Bool
-> Maybe TeammateNameDisplayMode
-> Maybe [TeamId]
-> UserPreferences
UserPreferences { _userPrefShowJoinLeave :: Bool
_userPrefShowJoinLeave     = Bool
True
                    , _userPrefFlaggedPostList :: Seq FlaggedPost
_userPrefFlaggedPostList   = Seq FlaggedPost
forall a. Monoid a => a
mempty
                    , _userPrefGroupChannelPrefs :: HashMap ChannelId Bool
_userPrefGroupChannelPrefs = HashMap ChannelId Bool
forall a. Monoid a => a
mempty
                    , _userPrefDirectChannelPrefs :: HashMap UserId Bool
_userPrefDirectChannelPrefs = HashMap UserId Bool
forall a. Monoid a => a
mempty
                    , _userPrefFavoriteChannelPrefs :: HashMap ChannelId Bool
_userPrefFavoriteChannelPrefs = HashMap ChannelId Bool
forall a. Monoid a => a
mempty
                    , _userPrefTeammateNameDisplayMode :: Maybe TeammateNameDisplayMode
_userPrefTeammateNameDisplayMode = Maybe TeammateNameDisplayMode
forall a. Maybe a
Nothing
                    , _userPrefTeamOrder :: Maybe [TeamId]
_userPrefTeamOrder = Maybe [TeamId]
forall a. Maybe a
Nothing
                    }

setUserPreferences :: Seq Preference -> UserPreferences -> UserPreferences
setUserPreferences :: Seq Preference -> UserPreferences -> UserPreferences
setUserPreferences = (UserPreferences -> Seq Preference -> UserPreferences)
-> Seq Preference -> UserPreferences -> UserPreferences
forall a b c. (a -> b -> c) -> b -> a -> c
flip ((Preference -> UserPreferences -> UserPreferences)
-> UserPreferences -> Seq Preference -> UserPreferences
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
F.foldr Preference -> UserPreferences -> UserPreferences
go)
    where go :: Preference -> UserPreferences -> UserPreferences
go Preference
p UserPreferences
u
            | Just FlaggedPost
fp <- Preference -> Maybe FlaggedPost
preferenceToFlaggedPost Preference
p =
              UserPreferences
u { _userPrefFlaggedPostList :: Seq FlaggedPost
_userPrefFlaggedPostList =
                  UserPreferences -> Seq FlaggedPost
_userPrefFlaggedPostList UserPreferences
u Seq FlaggedPost -> FlaggedPost -> Seq FlaggedPost
forall a. Seq a -> a -> Seq a
Seq.|> FlaggedPost
fp
                }
            | Just DirectChannelShowStatus
gp <- Preference -> Maybe DirectChannelShowStatus
preferenceToDirectChannelShowStatus Preference
p =
              UserPreferences
u { _userPrefDirectChannelPrefs :: HashMap UserId Bool
_userPrefDirectChannelPrefs =
                  UserId -> Bool -> HashMap UserId Bool -> HashMap UserId Bool
forall k v.
(Eq k, Hashable k) =>
k -> v -> HashMap k v -> HashMap k v
HM.insert
                    (DirectChannelShowStatus -> UserId
directChannelShowUserId DirectChannelShowStatus
gp)
                    (DirectChannelShowStatus -> Bool
directChannelShowValue DirectChannelShowStatus
gp)
                    (UserPreferences -> HashMap UserId Bool
_userPrefDirectChannelPrefs UserPreferences
u)
                }
            | Just GroupChannelPreference
gp <- Preference -> Maybe GroupChannelPreference
preferenceToGroupChannelPreference Preference
p =
              UserPreferences
u { _userPrefGroupChannelPrefs :: HashMap ChannelId Bool
_userPrefGroupChannelPrefs =
                  ChannelId
-> Bool -> HashMap ChannelId Bool -> HashMap ChannelId Bool
forall k v.
(Eq k, Hashable k) =>
k -> v -> HashMap k v -> HashMap k v
HM.insert
                    (GroupChannelPreference -> ChannelId
groupChannelId GroupChannelPreference
gp)
                    (GroupChannelPreference -> Bool
groupChannelShow GroupChannelPreference
gp)
                    (UserPreferences -> HashMap ChannelId Bool
_userPrefGroupChannelPrefs UserPreferences
u)
                }
            | Just FavoriteChannelPreference
fp <- Preference -> Maybe FavoriteChannelPreference
preferenceToFavoriteChannelPreference Preference
p =
              UserPreferences
u { _userPrefFavoriteChannelPrefs :: HashMap ChannelId Bool
_userPrefFavoriteChannelPrefs =
                  ChannelId
-> Bool -> HashMap ChannelId Bool -> HashMap ChannelId Bool
forall k v.
(Eq k, Hashable k) =>
k -> v -> HashMap k v -> HashMap k v
HM.insert
                    (FavoriteChannelPreference -> ChannelId
favoriteChannelId FavoriteChannelPreference
fp)
                    (FavoriteChannelPreference -> Bool
favoriteChannelShow FavoriteChannelPreference
fp)
                    (UserPreferences -> HashMap ChannelId Bool
_userPrefFavoriteChannelPrefs UserPreferences
u)
                }
            | Just [TeamId]
tIds <- Preference -> Maybe [TeamId]
preferenceToTeamOrder Preference
p =
              UserPreferences
u { _userPrefTeamOrder :: Maybe [TeamId]
_userPrefTeamOrder = [TeamId] -> Maybe [TeamId]
forall a. a -> Maybe a
Just [TeamId]
tIds
                }
            | Preference -> PreferenceName
preferenceName Preference
p PreferenceName -> PreferenceName -> Bool
forall a. Eq a => a -> a -> Bool
== Text -> PreferenceName
PreferenceName Text
"join_leave" =
              UserPreferences
u { _userPrefShowJoinLeave :: Bool
_userPrefShowJoinLeave =
                  Preference -> PreferenceValue
preferenceValue Preference
p PreferenceValue -> PreferenceValue -> Bool
forall a. Eq a => a -> a -> Bool
/= Text -> PreferenceValue
PreferenceValue Text
"false" }
            | Preference -> PreferenceCategory
preferenceCategory Preference
p PreferenceCategory -> PreferenceCategory -> Bool
forall a. Eq a => a -> a -> Bool
== PreferenceCategory
PreferenceCategoryDisplaySettings Bool -> Bool -> Bool
&&
              Preference -> PreferenceName
preferenceName Preference
p PreferenceName -> PreferenceName -> Bool
forall a. Eq a => a -> a -> Bool
== Text -> PreferenceName
PreferenceName Text
"name_format" =
                  let PreferenceValue Text
txt = Preference -> PreferenceValue
preferenceValue Preference
p
                  in UserPreferences
u { _userPrefTeammateNameDisplayMode :: Maybe TeammateNameDisplayMode
_userPrefTeammateNameDisplayMode = TeammateNameDisplayMode -> Maybe TeammateNameDisplayMode
forall a. a -> Maybe a
Just (TeammateNameDisplayMode -> Maybe TeammateNameDisplayMode)
-> TeammateNameDisplayMode -> Maybe TeammateNameDisplayMode
forall a b. (a -> b) -> a -> b
$ Text -> TeammateNameDisplayMode
teammateDisplayModeFromText Text
txt }
            | Bool
otherwise = UserPreferences
u

-- | Log message tags.
data LogCategory =
    LogGeneral
    | LogAPI
    | LogWebsocket
    | LogError
    | LogUserMark
    deriving (LogCategory -> LogCategory -> Bool
(LogCategory -> LogCategory -> Bool)
-> (LogCategory -> LogCategory -> Bool) -> Eq LogCategory
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: LogCategory -> LogCategory -> Bool
$c/= :: LogCategory -> LogCategory -> Bool
== :: LogCategory -> LogCategory -> Bool
$c== :: LogCategory -> LogCategory -> Bool
Eq, Int -> LogCategory -> ShowS
[LogCategory] -> ShowS
LogCategory -> String
(Int -> LogCategory -> ShowS)
-> (LogCategory -> String)
-> ([LogCategory] -> ShowS)
-> Show LogCategory
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [LogCategory] -> ShowS
$cshowList :: [LogCategory] -> ShowS
show :: LogCategory -> String
$cshow :: LogCategory -> String
showsPrec :: Int -> LogCategory -> ShowS
$cshowsPrec :: Int -> LogCategory -> ShowS
Show)

-- | A log message.
data LogMessage =
    LogMessage { LogMessage -> Text
logMessageText :: !Text
               -- ^ The text of the log message.
               , LogMessage -> Maybe LogContext
logMessageContext :: !(Maybe LogContext)
               -- ^ The optional context information relevant to the log
               -- message.
               , LogMessage -> LogCategory
logMessageCategory :: !LogCategory
               -- ^ The category of the log message.
               , LogMessage -> UTCTime
logMessageTimestamp :: !UTCTime
               -- ^ The timestamp of the log message.
               }
               deriving (LogMessage -> LogMessage -> Bool
(LogMessage -> LogMessage -> Bool)
-> (LogMessage -> LogMessage -> Bool) -> Eq LogMessage
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: LogMessage -> LogMessage -> Bool
$c/= :: LogMessage -> LogMessage -> Bool
== :: LogMessage -> LogMessage -> Bool
$c== :: LogMessage -> LogMessage -> Bool
Eq, Int -> LogMessage -> ShowS
[LogMessage] -> ShowS
LogMessage -> String
(Int -> LogMessage -> ShowS)
-> (LogMessage -> String)
-> ([LogMessage] -> ShowS)
-> Show LogMessage
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [LogMessage] -> ShowS
$cshowList :: [LogMessage] -> ShowS
show :: LogMessage -> String
$cshow :: LogMessage -> String
showsPrec :: Int -> LogMessage -> ShowS
$cshowsPrec :: Int -> LogMessage -> ShowS
Show)

-- | A logging thread command.
data LogCommand =
    LogToFile FilePath
    -- ^ Start logging to the specified path.
    | LogAMessage !LogMessage
    -- ^ Log the specified message.
    | StopLogging
    -- ^ Stop any active logging.
    | ShutdownLogging
    -- ^ Shut down.
    | GetLogDestination
    -- ^ Ask the logging thread about its active logging destination.
    | LogSnapshot FilePath
    -- ^ Ask the logging thread to dump the current buffer to the
    -- specified destination.
    deriving (Int -> LogCommand -> ShowS
[LogCommand] -> ShowS
LogCommand -> String
(Int -> LogCommand -> ShowS)
-> (LogCommand -> String)
-> ([LogCommand] -> ShowS)
-> Show LogCommand
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [LogCommand] -> ShowS
$cshowList :: [LogCommand] -> ShowS
show :: LogCommand -> String
$cshow :: LogCommand -> String
showsPrec :: Int -> LogCommand -> ShowS
$cshowsPrec :: Int -> LogCommand -> ShowS
Show)

-- | A handle to the log manager thread.
data LogManager =
    LogManager { LogManager -> TChan LogCommand
logManagerCommandChannel :: STM.TChan LogCommand
               , LogManager -> Async ()
logManagerHandle :: Async ()
               }

startLoggingToFile :: LogManager -> FilePath -> IO ()
startLoggingToFile :: LogManager -> String -> IO ()
startLoggingToFile LogManager
mgr String
loc = LogManager -> LogCommand -> IO ()
sendLogCommand LogManager
mgr (LogCommand -> IO ()) -> LogCommand -> IO ()
forall a b. (a -> b) -> a -> b
$ String -> LogCommand
LogToFile String
loc

stopLoggingToFile :: LogManager -> IO ()
stopLoggingToFile :: LogManager -> IO ()
stopLoggingToFile LogManager
mgr = LogManager -> LogCommand -> IO ()
sendLogCommand LogManager
mgr LogCommand
StopLogging

requestLogSnapshot :: LogManager -> FilePath -> IO ()
requestLogSnapshot :: LogManager -> String -> IO ()
requestLogSnapshot LogManager
mgr String
path = LogManager -> LogCommand -> IO ()
sendLogCommand LogManager
mgr (LogCommand -> IO ()) -> LogCommand -> IO ()
forall a b. (a -> b) -> a -> b
$ String -> LogCommand
LogSnapshot String
path

requestLogDestination :: LogManager -> IO ()
requestLogDestination :: LogManager -> IO ()
requestLogDestination LogManager
mgr = LogManager -> LogCommand -> IO ()
sendLogCommand LogManager
mgr LogCommand
GetLogDestination

sendLogMessage :: LogManager -> LogMessage -> IO ()
sendLogMessage :: LogManager -> LogMessage -> IO ()
sendLogMessage LogManager
mgr LogMessage
lm = LogManager -> LogCommand -> IO ()
sendLogCommand LogManager
mgr (LogCommand -> IO ()) -> LogCommand -> IO ()
forall a b. (a -> b) -> a -> b
$ LogMessage -> LogCommand
LogAMessage LogMessage
lm

sendLogCommand :: LogManager -> LogCommand -> IO ()
sendLogCommand :: LogManager -> LogCommand -> IO ()
sendLogCommand LogManager
mgr LogCommand
c =
    STM () -> IO ()
forall a. STM a -> IO a
STM.atomically (STM () -> IO ()) -> STM () -> IO ()
forall a b. (a -> b) -> a -> b
$ TChan LogCommand -> LogCommand -> STM ()
forall a. TChan a -> a -> STM ()
STM.writeTChan (LogManager -> TChan LogCommand
logManagerCommandChannel LogManager
mgr) LogCommand
c

-- | 'ChatResources' represents configuration and connection-related
-- information, as opposed to current model or view information.
-- Information that goes in the 'ChatResources' value should be limited
-- to information that we read or set up prior to setting up the bulk of
-- the application state.
data ChatResources =
    ChatResources { ChatResources -> Session
_crSession             :: Session
                  , ChatResources -> Maybe ThreadId
_crWebsocketThreadId   :: Maybe ThreadId
                  , ChatResources -> ConnectionData
_crConn                :: ConnectionData
                  , ChatResources -> RequestChan
_crRequestQueue        :: RequestChan
                  , ChatResources -> BChan MHEvent
_crEventQueue          :: BCH.BChan MHEvent
                  , ChatResources -> TChan ProgramOutput
_crSubprocessLog       :: STM.TChan ProgramOutput
                  , ChatResources -> TChan WebsocketAction
_crWebsocketActionChan :: STM.TChan WebsocketAction
                  , ChatResources -> AttrMap
_crTheme               :: AttrMap
                  , ChatResources -> TChan [UserId]
_crStatusUpdateChan    :: STM.TChan [UserId]
                  , ChatResources -> Config
_crConfiguration       :: Config
                  , ChatResources -> Set PostId
_crFlaggedPosts        :: Set PostId
                  , ChatResources -> UserPreferences
_crUserPreferences     :: UserPreferences
                  , ChatResources -> SyntaxMap
_crSyntaxMap           :: SyntaxMap
                  , ChatResources -> LogManager
_crLogManager          :: LogManager
                  , ChatResources -> EmojiCollection
_crEmoji               :: EmojiCollection
                  , ChatResources -> Maybe Aspell
_crSpellChecker        :: Maybe Aspell
                  }

-- | The 'GlobalEditState' value contains state not specific to any
-- single editor.
data GlobalEditState =
    GlobalEditState { GlobalEditState -> Text
_gedYankBuffer :: Text
                    }

emptyGlobalEditState :: GlobalEditState
emptyGlobalEditState :: GlobalEditState
emptyGlobalEditState =
    GlobalEditState :: Text -> GlobalEditState
GlobalEditState { _gedYankBuffer :: Text
_gedYankBuffer   = Text
""
                    }

-- | A 'RequestChan' is a queue of operations we have to perform in the
-- background to avoid blocking on the main loop
type RequestChan = STM.TChan (IO (Maybe (MH ())))

-- | Help topics
data HelpTopic =
    HelpTopic { HelpTopic -> Text
helpTopicName         :: Text
              , HelpTopic -> Text
helpTopicDescription  :: Text
              , HelpTopic -> HelpScreen
helpTopicScreen       :: HelpScreen
              }
              deriving (HelpTopic -> HelpTopic -> Bool
(HelpTopic -> HelpTopic -> Bool)
-> (HelpTopic -> HelpTopic -> Bool) -> Eq HelpTopic
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: HelpTopic -> HelpTopic -> Bool
$c/= :: HelpTopic -> HelpTopic -> Bool
== :: HelpTopic -> HelpTopic -> Bool
$c== :: HelpTopic -> HelpTopic -> Bool
Eq, Int -> HelpTopic -> ShowS
[HelpTopic] -> ShowS
HelpTopic -> String
(Int -> HelpTopic -> ShowS)
-> (HelpTopic -> String)
-> ([HelpTopic] -> ShowS)
-> Show HelpTopic
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [HelpTopic] -> ShowS
$cshowList :: [HelpTopic] -> ShowS
show :: HelpTopic -> String
$cshow :: HelpTopic -> String
showsPrec :: Int -> HelpTopic -> ShowS
$cshowsPrec :: Int -> HelpTopic -> ShowS
Show)

-- | Mode type for the current contents of the post list window
data PostListContents =
    PostListFlagged
    | PostListPinned ChannelId
    | PostListSearch Text Bool -- for the query and search status
    deriving (PostListContents -> PostListContents -> Bool
(PostListContents -> PostListContents -> Bool)
-> (PostListContents -> PostListContents -> Bool)
-> Eq PostListContents
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PostListContents -> PostListContents -> Bool
$c/= :: PostListContents -> PostListContents -> Bool
== :: PostListContents -> PostListContents -> Bool
$c== :: PostListContents -> PostListContents -> Bool
Eq, Int -> PostListContents -> ShowS
[PostListContents] -> ShowS
PostListContents -> String
(Int -> PostListContents -> ShowS)
-> (PostListContents -> String)
-> ([PostListContents] -> ShowS)
-> Show PostListContents
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PostListContents] -> ShowS
$cshowList :: [PostListContents] -> ShowS
show :: PostListContents -> String
$cshow :: PostListContents -> String
showsPrec :: Int -> PostListContents -> ShowS
$cshowsPrec :: Int -> PostListContents -> ShowS
Show)

-- | The 'Mode' represents the current dominant UI activity
data Mode =
    Main
    | ShowHelp HelpTopic
    | ChannelSelect
    | LeaveChannelConfirm
    | DeleteChannelConfirm
    | MessageSelectDeleteConfirm MessageInterfaceTarget
    | PostListWindow PostListContents
    | UserListWindow
    | ReactionEmojiListWindow
    | ChannelListWindow
    | ThemeListWindow
    | ViewMessage
    | EditNotifyPrefs
    | ChannelTopicWindow
    deriving (Mode -> Mode -> Bool
(Mode -> Mode -> Bool) -> (Mode -> Mode -> Bool) -> Eq Mode
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Mode -> Mode -> Bool
$c/= :: Mode -> Mode -> Bool
== :: Mode -> Mode -> Bool
$c== :: Mode -> Mode -> Bool
Eq, Int -> Mode -> ShowS
[Mode] -> ShowS
Mode -> String
(Int -> Mode -> ShowS)
-> (Mode -> String) -> ([Mode] -> ShowS) -> Show Mode
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Mode] -> ShowS
$cshowList :: [Mode] -> ShowS
show :: Mode -> String
$cshow :: Mode -> String
showsPrec :: Int -> Mode -> ShowS
$cshowsPrec :: Int -> Mode -> ShowS
Show)

-- | We're either connected or we're not.
data ConnectionStatus = Connected | Disconnected deriving (ConnectionStatus -> ConnectionStatus -> Bool
(ConnectionStatus -> ConnectionStatus -> Bool)
-> (ConnectionStatus -> ConnectionStatus -> Bool)
-> Eq ConnectionStatus
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ConnectionStatus -> ConnectionStatus -> Bool
$c/= :: ConnectionStatus -> ConnectionStatus -> Bool
== :: ConnectionStatus -> ConnectionStatus -> Bool
$c== :: ConnectionStatus -> ConnectionStatus -> Bool
Eq)

type ThreadInterface = MessageInterface Name PostId
type ChannelMessageInterface = MessageInterface Name ()

data ChannelListOrientation =
    ChannelListLeft
    -- ^ Show the channel list to the left of the message area.
    | ChannelListRight
    -- ^ Show the channel list to the right of the message area.
    deriving (ChannelListOrientation -> ChannelListOrientation -> Bool
(ChannelListOrientation -> ChannelListOrientation -> Bool)
-> (ChannelListOrientation -> ChannelListOrientation -> Bool)
-> Eq ChannelListOrientation
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ChannelListOrientation -> ChannelListOrientation -> Bool
$c/= :: ChannelListOrientation -> ChannelListOrientation -> Bool
== :: ChannelListOrientation -> ChannelListOrientation -> Bool
$c== :: ChannelListOrientation -> ChannelListOrientation -> Bool
Eq, Int -> ChannelListOrientation -> ShowS
[ChannelListOrientation] -> ShowS
ChannelListOrientation -> String
(Int -> ChannelListOrientation -> ShowS)
-> (ChannelListOrientation -> String)
-> ([ChannelListOrientation] -> ShowS)
-> Show ChannelListOrientation
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ChannelListOrientation] -> ShowS
$cshowList :: [ChannelListOrientation] -> ShowS
show :: ChannelListOrientation -> String
$cshow :: ChannelListOrientation -> String
showsPrec :: Int -> ChannelListOrientation -> ShowS
$cshowsPrec :: Int -> ChannelListOrientation -> ShowS
Show)

data ThreadOrientation =
    ThreadBelow
    -- ^ Show the thread below the channel message area.
    | ThreadAbove
    -- ^ Show the thread above the channel message area.
    | ThreadLeft
    -- ^ Show the thread to the left of the channel message area.
    | ThreadRight
    -- ^ Show the thread to the right of the channel message area.
    deriving (ThreadOrientation -> ThreadOrientation -> Bool
(ThreadOrientation -> ThreadOrientation -> Bool)
-> (ThreadOrientation -> ThreadOrientation -> Bool)
-> Eq ThreadOrientation
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ThreadOrientation -> ThreadOrientation -> Bool
$c/= :: ThreadOrientation -> ThreadOrientation -> Bool
== :: ThreadOrientation -> ThreadOrientation -> Bool
$c== :: ThreadOrientation -> ThreadOrientation -> Bool
Eq, Int -> ThreadOrientation -> ShowS
[ThreadOrientation] -> ShowS
ThreadOrientation -> String
(Int -> ThreadOrientation -> ShowS)
-> (ThreadOrientation -> String)
-> ([ThreadOrientation] -> ShowS)
-> Show ThreadOrientation
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ThreadOrientation] -> ShowS
$cshowList :: [ThreadOrientation] -> ShowS
show :: ThreadOrientation -> String
$cshow :: ThreadOrientation -> String
showsPrec :: Int -> ThreadOrientation -> ShowS
$cshowsPrec :: Int -> ThreadOrientation -> ShowS
Show)

-- | This type represents the current state of our application at any
-- given time.
data ChatState =
    ChatState { ChatState -> ChatResources
_csResources :: ChatResources
              -- ^ Global application-wide resources that don't change
              -- much.
              , ChatState -> Maybe (BrickEvent Name MHEvent)
_csLastMouseDownEvent :: Maybe (Brick.BrickEvent Name MHEvent)
              -- ^ The most recent mouse click event we got. We reset
              -- this on mouse up so we can ignore clicks whenever this
              -- is already set.
              , ChatState -> Maybe Int
_csVerbatimTruncateSetting :: Maybe Int
              -- ^ The current verbatim block truncation setting. This
              -- is used to toggle truncation behavior and is updated
              -- from the configTruncateVerbatimBlocks Config field.
              , ChatState -> HashMap TeamId TeamState
_csTeams :: HashMap TeamId TeamState
              -- ^ The state for each team that we are in.
              , ChatState -> Zipper () TeamId
_csTeamZipper :: Z.Zipper () TeamId
              -- ^ The list of teams we can cycle through.
              , ChatState -> ChannelListOrientation
_csChannelListOrientation :: ChannelListOrientation
              -- ^ The orientation of the channel list.
              , ChatState -> User
_csMe :: User
              -- ^ The authenticated user.
              , ChatState -> ClientChannels
_csChannels :: ClientChannels
              -- ^ The channels that we are showing, including their
              -- message lists.
              , ChatState -> HashMap TeamId (Set ChannelListGroupLabel)
_csHiddenChannelGroups :: HM.HashMap TeamId (Set ChannelListGroupLabel)
              -- ^ The set of channel list groups that are currently
              -- collapsed in the sidebar.
              , ChatState -> HashMap PostId Message
_csPostMap :: HashMap PostId Message
              -- ^ The map of post IDs to messages. This allows us to
              -- access messages by ID without having to linearly scan
              -- channel message lists.
              , ChatState -> Users
_csUsers :: Users
              -- ^ All of the users we know about.
              , ChatState -> TimeZoneSeries
_timeZone :: TimeZoneSeries
              -- ^ The client time zone.
              , ChatState -> ConnectionStatus
_csConnectionStatus :: ConnectionStatus
              -- ^ Our view of the connection status.
              , ChatState -> Maybe (Maybe Int)
_csWorkerIsBusy :: Maybe (Maybe Int)
              -- ^ Whether the async worker thread is busy, and its
              -- queue length if so.
              , ChatState -> Maybe ClientConfig
_csClientConfig :: Maybe ClientConfig
              -- ^ The Mattermost client configuration, as we understand it.
              , ChatState -> InputHistory
_csInputHistory :: InputHistory
              -- ^ The map of per-channel input history for the
              -- application. We don't distribute the per-channel
              -- history into the per-channel states (like we do
              -- for other per-channel state) since keeping it
              -- under the InputHistory banner lets us use a nicer
              -- startup/shutdown disk file management API.
              , ChatState -> GlobalEditState
_csGlobalEditState :: GlobalEditState
              -- ^ Bits of global state common to all editors.
              }

-- | All application state specific to a team, along with state specific
-- to our user interface's presentation of that team. We include the
-- UI state relevant to the team so that we can easily switch which
-- team the UI is presenting without having to reinitialize the UI from
-- the new team. This allows the user to be engaged in just about any
-- application activity while viewing a team, switch to another team,
-- and return to the original team and resume what they were doing, all
-- without us doing any work.
data TeamState =
    TeamState { TeamState -> Zipper ChannelListGroup ChannelListEntry
_tsFocus :: Z.Zipper ChannelListGroup ChannelListEntry
              -- ^ The channel sidebar zipper that tracks which channel
              -- is selected.
              , TeamState -> Team
_tsTeam :: Team
              -- ^ The team data.
              , TeamState -> Maybe ChannelId
_tsRecentChannel :: Maybe ChannelId
              -- ^ The most recently-selected channel, if any.
              , TeamState -> Maybe ChannelId
_tsReturnChannel :: Maybe ChannelId
              -- ^ The channel to return to after visiting one or more
              -- unread channels.
              , TeamState -> NonemptyStack Mode
_tsModeStack :: NonemptyStack Mode
              -- ^ The current application mode stack when viewing this
              -- team. This is used to dispatch to different rendering
              -- and event handling routines. The current mode is always
              -- in at the top of the stack.
              , TeamState -> ChannelSelectState
_tsChannelSelectState :: ChannelSelectState
              -- ^ The state of the user's input and selection for
              -- channel selection mode.
              , TeamState -> Maybe PendingChannelChange
_tsPendingChannelChange :: Maybe PendingChannelChange
              -- ^ A pending channel change that we need to apply once
              -- the channel in question is available. We set this up
              -- when we need to change to a channel in the sidebar, but
              -- it isn't even there yet because we haven't loaded its
              -- metadata.
              , TeamState
-> Maybe
     (Message, TabbedWindow ChatState MH Name ViewMessageWindowTab)
_tsViewedMessage :: Maybe (Message, TabbedWindow ChatState MH Name ViewMessageWindowTab)
              -- ^ Set when the ViewMessage mode is active. The message
              -- being viewed. Note that this stores a message, not
              -- a message ID. That's because not all messages have
              -- message IDs (e.g. client messages) and we still
              -- want to support viewing of those messages. It's the
              -- responsibility of code that uses this message to always
              -- consult the chat state for the latest *version* of any
              -- message with an ID here, to be sure that the latest
              -- version is used (e.g. if it gets edited, etc.).
              , TeamState -> PostListWindowState
_tsPostListWindow :: PostListWindowState
              -- ^ The state of the post list window.
              , TeamState -> ListWindowState UserInfo UserSearchScope
_tsUserListWindow :: ListWindowState UserInfo UserSearchScope
              -- ^ The state of the user list window.
              , TeamState -> ListWindowState Channel ChannelSearchScope
_tsChannelListWindow :: ListWindowState Channel ChannelSearchScope
              -- ^ The state of the user list window.
              , TeamState -> Maybe (Form ChannelNotifyProps MHEvent Name)
_tsNotifyPrefs :: Maybe (Form ChannelNotifyProps MHEvent Name)
              -- ^ A form for editing the notification preferences for
              -- the current channel. This is set when entering
              -- EditNotifyPrefs mode and updated when the user
              -- changes the form state.
              , TeamState -> ChannelTopicDialogState
_tsChannelTopicDialog :: ChannelTopicDialogState
              -- ^ The state for the interactive channel topic editor
              -- window.
              , TeamState -> ListWindowState (Bool, Text) ()
_tsReactionEmojiListWindow :: ListWindowState (Bool, T.Text) ()
              -- ^ The state of the reaction emoji list window.
              , TeamState -> ListWindowState InternalTheme ()
_tsThemeListWindow :: ListWindowState InternalTheme ()
              -- ^ The state of the theme list window.
              , TeamState -> ChannelListSorting
_tsChannelListSorting :: ChannelListSorting
              -- ^ How to sort channels in this team's channel list
              -- groups
              , TeamState -> Maybe ThreadInterface
_tsThreadInterface :: Maybe ThreadInterface
              -- ^ The thread interface for this team for participating
              -- in a single thread
              , TeamState -> MessageInterfaceFocus
_tsMessageInterfaceFocus :: MessageInterfaceFocus
              -- ^ Which message interface is focused for editing input
              }

data MessageInterfaceFocus =
    FocusThread
    | FocusCurrentChannel
    deriving (MessageInterfaceFocus -> MessageInterfaceFocus -> Bool
(MessageInterfaceFocus -> MessageInterfaceFocus -> Bool)
-> (MessageInterfaceFocus -> MessageInterfaceFocus -> Bool)
-> Eq MessageInterfaceFocus
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: MessageInterfaceFocus -> MessageInterfaceFocus -> Bool
$c/= :: MessageInterfaceFocus -> MessageInterfaceFocus -> Bool
== :: MessageInterfaceFocus -> MessageInterfaceFocus -> Bool
$c== :: MessageInterfaceFocus -> MessageInterfaceFocus -> Bool
Eq, Int -> MessageInterfaceFocus -> ShowS
[MessageInterfaceFocus] -> ShowS
MessageInterfaceFocus -> String
(Int -> MessageInterfaceFocus -> ShowS)
-> (MessageInterfaceFocus -> String)
-> ([MessageInterfaceFocus] -> ShowS)
-> Show MessageInterfaceFocus
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [MessageInterfaceFocus] -> ShowS
$cshowList :: [MessageInterfaceFocus] -> ShowS
show :: MessageInterfaceFocus -> String
$cshow :: MessageInterfaceFocus -> String
showsPrec :: Int -> MessageInterfaceFocus -> ShowS
$cshowsPrec :: Int -> MessageInterfaceFocus -> ShowS
Show)

messageInterfaceFocusList :: [MessageInterfaceFocus]
messageInterfaceFocusList :: [MessageInterfaceFocus]
messageInterfaceFocusList =
    [ MessageInterfaceFocus
FocusCurrentChannel
    , MessageInterfaceFocus
FocusThread
    ]

messageInterfaceFocusNext :: TeamState -> TeamState
messageInterfaceFocusNext :: TeamState -> TeamState
messageInterfaceFocusNext = [MessageInterfaceFocus] -> TeamState -> TeamState
messageInterfaceFocusWith [MessageInterfaceFocus]
messageInterfaceFocusList

messageInterfaceFocusPrev :: TeamState -> TeamState
messageInterfaceFocusPrev :: TeamState -> TeamState
messageInterfaceFocusPrev = [MessageInterfaceFocus] -> TeamState -> TeamState
messageInterfaceFocusWith ([MessageInterfaceFocus] -> [MessageInterfaceFocus]
forall a. [a] -> [a]
reverse [MessageInterfaceFocus]
messageInterfaceFocusList)

messageInterfaceFocusWith :: [MessageInterfaceFocus] -> TeamState -> TeamState
messageInterfaceFocusWith :: [MessageInterfaceFocus] -> TeamState -> TeamState
messageInterfaceFocusWith [MessageInterfaceFocus]
lst TeamState
ts =
    let next :: MessageInterfaceFocus
next = Maybe MessageInterfaceFocus -> MessageInterfaceFocus
forall a. HasCallStack => Maybe a -> a
fromJust (Maybe MessageInterfaceFocus -> MessageInterfaceFocus)
-> Maybe MessageInterfaceFocus -> MessageInterfaceFocus
forall a b. (a -> b) -> a -> b
$ MessageInterfaceFocus
-> [MessageInterfaceFocus] -> Maybe MessageInterfaceFocus
forall a. Eq a => a -> [a] -> Maybe a
cycleElemAfter MessageInterfaceFocus
cur [MessageInterfaceFocus]
lst
        cur :: MessageInterfaceFocus
cur = TeamState -> MessageInterfaceFocus
_tsMessageInterfaceFocus TeamState
ts
        noThread :: Bool
noThread = Maybe ThreadInterface -> Bool
forall a. Maybe a -> Bool
isNothing (Maybe ThreadInterface -> Bool) -> Maybe ThreadInterface -> Bool
forall a b. (a -> b) -> a -> b
$ TeamState -> Maybe ThreadInterface
_tsThreadInterface TeamState
ts
        newFocus :: MessageInterfaceFocus
newFocus = if MessageInterfaceFocus
next MessageInterfaceFocus -> MessageInterfaceFocus -> Bool
forall a. Eq a => a -> a -> Bool
== MessageInterfaceFocus
FocusThread Bool -> Bool -> Bool
&& Bool
noThread
                   then Maybe MessageInterfaceFocus -> MessageInterfaceFocus
forall a. HasCallStack => Maybe a -> a
fromJust (Maybe MessageInterfaceFocus -> MessageInterfaceFocus)
-> Maybe MessageInterfaceFocus -> MessageInterfaceFocus
forall a b. (a -> b) -> a -> b
$ MessageInterfaceFocus
-> [MessageInterfaceFocus] -> Maybe MessageInterfaceFocus
forall a. Eq a => a -> [a] -> Maybe a
cycleElemAfter MessageInterfaceFocus
FocusThread [MessageInterfaceFocus]
lst
                   else MessageInterfaceFocus
next
    in TeamState
ts { _tsMessageInterfaceFocus :: MessageInterfaceFocus
_tsMessageInterfaceFocus = MessageInterfaceFocus
newFocus }

cycleElemAfter :: (Eq a) => a -> [a] -> Maybe a
cycleElemAfter :: a -> [a] -> Maybe a
cycleElemAfter a
e [a]
es =
    if a
e a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [a]
es
    then Maybe a
forall a. Maybe a
Nothing
    else a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> a -> Maybe a
forall a b. (a -> b) -> a -> b
$ [a] -> a
forall a. [a] -> a
head ([a] -> a) -> [a] -> a
forall a b. (a -> b) -> a -> b
$ Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
drop Int
1 ([a] -> [a]) -> [a] -> [a]
forall a b. (a -> b) -> a -> b
$ (a -> Bool) -> [a] -> [a]
forall a. (a -> Bool) -> [a] -> [a]
dropWhile (a -> a -> Bool
forall a. Eq a => a -> a -> Bool
/= a
e) ([a] -> [a]) -> [a] -> [a]
forall a b. (a -> b) -> a -> b
$ [a] -> [a]
forall a. [a] -> [a]
cycle [a]
es

-- | Handles for the View Message window's tabs.
data ViewMessageWindowTab =
    VMTabMessage
    -- ^ The message tab.
    | VMTabReactions
    -- ^ The reactions tab.
    deriving (ViewMessageWindowTab -> ViewMessageWindowTab -> Bool
(ViewMessageWindowTab -> ViewMessageWindowTab -> Bool)
-> (ViewMessageWindowTab -> ViewMessageWindowTab -> Bool)
-> Eq ViewMessageWindowTab
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ViewMessageWindowTab -> ViewMessageWindowTab -> Bool
$c/= :: ViewMessageWindowTab -> ViewMessageWindowTab -> Bool
== :: ViewMessageWindowTab -> ViewMessageWindowTab -> Bool
$c== :: ViewMessageWindowTab -> ViewMessageWindowTab -> Bool
Eq, Int -> ViewMessageWindowTab -> ShowS
[ViewMessageWindowTab] -> ShowS
ViewMessageWindowTab -> String
(Int -> ViewMessageWindowTab -> ShowS)
-> (ViewMessageWindowTab -> String)
-> ([ViewMessageWindowTab] -> ShowS)
-> Show ViewMessageWindowTab
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ViewMessageWindowTab] -> ShowS
$cshowList :: [ViewMessageWindowTab] -> ShowS
show :: ViewMessageWindowTab -> String
$cshow :: ViewMessageWindowTab -> String
showsPrec :: Int -> ViewMessageWindowTab -> ShowS
$cshowsPrec :: Int -> ViewMessageWindowTab -> ShowS
Show)

data PendingChannelChange =
    ChangeByChannelId TeamId ChannelId (Maybe (MH ()))
    | ChangeByUserId UserId

-- | Startup state information that is constructed prior to building a
-- ChatState.
data StartupStateInfo =
    StartupStateInfo { StartupStateInfo -> ChatResources
startupStateResources      :: ChatResources
                     , StartupStateInfo -> User
startupStateConnectedUser  :: User
                     , StartupStateInfo -> HashMap TeamId TeamState
startupStateTeams          :: HM.HashMap TeamId TeamState
                     , StartupStateInfo -> TimeZoneSeries
startupStateTimeZone       :: TimeZoneSeries
                     , StartupStateInfo -> InputHistory
startupStateInitialHistory :: InputHistory
                     , StartupStateInfo -> TeamId
startupStateInitialTeam    :: TeamId
                     }

-- | The state of the channel topic editor window.
data ChannelTopicDialogState =
    ChannelTopicDialogState { ChannelTopicDialogState -> Editor Text Name
_channelTopicDialogEditor :: Editor T.Text Name
                            -- ^ The topic string editor state.
                            , ChannelTopicDialogState -> FocusRing Name
_channelTopicDialogFocus :: FocusRing Name
                            -- ^ The window focus state (editor/buttons)
                            }

sortTeams :: [Team] -> [Team]
sortTeams :: [Team] -> [Team]
sortTeams = (Team -> Team -> Ordering) -> [Team] -> [Team]
forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy (Text -> Text -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (Text -> Text -> Ordering)
-> (Team -> Text) -> Team -> Team -> Ordering
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` (Text -> Text
T.strip (Text -> Text) -> (Team -> Text) -> Team -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UserText -> Text
sanitizeUserText (UserText -> Text) -> (Team -> UserText) -> Team -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Team -> UserText
teamName))

matchesTeam :: T.Text -> Team -> Bool
matchesTeam :: Text -> Team -> Bool
matchesTeam Text
tName Team
t =
    let normalizeUserText :: UserText -> Text
normalizeUserText = Text -> Text
normalize (Text -> Text) -> (UserText -> Text) -> UserText -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UserText -> Text
sanitizeUserText
        normalize :: Text -> Text
normalize = Text -> Text
T.strip (Text -> Text) -> (Text -> Text) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
T.toLower
        urlName :: Text
urlName = UserText -> Text
normalizeUserText (UserText -> Text) -> UserText -> Text
forall a b. (a -> b) -> a -> b
$ Team -> UserText
teamName Team
t
        displayName :: Text
displayName = UserText -> Text
normalizeUserText (UserText -> Text) -> UserText -> Text
forall a b. (a -> b) -> a -> b
$ Team -> UserText
teamDisplayName Team
t
    in Text -> Text
normalize Text
tName Text -> [Text] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Text
displayName, Text
urlName]

mkTeamZipper :: HM.HashMap TeamId TeamState -> Z.Zipper () TeamId
mkTeamZipper :: HashMap TeamId TeamState -> Zipper () TeamId
mkTeamZipper HashMap TeamId TeamState
m =
    let sortedTeams :: [Team]
sortedTeams = [Team] -> [Team]
sortTeams ([Team] -> [Team]) -> [Team] -> [Team]
forall a b. (a -> b) -> a -> b
$ TeamState -> Team
_tsTeam (TeamState -> Team) -> [TeamState] -> [Team]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> HashMap TeamId TeamState -> [TeamState]
forall k v. HashMap k v -> [v]
HM.elems HashMap TeamId TeamState
m
    in [TeamId] -> Zipper () TeamId
mkTeamZipperFromIds ([TeamId] -> Zipper () TeamId) -> [TeamId] -> Zipper () TeamId
forall a b. (a -> b) -> a -> b
$ Team -> TeamId
teamId (Team -> TeamId) -> [Team] -> [TeamId]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Team]
sortedTeams

mkTeamZipperFromIds :: [TeamId] -> Z.Zipper () TeamId
mkTeamZipperFromIds :: [TeamId] -> Zipper () TeamId
mkTeamZipperFromIds [TeamId]
tIds = [((), [TeamId])] -> Zipper () TeamId
forall b a. Eq b => [(a, [b])] -> Zipper a b
Z.fromList [((), [TeamId]
tIds)]

teamZipperIds :: Z.Zipper () TeamId -> [TeamId]
teamZipperIds :: Zipper () TeamId -> [TeamId]
teamZipperIds = [[TeamId]] -> [TeamId]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[TeamId]] -> [TeamId])
-> (Zipper () TeamId -> [[TeamId]]) -> Zipper () TeamId -> [TeamId]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (((), [TeamId]) -> [TeamId]) -> [((), [TeamId])] -> [[TeamId]]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((), [TeamId]) -> [TeamId]
forall a b. (a, b) -> b
snd ([((), [TeamId])] -> [[TeamId]])
-> (Zipper () TeamId -> [((), [TeamId])])
-> Zipper () TeamId
-> [[TeamId]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Zipper () TeamId -> [((), [TeamId])]
forall a b. Zipper a b -> [(a, [b])]
Z.toList

-- | The state of channel selection mode.
data ChannelSelectState =
    ChannelSelectState { ChannelSelectState -> Editor Text Name
_channelSelectInput :: Editor Text Name
                       , ChannelSelectState -> Zipper ChannelListGroup ChannelSelectMatch
_channelSelectMatches :: Z.Zipper ChannelListGroup ChannelSelectMatch
                       }

emptyChannelSelectState :: TeamId -> ChannelSelectState
emptyChannelSelectState :: TeamId -> ChannelSelectState
emptyChannelSelectState TeamId
tId =
    ChannelSelectState :: Editor Text Name
-> Zipper ChannelListGroup ChannelSelectMatch -> ChannelSelectState
ChannelSelectState { _channelSelectInput :: Editor Text Name
_channelSelectInput = Name -> Maybe Int -> Text -> Editor Text Name
forall a n.
GenericTextZipper a =>
n -> Maybe Int -> a -> Editor a n
editor (TeamId -> Name
ChannelSelectInput TeamId
tId) (Int -> Maybe Int
forall a. a -> Maybe a
Just Int
1) Text
""
                       , _channelSelectMatches :: Zipper ChannelListGroup ChannelSelectMatch
_channelSelectMatches = [(ChannelListGroup, [ChannelSelectMatch])]
-> Zipper ChannelListGroup ChannelSelectMatch
forall b a. Eq b => [(a, [b])] -> Zipper a b
Z.fromList []
                       }

-- | The state of the post list window.
data PostListWindowState =
    PostListWindowState { PostListWindowState -> Messages
_postListPosts    :: Messages
                         , PostListWindowState -> Maybe PostId
_postListSelected :: Maybe PostId
                         }

data InternalTheme =
    InternalTheme { InternalTheme -> Text
internalThemeName :: Text
                  , InternalTheme -> Theme
internalTheme :: Theme
                  , InternalTheme -> Text
internalThemeDesc :: Text
                  }

-- | The state of the search result list window. Type 'a' is the type
-- of data in the list. Type 'b' is the search scope type.
data ListWindowState a b =
    ListWindowState { ListWindowState a b -> List Name a
_listWindowSearchResults :: List Name a
                     -- ^ The list of search results currently shown in
                     -- the window.
                     , ListWindowState a b -> Editor Text Name
_listWindowSearchInput :: Editor Text Name
                     -- ^ The editor for the window's search input.
                     , ListWindowState a b -> b
_listWindowSearchScope :: b
                     -- ^ The window's current search scope.
                     , ListWindowState a b -> Bool
_listWindowSearching :: Bool
                     -- ^ Whether a search is in progress (i.e. whether
                     -- we are currently awaiting a response from a
                     -- search query to the server).
                     , ListWindowState a b -> a -> MH Bool
_listWindowEnterHandler :: a -> MH Bool
                     -- ^ The handler to invoke on the selected element
                     -- when the user presses Enter.
                     , ListWindowState a b -> Vector a -> List Name a
_listWindowNewList :: Vec.Vector a -> List Name a
                     -- ^ The function to build a new brick List from a
                     -- vector of search results.
                     , ListWindowState a b -> b -> Session -> Text -> IO (Vector a)
_listWindowFetchResults :: b -> Session -> Text -> IO (Vec.Vector a)
                     -- ^ The function to call to issue a search query
                     -- to the server.
                     , ListWindowState a b -> Maybe Int
_listWindowRecordCount :: Maybe Int
                     -- ^ The total number of available records, if known.
                     }

-- | The scope for searching for users in a user list window.
data UserSearchScope =
    ChannelMembers ChannelId TeamId
    | ChannelNonMembers ChannelId TeamId
    | AllUsers (Maybe TeamId)

-- | The scope for searching for channels to join.
data ChannelSearchScope =
    AllChannels

-- | Actions that can be sent on the websocket to the server.
data WebsocketAction =
    UserTyping UTCTime ChannelId (Maybe PostId) -- ^ user typing in the input box
    deriving (ReadPrec [WebsocketAction]
ReadPrec WebsocketAction
Int -> ReadS WebsocketAction
ReadS [WebsocketAction]
(Int -> ReadS WebsocketAction)
-> ReadS [WebsocketAction]
-> ReadPrec WebsocketAction
-> ReadPrec [WebsocketAction]
-> Read WebsocketAction
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [WebsocketAction]
$creadListPrec :: ReadPrec [WebsocketAction]
readPrec :: ReadPrec WebsocketAction
$creadPrec :: ReadPrec WebsocketAction
readList :: ReadS [WebsocketAction]
$creadList :: ReadS [WebsocketAction]
readsPrec :: Int -> ReadS WebsocketAction
$creadsPrec :: Int -> ReadS WebsocketAction
Read, Int -> WebsocketAction -> ShowS
[WebsocketAction] -> ShowS
WebsocketAction -> String
(Int -> WebsocketAction -> ShowS)
-> (WebsocketAction -> String)
-> ([WebsocketAction] -> ShowS)
-> Show WebsocketAction
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [WebsocketAction] -> ShowS
$cshowList :: [WebsocketAction] -> ShowS
show :: WebsocketAction -> String
$cshow :: WebsocketAction -> String
showsPrec :: Int -> WebsocketAction -> ShowS
$cshowsPrec :: Int -> WebsocketAction -> ShowS
Show, WebsocketAction -> WebsocketAction -> Bool
(WebsocketAction -> WebsocketAction -> Bool)
-> (WebsocketAction -> WebsocketAction -> Bool)
-> Eq WebsocketAction
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: WebsocketAction -> WebsocketAction -> Bool
$c/= :: WebsocketAction -> WebsocketAction -> Bool
== :: WebsocketAction -> WebsocketAction -> Bool
$c== :: WebsocketAction -> WebsocketAction -> Bool
Eq, Eq WebsocketAction
Eq WebsocketAction
-> (WebsocketAction -> WebsocketAction -> Ordering)
-> (WebsocketAction -> WebsocketAction -> Bool)
-> (WebsocketAction -> WebsocketAction -> Bool)
-> (WebsocketAction -> WebsocketAction -> Bool)
-> (WebsocketAction -> WebsocketAction -> Bool)
-> (WebsocketAction -> WebsocketAction -> WebsocketAction)
-> (WebsocketAction -> WebsocketAction -> WebsocketAction)
-> Ord WebsocketAction
WebsocketAction -> WebsocketAction -> Bool
WebsocketAction -> WebsocketAction -> Ordering
WebsocketAction -> WebsocketAction -> WebsocketAction
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: WebsocketAction -> WebsocketAction -> WebsocketAction
$cmin :: WebsocketAction -> WebsocketAction -> WebsocketAction
max :: WebsocketAction -> WebsocketAction -> WebsocketAction
$cmax :: WebsocketAction -> WebsocketAction -> WebsocketAction
>= :: WebsocketAction -> WebsocketAction -> Bool
$c>= :: WebsocketAction -> WebsocketAction -> Bool
> :: WebsocketAction -> WebsocketAction -> Bool
$c> :: WebsocketAction -> WebsocketAction -> Bool
<= :: WebsocketAction -> WebsocketAction -> Bool
$c<= :: WebsocketAction -> WebsocketAction -> Bool
< :: WebsocketAction -> WebsocketAction -> Bool
$c< :: WebsocketAction -> WebsocketAction -> Bool
compare :: WebsocketAction -> WebsocketAction -> Ordering
$ccompare :: WebsocketAction -> WebsocketAction -> Ordering
$cp1Ord :: Eq WebsocketAction
Ord)

-- * MH Monad

-- | Logging context information, in the event that metadata should
-- accompany a log message.
data LogContext =
    LogContext { LogContext -> Maybe ChannelId
logContextChannelId :: Maybe ChannelId
               }
               deriving (LogContext -> LogContext -> Bool
(LogContext -> LogContext -> Bool)
-> (LogContext -> LogContext -> Bool) -> Eq LogContext
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: LogContext -> LogContext -> Bool
$c/= :: LogContext -> LogContext -> Bool
== :: LogContext -> LogContext -> Bool
$c== :: LogContext -> LogContext -> Bool
Eq, Int -> LogContext -> ShowS
[LogContext] -> ShowS
LogContext -> String
(Int -> LogContext -> ShowS)
-> (LogContext -> String)
-> ([LogContext] -> ShowS)
-> Show LogContext
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [LogContext] -> ShowS
$cshowList :: [LogContext] -> ShowS
show :: LogContext -> String
$cshow :: LogContext -> String
showsPrec :: Int -> LogContext -> ShowS
$cshowsPrec :: Int -> LogContext -> ShowS
Show)

-- | A user fetching strategy.
data UserFetch =
    UserFetchById UserId
    -- ^ Fetch the user with the specified ID.
    | UserFetchByUsername Text
    -- ^ Fetch the user with the specified username.
    | UserFetchByNickname Text
    -- ^ Fetch the user with the specified nickname.
    deriving (UserFetch -> UserFetch -> Bool
(UserFetch -> UserFetch -> Bool)
-> (UserFetch -> UserFetch -> Bool) -> Eq UserFetch
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: UserFetch -> UserFetch -> Bool
$c/= :: UserFetch -> UserFetch -> Bool
== :: UserFetch -> UserFetch -> Bool
$c== :: UserFetch -> UserFetch -> Bool
Eq, Int -> UserFetch -> ShowS
[UserFetch] -> ShowS
UserFetch -> String
(Int -> UserFetch -> ShowS)
-> (UserFetch -> String)
-> ([UserFetch] -> ShowS)
-> Show UserFetch
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [UserFetch] -> ShowS
$cshowList :: [UserFetch] -> ShowS
show :: UserFetch -> String
$cshow :: UserFetch -> String
showsPrec :: Int -> UserFetch -> ShowS
$cshowsPrec :: Int -> UserFetch -> ShowS
Show)

data MHState =
    MHState { MHState -> ChatState
mhCurrentState :: ChatState
            , MHState -> ChatState -> EventM Name (Next ChatState)
mhNextAction :: ChatState -> EventM Name (Next ChatState)
            , MHState -> [UserFetch]
mhUsersToFetch :: [UserFetch]
            , MHState -> Maybe [UserId]
mhPendingStatusList :: Maybe [UserId]
            }

-- | A value of type 'MH' @a@ represents a computation that can
-- manipulate the application state and also request that the
-- application quit
newtype MH a =
    MH { MH a -> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a
fromMH :: R.ReaderT (Maybe LogContext) (St.StateT MHState (EventM Name)) a }

-- | Use a modified logging context for the duration of the specified MH
-- action.
withLogContext :: (Maybe LogContext -> Maybe LogContext) -> MH a -> MH a
withLogContext :: (Maybe LogContext -> Maybe LogContext) -> MH a -> MH a
withLogContext Maybe LogContext -> Maybe LogContext
modifyContext MH a
act =
    ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a -> MH a
forall a.
ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a -> MH a
MH (ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a
 -> MH a)
-> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a
-> MH a
forall a b. (a -> b) -> a -> b
$ (Maybe LogContext -> Maybe LogContext)
-> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a
-> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a
forall r' r (m :: * -> *) a.
(r' -> r) -> ReaderT r m a -> ReaderT r' m a
R.withReaderT Maybe LogContext -> Maybe LogContext
modifyContext (MH a -> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a
forall a.
MH a -> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a
fromMH MH a
act)

withLogContextChannelId :: ChannelId -> MH a -> MH a
withLogContextChannelId :: ChannelId -> MH a -> MH a
withLogContextChannelId ChannelId
cId MH a
act =
    let f :: Maybe LogContext -> Maybe LogContext
f Maybe LogContext
Nothing = LogContext -> Maybe LogContext
forall a. a -> Maybe a
Just (LogContext -> Maybe LogContext) -> LogContext -> Maybe LogContext
forall a b. (a -> b) -> a -> b
$ Maybe ChannelId -> LogContext
LogContext (ChannelId -> Maybe ChannelId
forall a. a -> Maybe a
Just ChannelId
cId)
        f (Just LogContext
c) = LogContext -> Maybe LogContext
forall a. a -> Maybe a
Just (LogContext -> Maybe LogContext) -> LogContext -> Maybe LogContext
forall a b. (a -> b) -> a -> b
$ LogContext
c { logContextChannelId :: Maybe ChannelId
logContextChannelId = ChannelId -> Maybe ChannelId
forall a. a -> Maybe a
Just ChannelId
cId }
    in (Maybe LogContext -> Maybe LogContext) -> MH a -> MH a
forall a. (Maybe LogContext -> Maybe LogContext) -> MH a -> MH a
withLogContext Maybe LogContext -> Maybe LogContext
f MH a
act

-- | Get the current logging context.
getLogContext :: MH (Maybe LogContext)
getLogContext :: MH (Maybe LogContext)
getLogContext = ReaderT
  (Maybe LogContext)
  (StateT MHState (EventM Name))
  (Maybe LogContext)
-> MH (Maybe LogContext)
forall a.
ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a -> MH a
MH ReaderT
  (Maybe LogContext)
  (StateT MHState (EventM Name))
  (Maybe LogContext)
forall r (m :: * -> *). MonadReader r m => m r
R.ask

-- | Log a message.
mhLog :: LogCategory -> Text -> MH ()
mhLog :: LogCategory -> Text -> MH ()
mhLog LogCategory
cat Text
msg = do
    LogCategory -> Text -> IO ()
logger <- MH (LogCategory -> Text -> IO ())
mhGetIOLogger
    IO () -> MH ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> MH ()) -> IO () -> MH ()
forall a b. (a -> b) -> a -> b
$ LogCategory -> Text -> IO ()
logger LogCategory
cat Text
msg

-- | Get a logger suitable for use in IO. The logger always logs using
-- the MH monad log context at the time of the call to mhGetIOLogger.
mhGetIOLogger :: MH (LogCategory -> Text -> IO ())
mhGetIOLogger :: MH (LogCategory -> Text -> IO ())
mhGetIOLogger = do
    Maybe LogContext
ctx <- MH (Maybe LogContext)
getLogContext
    LogManager
mgr <- Getting LogManager ChatState LogManager -> MH LogManager
forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use ((ChatState -> LogManager) -> SimpleGetter ChatState LogManager
forall s a. (s -> a) -> SimpleGetter s a
to (ChatResources -> LogManager
_crLogManager (ChatResources -> LogManager)
-> (ChatState -> ChatResources) -> ChatState -> LogManager
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ChatState -> ChatResources
_csResources))
    (LogCategory -> Text -> IO ()) -> MH (LogCategory -> Text -> IO ())
forall (m :: * -> *) a. Monad m => a -> m a
return ((LogCategory -> Text -> IO ())
 -> MH (LogCategory -> Text -> IO ()))
-> (LogCategory -> Text -> IO ())
-> MH (LogCategory -> Text -> IO ())
forall a b. (a -> b) -> a -> b
$ LogManager -> Maybe LogContext -> LogCategory -> Text -> IO ()
ioLogWithManager LogManager
mgr Maybe LogContext
ctx

ioLogWithManager :: LogManager -> Maybe LogContext -> LogCategory -> Text -> IO ()
ioLogWithManager :: LogManager -> Maybe LogContext -> LogCategory -> Text -> IO ()
ioLogWithManager LogManager
mgr Maybe LogContext
ctx LogCategory
cat Text
msg = do
    UTCTime
now <- IO UTCTime
getCurrentTime
    let lm :: LogMessage
lm = LogMessage :: Text -> Maybe LogContext -> LogCategory -> UTCTime -> LogMessage
LogMessage { logMessageText :: Text
logMessageText = Text
msg
                        , logMessageContext :: Maybe LogContext
logMessageContext = Maybe LogContext
ctx
                        , logMessageCategory :: LogCategory
logMessageCategory = LogCategory
cat
                        , logMessageTimestamp :: UTCTime
logMessageTimestamp = UTCTime
now
                        }
    LogManager -> LogMessage -> IO ()
sendLogMessage LogManager
mgr LogMessage
lm

-- | Run an 'MM' computation, choosing whether to continue or halt based
-- on the resulting
runMHEvent :: ChatState -> MH () -> EventM Name (Next ChatState)
runMHEvent :: ChatState -> MH () -> EventM Name (Next ChatState)
runMHEvent ChatState
st (MH ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) ()
mote) = do
  let mhSt :: MHState
mhSt = MHState :: ChatState
-> (ChatState -> EventM Name (Next ChatState))
-> [UserFetch]
-> Maybe [UserId]
-> MHState
MHState { mhCurrentState :: ChatState
mhCurrentState = ChatState
st
                     , mhNextAction :: ChatState -> EventM Name (Next ChatState)
mhNextAction = ChatState -> EventM Name (Next ChatState)
forall s n. s -> EventM n (Next s)
Brick.continue
                     , mhUsersToFetch :: [UserFetch]
mhUsersToFetch = []
                     , mhPendingStatusList :: Maybe [UserId]
mhPendingStatusList = Maybe [UserId]
forall a. Maybe a
Nothing
                     }
  ((), MHState
st') <- StateT MHState (EventM Name) ()
-> MHState -> EventM Name ((), MHState)
forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
St.runStateT (ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) ()
-> Maybe LogContext -> StateT MHState (EventM Name) ()
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
R.runReaderT ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) ()
mote Maybe LogContext
forall a. Maybe a
Nothing) MHState
mhSt
  (MHState -> ChatState -> EventM Name (Next ChatState)
mhNextAction MHState
st') (MHState -> ChatState
mhCurrentState MHState
st')

scheduleUserFetches :: [UserFetch] -> MH ()
scheduleUserFetches :: [UserFetch] -> MH ()
scheduleUserFetches [UserFetch]
fs = ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) ()
-> MH ()
forall a.
ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a -> MH a
MH (ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) ()
 -> MH ())
-> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) ()
-> MH ()
forall a b. (a -> b) -> a -> b
$ do
    (MHState -> MHState)
-> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
St.modify ((MHState -> MHState)
 -> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) ())
-> (MHState -> MHState)
-> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) ()
forall a b. (a -> b) -> a -> b
$ \MHState
s -> MHState
s { mhUsersToFetch :: [UserFetch]
mhUsersToFetch = [UserFetch]
fs [UserFetch] -> [UserFetch] -> [UserFetch]
forall a. Semigroup a => a -> a -> a
<> MHState -> [UserFetch]
mhUsersToFetch MHState
s }

scheduleUserStatusFetches :: [UserId] -> MH ()
scheduleUserStatusFetches :: [UserId] -> MH ()
scheduleUserStatusFetches [UserId]
is = ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) ()
-> MH ()
forall a.
ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a -> MH a
MH (ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) ()
 -> MH ())
-> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) ()
-> MH ()
forall a b. (a -> b) -> a -> b
$ do
    (MHState -> MHState)
-> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
St.modify ((MHState -> MHState)
 -> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) ())
-> (MHState -> MHState)
-> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) ()
forall a b. (a -> b) -> a -> b
$ \MHState
s -> MHState
s { mhPendingStatusList :: Maybe [UserId]
mhPendingStatusList = [UserId] -> Maybe [UserId]
forall a. a -> Maybe a
Just [UserId]
is }

getScheduledUserFetches :: MH [UserFetch]
getScheduledUserFetches :: MH [UserFetch]
getScheduledUserFetches = ReaderT
  (Maybe LogContext) (StateT MHState (EventM Name)) [UserFetch]
-> MH [UserFetch]
forall a.
ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a -> MH a
MH (ReaderT
   (Maybe LogContext) (StateT MHState (EventM Name)) [UserFetch]
 -> MH [UserFetch])
-> ReaderT
     (Maybe LogContext) (StateT MHState (EventM Name)) [UserFetch]
-> MH [UserFetch]
forall a b. (a -> b) -> a -> b
$ (MHState -> [UserFetch])
-> ReaderT
     (Maybe LogContext) (StateT MHState (EventM Name)) [UserFetch]
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
St.gets MHState -> [UserFetch]
mhUsersToFetch

getScheduledUserStatusFetches :: MH (Maybe [UserId])
getScheduledUserStatusFetches :: MH (Maybe [UserId])
getScheduledUserStatusFetches = ReaderT
  (Maybe LogContext) (StateT MHState (EventM Name)) (Maybe [UserId])
-> MH (Maybe [UserId])
forall a.
ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a -> MH a
MH (ReaderT
   (Maybe LogContext) (StateT MHState (EventM Name)) (Maybe [UserId])
 -> MH (Maybe [UserId]))
-> ReaderT
     (Maybe LogContext) (StateT MHState (EventM Name)) (Maybe [UserId])
-> MH (Maybe [UserId])
forall a b. (a -> b) -> a -> b
$ (MHState -> Maybe [UserId])
-> ReaderT
     (Maybe LogContext) (StateT MHState (EventM Name)) (Maybe [UserId])
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
St.gets MHState -> Maybe [UserId]
mhPendingStatusList

-- | lift a computation in 'EventM' into 'MH'
mh :: EventM Name a -> MH a
mh :: EventM Name a -> MH a
mh = ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a -> MH a
forall a.
ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a -> MH a
MH (ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a
 -> MH a)
-> (EventM Name a
    -> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a)
-> EventM Name a
-> MH a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StateT MHState (EventM Name) a
-> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
R.lift (StateT MHState (EventM Name) a
 -> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a)
-> (EventM Name a -> StateT MHState (EventM Name) a)
-> EventM Name a
-> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. EventM Name a -> StateT MHState (EventM Name) a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
St.lift

generateUUID :: MH UUID
generateUUID :: MH UUID
generateUUID = IO UUID -> MH UUID
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO UUID
generateUUID_IO

generateUUID_IO :: IO UUID
generateUUID_IO :: IO UUID
generateUUID_IO = IO UUID
forall a. Random a => IO a
randomIO

mhHandleEventLensed :: Lens' ChatState b -> (e -> b -> EventM Name b) -> e -> MH ()
mhHandleEventLensed :: Lens' ChatState b -> (e -> b -> EventM Name b) -> e -> MH ()
mhHandleEventLensed Lens' ChatState b
ln e -> b -> EventM Name b
f e
event = ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) ()
-> MH ()
forall a.
ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a -> MH a
MH (ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) ()
 -> MH ())
-> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) ()
-> MH ()
forall a b. (a -> b) -> a -> b
$ do
    MHState
s <- ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) MHState
forall s (m :: * -> *). MonadState s m => m s
St.get
    let st :: ChatState
st = MHState -> ChatState
mhCurrentState MHState
s
    b
n <- StateT MHState (EventM Name) b
-> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
R.lift (StateT MHState (EventM Name) b
 -> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) b)
-> StateT MHState (EventM Name) b
-> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) b
forall a b. (a -> b) -> a -> b
$ EventM Name b -> StateT MHState (EventM Name) b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
St.lift (EventM Name b -> StateT MHState (EventM Name) b)
-> EventM Name b -> StateT MHState (EventM Name) b
forall a b. (a -> b) -> a -> b
$ e -> b -> EventM Name b
f e
event (ChatState
st ChatState -> Getting b ChatState b -> b
forall s a. s -> Getting a s a -> a
^. Getting b ChatState b
Lens' ChatState b
ln)
    MHState
-> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) ()
forall s (m :: * -> *). MonadState s m => s -> m ()
St.put (MHState
s { mhCurrentState :: ChatState
mhCurrentState = ChatState
st ChatState -> (ChatState -> ChatState) -> ChatState
forall a b. a -> (a -> b) -> b
& (b -> Identity b) -> ChatState -> Identity ChatState
Lens' ChatState b
ln ((b -> Identity b) -> ChatState -> Identity ChatState)
-> b -> ChatState -> ChatState
forall s t a b. ASetter s t a b -> b -> s -> t
.~ b
n })

mhHandleEventLensed' :: Lens' ChatState b -> (b -> EventM Name b) -> MH ()
mhHandleEventLensed' :: Lens' ChatState b -> (b -> EventM Name b) -> MH ()
mhHandleEventLensed' Lens' ChatState b
ln b -> EventM Name b
f = ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) ()
-> MH ()
forall a.
ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a -> MH a
MH (ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) ()
 -> MH ())
-> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) ()
-> MH ()
forall a b. (a -> b) -> a -> b
$ do
    MHState
s <- ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) MHState
forall s (m :: * -> *). MonadState s m => m s
St.get
    let st :: ChatState
st = MHState -> ChatState
mhCurrentState MHState
s
    b
n <- StateT MHState (EventM Name) b
-> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
R.lift (StateT MHState (EventM Name) b
 -> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) b)
-> StateT MHState (EventM Name) b
-> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) b
forall a b. (a -> b) -> a -> b
$ EventM Name b -> StateT MHState (EventM Name) b
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
St.lift (EventM Name b -> StateT MHState (EventM Name) b)
-> EventM Name b -> StateT MHState (EventM Name) b
forall a b. (a -> b) -> a -> b
$ b -> EventM Name b
f (ChatState
st ChatState -> Getting b ChatState b -> b
forall s a. s -> Getting a s a -> a
^. Getting b ChatState b
Lens' ChatState b
ln)
    MHState
-> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) ()
forall s (m :: * -> *). MonadState s m => s -> m ()
St.put (MHState
s { mhCurrentState :: ChatState
mhCurrentState = ChatState
st ChatState -> (ChatState -> ChatState) -> ChatState
forall a b. a -> (a -> b) -> b
& (b -> Identity b) -> ChatState -> Identity ChatState
Lens' ChatState b
ln ((b -> Identity b) -> ChatState -> Identity ChatState)
-> b -> ChatState -> ChatState
forall s t a b. ASetter s t a b -> b -> s -> t
.~ b
n })

mhSuspendAndResume :: (ChatState -> IO ChatState) -> MH ()
mhSuspendAndResume :: (ChatState -> IO ChatState) -> MH ()
mhSuspendAndResume ChatState -> IO ChatState
mote = ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) ()
-> MH ()
forall a.
ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a -> MH a
MH (ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) ()
 -> MH ())
-> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) ()
-> MH ()
forall a b. (a -> b) -> a -> b
$ do
    MHState
s <- ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) MHState
forall s (m :: * -> *). MonadState s m => m s
St.get
    MHState
-> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) ()
forall s (m :: * -> *). MonadState s m => s -> m ()
St.put (MHState
 -> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) ())
-> MHState
-> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) ()
forall a b. (a -> b) -> a -> b
$ MHState
s { mhNextAction :: ChatState -> EventM Name (Next ChatState)
mhNextAction = \ ChatState
_ -> IO ChatState -> EventM Name (Next ChatState)
forall s n. IO s -> EventM n (Next s)
Brick.suspendAndResume (ChatState -> IO ChatState
mote (ChatState -> IO ChatState) -> ChatState -> IO ChatState
forall a b. (a -> b) -> a -> b
$ MHState -> ChatState
mhCurrentState MHState
s) }

mhContinueWithoutRedraw :: MH ()
mhContinueWithoutRedraw :: MH ()
mhContinueWithoutRedraw = ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) ()
-> MH ()
forall a.
ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a -> MH a
MH (ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) ()
 -> MH ())
-> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) ()
-> MH ()
forall a b. (a -> b) -> a -> b
$ do
    MHState
s <- ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) MHState
forall s (m :: * -> *). MonadState s m => m s
St.get
    MHState
-> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) ()
forall s (m :: * -> *). MonadState s m => s -> m ()
St.put (MHState
 -> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) ())
-> MHState
-> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) ()
forall a b. (a -> b) -> a -> b
$ MHState
s { mhNextAction :: ChatState -> EventM Name (Next ChatState)
mhNextAction = \ ChatState
_ -> ChatState -> EventM Name (Next ChatState)
forall s n. s -> EventM n (Next s)
Brick.continueWithoutRedraw (MHState -> ChatState
mhCurrentState MHState
s) }

-- | This will request that after this computation finishes the
-- application should exit
requestQuit :: MH ()
requestQuit :: MH ()
requestQuit = ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) ()
-> MH ()
forall a.
ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a -> MH a
MH (ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) ()
 -> MH ())
-> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) ()
-> MH ()
forall a b. (a -> b) -> a -> b
$ do
    MHState
s <- ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) MHState
forall s (m :: * -> *). MonadState s m => m s
St.get
    MHState
-> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) ()
forall s (m :: * -> *). MonadState s m => s -> m ()
St.put (MHState
 -> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) ())
-> MHState
-> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) ()
forall a b. (a -> b) -> a -> b
$ MHState
s { mhNextAction :: ChatState -> EventM Name (Next ChatState)
mhNextAction = ChatState -> EventM Name (Next ChatState)
forall s n. s -> EventM n (Next s)
Brick.halt }

instance Functor MH where
    fmap :: (a -> b) -> MH a -> MH b
fmap a -> b
f (MH ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a
x) = ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) b -> MH b
forall a.
ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a -> MH a
MH ((a -> b)
-> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a
-> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a
x)

instance Applicative MH where
    pure :: a -> MH a
pure a
x = ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a -> MH a
forall a.
ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a -> MH a
MH (a -> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x)
    MH ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) (a -> b)
f <*> :: MH (a -> b) -> MH a -> MH b
<*> MH ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a
x = ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) b -> MH b
forall a.
ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a -> MH a
MH (ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) (a -> b)
f ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) (a -> b)
-> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a
-> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a
x)

instance MHF.MonadFail MH where
    fail :: String -> MH a
fail = ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a -> MH a
forall a.
ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a -> MH a
MH (ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a
 -> MH a)
-> (String
    -> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a)
-> String
-> MH a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String
-> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a
forall (m :: * -> *) a. MonadFail m => String -> m a
MHF.fail

instance Monad MH where
    return :: a -> MH a
return = a -> MH a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
    MH ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a
x >>= :: MH a -> (a -> MH b) -> MH b
>>= a -> MH b
f = ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) b -> MH b
forall a.
ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a -> MH a
MH (ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a
x ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a
-> (a
    -> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) b)
-> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \ a
x' -> MH b -> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) b
forall a.
MH a -> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a
fromMH (a -> MH b
f a
x'))

-- We want to pretend that the state is only the ChatState, rather
-- than the ChatState and the Brick continuation
instance St.MonadState ChatState MH where
    get :: MH ChatState
get = MHState -> ChatState
mhCurrentState (MHState -> ChatState) -> MH MHState -> MH ChatState
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) MHState
-> MH MHState
forall a.
ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a -> MH a
MH ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) MHState
forall s (m :: * -> *). MonadState s m => m s
St.get
    put :: ChatState -> MH ()
put ChatState
st = ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) ()
-> MH ()
forall a.
ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a -> MH a
MH (ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) ()
 -> MH ())
-> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) ()
-> MH ()
forall a b. (a -> b) -> a -> b
$ do
        MHState
s <- ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) MHState
forall s (m :: * -> *). MonadState s m => m s
St.get
        MHState
-> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) ()
forall s (m :: * -> *). MonadState s m => s -> m ()
St.put (MHState
 -> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) ())
-> MHState
-> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) ()
forall a b. (a -> b) -> a -> b
$ MHState
s { mhCurrentState :: ChatState
mhCurrentState = ChatState
st }

instance St.MonadIO MH where
    liftIO :: IO a -> MH a
liftIO = ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a -> MH a
forall a.
ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a -> MH a
MH (ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a
 -> MH a)
-> (IO a
    -> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a)
-> IO a
-> MH a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO a -> ReaderT (Maybe LogContext) (StateT MHState (EventM Name)) a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
St.liftIO

-- | This represents events that we handle in the main application loop.
data MHEvent =
    WSEvent WebsocketEvent
    -- ^ For events that arise from the websocket
    | WSActionResponse WebsocketActionResponse
    -- ^ For responses to websocket actions
    | RespEvent (MH ())
    -- ^ For the result values of async IO operations
    | RefreshWebsocketEvent
    -- ^ Tell our main loop to refresh the websocket connection
    | WebsocketParseError String
    -- ^ We failed to parse an incoming websocket event
    | WebsocketDisconnect
    -- ^ The websocket connection went down.
    | WebsocketConnect
    -- ^ The websocket connection came up.
    | BGIdle
    -- ^ background worker is idle
    | BGBusy (Maybe Int)
    -- ^ background worker is busy (with n requests)
    | RateLimitExceeded Int
    -- ^ A request initially failed due to a rate limit but will be
    -- retried if possible. The argument is the number of seconds in
    -- which the retry will be attempted.
    | RateLimitSettingsMissing
    -- ^ A request denied by a rate limit could not be retried because
    -- the response contained no rate limit metadata
    | RequestDropped
    -- ^ A request was reattempted due to a rate limit and was rate
    -- limited again
    | IEvent InternalEvent
    -- ^ MH-internal events

-- | Internal application events.
data InternalEvent =
    DisplayError MHError
    -- ^ Some kind of application error occurred
    | LoggingStarted FilePath
    | LoggingStopped FilePath
    | LogStartFailed FilePath String
    | LogDestination (Maybe FilePath)
    | LogSnapshotSucceeded FilePath
    | LogSnapshotFailed FilePath String
    -- ^ Logging events from the logging thread

-- | Application errors.
data MHError =
    GenericError T.Text
    -- ^ A generic error message constructor
    | NoSuchChannel T.Text
    -- ^ The specified channel does not exist
    | NoSuchUser T.Text
    -- ^ The specified user does not exist
    | AmbiguousName T.Text
    -- ^ The specified name matches both a user and a channel
    | ServerError MattermostError
    -- ^ A Mattermost server error occurred
    | ClipboardError T.Text
    -- ^ A problem occurred trying to deal with yanking or the system
    -- clipboard
    | ConfigOptionMissing T.Text
    -- ^ A missing config option is required to perform an operation
    | ProgramExecutionFailed T.Text T.Text
    -- ^ Args: program name, path to log file. A problem occurred when
    -- running the program.
    | NoSuchScript T.Text
    -- ^ The specified script was not found
    | NoSuchHelpTopic T.Text
    -- ^ The specified help topic was not found
    | AttachmentException SomeException
    -- ^ IO operations for attaching a file threw an exception
    | BadAttachmentPath T.Text
    -- ^ The specified file is either a directory or doesn't exist
    | AsyncErrEvent SomeException
    -- ^ For errors that arise in the course of async IO operations
    deriving (Int -> MHError -> ShowS
[MHError] -> ShowS
MHError -> String
(Int -> MHError -> ShowS)
-> (MHError -> String) -> ([MHError] -> ShowS) -> Show MHError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [MHError] -> ShowS
$cshowList :: [MHError] -> ShowS
show :: MHError -> String
$cshow :: MHError -> String
showsPrec :: Int -> MHError -> ShowS
$cshowsPrec :: Int -> MHError -> ShowS
Show)

-- ** Application State Lenses

makeLenses ''ChatResources
makeLenses ''ChatState
makeLenses ''TeamState
makeLenses ''GlobalEditState
makeLenses ''PostListWindowState
makeLenses ''ListWindowState
makeLenses ''ChannelSelectState
makeLenses ''UserPreferences
makeLenses ''ConnectionInfo
makeLenses ''ChannelTopicDialogState
Brick.suffixLenses ''Config

-- | Given a list of event handlers and an event, try to handle the
-- event with the handlers in the specified order. If a handler returns
-- False (indicating it did not handle the event), try the next handler
-- until either a handler returns True or all handlers are tried.
-- Returns True if any handler handled the event or False otherwise.
handleEventWith :: [Vty.Event -> MH Bool] -> Vty.Event -> MH Bool
handleEventWith :: [Event -> MH Bool] -> Event -> MH Bool
handleEventWith [] Event
_ =
    Bool -> MH Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
handleEventWith (Event -> MH Bool
handler:[Event -> MH Bool]
rest) Event
e = do
    Bool
handled <- Event -> MH Bool
handler Event
e
    if Bool
handled
       then Bool -> MH Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
       else [Event -> MH Bool] -> Event -> MH Bool
handleEventWith [Event -> MH Bool]
rest Event
e

applyTeamOrderPref :: Maybe [TeamId] -> ChatState -> ChatState
applyTeamOrderPref :: Maybe [TeamId] -> ChatState -> ChatState
applyTeamOrderPref Maybe [TeamId]
Nothing ChatState
st = ChatState
st
applyTeamOrderPref (Just [TeamId]
prefTIds) ChatState
st =
    let teams :: HashMap TeamId TeamState
teams = ChatState -> HashMap TeamId TeamState
_csTeams ChatState
st
        ourTids :: [TeamId]
ourTids = HashMap TeamId TeamState -> [TeamId]
forall k v. HashMap k v -> [k]
HM.keys HashMap TeamId TeamState
teams
        tIds :: [TeamId]
tIds = (TeamId -> Bool) -> [TeamId] -> [TeamId]
forall a. (a -> Bool) -> [a] -> [a]
filter (TeamId -> [TeamId] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [TeamId]
ourTids) [TeamId]
prefTIds
        curTId :: Maybe TeamId
curTId = ChatState
stChatState
-> Getting (Maybe TeamId) ChatState (Maybe TeamId) -> Maybe TeamId
forall s a. s -> Getting a s a -> a
^.Getting (Maybe TeamId) ChatState (Maybe TeamId)
SimpleGetter ChatState (Maybe TeamId)
csCurrentTeamId
        unmentioned :: [TeamState]
unmentioned = (TeamState -> Bool) -> [TeamState] -> [TeamState]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> (TeamState -> Bool) -> TeamState -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TeamState -> Bool
wasMentioned) ([TeamState] -> [TeamState]) -> [TeamState] -> [TeamState]
forall a b. (a -> b) -> a -> b
$ HashMap TeamId TeamState -> [TeamState]
forall k v. HashMap k v -> [v]
HM.elems HashMap TeamId TeamState
teams
        wasMentioned :: TeamState -> Bool
wasMentioned TeamState
ts = (Team -> TeamId
teamId (Team -> TeamId) -> Team -> TeamId
forall a b. (a -> b) -> a -> b
$ TeamState -> Team
_tsTeam TeamState
ts) TeamId -> [TeamId] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [TeamId]
tIds
        zipperTids :: [TeamId]
zipperTids = [TeamId]
tIds [TeamId] -> [TeamId] -> [TeamId]
forall a. Semigroup a => a -> a -> a
<> (Team -> TeamId
teamId (Team -> TeamId) -> [Team] -> [TeamId]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Team] -> [Team]
sortTeams (TeamState -> Team
_tsTeam (TeamState -> Team) -> [TeamState] -> [Team]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [TeamState]
unmentioned))
    in ChatState
st { _csTeamZipper :: Zipper () TeamId
_csTeamZipper = ((TeamId -> Bool) -> Zipper () TeamId -> Zipper () TeamId
forall b a. (b -> Bool) -> Zipper a b -> Zipper a b
Z.findRight ((Maybe TeamId -> Maybe TeamId -> Bool
forall a. Eq a => a -> a -> Bool
== Maybe TeamId
curTId) (Maybe TeamId -> Bool)
-> (TeamId -> Maybe TeamId) -> TeamId -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TeamId -> Maybe TeamId
forall a. a -> Maybe a
Just) (Zipper () TeamId -> Zipper () TeamId)
-> Zipper () TeamId -> Zipper () TeamId
forall a b. (a -> b) -> a -> b
$ [TeamId] -> Zipper () TeamId
mkTeamZipperFromIds [TeamId]
zipperTids)
          }

refreshTeamZipper :: MH ()
refreshTeamZipper :: MH ()
refreshTeamZipper = do
    Maybe [TeamId]
tidOrder <- Getting (Maybe [TeamId]) ChatState (Maybe [TeamId])
-> MH (Maybe [TeamId])
forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use ((ChatResources -> Const (Maybe [TeamId]) ChatResources)
-> ChatState -> Const (Maybe [TeamId]) ChatState
Lens' ChatState ChatResources
csResources((ChatResources -> Const (Maybe [TeamId]) ChatResources)
 -> ChatState -> Const (Maybe [TeamId]) ChatState)
-> ((Maybe [TeamId] -> Const (Maybe [TeamId]) (Maybe [TeamId]))
    -> ChatResources -> Const (Maybe [TeamId]) ChatResources)
-> Getting (Maybe [TeamId]) ChatState (Maybe [TeamId])
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(UserPreferences -> Const (Maybe [TeamId]) UserPreferences)
-> ChatResources -> Const (Maybe [TeamId]) ChatResources
Lens' ChatResources UserPreferences
crUserPreferences((UserPreferences -> Const (Maybe [TeamId]) UserPreferences)
 -> ChatResources -> Const (Maybe [TeamId]) ChatResources)
-> ((Maybe [TeamId] -> Const (Maybe [TeamId]) (Maybe [TeamId]))
    -> UserPreferences -> Const (Maybe [TeamId]) UserPreferences)
-> (Maybe [TeamId] -> Const (Maybe [TeamId]) (Maybe [TeamId]))
-> ChatResources
-> Const (Maybe [TeamId]) ChatResources
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Maybe [TeamId] -> Const (Maybe [TeamId]) (Maybe [TeamId]))
-> UserPreferences -> Const (Maybe [TeamId]) UserPreferences
Lens' UserPreferences (Maybe [TeamId])
userPrefTeamOrder)
    (ChatState -> ChatState) -> MH ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
St.modify (Maybe [TeamId] -> ChatState -> ChatState
applyTeamOrderPref Maybe [TeamId]
tidOrder)

applyTeamOrder :: [TeamId] -> MH ()
applyTeamOrder :: [TeamId] -> MH ()
applyTeamOrder [TeamId]
tIds = (ChatState -> ChatState) -> MH ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
St.modify (Maybe [TeamId] -> ChatState -> ChatState
applyTeamOrderPref (Maybe [TeamId] -> ChatState -> ChatState)
-> Maybe [TeamId] -> ChatState -> ChatState
forall a b. (a -> b) -> a -> b
$ [TeamId] -> Maybe [TeamId]
forall a. a -> Maybe a
Just [TeamId]
tIds)

newState :: StartupStateInfo -> ChatState
newState :: StartupStateInfo -> ChatState
newState (StartupStateInfo {HashMap TeamId TeamState
TeamId
User
TimeZoneSeries
InputHistory
ChatResources
startupStateInitialTeam :: TeamId
startupStateInitialHistory :: InputHistory
startupStateTimeZone :: TimeZoneSeries
startupStateTeams :: HashMap TeamId TeamState
startupStateConnectedUser :: User
startupStateResources :: ChatResources
startupStateInitialTeam :: StartupStateInfo -> TeamId
startupStateInitialHistory :: StartupStateInfo -> InputHistory
startupStateTimeZone :: StartupStateInfo -> TimeZoneSeries
startupStateTeams :: StartupStateInfo -> HashMap TeamId TeamState
startupStateConnectedUser :: StartupStateInfo -> User
startupStateResources :: StartupStateInfo -> ChatResources
..}) =
    let config :: Config
config = ChatResources -> Config
_crConfiguration ChatResources
startupStateResources
    in Maybe [TeamId] -> ChatState -> ChatState
applyTeamOrderPref (UserPreferences -> Maybe [TeamId]
_userPrefTeamOrder (UserPreferences -> Maybe [TeamId])
-> UserPreferences -> Maybe [TeamId]
forall a b. (a -> b) -> a -> b
$ ChatResources -> UserPreferences
_crUserPreferences ChatResources
startupStateResources) (ChatState -> ChatState) -> ChatState -> ChatState
forall a b. (a -> b) -> a -> b
$
       ChatState :: ChatResources
-> Maybe (BrickEvent Name MHEvent)
-> Maybe Int
-> HashMap TeamId TeamState
-> Zipper () TeamId
-> ChannelListOrientation
-> User
-> ClientChannels
-> HashMap TeamId (Set ChannelListGroupLabel)
-> HashMap PostId Message
-> Users
-> TimeZoneSeries
-> ConnectionStatus
-> Maybe (Maybe Int)
-> Maybe ClientConfig
-> InputHistory
-> GlobalEditState
-> ChatState
ChatState { _csResources :: ChatResources
_csResources                   = ChatResources
startupStateResources
                 , _csLastMouseDownEvent :: Maybe (BrickEvent Name MHEvent)
_csLastMouseDownEvent          = Maybe (BrickEvent Name MHEvent)
forall a. Maybe a
Nothing
                 , _csGlobalEditState :: GlobalEditState
_csGlobalEditState             = GlobalEditState
emptyGlobalEditState
                 , _csVerbatimTruncateSetting :: Maybe Int
_csVerbatimTruncateSetting     = Config -> Maybe Int
configTruncateVerbatimBlocks Config
config
                 , _csTeamZipper :: Zipper () TeamId
_csTeamZipper                  = (TeamId -> Bool) -> Zipper () TeamId -> Zipper () TeamId
forall b a. (b -> Bool) -> Zipper a b -> Zipper a b
Z.findRight (TeamId -> TeamId -> Bool
forall a. Eq a => a -> a -> Bool
== TeamId
startupStateInitialTeam) (Zipper () TeamId -> Zipper () TeamId)
-> Zipper () TeamId -> Zipper () TeamId
forall a b. (a -> b) -> a -> b
$
                                                    HashMap TeamId TeamState -> Zipper () TeamId
mkTeamZipper HashMap TeamId TeamState
startupStateTeams
                 , _csTeams :: HashMap TeamId TeamState
_csTeams                       = HashMap TeamId TeamState
startupStateTeams
                 , _csChannelListOrientation :: ChannelListOrientation
_csChannelListOrientation      = Config -> ChannelListOrientation
configChannelListOrientation Config
config
                 , _csMe :: User
_csMe                          = User
startupStateConnectedUser
                 , _csChannels :: ClientChannels
_csChannels                    = ClientChannels
noChannels
                 , _csPostMap :: HashMap PostId Message
_csPostMap                     = HashMap PostId Message
forall k v. HashMap k v
HM.empty
                 , _csUsers :: Users
_csUsers                       = Users
noUsers
                 , _timeZone :: TimeZoneSeries
_timeZone                      = TimeZoneSeries
startupStateTimeZone
                 , _csConnectionStatus :: ConnectionStatus
_csConnectionStatus            = ConnectionStatus
Connected
                 , _csWorkerIsBusy :: Maybe (Maybe Int)
_csWorkerIsBusy                = Maybe (Maybe Int)
forall a. Maybe a
Nothing
                 , _csClientConfig :: Maybe ClientConfig
_csClientConfig                = Maybe ClientConfig
forall a. Maybe a
Nothing
                 , _csInputHistory :: InputHistory
_csInputHistory                = InputHistory
startupStateInitialHistory
                 , _csHiddenChannelGroups :: HashMap TeamId (Set ChannelListGroupLabel)
_csHiddenChannelGroups         = HashMap TeamId (Set ChannelListGroupLabel)
forall a. Monoid a => a
mempty
                 }

getServerBaseUrl :: TeamId -> MH TeamBaseURL
getServerBaseUrl :: TeamId -> MH TeamBaseURL
getServerBaseUrl TeamId
tId = do
    ChatState
st <- Getting ChatState ChatState ChatState -> MH ChatState
forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use Getting ChatState ChatState ChatState
forall a. a -> a
id
    TeamBaseURL -> MH TeamBaseURL
forall (m :: * -> *) a. Monad m => a -> m a
return (TeamBaseURL -> MH TeamBaseURL) -> TeamBaseURL -> MH TeamBaseURL
forall a b. (a -> b) -> a -> b
$ ChatState -> TeamId -> TeamBaseURL
serverBaseUrl ChatState
st TeamId
tId

serverBaseUrl :: ChatState -> TeamId -> TeamBaseURL
serverBaseUrl :: ChatState -> TeamId -> TeamBaseURL
serverBaseUrl ChatState
st TeamId
tId =
    let baseUrl :: ServerBaseURL
baseUrl = ConnectionData -> ServerBaseURL
connectionDataURL (ConnectionData -> ServerBaseURL)
-> ConnectionData -> ServerBaseURL
forall a b. (a -> b) -> a -> b
$ ChatResources -> ConnectionData
_crConn (ChatResources -> ConnectionData)
-> ChatResources -> ConnectionData
forall a b. (a -> b) -> a -> b
$ ChatState -> ChatResources
_csResources ChatState
st
        tName :: UserText
tName = Team -> UserText
teamName (Team -> UserText) -> Team -> UserText
forall a b. (a -> b) -> a -> b
$ ChatState
stChatState -> Getting Team ChatState Team -> Team
forall s a. s -> Getting a s a -> a
^.TeamId -> Lens' ChatState TeamState
csTeam(TeamId
tId)((TeamState -> Const Team TeamState)
 -> ChatState -> Const Team ChatState)
-> ((Team -> Const Team Team) -> TeamState -> Const Team TeamState)
-> Getting Team ChatState Team
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Team -> Const Team Team) -> TeamState -> Const Team TeamState
Lens' TeamState Team
tsTeam
    in TeamURLName -> ServerBaseURL -> TeamBaseURL
TeamBaseURL (Text -> TeamURLName
TeamURLName (Text -> TeamURLName) -> Text -> TeamURLName
forall a b. (a -> b) -> a -> b
$ UserText -> Text
sanitizeUserText UserText
tName) ServerBaseURL
baseUrl

getSession :: MH Session
getSession :: MH Session
getSession = Getting Session ChatState Session -> MH Session
forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use ((ChatResources -> Const Session ChatResources)
-> ChatState -> Const Session ChatState
Lens' ChatState ChatResources
csResources((ChatResources -> Const Session ChatResources)
 -> ChatState -> Const Session ChatState)
-> ((Session -> Const Session Session)
    -> ChatResources -> Const Session ChatResources)
-> Getting Session ChatState Session
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Session -> Const Session Session)
-> ChatResources -> Const Session ChatResources
Lens' ChatResources Session
crSession)

getResourceSession :: ChatResources -> Session
getResourceSession :: ChatResources -> Session
getResourceSession = ChatResources -> Session
_crSession

whenMode :: TeamId -> Mode -> MH () -> MH ()
whenMode :: TeamId -> Mode -> MH () -> MH ()
whenMode TeamId
tId Mode
m MH ()
act = do
    Mode
curMode <- NonemptyStack Mode -> Mode
forall a. NonemptyStack a -> a
top (NonemptyStack Mode -> Mode) -> MH (NonemptyStack Mode) -> MH Mode
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Getting (NonemptyStack Mode) ChatState (NonemptyStack Mode)
-> MH (NonemptyStack Mode)
forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use (TeamId -> Lens' ChatState TeamState
csTeam(TeamId
tId)((TeamState -> Const (NonemptyStack Mode) TeamState)
 -> ChatState -> Const (NonemptyStack Mode) ChatState)
-> ((NonemptyStack Mode
     -> Const (NonemptyStack Mode) (NonemptyStack Mode))
    -> TeamState -> Const (NonemptyStack Mode) TeamState)
-> Getting (NonemptyStack Mode) ChatState (NonemptyStack Mode)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(NonemptyStack Mode
 -> Const (NonemptyStack Mode) (NonemptyStack Mode))
-> TeamState -> Const (NonemptyStack Mode) TeamState
Lens' TeamState (NonemptyStack Mode)
tsModeStack)
    Bool -> MH () -> MH ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Mode
curMode Mode -> Mode -> Bool
forall a. Eq a => a -> a -> Bool
== Mode
m) MH ()
act

pushMode :: TeamId -> Mode -> MH ()
pushMode :: TeamId -> Mode -> MH ()
pushMode TeamId
tId Mode
m = do
    (ChatState -> ChatState) -> MH ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
St.modify (TeamId -> Mode -> ChatState -> ChatState
pushMode' TeamId
tId Mode
m)
    EventM Name () -> MH ()
forall a. EventM Name a -> MH a
mh EventM Name ()
forall n. Ord n => EventM n ()
invalidateCache

replaceMode :: TeamId -> Mode -> MH ()
replaceMode :: TeamId -> Mode -> MH ()
replaceMode TeamId
tId Mode
m = TeamId -> MH ()
popMode TeamId
tId MH () -> MH () -> MH ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> TeamId -> Mode -> MH ()
pushMode TeamId
tId Mode
m

popMode :: TeamId -> MH ()
popMode :: TeamId -> MH ()
popMode TeamId
tId = do
    NonemptyStack Mode
s <- Getting (NonemptyStack Mode) ChatState (NonemptyStack Mode)
-> MH (NonemptyStack Mode)
forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use (TeamId -> Lens' ChatState TeamState
csTeam(TeamId
tId)((TeamState -> Const (NonemptyStack Mode) TeamState)
 -> ChatState -> Const (NonemptyStack Mode) ChatState)
-> ((NonemptyStack Mode
     -> Const (NonemptyStack Mode) (NonemptyStack Mode))
    -> TeamState -> Const (NonemptyStack Mode) TeamState)
-> Getting (NonemptyStack Mode) ChatState (NonemptyStack Mode)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(NonemptyStack Mode
 -> Const (NonemptyStack Mode) (NonemptyStack Mode))
-> TeamState -> Const (NonemptyStack Mode) TeamState
Lens' TeamState (NonemptyStack Mode)
tsModeStack)
    let (NonemptyStack Mode
s', Maybe Mode
topVal) = NonemptyStack Mode -> (NonemptyStack Mode, Maybe Mode)
forall a. NonemptyStack a -> (NonemptyStack a, Maybe a)
pop NonemptyStack Mode
s
    case Maybe Mode
topVal of
        Maybe Mode
Nothing -> () -> MH ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
        Just Mode
_ -> do
            TeamId -> Lens' ChatState TeamState
csTeam(TeamId
tId)((TeamState -> Identity TeamState)
 -> ChatState -> Identity ChatState)
-> ((NonemptyStack Mode -> Identity (NonemptyStack Mode))
    -> TeamState -> Identity TeamState)
-> (NonemptyStack Mode -> Identity (NonemptyStack Mode))
-> ChatState
-> Identity ChatState
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(NonemptyStack Mode -> Identity (NonemptyStack Mode))
-> TeamState -> Identity TeamState
Lens' TeamState (NonemptyStack Mode)
tsModeStack ((NonemptyStack Mode -> Identity (NonemptyStack Mode))
 -> ChatState -> Identity ChatState)
-> NonemptyStack Mode -> MH ()
forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> b -> m ()
.= NonemptyStack Mode
s'
            EventM Name () -> MH ()
forall a. EventM Name a -> MH a
mh EventM Name ()
forall n. Ord n => EventM n ()
invalidateCache

pushMode' :: TeamId -> Mode -> ChatState -> ChatState
pushMode' :: TeamId -> Mode -> ChatState -> ChatState
pushMode' TeamId
tId Mode
m ChatState
st =
    let s :: NonemptyStack Mode
s = ChatState
stChatState
-> Getting (NonemptyStack Mode) ChatState (NonemptyStack Mode)
-> NonemptyStack Mode
forall s a. s -> Getting a s a -> a
^.TeamId -> Lens' ChatState TeamState
csTeam(TeamId
tId)((TeamState -> Const (NonemptyStack Mode) TeamState)
 -> ChatState -> Const (NonemptyStack Mode) ChatState)
-> ((NonemptyStack Mode
     -> Const (NonemptyStack Mode) (NonemptyStack Mode))
    -> TeamState -> Const (NonemptyStack Mode) TeamState)
-> Getting (NonemptyStack Mode) ChatState (NonemptyStack Mode)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(NonemptyStack Mode
 -> Const (NonemptyStack Mode) (NonemptyStack Mode))
-> TeamState -> Const (NonemptyStack Mode) TeamState
Lens' TeamState (NonemptyStack Mode)
tsModeStack
    in if NonemptyStack Mode -> Mode
forall a. NonemptyStack a -> a
top NonemptyStack Mode
s Mode -> Mode -> Bool
forall a. Eq a => a -> a -> Bool
== Mode
m
       then ChatState
st
       else ChatState
st ChatState -> (ChatState -> ChatState) -> ChatState
forall a b. a -> (a -> b) -> b
& TeamId -> Lens' ChatState TeamState
csTeam(TeamId
tId)((TeamState -> Identity TeamState)
 -> ChatState -> Identity ChatState)
-> ((NonemptyStack Mode -> Identity (NonemptyStack Mode))
    -> TeamState -> Identity TeamState)
-> (NonemptyStack Mode -> Identity (NonemptyStack Mode))
-> ChatState
-> Identity ChatState
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(NonemptyStack Mode -> Identity (NonemptyStack Mode))
-> TeamState -> Identity TeamState
Lens' TeamState (NonemptyStack Mode)
tsModeStack ((NonemptyStack Mode -> Identity (NonemptyStack Mode))
 -> ChatState -> Identity ChatState)
-> (NonemptyStack Mode -> NonemptyStack Mode)
-> ChatState
-> ChatState
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ (Mode -> NonemptyStack Mode -> NonemptyStack Mode
forall a. a -> NonemptyStack a -> NonemptyStack a
push Mode
m)

-- ** Utility Lenses
csCurrentChannelId :: TeamId -> SimpleGetter ChatState (Maybe ChannelId)
csCurrentChannelId :: TeamId -> SimpleGetter ChatState (Maybe ChannelId)
csCurrentChannelId TeamId
tId =
    TeamId -> Lens' ChatState TeamState
csTeam(TeamId
tId)((TeamState -> Const r TeamState)
 -> ChatState -> Const r ChatState)
-> ((Maybe ChannelId -> Const r (Maybe ChannelId))
    -> TeamState -> Const r TeamState)
-> (Maybe ChannelId -> Const r (Maybe ChannelId))
-> ChatState
-> Const r ChatState
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Zipper ChannelListGroup ChannelListEntry
 -> Const r (Zipper ChannelListGroup ChannelListEntry))
-> TeamState -> Const r TeamState
Lens' TeamState (Zipper ChannelListGroup ChannelListEntry)
tsFocus((Zipper ChannelListGroup ChannelListEntry
  -> Const r (Zipper ChannelListGroup ChannelListEntry))
 -> TeamState -> Const r TeamState)
-> ((Maybe ChannelId -> Const r (Maybe ChannelId))
    -> Zipper ChannelListGroup ChannelListEntry
    -> Const r (Zipper ChannelListGroup ChannelListEntry))
-> (Maybe ChannelId -> Const r (Maybe ChannelId))
-> TeamState
-> Const r TeamState
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Zipper ChannelListGroup ChannelListEntry
 -> Maybe ChannelListEntry)
-> SimpleGetter
     (Zipper ChannelListGroup ChannelListEntry) (Maybe ChannelListEntry)
forall s a. (s -> a) -> SimpleGetter s a
to Zipper ChannelListGroup ChannelListEntry -> Maybe ChannelListEntry
forall a b. Zipper a b -> Maybe b
Z.focusGetting
  r
  (Zipper ChannelListGroup ChannelListEntry)
  (Maybe ChannelListEntry)
-> ((Maybe ChannelId -> Const r (Maybe ChannelId))
    -> Maybe ChannelListEntry -> Const r (Maybe ChannelListEntry))
-> (Maybe ChannelId -> Const r (Maybe ChannelId))
-> Zipper ChannelListGroup ChannelListEntry
-> Const r (Zipper ChannelListGroup ChannelListEntry)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Maybe ChannelListEntry -> Maybe ChannelId)
-> SimpleGetter (Maybe ChannelListEntry) (Maybe ChannelId)
forall s a. (s -> a) -> SimpleGetter s a
to ((ChannelListEntry -> ChannelId)
-> Maybe ChannelListEntry -> Maybe ChannelId
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ChannelListEntry -> ChannelId
channelListEntryChannelId)

withCurrentTeam :: (TeamId -> MH ()) -> MH ()
withCurrentTeam :: (TeamId -> MH ()) -> MH ()
withCurrentTeam TeamId -> MH ()
f = do
    Maybe TeamId
mtId <- Getting (Maybe TeamId) ChatState (Maybe TeamId)
-> MH (Maybe TeamId)
forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use Getting (Maybe TeamId) ChatState (Maybe TeamId)
SimpleGetter ChatState (Maybe TeamId)
csCurrentTeamId
    case Maybe TeamId
mtId of
        Maybe TeamId
Nothing -> () -> MH ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
        Just TeamId
tId -> TeamId -> MH ()
f TeamId
tId

withCurrentChannel :: TeamId -> (ChannelId -> ClientChannel -> MH ()) -> MH ()
withCurrentChannel :: TeamId -> (ChannelId -> ClientChannel -> MH ()) -> MH ()
withCurrentChannel TeamId
tId ChannelId -> ClientChannel -> MH ()
f = do
    Maybe ChannelId
mcId <- Getting (Maybe ChannelId) ChatState (Maybe ChannelId)
-> MH (Maybe ChannelId)
forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use (Getting (Maybe ChannelId) ChatState (Maybe ChannelId)
 -> MH (Maybe ChannelId))
-> Getting (Maybe ChannelId) ChatState (Maybe ChannelId)
-> MH (Maybe ChannelId)
forall a b. (a -> b) -> a -> b
$ TeamId -> SimpleGetter ChatState (Maybe ChannelId)
csCurrentChannelId TeamId
tId
    case Maybe ChannelId
mcId of
        Maybe ChannelId
Nothing -> () -> MH ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
        Just ChannelId
cId -> do
            Maybe ClientChannel
mChan <- Getting (First ClientChannel) ChatState ClientChannel
-> MH (Maybe ClientChannel)
forall s (m :: * -> *) a.
MonadState s m =>
Getting (First a) s a -> m (Maybe a)
preuse (Getting (First ClientChannel) ChatState ClientChannel
 -> MH (Maybe ClientChannel))
-> Getting (First ClientChannel) ChatState ClientChannel
-> MH (Maybe ClientChannel)
forall a b. (a -> b) -> a -> b
$ ChannelId -> Traversal' ChatState ClientChannel
csChannel ChannelId
cId
            case Maybe ClientChannel
mChan of
                Just ClientChannel
ch -> ChannelId -> ClientChannel -> MH ()
f ChannelId
cId ClientChannel
ch
                Maybe ClientChannel
_ -> () -> MH ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()

withCurrentChannel' :: TeamId -> (ChannelId -> ClientChannel -> MH (Maybe a)) -> MH (Maybe a)
withCurrentChannel' :: TeamId
-> (ChannelId -> ClientChannel -> MH (Maybe a)) -> MH (Maybe a)
withCurrentChannel' TeamId
tId ChannelId -> ClientChannel -> MH (Maybe a)
f = do
    Maybe ChannelId
mcId <- Getting (Maybe ChannelId) ChatState (Maybe ChannelId)
-> MH (Maybe ChannelId)
forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use (Getting (Maybe ChannelId) ChatState (Maybe ChannelId)
 -> MH (Maybe ChannelId))
-> Getting (Maybe ChannelId) ChatState (Maybe ChannelId)
-> MH (Maybe ChannelId)
forall a b. (a -> b) -> a -> b
$ TeamId -> SimpleGetter ChatState (Maybe ChannelId)
csCurrentChannelId TeamId
tId
    case Maybe ChannelId
mcId of
        Maybe ChannelId
Nothing -> Maybe a -> MH (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing
        Just ChannelId
cId -> do
            Maybe ClientChannel
mChan <- Getting (First ClientChannel) ChatState ClientChannel
-> MH (Maybe ClientChannel)
forall s (m :: * -> *) a.
MonadState s m =>
Getting (First a) s a -> m (Maybe a)
preuse (Getting (First ClientChannel) ChatState ClientChannel
 -> MH (Maybe ClientChannel))
-> Getting (First ClientChannel) ChatState ClientChannel
-> MH (Maybe ClientChannel)
forall a b. (a -> b) -> a -> b
$ ChannelId -> Traversal' ChatState ClientChannel
csChannel ChannelId
cId
            case Maybe ClientChannel
mChan of
                Just ClientChannel
ch -> ChannelId -> ClientChannel -> MH (Maybe a)
f ChannelId
cId ClientChannel
ch
                Maybe ClientChannel
_ -> Maybe a -> MH (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing

csCurrentTeamId :: SimpleGetter ChatState (Maybe TeamId)
csCurrentTeamId :: Getting r ChatState (Maybe TeamId)
csCurrentTeamId = (Zipper () TeamId -> Const r (Zipper () TeamId))
-> ChatState -> Const r ChatState
Lens' ChatState (Zipper () TeamId)
csTeamZipper((Zipper () TeamId -> Const r (Zipper () TeamId))
 -> ChatState -> Const r ChatState)
-> ((Maybe TeamId -> Const r (Maybe TeamId))
    -> Zipper () TeamId -> Const r (Zipper () TeamId))
-> Getting r ChatState (Maybe TeamId)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Zipper () TeamId -> Maybe TeamId)
-> SimpleGetter (Zipper () TeamId) (Maybe TeamId)
forall s a. (s -> a) -> SimpleGetter s a
to Zipper () TeamId -> Maybe TeamId
forall a b. Zipper a b -> Maybe b
Z.focus

csChannelMessageInterface :: ChannelId -> Lens' ChatState ChannelMessageInterface
csChannelMessageInterface :: ChannelId -> Lens' ChatState ChannelMessageInterface
csChannelMessageInterface ChannelId
cId =
    (ClientChannels -> f ClientChannels) -> ChatState -> f ChatState
Lens' ChatState ClientChannels
csChannels((ClientChannels -> f ClientChannels) -> ChatState -> f ChatState)
-> ((ChannelMessageInterface -> f ChannelMessageInterface)
    -> ClientChannels -> f ClientChannels)
-> (ChannelMessageInterface -> f ChannelMessageInterface)
-> ChatState
-> f ChatState
forall b c a. (b -> c) -> (a -> b) -> a -> c
.ChannelId -> Lens' ClientChannels (Maybe ClientChannel)
maybeChannelByIdL ChannelId
cId((Maybe ClientChannel -> f (Maybe ClientChannel))
 -> ClientChannels -> f ClientChannels)
-> ((ChannelMessageInterface -> f ChannelMessageInterface)
    -> Maybe ClientChannel -> f (Maybe ClientChannel))
-> (ChannelMessageInterface -> f ChannelMessageInterface)
-> ClientChannels
-> f ClientChannels
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Traversal
  (Maybe ClientChannel)
  (Maybe ClientChannel)
  ClientChannel
  ClientChannel
-> Lens
     (Maybe ClientChannel)
     (Maybe ClientChannel)
     ClientChannel
     ClientChannel
forall s t a. HasCallStack => Traversal s t a a -> Lens s t a a
singular forall a a'. Traversal (Maybe a) (Maybe a') a a'
Traversal
  (Maybe ClientChannel)
  (Maybe ClientChannel)
  ClientChannel
  ClientChannel
_Just((ClientChannel -> f ClientChannel)
 -> Maybe ClientChannel -> f (Maybe ClientChannel))
-> ((ChannelMessageInterface -> f ChannelMessageInterface)
    -> ClientChannel -> f ClientChannel)
-> (ChannelMessageInterface -> f ChannelMessageInterface)
-> Maybe ClientChannel
-> f (Maybe ClientChannel)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(ChannelMessageInterface -> f ChannelMessageInterface)
-> ClientChannel -> f ClientChannel
Lens' ClientChannel ChannelMessageInterface
ccMessageInterface

maybeChannelMessageInterface :: ChannelId -> Traversal' ChatState ChannelMessageInterface
maybeChannelMessageInterface :: ChannelId -> Traversal' ChatState ChannelMessageInterface
maybeChannelMessageInterface ChannelId
cId =
    (ClientChannels -> f ClientChannels) -> ChatState -> f ChatState
Lens' ChatState ClientChannels
csChannels((ClientChannels -> f ClientChannels) -> ChatState -> f ChatState)
-> ((ChannelMessageInterface -> f ChannelMessageInterface)
    -> ClientChannels -> f ClientChannels)
-> (ChannelMessageInterface -> f ChannelMessageInterface)
-> ChatState
-> f ChatState
forall b c a. (b -> c) -> (a -> b) -> a -> c
.ChannelId -> Lens' ClientChannels (Maybe ClientChannel)
maybeChannelByIdL ChannelId
cId((Maybe ClientChannel -> f (Maybe ClientChannel))
 -> ClientChannels -> f ClientChannels)
-> ((ChannelMessageInterface -> f ChannelMessageInterface)
    -> Maybe ClientChannel -> f (Maybe ClientChannel))
-> (ChannelMessageInterface -> f ChannelMessageInterface)
-> ClientChannels
-> f ClientChannels
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(ClientChannel -> f ClientChannel)
-> Maybe ClientChannel -> f (Maybe ClientChannel)
forall a a'. Traversal (Maybe a) (Maybe a') a a'
_Just((ClientChannel -> f ClientChannel)
 -> Maybe ClientChannel -> f (Maybe ClientChannel))
-> ((ChannelMessageInterface -> f ChannelMessageInterface)
    -> ClientChannel -> f ClientChannel)
-> (ChannelMessageInterface -> f ChannelMessageInterface)
-> Maybe ClientChannel
-> f (Maybe ClientChannel)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(ChannelMessageInterface -> f ChannelMessageInterface)
-> ClientChannel -> f ClientChannel
Lens' ClientChannel ChannelMessageInterface
ccMessageInterface

channelEditor :: ChannelId -> Lens' ChatState (EditState Name)
channelEditor :: ChannelId -> Lens' ChatState (EditState Name)
channelEditor ChannelId
cId =
    (ClientChannels -> f ClientChannels) -> ChatState -> f ChatState
Lens' ChatState ClientChannels
csChannels((ClientChannels -> f ClientChannels) -> ChatState -> f ChatState)
-> ((EditState Name -> f (EditState Name))
    -> ClientChannels -> f ClientChannels)
-> (EditState Name -> f (EditState Name))
-> ChatState
-> f ChatState
forall b c a. (b -> c) -> (a -> b) -> a -> c
.ChannelId -> Lens' ClientChannels (Maybe ClientChannel)
maybeChannelByIdL ChannelId
cId((Maybe ClientChannel -> f (Maybe ClientChannel))
 -> ClientChannels -> f ClientChannels)
-> ((EditState Name -> f (EditState Name))
    -> Maybe ClientChannel -> f (Maybe ClientChannel))
-> (EditState Name -> f (EditState Name))
-> ClientChannels
-> f ClientChannels
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Traversal
  (Maybe ClientChannel)
  (Maybe ClientChannel)
  ClientChannel
  ClientChannel
-> Lens
     (Maybe ClientChannel)
     (Maybe ClientChannel)
     ClientChannel
     ClientChannel
forall s t a. HasCallStack => Traversal s t a a -> Lens s t a a
singular forall a a'. Traversal (Maybe a) (Maybe a') a a'
Traversal
  (Maybe ClientChannel)
  (Maybe ClientChannel)
  ClientChannel
  ClientChannel
_Just((ClientChannel -> f ClientChannel)
 -> Maybe ClientChannel -> f (Maybe ClientChannel))
-> ((EditState Name -> f (EditState Name))
    -> ClientChannel -> f ClientChannel)
-> (EditState Name -> f (EditState Name))
-> Maybe ClientChannel
-> f (Maybe ClientChannel)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(ChannelMessageInterface -> f ChannelMessageInterface)
-> ClientChannel -> f ClientChannel
Lens' ClientChannel ChannelMessageInterface
ccMessageInterface((ChannelMessageInterface -> f ChannelMessageInterface)
 -> ClientChannel -> f ClientChannel)
-> ((EditState Name -> f (EditState Name))
    -> ChannelMessageInterface -> f ChannelMessageInterface)
-> (EditState Name -> f (EditState Name))
-> ClientChannel
-> f ClientChannel
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(EditState Name -> f (EditState Name))
-> ChannelMessageInterface -> f ChannelMessageInterface
forall n i. Lens' (MessageInterface n i) (EditState n)
miEditor

channelMessageSelect :: ChannelId -> Lens' ChatState MessageSelectState
channelMessageSelect :: ChannelId -> Lens' ChatState MessageSelectState
channelMessageSelect ChannelId
cId =
    (ClientChannels -> f ClientChannels) -> ChatState -> f ChatState
Lens' ChatState ClientChannels
csChannels((ClientChannels -> f ClientChannels) -> ChatState -> f ChatState)
-> ((MessageSelectState -> f MessageSelectState)
    -> ClientChannels -> f ClientChannels)
-> (MessageSelectState -> f MessageSelectState)
-> ChatState
-> f ChatState
forall b c a. (b -> c) -> (a -> b) -> a -> c
.ChannelId -> Lens' ClientChannels (Maybe ClientChannel)
maybeChannelByIdL ChannelId
cId((Maybe ClientChannel -> f (Maybe ClientChannel))
 -> ClientChannels -> f ClientChannels)
-> ((MessageSelectState -> f MessageSelectState)
    -> Maybe ClientChannel -> f (Maybe ClientChannel))
-> (MessageSelectState -> f MessageSelectState)
-> ClientChannels
-> f ClientChannels
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Traversal
  (Maybe ClientChannel)
  (Maybe ClientChannel)
  ClientChannel
  ClientChannel
-> Lens
     (Maybe ClientChannel)
     (Maybe ClientChannel)
     ClientChannel
     ClientChannel
forall s t a. HasCallStack => Traversal s t a a -> Lens s t a a
singular forall a a'. Traversal (Maybe a) (Maybe a') a a'
Traversal
  (Maybe ClientChannel)
  (Maybe ClientChannel)
  ClientChannel
  ClientChannel
_Just((ClientChannel -> f ClientChannel)
 -> Maybe ClientChannel -> f (Maybe ClientChannel))
-> ((MessageSelectState -> f MessageSelectState)
    -> ClientChannel -> f ClientChannel)
-> (MessageSelectState -> f MessageSelectState)
-> Maybe ClientChannel
-> f (Maybe ClientChannel)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(ChannelMessageInterface -> f ChannelMessageInterface)
-> ClientChannel -> f ClientChannel
Lens' ClientChannel ChannelMessageInterface
ccMessageInterface((ChannelMessageInterface -> f ChannelMessageInterface)
 -> ClientChannel -> f ClientChannel)
-> ((MessageSelectState -> f MessageSelectState)
    -> ChannelMessageInterface -> f ChannelMessageInterface)
-> (MessageSelectState -> f MessageSelectState)
-> ClientChannel
-> f ClientChannel
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(MessageSelectState -> f MessageSelectState)
-> ChannelMessageInterface -> f ChannelMessageInterface
forall n i. Lens' (MessageInterface n i) MessageSelectState
miMessageSelect

csTeam :: TeamId -> Lens' ChatState TeamState
csTeam :: TeamId -> Lens' ChatState TeamState
csTeam TeamId
tId =
    (ChatState -> TeamState)
-> (ChatState -> TeamState -> ChatState)
-> Lens' ChatState TeamState
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
lens (\ ChatState
st -> ChatState
st ChatState
-> Getting (Maybe TeamState) ChatState (Maybe TeamState)
-> Maybe TeamState
forall s a. s -> Getting a s a -> a
^. (HashMap TeamId TeamState
 -> Const (Maybe TeamState) (HashMap TeamId TeamState))
-> ChatState -> Const (Maybe TeamState) ChatState
Lens' ChatState (HashMap TeamId TeamState)
csTeams ((HashMap TeamId TeamState
  -> Const (Maybe TeamState) (HashMap TeamId TeamState))
 -> ChatState -> Const (Maybe TeamState) ChatState)
-> ((Maybe TeamState -> Const (Maybe TeamState) (Maybe TeamState))
    -> HashMap TeamId TeamState
    -> Const (Maybe TeamState) (HashMap TeamId TeamState))
-> Getting (Maybe TeamState) ChatState (Maybe TeamState)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (HashMap TeamId TeamState)
-> Lens'
     (HashMap TeamId TeamState)
     (Maybe (IxValue (HashMap TeamId TeamState)))
forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at TeamId
Index (HashMap TeamId TeamState)
tId Maybe TeamState
-> Getting (Endo TeamState) (Maybe TeamState) TeamState
-> TeamState
forall s a. HasCallStack => s -> Getting (Endo a) s a -> a
^?! Getting (Endo TeamState) (Maybe TeamState) TeamState
forall a a'. Traversal (Maybe a) (Maybe a') a a'
_Just)
         (\ ChatState
st TeamState
t -> ChatState
st ChatState -> (ChatState -> ChatState) -> ChatState
forall a b. a -> (a -> b) -> b
& (HashMap TeamId TeamState -> Identity (HashMap TeamId TeamState))
-> ChatState -> Identity ChatState
Lens' ChatState (HashMap TeamId TeamState)
csTeams ((HashMap TeamId TeamState -> Identity (HashMap TeamId TeamState))
 -> ChatState -> Identity ChatState)
-> ((Maybe TeamState -> Identity (Maybe TeamState))
    -> HashMap TeamId TeamState -> Identity (HashMap TeamId TeamState))
-> (Maybe TeamState -> Identity (Maybe TeamState))
-> ChatState
-> Identity ChatState
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (HashMap TeamId TeamState)
-> Lens'
     (HashMap TeamId TeamState)
     (Maybe (IxValue (HashMap TeamId TeamState)))
forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at TeamId
Index (HashMap TeamId TeamState)
tId ((Maybe TeamState -> Identity (Maybe TeamState))
 -> ChatState -> Identity ChatState)
-> Maybe TeamState -> ChatState -> ChatState
forall s t a b. ASetter s t a b -> b -> s -> t
.~ TeamState -> Maybe TeamState
forall a. a -> Maybe a
Just TeamState
t)

teamMode :: TeamState -> Mode
teamMode :: TeamState -> Mode
teamMode = NonemptyStack Mode -> Mode
forall a. NonemptyStack a -> a
top (NonemptyStack Mode -> Mode)
-> (TeamState -> NonemptyStack Mode) -> TeamState -> Mode
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TeamState -> NonemptyStack Mode
_tsModeStack

teamModes :: TeamState -> [Mode]
teamModes :: TeamState -> [Mode]
teamModes = NonemptyStack Mode -> [Mode]
forall a. NonemptyStack a -> [a]
stackToList (NonemptyStack Mode -> [Mode])
-> (TeamState -> NonemptyStack Mode) -> TeamState -> [Mode]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TeamState -> NonemptyStack Mode
_tsModeStack

getTeamMode :: TeamId -> MH Mode
getTeamMode :: TeamId -> MH Mode
getTeamMode TeamId
tId = TeamState -> Mode
teamMode (TeamState -> Mode) -> MH TeamState -> MH Mode
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Getting TeamState ChatState TeamState -> MH TeamState
forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use (TeamId -> Lens' ChatState TeamState
csTeam(TeamId
tId))

channelListEntryUserId :: ChannelListEntry -> Maybe UserId
channelListEntryUserId :: ChannelListEntry -> Maybe UserId
channelListEntryUserId ChannelListEntry
e =
    case ChannelListEntry -> ChannelListEntryType
channelListEntryType ChannelListEntry
e of
        CLUserDM UserId
uId -> UserId -> Maybe UserId
forall a. a -> Maybe a
Just UserId
uId
        ChannelListEntryType
_ -> Maybe UserId
forall a. Maybe a
Nothing

userIdsFromZipper :: Z.Zipper ChannelListGroup ChannelListEntry -> [UserId]
userIdsFromZipper :: Zipper ChannelListGroup ChannelListEntry -> [UserId]
userIdsFromZipper Zipper ChannelListGroup ChannelListEntry
z =
    [[UserId]] -> [UserId]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[UserId]] -> [UserId]) -> [[UserId]] -> [UserId]
forall a b. (a -> b) -> a -> b
$ ([Maybe UserId] -> [UserId]
forall a. [Maybe a] -> [a]
catMaybes ([Maybe UserId] -> [UserId])
-> ((ChannelListGroup, [ChannelListEntry]) -> [Maybe UserId])
-> (ChannelListGroup, [ChannelListEntry])
-> [UserId]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ChannelListEntry -> Maybe UserId)
-> [ChannelListEntry] -> [Maybe UserId]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ChannelListEntry -> Maybe UserId
channelListEntryUserId ([ChannelListEntry] -> [Maybe UserId])
-> ((ChannelListGroup, [ChannelListEntry]) -> [ChannelListEntry])
-> (ChannelListGroup, [ChannelListEntry])
-> [Maybe UserId]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ChannelListGroup, [ChannelListEntry]) -> [ChannelListEntry]
forall a b. (a, b) -> b
snd) ((ChannelListGroup, [ChannelListEntry]) -> [UserId])
-> [(ChannelListGroup, [ChannelListEntry])] -> [[UserId]]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Zipper ChannelListGroup ChannelListEntry
-> [(ChannelListGroup, [ChannelListEntry])]
forall a b. Zipper a b -> [(a, [b])]
Z.toList Zipper ChannelListGroup ChannelListEntry
z

entryIsDMEntry :: ChannelListEntry -> Bool
entryIsDMEntry :: ChannelListEntry -> Bool
entryIsDMEntry ChannelListEntry
e =
    case ChannelListEntry -> ChannelListEntryType
channelListEntryType ChannelListEntry
e of
        CLUserDM {} -> Bool
True
        CLGroupDM {} -> Bool
True
        CLChannel {} -> Bool
False

csChannel :: ChannelId -> Traversal' ChatState ClientChannel
csChannel :: ChannelId -> Traversal' ChatState ClientChannel
csChannel ChannelId
cId =
    (ClientChannels -> f ClientChannels) -> ChatState -> f ChatState
Lens' ChatState ClientChannels
csChannels ((ClientChannels -> f ClientChannels) -> ChatState -> f ChatState)
-> ((ClientChannel -> f ClientChannel)
    -> ClientChannels -> f ClientChannels)
-> (ClientChannel -> f ClientChannel)
-> ChatState
-> f ChatState
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ChannelId -> Traversal' ClientChannels ClientChannel
channelByIdL ChannelId
cId

csChannelMessages :: ChannelId -> Traversal' ChatState Messages
csChannelMessages :: ChannelId -> Traversal' ChatState Messages
csChannelMessages ChannelId
cId =
    ChannelId -> Lens' ChatState ChannelMessageInterface
csChannelMessageInterface(ChannelId
cId)((ChannelMessageInterface -> f ChannelMessageInterface)
 -> ChatState -> f ChatState)
-> ((Messages -> f Messages)
    -> ChannelMessageInterface -> f ChannelMessageInterface)
-> (Messages -> f Messages)
-> ChatState
-> f ChatState
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Messages -> f Messages)
-> ChannelMessageInterface -> f ChannelMessageInterface
forall n i. Lens' (MessageInterface n i) Messages
miMessages

withChannel :: ChannelId -> (ClientChannel -> MH ()) -> MH ()
withChannel :: ChannelId -> (ClientChannel -> MH ()) -> MH ()
withChannel ChannelId
cId = ChannelId -> () -> (ClientChannel -> MH ()) -> MH ()
forall a. ChannelId -> a -> (ClientChannel -> MH a) -> MH a
withChannelOrDefault ChannelId
cId ()

withChannelOrDefault :: ChannelId -> a -> (ClientChannel -> MH a) -> MH a
withChannelOrDefault :: ChannelId -> a -> (ClientChannel -> MH a) -> MH a
withChannelOrDefault ChannelId
cId a
deflt ClientChannel -> MH a
mote = do
    Maybe ClientChannel
chan <- Getting (First ClientChannel) ChatState ClientChannel
-> MH (Maybe ClientChannel)
forall s (m :: * -> *) a.
MonadState s m =>
Getting (First a) s a -> m (Maybe a)
preuse (ChannelId -> Traversal' ChatState ClientChannel
csChannel(ChannelId
cId))
    case Maybe ClientChannel
chan of
        Maybe ClientChannel
Nothing -> a -> MH a
forall (m :: * -> *) a. Monad m => a -> m a
return a
deflt
        Just ClientChannel
c  -> ClientChannel -> MH a
mote ClientChannel
c

-- ** 'ChatState' Helper Functions

raiseInternalEvent :: InternalEvent -> MH ()
raiseInternalEvent :: InternalEvent -> MH ()
raiseInternalEvent InternalEvent
ev = do
    BChan MHEvent
queue <- Getting (BChan MHEvent) ChatState (BChan MHEvent)
-> MH (BChan MHEvent)
forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use ((ChatResources -> Const (BChan MHEvent) ChatResources)
-> ChatState -> Const (BChan MHEvent) ChatState
Lens' ChatState ChatResources
csResources((ChatResources -> Const (BChan MHEvent) ChatResources)
 -> ChatState -> Const (BChan MHEvent) ChatState)
-> ((BChan MHEvent -> Const (BChan MHEvent) (BChan MHEvent))
    -> ChatResources -> Const (BChan MHEvent) ChatResources)
-> Getting (BChan MHEvent) ChatState (BChan MHEvent)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(BChan MHEvent -> Const (BChan MHEvent) (BChan MHEvent))
-> ChatResources -> Const (BChan MHEvent) ChatResources
Lens' ChatResources (BChan MHEvent)
crEventQueue)
    BChan MHEvent -> MHEvent -> MH ()
forall (m :: * -> *). MonadIO m => BChan MHEvent -> MHEvent -> m ()
writeBChan BChan MHEvent
queue (InternalEvent -> MHEvent
IEvent InternalEvent
ev)

writeBChan :: (MonadIO m) => BCH.BChan MHEvent -> MHEvent -> m ()
writeBChan :: BChan MHEvent -> MHEvent -> m ()
writeBChan BChan MHEvent
chan MHEvent
e = do
    Bool
written <- IO Bool -> m Bool
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Bool -> m Bool) -> IO Bool -> m Bool
forall a b. (a -> b) -> a -> b
$ BChan MHEvent -> MHEvent -> IO Bool
forall a. BChan a -> a -> IO Bool
BCH.writeBChanNonBlocking BChan MHEvent
chan MHEvent
e
    Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool -> Bool
not Bool
written) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$
        String -> m ()
forall a. HasCallStack => String -> a
error (String -> m ()) -> String -> m ()
forall a b. (a -> b) -> a -> b
$ String
"mhSendEvent: BChan full, please report this as a bug!"

-- | Log and raise an error.
mhError :: MHError -> MH ()
mhError :: MHError -> MH ()
mhError MHError
err = do
    LogCategory -> Text -> MH ()
mhLog LogCategory
LogError (Text -> MH ()) -> Text -> MH ()
forall a b. (a -> b) -> a -> b
$ String -> Text
T.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ MHError -> String
forall a. Show a => a -> String
show MHError
err
    InternalEvent -> MH ()
raiseInternalEvent (MHError -> InternalEvent
DisplayError MHError
err)

isMine :: ChatState -> Message -> Bool
isMine :: ChatState -> Message -> Bool
isMine ChatState
st Message
msg =
    case Message
msgMessage -> Getting UserRef Message UserRef -> UserRef
forall s a. s -> Getting a s a -> a
^.Getting UserRef Message UserRef
Lens' Message UserRef
mUser of
        UserI Bool
_ UserId
uid -> UserId
uid UserId -> UserId -> Bool
forall a. Eq a => a -> a -> Bool
== ChatState -> UserId
myUserId ChatState
st
        UserRef
_ -> Bool
False

getMessageForPostId :: ChatState -> PostId -> Maybe Message
getMessageForPostId :: ChatState -> PostId -> Maybe Message
getMessageForPostId ChatState
st PostId
pId = ChatState
stChatState
-> Getting (Maybe Message) ChatState (Maybe Message)
-> Maybe Message
forall s a. s -> Getting a s a -> a
^.(HashMap PostId Message
 -> Const (Maybe Message) (HashMap PostId Message))
-> ChatState -> Const (Maybe Message) ChatState
Lens' ChatState (HashMap PostId Message)
csPostMap((HashMap PostId Message
  -> Const (Maybe Message) (HashMap PostId Message))
 -> ChatState -> Const (Maybe Message) ChatState)
-> ((Maybe Message -> Const (Maybe Message) (Maybe Message))
    -> HashMap PostId Message
    -> Const (Maybe Message) (HashMap PostId Message))
-> Getting (Maybe Message) ChatState (Maybe Message)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Index (HashMap PostId Message)
-> Lens'
     (HashMap PostId Message) (Maybe (IxValue (HashMap PostId Message)))
forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at(PostId
Index (HashMap PostId Message)
pId)

getParentMessage :: ChatState -> Message -> Maybe Message
getParentMessage :: ChatState -> Message -> Maybe Message
getParentMessage ChatState
st Message
msg
    | InReplyTo PostId
pId <- Message
msgMessage -> Getting ReplyState Message ReplyState -> ReplyState
forall s a. s -> Getting a s a -> a
^.Getting ReplyState Message ReplyState
Lens' Message ReplyState
mInReplyToMsg
      = ChatState
stChatState
-> Getting (Maybe Message) ChatState (Maybe Message)
-> Maybe Message
forall s a. s -> Getting a s a -> a
^.(HashMap PostId Message
 -> Const (Maybe Message) (HashMap PostId Message))
-> ChatState -> Const (Maybe Message) ChatState
Lens' ChatState (HashMap PostId Message)
csPostMap((HashMap PostId Message
  -> Const (Maybe Message) (HashMap PostId Message))
 -> ChatState -> Const (Maybe Message) ChatState)
-> ((Maybe Message -> Const (Maybe Message) (Maybe Message))
    -> HashMap PostId Message
    -> Const (Maybe Message) (HashMap PostId Message))
-> Getting (Maybe Message) ChatState (Maybe Message)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Index (HashMap PostId Message)
-> Lens'
     (HashMap PostId Message) (Maybe (IxValue (HashMap PostId Message)))
forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at(PostId
Index (HashMap PostId Message)
pId)
    | Bool
otherwise = Maybe Message
forall a. Maybe a
Nothing

getReplyRootMessage :: Message -> MH Message
getReplyRootMessage :: Message -> MH Message
getReplyRootMessage Message
msg = do
    case Post -> Maybe PostId
postRootId (Post -> Maybe PostId) -> Maybe Post -> Maybe PostId
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< (Message
msgMessage -> Getting (Maybe Post) Message (Maybe Post) -> Maybe Post
forall s a. s -> Getting a s a -> a
^.Getting (Maybe Post) Message (Maybe Post)
Lens' Message (Maybe Post)
mOriginalPost) of
        Maybe PostId
Nothing -> Message -> MH Message
forall (m :: * -> *) a. Monad m => a -> m a
return Message
msg
        Just PostId
rootId -> do
            ChatState
st <- Getting ChatState ChatState ChatState -> MH ChatState
forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use Getting ChatState ChatState ChatState
forall a. a -> a
id
            case ChatState -> PostId -> Maybe Message
getMessageForPostId ChatState
st PostId
rootId of
                -- NOTE: this case should never happen. This is the
                -- case where a message has a root post ID but we
                -- don't have a copy of the root post in storage. This
                -- shouldn't happen because whenever we add a message
                -- to a channel, we always fetch the parent post and
                -- store it if it is in a thread. That should mean that
                -- whenever we reply to a post, if that post is itself
                -- a reply, we should have its root post in storage
                -- and this case should never match. Even though it
                -- shouldn't happen, rather than raising a BUG exception
                -- here we'll just fall back to the input message.
                Maybe Message
Nothing -> Message -> MH Message
forall (m :: * -> *) a. Monad m => a -> m a
return Message
msg
                Just Message
m -> Message -> MH Message
forall (m :: * -> *) a. Monad m => a -> m a
return Message
m

setUserStatus :: UserId -> Text -> MH ()
setUserStatus :: UserId -> Text -> MH ()
setUserStatus UserId
uId Text
t = do
    (Users -> Identity Users) -> ChatState -> Identity ChatState
Lens' ChatState Users
csUsers ((Users -> Identity Users) -> ChatState -> Identity ChatState)
-> (Users -> Users) -> MH ()
forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> (a -> b) -> m ()
%= UserId -> (UserInfo -> UserInfo) -> Users -> Users
modifyUserById UserId
uId ((UserStatus -> Identity UserStatus)
-> UserInfo -> Identity UserInfo
Lens' UserInfo UserStatus
uiStatus ((UserStatus -> Identity UserStatus)
 -> UserInfo -> Identity UserInfo)
-> UserStatus -> UserInfo -> UserInfo
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Text -> UserStatus
statusFromText Text
t)
    ClientChannels
cs <- Getting ClientChannels ChatState ClientChannels
-> MH ClientChannels
forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use Getting ClientChannels ChatState ClientChannels
Lens' ChatState ClientChannels
csChannels
    [TeamId] -> (TeamId -> MH ()) -> MH ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (ClientChannels -> [TeamId]
allTeamIds ClientChannels
cs) ((TeamId -> MH ()) -> MH ()) -> (TeamId -> MH ()) -> MH ()
forall a b. (a -> b) -> a -> b
$ \TeamId
tId ->
        EventM Name () -> MH ()
forall a. EventM Name a -> MH a
mh (EventM Name () -> MH ()) -> EventM Name () -> MH ()
forall a b. (a -> b) -> a -> b
$ Name -> EventM Name ()
forall n. Ord n => n -> EventM n ()
invalidateCacheEntry (Name -> EventM Name ()) -> Name -> EventM Name ()
forall a b. (a -> b) -> a -> b
$ TeamId -> Name
ChannelSidebar TeamId
tId

usernameForUserId :: UserId -> ChatState -> Maybe Text
usernameForUserId :: UserId -> ChatState -> Maybe Text
usernameForUserId UserId
uId ChatState
st = UserInfo -> Text
_uiName (UserInfo -> Text) -> Maybe UserInfo -> Maybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> UserId -> Users -> Maybe UserInfo
findUserById UserId
uId (ChatState
stChatState -> Getting Users ChatState Users -> Users
forall s a. s -> Getting a s a -> a
^.Getting Users ChatState Users
Lens' ChatState Users
csUsers)

displayNameForUserId :: UserId -> ChatState -> Maybe Text
displayNameForUserId :: UserId -> ChatState -> Maybe Text
displayNameForUserId UserId
uId ChatState
st = do
    UserInfo
u <- UserId -> Users -> Maybe UserInfo
findUserById UserId
uId (ChatState
stChatState -> Getting Users ChatState Users -> Users
forall s a. s -> Getting a s a -> a
^.Getting Users ChatState Users
Lens' ChatState Users
csUsers)
    Text -> Maybe Text
forall (m :: * -> *) a. Monad m => a -> m a
return (Text -> Maybe Text) -> Text -> Maybe Text
forall a b. (a -> b) -> a -> b
$ UserInfo -> Maybe ClientConfig -> UserPreferences -> Text
displayNameForUser UserInfo
u (ChatState
stChatState
-> Getting (Maybe ClientConfig) ChatState (Maybe ClientConfig)
-> Maybe ClientConfig
forall s a. s -> Getting a s a -> a
^.Getting (Maybe ClientConfig) ChatState (Maybe ClientConfig)
Lens' ChatState (Maybe ClientConfig)
csClientConfig) (ChatState
stChatState
-> Getting UserPreferences ChatState UserPreferences
-> UserPreferences
forall s a. s -> Getting a s a -> a
^.(ChatResources -> Const UserPreferences ChatResources)
-> ChatState -> Const UserPreferences ChatState
Lens' ChatState ChatResources
csResources((ChatResources -> Const UserPreferences ChatResources)
 -> ChatState -> Const UserPreferences ChatState)
-> ((UserPreferences -> Const UserPreferences UserPreferences)
    -> ChatResources -> Const UserPreferences ChatResources)
-> Getting UserPreferences ChatState UserPreferences
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(UserPreferences -> Const UserPreferences UserPreferences)
-> ChatResources -> Const UserPreferences ChatResources
Lens' ChatResources UserPreferences
crUserPreferences)

-- | Note: this only searches users we have already loaded. Be
-- aware that if you think you need a user we haven't fetched, use
-- withFetchedUser!
userIdForUsername :: Text -> ChatState -> Maybe UserId
userIdForUsername :: Text -> ChatState -> Maybe UserId
userIdForUsername Text
name ChatState
st =
    (UserId, UserInfo) -> UserId
forall a b. (a, b) -> a
fst ((UserId, UserInfo) -> UserId)
-> Maybe (UserId, UserInfo) -> Maybe UserId
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Text -> Users -> Maybe (UserId, UserInfo)
findUserByUsername Text
name (Users -> Maybe (UserId, UserInfo))
-> Users -> Maybe (UserId, UserInfo)
forall a b. (a -> b) -> a -> b
$ ChatState
stChatState -> Getting Users ChatState Users -> Users
forall s a. s -> Getting a s a -> a
^.Getting Users ChatState Users
Lens' ChatState Users
csUsers)

channelIdByChannelName :: TeamId -> Text -> ChatState -> Maybe ChannelId
channelIdByChannelName :: TeamId -> Text -> ChatState -> Maybe ChannelId
channelIdByChannelName TeamId
tId Text
name ChatState
st =
    let matches :: (ChannelId, ClientChannel) -> Bool
matches (ChannelId
_, ClientChannel
cc) = ClientChannel
ccClientChannel -> Getting Text ClientChannel Text -> Text
forall s a. s -> Getting a s a -> a
^.(ChannelInfo -> Const Text ChannelInfo)
-> ClientChannel -> Const Text ClientChannel
Lens' ClientChannel ChannelInfo
ccInfo((ChannelInfo -> Const Text ChannelInfo)
 -> ClientChannel -> Const Text ClientChannel)
-> ((Text -> Const Text Text)
    -> ChannelInfo -> Const Text ChannelInfo)
-> Getting Text ClientChannel Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Text -> Const Text Text) -> ChannelInfo -> Const Text ChannelInfo
Lens' ChannelInfo Text
cdName Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== (Text -> Text
trimChannelSigil Text
name) Bool -> Bool -> Bool
&&
                          ClientChannel
ccClientChannel
-> Getting (Maybe TeamId) ClientChannel (Maybe TeamId)
-> Maybe TeamId
forall s a. s -> Getting a s a -> a
^.(ChannelInfo -> Const (Maybe TeamId) ChannelInfo)
-> ClientChannel -> Const (Maybe TeamId) ClientChannel
Lens' ClientChannel ChannelInfo
ccInfo((ChannelInfo -> Const (Maybe TeamId) ChannelInfo)
 -> ClientChannel -> Const (Maybe TeamId) ClientChannel)
-> ((Maybe TeamId -> Const (Maybe TeamId) (Maybe TeamId))
    -> ChannelInfo -> Const (Maybe TeamId) ChannelInfo)
-> Getting (Maybe TeamId) ClientChannel (Maybe TeamId)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Maybe TeamId -> Const (Maybe TeamId) (Maybe TeamId))
-> ChannelInfo -> Const (Maybe TeamId) ChannelInfo
Lens' ChannelInfo (Maybe TeamId)
cdTeamId Maybe TeamId -> Maybe TeamId -> Bool
forall a. Eq a => a -> a -> Bool
== (TeamId -> Maybe TeamId
forall a. a -> Maybe a
Just TeamId
tId)
    in [ChannelId] -> Maybe ChannelId
forall a. [a] -> Maybe a
listToMaybe ([ChannelId] -> Maybe ChannelId) -> [ChannelId] -> Maybe ChannelId
forall a b. (a -> b) -> a -> b
$ (ChannelId, ClientChannel) -> ChannelId
forall a b. (a, b) -> a
fst ((ChannelId, ClientChannel) -> ChannelId)
-> [(ChannelId, ClientChannel)] -> [ChannelId]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((ChannelId, ClientChannel) -> Bool)
-> ClientChannels -> [(ChannelId, ClientChannel)]
filteredChannels (ChannelId, ClientChannel) -> Bool
matches (ChatState
stChatState
-> Getting ClientChannels ChatState ClientChannels
-> ClientChannels
forall s a. s -> Getting a s a -> a
^.Getting ClientChannels ChatState ClientChannels
Lens' ChatState ClientChannels
csChannels)

channelIdByUsername :: Text -> ChatState -> Maybe ChannelId
channelIdByUsername :: Text -> ChatState -> Maybe ChannelId
channelIdByUsername Text
name ChatState
st = do
    UserId
uId <- Text -> ChatState -> Maybe UserId
userIdForUsername Text
name ChatState
st
    UserId -> ClientChannels -> Maybe ChannelId
getDmChannelFor UserId
uId (ChatState
stChatState
-> Getting ClientChannels ChatState ClientChannels
-> ClientChannels
forall s a. s -> Getting a s a -> a
^.Getting ClientChannels ChatState ClientChannels
Lens' ChatState ClientChannels
csChannels)

useNickname :: ChatState -> Bool
useNickname :: ChatState -> Bool
useNickname ChatState
st =
    Maybe ClientConfig -> UserPreferences -> Bool
useNickname' (ChatState
stChatState
-> Getting (Maybe ClientConfig) ChatState (Maybe ClientConfig)
-> Maybe ClientConfig
forall s a. s -> Getting a s a -> a
^.Getting (Maybe ClientConfig) ChatState (Maybe ClientConfig)
Lens' ChatState (Maybe ClientConfig)
csClientConfig) (ChatState
stChatState
-> Getting UserPreferences ChatState UserPreferences
-> UserPreferences
forall s a. s -> Getting a s a -> a
^.(ChatResources -> Const UserPreferences ChatResources)
-> ChatState -> Const UserPreferences ChatState
Lens' ChatState ChatResources
csResources((ChatResources -> Const UserPreferences ChatResources)
 -> ChatState -> Const UserPreferences ChatState)
-> ((UserPreferences -> Const UserPreferences UserPreferences)
    -> ChatResources -> Const UserPreferences ChatResources)
-> Getting UserPreferences ChatState UserPreferences
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(UserPreferences -> Const UserPreferences UserPreferences)
-> ChatResources -> Const UserPreferences ChatResources
Lens' ChatResources UserPreferences
crUserPreferences)

trimChannelSigil :: Text -> Text
trimChannelSigil :: Text -> Text
trimChannelSigil Text
n
    | Text
normalChannelSigil Text -> Text -> Bool
`T.isPrefixOf` Text
n = Text -> Text
T.tail Text
n
    | Bool
otherwise = Text
n

addNewUser :: UserInfo -> MH ()
addNewUser :: UserInfo -> MH ()
addNewUser UserInfo
u = do
    (Users -> Identity Users) -> ChatState -> Identity ChatState
Lens' ChatState Users
csUsers ((Users -> Identity Users) -> ChatState -> Identity ChatState)
-> (Users -> Users) -> MH ()
forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> (a -> b) -> m ()
%= UserInfo -> Users -> Users
addUser UserInfo
u
    -- Invalidate the cache because channel message rendering may need
    -- to get updated if this user authored posts in any channels.
    EventM Name () -> MH ()
forall a. EventM Name a -> MH a
mh EventM Name ()
forall n. Ord n => EventM n ()
invalidateCache

data SidebarUpdate =
    SidebarUpdateImmediate
    | SidebarUpdateDeferred
    deriving (SidebarUpdate -> SidebarUpdate -> Bool
(SidebarUpdate -> SidebarUpdate -> Bool)
-> (SidebarUpdate -> SidebarUpdate -> Bool) -> Eq SidebarUpdate
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SidebarUpdate -> SidebarUpdate -> Bool
$c/= :: SidebarUpdate -> SidebarUpdate -> Bool
== :: SidebarUpdate -> SidebarUpdate -> Bool
$c== :: SidebarUpdate -> SidebarUpdate -> Bool
Eq, Int -> SidebarUpdate -> ShowS
[SidebarUpdate] -> ShowS
SidebarUpdate -> String
(Int -> SidebarUpdate -> ShowS)
-> (SidebarUpdate -> String)
-> ([SidebarUpdate] -> ShowS)
-> Show SidebarUpdate
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SidebarUpdate] -> ShowS
$cshowList :: [SidebarUpdate] -> ShowS
show :: SidebarUpdate -> String
$cshow :: SidebarUpdate -> String
showsPrec :: Int -> SidebarUpdate -> ShowS
$cshowsPrec :: Int -> SidebarUpdate -> ShowS
Show)


resetAutocomplete :: Traversal' ChatState (EditState n) -> MH ()
resetAutocomplete :: Traversal' ChatState (EditState n) -> MH ()
resetAutocomplete Traversal' ChatState (EditState n)
which = do
    (EditState n -> Identity (EditState n))
-> ChatState -> Identity ChatState
Traversal' ChatState (EditState n)
which((EditState n -> Identity (EditState n))
 -> ChatState -> Identity ChatState)
-> ((Maybe (AutocompleteState n)
     -> Identity (Maybe (AutocompleteState n)))
    -> EditState n -> Identity (EditState n))
-> (Maybe (AutocompleteState n)
    -> Identity (Maybe (AutocompleteState n)))
-> ChatState
-> Identity ChatState
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Maybe (AutocompleteState n)
 -> Identity (Maybe (AutocompleteState n)))
-> EditState n -> Identity (EditState n)
forall n. Lens' (EditState n) (Maybe (AutocompleteState n))
esAutocomplete ((Maybe (AutocompleteState n)
  -> Identity (Maybe (AutocompleteState n)))
 -> ChatState -> Identity ChatState)
-> Maybe (AutocompleteState n) -> MH ()
forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> b -> m ()
.= Maybe (AutocompleteState n)
forall a. Maybe a
Nothing
    (EditState n -> Identity (EditState n))
-> ChatState -> Identity ChatState
Traversal' ChatState (EditState n)
which((EditState n -> Identity (EditState n))
 -> ChatState -> Identity ChatState)
-> ((Maybe Text -> Identity (Maybe Text))
    -> EditState n -> Identity (EditState n))
-> (Maybe Text -> Identity (Maybe Text))
-> ChatState
-> Identity ChatState
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Maybe Text -> Identity (Maybe Text))
-> EditState n -> Identity (EditState n)
forall n. Lens' (EditState n) (Maybe Text)
esAutocompletePending ((Maybe Text -> Identity (Maybe Text))
 -> ChatState -> Identity ChatState)
-> Maybe Text -> MH ()
forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> b -> m ()
.= Maybe Text
forall a. Maybe a
Nothing


-- * Slash Commands

-- | The 'CmdArgs' type represents the arguments to a slash-command; the
-- type parameter represents the argument structure.
data CmdArgs :: K.Type -> K.Type where
    NoArg    :: CmdArgs ()
    LineArg  :: Text -> CmdArgs Text
    UserArg  :: CmdArgs rest -> CmdArgs (Text, rest)
    ChannelArg :: CmdArgs rest -> CmdArgs (Text, rest)
    TokenArg :: Text -> CmdArgs rest -> CmdArgs (Text, rest)

-- | A 'CmdExec' value represents the implementation of a command when
-- provided with its arguments
type CmdExec a = a -> MH ()

-- | A 'Cmd' packages up a 'CmdArgs' specifier and the 'CmdExec'
-- implementation with a name and a description.
data Cmd =
    forall a. Cmd { Cmd -> Text
cmdName    :: Text
                  , Cmd -> Text
cmdDescr   :: Text
                  , ()
cmdArgSpec :: CmdArgs a
                  , ()
cmdAction  :: CmdExec a
                  }

-- | Helper function to extract the name out of a 'Cmd' value
commandName :: Cmd -> Text
commandName :: Cmd -> Text
commandName (Cmd Text
name Text
_ CmdArgs a
_ CmdExec a
_ ) = Text
name

-- *  Channel Updates and Notifications

userList :: ChatState -> [UserInfo]
userList :: ChatState -> [UserInfo]
userList ChatState
st = (UserInfo -> Bool) -> [UserInfo] -> [UserInfo]
forall a. (a -> Bool) -> [a] -> [a]
filter UserInfo -> Bool
showUser ([UserInfo] -> [UserInfo]) -> [UserInfo] -> [UserInfo]
forall a b. (a -> b) -> a -> b
$ Users -> [UserInfo]
allUsers (ChatState
stChatState -> Getting Users ChatState Users -> Users
forall s a. s -> Getting a s a -> a
^.Getting Users ChatState Users
Lens' ChatState Users
csUsers)
    where showUser :: UserInfo -> Bool
showUser UserInfo
u = Bool -> Bool
not (UserInfo -> Bool
isSelf UserInfo
u) Bool -> Bool -> Bool
&& (UserInfo
uUserInfo -> Getting Bool UserInfo Bool -> Bool
forall s a. s -> Getting a s a -> a
^.Getting Bool UserInfo Bool
Lens' UserInfo Bool
uiInTeam)
          isSelf :: UserInfo -> Bool
isSelf UserInfo
u = (ChatState -> UserId
myUserId ChatState
st) UserId -> UserId -> Bool
forall a. Eq a => a -> a -> Bool
== (UserInfo
uUserInfo -> Getting UserId UserInfo UserId -> UserId
forall s a. s -> Getting a s a -> a
^.Getting UserId UserInfo UserId
Lens' UserInfo UserId
uiId)

allUserIds :: ChatState -> [UserId]
allUserIds :: ChatState -> [UserId]
allUserIds ChatState
st = Users -> [UserId]
getAllUserIds (Users -> [UserId]) -> Users -> [UserId]
forall a b. (a -> b) -> a -> b
$ ChatState
stChatState -> Getting Users ChatState Users -> Users
forall s a. s -> Getting a s a -> a
^.Getting Users ChatState Users
Lens' ChatState Users
csUsers

-- BEWARE: you probably don't want this, but instead
-- State.Users.withFetchedUser, since this only looks up users in the
-- collection we have already loaded rather than all valid users on the
-- server.
userById :: UserId -> ChatState -> Maybe UserInfo
userById :: UserId -> ChatState -> Maybe UserInfo
userById UserId
uId ChatState
st = UserId -> Users -> Maybe UserInfo
findUserById UserId
uId (ChatState
stChatState -> Getting Users ChatState Users -> Users
forall s a. s -> Getting a s a -> a
^.Getting Users ChatState Users
Lens' ChatState Users
csUsers)

myUserId :: ChatState -> UserId
myUserId :: ChatState -> UserId
myUserId ChatState
st = ChatState -> User
myUser ChatState
st User -> Getting UserId User UserId -> UserId
forall s a. s -> Getting a s a -> a
^. Getting UserId User UserId
Lens' User UserId
userIdL

myUser :: ChatState -> User
myUser :: ChatState -> User
myUser ChatState
st = ChatState
stChatState -> Getting User ChatState User -> User
forall s a. s -> Getting a s a -> a
^.Getting User ChatState User
Lens' ChatState User
csMe

myUsername :: ChatState -> Text
myUsername :: ChatState -> Text
myUsername ChatState
st = User -> Text
userUsername (User -> Text) -> User -> Text
forall a b. (a -> b) -> a -> b
$ ChatState
stChatState -> Getting User ChatState User -> User
forall s a. s -> Getting a s a -> a
^.Getting User ChatState User
Lens' ChatState User
csMe

-- BEWARE: you probably don't want this, but instead
-- State.Users.withFetchedUser, since this only looks up users in the
-- collection we have already loaded rather than all valid users on the
-- server.
userByUsername :: Text -> ChatState -> Maybe UserInfo
userByUsername :: Text -> ChatState -> Maybe UserInfo
userByUsername Text
name ChatState
st = do
    (UserId, UserInfo) -> UserInfo
forall a b. (a, b) -> b
snd ((UserId, UserInfo) -> UserInfo)
-> Maybe (UserId, UserInfo) -> Maybe UserInfo
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Text -> Users -> Maybe (UserId, UserInfo)
findUserByUsername Text
name (Users -> Maybe (UserId, UserInfo))
-> Users -> Maybe (UserId, UserInfo)
forall a b. (a -> b) -> a -> b
$ ChatState
stChatState -> Getting Users ChatState Users -> Users
forall s a. s -> Getting a s a -> a
^.Getting Users ChatState Users
Lens' ChatState Users
csUsers)

-- BEWARE: you probably don't want this, but instead
-- State.Users.withFetchedUser, since this only looks up users in the
-- collection we have already loaded rather than all valid users on the
-- server.
userByNickname :: Text -> ChatState -> Maybe UserInfo
userByNickname :: Text -> ChatState -> Maybe UserInfo
userByNickname Text
name ChatState
st =
    (UserId, UserInfo) -> UserInfo
forall a b. (a, b) -> b
snd ((UserId, UserInfo) -> UserInfo)
-> Maybe (UserId, UserInfo) -> Maybe UserInfo
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Text -> Users -> Maybe (UserId, UserInfo)
findUserByNickname Text
name (Users -> Maybe (UserId, UserInfo))
-> Users -> Maybe (UserId, UserInfo)
forall a b. (a -> b) -> a -> b
$ ChatState
stChatState -> Getting Users ChatState Users -> Users
forall s a. s -> Getting a s a -> a
^.Getting Users ChatState Users
Lens' ChatState Users
csUsers)

getUsers :: MH Users
getUsers :: MH Users
getUsers = Getting Users ChatState Users -> MH Users
forall s (m :: * -> *) a. MonadState s m => Getting a s a -> m a
use Getting Users ChatState Users
Lens' ChatState Users
csUsers

-- * HighlightSet

type UserSet = Set Text
type ChannelSet = Set Text

-- | The set of usernames, channel names, and language names used for
-- highlighting when rendering messages.
data HighlightSet =
    HighlightSet { HighlightSet -> Set Text
hUserSet    :: Set Text
                 , HighlightSet -> Set Text
hChannelSet :: Set Text
                 , HighlightSet -> SyntaxMap
hSyntaxMap  :: SyntaxMap
                 }

emptyHSet :: HighlightSet
emptyHSet :: HighlightSet
emptyHSet = Set Text -> Set Text -> SyntaxMap -> HighlightSet
HighlightSet Set Text
forall a. Set a
Set.empty Set Text
forall a. Set a
Set.empty SyntaxMap
forall a. Monoid a => a
mempty

getHighlightSet :: ChatState -> TeamId -> HighlightSet
getHighlightSet :: ChatState -> TeamId -> HighlightSet
getHighlightSet ChatState
st TeamId
tId =
    HighlightSet :: Set Text -> Set Text -> SyntaxMap -> HighlightSet
HighlightSet { hUserSet :: Set Text
hUserSet = Set Text -> Set Text
addSpecialUserMentions (Set Text -> Set Text) -> Set Text -> Set Text
forall a b. (a -> b) -> a -> b
$ Users -> Set Text
getUsernameSet (Users -> Set Text) -> Users -> Set Text
forall a b. (a -> b) -> a -> b
$ ChatState
stChatState -> Getting Users ChatState Users -> Users
forall s a. s -> Getting a s a -> a
^.Getting Users ChatState Users
Lens' ChatState Users
csUsers
                 , hChannelSet :: Set Text
hChannelSet = TeamId -> ClientChannels -> Set Text
getChannelNameSet TeamId
tId (ClientChannels -> Set Text) -> ClientChannels -> Set Text
forall a b. (a -> b) -> a -> b
$ ChatState
stChatState
-> Getting ClientChannels ChatState ClientChannels
-> ClientChannels
forall s a. s -> Getting a s a -> a
^.Getting ClientChannels ChatState ClientChannels
Lens' ChatState ClientChannels
csChannels
                 , hSyntaxMap :: SyntaxMap
hSyntaxMap = ChatState
stChatState -> Getting SyntaxMap ChatState SyntaxMap -> SyntaxMap
forall s a. s -> Getting a s a -> a
^.(ChatResources -> Const SyntaxMap ChatResources)
-> ChatState -> Const SyntaxMap ChatState
Lens' ChatState ChatResources
csResources((ChatResources -> Const SyntaxMap ChatResources)
 -> ChatState -> Const SyntaxMap ChatState)
-> ((SyntaxMap -> Const SyntaxMap SyntaxMap)
    -> ChatResources -> Const SyntaxMap ChatResources)
-> Getting SyntaxMap ChatState SyntaxMap
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(SyntaxMap -> Const SyntaxMap SyntaxMap)
-> ChatResources -> Const SyntaxMap ChatResources
Lens' ChatResources SyntaxMap
crSyntaxMap
                 }

attrNameToConfig :: Brick.AttrName -> Text
attrNameToConfig :: AttrName -> Text
attrNameToConfig = String -> Text
T.pack (String -> Text) -> (AttrName -> String) -> AttrName -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
"." ([String] -> String)
-> (AttrName -> [String]) -> AttrName -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AttrName -> [String]
Brick.attrNameComponents

-- From: https://docs.mattermost.com/help/messaging/mentioning-teammates.html
specialUserMentions :: [T.Text]
specialUserMentions :: [Text]
specialUserMentions = [Text
"all", Text
"channel", Text
"here"]

addSpecialUserMentions :: Set Text -> Set Text
addSpecialUserMentions :: Set Text -> Set Text
addSpecialUserMentions Set Text
s = (Text -> Set Text -> Set Text) -> Set Text -> [Text] -> Set Text
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Text -> Set Text -> Set Text
forall a. Ord a => a -> Set a -> Set a
Set.insert Set Text
s [Text]
specialUserMentions

getNewMessageCutoff :: ChannelId -> ChatState -> Maybe NewMessageIndicator
getNewMessageCutoff :: ChannelId -> ChatState -> Maybe NewMessageIndicator
getNewMessageCutoff ChannelId
cId ChatState
st = do
    ClientChannel
cc <- ChatState
stChatState
-> Getting (First ClientChannel) ChatState ClientChannel
-> Maybe ClientChannel
forall s a. s -> Getting (First a) s a -> Maybe a
^?ChannelId -> Traversal' ChatState ClientChannel
csChannel(ChannelId
cId)
    NewMessageIndicator -> Maybe NewMessageIndicator
forall (m :: * -> *) a. Monad m => a -> m a
return (NewMessageIndicator -> Maybe NewMessageIndicator)
-> NewMessageIndicator -> Maybe NewMessageIndicator
forall a b. (a -> b) -> a -> b
$ ClientChannel
ccClientChannel
-> Getting NewMessageIndicator ClientChannel NewMessageIndicator
-> NewMessageIndicator
forall s a. s -> Getting a s a -> a
^.(ChannelInfo -> Const NewMessageIndicator ChannelInfo)
-> ClientChannel -> Const NewMessageIndicator ClientChannel
Lens' ClientChannel ChannelInfo
ccInfo((ChannelInfo -> Const NewMessageIndicator ChannelInfo)
 -> ClientChannel -> Const NewMessageIndicator ClientChannel)
-> ((NewMessageIndicator
     -> Const NewMessageIndicator NewMessageIndicator)
    -> ChannelInfo -> Const NewMessageIndicator ChannelInfo)
-> Getting NewMessageIndicator ClientChannel NewMessageIndicator
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(NewMessageIndicator
 -> Const NewMessageIndicator NewMessageIndicator)
-> ChannelInfo -> Const NewMessageIndicator ChannelInfo
Lens' ChannelInfo NewMessageIndicator
cdNewMessageIndicator

getEditedMessageCutoff :: ChannelId -> ChatState -> Maybe ServerTime
getEditedMessageCutoff :: ChannelId -> ChatState -> Maybe ServerTime
getEditedMessageCutoff ChannelId
cId ChatState
st = do
    ClientChannel
cc <- ChatState
stChatState
-> Getting (First ClientChannel) ChatState ClientChannel
-> Maybe ClientChannel
forall s a. s -> Getting (First a) s a -> Maybe a
^?ChannelId -> Traversal' ChatState ClientChannel
csChannel(ChannelId
cId)
    ClientChannel
ccClientChannel
-> Getting (Maybe ServerTime) ClientChannel (Maybe ServerTime)
-> Maybe ServerTime
forall s a. s -> Getting a s a -> a
^.(ChannelInfo -> Const (Maybe ServerTime) ChannelInfo)
-> ClientChannel -> Const (Maybe ServerTime) ClientChannel
Lens' ClientChannel ChannelInfo
ccInfo((ChannelInfo -> Const (Maybe ServerTime) ChannelInfo)
 -> ClientChannel -> Const (Maybe ServerTime) ClientChannel)
-> ((Maybe ServerTime
     -> Const (Maybe ServerTime) (Maybe ServerTime))
    -> ChannelInfo -> Const (Maybe ServerTime) ChannelInfo)
-> Getting (Maybe ServerTime) ClientChannel (Maybe ServerTime)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Maybe ServerTime -> Const (Maybe ServerTime) (Maybe ServerTime))
-> ChannelInfo -> Const (Maybe ServerTime) ChannelInfo
Lens' ChannelInfo (Maybe ServerTime)
cdEditedMessageThreshold

clearChannelUnreadStatus :: ChannelId -> MH ()
clearChannelUnreadStatus :: ChannelId -> MH ()
clearChannelUnreadStatus ChannelId
cId = do
    EventM Name () -> MH ()
forall a. EventM Name a -> MH a
mh (EventM Name () -> MH ()) -> EventM Name () -> MH ()
forall a b. (a -> b) -> a -> b
$ Name -> EventM Name ()
forall n. Ord n => n -> EventM n ()
invalidateCacheEntry (Name -> Name
MessageInterfaceMessages (Name -> Name) -> Name -> Name
forall a b. (a -> b) -> a -> b
$ ChannelId -> Name
MessageInput ChannelId
cId)
    ChannelId -> Traversal' ChatState ClientChannel
csChannel(ChannelId
cId) ((ClientChannel -> Identity ClientChannel)
 -> ChatState -> Identity ChatState)
-> (ClientChannel -> ClientChannel) -> MH ()
forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> (a -> b) -> m ()
%= (ClientChannel -> ClientChannel
clearNewMessageIndicator (ClientChannel -> ClientChannel)
-> (ClientChannel -> ClientChannel)
-> ClientChannel
-> ClientChannel
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
                       ClientChannel -> ClientChannel
clearEditedThreshold)

moveLeft :: (Eq a) => a -> [a] -> [a]
moveLeft :: a -> [a] -> [a]
moveLeft a
v [a]
as =
    case a -> [a] -> Maybe Int
forall a. Eq a => a -> [a] -> Maybe Int
elemIndex a
v [a]
as of
        Maybe Int
Nothing -> [a]
as
        Just Int
0 -> [a]
as
        Just Int
i ->
            let ([a]
h, [a]
t) = Int -> [a] -> ([a], [a])
forall a. Int -> [a] -> ([a], [a])
splitAt Int
i [a]
as
            in [a] -> [a]
forall a. [a] -> [a]
init [a]
h [a] -> [a] -> [a]
forall a. Semigroup a => a -> a -> a
<> [a
v, [a] -> a
forall a. [a] -> a
last [a]
h] [a] -> [a] -> [a]
forall a. Semigroup a => a -> a -> a
<> [a] -> [a]
forall a. [a] -> [a]
tail [a]
t

moveRight :: (Eq a) => a -> [a] -> [a]
moveRight :: a -> [a] -> [a]
moveRight a
v [a]
as =
    case a -> [a] -> Maybe Int
forall a. Eq a => a -> [a] -> Maybe Int
elemIndex a
v [a]
as of
        Maybe Int
Nothing -> [a]
as
        Just Int
i
            | Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
as Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1 -> [a]
as
            | Bool
otherwise ->
                let ([a]
h, [a]
t) = Int -> [a] -> ([a], [a])
forall a. Int -> [a] -> ([a], [a])
splitAt Int
i [a]
as
                in [a]
h [a] -> [a] -> [a]
forall a. Semigroup a => a -> a -> a
<> [[a] -> a
forall a. [a] -> a
head ([a] -> [a]
forall a. [a] -> [a]
tail [a]
t), a
v] [a] -> [a] -> [a]
forall a. Semigroup a => a -> a -> a
<> ([a] -> [a]
forall a. [a] -> [a]
tail ([a] -> [a]
forall a. [a] -> [a]
tail [a]
t))

resultToWidget :: Result n -> Widget n
resultToWidget :: Result n -> Widget n
resultToWidget = Size -> Size -> RenderM n (Result n) -> Widget n
forall n. Size -> Size -> RenderM n (Result n) -> Widget n
Widget Size
Fixed Size
Fixed (RenderM n (Result n) -> Widget n)
-> (Result n -> RenderM n (Result n)) -> Result n -> Widget n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Result n -> RenderM n (Result n)
forall (m :: * -> *) a. Monad m => a -> m a
return

threadInterface :: (HasCallStack) => TeamId -> Traversal' ChatState ThreadInterface
threadInterface :: TeamId -> Traversal' ChatState ThreadInterface
threadInterface TeamId
tId = TeamId -> Lens' ChatState (Maybe ThreadInterface)
maybeThreadInterface(TeamId
tId)((Maybe ThreadInterface -> f (Maybe ThreadInterface))
 -> ChatState -> f ChatState)
-> ((ThreadInterface -> f ThreadInterface)
    -> Maybe ThreadInterface -> f (Maybe ThreadInterface))
-> (ThreadInterface -> f ThreadInterface)
-> ChatState
-> f ChatState
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(ThreadInterface -> f ThreadInterface)
-> Maybe ThreadInterface -> f (Maybe ThreadInterface)
forall a a'. Traversal (Maybe a) (Maybe a') a a'
_Just

-- An unsafe lens to get the specified team's thread interface. Assumes
-- the interface is present; if not, this crashes. Intended for places
-- where you know the interface will be present due to other state and
-- don't want to deal with Maybe.
unsafeThreadInterface :: (HasCallStack) => TeamId -> Lens' ChatState ThreadInterface
unsafeThreadInterface :: TeamId -> Lens' ChatState ThreadInterface
unsafeThreadInterface TeamId
tId = TeamId -> Lens' ChatState (Maybe ThreadInterface)
maybeThreadInterface(TeamId
tId)((Maybe ThreadInterface -> f (Maybe ThreadInterface))
 -> ChatState -> f ChatState)
-> ((ThreadInterface -> f ThreadInterface)
    -> Maybe ThreadInterface -> f (Maybe ThreadInterface))
-> (ThreadInterface -> f ThreadInterface)
-> ChatState
-> f ChatState
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Traversal
  (Maybe ThreadInterface)
  (Maybe ThreadInterface)
  ThreadInterface
  ThreadInterface
-> Lens
     (Maybe ThreadInterface)
     (Maybe ThreadInterface)
     ThreadInterface
     ThreadInterface
forall s t a. HasCallStack => Traversal s t a a -> Lens s t a a
singular forall a a'. Traversal (Maybe a) (Maybe a') a a'
Traversal
  (Maybe ThreadInterface)
  (Maybe ThreadInterface)
  ThreadInterface
  ThreadInterface
_Just

-- A safe version of unsafeThreadInterface.
maybeThreadInterface :: TeamId -> Lens' ChatState (Maybe ThreadInterface)
maybeThreadInterface :: TeamId -> Lens' ChatState (Maybe ThreadInterface)
maybeThreadInterface TeamId
tId = TeamId -> Lens' ChatState TeamState
csTeam(TeamId
tId)((TeamState -> f TeamState) -> ChatState -> f ChatState)
-> ((Maybe ThreadInterface -> f (Maybe ThreadInterface))
    -> TeamState -> f TeamState)
-> (Maybe ThreadInterface -> f (Maybe ThreadInterface))
-> ChatState
-> f ChatState
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Maybe ThreadInterface -> f (Maybe ThreadInterface))
-> TeamState -> f TeamState
Lens' TeamState (Maybe ThreadInterface)
tsThreadInterface

threadInterfaceEmpty :: TeamId -> MH Bool
threadInterfaceEmpty :: TeamId -> MH Bool
threadInterfaceEmpty TeamId
tId = do
    Maybe Int
mLen <- Getting (First Int) ChatState Int -> MH (Maybe Int)
forall s (m :: * -> *) a.
MonadState s m =>
Getting (First a) s a -> m (Maybe a)
preuse (TeamId -> Lens' ChatState (Maybe ThreadInterface)
maybeThreadInterface(TeamId
tId)((Maybe ThreadInterface
  -> Const (First Int) (Maybe ThreadInterface))
 -> ChatState -> Const (First Int) ChatState)
-> ((Int -> Const (First Int) Int)
    -> Maybe ThreadInterface
    -> Const (First Int) (Maybe ThreadInterface))
-> Getting (First Int) ChatState Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(ThreadInterface -> Const (First Int) ThreadInterface)
-> Maybe ThreadInterface
-> Const (First Int) (Maybe ThreadInterface)
forall a a'. Traversal (Maybe a) (Maybe a') a a'
_Just((ThreadInterface -> Const (First Int) ThreadInterface)
 -> Maybe ThreadInterface
 -> Const (First Int) (Maybe ThreadInterface))
-> ((Int -> Const (First Int) Int)
    -> ThreadInterface -> Const (First Int) ThreadInterface)
-> (Int -> Const (First Int) Int)
-> Maybe ThreadInterface
-> Const (First Int) (Maybe ThreadInterface)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Messages -> Const (First Int) Messages)
-> ThreadInterface -> Const (First Int) ThreadInterface
forall n i. Lens' (MessageInterface n i) Messages
miMessages((Messages -> Const (First Int) Messages)
 -> ThreadInterface -> Const (First Int) ThreadInterface)
-> ((Int -> Const (First Int) Int)
    -> Messages -> Const (First Int) Messages)
-> (Int -> Const (First Int) Int)
-> ThreadInterface
-> Const (First Int) ThreadInterface
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Messages -> Int) -> SimpleGetter Messages Int
forall s a. (s -> a) -> SimpleGetter s a
to Messages -> Int
forall seq a. DirectionalSeq seq a -> Int
messagesLength)
    case Maybe Int
mLen of
        Maybe Int
Nothing -> Bool -> MH Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
        Just Int
len -> Bool -> MH Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> MH Bool) -> Bool -> MH Bool
forall a b. (a -> b) -> a -> b
$ Int
len Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0

withThreadInterface :: TeamId -> ChannelId -> MH () -> MH ()
withThreadInterface :: TeamId -> ChannelId -> MH () -> MH ()
withThreadInterface TeamId
tId ChannelId
cId MH ()
act = do
    Maybe ChannelId
mCid <- Getting (First ChannelId) ChatState ChannelId
-> MH (Maybe ChannelId)
forall s (m :: * -> *) a.
MonadState s m =>
Getting (First a) s a -> m (Maybe a)
preuse (TeamId -> Lens' ChatState (Maybe ThreadInterface)
maybeThreadInterface(TeamId
tId)((Maybe ThreadInterface
  -> Const (First ChannelId) (Maybe ThreadInterface))
 -> ChatState -> Const (First ChannelId) ChatState)
-> ((ChannelId -> Const (First ChannelId) ChannelId)
    -> Maybe ThreadInterface
    -> Const (First ChannelId) (Maybe ThreadInterface))
-> Getting (First ChannelId) ChatState ChannelId
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(ThreadInterface -> Const (First ChannelId) ThreadInterface)
-> Maybe ThreadInterface
-> Const (First ChannelId) (Maybe ThreadInterface)
forall a a'. Traversal (Maybe a) (Maybe a') a a'
_Just((ThreadInterface -> Const (First ChannelId) ThreadInterface)
 -> Maybe ThreadInterface
 -> Const (First ChannelId) (Maybe ThreadInterface))
-> ((ChannelId -> Const (First ChannelId) ChannelId)
    -> ThreadInterface -> Const (First ChannelId) ThreadInterface)
-> (ChannelId -> Const (First ChannelId) ChannelId)
-> Maybe ThreadInterface
-> Const (First ChannelId) (Maybe ThreadInterface)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(ChannelId -> Const (First ChannelId) ChannelId)
-> ThreadInterface -> Const (First ChannelId) ThreadInterface
forall n i. Lens' (MessageInterface n i) ChannelId
miChannelId)
    case Maybe ChannelId
mCid of
        Just ChannelId
i | ChannelId
i ChannelId -> ChannelId -> Bool
forall a. Eq a => a -> a -> Bool
== ChannelId
cId -> MH ()
act
        Maybe ChannelId
_ -> () -> MH ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()

threadInterfaceDeleteWhere :: TeamId -> ChannelId -> (Message -> Bool) -> MH ()
threadInterfaceDeleteWhere :: TeamId -> ChannelId -> (Message -> Bool) -> MH ()
threadInterfaceDeleteWhere TeamId
tId ChannelId
cId Message -> Bool
f =
    TeamId -> ChannelId -> MH () -> MH ()
withThreadInterface TeamId
tId ChannelId
cId (MH () -> MH ()) -> MH () -> MH ()
forall a b. (a -> b) -> a -> b
$ do
        TeamId -> Lens' ChatState (Maybe ThreadInterface)
maybeThreadInterface(TeamId
tId)((Maybe ThreadInterface -> Identity (Maybe ThreadInterface))
 -> ChatState -> Identity ChatState)
-> ((Message -> Identity Message)
    -> Maybe ThreadInterface -> Identity (Maybe ThreadInterface))
-> (Message -> Identity Message)
-> ChatState
-> Identity ChatState
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(ThreadInterface -> Identity ThreadInterface)
-> Maybe ThreadInterface -> Identity (Maybe ThreadInterface)
forall a a'. Traversal (Maybe a) (Maybe a') a a'
_Just((ThreadInterface -> Identity ThreadInterface)
 -> Maybe ThreadInterface -> Identity (Maybe ThreadInterface))
-> ((Message -> Identity Message)
    -> ThreadInterface -> Identity ThreadInterface)
-> (Message -> Identity Message)
-> Maybe ThreadInterface
-> Identity (Maybe ThreadInterface)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Messages -> Identity Messages)
-> ThreadInterface -> Identity ThreadInterface
forall n i. Lens' (MessageInterface n i) Messages
miMessages((Messages -> Identity Messages)
 -> ThreadInterface -> Identity ThreadInterface)
-> ((Message -> Identity Message) -> Messages -> Identity Messages)
-> (Message -> Identity Message)
-> ThreadInterface
-> Identity ThreadInterface
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Message -> Identity Message) -> Messages -> Identity Messages
forall (f :: * -> *) a b.
Traversable f =>
Traversal (f a) (f b) a b
traversed((Message -> Identity Message) -> Messages -> Identity Messages)
-> ((Message -> Identity Message) -> Message -> Identity Message)
-> (Message -> Identity Message)
-> Messages
-> Identity Messages
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Message -> Bool) -> Traversal' Message Message
forall a. (a -> Bool) -> Traversal' a a
filtered Message -> Bool
f ((Message -> Identity Message) -> ChatState -> Identity ChatState)
-> (Message -> Message) -> MH ()
forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> (a -> b) -> m ()
%=
            (Message -> (Message -> Message) -> Message
forall a b. a -> (a -> b) -> b
& (Bool -> Identity Bool) -> Message -> Identity Message
Lens' Message Bool
mDeleted ((Bool -> Identity Bool) -> Message -> Identity Message)
-> Bool -> Message -> Message
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Bool
True)

modifyThreadMessages :: TeamId -> ChannelId -> (Messages -> Messages) -> MH ()
modifyThreadMessages :: TeamId -> ChannelId -> (Messages -> Messages) -> MH ()
modifyThreadMessages TeamId
tId ChannelId
cId Messages -> Messages
f = do
    TeamId -> ChannelId -> MH () -> MH ()
withThreadInterface TeamId
tId ChannelId
cId (MH () -> MH ()) -> MH () -> MH ()
forall a b. (a -> b) -> a -> b
$ do
        TeamId -> Lens' ChatState (Maybe ThreadInterface)
maybeThreadInterface(TeamId
tId)((Maybe ThreadInterface -> Identity (Maybe ThreadInterface))
 -> ChatState -> Identity ChatState)
-> ((Messages -> Identity Messages)
    -> Maybe ThreadInterface -> Identity (Maybe ThreadInterface))
-> (Messages -> Identity Messages)
-> ChatState
-> Identity ChatState
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(ThreadInterface -> Identity ThreadInterface)
-> Maybe ThreadInterface -> Identity (Maybe ThreadInterface)
forall a a'. Traversal (Maybe a) (Maybe a') a a'
_Just((ThreadInterface -> Identity ThreadInterface)
 -> Maybe ThreadInterface -> Identity (Maybe ThreadInterface))
-> ((Messages -> Identity Messages)
    -> ThreadInterface -> Identity ThreadInterface)
-> (Messages -> Identity Messages)
-> Maybe ThreadInterface
-> Identity (Maybe ThreadInterface)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Messages -> Identity Messages)
-> ThreadInterface -> Identity ThreadInterface
forall n i. Lens' (MessageInterface n i) Messages
miMessages ((Messages -> Identity Messages)
 -> ChatState -> Identity ChatState)
-> (Messages -> Messages) -> MH ()
forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> (a -> b) -> m ()
%= Messages -> Messages
f

modifyEachThreadMessage :: TeamId -> ChannelId -> (Message -> Message) -> MH ()
modifyEachThreadMessage :: TeamId -> ChannelId -> (Message -> Message) -> MH ()
modifyEachThreadMessage TeamId
tId ChannelId
cId Message -> Message
f = do
    TeamId -> ChannelId -> MH () -> MH ()
withThreadInterface TeamId
tId ChannelId
cId (MH () -> MH ()) -> MH () -> MH ()
forall a b. (a -> b) -> a -> b
$ do
        TeamId -> Lens' ChatState (Maybe ThreadInterface)
maybeThreadInterface(TeamId
tId)((Maybe ThreadInterface -> Identity (Maybe ThreadInterface))
 -> ChatState -> Identity ChatState)
-> ((Message -> Identity Message)
    -> Maybe ThreadInterface -> Identity (Maybe ThreadInterface))
-> (Message -> Identity Message)
-> ChatState
-> Identity ChatState
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(ThreadInterface -> Identity ThreadInterface)
-> Maybe ThreadInterface -> Identity (Maybe ThreadInterface)
forall a a'. Traversal (Maybe a) (Maybe a') a a'
_Just((ThreadInterface -> Identity ThreadInterface)
 -> Maybe ThreadInterface -> Identity (Maybe ThreadInterface))
-> ((Message -> Identity Message)
    -> ThreadInterface -> Identity ThreadInterface)
-> (Message -> Identity Message)
-> Maybe ThreadInterface
-> Identity (Maybe ThreadInterface)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Messages -> Identity Messages)
-> ThreadInterface -> Identity ThreadInterface
forall n i. Lens' (MessageInterface n i) Messages
miMessages((Messages -> Identity Messages)
 -> ThreadInterface -> Identity ThreadInterface)
-> ((Message -> Identity Message) -> Messages -> Identity Messages)
-> (Message -> Identity Message)
-> ThreadInterface
-> Identity ThreadInterface
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Message -> Identity Message) -> Messages -> Identity Messages
forall (f :: * -> *) a b.
Traversable f =>
Traversal (f a) (f b) a b
traversed ((Message -> Identity Message) -> ChatState -> Identity ChatState)
-> (Message -> Message) -> MH ()
forall s (m :: * -> *) a b.
MonadState s m =>
ASetter s s a b -> (a -> b) -> m ()
%= Message -> Message
f