module Matterhorn.State.Links
  ( openLinkTarget
  )
where

import           Prelude ()
import           Matterhorn.Prelude

import qualified Data.Text as T

import           Matterhorn.State.Common
import           Matterhorn.State.Messages ( jumpToPost )
import           Matterhorn.Types
import           Matterhorn.Types.RichText ( unURL )


openLinkTarget :: LinkTarget -> MH Bool
openLinkTarget :: LinkTarget -> MH Bool
openLinkTarget LinkTarget
target = do
    Session
session <- MH Session
getSession
    case LinkTarget
target of
        LinkURL URL
url -> MH String -> MH Bool
openWithOpener (String -> MH String
forall (m :: * -> *) a. Monad m => a -> m a
return (String -> MH String) -> String -> MH 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 String -> MH Bool
openWithOpener (IO String -> MH String
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO String -> MH String) -> IO String -> MH String
forall a b. (a -> b) -> a -> b
$ FileId -> Session -> IO String
prepareAttachment FileId
fId Session
session)
        LinkPermalink TeamURLName
_ PostId
pId -> PostId -> MH ()
jumpToPost PostId
pId MH () -> MH Bool -> MH Bool
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Bool -> MH Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True