-- This file is part of the Wire Server implementation.
--
-- Copyright (C) 2022 Wire Swiss GmbH <opensource@wire.com>
--
-- This program is free software: you can redistribute it and/or modify it under
-- the terms of the GNU Affero General Public License as published by the Free
-- Software Foundation, either version 3 of the License, or (at your option) any
-- later version.
--
-- This program is distributed in the hope that it will be useful, but WITHOUT
-- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
-- FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
-- details.
--
-- You should have received a copy of the GNU Affero General Public License along
-- with this program. If not, see <https://www.gnu.org/licenses/>.

module Galley.Cassandra.Conversation
  ( createConversation,
    deleteConversation,
    interpretConversationStoreToCassandra,
  )
where

import Cassandra hiding (Set)
import Cassandra qualified as Cql
import Cassandra.Util
import Control.Error.Util
import Control.Monad.Trans.Maybe
import Data.ByteString.Conversion
import Data.Id
import Data.Map qualified as Map
import Data.Misc
import Data.Qualified
import Data.Range
import Data.Set qualified as Set
import Data.Time.Clock
import Data.UUID.V4 (nextRandom)
import Galley.Cassandra.Access
import Galley.Cassandra.Conversation.MLS
import Galley.Cassandra.Conversation.Members
import Galley.Cassandra.Queries qualified as Cql
import Galley.Cassandra.Store
import Galley.Cassandra.Util
import Galley.Data.Conversation
import Galley.Data.Conversation.Types
import Galley.Effects.ConversationStore (ConversationStore (..))
import Galley.Types.Conversations.Members
import Galley.Types.ToUserRole
import Galley.Types.UserList
import Imports
import Polysemy
import Polysemy.Input
import Polysemy.TinyLog
import System.Logger qualified as Log
import UnliftIO qualified
import Wire.API.Conversation hiding (Conversation, Member)
import Wire.API.Conversation.Protocol
import Wire.API.MLS.CipherSuite
import Wire.API.MLS.Group.Serialisation
import Wire.API.MLS.GroupInfo
import Wire.API.MLS.SubConversation
import Wire.API.User

createMLSSelfConversation ::
  Local UserId ->
  Client Conversation
createMLSSelfConversation :: Local UserId -> Client Conversation
createMLSSelfConversation Local UserId
lusr = do
  let cnv :: ConvId
cnv = UserId -> ConvId
mlsSelfConvId (UserId -> ConvId)
-> (Local UserId -> UserId) -> Local UserId -> ConvId
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Local UserId -> UserId
forall (t :: QTag) a. QualifiedWithTag t a -> a
tUnqualified (Local UserId -> ConvId) -> Local UserId -> ConvId
forall a b. (a -> b) -> a -> b
$ Local UserId
lusr
      usr :: UserId
usr = Local UserId -> UserId
forall (t :: QTag) a. QualifiedWithTag t a -> a
tUnqualified Local UserId
lusr
      nc :: NewConversation
nc =
        NewConversation
          { $sel:ncMetadata:NewConversation :: ConversationMetadata
ncMetadata =
              (Maybe UserId -> ConversationMetadata
defConversationMetadata (UserId -> Maybe UserId
forall a. a -> Maybe a
Just UserId
usr)) {cnvmType = SelfConv},
            $sel:ncUsers:NewConversation :: UserList (UserId, RoleName)
ncUsers = [(UserId, RoleName)] -> UserList (UserId, RoleName)
forall a. [a] -> UserList a
ulFromLocals [UserId -> (UserId, RoleName)
forall a. ToUserRole a => a -> (UserId, RoleName)
toUserRole UserId
usr],
            $sel:ncProtocol:NewConversation :: BaseProtocolTag
ncProtocol = BaseProtocolTag
BaseProtocolMLSTag
          }
      meta :: ConversationMetadata
meta = NewConversation -> ConversationMetadata
ncMetadata NewConversation
nc
      gid :: GroupId
