mirror of
https://github.com/simplex-chat/simplex-chat.git
synced 2026-11-04 08:37:30 +00:00
store: move the registration_changed answer
This commit is contained in:
1 parent
69ac68090c
commit
7fbbdd21cb
2 files changed
+17
-14
No files matched your search
@@ -35,9 +35,9 @@ import StoreService.Orders (publishRegistration)
|
||||
import StoreService.Providers (OrderDraft (..), Provider (..), ProviderError (..), ProviderInvoice (..), expiresItself)
|
||||
import StoreService.Store.Invoices (InvoiceRow (..), NewNameInvoice (..), getInvoice, insertNameInvoice, newInvoiceId, truncateToSecond)
|
||||
import StoreService.Waiters (Waiters, publish)
|
||||
import StoreService.Web.Http (Respond, badRequest, confirmationsFor, internalError, invalidName, invoiceAt, invoicePairs, invoiceWindow, jsonError, jsonResponse, nameBodyBytes, notFound, providerFor, providerNamed, providerUnavailable, readBoundedBody)
|
||||
import StoreService.Web.Http (Respond, badRequest, confirmationsFor, internalError, invalidName, invoiceAt, invoicePairs, invoiceWindow, jsonError, jsonResponse, nameBodyBytes, providerFor, providerNamed, providerUnavailable, readBoundedBody)
|
||||
import StoreService.Web.Names.Codes (codeRefusalResponse)
|
||||
import StoreService.Web.Names.Registration (registrationAt)
|
||||
import StoreService.Web.Names.Registration (registrationAt, registrationChangedResponse)
|
||||
|
||||
data InvoiceOrder = InvoiceOrder
|
||||
{ ioRequest :: OrderRequest,
|
||||
@@ -221,16 +221,6 @@ nameInvoiceView OrdersEnv {oeStore, oeConfig, oeContracts} confirmations row@Inv
|
||||
view <- registrationView oeStore oeConfig oeContracts r
|
||||
pure $ J.object $ ["invoiceId" .= invId, "kind" .= ("name" :: Text), "registration" .= view] <> invoicePairs confirmations row
|
||||
|
||||
-- | The registration's current view, for a request that lost the race to change it.
|
||||
registrationChangedResponse :: OrdersEnv -> RegistrationId -> IO Response
|
||||
registrationChangedResponse OrdersEnv {oeStore, oeConfig, oeContracts, oeNow} rid =
|
||||
withTransaction oeStore (`getRegistration` rid) >>= \case
|
||||
Nothing -> pure notFound
|
||||
Just r -> do
|
||||
now <- oeNow
|
||||
view <- registrationView oeStore oeConfig oeContracts (registrationAt now r)
|
||||
pure $ jsonResponse status409 $ J.object ["error" .= ("registration_changed" :: Text), "registration" .= view]
|
||||
|
||||
orderRefusalResponse :: OrderRefusal -> Response
|
||||
orderRefusalResponse = \case
|
||||
ORBadRequest -> badRequest
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
module StoreService.Web.Names.Registration
|
||||
( registrationHandler,
|
||||
registrationChangedResponse,
|
||||
registrationAt,
|
||||
registrationVersion,
|
||||
lapseMoment,
|
||||
@@ -14,6 +15,8 @@ module StoreService.Web.Names.Registration
|
||||
where
|
||||
|
||||
import Control.Monad (join)
|
||||
import Data.Aeson ((.=))
|
||||
import qualified Data.Aeson as J
|
||||
import qualified Data.ByteString.Char8 as B
|
||||
import Data.Char (isDigit)
|
||||
import Data.Int (Int64)
|
||||
@@ -21,8 +24,8 @@ import Data.Maybe (maybeToList)
|
||||
import Data.Text (Text)
|
||||
import Data.Time.Clock (UTCTime, diffUTCTime)
|
||||
import Data.Time.Clock.POSIX (posixSecondsToUTCTime)
|
||||
import Network.HTTP.Types (status200)
|
||||
import Network.Wai (Request, ResponseReceived, queryString)
|
||||
import Network.HTTP.Types (status200, status409)
|
||||
import Network.Wai (Request, Response, ResponseReceived, queryString)
|
||||
import Simplex.Messaging.Agent.Store.Common (DBStore, withTransaction)
|
||||
import StoreService.Names.Orders (OrdersEnv (..))
|
||||
import StoreService.Names.Phases (RegistrationId (..), awaitChange)
|
||||
@@ -46,6 +49,16 @@ registrationHandler OrdersEnv {oeStore, oeConfig, oeContracts, oePhases, oeNow}
|
||||
readNow = oeNow >>= \now -> fmap (now,) <$> withTransaction oeStore (`getRegistration` rid)
|
||||
answer (now, r) = respond . jsonResponse status200 =<< registrationView oeStore oeConfig oeContracts (registrationAt now r)
|
||||
|
||||
-- | The registration's current view, for a request that lost the race to change it.
|
||||
registrationChangedResponse :: OrdersEnv -> RegistrationId -> IO Response
|
||||
registrationChangedResponse OrdersEnv {oeStore, oeConfig, oeContracts, oeNow} rid =
|
||||
withTransaction oeStore (`getRegistration` rid) >>= \case
|
||||
Nothing -> pure notFound
|
||||
Just r -> do
|
||||
now <- oeNow
|
||||
view <- registrationView oeStore oeConfig oeContracts (registrationAt now r)
|
||||
pure $ jsonResponse status409 $ J.object ["error" .= ("registration_changed" :: Text), "registration" .= view]
|
||||
|
||||
-- | Versions start at 1, so a registration deleted during a hold reads as changed.
|
||||
registrationVersion :: DBStore -> RegistrationId -> IO Int64
|
||||
registrationVersion st rid = maybe 0 nrVersion <$> withTransaction st (`getRegistration` rid)
|
||||
|
||||
Reference in new issue
Block a user