<commit_before>module State.Messages
    ( addDisconnectGaps
    , removeEndGaps
    , lastMsg
    )
    where


import           Control.Monad (unless)
import qualified Data.Text as T
import           Lens.Micro.Platform
import           Network.Mattermost
import           Network.Mattermost.Types
import           TimeUtils
import           Types
import           Types.Channels
import           Types.Messages
import           Types.Posts


-- | Called to add an UnknownGap to the end of the Messages collection
-- for all channels when the client has become disconnected from the
-- server.  This gaps will later be removed by successful fetching
-- overlaps if the connection is re-established.  Note that the
-- disconnect is re-iterated periodically via a re-connect timer
-- attempt, so do not duplicate gaps.
addDisconnectGaps :: MH ()
addDisconnectGaps = mapM_ addEndGap . filteredChannelIds (const True) =<< use csChannels


addEndGap :: ChannelId -> MH ()
addEndGap cId = withChannelOrDefault cId () $ \chan ->
    let lastmsg_ = chan^.ccContents.cdMessages.to reverseMessages.to lastMsg
        lastIsGap = maybe False isGap lastmsg_
        gapMsg = newGapMessage timeJustAfterLast
        timeJustAfterLast = maybe t0 (justAfter . _mDate) lastmsg_
        t0 = ServerTime $ originTime  -- use any time for a channel with no messages yet
        newGapMessage = newMessageOfType (T.pack "Disconnected... will update when connected") (C UnknownGap)
    in unless lastIsGap
           (csChannels %= modifyChannelById cId (ccContents.cdMessages %~ addMessage gapMsg))

removeEndGaps :: ChannelId -> MH ()
removeEndGaps cId =
        csChannel(cId).ccContents.cdMessages %=
                     (unreverseMessages .
                      snd . snd . splitRetrogradeMessagesOn (not . isGap) .
                      reverseMessages)


lastMsg :: RetrogradeMessages -> Maybe Message
lastMsg = withFirstMessage id
<commit_msg>Simplify addEndGap operation.<commit_after>module State.Messages
    ( addDisconnectGaps
    , removeEndGaps
    , lastMsg
    )
    where


import           Control.Monad (unless)
import qualified Data.Text as T
import           Lens.Micro.Platform
import           Network.Mattermost
import           Network.Mattermost.Types
import           TimeUtils
import           Types
import           Types.Channels
import           Types.Messages
import           Types.Posts


-- | Called to add an UnknownGap to the end of the Messages collection
-- for all channels when the client has become disconnected from the
-- server.  This gaps will later be removed by successful fetching
-- overlaps if the connection is re-established.  Note that the
-- disconnect is re-iterated periodically via a re-connect timer
-- attempt, so do not duplicate gaps.
addDisconnectGaps :: MH ()
addDisconnectGaps = mapM_ addEndGap . filteredChannelIds (const True) =<< use csChannels


addEndGap :: ChannelId -> MH ()
addEndGap cId = withChannel cId $ \chan ->
    let lastmsg_ = chan^.ccContents.cdMessages.to reverseMessages.to lastMsg
        lastIsGap = maybe False isGap lastmsg_
        gapMsg = newGapMessage timeJustAfterLast
        timeJustAfterLast = maybe t0 (justAfter . _mDate) lastmsg_
        t0 = ServerTime $ originTime  -- use any time for a channel with no messages yet
        newGapMessage = newMessageOfType (T.pack "Disconnected... will update when connected") (C UnknownGap)
    in unless lastIsGap
           (csChannels %= modifyChannelById cId (ccContents.cdMessages %~ addMessage gapMsg))

removeEndGaps :: ChannelId -> MH ()
removeEndGaps cId =
        csChannel(cId).ccContents.cdMessages %=
                     (unreverseMessages .
                      snd . snd . splitRetrogradeMessagesOn (not . isGap) .
                      reverseMessages)


lastMsg :: RetrogradeMessages -> Maybe Message
lastMsg = withFirstMessage id

{'fraction_non_alphanumeric': 0.06534090909090909, 'fraction_numerical': 0.0010330578512396695, 'mean_word_length': 2.64346190028222, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3061054', 'n_tokens_mistral': 1054, 'n_tokens_neox': 1026, 'n_words': 352}