gid = GroupIdParts -> GroupId
convToGroupId (GroupIdParts -> GroupId)
-> (ConvId -> GroupIdParts) -> ConvId -> GroupId
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ConvType -> Qualified ConvOrSubConvId -> GroupIdParts
groupIdParts ConversationMetadata
meta.cnvmType (Qualified ConvOrSubConvId -> GroupIdParts)
-> (ConvId -> Qualified ConvOrSubConvId) -> ConvId -> GroupIdParts
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ConvId -> ConvOrSubConvId)
-> Qualified ConvId -> Qualified ConvOrSubConvId
forall a b. (a -> b) -> Qualified a -> Qualified b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ConvId -> ConvOrSubConvId
forall c s. c -> ConvOrSubChoice c s
Conv (Qualified ConvId -> Qualified ConvOrSubConvId)
-> (ConvId -> Qualified ConvId)
-> ConvId
-> Qualified ConvOrSubConvId
forall b c a. (b -> c) -> (a -> b) -> a -> c
. QualifiedWithTag 'QLocal ConvId -> Qualified ConvId
forall (t :: QTag) a. QualifiedWithTag t a -> Qualified a
tUntagged (QualifiedWithTag 'QLocal ConvId -> Qualified ConvId)
-> (ConvId -> QualifiedWithTag 'QLocal ConvId)
-> ConvId
-> Qualified ConvId
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Local UserId -> ConvId -> QualifiedWithTag 'QLocal ConvId
forall (t :: QTag) x a.
QualifiedWithTag t x -> a -> QualifiedWithTag t a
qualifyAs Local UserId
lusr (ConvId -> GroupId) -> ConvId -> GroupId
forall a b. (a -> b) -> a -> b
$ ConvId
cnv
      proto :: Protocol
proto =
        ConversationMLSData -> Protocol
ProtocolMLS
          ConversationMLSData
            { $sel:cnvmlsGroupId:ConversationMLSData :: GroupId
cnvmlsGroupId = GroupId
gid,
              $sel:cnvmlsActiveData:ConversationMLSData :: Maybe ActiveMLSConversationData
cnvmlsActiveData = Maybe ActiveMLSConversationData
forall a. Maybe a
Nothing
            }
  RetrySettings -> Client () -> Client ()
forall (m :: * -> *) a.
MonadClient m =>
RetrySettings -> m a -> m a
retry RetrySettings
x5 (Client () -> Client ())
-> (BatchM () -> Client ()) -> BatchM () -> Client ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BatchM () -> Client ()
forall (m :: * -> *). MonadClient m => BatchM () -> m ()
batch (BatchM () -> Client ()) -> BatchM () -> Client ()
forall a b. (a -> b) -> a -> b
$ do
    BatchType -> BatchM ()
setType BatchType
BatchLogged
    Consistency -> BatchM ()
setConsistency Consistency
LocalQuorum
    PrepQuery
  W
  (ConvId, ConvType, Maybe UserId, Set Access, Set AccessRole,
   Maybe Text, Maybe TeamId, Maybe Milliseconds, Maybe ReceiptMode,
   Maybe GroupId)
  ()
-> (ConvId, ConvType, Maybe UserId, Set Access, Set AccessRole,
    Maybe Text, Maybe TeamId, Maybe Milliseconds, Maybe ReceiptMode,
    Maybe GroupId)
-> BatchM ()
forall a b.
(Show a, Tuple a, Tuple b) =>
PrepQuery W a b -> a -> BatchM ()
addPrepQuery
      PrepQuery
  W
  (ConvId, ConvType, Maybe UserId, Set Access, Set AccessRole,
   Maybe Text, Maybe TeamId, Maybe Milliseconds, Maybe ReceiptMode,
   Maybe GroupId)
  ()
Cql.insertMLSSelfConv
      ( ConvId
cnv,
        ConversationMetadata -> ConvType
cnvmType ConversationMetadata
meta,
        ConversationMetadata -> Maybe UserId
cnvmCreator ConversationMetadata
meta,
        [Access] -> Set Access
forall a. [a] -> Set a
Cql.Set (ConversationMetadata -> [Access]
cnvmAccess ConversationMetadata
meta),
        [AccessRole] -> Set AccessRole
forall a. [a] -> Set a
Cql.Set (Set AccessRole -> [AccessRole]
forall a. Set a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList (ConversationMetadata -> Set AccessRole
cnvmAccessRoles ConversationMetadata
meta)),
        ConversationMetadata -> Maybe Text
cnvmName ConversationMetadata
meta,
        ConversationMetadata -> Maybe TeamId
cnvmTeam ConversationMetadata
meta,
        ConversationMetadata -> Maybe Milliseconds
cnvmMessageTimer ConversationMetadata
meta,
        ConversationMetadata -> Maybe ReceiptMode
cnvmReceiptMode ConversationMetadata
meta,
        GroupId -> Maybe GroupId
forall a. a -> Maybe a
Just GroupId
gid
      )

  ([LocalMember]
lmems, [RemoteMember]
rmems) <- ConvId
-> UserList (UserId, RoleName)
-> Client ([LocalMember], [RemoteMember])
forall a.
ToUserRole a =>
ConvId -> UserList a -> Client ([LocalMember], [RemoteMember])
addMembers ConvId
cnv (NewConversation -> UserList (UserId, RoleName)
ncUsers NewConversation
nc)
  Conversation -> Client Conversation
forall a. a -> Client a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
    Conversation
      { $sel:convId:Conversation :: ConvId
convId = ConvId
cnv,
        $sel:convLocalMembers:Conversation :: [LocalMember]
convLocalMembers = [LocalMember]
lmems,
        $sel:convRemoteMembers:Conversation :: [RemoteMember]
convRemoteMembers = [RemoteMember]
rmems,
        $sel:convDeleted:Conversation :: Bool
convDeleted = Bool
False,
        $sel:convMetadata:Conversation :: ConversationMetadata
convMetadata = ConversationMetadata
meta,
        $sel:convProtocol:Conversation :: Protocol
convProtocol = Protocol
proto
      }

createConversation :: Local ConvId -> NewConversation -> Client Conversation
createConversation :: QualifiedWithTag 'QLocal ConvId
-> NewConversation -> Client Conversation
createConversation QualifiedWithTag 'QLocal ConvId
lcnv NewConversation
nc = do
  let meta :: ConversationMetadata
meta = NewConversation -> ConversationMetadata
ncMetadata NewConversation
nc
      (Protocol
proto, Maybe GroupId
mgid) = case NewConversation -> BaseProtocolTag
ncProtocol NewConversation
nc of
        BaseProtocolTag
BaseProtocolProteusTag -> (Protocol
ProtocolProteus, Maybe GroupId
forall a. Maybe a
Nothing)
        BaseProtocolTag
BaseProtocolMLSTag ->
          let gid :: GroupId
gid = GroupIdParts -> GroupId
convToGroupId (GroupIdParts -> GroupId)
-> (Qualified ConvOrSubConvId -> GroupIdParts)
-> Qualified ConvOrSubConvId
-> GroupId
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ConvType -> Qualified ConvOrSubConvId -> GroupIdParts
groupIdParts ConversationMetadata
meta.cnvmType (Qualified ConvOrSubConvId -> GroupId)
-> Qualified ConvOrSubConvId -> GroupId
forall a b. (a -> b) -> a -> b
$ ConvId -> ConvOrSubConvId
forall c s. c -> ConvOrSubChoice c s
Conv (ConvId -> ConvOrSubConvId)
-> Qualified ConvId -> Qualified ConvOrSubConvId
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> QualifiedWithTag 'QLocal ConvId -> Qualified ConvId
forall (t :: QTag) a. QualifiedWithTag t a -> Qualified a
tUntagged QualifiedWithTag 'QLocal ConvId
lcnv
           in ( ConversationMLSData -> Protocol
ProtocolMLS
                  ConversationMLSData
                    { $sel:cnvmlsGroupId:ConversationMLSData :: GroupId
cnvmlsGroupId = GroupId
gid,
                      $sel:cnvmlsActiveData:ConversationMLSData :: Maybe ActiveMLSConversationData
cnvmlsActiveData = Maybe ActiveMLSConversationData
forall a. Maybe a
Nothing
                    },
                GroupId -> Maybe GroupId
forall a. a -> Maybe a
Just GroupId
gid
              )
  RetrySettings -> Client () -> Client ()
forall (m :: * -> *) a.
MonadClient m =>
RetrySettings -> m a -> m a
retry RetrySettings
x5 (Client () -> Client ())
-> (BatchM () -> Client ()) -> BatchM () -> Client ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BatchM () -> Client ()
forall (m :: * -> *). MonadClient m => BatchM () -> m ()
batch (BatchM () -> Client ()) -> BatchM () -> Client ()
forall a b. (a -> b) -> a -> b
$ do
    BatchType -> BatchM ()
setType BatchType
BatchLogged
    Consistency -> BatchM ()
setConsistency Consistency
LocalQuorum
    PrepQuery
  W
  (ConvId, ConvType, Maybe UserId, Set Access, Set AccessRole,
   Maybe Text, Maybe TeamId, Maybe Milliseconds, Maybe ReceiptMode,
   ProtocolTag, Maybe GroupId)
  ()
-> (ConvId, ConvType, Maybe UserId, Set Access, Set AccessRole,
    Maybe Text, Maybe TeamId, Maybe Milliseconds, Maybe ReceiptMode,
    ProtocolTag, Maybe GroupId)
-> BatchM ()
forall a b.
(Show a, Tuple a, Tuple b) =>
PrepQuery W a b -> a -> BatchM ()
addPrepQuery
      PrepQuery
  W
  (ConvId, ConvType, Maybe UserId, Set Access, Set AccessRole,
   Maybe Text, Maybe TeamId, Maybe Milliseconds, Maybe ReceiptMode,
   ProtocolTag, Maybe GroupId)
  ()
Cql.insertConv
      ( QualifiedWithTag 'QLocal ConvId -> ConvId
forall (t :: QTag) a. QualifiedWithTag t a -> a
tUnqualified QualifiedWithTag 'QLocal ConvId
lcnv,
        ConversationMetadata -> ConvType
cnvmType ConversationMetadata
meta,
        ConversationMetadata -> Maybe UserId
cnvmCreator ConversationMetadata
meta,
        [Access] -> Set Access
forall a. [a] -> Set a
Cql.Set (ConversationMetadata -> [Access]
cnvmAccess ConversationMetadata
meta),
        [AccessRole] -> Set AccessRole
forall a. [a] -> Set a
Cql.Set (Set AccessRole -> [AccessRole]
forall a. Set a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList (ConversationMetadata -> Set AccessRole
cnvmAccessRoles ConversationMetadata
meta)),
        ConversationMetadata -> Maybe Text
cnvmName ConversationMetadata
meta,
        ConversationMetadata -> Maybe TeamId
cnvmTeam ConversationMetadata
meta,
        ConversationMetadata -> Maybe Milliseconds
cnvmMessageTimer ConversationMetadata
meta,
        ConversationMetadata -> Maybe ReceiptMode
cnvmReceiptMode ConversationMetadata
meta,
        BaseProtocolTag -> ProtocolTag
baseProtocolToProtocol (NewConversation -> BaseProtocolTag
ncProtocol NewConversation
nc),
        Maybe GroupId
mgid
      )
    Maybe TeamId -> (TeamId -> BatchM ()) -> BatchM ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ (ConversationMetadata -> Maybe TeamId
cnvmTeam ConversationMetadata
meta) ((TeamId -> BatchM ()) -> BatchM ())
-> (TeamId -> BatchM ()) -> BatchM ()
forall a b. (a -> b) -> a -> b
$ \TeamId
tid -> PrepQuery W (TeamId, ConvId) () -> (TeamId, ConvId) -> BatchM ()
forall a b.
(Show a, Tuple a, Tuple b) =>
PrepQuery W a b -> a -> BatchM ()
addPrepQuery PrepQuery W (TeamId, ConvId) ()
Cql.insertTeamConv (TeamId
tid, QualifiedWithTag 'QLocal ConvId -> ConvId
forall (t :: QTag) a. QualifiedWithTag t a -> a
tUnqualified QualifiedWithTag 'QLocal ConvId
lcnv)
  ([LocalMember]
lmems, [RemoteMember]
rmems) <- ConvId
-> UserList (UserId, RoleName)
-> Client ([LocalMember], [RemoteMember])
forall a.
ToUserRole a =>
ConvId -> UserList a -> Client ([LocalMember], [RemoteMember])
addMembers (QualifiedWithTag 'QLocal ConvId -> ConvId
forall (t :: QTag) a. QualifiedWithTag t a -> a
tUnqualified QualifiedWithTag 'QLocal ConvId
lcnv) (NewConversation -> UserList (UserId, RoleName)
ncUsers NewConversation
nc)
  Conversation -> Client Conversation
forall a. a -> Client a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
    Conversation
      { $sel:convId:Conversation :: ConvId
convId = QualifiedWithTag 'QLocal ConvId -> ConvId
forall (t :: QTag) a. QualifiedWithTag t a -> a
tUnqualified QualifiedWithTag 'QLocal ConvId
lcnv,
        $sel:convLocalMembers:Conversation :: [LocalMember]
convLocalMembers = [LocalMember]
lmems,
        $sel:convRemoteMembers:Conversation :: [RemoteMember]
convRemoteMembers = [RemoteMember]
rmems,
        $sel:convDeleted:Conversation :: Bool
convDeleted = Bool
False,
        $sel:convMetadata:Conversation :: ConversationMetadata
convMetadata = ConversationMetadata
meta,
        $sel:convProtocol:Conversation :: Protocol
convProtocol = Protocol
proto
      }

deleteConversation :: ConvId -> Client ()
deleteConversation :: ConvId -> Client ()
deleteConversation ConvId
cid = do
  RetrySettings -> Client () -> Client ()
forall (m :: * -> *) a.
MonadClient m =>
RetrySettings -> m a -> m a
retry RetrySettings
x5 (Client () -> Client ()) -> Client () -> Client ()
forall a b. (a -> b) -> a -> b
$ PrepQuery W (Identity ConvId) ()
-> QueryParams (Identity ConvId) -> Client ()
forall (m :: * -> *) a (q :: * -> * -> * -> *).
(MonadClient m, Tuple a, RunQ q) =>
q W a () -> QueryParams a -> m ()
write PrepQuery W (Identity ConvId) ()
Cql.markConvDeleted (Consistency -> Identity ConvId -> QueryParams (Identity ConvId)
forall a. Consistency -> a -> QueryParams a
params Consistency
LocalQuorum (ConvId -> Identity ConvId
forall a. a -> Identity a
Identity ConvId
cid))

  [LocalMember]
localMembers <- ConvId -> Client [LocalMember]
members ConvId
cid
  [RemoteMember]
remoteMembers <- ConvId -> Client [RemoteMember]
lookupRemoteMembers ConvId
cid

  ConvId -> UserList UserId -> Client ()
removeMembersFromLocalConv ConvId
cid (UserList UserId -> Client ()) -> UserList UserId -> Client ()
forall a b. (a -> b) -> a -> b
$
    [UserId] -> [Remote UserId] -> UserList UserId
forall a. [a] -> [Remote a] -> UserList a
UserList (LocalMember -> UserId
lmId (LocalMember -> UserId) -> [LocalMember] -> [UserId]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [LocalMember]
localMembers) (RemoteMember -> Remote UserId
rmId (RemoteMember -> Remote UserId)
-> [RemoteMember] -> [Remote UserId]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [RemoteMember]
remoteMembers)

  RetrySettings -> Client () -> Client ()
forall (m :: * -> *) a.
MonadClient m =>
RetrySettings -> m a -> m a
retry RetrySettings
x5 (Client () -> Client ()) -> Client () -> Client ()
forall a b. (a -> b) -> a -> b
$ PrepQuery W (Identity ConvId) ()
-> QueryParams (Identity ConvId) -> Client ()
forall (m :: * -> *) a (q :: * -> * -> * -> *).
(MonadClient m, Tuple a, RunQ q) =>
q W a () -> QueryParams a -> m ()
write PrepQuery W (Identity ConvId) ()
Cql.deleteConv (Consistency -> Identity ConvId -> QueryParams (Identity ConvId)
forall a. Consistency -> a -> QueryParams a
params Consistency
LocalQuorum (ConvId -> Identity ConvId
forall a. a -> Identity a
Identity ConvId
cid))

conversationMeta :: ConvId -> Client (Maybe ConversationMetadata)
conversationMeta :: ConvId -> Client (Maybe ConversationMetadata)
conversationMeta ConvId
conv =
  ((ConvType, Maybe UserId, Maybe (Set Access),
 Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
 Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
 Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
 Maybe (Writetime Epoch), Maybe CipherSuiteTag)
-> Maybe ConversationMetadata
forall {f :: * -> *} {h} {k} {l} {m} {n} {o}.
Applicative f =>
(ConvType, Maybe UserId, Maybe (Set Access),
 Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
 Maybe TeamId, h, Maybe Milliseconds, Maybe ReceiptMode, k, l, m, n,
 o)
-> f ConversationMetadata
toConvMeta =<<)
    (Maybe
   (ConvType, Maybe UserId, Maybe (Set Access),
    Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
    Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
    Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
    Maybe (Writetime Epoch), Maybe CipherSuiteTag)
 -> Maybe ConversationMetadata)
-> Client
     (Maybe
        (ConvType, Maybe UserId, Maybe (Set Access),
         Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
         Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
         Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
         Maybe (Writetime Epoch), Maybe CipherSuiteTag))
-> Client (Maybe ConversationMetadata)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> RetrySettings
-> Client
     (Maybe
        (ConvType, Maybe UserId, Maybe (Set Access),
         Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
         Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
         Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
         Maybe (Writetime Epoch), Maybe CipherSuiteTag))
-> Client
     (Maybe
        (ConvType, Maybe UserId, Maybe (Set Access),
         Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
         Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
         Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
         Maybe (Writetime Epoch), Maybe CipherSuiteTag))
forall (m :: * -> *) a.
MonadClient m =>
RetrySettings -> m a -> m a
retry RetrySettings
x1 (PrepQuery
  R
  (Identity ConvId)
  (ConvType, Maybe UserId, Maybe (Set Access),
   Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
   Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
   Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
   Maybe (Writetime Epoch), Maybe CipherSuiteTag)
-> QueryParams (Identity ConvId)
-> Client
     (Maybe
        (ConvType, Maybe UserId, Maybe (Set Access),
         Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
         Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
         Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
         Maybe (Writetime Epoch), Maybe CipherSuiteTag))
forall (m :: * -> *) a b (q :: * -> * -> * -> *).
(MonadClient m, Tuple a, Tuple b, RunQ q) =>
q R a b -> QueryParams a -> m (Maybe b)
query1 PrepQuery
  R
  (Identity ConvId)
  (ConvType, Maybe UserId, Maybe (Set Access),
   Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
   Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
   Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
   Maybe (Writetime Epoch), Maybe CipherSuiteTag)
Cql.selectConv (Consistency -> Identity ConvId -> QueryParams (Identity ConvId)
forall a. Consistency -> a -> QueryParams a
params Consistency
LocalQuorum (ConvId -> Identity ConvId
forall a. a -> Identity a
Identity ConvId
conv)))
  where
    toConvMeta :: (ConvType, Maybe UserId, Maybe (Set Access),
 Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
 Maybe TeamId, h, Maybe Milliseconds, Maybe ReceiptMode, k, l, m, n,
 o)
-> f ConversationMetadata
toConvMeta (ConvType
t, Maybe UserId
mc, Maybe (Set Access)
a, Maybe AccessRoleLegacy
r, Maybe (Set AccessRole)
r', Maybe Text
n, Maybe TeamId
i, h
_, Maybe Milliseconds
mt, Maybe ReceiptMode
rm, k
_, l
_, m
_, n
_, o
_) = do
      let mbAccessRolesV2 :: Maybe (Set AccessRole)
mbAccessRolesV2 = [AccessRole] -> Set AccessRole
forall a. Ord a => [a] -> Set a
Set.fromList ([AccessRole] -> Set AccessRole)
-> (Set AccessRole -> [AccessRole])
-> Set AccessRole
-> Set AccessRole
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Set AccessRole -> [AccessRole]
forall a. Set a -> [a]
Cql.fromSet (Set AccessRole -> Set AccessRole)
-> Maybe (Set AccessRole) -> Maybe (Set AccessRole)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (Set AccessRole)
r'
          accessRoles :: Set AccessRole
accessRoles = ConvType -> Maybe (Set AccessRole) -> Set AccessRole
maybeRole ConvType
t (Maybe (Set AccessRole) -> Set AccessRole)
-> Maybe (Set AccessRole) -> Set AccessRole
forall a b. (a -> b) -> a -> b
$ Maybe AccessRoleLegacy
-> Maybe (Set AccessRole) -> Maybe (Set AccessRole)
parseAccessRoles Maybe AccessRoleLegacy
r Maybe (Set AccessRole)
mbAccessRolesV2
      ConversationMetadata -> f ConversationMetadata
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ConversationMetadata -> f ConversationMetadata)
-> ConversationMetadata -> f ConversationMetadata
forall a b. (a -> b) -> a -> b
$ ConvType
-> Maybe UserId
-> [Access]
-> Set AccessRole
-> Maybe Text
-> Maybe TeamId
-> Maybe Milliseconds
-> Maybe ReceiptMode
-> ConversationMetadata
ConversationMetadata ConvType
t Maybe UserId
mc (ConvType -> Maybe (Set Access) -> [Access]
defAccess ConvType
t Maybe (Set Access)
a) Set AccessRole
accessRoles Maybe Text
n Maybe TeamId
i Maybe Milliseconds
mt Maybe ReceiptMode
rm

getGroupInfo :: ConvId -> Client (Maybe GroupInfoData)
getGroupInfo :: ConvId -> Client (Maybe GroupInfoData)
getGroupInfo ConvId
cid = do
  (Identity (Maybe GroupInfoData) -> Maybe GroupInfoData
forall a. Identity a -> a
runIdentity =<<)
    (Maybe (Identity (Maybe GroupInfoData)) -> Maybe GroupInfoData)
-> Client (Maybe (Identity (Maybe GroupInfoData)))
-> Client (Maybe GroupInfoData)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> RetrySettings
-> Client (Maybe (Identity (Maybe GroupInfoData)))
-> Client (Maybe (Identity (Maybe GroupInfoData)))
forall (m :: * -> *) a.
MonadClient m =>
RetrySettings -> m a -> m a
retry
      RetrySettings
x1
      ( PrepQuery R (Identity ConvId) (Identity (Maybe GroupInfoData))
-> QueryParams (Identity ConvId)
-> Client (Maybe (Identity (Maybe GroupInfoData)))
forall (m :: * -> *) a b (q :: * -> * -> * -> *).
(MonadClient m, Tuple a, Tuple b, RunQ q) =>
q R a b -> QueryParams a -> m (Maybe b)
query1
          PrepQuery R (Identity ConvId) (Identity (Maybe GroupInfoData))
Cql.selectGroupInfo
          (Consistency -> Identity ConvId -> QueryParams (Identity ConvId)
forall a. Consistency -> a -> QueryParams a
params Consistency
LocalQuorum (ConvId -> Identity ConvId
forall a. a -> Identity a
Identity ConvId
cid))
      )

isConvAlive :: ConvId -> Client Bool
isConvAlive :: ConvId -> Client Bool
isConvAlive ConvId
cid = do
  Maybe (Identity (Maybe Bool))
result <- RetrySettings
-> Client (Maybe (Identity (Maybe Bool)))
-> Client (Maybe (Identity (Maybe Bool)))
forall (m :: * -> *) a.
MonadClient m =>
RetrySettings -> m a -> m a
retry RetrySettings
x1 (PrepQuery R (Identity ConvId) (Identity (Maybe Bool))
-> QueryParams (Identity ConvId)
-> Client (Maybe (Identity (Maybe Bool)))
forall (m :: * -> *) a b (q :: * -> * -> * -> *).
(MonadClient m, Tuple a, Tuple b, RunQ q) =>
q R a b -> QueryParams a -> m (Maybe b)
query1 PrepQuery R (Identity ConvId) (Identity (Maybe Bool))
Cql.isConvDeleted (Consistency -> Identity ConvId -> QueryParams (Identity ConvId)
forall a. Consistency -> a -> QueryParams a
params Consistency
LocalQuorum (ConvId -> Identity ConvId
forall a. a -> Identity a
Identity ConvId
cid)))
  case Identity (Maybe Bool) -> Maybe Bool
forall a. Identity a -> a
runIdentity (Identity (Maybe Bool) -> Maybe Bool)
-> Maybe (Identity (Maybe Bool)) -> Maybe (Maybe Bool)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (Identity (Maybe Bool))
result of
    Maybe (Maybe Bool)
Nothing -> Bool -> Client Bool
forall a. a -> Client a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
False
    Just Maybe Bool
Nothing -> Bool -> Client Bool
forall a. a -> Client a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
True
    Just (Just Bool
True) -> Bool -> Client Bool
forall a. a -> Client a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
False
    Just (Just Bool
False) -> Bool -> Client Bool
forall a. a -> Client a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
True

updateConvType :: ConvId -> ConvType -> Client ()
updateConvType :: ConvId -> ConvType -> Client ()
updateConvType ConvId
cid ConvType
ty =
  RetrySettings -> Client () -> Client ()
forall (m :: * -> *) a.
MonadClient m =>
RetrySettings -> m a -> m a
retry RetrySettings
x5 (Client () -> Client ()) -> Client () -> Client ()
forall a b. (a -> b) -> a -> b
$
    PrepQuery W (ConvType, ConvId) ()
-> QueryParams (ConvType, ConvId) -> Client ()
forall (m :: * -> *) a (q :: * -> * -> * -> *).
(MonadClient m, Tuple a, RunQ q) =>
q W a () -> QueryParams a -> m ()
write PrepQuery W (ConvType, ConvId) ()
Cql.updateConvType (Consistency -> (ConvType, ConvId) -> QueryParams (ConvType, ConvId)
forall a. Consistency -> a -> QueryParams a
params Consistency
LocalQuorum (ConvType
ty, ConvId
cid))

updateConvName :: ConvId -> Range 1 256 Text -> Client ()
updateConvName :: ConvId -> Range 1 256 Text -> Client ()
updateConvName ConvId
cid Range 1 256 Text
name = RetrySettings -> Client () -> Client ()
forall (m :: * -> *) a.
MonadClient m =>
RetrySettings -> m a -> m a
retry RetrySettings
x5 (Client () -> Client ()) -> Client () -> Client ()
forall a b. (a -> b) -> a -> b
$ PrepQuery W (Text, ConvId) ()
-> QueryParams (Text, ConvId) -> Client ()
forall (m :: * -> *) a (q :: * -> * -> * -> *).
(MonadClient m, Tuple a, RunQ q) =>
q W a () -> QueryParams a -> m ()
write PrepQuery W (Text, ConvId) ()
Cql.updateConvName (Consistency -> (Text, ConvId) -> QueryParams (Text, ConvId)
forall a. Consistency -> a -> QueryParams a
params Consistency
LocalQuorum (Range 1 256 Text -> Text
forall (n :: Nat) (m :: Nat) a. Range n m a -> a
fromRange Range 1 256 Text
name, ConvId
cid))

updateConvAccess :: ConvId -> ConversationAccessData -> Client ()
updateConvAccess :: ConvId -> ConversationAccessData -> Client ()
updateConvAccess ConvId
cid (ConversationAccessData Set Access
acc Set AccessRole
role) =
  RetrySettings -> Client () -> Client ()
forall (m :: * -> *) a.
MonadClient m =>
RetrySettings -> m a -> m a
retry RetrySettings
x5 (Client () -> Client ()) -> Client () -> Client ()
forall a b. (a -> b) -> a -> b
$
    PrepQuery W (Set Access, Set AccessRole, ConvId) ()
-> QueryParams (Set Access, Set AccessRole, ConvId) -> Client ()
forall (m :: * -> *) a (q :: * -> * -> * -> *).
(MonadClient m, Tuple a, RunQ q) =>
q W a () -> QueryParams a -> m ()
write PrepQuery W (Set Access, Set AccessRole, ConvId) ()
Cql.updateConvAccess (Consistency
-> (Set Access, Set AccessRole, ConvId)
-> QueryParams (Set Access, Set AccessRole, ConvId)
forall a. Consistency -> a -> QueryParams a
params Consistency
LocalQuorum ([Access] -> Set Access
forall a. [a] -> Set a
Cql.Set (Set Access -> [Access]
forall a. Set a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList Set Access
acc), [AccessRole] -> Set AccessRole
forall a. [a] -> Set a
Cql.Set (Set AccessRole -> [AccessRole]
forall a. Set a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList Set AccessRole
role), ConvId
cid))

updateConvReceiptMode :: ConvId -> ReceiptMode -> Client ()
updateConvReceiptMode :: ConvId -> ReceiptMode -> Client ()
updateConvReceiptMode ConvId
cid ReceiptMode
receiptMode = RetrySettings -> Client () -> Client ()
forall (m :: * -> *) a.
MonadClient m =>
RetrySettings -> m a -> m a
retry RetrySettings
x5 (Client () -> Client ()) -> Client () -> Client ()
forall a b. (a -> b) -> a -> b
$ PrepQuery W (ReceiptMode, ConvId) ()
-> QueryParams (ReceiptMode, ConvId) -> Client ()
forall (m :: * -> *) a (q :: * -> * -> * -> *).
(MonadClient m, Tuple a, RunQ q) =>
q W a () -> QueryParams a -> m ()
write PrepQuery W (ReceiptMode, ConvId) ()
Cql.updateConvReceiptMode (Consistency
-> (ReceiptMode, ConvId) -> QueryParams (ReceiptMode, ConvId)
forall a. Consistency -> a -> QueryParams a
params Consistency
LocalQuorum (ReceiptMode
receiptMode, ConvId
cid))

updateConvMessageTimer :: ConvId -> Maybe Milliseconds -> Client ()
updateConvMessageTimer :: ConvId -> Maybe Milliseconds -> Client ()
updateConvMessageTimer ConvId
cid Maybe Milliseconds
mtimer = RetrySettings -> Client () -> Client ()
forall (m :: * -> *) a.
MonadClient m =>
RetrySettings -> m a -> m a
retry RetrySettings
x5 (Client () -> Client ()) -> Client () -> Client ()
forall a b. (a -> b) -> a -> b
$ PrepQuery W (Maybe Milliseconds, ConvId) ()
-> QueryParams (Maybe Milliseconds, ConvId) -> Client ()
forall (m :: * -> *) a (q :: * -> * -> * -> *).
(MonadClient m, Tuple a, RunQ q) =>
q W a () -> QueryParams a -> m ()
write PrepQuery W (Maybe Milliseconds, ConvId) ()
Cql.updateConvMessageTimer (Consistency
-> (Maybe Milliseconds, ConvId)
-> QueryParams (Maybe Milliseconds, ConvId)
forall a. Consistency -> a -> QueryParams a
params Consistency
LocalQuorum (Maybe Milliseconds
mtimer, ConvId
cid))

getConvEpoch :: ConvId -> Client (Maybe Epoch)
getConvEpoch :: ConvId -> Client (Maybe Epoch)
getConvEpoch ConvId
cid =
  (Identity (Maybe Epoch) -> Maybe Epoch
forall a. Identity a -> a
runIdentity =<<)
    (Maybe (Identity (Maybe Epoch)) -> Maybe Epoch)
-> Client (Maybe (Identity (Maybe Epoch))) -> Client (Maybe Epoch)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> RetrySettings
-> Client (Maybe (Identity (Maybe Epoch)))
-> Client (Maybe (Identity (Maybe Epoch)))
forall (m :: * -> *) a.
MonadClient m =>
RetrySettings -> m a -> m a
retry
      RetrySettings
x1
      (PrepQuery R (Identity ConvId) (Identity (Maybe Epoch))
-> QueryParams (Identity ConvId)
-> Client (Maybe (Identity (Maybe Epoch)))
forall (m :: * -> *) a b (q :: * -> * -> * -> *).
(MonadClient m, Tuple a, Tuple b, RunQ q) =>
q R a b -> QueryParams a -> m (Maybe b)
query1 PrepQuery R (Identity ConvId) (Identity (Maybe Epoch))
Cql.getConvEpoch (Consistency -> Identity ConvId -> QueryParams (Identity ConvId)
forall a. Consistency -> a -> QueryParams a
params Consistency
LocalQuorum (ConvId -> Identity ConvId
forall a. a -> Identity a
Identity ConvId
cid)))

updateConvEpoch :: ConvId -> Epoch -> Client ()
updateConvEpoch :: ConvId -> Epoch -> Client ()
updateConvEpoch ConvId
cid Epoch
epoch = RetrySettings -> Client () -> Client ()
forall (m :: * -> *) a.
MonadClient m =>
RetrySettings -> m a -> m a
retry RetrySettings
x5 (Client () -> Client ()) -> Client () -> Client ()
forall a b. (a -> b) -> a -> b
$ PrepQuery W (Epoch, ConvId) ()
-> QueryParams (Epoch, ConvId) -> Client ()
forall (m :: * -> *) a (q :: * -> * -> * -> *).
(MonadClient m, Tuple a, RunQ q) =>
q W a () -> QueryParams a -> m ()
write PrepQuery W (Epoch, ConvId) ()
Cql.updateConvEpoch (Consistency -> (Epoch, ConvId) -> QueryParams (Epoch, ConvId)
forall a. Consistency -> a -> QueryParams a
params Consistency
LocalQuorum (Epoch
epoch, ConvId
cid))

updateConvCipherSuite :: ConvId -> CipherSuiteTag -> Client ()
updateConvCipherSuite :: ConvId -> CipherSuiteTag -> Client ()
updateConvCipherSuite ConvId
cid CipherSuiteTag
cs =
  RetrySettings -> Client () -> Client ()
forall (m :: * -> *) a.
MonadClient m =>
RetrySettings -> m a -> m a
retry RetrySettings
x5 (Client () -> Client ()) -> Client () -> Client ()
forall a b. (a -> b) -> a -> b
$
    PrepQuery W (CipherSuiteTag, ConvId) ()
-> QueryParams (CipherSuiteTag, ConvId) -> Client ()
forall (m :: * -> *) a (q :: * -> * -> * -> *).
(MonadClient m, Tuple a, RunQ q) =>
q W a () -> QueryParams a -> m ()
write
      PrepQuery W (CipherSuiteTag, ConvId) ()
Cql.updateConvCipherSuite
      (Consistency
-> (CipherSuiteTag, ConvId) -> QueryParams (CipherSuiteTag, ConvId)
forall a. Consistency -> a -> QueryParams a
params Consistency
LocalQuorum (CipherSuiteTag
cs, ConvId
cid))

setGroupInfo :: ConvId -> GroupInfoData -> Client ()
setGroupInfo :: ConvId -> GroupInfoData -> Client ()
setGroupInfo ConvId
conv GroupInfoData
gid =
  PrepQuery W (GroupInfoData, ConvId) ()
-> QueryParams (GroupInfoData, ConvId) -> Client ()
forall (m :: * -> *) a (q :: * -> * -> * -> *).
(MonadClient m, Tuple a, RunQ q) =>
q W a () -> QueryParams a -> m ()
write PrepQuery W (GroupInfoData, ConvId) ()
Cql.updateGroupInfo (Consistency
-> (GroupInfoData, ConvId) -> QueryParams (GroupInfoData, ConvId)
forall a. Consistency -> a -> QueryParams a
params Consistency
LocalQuorum (GroupInfoData
gid, ConvId
conv))

getConversation :: ConvId -> Client (Maybe Conversation)
getConversation :: ConvId -> Client (Maybe Conversation)
getConversation ConvId
conv = do
  Async
  (Maybe
     (ConvType, Maybe UserId, Maybe (Set Access),
      Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
      Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
      Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
      Maybe (Writetime Epoch), Maybe CipherSuiteTag))
cdata <- Client
  (Maybe
     (ConvType, Maybe UserId, Maybe (Set Access),
      Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
      Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
      Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
      Maybe (Writetime Epoch), Maybe CipherSuiteTag))
-> Client
     (Async
        (Maybe
           (ConvType, Maybe UserId, Maybe (Set Access),
            Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
            Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
            Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
            Maybe (Writetime Epoch), Maybe CipherSuiteTag)))
forall (m :: * -> *) a. MonadUnliftIO m => m a -> m (Async a)
UnliftIO.async (Client
   (Maybe
      (ConvType, Maybe UserId, Maybe (Set Access),
       Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
       Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
       Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
       Maybe (Writetime Epoch), Maybe CipherSuiteTag))
 -> Client
      (Async
         (Maybe
            (ConvType, Maybe UserId, Maybe (Set Access),
             Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
             Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
             Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
             Maybe (Writetime Epoch), Maybe CipherSuiteTag))))
-> Client
     (Maybe
        (ConvType, Maybe UserId, Maybe (Set Access),
         Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
         Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
         Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
         Maybe (Writetime Epoch), Maybe CipherSuiteTag))
-> Client
     (Async
        (Maybe
           (ConvType, Maybe UserId, Maybe (Set Access),
            Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
            Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
            Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
            Maybe (Writetime Epoch), Maybe CipherSuiteTag)))
forall a b. (a -> b) -> a -> b
$ RetrySettings
-> Client
     (Maybe
        (ConvType, Maybe UserId, Maybe (Set Access),
         Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
         Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
         Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
         Maybe (Writetime Epoch), Maybe CipherSuiteTag))
-> Client
     (Maybe
        (ConvType, Maybe UserId, Maybe (Set Access),
         Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
         Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
         Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
         Maybe (Writetime Epoch), Maybe CipherSuiteTag))
forall (m :: * -> *) a.
MonadClient m =>
RetrySettings -> m a -> m a
retry RetrySettings
x1 (PrepQuery
  R
  (Identity ConvId)
  (ConvType, Maybe UserId, Maybe (Set Access),
   Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
   Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
   Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
   Maybe (Writetime Epoch), Maybe CipherSuiteTag)
-> QueryParams (Identity ConvId)
-> Client
     (Maybe
        (ConvType, Maybe UserId, Maybe (Set Access),
         Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
         Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
         Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
         Maybe (Writetime Epoch), Maybe CipherSuiteTag))
forall (m :: * -> *) a b (q :: * -> * -> * -> *).
(MonadClient m, Tuple a, Tuple b, RunQ q) =>
q R a b -> QueryParams a -> m (Maybe b)
query1 PrepQuery
  R
  (Identity ConvId)
  (ConvType, Maybe UserId, Maybe (Set Access),
   Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
   Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
   Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
   Maybe (Writetime Epoch), Maybe CipherSuiteTag)
Cql.selectConv (Consistency -> Identity ConvId -> QueryParams (Identity ConvId)
forall a. Consistency -> a -> QueryParams a
params Consistency
LocalQuorum (ConvId -> Identity ConvId
forall a. a -> Identity a
Identity ConvId
conv)))
  Async [RemoteMember]
remoteMems <- Client [RemoteMember] -> Client (Async [RemoteMember])
forall (m :: * -> *) a. MonadUnliftIO m => m a -> m (Async a)
UnliftIO.async (Client [RemoteMember] -> Client (Async [RemoteMember]))
-> Client [RemoteMember] -> Client (Async [RemoteMember])
forall a b. (a -> b) -> a -> b
$ ConvId -> Client [RemoteMember]
lookupRemoteMembers ConvId
conv
  Maybe Conversation
mbConv <-
    ConvId
-> [LocalMember]
-> [RemoteMember]
-> Maybe
     (ConvType, Maybe UserId, Maybe (Set Access),
      Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
      Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
      Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
      Maybe (Writetime Epoch), Maybe CipherSuiteTag)
-> Maybe Conversation
toConv ConvId
conv
      ([LocalMember]
 -> [RemoteMember]
 -> Maybe
      (ConvType, Maybe UserId, Maybe (Set Access),
       Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
       Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
       Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
       Maybe (Writetime Epoch), Maybe CipherSuiteTag)
 -> Maybe Conversation)
-> Client [LocalMember]
-> Client
     ([RemoteMember]
      -> Maybe
           (ConvType, Maybe UserId, Maybe (Set Access),
            Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
            Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
            Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
            Maybe (Writetime Epoch), Maybe CipherSuiteTag)
      -> Maybe Conversation)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ConvId -> Client [LocalMember]
members ConvId
conv
      Client
  ([RemoteMember]
   -> Maybe
        (ConvType, Maybe UserId, Maybe (Set Access),
         Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
         Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
         Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
         Maybe (Writetime Epoch), Maybe CipherSuiteTag)
   -> Maybe Conversation)
-> Client [RemoteMember]
-> Client
     (Maybe
        (ConvType, Maybe UserId, Maybe (Set Access),
         Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
         Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
         Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
         Maybe (Writetime Epoch), Maybe CipherSuiteTag)
      -> Maybe Conversation)
forall a b. Client (a -> b) -> Client a -> Client b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Async [RemoteMember] -> Client [RemoteMember]
forall (m :: * -> *) a. MonadIO m => Async a -> m a
UnliftIO.wait Async [RemoteMember]
remoteMems
      Client
  (Maybe
     (ConvType, Maybe UserId, Maybe (Set Access),
      Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
      Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
      Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
      Maybe (Writetime Epoch), Maybe CipherSuiteTag)
   -> Maybe Conversation)
-> Client
     (Maybe
        (ConvType, Maybe UserId, Maybe (Set Access),
         Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
         Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
         Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
         Maybe (Writetime Epoch), Maybe CipherSuiteTag))
-> Client (Maybe Conversation)
forall a b. Client (a -> b) -> Client a -> Client b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Async
  (Maybe
     (ConvType, Maybe UserId, Maybe (Set Access),
      Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
      Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
      Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
      Maybe (Writetime Epoch), Maybe CipherSuiteTag))
-> Client
     (Maybe
        (ConvType, Maybe UserId, Maybe (Set Access),
         Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
         Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
         Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
         Maybe (Writetime Epoch), Maybe CipherSuiteTag))
forall (m :: * -> *) a. MonadIO m => Async a -> m a
UnliftIO.wait Async
  (Maybe
     (ConvType, Maybe UserId, Maybe (Set Access),
      Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
      Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
      Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
      Maybe (Writetime Epoch), Maybe CipherSuiteTag))
cdata
  MaybeT Client Conversation -> Client (Maybe Conversation)
forall (m :: * -> *) a. MaybeT m a -> m (Maybe a)
runMaybeT (MaybeT Client Conversation -> Client (Maybe Conversation))
-> MaybeT Client Conversation -> Client (Maybe Conversation)
forall a b. (a -> b) -> a -> b
$ Conversation -> MaybeT Client Conversation
conversationGC (Conversation -> MaybeT Client Conversation)
-> MaybeT Client Conversation -> MaybeT Client Conversation
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< MaybeT Client Conversation
-> (Conversation -> MaybeT Client Conversation)
-> Maybe Conversation
-> MaybeT Client Conversation
forall b a. b -> (a -> b) -> Maybe a -> b
maybe MaybeT Client Conversation
forall a. MaybeT Client a
forall (m :: * -> *) a. MonadPlus m => m a
mzero Conversation -> MaybeT Client Conversation
forall a. a -> MaybeT Client a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe Conversation
mbConv

-- | "Garbage collect" a 'Conversation', i.e. if the conversation is
-- marked as deleted, actually remove it from the database and return
-- 'Nothing'.
conversationGC ::
  Conversation ->
  MaybeT Client Conversation
conversationGC :: Conversation -> MaybeT Client Conversation
conversationGC Conversation
conv =
  [MaybeT Client Conversation] -> MaybeT Client Conversation
forall (t :: * -> *) (f :: * -> *) a.
(Foldable t, Alternative f) =>
t (f a) -> f a
asum
    [ -- return conversation if not deleted
      Bool -> MaybeT Client ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> Bool
not (Conversation -> Bool
convDeleted Conversation
conv)) MaybeT Client () -> Conversation -> MaybeT Client Conversation
forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> Conversation
conv,
      -- actually delete it and fail
      Client () -> MaybeT Client ()
forall (m :: * -> *) a. Monad m => m a -> MaybeT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ConvId -> Client ()
deleteConversation (Conversation -> ConvId
convId Conversation
conv)) MaybeT Client ()
-> MaybeT Client Conversation -> MaybeT Client Conversation
forall a b. MaybeT Client a -> MaybeT Client b -> MaybeT Client b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> MaybeT Client Conversation
forall a. MaybeT Client a
forall (m :: * -> *) a. MonadPlus m => m a
mzero
    ]

