module HZulip.Types where
import Control.Applicative ((<$>), (<*>), pure)
import Control.Monad (mzero)
import Data.Aeson
data ZulipClient = ZulipClient { clientEmail :: String
, clientApiKey :: String
, clientBaseUrl :: String
}
data Response = Response { responseResult :: ResponseResult
, responseMsg :: String
, responseMessageId :: Maybe Int
, responseQueueId :: Maybe String
, responseLastEventId :: Maybe Int
, responseEvents :: Maybe [Event]
}
instance FromJSON Response where
parseJSON (Object o) = Response <$>
o .: "result" <*>
o .: "msg" <*>
o .:? "id" <*>
o .:? "queue_id" <*>
o .:? "last_event_id" <*>
o .:? "events"
parseJSON _ = mzero
data ResponseResult = ResponseError | ResponseSuccess
deriving(Eq, Show, Ord)
instance FromJSON ResponseResult where
parseJSON (String "success") = pure ResponseSuccess
parseJSON _ = pure ResponseError
data Event = Event { eventType :: String
, eventId :: Int
, eventMessage :: Maybe Message
}
instance FromJSON Event where
parseJSON (Object o) = Event <$>
o .: "type" <*>
o .: "id" <*>
o .:? "message"
parseJSON _ = mzero
data Message = Message { messageId :: Int
, messageType :: String
, messageContent :: String
, messageAvatarUrl :: String
, messageTimestamp :: Int
, messageSender :: User
, messageGravatarHash :: String
, messageRecipientId :: Int
, messageClient :: String
, messageSubjectLinks :: [String]
, messageSubject :: String
}
instance FromJSON Message where
parseJSON (Object o) = Message <$>
o .: "id" <*>
o .: "type" <*>
o .: "content" <*>
o .: "avatar_url" <*>
o .: "timestamp" <*>
(User <$>
o .: "sender_id" <*>
o .: "sender_full_name" <*>
o .: "sender_domain" <*>
o .: "sender_email" <*>
o .: "sender_short_name"
) <*>
o .: "gravatar_hash" <*>
o .: "recipient_id" <*>
o .: "client" <*>
o .: "subject_links" <*>
o .: "subject"
parseJSON _ = mzero
data User = User { userId :: Int
, userFullName :: String
, userEmail :: String
, userDomain :: String
, userShortName :: String
}
instance FromJSON User where
parseJSON (Object o) = User <$>
o .: "id" <*>
o .: "full_name" <*>
o .: "domain" <*>
o .: "email" <*>
o .: "short_name"
parseJSON _ = mzero
data Queue = Queue { queueId :: String
, lastEventId :: Int
}
type EventCallback = Event -> IO ()
type MessageCallback = Message -> IO ()