module Matterhorn.State.Links
  ( openLinkTarget
  )
where

import           Prelude ()
import           Matterhorn.Prelude

import           Control.Exception ( SomeException, catch )
import qualified Data.Text as T

import           Network.Mattermost.Exceptions
import           Network.Mattermost.Types

import           Matterhorn.State.Common
import {-# SOURCE #-} Matterhorn.State.Messages ( jumpToPost )
import           Matterhorn.Types
import           Matterhorn.Types.RichText ( unURL )


openLinkTarget :: LinkTarget -> MH ()
openLinkTarget :: LinkTarget -> MH ()
openLinkTarget LinkTarget
target = do
    Session
session <- MH Session
getSession
    case LinkTarget
target of
        LinkURL URL
url -> MH (Either MHError String) -> MH ()
openWithOpener (Either MHError String -> MH (Either MHError String)
forall a. a -> MH a
forall (m :: * -> *) a. Monad m => a -> m a
return (Either MHError String -> MH (Either MHError String))
-> Either MHError String -> MH (Either MHError String)
forall a b. (a -> b) -> a -> b
$ String -> Either MHError String
forall a b. b -> Either a b
Right (String -> Either MHError String)
-> String -> Either MHError String
forall a b. (a -> b) -> a -> b
$ Text -> String
T.unpack (Text -> String) -> Text -> String
forall a b. (a -> b) -> a -> b
$ URL -> Text
unURL URL
url)
        LinkFileId FileId
fId -> MH (Either MHError String) -> MH ()
openWithOpener (FileId -> Session -> MH (Either MHError String)
fetchAttachment FileId
fId Session
session)
        LinkPermalink TeamURLName
_ PostId
pId -> PostId -> MH ()
jumpToPost PostId
pId

fetchAttachment :: FileId -> Session -> MH (Either MHError String)
fetchAttachment :: FileId -> Session -> MH (Either MHError String)
fetchAttachment FileId
fId Session
session =
    IO (Either MHError String) -> MH (Either MHError String)
forall a. IO a -> MH a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Either MHError String) -> MH (Either MHError String))
-> IO (Either MHError String) -> MH (Either MHError String)
forall a b. (a -> b) -> a -> b
$ (String -> Either MHError String
forall a b. b -> Either a b
Right (String -> Either MHError String)
-> IO String -> IO (Either MHError String)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FileId -> Session -> IO String
fetchFile FileId
fId Session
session)
        IO (Either MHError String)
-> (MattermostError -> IO (Either MHError String))
-> IO (Either MHError String)
forall e a. Exception e => IO a -> (e -> IO a) -> IO a
`catch` (\(MattermostError
e::MattermostError) -> Either MHError String -> IO (Either MHError String)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Either MHError String -> IO (Either MHError String))
-> Either MHError String -> IO (Either MHError String)
forall a b. (a -> b) -> a -> b
$ MHError -> Either MHError String
forall a b. a -> Either a b
Left (MHError -> Either MHError String)
-> MHError -> Either MHError String
forall a b. (a -> b) -> a -> b
$ MattermostError -> MHError
ServerError MattermostError
e)
        IO (Either MHError String)
-> (SomeException -> IO (Either MHError String))
-> IO (Either MHError String)
forall e a. Exception e => IO a -> (e -> IO a) -> IO a
`catch` (\(SomeException
e::SomeException) -> Either MHError String -> IO (Either MHError String)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Either MHError String -> IO (Either MHError String))
-> Either MHError String -> IO (Either MHError String)
forall a b. (a -> b) -> a -> b
$ MHError -> Either MHError String
forall a b. a -> Either a b
Left (MHError -> Either MHError String)
-> MHError -> Either MHError String
forall a b. (a -> b) -> a -> b
$ Text -> MHError
GenericError (Text -> MHError) -> Text -> MHError
forall a b. (a -> b) -> a -> b
$ String -> Text
T.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ SomeException -> String
forall a. Show a => a -> String
show SomeException
e)