localConversation :: ConvId -> Client (Maybe Conversation)
localConversation :: ConvId -> Client (Maybe Conversation)
localConversation ConvId
cid =
  Concurrently Client (Maybe Conversation)
-> Client (Maybe Conversation)
forall (m :: * -> *) a. Concurrently m a -> m a
UnliftIO.runConcurrently (Concurrently Client (Maybe Conversation)
 -> Client (Maybe Conversation))
-> Concurrently Client (Maybe Conversation)
-> Client (Maybe Conversation)
forall a b. (a -> b) -> a -> b
$
    ConvId
-> [LocalMember]
-> [RemoteMember]
-> Maybe
     (ConvType, Maybe UserId, Maybe (Set Access),
      Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
      Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
      Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
      Maybe (Writetime Epoch), Maybe CipherSuiteTag)
-> Maybe Conversation
toConv ConvId
cid
      ([LocalMember]
 -> [RemoteMember]
 -> Maybe
      (ConvType, Maybe UserId, Maybe (Set Access),
       Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
       Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
       Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
       Maybe (Writetime Epoch), Maybe CipherSuiteTag)
 -> Maybe Conversation)
-> Concurrently Client [LocalMember]
-> Concurrently
     Client
     ([RemoteMember]
      -> Maybe
           (ConvType, Maybe UserId, Maybe (Set Access),
            Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
            Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
            Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
            Maybe (Writetime Epoch), Maybe CipherSuiteTag)
      -> Maybe Conversation)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Client [LocalMember] -> Concurrently Client [LocalMember]
