<commit_before>module Matterhorn.Events.Websocket
  ( handleWebsocketEvent
  , handleWebsocketActionResponse
  )
where

import           Prelude ()
import           Matterhorn.Prelude

import qualified Data.HashMap.Strict as HM
import qualified Data.Sequence as Seq
import qualified Data.Set as Set
import qualified Data.Text as T
import           Lens.Micro.Platform ( preuse )

import           Network.Mattermost.Lenses
import           Network.Mattermost.Types
import           Network.Mattermost.WebSocket

import           Matterhorn.State.ChannelList
import           Matterhorn.State.Channels
import           Matterhorn.State.Common
import           Matterhorn.State.Flagging
import           Matterhorn.State.Messages
import           Matterhorn.State.Reactions
import           Matterhorn.State.Teams
import           Matterhorn.State.Users
import           Matterhorn.Types
import           Matterhorn.Types.Common


handleWebsocketEvent :: WebsocketEvent -> MH ()
handleWebsocketEvent we = do
    myId <- gets myUserId
    ts <- use csTeams
    let myTIds = HM.keys ts
        -- The team ID is one of the teams we're in, or the team ID is
        -- absent, which typically indicates a DM channel event since DM
        -- channels are not associated with teams.
        inMyTeamOrDM (Just i) = i `elem` myTIds
        inMyTeamOrDM Nothing = True

        -- The team ID is one of the teams we're in. A missing team ID
        -- yields False.
        inMyTeam (Just i) = i `elem` myTIds
        inMyTeam Nothing = False

    case weEvent we of
        WMPosted
            | Just p <- wepPost (weData we) ->
                when (inMyTeamOrDM (wepTeamId (weData we))) $ do
                    let wasMentioned = maybe False (Set.member myId) $ wepMentions (weData we)
                    addNewPostedMessage $ RecentPost p wasMentioned

                    currTId <- use csCurrentTeamId
                    currCId <- use (csCurrentChannelId currTId)
                    case wepTeamId $ weData we of
                        Nothing -> do
                            forM_ myTIds $ \tId -> do
                                when (tId /= currTId || postChannelId p /= currCId) $
                                   showChannelInSidebar (p^.postChannelIdL) False
                        Just tId -> do
                            cId <- use (csCurrentChannelId tId)
                            when (postChannelId p /= cId) $
                                showChannelInSidebar (p^.postChannelIdL) False

            | otherwise -> return ()

        WMPostEdited
            | Just p <- wepPost (weData we) -> do
                editMessage p

                currTid <- use csCurrentTeamId
                forM_ myTIds $ \tId -> do
                    cId <- use (csCurrentChannelId tId)
                    when (postChannelId p == cId && tId == currTid) $
                        updateViewed False
                    when (postChannelId p /= cId) $
                        showChannelInSidebar (p^.postChannelIdL) False
            | otherwise -> return ()

        WMPostDeleted
            | Just p <- wepPost (weData we) -> do
                deleteMessage p

                currTid <- use csCurrentTeamId
                forM_ myTIds $ \tId -> do
                    cId <- use (csCurrentChannelId tId)
                    when (postChannelId p == cId && tId == currTid) $
                        updateViewed False
                    when (postChannelId p /= cId) $
                        showChannelInSidebar (p^.postChannelIdL) False
            | otherwise -> return ()

        WMStatusChange
            | Just status <- wepStatus (weData we)
            , Just uId <- wepUserId (weData we) ->
                setUserStatus uId status
            | otherwise -> return ()
<commit_msg>Events.Websocket: factor out foreachTeam<commit_after>module Matterhorn.Events.Websocket
  ( handleWebsocketEvent
  , handleWebsocketActionResponse
  )
where

import           Prelude ()
import           Matterhorn.Prelude

import qualified Data.HashMap.Strict as HM
import qualified Data.Sequence as Seq
import qualified Data.Set as Set
import qualified Data.Text as T
import           Lens.Micro.Platform ( preuse )

import           Network.Mattermost.Lenses
import           Network.Mattermost.Types
import           Network.Mattermost.WebSocket

import           Matterhorn.State.ChannelList
import           Matterhorn.State.Channels
import           Matterhorn.State.Common
import           Matterhorn.State.Flagging
import           Matterhorn.State.Messages
import           Matterhorn.State.Reactions
import           Matterhorn.State.Teams
import           Matterhorn.State.Users
import           Matterhorn.Types
import           Matterhorn.Types.Common


foreachTeam :: (TeamId -> MH ()) -> MH ()
foreachTeam act = do
    ts <- use csTeams
    let myTIds = HM.keys ts
    mapM_ act myTIds

handleWebsocketEvent :: WebsocketEvent -> MH ()
handleWebsocketEvent we = do
    myId <- gets myUserId
    ts <- use csTeams
    let myTIds = HM.keys ts
        -- The team ID is one of the teams we're in, or the team ID is
        -- absent, which typically indicates a DM channel event since DM
        -- channels are not associated with teams.
        inMyTeamOrDM (Just i) = i `elem` myTIds
        inMyTeamOrDM Nothing = True

        -- The team ID is one of the teams we're in. A missing team ID
        -- yields False.
        inMyTeam (Just i) = i `elem` myTIds
        inMyTeam Nothing = False

    case weEvent we of
        WMPosted
            | Just p <- wepPost (weData we) ->
                when (inMyTeamOrDM (wepTeamId (weData we))) $ do
                    let wasMentioned = maybe False (Set.member myId) $ wepMentions (weData we)
                    addNewPostedMessage $ RecentPost p wasMentioned

                    currTId <- use csCurrentTeamId
                    currCId <- use (csCurrentChannelId currTId)
                    case wepTeamId $ weData we of
                        Nothing -> do
                            foreachTeam $ \tId -> do
                                when (tId /= currTId || postChannelId p /= currCId) $
                                   showChannelInSidebar (p^.postChannelIdL) False
                        Just tId -> do
                            cId <- use (csCurrentChannelId tId)
                            when (postChannelId p /= cId) $
                                showChannelInSidebar (p^.postChannelIdL) False

            | otherwise -> return ()

        WMPostEdited
            | Just p <- wepPost (weData we) -> do
                editMessage p

                currTid <- use csCurrentTeamId
                foreachTeam $ \tId -> do
                    cId <- use (csCurrentChannelId tId)
                    when (postChannelId p == cId && tId == currTid) $
                        updateViewed False
                    when (postChannelId p /= cId) $
                        showChannelInSidebar (p^.postChannelIdL) False
            | otherwise -> return ()

        WMPostDeleted
            | Just p <- wepPost (weData we) -> do
                deleteMessage p

                currTid <- use csCurrentTeamId
                foreachTeam $ \tId -> do
                    cId <- use (csCurrentChannelId tId)
                    when (postChannelId p == cId && tId == currTid) $
                        updateViewed False
                    when (postChannelId p /= cId) $
                        showChannelInSidebar (p^.postChannelIdL) False
            | otherwise -> return ()

        WMStatusChange
            | Just status <- wepStatus (weData we)
            , Just uId <- wepUserId (weData we) ->
                setUserStatus uId status
            | otherwise -> return ()

{'fraction_non_alphanumeric': 0.06709429121231558, 'fraction_numerical': 0.0, 'mean_word_length': 1.4624131396083386, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1346504', 'n_tokens_mistral': 2128, 'n_tokens_neox': 2024, 'n_words': 619}