forall (m :: * -> *) a. m a -> Concurrently m a
UnliftIO.Concurrently (ConvId -> Client [LocalMember]
members ConvId
cid)
      Concurrently
  Client
  ([RemoteMember]
   -> Maybe
        (ConvType, Maybe UserId, Maybe (Set Access),
         Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
         Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
         Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
         Maybe (Writetime Epoch), Maybe CipherSuiteTag)
   -> Maybe Conversation)
-> Concurrently Client [RemoteMember]
-> Concurrently
     Client
     (Maybe
        (ConvType, Maybe UserId, Maybe (Set Access),
         Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
         Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
         Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
         Maybe (Writetime Epoch), Maybe CipherSuiteTag)
      -> Maybe Conversation)
forall a b.
Concurrently Client (a -> b)
-> Concurrently Client a -> Concurrently Client b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Client [RemoteMember] -> Concurrently Client [RemoteMember]
forall (m :: * -> *) a. m a -> Concurrently m a
UnliftIO.Concurrently (ConvId -> Client [RemoteMember]
lookupRemoteMembers ConvId
cid)
      Concurrently
  Client
  (Maybe
     (ConvType, Maybe UserId, Maybe (Set Access),
      Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
      Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
      Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
      Maybe (Writetime Epoch), Maybe CipherSuiteTag)
   -> Maybe Conversation)
-> Concurrently
     Client
     (Maybe
        (ConvType, Maybe UserId, Maybe (Set Access),
         Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
         Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
         Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
         Maybe (Writetime Epoch), Maybe CipherSuiteTag))
-> Concurrently Client (Maybe Conversation)
forall a b.
Concurrently Client (a -> b)
-> Concurrently Client a -> Concurrently Client b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Client
  (Maybe
     (ConvType, Maybe UserId, Maybe (Set Access),
      Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
      Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
      Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
      Maybe (Writetime Epoch), Maybe CipherSuiteTag))
-> Concurrently
     Client
     (Maybe
        (ConvType, Maybe UserId, Maybe (Set Access),
         Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
         Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
         Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
         Maybe (Writetime Epoch), Maybe CipherSuiteTag))
forall (m :: * -> *) a. m a -> Concurrently m a
UnliftIO.Concurrently
        ( RetrySettings
-> Client
     (Maybe
        (ConvType, Maybe UserId, Maybe (Set Access),
         Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
         Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
         Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
         Maybe (Writetime Epoch), Maybe CipherSuiteTag))
-> Client
     (Maybe
        (ConvType, Maybe UserId, Maybe (Set Access),
         Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
         Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
         Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
         Maybe (Writetime Epoch), Maybe CipherSuiteTag))
forall (m :: * -> *) a.
MonadClient m =>
RetrySettings -> m a -> m a
retry RetrySettings
x1 (Client
   (Maybe
      (ConvType, Maybe UserId, Maybe (Set Access),
       Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
       Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
       Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
       Maybe (Writetime Epoch), Maybe CipherSuiteTag))
 -> Client
      (Maybe
         (ConvType, Maybe UserId, Maybe (Set Access),
          Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
          Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
          Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
          Maybe (Writetime Epoch), Maybe CipherSuiteTag)))
-> Client
     (Maybe
        (ConvType, Maybe UserId, Maybe (Set Access),
         Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
         Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
         Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
         Maybe (Writetime Epoch), Maybe CipherSuiteTag))
-> Client
     (Maybe
        (ConvType, Maybe UserId, Maybe (Set Access),
         Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
         Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
         Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
         Maybe (Writetime Epoch), Maybe CipherSuiteTag))
forall a b. (a -> b) -> a -> b
$
            PrepQuery
  R
  (Identity ConvId)
  (ConvType, Maybe UserId, Maybe (Set Access),
   Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
   Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
   Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
   Maybe (Writetime Epoch), Maybe CipherSuiteTag)
-> QueryParams (Identity ConvId)
-> Client
     (Maybe
        (ConvType, Maybe UserId, Maybe (Set Access),
         Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
         Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
         Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
         Maybe (Writetime Epoch), Maybe CipherSuiteTag))
forall (m :: * -> *) a b (q :: * -> * -> * -> *).
(MonadClient m, Tuple a, Tuple b, RunQ q) =>
q R a b -> QueryParams a -> m (Maybe b)
query1 PrepQuery
  R
  (Identity ConvId)
  (ConvType, Maybe UserId, Maybe (Set Access),
   Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
   Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
   Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
   Maybe (Writetime Epoch), Maybe CipherSuiteTag)
Cql.selectConv (Consistency -> Identity ConvId -> QueryParams (Identity ConvId)
forall a. Consistency -> a -> QueryParams a
params Consistency
LocalQuorum (ConvId -> Identity ConvId
forall a. a -> Identity a
Identity ConvId
cid))
        )

localConversations ::
  ( Member (Embed IO) r,
    Member (Input ClientState) r,
    Member TinyLog r
  ) =>
  [ConvId] ->
  Sem r [Conversation]
localConversations :: forall (r :: EffectRow).
(Member (Embed IO) r, Member (Input ClientState) r,
 Member TinyLog r) =>
[ConvId] -> Sem r [Conversation]
localConversations =
  [Either ByteString Conversation] -> Sem r [Conversation]
forall {r :: EffectRow} {a} {b}.
(Member TinyLog r, ToBytes a) =>
[Either a b] -> Sem r [b]
collectAndLog
    ([Either ByteString Conversation] -> Sem r [Conversation])
-> ([ConvId] -> Sem r [Either ByteString Conversation])
-> [ConvId]
-> Sem r [Conversation]
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< ( Client [Either ByteString Conversation]
-> Sem r [Either ByteString Conversation]
forall (r :: EffectRow) a.
(Member (Embed IO) r, Member (Input ClientState) r) =>
Client a -> Sem r a
embedClient
            (Client [Either ByteString Conversation]
 -> Sem r [Either ByteString Conversation])
-> ([ConvId] -> Client [Either ByteString Conversation])
-> [ConvId]
-> Sem r [Either ByteString Conversation]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int
-> (ConvId -> Client (Either ByteString Conversation))
-> [ConvId]
-> Client [Either ByteString Conversation]
forall (m :: * -> *) (t :: * -> *) a b.
(MonadUnliftIO m, Traversable t) =>
Int -> (a -> m b) -> t a -> m (t b)
UnliftIO.pooledMapConcurrentlyN Int
8 ConvId -> Client (Either ByteString Conversation)
localConversation'
        )
  where
    collectAndLog :: [Either a b] -> Sem r [b]
collectAndLog [Either a b]
cs = case [Either a b] -> ([a], [b])
forall a b. [Either a b] -> ([a], [b])
partitionEithers [Either a b]
cs of
      ([a]
errs, [b]
convs) -> (a -> Sem r ()) -> [a] -> Sem r ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
(a -> f b) -> t a -> f ()
traverse_ ((Msg -> Msg) -> Sem r ()
forall msg (r :: EffectRow).
Member (Logger msg) r =>
msg -> Sem r ()
warn ((Msg -> Msg) -> Sem r ()) -> (a -> Msg -> Msg) -> a -> Sem r ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Msg -> Msg
forall a. ToBytes a => a -> Msg -> Msg
Log.msg) [a]
errs Sem r () -> [b] -> Sem r [b]
forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> [b]
convs

    localConversation' :: ConvId -> Client (Either ByteString Conversation)
    localConversation' :: ConvId -> Client (Either ByteString Conversation)
localConversation' ConvId
cid =
      ByteString -> Maybe Conversation -> Either ByteString Conversation
forall a b. a -> Maybe b -> Either a b
note (ByteString
"No conversation for: " ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ConvId -> ByteString
forall a. ToByteString a => a -> ByteString
toByteString' ConvId
cid) (Maybe Conversation -> Either ByteString Conversation)
-> Client (Maybe Conversation)
-> Client (Either ByteString Conversation)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ConvId -> Client (Maybe Conversation)
localConversation ConvId
cid

-- | Takes a list of conversation ids and returns those found for the given
-- user.
localConversationIdsOf :: UserId -> [ConvId] -> Client [ConvId]
localConversationIdsOf :: UserId -> [ConvId] -> Client [ConvId]
localConversationIdsOf UserId
usr [ConvId]
cids = do
  Identity ConvId -> ConvId
forall a. Identity a -> a
runIdentity (Identity ConvId -> ConvId)
-> Client [Identity ConvId] -> Client [ConvId]
forall (f :: * -> *) (g :: * -> *) a b.
(Functor f, Functor g) =>
(a -> b) -> f (g a) -> f (g b)
<$$> RetrySettings
-> Client [Identity ConvId] -> Client [Identity ConvId]
forall (m :: * -> *) a.
MonadClient m =>
RetrySettings -> m a -> m a
retry RetrySettings
x1 (PrepQuery R (UserId, [ConvId]) (Identity ConvId)
-> QueryParams (UserId, [ConvId]) -> Client [Identity ConvId]
forall (m :: * -> *) a b (q :: * -> * -> * -> *).
(MonadClient m, Tuple a, Tuple b, RunQ q) =>
q R a b -> QueryParams a -> m [b]
query PrepQuery R (UserId, [ConvId]) (Identity ConvId)
Cql.selectUserConvsIn (Consistency -> (UserId, [ConvId]) -> QueryParams (UserId, [ConvId])
forall a. Consistency -> a -> QueryParams a
params Consistency
LocalQuorum (UserId
usr, [ConvId]
cids)))

-- | Takes a list of remote conversation ids and fetches member status flags
-- for the given user
remoteConversationStatus ::
  UserId ->
  [Remote ConvId] ->
  Client (Map (Remote ConvId) MemberStatus)
remoteConversationStatus :: UserId
-> [Remote ConvId] -> Client (Map (Remote ConvId) MemberStatus)
remoteConversationStatus UserId
uid =
  ([Map (Remote ConvId) MemberStatus]
 -> Map (Remote ConvId) MemberStatus)
-> Client [Map (Remote ConvId) MemberStatus]
-> Client (Map (Remote ConvId) MemberStatus)
forall a b. (a -> b) -> Client a -> Client b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Map (Remote ConvId) MemberStatus]
-> Map (Remote ConvId) MemberStatus
forall a. Monoid a => [a] -> a
mconcat
    (Client [Map (Remote ConvId) MemberStatus]
 -> Client (Map (Remote ConvId) MemberStatus))
-> ([Remote ConvId] -> Client [Map (Remote ConvId) MemberStatus])
-> [Remote ConvId]
-> Client (Map (Remote ConvId) MemberStatus)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int
-> (Remote [ConvId] -> Client (Map (Remote ConvId) MemberStatus))
-> [Remote [ConvId]]
-> Client [Map (Remote ConvId) MemberStatus]
forall (m :: * -> *) (t :: * -> *) a b.
(MonadUnliftIO m, Traversable t) =>
Int -> (a -> m b) -> t a -> m (t b)
UnliftIO.pooledMapConcurrentlyN Int
8 (UserId
-> Remote [ConvId] -> Client (Map (Remote ConvId) MemberStatus)
remoteConversationStatusOnDomain UserId
uid)
    ([Remote [ConvId]] -> Client [Map (Remote ConvId) MemberStatus])
-> ([Remote ConvId] -> [Remote [ConvId]])
-> [Remote ConvId]
-> Client [Map (Remote ConvId) MemberStatus]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Remote ConvId] -> [Remote [ConvId]]
forall (f :: * -> *) a.
(Functor f, Foldable f) =>
f (Remote a) -> [Remote [a]]
bucketRemote

remoteConversationStatusOnDomain :: UserId -> Remote [ConvId] -> Client (Map (Remote ConvId) MemberStatus)
remoteConversationStatusOnDomain :: UserId
-> Remote [ConvId] -> Client (Map (Remote ConvId) MemberStatus)
remoteConversationStatusOnDomain UserId
uid Remote [ConvId]
rconvs =
  [(Remote ConvId, MemberStatus)] -> Map (Remote ConvId) MemberStatus
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList ([(Remote ConvId, MemberStatus)]
 -> Map (Remote ConvId) MemberStatus)
-> ([(ConvId, Maybe MutedStatus, Maybe Text, Maybe Bool,
      Maybe Text, Maybe Bool, Maybe Text)]
    -> [(Remote ConvId, MemberStatus)])
-> [(ConvId, Maybe MutedStatus, Maybe Text, Maybe Bool, Maybe Text,
     Maybe Bool, Maybe Text)]
-> Map (Remote ConvId) MemberStatus
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((ConvId, Maybe MutedStatus, Maybe Text, Maybe Bool, Maybe Text,
  Maybe Bool, Maybe Text)
 -> (Remote ConvId, MemberStatus))
-> [(ConvId, Maybe MutedStatus, Maybe Text, Maybe Bool, Maybe Text,
     Maybe Bool, Maybe Text)]
-> [(Remote ConvId, MemberStatus)]
forall a b. (a -> b) -> [a] -> [b]
map (ConvId, Maybe MutedStatus, Maybe Text, Maybe Bool, Maybe Text,
 Maybe Bool, Maybe Text)
-> (Remote ConvId, MemberStatus)
toPair
    ([(ConvId, Maybe MutedStatus, Maybe Text, Maybe Bool, Maybe Text,
   Maybe Bool, Maybe Text)]
 -> Map (Remote ConvId) MemberStatus)
-> Client
     [(ConvId, Maybe MutedStatus, Maybe Text, Maybe Bool, Maybe Text,
       Maybe Bool, Maybe Text)]
-> Client (Map (Remote ConvId) MemberStatus)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> PrepQuery
  R
  (UserId, Domain, [ConvId])
  (ConvId, Maybe MutedStatus, Maybe Text, Maybe Bool, Maybe Text,
   Maybe Bool, Maybe Text)
-> QueryParams (UserId, Domain, [ConvId])
-> Client
     [(ConvId, Maybe MutedStatus, Maybe Text, Maybe Bool, Maybe Text,
       Maybe Bool, Maybe Text)]
forall (m :: * -> *) a b (q :: * -> * -> * -> *).
(MonadClient m, Tuple a, Tuple b, RunQ q) =>
q R a b -> QueryParams a -> m [b]
query PrepQuery
  R
  (UserId, Domain, [ConvId])
  (ConvId, Maybe MutedStatus, Maybe Text, Maybe Bool, Maybe Text,
   Maybe Bool, Maybe Text)
Cql.selectRemoteConvMemberStatuses (Consistency
-> (UserId, Domain, [ConvId])
-> QueryParams (UserId, Domain, [ConvId])
forall a. Consistency -> a -> QueryParams a
params Consistency
LocalQuorum (UserId
uid, Remote [ConvId] -> Domain
forall (t :: QTag) a. QualifiedWithTag t a -> Domain
tDomain Remote [ConvId]
rconvs, Remote [ConvId] -> [ConvId]
forall (t :: QTag) a. QualifiedWithTag t a -> a
tUnqualified Remote [ConvId]
rconvs))
  where
    toPair :: (ConvId, Maybe MutedStatus, Maybe Text, Maybe Bool, Maybe Text,
 Maybe Bool, Maybe Text)
-> (Remote ConvId, MemberStatus)
toPair (ConvId
conv, Maybe MutedStatus
omus, Maybe Text
omur, Maybe Bool
oar, Maybe Text
oarr, Maybe Bool
hid, Maybe Text
hidr) =
      ( Remote [ConvId] -> ConvId -> Remote ConvId
forall (t :: QTag) x a.
QualifiedWithTag t x -> a -> QualifiedWithTag t a
qualifyAs Remote [ConvId]
rconvs ConvId
conv,
        (Maybe MutedStatus, Maybe Text, Maybe Bool, Maybe Text, Maybe Bool,
 Maybe Text)
-> MemberStatus
toMemberStatus (Maybe MutedStatus
omus, Maybe Text
omur, Maybe Bool
oar, Maybe Text
oarr, Maybe Bool
hid, Maybe Text
hidr)
      )

toProtocol ::
  Maybe ProtocolTag ->
  Maybe GroupId ->
  Maybe Epoch ->
  Maybe UTCTime ->
  Maybe CipherSuiteTag ->
  Maybe Protocol
toProtocol :: Maybe ProtocolTag
-> Maybe GroupId
-> Maybe Epoch
-> Maybe UTCTime
-> Maybe CipherSuiteTag
-> Maybe Protocol
toProtocol Maybe ProtocolTag
Nothing Maybe GroupId
_ Maybe Epoch
_ Maybe UTCTime
_ Maybe CipherSuiteTag
_ = Protocol -> Maybe Protocol
forall a. a -> Maybe a
Just Protocol
ProtocolProteus
toProtocol (Just ProtocolTag
ProtocolProteusTag) Maybe GroupId
_ Maybe Epoch
_ Maybe UTCTime
_ Maybe CipherSuiteTag
_ = Protocol -> Maybe Protocol
forall a. a -> Maybe a
Just Protocol
ProtocolProteus
toProtocol (Just ProtocolTag
ProtocolMLSTag) Maybe GroupId
mgid Maybe Epoch
mepoch Maybe UTCTime
mtimestamp Maybe CipherSuiteTag
mcs = ConversationMLSData -> Protocol
ProtocolMLS (ConversationMLSData -> Protocol)
-> Maybe ConversationMLSData -> Maybe Protocol
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe GroupId
-> Maybe Epoch
-> Maybe UTCTime
-> Maybe CipherSuiteTag
-> Maybe ConversationMLSData
toConversationMLSData Maybe GroupId
mgid Maybe Epoch
mepoch Maybe UTCTime
mtimestamp Maybe CipherSuiteTag
mcs
toProtocol (Just ProtocolTag
ProtocolMixedTag) Maybe GroupId
mgid Maybe Epoch
mepoch Maybe UTCTime
mtimestamp Maybe CipherSuiteTag
mcs = ConversationMLSData -> Protocol
ProtocolMixed (ConversationMLSData -> Protocol)
-> Maybe ConversationMLSData -> Maybe Protocol
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe GroupId
-> Maybe Epoch
-> Maybe UTCTime
-> Maybe CipherSuiteTag
-> Maybe ConversationMLSData
toConversationMLSData Maybe GroupId
mgid Maybe Epoch
mepoch Maybe UTCTime
mtimestamp Maybe CipherSuiteTag
mcs

toConversationMLSData :: Maybe GroupId -> Maybe Epoch -> Maybe UTCTime -> Maybe CipherSuiteTag -> Maybe ConversationMLSData
toConversationMLSData :: Maybe GroupId
-> Maybe Epoch
-> Maybe UTCTime
-> Maybe CipherSuiteTag
-> Maybe ConversationMLSData
toConversationMLSData Maybe GroupId
mgid Maybe Epoch
mepoch Maybe UTCTime
mtimestamp Maybe CipherSuiteTag
mcs =
  GroupId -> Maybe ActiveMLSConversationData -> ConversationMLSData
ConversationMLSData
    (GroupId -> Maybe ActiveMLSConversationData -> ConversationMLSData)
-> Maybe GroupId
-> Maybe (Maybe ActiveMLSConversationData -> ConversationMLSData)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe GroupId
mgid
    Maybe (Maybe ActiveMLSConversationData -> ConversationMLSData)
-> Maybe (Maybe ActiveMLSConversationData)
-> Maybe ConversationMLSData
forall a b. Maybe (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Maybe ActiveMLSConversationData
-> Maybe (Maybe ActiveMLSConversationData)
forall a. a -> Maybe a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
      ( Epoch -> UTCTime -> CipherSuiteTag -> ActiveMLSConversationData
ActiveMLSConversationData
          (Epoch -> UTCTime -> CipherSuiteTag -> ActiveMLSConversationData)
-> Maybe Epoch
-> Maybe (UTCTime -> CipherSuiteTag -> ActiveMLSConversationData)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Epoch
mepoch
          Maybe (UTCTime -> CipherSuiteTag -> ActiveMLSConversationData)
-> Maybe UTCTime
-> Maybe (CipherSuiteTag -> ActiveMLSConversationData)
forall a b. Maybe (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Maybe UTCTime
mtimestamp
          Maybe (CipherSuiteTag -> ActiveMLSConversationData)
-> Maybe CipherSuiteTag -> Maybe ActiveMLSConversationData
forall a b. Maybe (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Maybe CipherSuiteTag
mcs
      )

toConv ::
  ConvId ->
  [LocalMember] ->
  [RemoteMember] ->
  Maybe
    ( ConvType,
      Maybe UserId,
      Maybe (Cql.Set Access),
      Maybe AccessRoleLegacy,
      Maybe (Cql.Set AccessRole),
      Maybe Text,
      Maybe TeamId,
      Maybe Bool,
      Maybe Milliseconds,
      Maybe ReceiptMode,
      Maybe ProtocolTag,
      Maybe GroupId,
      Maybe Epoch,
      Maybe (Writetime Epoch),
      Maybe CipherSuiteTag
    ) ->
  Maybe Conversation
toConv :: ConvId
-> [LocalMember]
-> [RemoteMember]
-> Maybe
     (ConvType, Maybe UserId, Maybe (Set Access),
      Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
      Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
      Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
      Maybe (Writetime Epoch), Maybe CipherSuiteTag)
-> Maybe Conversation
toConv ConvId
cid [LocalMember]
ms [RemoteMember]
remoteMems Maybe
  (ConvType, Maybe UserId, Maybe (Set Access),
   Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
   Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
   Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
   Maybe (Writetime Epoch), Maybe CipherSuiteTag)
mconv = do
  (ConvType
cty, Maybe UserId
muid, Maybe (Set Access)
acc, Maybe AccessRoleLegacy
role, Maybe (Set AccessRole)
roleV2, Maybe Text
nme, Maybe TeamId
ti, Maybe Bool
del, Maybe Milliseconds
timer, Maybe ReceiptMode
rm, Maybe ProtocolTag
ptag, Maybe GroupId
mgid, Maybe Epoch
mep, Maybe (Writetime Epoch)
mts, Maybe CipherSuiteTag
mcs) <- Maybe
  (ConvType, Maybe UserId, Maybe (Set Access),
   Maybe AccessRoleLegacy, Maybe (Set AccessRole), Maybe Text,
   Maybe TeamId, Maybe Bool, Maybe Milliseconds, Maybe ReceiptMode,
   Maybe ProtocolTag, Maybe GroupId, Maybe Epoch,
   Maybe (Writetime Epoch), Maybe CipherSuiteTag)
mconv
  let mbAccessRolesV2 :: Maybe (Set AccessRole)
mbAccessRolesV2 = [AccessRole] -> Set AccessRole
forall a. Ord a => [a] -> Set a
Set.fromList ([AccessRole] -> Set AccessRole)
-> (Set AccessRole -> [AccessRole])
-> Set AccessRole
-> Set AccessRole
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Set AccessRole -> [AccessRole]
forall a. Set a -> [a]
Cql.fromSet (Set AccessRole -> Set AccessRole)
-> Maybe (Set AccessRole) -> Maybe (Set AccessRole)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (Set AccessRole)
roleV2
      accessRoles :: Set AccessRole
accessRoles = ConvType -> Maybe (Set AccessRole) -> Set AccessRole
maybeRole ConvType
cty (Maybe (Set AccessRole) -> Set AccessRole)
-> Maybe (Set AccessRole) -> Set AccessRole
forall a b. (a -> b) -> a -> b
$ Maybe AccessRoleLegacy
-> Maybe (Set AccessRole) -> Maybe (Set AccessRole)
parseAccessRoles Maybe AccessRoleLegacy
role Maybe (Set AccessRole)
mbAccessRolesV2
  Protocol
proto <- Maybe ProtocolTag
-> Maybe GroupId
-> Maybe Epoch
-> Maybe UTCTime
-> Maybe CipherSuiteTag
-> Maybe Protocol
toProtocol Maybe ProtocolTag
ptag Maybe GroupId
mgid Maybe Epoch
mep (Writetime Epoch -> UTCTime
forall {k} (a :: k). Writetime a -> UTCTime
writetimeToUTC (Writetime Epoch -> UTCTime)
-> Maybe (Writetime Epoch) -> Maybe UTCTime
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (Writetime Epoch)
mts) Maybe CipherSuiteTag
mcs
  Conversation -> Maybe Conversation
forall a. a -> Maybe a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
    Conversation
      { $sel:convId:Conversation :: ConvId
convId = ConvId
cid,
        $sel:convDeleted:Conversation :: Bool
convDeleted = Bool -> Maybe Bool -> Bool
forall a. a -> Maybe a -> a
fromMaybe Bool
False Maybe Bool
del,
        $sel:convLocalMembers:Conversation :: [LocalMember]
convLocalMembers = [LocalMember]
ms,
        $sel:convRemoteMembers:Conversation :: [RemoteMember]
convRemoteMembers = [RemoteMember]
remoteMems,
        $sel:convProtocol:Conversation :: Protocol
convProtocol = Protocol
proto,
        $sel:convMetadata:Conversation :: ConversationMetadata
convMetadata =
          ConversationMetadata
            { $sel:cnvmType:ConversationMetadata :: ConvType
cnvmType = ConvType
cty,
              $sel:cnvmCreator:ConversationMetadata :: Maybe UserId
cnvmCreator = Maybe UserId
muid,
              $sel:cnvmAccess:ConversationMetadata :: [Access]
cnvmAccess = ConvType -> Maybe (Set Access) -> [Access]
defAccess ConvType
cty Maybe (Set Access)
acc,
              $sel:cnvmAccessRoles:ConversationMetadata :: Set AccessRole
cnvmAccessRoles = Set AccessRole
accessRoles,
              $sel:cnvmName:ConversationMetadata :: Maybe Text
cnvmName = Maybe Text
nme,
              $sel:cnvmTeam:ConversationMetadata :: Maybe TeamId
cnvmTeam = Maybe TeamId
ti,
              $sel:cnvmMessageTimer:ConversationMetadata :: Maybe Milliseconds
cnvmMessageTimer = Maybe Milliseconds
timer,
              $sel:cnvmReceiptMode:ConversationMetadata :: Maybe ReceiptMode
cnvmReceiptMode = Maybe ReceiptMode
rm
            }
      }

updateToMixedProtocol ::
  ( Members
      '[ Embed IO,
         Input ClientState
       ]
      r
  ) =>
  Local ConvId ->
  ConvType ->
  Sem r ()
updateToMixedProtocol :: forall (r :: EffectRow).
Members '[Embed IO, Input ClientState] r =>
QualifiedWithTag 'QLocal ConvId -> ConvType -> Sem r ()
updateToMixedProtocol QualifiedWithTag 'QLocal ConvId
lcnv ConvType
ct = do
  let gid :: GroupId
gid = GroupIdParts -> GroupId
convToGroupId (GroupIdParts -> GroupId)
-> (Qualified ConvOrSubConvId -> GroupIdParts)
-> Qualified ConvOrSubConvId
-> GroupId
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ConvType -> Qualified ConvOrSubConvId -> GroupIdParts
groupIdParts ConvType
ct (Qualified ConvOrSubConvId -> GroupId)
-> Qualified ConvOrSubConvId -> GroupId
forall a b. (a -> b) -> a -> b
$ ConvId -> ConvOrSubConvId
forall c s. c -> ConvOrSubChoice c s
Conv (ConvId -> ConvOrSubConvId)
-> Qualified ConvId -> Qualified ConvOrSubConvId
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> QualifiedWithTag 'QLocal ConvId -> Qualified ConvId
forall (t :: QTag) a. QualifiedWithTag t a -> Qualified a
tUntagged QualifiedWithTag 'QLocal ConvId
lcnv
      epoch :: Epoch
epoch = Word64 -> Epoch
Epoch Word64
0
  Client () -> Sem r ()
forall (r :: EffectRow) a.
(Member (Embed IO) r, Member (Input ClientState) r) =>
Client a -> Sem r a
embedClient (Client () -> Sem r ())
-> (BatchM () -> Client ()) -> BatchM () -> Sem r ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RetrySettings -> Client () -> Client ()
forall (m :: * -> *) a.
MonadClient m =>
RetrySettings -> m a -> m a
retry RetrySettings
x5 (Client () -> Client ())
-> (BatchM () -> Client ()) -> BatchM () -> Client ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BatchM () -> Client ()
forall (m :: * -> *). MonadClient m => BatchM () -> m ()
batch (BatchM () -> Sem r ()) -> BatchM () -> Sem r ()
forall a b. (a -> b) -> a -> b
$ do
    BatchType -> BatchM ()
setType BatchType
BatchLogged
    Consistency -> BatchM ()
setConsistency Consistency
LocalQuorum
    PrepQuery W (ConvId, ProtocolTag, GroupId, Epoch) ()
-> (ConvId, ProtocolTag, GroupId, Epoch) -> BatchM ()
forall a b.
(Show a, Tuple a, Tuple b) =>
PrepQuery W a b -> a -> BatchM ()
addPrepQuery PrepQuery W (ConvId, ProtocolTag, GroupId, Epoch) ()
Cql.updateToMixedConv (QualifiedWithTag 'QLocal ConvId -> ConvId
forall (t :: QTag) a. QualifiedWithTag t a -> a
tUnqualified QualifiedWithTag 'QLocal ConvId
lcnv, ProtocolTag
ProtocolMixedTag, GroupId
gid, Epoch
epoch)
  () -> Sem r ()
forall a. a -> Sem r a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()

updateToMLSProtocol ::
  ( Members
      '[ Embed IO,
         Input ClientState
       ]
      r
  ) =>
  Local ConvId ->
  Sem r ()
updateToMLSProtocol :: forall (r :: EffectRow).
Members '[Embed IO, Input ClientState] r =>
QualifiedWithTag 'QLocal ConvId -> Sem r ()
updateToMLSProtocol QualifiedWithTag 'QLocal ConvId
lcnv =
  Client () -> Sem r ()
forall (r :: EffectRow) a.
(Member (Embed IO) r, Member (Input ClientState) r) =>
Client a -> Sem r a
embedClient (Client () -> Sem r ())
-> (Client () -> Client ()) -> Client () -> Sem r ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RetrySettings -> Client () -> Client ()
forall (m :: * -> *) a.
MonadClient m =>
RetrySettings -> m a -> m a
retry RetrySettings
x5 (Client () -> Sem r ()) -> Client () -> Sem r ()
forall a b. (a -> b) -> a -> b
$
    PrepQuery W (ConvId, ProtocolTag) ()
-> QueryParams (ConvId, ProtocolTag) -> Client ()
forall (m :: * -> *) a (q :: * -> * -> * -> *).
(MonadClient m, Tuple a, RunQ q) =>
q W a () -> QueryParams a -> m ()
write PrepQuery W (ConvId, ProtocolTag) ()
Cql.updateToMLSConv (Consistency
-> (ConvId, ProtocolTag) -> QueryParams (ConvId, ProtocolTag)
forall a. Consistency -> a -> QueryParams a
params Consistency
LocalQuorum (QualifiedWithTag 'QLocal ConvId -> ConvId
forall (t :: QTag) a. QualifiedWithTag t a -> a
tUnqualified QualifiedWithTag 'QLocal ConvId
lcnv, ProtocolTag
ProtocolMLSTag))

interpretConversationStoreToCassandra ::
  ( Member (Embed IO) r,
    Member (Input ClientState) r,
    Member TinyLog r
  ) =>
  Sem (ConversationStore ': r) a ->
  Sem r a
interpretConversationStoreToCassandra :: forall (r :: EffectRow) a.
(Member (Embed IO) r, Member (Input ClientState) r,
 Member TinyLog r) =>
Sem (ConversationStore : r) a -> Sem r a
interpretConversationStoreToCassandra = (forall (rInitial :: EffectRow) x.
 ConversationStore (Sem rInitial) x -> Sem r x)
-> Sem (ConversationStore : r) a -> Sem r a
forall (e :: (* -> *) -> * -> *) (r :: EffectRow) a.
FirstOrder e "interpret" =>
(forall (rInitial :: EffectRow) x. e (Sem rInitial) x -> Sem r x)
-> Sem (e : r) a -> Sem r a
interpret ((forall (rInitial :: EffectRow) x.
  ConversationStore (Sem rInitial) x -> Sem r x)
 -> Sem (ConversationStore : r) a -> Sem r a)
-> (forall (rInitial :: EffectRow) x.
    ConversationStore (Sem rInitial) x -> Sem r x)
-> Sem (ConversationStore : r) a
-> Sem r a
forall a b. (a -> b) -> a -> b
$ \case
  ConversationStore (Sem rInitial) x
CreateConversationId -> do
    ByteString -> Sem r ()
forall (r :: EffectRow). Member TinyLog r => ByteString -> Sem r ()
logEffect ByteString
"ConversationStore.CreateConversationId"
    UUID -> x
UUID -> ConvId
forall {k} (a :: k). UUID -> Id a
Id (UUID -> x) -> Sem r UUID -> Sem r x
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO UUID -> Sem r UUID
forall (m :: * -> *) (r :: EffectRow) a.
Member (Embed m) r =>
m a -> Sem r a
embed IO UUID
nextRandom
  CreateConversation QualifiedWithTag 'QLocal ConvId
loc NewConversation
nc -> do
    ByteString -> Sem r ()
forall (r :: EffectRow). Member TinyLog r => ByteString -> Sem r ()
logEffect ByteString
"ConversationStore.CreateConversation"
    Client Conversation -> Sem r Conversation
forall (r :: EffectRow) a.
(Member (Embed IO) r, Member (Input ClientState) r) =>
Client a -> Sem r a
embedClient (Client Conversation -> Sem r Conversation)
-> Client Conversation -> Sem r Conversation
forall a b. (a -> b) -> a -> b
$ QualifiedWithTag 'QLocal ConvId
-> NewConversation -> Client Conversation
createConversation QualifiedWithTag 'QLocal ConvId
loc NewConversation
nc
  CreateMLSSelfConversation Local UserId
lusr -> do
    ByteString -> Sem r ()
forall (r :: EffectRow). Member TinyLog r => ByteString -> Sem r ()
logEffect ByteString
"ConversationStore.CreateMLSSelfConversation"
    Client Conversation -> Sem r Conversation
forall (r :: EffectRow) a.
(Member (Embed IO) r, Member (Input ClientState) r) =>
Client a -> Sem r a
embedClient (Client Conversation -> Sem r Conversation)
-> Client Conversation -> Sem r Conversation
forall a b. (a -> b) -> a -> b
$ Local UserId -> Client Conversation
createMLSSelfConversation Local UserId
lusr
  GetConversation ConvId
cid -> do
    ByteString -> Sem r ()
forall (r :: EffectRow). Member TinyLog r => ByteString -> Sem r ()
logEffect ByteString
"ConversationStore.GetConversation"
    Client (Maybe Conversation) -> Sem r (Maybe Conversation)
forall (r :: EffectRow) a.
(Member (Embed IO) r, Member (Input ClientState) r) =>
Client a -> Sem r a
embedClient (Client (Maybe Conversation) -> Sem r (Maybe Conversation))
-> Client (Maybe Conversation) -> Sem r (Maybe Conversation)
forall a b. (a -> b) -> a -> b
$ ConvId -> Client (Maybe Conversation)
getConversation ConvId
cid
  GetConversationEpoch ConvId
cid -> do
    ByteString -> Sem r ()
forall (r :: EffectRow). Member TinyLog r => ByteString -> Sem r ()
logEffect ByteString
"ConversationStore.GetConversationEpoch"
    Client (Maybe Epoch) -> Sem r (Maybe Epoch)
forall (r :: EffectRow) a.
(Member (Embed IO) r, Member (Input ClientState) r) =>
Client a -> Sem r a
embedClient (Client (Maybe Epoch) -> Sem r (Maybe Epoch))
-> Client (Maybe Epoch) -> Sem r (Maybe Epoch)
forall a b. (a -> b) -> a -> b
$ ConvId -> Client (Maybe Epoch)
getConvEpoch ConvId
cid
  GetConversations [ConvId]
cids -> do
    ByteString -> Sem r ()
forall (r :: EffectRow). Member TinyLog r => ByteString -> Sem r ()
logEffect ByteString
"ConversationStore.GetConversations"
    [ConvId] -> Sem r [Conversation]
forall (r :: EffectRow).
(Member (Embed IO) r, Member (Input ClientState) r,
 Member TinyLog r) =>
[ConvId] -> Sem r [Conversation]
localConversations [ConvId]
cids
  GetConversationMetadata ConvId
cid -> do
    ByteString -> Sem r ()
forall (r :: EffectRow). Member TinyLog r => ByteString -> Sem r ()
logEffect ByteString
"ConversationStore.GetConversationMetadata"
    Client (Maybe ConversationMetadata)
-> Sem r (Maybe ConversationMetadata)
forall (r :: EffectRow) a.
(Member (Embed IO) r, Member (Input ClientState) r) =>
Client a -> Sem r a
embedClient (Client (Maybe ConversationMetadata)
 -> Sem r (Maybe ConversationMetadata))
-> Client (Maybe ConversationMetadata)
-> Sem r (Maybe ConversationMetadata)
forall a b. (a -> b) -> a -> b
$ ConvId -> Client (Maybe ConversationMetadata)
conversationMeta ConvId
cid
  GetGroupInfo ConvId
cid -> do
    ByteString -> Sem r ()
forall (r :: EffectRow). Member TinyLog r => ByteString -> Sem r ()
logEffect ByteString
"ConversationStore.GetGroupInfo"
    Client (Maybe GroupInfoData) -> Sem r (Maybe GroupInfoData)
forall (r :: EffectRow) a.
(Member (Embed IO) r, Member (Input ClientState) r) =>
Client a -> Sem r a
embedClient (Client (Maybe GroupInfoData) -> Sem r (Maybe GroupInfoData))
-> Client (Maybe GroupInfoData) -> Sem r (Maybe GroupInfoData)
forall a b. (a -> b) -> a -> b
$ ConvId -> Client (Maybe GroupInfoData)
getGroupInfo ConvId
cid
  IsConversationAlive ConvId
cid -> do
    ByteString -> Sem r ()
forall (r :: EffectRow). Member TinyLog r => ByteString -> Sem r ()
logEffect ByteString
"ConversationStore.IsConversationAlive"
    Client Bool -> Sem r Bool
forall (r :: EffectRow) a.
(Member (Embed IO) r, Member (Input ClientState) r) =>
Client a -> Sem r a
embedClient (Client Bool -> Sem r Bool) -> Client Bool -> Sem r Bool
forall a b. (a -> b) -> a -> b
$ ConvId -> Client Bool
isConvAlive ConvId
cid
  SelectConversations UserId
uid [ConvId]
cids -> do
    ByteString -> Sem r ()
forall (r :: EffectRow). Member TinyLog r => ByteString -> Sem r ()
logEffect ByteString
"ConversationStore.SelectConversations"
    Client [ConvId] -> Sem r [ConvId]
forall (r :: EffectRow) a.
(Member (Embed IO) r, Member (Input ClientState) r) =>
Client a -> Sem r a
embedClient (Client [ConvId] -> Sem r [ConvId])
-> Client [ConvId] -> Sem r [ConvId]
forall a b. (a -> b) -> a -> b
$ UserId -> [ConvId] -> Client [ConvId]
localConversationIdsOf UserId
uid [ConvId]
cids
  GetRemoteConversationStatus UserId
uid [Remote ConvId]
cids -> do
    ByteString -> Sem r ()
forall (r :: EffectRow). Member TinyLog r => ByteString -> Sem r ()
logEffect ByteString
"ConversationStore.GetRemoteConversationStatus"
    Client (Map (Remote ConvId) MemberStatus)
-> Sem r (Map (Remote ConvId) MemberStatus)
forall (r :: EffectRow) a.
(Member (Embed IO) r, Member (Input ClientState) r) =>
Client a -> Sem r a
embedClient (Client (Map (Remote ConvId) MemberStatus)
 -> Sem r (Map (Remote ConvId) MemberStatus))
-> Client (Map (Remote ConvId) MemberStatus)
-> Sem r (Map (Remote ConvId) MemberStatus)
forall a b. (a -> b) -> a -> b
$ UserId
-> [Remote ConvId] -> Client (Map (Remote ConvId) MemberStatus)
remoteConversationStatus UserId
uid [Remote ConvId]
cids
  SetConversationType ConvId
cid ConvType
ty -> do
    ByteString -> Sem r ()
forall (r :: EffectRow). Member TinyLog r => ByteString -> Sem r ()
logEffect ByteString
"ConversationStore.SetConversationType"
    Client () -> Sem r ()
forall (r :: EffectRow) a.
(Member (Embed IO) r, Member (Input ClientState) r) =>
Client a -> Sem r a
embedClient (Client () -> Sem r ()) -> Client () -> Sem r ()
forall a b. (a -> b) -> a -> b
$ ConvId -> ConvType -> Client ()
updateConvType ConvId
cid ConvType
ty
  SetConversationName ConvId
cid Range 1 256 Text
value -> do
    ByteString -> Sem r ()
forall (r :: EffectRow). Member TinyLog r => ByteString -> Sem r ()
logEffect ByteString
"ConversationStore.SetConversationName"
    Client () -> Sem r ()
forall (r :: EffectRow) a.
(Member (Embed IO) r, Member (Input ClientState) r) =>
Client a -> Sem r a
embedClient (Client () -> Sem r ()) -> Client () -> Sem r ()
forall a b. (a -> b) -> a -> b
$ ConvId -> Range 1 256 Text -> Client ()
updateConvName ConvId
cid Range 1 256 Text
value
  SetConversationAccess ConvId
cid ConversationAccessData
value -> do
    ByteString -> Sem r ()
forall (r :: EffectRow). Member TinyLog r => ByteString -> Sem r ()
logEffect ByteString
"ConversationStore.SetConversationAccess"
    Client () -> Sem r ()
forall (r :: EffectRow) a.
(Member (Embed IO) r, Member (Input ClientState) r) =>
Client a -> Sem r a
embedClient (Client () -> Sem r ()) -> Client () -> Sem r ()
forall a b. (a -> b) -> a -> b
$ ConvId -> ConversationAccessData -> Client ()
updateConvAccess ConvId
cid ConversationAccessData
value
  SetConversationReceiptMode ConvId
cid ReceiptMode
value -> do
    ByteString -> Sem r ()
forall (r :: EffectRow). Member TinyLog r => ByteString -> Sem r ()
logEffect ByteString
"ConversationStore.SetConversationReceiptMode"
    Client () -> Sem r ()
forall (r :: EffectRow) a.
(Member (Embed IO) r, Member (Input ClientState) r) =>
Client a -> Sem r a
embedClient (Client () -> Sem r ()) -> Client () -> Sem r ()
forall a b. (a -> b) -> a -> b
$ ConvId -> ReceiptMode -> Client ()
updateConvReceiptMode ConvId
cid ReceiptMode
value
  SetConversationMessageTimer ConvId
cid Maybe Milliseconds
value -> do
    ByteString -> Sem r ()
forall (r :: EffectRow). Member TinyLog r => ByteString -> Sem r ()
logEffect ByteString
"ConversationStore.SetConversationMessageTimer"
    Client () -> Sem r ()
forall (r :: EffectRow) a.
(Member (Embed IO) r, Member (Input ClientState) r) =>
Client a -> Sem r a
embedClient (Client () -> Sem r ()) -> Client () -> Sem r ()
forall a b. (a -> b) -> a -> b
$ ConvId -> Maybe Milliseconds -> Client ()
updateConvMessageTimer ConvId
cid Maybe Milliseconds
value
  SetConversationEpoch ConvId
cid Epoch
epoch -> do
    ByteString -> Sem r ()
forall (r :: EffectRow). Member TinyLog r => ByteString -> Sem r ()
logEffect ByteString
"ConversationStore.SetConversationEpoch"
    Client () -> Sem r ()
forall (r :: EffectRow) a.
(Member (Embed IO) r, Member (Input ClientState) r) =>
Client a -> Sem r a
embedClient (Client () -> Sem r ()) -> Client () -> Sem r ()
forall a b. (a -> b) -> a -> b
$ ConvId -> Epoch -> Client ()
updateConvEpoch ConvId
cid Epoch
epoch
  SetConversationCipherSuite ConvId
cid CipherSuiteTag
cs -> do
    ByteString -> Sem r ()
forall (r :: EffectRow). Member TinyLog r => ByteString -> Sem r ()
logEffect ByteString
"ConversationStore.SetConversationCipherSuite"
    Client () -> Sem r ()
forall (r :: EffectRow) a.
(Member (Embed IO) r, Member (Input ClientState) r) =>
Client a -> Sem r a
embedClient (Client () -> Sem r ()) -> Client () -> Sem r ()
forall a b. (a -> b) -> a -> b
$ ConvId -> CipherSuiteTag -> Client ()
updateConvCipherSuite ConvId
cid CipherSuiteTag
cs
  DeleteConversation ConvId
cid -> do
    ByteString -> Sem r ()
forall (r :: EffectRow). Member TinyLog r => ByteString -> Sem r ()
logEffect ByteString
"ConversationStore.DeleteConversation"
    Client () -> Sem r ()
forall (r :: EffectRow) a.
(Member (Embed IO) r, Member (Input ClientState) r) =>
Client a -> Sem r a
embedClient (Client () -> Sem r ()) -> Client () -> Sem r ()
forall a b. (a -> b) -> a -> b
$ ConvId -> Client ()
deleteConversation ConvId
cid
  SetGroupInfo ConvId
cid GroupInfoData
gib -> do
    ByteString -> Sem r ()
forall (r :: EffectRow). Member TinyLog r => ByteString -> Sem r ()
logEffect ByteString
"ConversationStore.SetGroupInfo"
    Client () -> Sem r ()
forall (r :: EffectRow) a.
(Member (Embed IO) r, Member (Input ClientState) r) =>
Client a -> Sem r a
embedClient (Client () -> Sem r ()) -> Client () -> Sem r ()
forall a b. (a -> b) -> a -> b
$ ConvId -> GroupInfoData -> Client ()
setGroupInfo ConvId
cid GroupInfoData
gib
  AcquireCommitLock GroupId
gId Epoch
epoch NominalDiffTime
ttl -> do
    ByteString -> Sem r ()
forall (r :: EffectRow). Member TinyLog r => ByteString -> Sem r ()
logEffect ByteString
"ConversationStore.AcquireCommitLock"
    Client LockAcquired -> Sem r LockAcquired
forall (r :: EffectRow) a.
(Member (Embed IO) r, Member (Input ClientState) r) =>
Client a -> Sem r a
embedClient (Client LockAcquired -> Sem r LockAcquired)
-> Client LockAcquired -> Sem r LockAcquired
forall a b. (a -> b) -> a -> b
$ GroupId -> Epoch -> NominalDiffTime -> Client LockAcquired
acquireCommitLock GroupId
gId Epoch
epoch NominalDiffTime
ttl
  ReleaseCommitLock GroupId
gId Epoch
epoch -> do
    ByteString -> Sem r ()
forall (r :: EffectRow). Member TinyLog r => ByteString -> Sem r ()
logEffect ByteString
"ConversationStore.ReleaseCommitLock"
    Client () -> Sem r ()
forall (r :: EffectRow) a.
(Member (Embed IO) r, Member (Input ClientState) r) =>
Client a -> Sem r a
embedClient (Client () -> Sem r ()) -> Client () -> Sem r ()
forall a b. (a -> b) -> a -> b
$ GroupId -> Epoch -> Client ()
releaseCommitLock GroupId
gId Epoch
epoch
  UpdateToMixedProtocol QualifiedWithTag 'QLocal ConvId
cid ConvType
ct -> do
    ByteString -> Sem r ()
forall (r :: EffectRow). Member TinyLog r => ByteString -> Sem r ()
logEffect ByteString
"ConversationStore.UpdateToMixedProtocol"
    QualifiedWithTag 'QLocal ConvId -> ConvType -> Sem r ()
forall (r :: EffectRow).
Members '[Embed IO, Input ClientState] r =>
QualifiedWithTag 'QLocal ConvId -> ConvType -> Sem r ()
updateToMixedProtocol QualifiedWithTag 'QLocal ConvId
cid ConvType
ct
  UpdateToMLSProtocol QualifiedWithTag 'QLocal ConvId
cid -> do
    ByteString -> Sem r ()
forall (r :: EffectRow). Member TinyLog r => ByteString -> Sem r ()
logEffect ByteString
"ConversationStore.UpdateToMLSProtocol"
    QualifiedWithTag 'QLocal ConvId -> Sem r ()
forall (r :: EffectRow).
Members '[Embed IO, Input ClientState] r =>
QualifiedWithTag 'QLocal ConvId -> Sem r ()
updateToMLSProtocol QualifiedWithTag 'QLocal ConvId
cid