diff --git a/apps/simplex-store-service/README.md b/apps/simplex-store-service/README.md index 15a2580311..44a4fba7c4 100644 --- a/apps/simplex-store-service/README.md +++ b/apps/simplex-store-service/README.md @@ -1357,26 +1357,26 @@ commands of this console, are in [Refunding a name balance](#refunding-a-name-ba ### From the group With `[group]` in `store_service.ini`, the service manages one group and serves three commands in -it: `/issue` and `/bulk` for moderators and above, `/revoke ` for admins and owners. Name codes -need an admin or owner too, as each one registers at the sponsor allowance's and the gas key's cost, -and the group issues at most 100 of them in 24 hours (`//issue name` in `--run-cli` has no limit). Both -code commands take every kind of code in one form: +it: `/issue` and `/bulk` for moderators and above, `/revoke ` for admins and owners. Each name code +registers at the sponsor allowance's and the gas key's cost, so the group issues at most 10 of them +in 24 hours (`//issue name` in `--run-cli` has no limit, and +its codes are not counted). The code commands take these forms: ``` /issue [] [years | months ] [uses ] -/bulk [] [years | months ] count +/bulk [months ] count ``` -`/issue` makes one code and `/bulk` makes `` single-use codes of the kind. `` is `name` -with a length, or a badge type: `supporter`, `legend` or `investor`. A name takes `years`, with the -bounds of `//issue name` (length 6 to 63, years 2 to 10, 2 by default); a name code issued in the -group is single-use and `free`. A badge takes `months`, 1 to 255 and 1 by default, and in `/issue` -also `uses`, 1 to 1000. `count` is 1 to 100. A value outside these bounds, a length for a badge, or -the other kind's unit gets the usage reply, which lists the form, each kind's bounds and an example -of each. The group's command menu shows every kind in each command's hint and fills the hint into -the message box. Without `[names]`, `/issue name` and `/bulk name` get a reply that names are not -enabled, and issue nothing. `/revoke` takes badge and name codes, so a name code issued before names -were turned off can still be withdrawn. The group has no `/refund`. A member's role +`/issue` makes one code and `/bulk` makes `` single-use badge codes. `` is `name` with a +length, in `/issue` only, or a badge type: `supporter`, `legend` or `investor`. A name takes a length +of 6 to 63 and `years`, 2 to 10 and 2 by default; a name code issued in the group is single-use and +`free`. A badge takes `months`, 1 to 255 and 1 +by default, and in `/issue` also `uses`, 1 to 1000. `count` is 1 to 100. A value outside these +bounds, a length for a badge, `months` or `uses` for a name, or `name` in `/bulk` gets the usage reply, which +lists the form, each kind's bounds and an example of each. The group's command menu shows the kinds +of each command in its hint and fills the hint into the message box. Without `[names]`, `/issue name` +gets a reply that names are not enabled, and issues nothing. `/revoke` takes badge and name codes, so +a name code issued before names were turned off can still be withdrawn. The group has no `/refund`. A member's role is checked as the service last saw it, so a command sent by a moderator just demoted or removed can still run if it reaches the service first; revoke any code the service posts for them after the change. `uses` above 1 makes a multi-use code, tracked by a group message showing its remaining uses and the time @@ -1402,11 +1402,13 @@ group command the service received but had not run when it stopped, or received `--run-cli` mode, is dropped with no reply, so resend it, or use `//revoke`. The first member to join through the link is promoted to owner, so the operator joins before sharing -it. Keep the service an owner too: below owner it cannot update the group's command menu, and below -author it cannot post codes or replies. A failed promotion is logged at once, and an owner who left -is logged at the next start or join. Then make the member you choose owner with the `/mr` command -that the log line names, in `--run-cli` mode; the service never promotes anyone once the first -promotion was attempted. +it. Keep the service an owner too: below owner it cannot update the group's command menu or its member +review setting, though as an admin it still holds joiners for review; below admin it ignores join +requests, and below author it cannot post codes or replies. A failed promotion is logged at +once, and an owner who left is logged at the next start or join. Then make the member you choose +owner with the `/mr` command that the log line names, in `--run-cli` mode; a member pending review +is accepted as owner with the log line's `/accept member # @ owner` instead, as `/mr` +refuses a pending member. The service never promotes anyone once the first promotion was attempted. The join link is never logged, as anyone who has it can join. The service stores it in the `group_link` column of `sx_badge_service_group`, and the operator reads it there to join, in the chat @@ -1416,9 +1418,16 @@ schema (`simplex_v1_chat_schema` by default): psql "$BADGE_DB_CONN" -Atc 'SELECT group_link FROM simplex_v1_chat_schema.sx_badge_service_group' ``` -The link stays valid: anyone who has it can join later, as a member, and read every code posted or -edited from then on. That includes a removed member, who can rejoin through it, so removing a member -does not stop them seeing new codes. A database copy holds it, as it holds the codes. +The link stays valid, but once the first promotion was attempted the service holds every joiner for +review, a removed member who rejoins included, in `--run-cli` mode too: the joiner waits until a +moderator, admin or owner accepts them, and receives no group messages until then. The service decides +this from its own record when the join request arrives, so a request handled at start, before the group +lane runs, and one that arrives while an owner has turned review off are held too. The first member is +not reviewed, as only the service could accept them. The group's member review setting is turned on +with that promotion, or at the next start for a group promoted before. In service mode it is turned +on again when an owner turns it off, and at every join while the group has an owner; an owner's +change made while the service runs in `--run-cli` mode stays until the next start in service mode. A +database copy holds the link, as it holds the codes. If an owner deletes the group, or removes the service from it, the service logs an error on start and stops serving the group. To create a new group, stop the service, delete the row, and start it diff --git a/apps/simplex-store-service/docs/names.md b/apps/simplex-store-service/docs/names.md index 289c1ac34f..33bcb7d522 100644 --- a/apps/simplex-store-service/docs/names.md +++ b/apps/simplex-store-service/docs/names.md @@ -83,7 +83,7 @@ How a request flows: 7. Every transaction is sent through the sender: dry run, nonce, fees, EIP-1559 signature with the registrar key, a saved row, then broadcast. Receipts are followed every 4 s to the confirmation depth ([`Names/Sender.hs:107-131`](../src/StoreService/Names/Sender.hs#L107-L131), [`Names/Sender.hs:151-226`](../src/StoreService/Names/Sender.hs#L151-L226)). 8. A link edit is submitted to the sender on its request's thread, and pending edits are settled by the record edits lane every 4 s ([`Names/Records.hs:156-263`](../src/StoreService/Names/Records.hs#L156-L263), [`Names/Records.hs:265-302`](../src/StoreService/Names/Records.hs#L265-L302)). 9. Link profiles are read through the chat core's connect plan, and the link data through SMP servers ([`Names/Profiles.hs:269-274`](../src/StoreService/Names/Profiles.hs#L269-L274), [`Chat/Library/Internal.hs:1723-1726`](../../../src/Simplex/Chat/Library/Internal.hs#L1723-L1726)). -10. Operators issue and revoke codes in the managed group while the service runs. In the console, a separate run of the same binary while the service is stopped, they issue and revoke codes, record refunds and close failed paid orders ([`Group.hs:263-300`](../src/StoreService/Group.hs#L263-L300), [`Main.hs:25-28`](../Main.hs#L25-L28), [`Service.hs:415-434`](../src/StoreService/Service.hs#L415-L434)). +10. Operators issue and revoke codes in the managed group while the service runs. In the console, a separate run of the same binary while the service is stopped, they issue and revoke codes, record refunds and close failed paid orders ([`Group.hs:278-313`](../src/StoreService/Group.hs#L278-L313), [`Main.hs:25-28`](../Main.hs#L25-L28), [`Service.hs:415-434`](../src/StoreService/Service.hs#L415-L434)). 11. One process runs per database: a PostgreSQL advisory lock is held on a connection of its own for the whole run ([`Store/Migrate.hs:46-58`](../src/StoreService/Store/Migrate.hs#L46-L58)). --- @@ -105,9 +105,9 @@ How a request flows: | In-memory coordination | Waiters: invoice status and payments. Phases: registration versions. Locks: one writer at a time per label hash, prepaid code and balance. Read hints: up to 256 webhook reads queued for the poller. | [`Waiters.hs:14-91`](../src/StoreService/Waiters.hs#L14-L91), [`Names/Phases.hs:15-65`](../src/StoreService/Names/Phases.hs#L15-L65), [`Names/Turns.hs:3-69`](../src/StoreService/Names/Turns.hs#L3-L69), [`Poller.hs:88-108`](../src/StoreService/Poller.hs#L88-L108) | | Chat core | The SimpleX chat controller with the bot user: the bot address for badge requests, the managed group's messages, and connect plans for link profiles. Every chat event is read from `outputQ` by one reader. | [`Service.hs:293-313`](../src/StoreService/Service.hs#L293-L313), [`Service.hs:636-645`](../src/StoreService/Service.hs#L636-L645), [`Names/Profiles.hs:269-274`](../src/StoreService/Names/Profiles.hs#L269-L274) | | Badge request lanes | Badge requests on the bot address, present before this PR: three lanes, each answer sent as `APISendServiceResponse`. | [`Service.hs:572-657`](../src/StoreService/Service.hs#L572-L657) | -| Group lane | With `[group]`: one managed group. `/issue` and `/bulk` are taken from moderators, `/revoke` and name codes from admins. A name code command is refused once the name codes created in the last 24 hours, console ones included, would pass 100. | [`Group.hs:87-304`](../src/StoreService/Group.hs#L87-L304), [`Group/Command.hs:59-73`](../src/StoreService/Group/Command.hs#L59-L73), [`Group/Command.hs:216-220`](../src/StoreService/Group/Command.hs#L216-L220) | +| Group lane | With `[group]`: one managed group. `/issue`, `/issue name` included, and `/bulk` are taken from moderators, `/revoke` from admins; `/bulk` issues badge codes only. A name code command is refused once the name codes the group issued in the last 24 hours would pass 10. Once the first promotion was attempted, every joiner waits for a moderator's review, decided when the join request arrives. | [`Group.hs:89-340`](../src/StoreService/Group.hs#L89-L340), [`Group/Command.hs:58-71`](../src/StoreService/Group/Command.hs#L58-L71), [`Group/Command.hs:220-224`](../src/StoreService/Group/Command.hs#L220-L224) | | Console | The same binary under `--run-cli`, while the service is stopped: the terminal chat and the badge request lanes only, with `//issue`, `//issue name`, `//revoke`, `//refund` and `//close`. `//close` reads the chain through a client of its own, made at start with `[names]`. | [`Main.hs:25-28`](../Main.hs#L25-L28), [`Service.hs:383-540`](../src/StoreService/Service.hs#L383-L540) | -| PostgreSQL | One database (`BADGE_DB_CONN`) with the chat core's schema and the `sx_badge_service_` tables. Names tables: prepaid codes, registrations, balances, name invoices, late credits, record edits and their changes, transactions, terms texts. Partial unique indexes admit one active order (`live = 1`) per commitment and per owner and label hash. | [`Store/Postgres/Migrations.hs:276-478`](../src/StoreService/Store/Postgres/Migrations.hs#L276-L478), [`compose.yml:16`](../../../scripts/store-service/compose.yml#L16) | +| PostgreSQL | One database (`BADGE_DB_CONN`) with the chat core's schema and the `sx_badge_service_` tables. Names tables: prepaid codes, registrations, balances, name invoices, late credits, record edits and their changes, transactions, terms texts. Partial unique indexes admit one active order (`live = 1`) per commitment and per owner and label hash. | [`Store/Postgres/Migrations.hs:276-479`](../src/StoreService/Store/Postgres/Migrations.hs#L276-L479), [`compose.yml:16`](../../../scripts/store-service/compose.yml#L16) | | Names page | simplex.domains, in the submodule `apps/simplex-domains`, outside this checkout. `/api/*` is called on its own origin, and the owner's EIP-191 and EIP-712 signatures are sent from it. | [`.gitmodules:4-7`](../../../.gitmodules#L4-L7), [`Web/Names.hs:28-44`](../src/StoreService/Web/Names.hs#L28-L44) | | BTCPay | Bitcoin and Monero invoices through Greenfield: create, read, list, cancel (status `Invalid`), store rates; webhooks signed with `BTCPay-Sig`. | [`Providers/BTCPay.hs:267-367`](../src/StoreService/Providers/BTCPay.hs#L267-L367), [`Providers/BTCPay.hs:524-538`](../src/StoreService/Providers/BTCPay.hs#L524-L538), [`Providers/BTCPay.hs:555-578`](../src/StoreService/Providers/BTCPay.hs#L555-L578) | | Stripe | Card PaymentIntents: create with an `Idempotency-Key`, read, list, cancel, also after 3 declined charges; webhooks signed with `Stripe-Signature`, within 15 minutes. | [`Providers/Stripe.hs:101-302`](../src/StoreService/Providers/Stripe.hs#L101-L302) | @@ -257,7 +257,7 @@ sequenceDiagram alt console, with the service stopped O->>C: //issue name 6 years 5 else managed group, admins only - O->>C: /issue name 6 years 5, a group message + O->>C: /issue name 6, a group message end C->>D: random code, only its SHA-256 stored with length and years C-->>O: Code: SN6-..., shown once @@ -278,7 +278,7 @@ sequenceDiagram Notes: - Only the code's SHA-256 is stored, so a code not copied when shown cannot be recovered ([`Names/Codes.hs:147-159`](../src/StoreService/Names/Codes.hs#L147-L159)). `unpaid` name codes are refused in the console ([`Service.hs:535-543`](../src/StoreService/Service.hs#L535-L543)). -- In the group, `/issue` is taken from moderators and above, a name code from admins. A name code command is refused once the name codes created in the last 24 hours, console ones included, would pass 100; the console has no cap ([`Group/Command.hs:59-73`](../src/StoreService/Group/Command.hs#L59-L73), [`Group.hs:289-294`](../src/StoreService/Group.hs#L289-L294), [`Service.hs:421-422`](../src/StoreService/Service.hs#L421-L422)). +- In the group, `/issue` is taken from moderators and above, a name code too, for 2 to 10 years. A name code command is refused once the name codes the group issued in the last 24 hours would pass 10; the console has no cap ([`Group/Command.hs:58-71`](../src/StoreService/Group/Command.hs#L58-L71), [`Group.hs:322-328`](../src/StoreService/Group.hs#L322-L328), [`Service.hs:425-426`](../src/StoreService/Service.hs#L425-L426)). - A code order is checked for the timestamp, the duration, the terms and an available name; its years are the code's, 2 for a community name ([`Web/Names/Orders.hs:216-222`](../src/StoreService/Web/Names/Orders.hs#L216-L222), [`Names/Community.hs:54-59`](../src/StoreService/Names/Community.hs#L54-L59)). - The code is spent when the name is registered, released when the order is set `taken`, and kept for 30 days with a failed order for Try again, then released by the daily sweep ([`Names/Orders.hs:397-400`](../src/StoreService/Names/Orders.hs#L397-L400), [`Names/Worker.hs:361-365`](../src/StoreService/Names/Worker.hs#L361-L365), [`Names/Worker.hs:503-522`](../src/StoreService/Names/Worker.hs#L503-L522)). - A name code is withdrawn by `//revoke` unless it is spent; a code an order already holds stays usable by that order ([`Names/Codes.hs:162-173`](../src/StoreService/Names/Codes.hs#L162-L173), [`Names/Codes.hs:78-88`](../src/StoreService/Names/Codes.hs#L78-L88)). diff --git a/apps/simplex-store-service/src/StoreService/Group.hs b/apps/simplex-store-service/src/StoreService/Group.hs index 8eadea9730..f5aabf7676 100644 --- a/apps/simplex-store-service/src/StoreService/Group.hs +++ b/apps/simplex-store-service/src/StoreService/Group.hs @@ -7,6 +7,7 @@ module StoreService.Group ( ensureManagedGroup, + joinerAcceptance, runGroupLane, inertGroupConfig, GroupEvent (..), @@ -29,11 +30,11 @@ import StoreService.Codes (issueFailedText, issueOneCode, revokeBadgeCode, singl import StoreService.Config (GroupConfig (..)) import StoreService.Group.Command (CmdAction (..), GroupCmd (..), groupCmdAction, groupCommands) import StoreService.Names.Codes (issueNameCode, revokeNameCode) -import StoreService.Names.Store (nameCodesIssuedSince) +import StoreService.Names.Store (CodeIssuer (..), groupNameCodesIssuedSince) import StoreService.Names.Turns (Turns) import StoreService.Store (CodeTracker (..), ManagedGroup (..), RevokeResult (..), clearCodeGroupItems, getCodeTracker, getEditableTrackers, getManagedGroup, insertManagedGroup, markOwnerBootstrapped, setCodeGroupItem) import StoreService.Store.Invoices (truncateToSecond) -import Control.Concurrent.STM (TQueue, atomically, flushTQueue, readTQueue, readTVarIO, writeTQueue) +import Control.Concurrent.STM (TMVar, TQueue, atomically, flushTQueue, readTMVar, readTQueue, readTVarIO, writeTQueue) import Control.Logger.Simple (logError, logNote, logWarn) import Control.Monad (forM_, forever, mfilter, replicateM, unless, void) import Control.Monad.Except (runExceptT) @@ -60,10 +61,11 @@ import Simplex.Chat.Messages import Simplex.Chat.Messages.CIContent (CIContent (..), ciContentToText) import Simplex.Chat.Protocol (MsgContent (..)) import Simplex.Chat.Store.Messages (getGroupChatItem) +import Simplex.Chat.Store.Profiles (GroupLinkInfo (..)) import Simplex.Chat.Store.Shared (StoreError (..)) import Simplex.Chat.Types -import Simplex.Chat.Types.Preferences (GroupPreferences (..), commands_, emptyGroupPrefs) -import Simplex.Chat.Types.Shared (GroupMemberRole (..)) +import Simplex.Chat.Types.Preferences (GroupPreferences (..), emptyGroupPrefs) +import Simplex.Chat.Types.Shared (GroupAcceptance (..), GroupMemberRole (..)) import Simplex.Chat.View (simplexChatContact) import Simplex.Messaging.Agent.Protocol (CreatedConnLink (..), UserId) import Simplex.Messaging.Encoding.String (StrEncoding, strEncode) @@ -91,12 +93,12 @@ ensureManagedGroup cc gc = -- The service stops instead, so a restart retries rather than leaving the group unserved. Left _ -> logError "badge group lookup failed, stopping" >> exitFailure -- The join link is a bearer secret, so it is never logged. - Right (Just ManagedGroup {mgGroupId}) -> + Right (Just ManagedGroup {mgGroupId, mgOwnerBootstrapped}) -> sendChatCmd cc (APIGroupInfo mgGroupId) >>= \case Right CRGroupInfo {groupInfo = GroupInfo {membership, groupProfile = p}} | memberCurrent membership -> do forM_ (inertGroupConfig gc p) logWarn - unless (commandsCurrent p) $ advertiseCommands cc mgGroupId p + syncServiceProfile cc mgGroupId mgOwnerBootstrapped p logNote "badge group ready" pure (Just mgGroupId) | otherwise -> groupGone @@ -134,17 +136,43 @@ orphanHint :: GroupName -> Text orphanHint gName = "delete this orphan group with /d #" <> viewName gName <> " in --run-cli mode" -- An existing group gets the current commands, so an upgrade that changes them reaches its members. -commandsCurrent :: GroupProfile -> Bool -commandsCurrent groupProfile = (groupPreferences groupProfile >>= commands_) == Just groupCommands - -advertiseCommands :: ChatController -> GroupId -> GroupProfile -> IO () -advertiseCommands cc groupId groupProfile = - sendChatCmd cc (APIUpdateGroupProfile groupId p') >>= \case - Right CRGroupUpdated {} -> logNote "badge group commands advertised" - r -> logError ("badge group profile update failed: " <> tshow r) +syncServiceProfile :: ChatController -> GroupId -> Bool -> GroupProfile -> IO () +syncServiceProfile cc groupId ownerSetUp groupProfile = + unless (p' == groupProfile) $ + sendChatCmd cc (APIUpdateGroupProfile groupId p') >>= \case + Right CRGroupUpdated {} -> logNote "badge group profile updated" + r -> logError ("badge group profile update failed: " <> tshow r) where - prefs = fromMaybe emptyGroupPrefs (groupPreferences groupProfile) - p' = groupProfile {groupPreferences = Just (prefs {commands = Just groupCommands} :: GroupPreferences)} + p' = serviceProfile ownerSetUp groupProfile + +restoreServiceProfile :: ChatController -> GroupId -> IO () +restoreServiceProfile cc groupId = + withDB' "getManagedGroup" cc getManagedGroup >>= \case + Right (Just ManagedGroup {mgOwnerBootstrapped}) -> + sendChatCmd cc (APIGroupInfo groupId) >>= \case + Right CRGroupInfo {groupInfo = GroupInfo {groupProfile}} -> syncServiceProfile cc groupId mgOwnerBootstrapped groupProfile + r -> logError ("badge group info failed: " <> tshow r) + _ -> pure () + +serviceProfile :: Bool -> GroupProfile -> GroupProfile +serviceProfile ownerSetUp p@GroupProfile {groupPreferences, memberAdmission} = + p {groupPreferences = Just (prefs {commands = Just groupCommands} :: GroupPreferences), memberAdmission = admission} + where + prefs = fromMaybe emptyGroupPrefs groupPreferences + admission + | ownerSetUp = Just (fromMaybe emptyGroupMemberAdmission memberAdmission) {review = Just MCAll} + | otherwise = memberAdmission + +-- The first joiner is not held for review, as nobody could approve them before the group has an owner. +joinerAcceptance :: TMVar ChatController -> GroupInfo -> GroupLinkInfo -> Profile -> IO (Either GroupRejectionReason (GroupAcceptance, GroupMemberRole)) +joinerAcceptance startedCC GroupInfo {groupId} GroupLinkInfo {memberRole} _ = do + cc <- atomically $ readTMVar startedCC + withDB' "getManagedGroup" cc getManagedGroup <&> \managed -> Right (acceptance managed, memberRole) + where + acceptance = \case + Right (Just ManagedGroup {mgGroupId, mgOwnerBootstrapped}) | mgGroupId == groupId && mgOwnerBootstrapped -> GAPendingReview + Right _ -> GAAccepted + Left _ -> GAPendingReview -- Config is applied only at creation, since a group rename is broadcast to every member. inertGroupConfig :: GroupConfig -> GroupProfile -> Maybe Text @@ -174,6 +202,7 @@ data GroupEvent data GroupAction = GAJoined + | GAProfileUpdated | GACommand GroupMemberRole Text deriving (Eq, Show) @@ -182,6 +211,7 @@ data GroupAction groupEvent :: ChatEvent -> Maybe GroupEvent groupEvent = \case CEvtJoinedGroupMember {groupInfo = GroupInfo {groupId}} -> Just $ GEInGroup groupId GAJoined + CEvtGroupUpdated {toGroup = GroupInfo {groupId}} -> Just $ GEInGroup groupId GAProfileUpdated CEvtNewChatItems {chatItems = AChatItem _ _ (GroupChat GroupInfo {groupId} scope) ChatItem {chatDir = CIGroupRcv m, content = CIRcvMsgContent (MCText t), meta = CIMeta {itemDeleted, itemLive}} : _} | isNothing scope && isNothing itemDeleted && itemLive /= Just True -> Just $ GEInGroup groupId (GACommand (memberRole' m) t) _ -> Nothing @@ -207,6 +237,7 @@ handleGroupEvent cc turns namesOn groupId ev = logUncaught (handle ev) | gid /= groupId -> pure () | otherwise -> case action of GAJoined -> promoteOwner cc groupId + GAProfileUpdated -> restoreServiceProfile cc groupId GACommand role t -> case groupCmdAction role t of RunCmd cmd -> runGroupCmd cc turns namesOn groupId cmd ReplyText txt -> void $ sendGroupText cc groupId "command reply" txt @@ -214,14 +245,15 @@ handleGroupEvent cc turns namesOn groupId ev = logUncaught (handle ev) GETracker badgeCodeId code -> updateTracker cc groupId badgeCodeId code promoteFirstOwner :: ChatController -> GroupInfo -> GroupMember -> IO () -promoteFirstOwner cc g@GroupInfo {groupId} member = +promoteFirstOwner cc g@GroupInfo {groupId, groupProfile} member = -- The flag is set before promoting, so a retry cannot promote a second owner. withDB' "markOwnerBootstrapped" cc (`markOwnerBootstrapped` groupId) >>= \case - Right True -> + Right True -> do sendChatCmd cc (APIMembersRole groupId (groupMemberId' member :| []) GROwner) >>= \case -- The core returns no members when every store update failed. Right CRMembersRoleUser {members = _ : _} -> logNote $ "badge group owner promoted: member " <> tshow (groupMemberId' member) r -> logError $ "badge group owner promotion failed: " <> tshow r <> " - " <> noOwnerHint (groupName' g) + syncServiceProfile cc groupId True groupProfile _ -> pure () -- Nothing is a group that is not served, so its events are drained. @@ -243,10 +275,11 @@ promoteOwner cc groupId = withDB' "getManagedGroup" cc getManagedGroup >>= \case Right (Just ManagedGroup {mgOwnerBootstrapped}) -> sendChatCmd cc (APIListMembers groupId) >>= \case - Right CRGroupMembers {group = Group {groupInfo, members}} + Right CRGroupMembers {group = Group {groupInfo = groupInfo@GroupInfo {groupProfile}, members}} -- An owner made by hand only needs the flag set, or promoting would add a second owner. - | any (\m -> memberRole' m == GROwner && memberCurrent m) members -> + | any (\m -> memberRole' m == GROwner && memberCurrent m) members -> do unless mgOwnerBootstrapped $ void $ withDB' "markOwnerBootstrapped" cc (`markOwnerBootstrapped` groupId) + syncServiceProfile cc groupId True groupProfile -- A crash between setting the flag and promoting leaves no owner, and only the operator may retry. | mgOwnerBootstrapped -> logError $ noOwnerHint (groupName' groupInfo) | otherwise -> mapM_ (promoteFirstOwner cc groupInfo) $ listToMaybe $ sortOn groupMemberId' $ filter joined members @@ -258,7 +291,9 @@ promoteOwner cc groupId = -- The live local name, quoted as --run-cli parses it, can differ from the configured one after a name clash or a rename. noOwnerHint :: GroupName -> Text -noOwnerHint gName = "the badge group has no member owner, make one with /mr #" <> viewName gName <> " owner in --run-cli mode" +noOwnerHint gName = + "the badge group has no member owner, make one with /mr #" <> viewName gName <> " owner in --run-cli mode" + <> ", or a member pending review with /accept member #" <> viewName gName <> " @ owner in --run-cli mode" runGroupCmd :: ChatController -> Turns -> Bool -> GroupId -> GroupCmd -> IO () runGroupCmd cc turns namesOn groupId = \case @@ -273,10 +308,8 @@ runGroupCmd cc turns namesOn groupId = \case >>= mapM_ (\iid -> withDB' "setCodeGroupItem" cc $ \db -> setCodeGroupItem db badgeCodeId iid now) GCBulk bt months count -> issueMany count (fmap (formatBadgeCode . fst) <$> issueOneCode cc bt months CPSFree singleUse) GCIssueName {} | not namesOn -> reply namesOffText - GCBulkName {} | not namesOn -> reply namesOffText GCIssueName len years -> - withinNameCodes 1 $ issueNameCode cc len years CPSFree >>= reply . either (const issueFailedText) (("Code: " <>) . formatNameCode . NCPrepaid) - GCBulkName len years count -> withinNameCodes count $ issueMany count (fmap (formatNameCode . NCPrepaid) <$> issueNameCode cc len years CPSFree) + withinNameCodes $ issueNameCode cc IssuedInGroup len years CPSFree >>= reply . either (const issueFailedText) (("Code: " <>) . formatNameCode . NCPrepaid) -- Naming the code tells concurrent revokes apart; the command already made it public. GCRevoke code -> do outcome <- either id id <$> revokeWithTracker cc code @@ -286,11 +319,11 @@ runGroupCmd cc turns namesOn groupId = \case reply (formatNameCode (NCPrepaid code) <> ": " <> outcome) where reply = void . sendGroupText cc groupId "reply, any codes in it are lost" - withinNameCodes n issue = do + withinNameCodes issue = do since <- addUTCTime (negate nominalDay) <$> getCurrentTime - withDB' "nameCodesIssuedSince" cc (`nameCodesIssuedSince` since) >>= \case + withDB' "groupNameCodesIssuedSince" cc (`groupNameCodesIssuedSince` since) >>= \case Right issued - | issued + n <= maxNameCodesPerDay -> issue + | issued < maxNameCodesPerDay -> issue | otherwise -> reply $ "At most " <> tshow maxNameCodesPerDay <> " name codes are issued in a day, " <> tshow issued <> " so far. Issue more with //issue name in --run-cli." Left _ -> reply issueFailedText issueMany count issueOne = do @@ -301,7 +334,7 @@ runGroupCmd cc turns namesOn groupId = \case -- | Each registers at the sponsor allowance's and the gas key's cost, so a member's account taken over issues few. maxNameCodesPerDay :: Int -maxNameCodesPerDay = 100 +maxNameCodesPerDay = 10 namesOffText :: Text namesOffText = "Names are not enabled on this service, so name codes cannot be issued." diff --git a/apps/simplex-store-service/src/StoreService/Group/Command.hs b/apps/simplex-store-service/src/StoreService/Group/Command.hs index 7341811330..c26b4ffa16 100644 --- a/apps/simplex-store-service/src/StoreService/Group/Command.hs +++ b/apps/simplex-store-service/src/StoreService/Group/Command.hs @@ -45,7 +45,6 @@ data GroupCmd = GCIssue BadgeType Int Int | GCBulk BadgeType Int Int | GCIssueName Int Int - | GCBulkName Int Int Int | GCRevoke BadgeCode | GCRevokeName NamePrepaidCode deriving (Eq, Show) @@ -59,18 +58,9 @@ data CmdAction groupCmdAction :: GroupMemberRole -> Text -> CmdAction groupCmdAction role t = case A.parseOnly cmdActionP (encodeUtf8 (T.strip t)) of Right (tag, r) - | role >= cmdMinRole tag -> either ReplyText runFor r + | role >= cmdMinRole tag -> either ReplyText RunCmd r | Right _ <- r, Just refusal <- cmdRefusal tag -> ReplyText refusal _ -> IgnoreMsg - where - -- a name code spends the sponsor allowance and the gas key's ETH when it registers, so only admins issue them - runFor cmd - | nameCode cmd && role < GRAdmin = ReplyText "Only admins can issue name codes." - | otherwise = RunCmd cmd - nameCode = \case - GCIssueName {} -> True - GCBulkName {} -> True - _ -> False -- | Left is the usage reply for an advertised command whose arguments do not parse. cmdActionP :: A.Parser (CmdTag, Either Text GroupCmd) @@ -84,7 +74,7 @@ cmdActionP = A.choice (map cmdP [minBound .. maxBound]) cmdArgsP :: CmdTag -> A.Parser GroupCmd cmdArgsP = \case CTIssue -> uncurry GCIssueName <$> nameCodeP <|> GCIssue <$> badgeTypeP <*> optP monthsOpt <*> optP usesOpt - CTBulk -> uncurry GCBulkName <$> nameCodeP <*> countP <|> GCBulk <$> badgeTypeP <*> optP monthsOpt <*> countP + CTBulk -> GCBulk <$> badgeTypeP <*> optP monthsOpt <*> countP CTRevoke -> GCRevoke <$> codeP <|> GCRevokeName <$> prepaidCodeP where countP = A.space *> optValueP countOpt @@ -175,28 +165,31 @@ cmdLabel = \case -- The menu hints and the usage replies are built from the same kinds, options and bounds, so they cannot drift apart. cmdParams :: CmdTag -> Text cmdParams = \case - CTIssue -> kindParams <> " [" <> optArg usesOpt <> "]" - CTBulk -> kindParams <> " " <> optArg countOpt + CTIssue -> kindParams (nameArg : badgeKinds) [yearsOpt, monthsOpt] <> " [" <> optArg usesOpt <> "]" + CTBulk -> kindParams badgeKinds [monthsOpt] <> " " <> optArg countOpt CTRevoke -> "" where - kindParams = "<" <> T.intercalate "|" (nameArg : badgeKinds) <> "> [" <> optKey yearsOpt <> "|" <> optKey monthsOpt <> " ]" + kindParams kinds opts = "<" <> T.intercalate "|" kinds <> "> [" <> T.intercalate "|" (map optKey opts) <> " ]" cmdUsage :: CmdTag -> Text cmdUsage = \case - CTIssue -> kindUsage CTIssue ("[" <> optArg usesOpt <> "]") [] [usesOpt] ["name 6 years 5", "supporter months 12"] - CTBulk -> kindUsage CTBulk (optArg countOpt) [countOpt] [countOpt] ["name 6 count 100", "legend months 3 count 10"] + CTIssue -> + kindUsage + CTIssue + ("[" <> optVar lengthOpt <> "] [" <> optArg yearsOpt <> " | " <> optArg monthsOpt <> "] [" <> optArg usesOpt <> "]") + [(nameArg, [lengthOpt, yearsOpt]), (badgeKindsText, [monthsOpt, usesOpt])] + ["name 6 years 5", "supporter months 12"] + CTBulk -> kindUsage CTBulk ("[" <> optArg monthsOpt <> "] " <> optArg countOpt) [(badgeKindsText, [monthsOpt, countOpt])] ["legend months 3 count 10"] CTRevoke -> "Usage: " <> cmdText CTRevoke (cmdParams CTRevoke) + where + badgeKindsText = T.intercalate ", " badgeKinds --- | nameOpts and badgeOpts are the command's own options that apply to each kind. -kindUsage :: CmdTag -> Text -> [CmdOpt] -> [CmdOpt] -> [Text] -> Text -kindUsage tag cmdOptArg nameOpts badgeOpts examples = - T.intercalate - "\n" - [ "Usage: " <> cmdText tag (" [" <> optVar lengthOpt <> "] [" <> optArg yearsOpt <> " | " <> optArg monthsOpt <> "] " <> cmdOptArg), - nameArg <> ": " <> bounds (lengthOpt : yearsOpt : nameOpts), - T.intercalate ", " badgeKinds <> ": " <> bounds (monthsOpt : badgeOpts), - "e.g. " <> T.intercalate " · " (map (cmdText tag) examples) - ] +kindUsage :: CmdTag -> Text -> [(Text, [CmdOpt])] -> [Text] -> Text +kindUsage tag kindArgs kinds examples = + T.intercalate "\n" $ + ["Usage: " <> cmdText tag (" " <> kindArgs)] + <> map (\(kind, opts) -> kind <> ": " <> bounds opts) kinds + <> ["e.g. " <> T.intercalate " · " (map (cmdText tag) examples)] where bounds = T.intercalate ", " . map optBounds optBounds CmdOpt {optKey, optMin, optMax} = optKey <> " " <> tshow optMin <> "–" <> tshow optMax diff --git a/apps/simplex-store-service/src/StoreService/Names/Codes.hs b/apps/simplex-store-service/src/StoreService/Names/Codes.hs index 9ab577f840..a2cd47200e 100644 --- a/apps/simplex-store-service/src/StoreService/Names/Codes.hs +++ b/apps/simplex-store-service/src/StoreService/Names/Codes.hs @@ -37,7 +37,7 @@ import qualified Simplex.Messaging.Agent.Store.DB as DB import StoreService.Config (minNameYears) import StoreService.Group.Command (maxNameYears) import StoreService.Names.Phases (RegistrationId) -import StoreService.Names.Store (CodeHashTaken (..), NewPrepaidCode (..), PrepaidCode (..), getPrepaidCode, getPrepaidCodeById, insertPrepaidCode, releasePrepaidCode, reservePrepaidCode, revokePrepaidCode, spendLandedPrepaidCode) +import StoreService.Names.Store (CodeHashTaken (..), CodeIssuer, NewPrepaidCode (..), PrepaidCode (..), getPrepaidCode, getPrepaidCodeById, insertPrepaidCode, releasePrepaidCode, reservePrepaidCode, revokePrepaidCode, spendLandedPrepaidCode) import StoreService.Names.Turns (Turns, withCodeTurn) import StoreService.Store (RevokeResult (..)) @@ -145,14 +145,14 @@ afterWrite turns st codeId change write = do maybe run (\codeHash -> withCodeTurn turns codeHash run) codeHash_ -- | The code table keeps only the hash, so the caller must deliver the code. -issueNameCode :: ChatController -> Int -> Int -> BadgeCodePaymentStatus -> IO (Either String NamePrepaidCode) -issueNameCode cc len years paymentStatus +issueNameCode :: ChatController -> CodeIssuer -> Int -> Int -> BadgeCodePaymentStatus -> IO (Either String NamePrepaidCode) +issueNameCode cc issuer len years paymentStatus | len < minNameLength || len > maxNameLength = pure $ Left $ "name code length " <> show len <> " is outside " <> show minNameLength <> "-" <> show maxNameLength | years < minNameYears || years > maxNameYears = pure $ Left $ "name code years " <> show years <> " is outside " <> show minNameYears <> "-" <> show maxNameYears | otherwise = do code <- randomNamePrepaidCode (random cc) len now <- getCurrentTime - let new = NewPrepaidCode {npcCodeHash = namePrepaidCodeHash code, npcMinLength = len, npcYears = years, npcPaymentStatus = paymentStatus, npcExpiresAt = Nothing} + let new = NewPrepaidCode {npcCodeHash = namePrepaidCodeHash code, npcMinLength = len, npcYears = years, npcPaymentStatus = paymentStatus, npcExpiresAt = Nothing, npcIssuer = issuer} withDB' "issueNameCode" cc (\db -> insertPrepaidCode db new now) <&> \case Right (Right _) -> Right code Right (Left CodeHashTaken) -> Left "prepaid code hash taken" diff --git a/apps/simplex-store-service/src/StoreService/Names/Store.hs b/apps/simplex-store-service/src/StoreService/Names/Store.hs index 64e2569f7d..a25b6736ac 100644 --- a/apps/simplex-store-service/src/StoreService/Names/Store.hs +++ b/apps/simplex-store-service/src/StoreService/Names/Store.hs @@ -28,6 +28,7 @@ module StoreService.Names.Store CleanupKept (..), PrepaidCode (..), NewPrepaidCode (..), + CodeIssuer (..), CodeHashTaken (..), TxFor (..), TxStatus (..), @@ -78,7 +79,7 @@ module StoreService.Names.Store lateWait, insertPrepaidCode, getPrepaidCode, - nameCodesIssuedSince, + groupNameCodesIssuedSince, getPrepaidCodeById, reservePrepaidCode, releasePrepaidCode, @@ -401,9 +402,13 @@ data NewPrepaidCode = NewPrepaidCode npcMinLength :: Int, npcYears :: Int, npcPaymentStatus :: BadgeCodePaymentStatus, - npcExpiresAt :: Maybe UTCTime + npcExpiresAt :: Maybe UTCTime, + npcIssuer :: CodeIssuer } +data CodeIssuer = IssuedInGroup | IssuedInConsole + deriving (Eq, Show) + data CodeHashTaken = CodeHashTaken deriving (Eq, Show) @@ -1263,16 +1268,16 @@ setLateSupport db (InvoiceId invId) support = DB.execute db "UPDATE sx_badge_service_name_invoices SET late_support = COALESCE(late_support, ?) WHERE invoice_id = ?" (support, invId) insertPrepaidCode :: DB.Connection -> NewPrepaidCode -> UTCTime -> IO (Either CodeHashTaken Int64) -insertPrepaidCode db NewPrepaidCode {npcCodeHash, npcMinLength, npcYears, npcPaymentStatus, npcExpiresAt} now = do +insertPrepaidCode db NewPrepaidCode {npcCodeHash, npcMinLength, npcYears, npcPaymentStatus, npcExpiresAt, npcIssuer} now = do inserted <- executeChanging db [sql| - INSERT INTO sx_badge_service_name_prepaid_codes (code_hash, min_length, years, code_payment_status, expires_at, created_at) - VALUES (?,?,?,?,?,?) + INSERT INTO sx_badge_service_name_prepaid_codes (code_hash, min_length, years, code_payment_status, expires_at, created_at, issued_in_group) + VALUES (?,?,?,?,?,?,?) ON CONFLICT DO NOTHING |] - (Binary npcCodeHash, npcMinLength, npcYears, npcPaymentStatus, truncateToSecond <$> npcExpiresAt, truncateToSecond now) + (Binary npcCodeHash, npcMinLength, npcYears, npcPaymentStatus, truncateToSecond <$> npcExpiresAt, truncateToSecond now, BI (npcIssuer == IssuedInGroup)) if inserted > 0 then Right <$> insertedRowId db else pure $ Left CodeHashTaken prepaidCodeColumns :: Query @@ -1288,8 +1293,8 @@ toPrepaidCode :: PrepaidCodeRow -> PrepaidCode toPrepaidCode ((pcId, Binary pcCodeHash, pcMinLength, pcYears, pcPaymentStatus, reservedBy, pcReservedAt, pcSpentAt, pcRevokedAt, pcExpiresAt) :. Only pcCreatedAt) = PrepaidCode {pcId, pcCodeHash, pcMinLength, pcYears, pcPaymentStatus, pcReservedBy = RegistrationId <$> reservedBy, pcReservedAt, pcSpentAt, pcRevokedAt, pcExpiresAt, pcCreatedAt} -nameCodesIssuedSince :: DB.Connection -> UTCTime -> IO Int -nameCodesIssuedSince db since = maybe 0 fromOnly <$> maybeFirstRow id (DB.query db "SELECT COUNT(*) FROM sx_badge_service_name_prepaid_codes WHERE created_at >= ?" (Only since)) +groupNameCodesIssuedSince :: DB.Connection -> UTCTime -> IO Int +groupNameCodesIssuedSince db since = maybe 0 fromOnly <$> maybeFirstRow id (DB.query db "SELECT COUNT(*) FROM sx_badge_service_name_prepaid_codes WHERE created_at >= ? AND issued_in_group = 1" (Only since)) getPrepaidCode :: DB.Connection -> ByteString -> IO (Maybe PrepaidCode) getPrepaidCode db codeHash = diff --git a/apps/simplex-store-service/src/StoreService/Service.hs b/apps/simplex-store-service/src/StoreService/Service.hs index c20611981a..e9179c8d50 100644 --- a/apps/simplex-store-service/src/StoreService/Service.hs +++ b/apps/simplex-store-service/src/StoreService/Service.hs @@ -47,7 +47,7 @@ where import StoreService.Catalog (StoreProduct (..), defaultCatalog, storeProduct) import StoreService.Codes (issueFailedText, issueOneCode, singleUse) import StoreService.Config (AppleStoreConfig (..), BadgeIssuerKey (..), ChainConfig (..), NamesConfig (..), PlayStoreConfig (..), ServiceConfig (..), minNameYears, readServiceConfig) -import StoreService.Group (GroupEvent, ensureManagedGroup, fmtTime, groupEvent, hasTracker, refreshTracker, revokeWithTracker, runGroupLane) +import StoreService.Group (GroupEvent, ensureManagedGroup, fmtTime, groupEvent, hasTracker, joinerAcceptance, refreshTracker, revokeWithTracker, runGroupLane) import StoreService.Group.Command (badgeTypeP, codeP, maxMonths, maxNameYears, nameCodeP, prepaidCodeP, textTokenP) import StoreService.Names.Abi (hex) import StoreService.Names.Balances (RefundRecord (..), RefundRefusal (..), recordRefund) @@ -59,7 +59,7 @@ import StoreService.Names.Orders (ordersEnv) import StoreService.Names.Phases (RegistrationId (..)) import StoreService.Names.Records (runRecordEdits) import StoreService.Names.Sender (checkPendingSignatures, runSender) -import StoreService.Names.Store (Balance (..), BalanceHold (..), BalanceLink (..), LateCredit (..), RegistrationPayment (..), balanceByReference, balanceChain, methodText) +import StoreService.Names.Store (Balance (..), BalanceHold (..), BalanceLink (..), CodeIssuer (..), LateCredit (..), RegistrationPayment (..), balanceByReference, balanceChain, methodText) import StoreService.Names.Turns (Turns, newTurns) import StoreService.Names.View (addressText) import StoreService.Names.Worker (CloseRefusal (..), Closed (..), closeRegistration, newWorker, runCodeSweep, runWorkers) @@ -286,6 +286,7 @@ storeService opts serviceCfg baseCfg env = do waiters <- newWaiters -- The hooks' //revoke takes a prepaid code's turn, so the turns exist before the chat starts. turns <- newTurns + startedCC <- newEmptyTMVarIO consoleChain <- mapM newConsoleChain (serviceCfg >>= names) requestEnv <- configureStoreVerifier serviceCfg env let groupCfg = serviceCfg >>= \ServiceConfig {group} -> group @@ -306,9 +307,10 @@ storeService opts serviceCfg baseCfg env = do raceAny_ $ processQueuedRequests key trackerQ_ requestEnv : maybeToList groupLane_ <> lanes chatHooks = defaultChatHooks - { preStartHook = Just $ badgePreStartHook opts (serviceCfg >>= names), + { preStartHook = Just $ badgePreStartHook opts (serviceCfg >>= names) startedCC, postStartHook = Just $ badgePostStartHook opts env, - preCmdHook = Just $ badgeCmdHook turns consoleChain + preCmdHook = Just $ badgeCmdHook turns consoleChain, + acceptMember = Just $ joinerAcceptance startedCC } -- The reader runs from the start and must not block, since outputQ carries every chat event and a full queue stalls the core. simplexChatCore cfg {chatHooks} (mkChatOpts opts) $ \_ cc -> raceAny_ [readEvents cc, startLanes cc] @@ -388,6 +390,7 @@ storeServiceCLI opts serviceCfg = do env <- newServiceState >>= configureStoreVerifier serviceCfg -- This mode runs no names lanes, so only its own //revoke takes these turns. turns <- newTurns + startedCC <- newEmptyTMVarIO consoleChain <- mapM newConsoleChain (serviceCfg >>= names) let eventHook _cc = \case Right (CEvtServiceRequest u reqId sigKey reqData) -> do @@ -396,10 +399,11 @@ storeServiceCLI opts serviceCfg = do ev -> pure ev chatHooks = defaultChatHooks - { preStartHook = Just $ badgePreStartHook opts Nothing, + { preStartHook = Just $ badgePreStartHook opts Nothing startedCC, postStartHook = Just $ badgePostStartHook opts env, preCmdHook = Just $ badgeCmdHook turns consoleChain, - eventHook = Just eventHook + eventHook = Just eventHook, + acceptMember = Just $ joinerAcceptance startedCC } raceAny_ [ simplexChatCLI' cfg {chatHooks} (mkChatOpts opts) Nothing, @@ -422,7 +426,7 @@ runBadgeCmd turns chain_ cc cmd | Right issueOpts <- A.parseOnly issueCmdP cmd = issuedReply formatBadgeCode <$> issueBadgeCode cc issueOpts | Right (len, years, paymentStatus) <- A.parseOnly issueNameCmdP cmd = - issuedReply (formatNameCode . NCPrepaid) <$> issueNameCode cc len years paymentStatus + issuedReply (formatNameCode . NCPrepaid) <$> issueNameCode cc IssuedInConsole len years paymentStatus | Right code <- A.parseOnly revokeCmdP cmd = textReply <$> revokeWithTracker cc code | Right code <- A.parseOnly revokeNameCmdP cmd = textReply <$> revokeNameCode turns cc code | Right (ref, cents_) <- A.parseOnly refundCmdP cmd = textReply <$> maybe (showRefund cc ref) (refundBalanceCmd turns cc ref) cents_ @@ -665,10 +669,11 @@ routeLookahead = 60 survive :: T.Text -> IO () -> IO () survive what action = action `catchOwn'` \_ -> logError $ "store service: " <> what <> " failed; the next one will be handled" -badgePreStartHook :: StoreServiceOpts -> Maybe NamesConfig -> ChatController -> IO () -badgePreStartHook opts namesCfg_ ChatController {config, chatStore, random} = do +badgePreStartHook :: StoreServiceOpts -> Maybe NamesConfig -> TMVar ChatController -> ChatController -> IO () +badgePreStartHook opts namesCfg_ startedCC cc@ChatController {config, chatStore, random} = do runStoreServiceMigrations opts config chatStore forM_ namesCfg_ $ \NamesConfig {nChain} -> checkPendingSignatures random nChain chatStore >>= orExit + void $ atomically $ tryPutTMVar startedCC cc badgePostStartHook :: StoreServiceOpts -> ServiceState -> ChatController -> IO () badgePostStartHook StoreServiceOpts {noAddress, testing} env cc = do diff --git a/apps/simplex-store-service/src/StoreService/Store/Postgres/Migrations.hs b/apps/simplex-store-service/src/StoreService/Store/Postgres/Migrations.hs index fba57b332f..cb74441d28 100644 --- a/apps/simplex-store-service/src/StoreService/Store/Postgres/Migrations.hs +++ b/apps/simplex-store-service/src/StoreService/Store/Postgres/Migrations.hs @@ -294,6 +294,7 @@ CREATE TABLE @name_prepaid_codes( revoked_at TIMESTAMPTZ, expires_at TIMESTAMPTZ, created_at TIMESTAMPTZ NOT NULL, + issued_in_group SMALLINT NOT NULL DEFAULT 0, UNIQUE(code_hash) ); diff --git a/apps/simplex-store-service/src/StoreService/Store/SQLite/Migrations.hs b/apps/simplex-store-service/src/StoreService/Store/SQLite/Migrations.hs index 2f29cff85f..98d9f666c4 100644 --- a/apps/simplex-store-service/src/StoreService/Store/SQLite/Migrations.hs +++ b/apps/simplex-store-service/src/StoreService/Store/SQLite/Migrations.hs @@ -295,6 +295,7 @@ CREATE TABLE @name_prepaid_codes( revoked_at TEXT, expires_at TEXT, created_at TEXT NOT NULL, + issued_in_group INTEGER NOT NULL DEFAULT 0, UNIQUE(code_hash) ) STRICT; diff --git a/tests/Bots/StoreService/GroupIntegrationTests.hs b/tests/Bots/StoreService/GroupIntegrationTests.hs index cafb0d9ace..0eebeff396 100644 --- a/tests/Bots/StoreService/GroupIntegrationTests.hs +++ b/tests/Bots/StoreService/GroupIntegrationTests.hs @@ -11,13 +11,13 @@ module Bots.StoreService.GroupIntegrationTests (badgeGroupIntegrationTests, namesBootTests, namesCodesChatTests, namesRefundChatTests, namesWorkerBootTests) where import StoreService.Config (BadgeIssuerKey (..), ChainConfig (cController, cServiceKey), GroupConfig (..), NamesConfig (nChain), ServiceConfig (devAcceptUnverifiedStoreReceipts, names), readServiceConfig) -import StoreService.Group (GroupAction (..), GroupEvent (..), codeInTracker, ensureManagedGroup) +import StoreService.Group (GroupAction (..), GroupEvent (..), codeInTracker, ensureManagedGroup, joinerAcceptance) import StoreService.Group.Command (CmdAction (..), groupCmdAction, maxUses) import StoreService.Names.Abi (addressWord, selector, uint256) import StoreService.Names.Codes (issueNameCode, reserveCode, revokeNameCode) import StoreService.Names.Phases (RegistrationId (..)) import StoreService.Names.Chain (SignedTx (..), TxRequest (..), newChainClient, signTransaction) -import StoreService.Names.Store (NamePhase (PFailed), NameRegistration (nrFailedAt, nrPhase), NewTransaction (..), PrepaidCode (..), TxFor (..), VersionWrite (Written), getPrepaidCode, insertTransaction, spendLandedPrepaidCode) +import StoreService.Names.Store (CodeIssuer (..), NamePhase (PFailed), NameRegistration (nrFailedAt, nrPhase), NewTransaction (..), PrepaidCode (..), TxFor (..), VersionWrite (Written), getPrepaidCode, insertTransaction, spendLandedPrepaidCode) import StoreService.Names.Turns (newTurns, withCodeTurn) import StoreService.Options (StoreServiceOpts (..)) import StoreService.Orders (cancelOpenInvoice) @@ -40,7 +40,7 @@ import ChatTests.Utils import Control.Concurrent (threadDelay) import Control.Concurrent.MVar (MVar, newEmptyMVar, putMVar, takeMVar) import Control.Concurrent.Async (Async, async, cancel, poll, wait, waitCatchSTM) -import Control.Concurrent.STM (TQueue, atomically, newTQueueIO, orElse, readTMVar, readTQueue, readTVarIO, tryReadTMVar, writeTQueue) +import Control.Concurrent.STM (TQueue, atomically, newTMVarIO, newTQueueIO, orElse, readTMVar, readTQueue, readTVarIO, tryReadTMVar, writeTQueue) import Control.Exception (IOException, bracket, catch, finally, throwIO, try) import Control.Monad (forM_, guard, mfilter, replicateM_, unless, void, when) import qualified Data.Aeson as J @@ -68,14 +68,15 @@ import Simplex.Chat.Badges (BadgeType (..)) import Simplex.Chat.Badges.Code (NameCode (..), NamePrepaidCode, formatBadgeCode, formatNameCode, namePrepaidCodeHash, namePrepaidCodeLength, parseBadgeCode, parseNameCode, randomBadgeCode, randomNamePrepaidCode) import Simplex.Chat.Badges.Types (BadgeCodePaymentStatus (..)) import Simplex.Chat.StoreService -import Simplex.Chat.Controller (ChatCommand (APIDeleteChatItem), ChatConfig (..), ChatController (..), ChatResponse (CRChatItemsDeleted, CRCustomChatResponse)) +import Simplex.Chat.Controller (ChatCommand (APIDeleteChatItem, APIGroupInfo), ChatConfig (..), ChatController (..), ChatResponse (CRChatItemsDeleted, CRCustomChatResponse, CRGroupCreated, CRGroupInfo)) import Simplex.Chat.Core (sendChatCmd, sendChatCmdStr) import Simplex.Chat.Messages (ChatRef (ChatRef), ChatType (CTGroup)) import Simplex.Chat.Messages.CIContent (CIDeleteMode (CIDMInternal)) import Simplex.Chat.PaymentService.Types (InvoiceId (..)) -import Simplex.Chat.Types (AgentInvId (..)) +import Simplex.Chat.Store.Profiles (GroupLinkInfo (..)) +import Simplex.Chat.Types (AgentInvId (..), GroupInfo, GroupMemberAdmission (..), MemberCriteria (..)) import Simplex.Chat.Types.Preferences (ChatBotCommand (..), FullDeleteGroupPreference (..), GroupFeatureEnabled (..), GroupFeatureI (..), GroupPreferences (..), HistoryGroupPreference (..), SGroupFeature (..), commands_, emptyGroupPrefs, getGroupPreference) -import Simplex.Chat.Types.Shared (GroupMemberRole (..)) +import Simplex.Chat.Types.Shared (GroupAcceptance (..), GroupMemberRole (..)) import Simplex.Messaging.Agent (disposeAgentClient) import Simplex.Messaging.Agent.Protocol (AConnShortLink (..), ConnShortLink (..), ContactConnType (..)) import Simplex.Messaging.Agent.Store.Common (withTransaction) @@ -102,7 +103,13 @@ badgeGroupIntegrationTests = do it "creates the group and a join link on first start; on restart reuses it, re-advertises commands and keeps other preferences" testGroupCreateReuse it "keeps the group's name and description when the configured ones change" testConfigNotApplied it "promotes the first joiner to owner and leaves a later joiner a member" testFirstJoinerPromoted - it "promotes nobody after the first promotion failed" testFailedPromotionNotRetried + it "requires member review once the group has an owner, again on start after it was cleared" testMemberReviewRequired + it "holds a removed member who rejoins through the link for review" testRejoinerReviewed + it "holds a joiner of the managed group for review once the owner was set up, whatever its profile says, and on a lookup failure, not one of another group" testJoinerAcceptance + it "requires member review again when an owner turns it off" testReviewRestored + it "requires member review again at the next join when the profile lost it" testReviewRepairedOnJoin + it "keeps a code from a member pending review, who a moderator accepts" testModeratorAdmitsPendingMember + it "promotes nobody after the first promotion failed, holding a later joiner for review" testFailedPromotionNotRetried it "promotes the earliest member on start when their join was never handled" testMissedOwnerPromotedOnStart it "promotes the earliest member, not a later joiner, when no owner was set up" testEarliestPromotedOnLaterJoin it "skips a member still joining when promoting on start" testJoiningMemberSkippedOnStart @@ -139,7 +146,7 @@ badgeGroupIntegrationTests = do it "publishes nothing when a code whose tracker was deleted is used up" testDeletedTrackerUsedUp it "revokes a code whose tracker an owner deleted for everyone without reposting it" testModeratedTrackerNotRepostedOnRevoke it "revokes a code on /revoke, answers a repeat as already revoked and an unknown code as no such code" testGroupRevoke - it "refuses /issue name and /bulk name while names are off, still revoking name codes" testGroupNameCodesNamesOff + it "refuses /issue name while names are off, still revoking name codes" testGroupNameCodesNamesOff it "retires the tracker message when a multi-use code is revoked" testRevokeRetiresTracker it "retires the tracker message when the service command revokes the code" testServiceRevokeRetiresTracker it "retires a tracker left behind when the code is revoked again" testGroupRevokeRepairsTracker @@ -412,8 +419,8 @@ testWorkerBoot ps = withFakeEthNode $ \node@FakeEthNode {fnUrl} -> do namesCodesChatTests :: SpecWith TestParams namesCodesChatTests = describe "StoreService names: prepaid codes" $ do - it "issues prepaid codes on /issue name and /bulk name, answering the usage to years below 2" testGroupNameCodes - it "issues at most 100 name codes a day in the group" testGroupNameCodesPerDay + it "issues a prepaid code on /issue name for 2 years by default and for the years given, answering the usage to /bulk name" testGroupNameCodes + it "issues at most 10 name codes a day in the group, not counting console codes" testGroupNameCodesPerDay it "issues prepaid codes on //issue name, checks them through the names router and revokes them on //revoke" testOperatorNameCodes it "revokes a prepaid code in the code's turn, after a reservation holding it" testRevokeTakesCodeTurn @@ -421,24 +428,24 @@ testGroupNameCodes :: HasCallStack => TestParams -> IO () testGroupNameCodes ps = do svc <- prepareGroupService ps runWithOwner svc {gsIniSections = "" : codesOnlyNames <> chainSection} $ \cc _ _ alice -> do - one <- prepaidIn . T.stripPrefix "Code: " =<< replyTo cc alice "/issue name 8 years 5" + one <- prepaidIn . T.stripPrefix "Code: " =<< replyTo cc alice "/issue name 8" namePrepaidCodeLength one `shouldBe` 8 - prepaidTerms cc one `shouldReturn` Just (8, 5, CPSFree) - bulk <- mapM (prepaidIn . Just) . T.lines =<< replyTo cc alice "/bulk name 6 count 3" - length bulk `shouldBe` 3 - forM_ bulk $ \code -> (namePrepaidCodeLength code,) <$> prepaidTerms cc code `shouldReturn` (6, Just (6, 2, CPSFree)) - replyTo cc alice "/issue name 8 years 1" `shouldReturn` issueUsage - countRows cc "sx_badge_service_name_prepaid_codes" `shouldReturn` 4 + prepaidTerms cc one `shouldReturn` Just (8, 2, CPSFree) + five <- prepaidIn . T.stripPrefix "Code: " =<< replyTo cc alice "/issue name 7 years 5" + prepaidTerms cc five `shouldReturn` Just (7, 5, CPSFree) + replyTo cc alice "/bulk name 6 count 3" `shouldReturn` bulkUsage + replyTo cc alice "/issue name 8 years 11" `shouldReturn` issueUsage + countRows cc "sx_badge_service_name_prepaid_codes" `shouldReturn` 2 testGroupNameCodesPerDay :: HasCallStack => TestParams -> IO () testGroupNameCodesPerDay ps = do svc <- prepareGroupService ps runWithOwner svc {gsIniSections = "" : codesOnlyNames <> chainSection} $ \cc _ _ alice -> do - length . T.lines <$> replyTo cc alice "/bulk name 6 count 99" `shouldReturn` 99 - replyTo cc alice "/bulk name 6 count 2" `shouldReturn` "At most 100 name codes are issued in a day, 99 so far. Issue more with //issue name in --run-cli." - T.isPrefixOf "Code: " <$> replyTo cc alice "/issue name 6" `shouldReturn` True - replyTo cc alice "/issue name 6" `shouldReturn` "At most 100 name codes are issued in a day, 100 so far. Issue more with //issue name in --run-cli." - countRows cc "sx_badge_service_name_prepaid_codes" `shouldReturn` 100 + replicateM_ 2 $ issueNameRaw cc "name 6" + replicateM_ 10 $ T.isPrefixOf "Code: " <$> replyTo cc alice "/issue name 6" `shouldReturn` True + replyTo cc alice "/issue name 6" `shouldReturn` "At most 10 name codes are issued in a day, 10 so far. Issue more with //issue name in --run-cli." + countRows cc "sx_badge_service_name_prepaid_codes" `shouldReturn` 12 + countRows cc "sx_badge_service_name_prepaid_codes WHERE issued_in_group = 1" `shouldReturn` 10 testOperatorNameCodes :: HasCallStack => TestParams -> IO () testOperatorNameCodes ps = do @@ -449,7 +456,7 @@ testOperatorNameCodes ps = do forM_ ["name", "name 5", "name 64", "name 07", "name 7 years 1", "name 7 years 11", "name 7 gratis", "name 6 unpaid"] $ \args -> (args,) <$> issueRaw cc args `shouldReturn` (args, Left badgeCmdUsage) forM_ [(5, 2), (64, 2), (7, 1), (7, 11)] $ \(len, years) -> - ((len, years),) . isLeft <$> issueNameCode cc len years CPSFree `shouldReturn` ((len, years), True) + ((len, years),) . isLeft <$> issueNameCode cc IssuedInConsole len years CPSFree `shouldReturn` ((len, years), True) countRows cc "sx_badge_service_name_prepaid_codes" `shouldReturn` 0 paid <- issueNameRaw cc "name 7 years 3 paid" prepaidTerms cc paid `shouldReturn` Just (7, 3, CPSPaid) @@ -787,7 +794,7 @@ withOwnerJoined ps cc env gid action = withNewTestChat ps "alice" aliceProfile $ \alice -> do joinGroup cc alice waitMemberRole cc gid "alice" "owner" - drainUntil alice ["#" <> groupName <> ": " <> botName <> " changed your role from member to owner"] + drainUntil alice ["#" <> groupName <> ": " <> botName <> " changed your role from member to owner", "changed member admission rules"] joined <- lastMessageId cc r <- action alice settleServingLane cc env gid @@ -796,11 +803,12 @@ withOwnerJoined ps cc env gid action = pure r -- Bob joins after the first member and is connected to her. -withMemberJoined :: HasCallStack => TestParams -> ChatController -> ServiceState -> Int64 -> TestCC -> (TestCC -> IO a) -> IO a -withMemberJoined ps cc env gid alice action = +withMemberJoined :: HasCallStack => TestParams -> ChatController -> ServiceState -> Int64 -> TestCC -> IO () -> (TestCC -> IO a) -> IO a +withMemberJoined ps cc env gid alice admit action = withNewTestChat ps "bob" bobProfile $ \bob -> do joinGroup cc bob drainUntil alice ["#" <> groupName <> ": new member bob is connected"] + admit joined <- lastMessageId cc r <- action bob settleServingLane cc env gid @@ -811,6 +819,13 @@ withMemberJoined ps cc env gid alice action = joinGroup :: HasCallStack => ChatController -> TestCC -> IO () joinGroup cc member = groupLink cc >>= send member . ("/c " <>) +admitMember :: HasCallStack => ChatController -> Int64 -> TestCC -> Text -> IO () +admitMember cc gid moderator name = do + memberStatus cc gid name `shouldReturn` Just "pending_review" + send moderator ("/accept member #" <> groupName <> " " <> T.unpack name) + drainUntil moderator ["#" <> groupName <> ": " <> T.unpack name <> " accepted"] + pollUntilTrue $ (`elem` [Just "connected", Just "complete"]) <$> memberStatus cc gid name + sendGroupCmd :: TestCC -> String -> IO () sendGroupCmd member cmd = send member ("#" <> groupName <> " " <> cmd) @@ -843,8 +858,10 @@ testGroupCreateReuse ps = do profileFullNameShortDescr cc gid `shouldReturn` ("", Nothing) groupFeaturePreference cc gid SGFFullDelete `shouldReturn` FullDeleteGroupPreference {enable = FEOn, role = Nothing} profileUpdatedAt cc gid - -- The lane drains only after the group is resolved, so the promotion proves the start-up sync has run. - runWithOwner svc $ \cc _ gid _ -> profileUpdatedAt cc gid `shouldReturn` advertisedAt + -- The lane drains only after the group is resolved, so a handled command proves the start-up sync has run. + runGroupService svc $ \cc env gid -> do + awaitLane cc env + profileUpdatedAt cc gid `shouldReturn` advertisedAt testConfigNotApplied :: HasCallStack => TestParams -> IO () testConfigNotApplied ps = do @@ -874,10 +891,12 @@ testEarliestPromotedOnLaterJoin ps = do runWithOwner svc $ \cc env gid alice -> do setMemberRole cc gid "alice" "member" executeSql cc "UPDATE sx_badge_service_group SET owner_bootstrapped = 0" - withMemberJoined ps cc env gid alice $ \bob -> do + clearMemberAdmission cc gid + withMemberJoined ps cc env gid alice (pure ()) $ \bob -> do waitMemberRole cc gid "alice" "owner" awaitLaneOnConsoles cc env [alice, bob] memberRoles cc gid `shouldReturn` [("alice", "owner"), ("bob", "member")] + profileMemberAdmission cc gid `shouldReturn` Just reviewAll testJoiningMemberSkippedOnStart :: HasCallStack => TestParams -> IO () testJoiningMemberSkippedOnStart ps = do @@ -989,7 +1008,7 @@ memberStatus cc gid name = withTwoMembersOwnerUnset :: HasCallStack => GroupSvc -> (ChatController -> Int64 -> TestCC -> TestCC -> IO ()) -> IO () withTwoMembersOwnerUnset svc@GroupSvc {gsPs = ps} action = runWithOwner svc $ \cc env gid alice -> - withMemberJoined ps cc env gid alice $ \bob -> do + withMemberJoined ps cc env gid alice (admitMember cc gid alice "bob") $ \bob -> do -- The lane must handle bob's join before the flag is cleared, or it would act on the cleared flag in this run. awaitLaneOnConsoles cc env [alice, bob] action cc gid alice bob @@ -999,7 +1018,7 @@ testFirstJoinerPromoted :: HasCallStack => TestParams -> IO () testFirstJoinerPromoted ps = do svc <- prepareGroupService ps runWithOwner svc $ \cc env gid alice -> - withMemberJoined ps cc env gid alice $ \bob -> do + withMemberJoined ps cc env gid alice (admitMember cc gid alice "bob") $ \bob -> do waitMemberRole cc gid "bob" "member" -- The bot queues bob's join before it introduces him, so alice's code proves the join was handled. sendGroupCmd alice "/issue supporter" @@ -1009,6 +1028,87 @@ testFirstJoinerPromoted ps = do drainUntil alice [codeReply] drainUntil bob [codeReply, "#" <> groupName <> ": member alice"] +testMemberReviewRequired :: HasCallStack => TestParams -> IO () +testMemberReviewRequired ps = do + svc <- prepareGroupService ps + runGroupService svc $ \cc env gid -> do + awaitLane cc env + profileMemberAdmission cc gid `shouldReturn` Nothing + runWithOwner svc $ \cc _ gid _ -> do + profileMemberAdmission cc gid `shouldReturn` Just reviewAll + clearMemberAdmission cc gid + runGroupService svc $ \cc _ gid -> pollUntilTrue $ (== Just reviewAll) <$> profileMemberAdmission cc gid + +testRejoinerReviewed :: HasCallStack => TestParams -> IO () +testRejoinerReviewed ps = do + svc <- prepareGroupService ps + runWithOwner svc $ \cc env gid alice -> + withMemberJoined ps cc env gid alice (admitMember cc gid alice "bob") $ \bob -> do + send alice ("/rm #" <> groupName <> " bob") + drainUntil bob ["#" <> groupName <> ": alice removed you from the group"] + pollUntilTrue $ (== Just "removed") <$> memberStatus cc gid "bob" + joinGroup cc bob + drainUntil bob ["connecting to group moderators for admission to group", "(Alice) is connected"] + drainUntil alice ["is connected and pending review"] + memberStatus cc gid "bob_1" `shouldReturn` Just "pending_review" + +testJoinerAcceptance :: HasCallStack => TestParams -> IO () +testJoinerAcceptance ps = do + svc <- prepareGroupService ps + runGroupService svc $ \cc _ gid -> do + startedCC <- newTMVarIO cc + managed <- groupInfoOf cc gid + other <- + sendChatCmdStr cc "/g othergroup" >>= \case + Right (CRGroupCreated _ g) -> pure g + r -> error $ "group not created: " <> show r + let acceptance g = joinerAcceptance startedCC g GroupLinkInfo {groupId = gid, memberRole = GRMember} bobProfile + accepted = Right (GAAccepted, GRMember) + held = Right (GAPendingReview, GRMember) + mapM acceptance [managed, other] `shouldReturn` [accepted, accepted] + executeSql cc "UPDATE sx_badge_service_group SET owner_bootstrapped = 1" + profileMemberAdmission cc gid `shouldReturn` Nothing + mapM acceptance [managed, other] `shouldReturn` [held, accepted] + executeSql cc "ALTER TABLE sx_badge_service_group RENAME TO sx_badge_service_group_hidden" + (acceptance other `shouldReturn` held) `finally` executeSql cc "ALTER TABLE sx_badge_service_group_hidden RENAME TO sx_badge_service_group" + +testReviewRestored :: HasCallStack => TestParams -> IO () +testReviewRestored ps = do + svc <- prepareGroupService ps + runWithOwner svc $ \cc _ gid alice -> do + send alice ("/set admission review #" <> groupName <> " off") + drainUntil alice [botName <> " updated group #" <> groupName] + profileMemberAdmission cc gid `shouldReturn` Just reviewAll + +testReviewRepairedOnJoin :: HasCallStack => TestParams -> IO () +testReviewRepairedOnJoin ps = do + svc <- prepareGroupService ps + runWithOwner svc $ \cc env gid alice -> do + clearMemberAdmission cc gid + withMemberJoined ps cc env gid alice (admitMember cc gid alice "bob") $ \_ -> + pollUntilTrue $ (== Just reviewAll) <$> profileMemberAdmission cc gid + +testModeratorAdmitsPendingMember :: HasCallStack => TestParams -> IO () +testModeratorAdmitsPendingMember ps = do + svc <- prepareGroupService ps + runWithOwner svc $ \cc env gid alice -> + withMemberJoined ps cc env gid alice (admitMember cc gid alice "bob") $ \bob -> + withNewTestChat ps "cath" cathProfile $ \cath -> do + send alice ("/mr #" <> groupName <> " bob moderator") + waitMemberRole cc gid "bob" "moderator" + drainUntil bob ["#" <> groupName <> ": alice changed your role from member to moderator"] + joinGroup cc cath + drainUntil bob ["#" <> groupName <> ": new member cath is connected and pending review"] + code <- extractCode <$> replyTo cc alice "/issue supporter" + awaitOnConsole bob ("Code: " <> code) + receivedItemsWithCode cath code `shouldReturn` 0 + admitMember cc gid bob "cath" + pollUntilTrue $ (`elem` [Just "connected", Just ("complete" :: Text)]) <$> queryFirst (chatController cath) "SELECT member_status FROM group_members WHERE member_category = 'user'" + receivedItemsWithCode cath code `shouldReturn` 0 + drainConsole cath + where + receivedItemsWithCode member code = countRows (chatController member) ("chat_items WHERE item_sent = 0 AND item_text LIKE '%" <> T.unpack code <> "%'") + testFailedPromotionNotRetried :: HasCallStack => TestParams -> IO () testFailedPromotionNotRetried ps = do svc <- prepareGroupService ps @@ -1020,7 +1120,13 @@ testFailedPromotionNotRetried ps = do pollUntilTrue $ mgOwnerBootstrapped <$> storedGroup cc awaitLaneOnConsoles cc env [alice] setBotRole cc "owner" - withMemberJoined ps cc env gid alice $ \bob -> awaitLaneOnConsoles cc env [alice, bob] + withNewTestChat ps "bob" bobProfile $ \bob -> do + joinGroup cc bob + drainUntil bob ["connecting to group moderators for admission to group"] + pollUntilTrue $ (== Just (1 :: Int)) <$> queryFirst cc ("SELECT members_require_attention FROM groups WHERE group_id = " <> show gid) + memberStatus cc gid "bob" `shouldReturn` Just "pending_review" + awaitLaneOnConsoles cc env [alice] + drainConsole bob memberRoles cc gid `shouldReturn` [("alice", "member"), ("bob", "member")] drainConsole alice @@ -1030,16 +1136,15 @@ testRoleGatedIssue ps = do runWithOwner svc $ \cc _ gid alice -> withNewTestChat ps "bob" bobProfile $ \bob -> withNewTestChat ps "cath" cathProfile $ \cath -> do - let announced name = "#" <> groupName <> ": " <> botName <> " added " <> name - preMember name = "#" <> groupName <> ": member " <> name + let preMember name = "#" <> groupName <> ": member " <> name newMember name = "#" <> groupName <> ": new member " <> name <> " is connected" usageReply = "#" <> groupName <> " " <> botName <> "> Usage:" joinGroup cc bob - waitMemberRole cc gid "bob" "member" - -- cath joins only after alice sees bob announced, or cath's console shows a different line. - drainUntil alice [announced "bob"] + drainUntil alice [newMember "bob"] + admitMember cc gid alice "bob" joinGroup cc cath - waitMemberRole cc gid "cath" "member" + drainUntil alice [newMember "cath"] + admitMember cc gid alice "cath" drainUntil cath [preMember "alice", preMember "bob"] drainConsole cath sendGroupCmd alice "/issue supporter" @@ -1056,7 +1161,7 @@ testRoleGatedIssue ps = do sendGroupCmd alice "/issue supporter uses 0" waitStoredItem cc issueUsage codeCount cc `shouldReturn` 2 - drainUntil alice [usageReply, newMember "bob", newMember "cath"] + drainUntil alice [usageReply] drainUntil bob [usageReply, preMember "alice", newMember "cath"] drainUntil cath [usageReply] drainConsole bob @@ -1066,7 +1171,7 @@ testBlockedMemberCommandIgnored :: HasCallStack => TestParams -> IO () testBlockedMemberCommandIgnored ps = do svc <- prepareGroupService ps runWithOwner svc $ \cc env gid alice -> - withMemberJoined ps cc env gid alice $ \bob -> do + withMemberJoined ps cc env gid alice (admitMember cc gid alice "bob") $ \bob -> do waitMemberRole cc gid "bob" "member" -- bob is made moderator so that only the block can refuse his command. send alice ("/mr #" <> groupName <> " bob moderator") @@ -1396,7 +1501,6 @@ testGroupNameCodesNamesOff :: HasCallStack => TestParams -> IO () testGroupNameCodesNamesOff ps = withGroupOwner ps $ \_ cc _ alice -> do replyTo cc alice "/issue name 8" `shouldReturn` namesOffReply - replyTo cc alice "/bulk name 6 count 2" `shouldReturn` namesOffReply countRows cc "sx_badge_service_name_prepaid_codes" `shouldReturn` 0 unissued <- formatNameCode . NCPrepaid <$> randomNamePrepaidCode (random cc) 8 revokeInGroup cc alice unissued "No such code." @@ -1695,11 +1799,14 @@ singleRow sql = \case issueUsage :: Text issueUsage = "Usage: /issue [] [years | months ] [uses ]\nname : length 6–63, years 2–10\nsupporter, legend, investor: months 1–255, uses 1–1000\ne.g. /issue name 6 years 5 · /issue supporter months 12" +bulkUsage :: Text +bulkUsage = "Usage: /bulk [months ] count \nsupporter, legend, investor: months 1–255, count 1–100\ne.g. /bulk legend months 3 count 10" + -- The list is spelled out rather than taken from the service, so a change there fails here. expectedCommands :: [ChatBotCommand] expectedCommands = [ CBCCommand "issue" "Generate a code" (Just "|supporter|legend|investor> [years|months ] [uses ]"), - CBCCommand "bulk" "Generate many codes" (Just "|supporter|legend|investor> [years|months ] count "), + CBCCommand "bulk" "Generate many codes" (Just " [months ] count "), CBCCommand "revoke" "Revoke a code" (Just "") ] @@ -1725,6 +1832,24 @@ profileUpdatedAt :: HasCallStack => ChatController -> Int64 -> IO UTCTime profileUpdatedAt cc gid = queryOne cc "no group profile to read an update time from" (groupProfileSql "p.updated_at" gid) +profileMemberAdmission :: HasCallStack => ChatController -> Int64 -> IO (Maybe GroupMemberAdmission) +profileMemberAdmission cc gid = + queryOne cc "no group profile to read member admission from" (groupProfileSql "p.member_admission" gid) + +groupInfoOf :: HasCallStack => ChatController -> Int64 -> IO GroupInfo +groupInfoOf cc gid = + sendChatCmd cc (APIGroupInfo gid) >>= \case + Right (CRGroupInfo _ g) -> pure g + r -> error $ "group info failed: " <> show r + +reviewAll :: GroupMemberAdmission +reviewAll = GroupMemberAdmission {review = Just MCAll} + +clearMemberAdmission :: ChatController -> Int64 -> IO () +clearMemberAdmission cc gid = + withTransaction (chatStore cc) $ \db -> + DB.execute db "UPDATE group_profiles SET member_admission = NULL WHERE group_profile_id IN (SELECT group_profile_id FROM groups WHERE group_id = ?)" (Only gid) + groupFeaturePreference :: HasCallStack => ChatController -> Int64 -> SGroupFeature f -> IO (GroupFeaturePreference f) groupFeaturePreference cc gid feature = getGroupPreference feature diff --git a/tests/Bots/StoreService/GroupTests.hs b/tests/Bots/StoreService/GroupTests.hs index bc2f02289a..8d3c1e5547 100644 --- a/tests/Bots/StoreService/GroupTests.hs +++ b/tests/Bots/StoreService/GroupTests.hs @@ -11,7 +11,7 @@ import Data.Time.Calendar (fromGregorian) import Data.Time.Clock (UTCTime (..), addUTCTime) import Simplex.Chat.Badges (BadgeType (..)) import Simplex.Chat.Badges.Code (BadgeCode, badgeCodeText, randomBadgeCode) -import Simplex.Chat.Controller (ChatCommand (DeleteGroup, MemberRole)) +import Simplex.Chat.Controller (ChatCommand (AcceptMember, DeleteGroup, MemberRole)) import Simplex.Chat.Library.Commands (parseChatCommand) import Simplex.Chat.Types (GroupProfile (..)) import Simplex.Chat.Types.Shared (GroupMemberRole (..)) @@ -23,7 +23,7 @@ badgeGroupTests :: Spec badgeGroupTests = describe "badge group" $ do let p = groupCmdAction GROwner issueUsage = ReplyText "Usage: /issue [] [years | months ] [uses ]\nname : length 6–63, years 2–10\nsupporter, legend, investor: months 1–255, uses 1–1000\ne.g. /issue name 6 years 5 · /issue supporter months 12" - bulkUsage = ReplyText "Usage: /bulk [] [years | months ] count \nname : length 6–63, years 2–10, count 1–100\nsupporter, legend, investor: months 1–255, count 1–100\ne.g. /bulk name 6 count 100 · /bulk legend months 3 count 10" + bulkUsage = ReplyText "Usage: /bulk [months ] count \nsupporter, legend, investor: months 1–255, count 1–100\ne.g. /bulk legend months 3 count 10" revokeUsage = ReplyText "Usage: /revoke " it "issue defaults" $ p "/issue supporter" `shouldBe` RunCmd (GCIssue BTSupporter 1 1) it "issue with months and uses" $ p "/issue legend months 6 uses 50" `shouldBe` RunCmd (GCIssue BTLegend 6 50) @@ -83,16 +83,16 @@ badgeGroupTests = describe "badge group" $ do (GROwner, (True, True, True)) ] describe "log hints give commands --run-cli parses for a group name with a space" $ do - let hintCommand = fst . T.breakOn " in --run-cli" . snd . T.breakOnEnd " with " - parsedHint hint = let cmd = hintCommand hint in (cmd, parseChatCommand (encodeUtf8 cmd)) + let hintCommands = map (fst . T.breakOn " in --run-cli") . drop 1 . T.splitOn " with " + parsedHint hint = map (\cmd -> (cmd, parseChatCommand (encodeUtf8 cmd))) (hintCommands hint) it "owner" $ case parsedHint (T.replace "" "alice" $ noOwnerHint "SimpleX Badges_1") of - (_, Right (MemberRole g m GROwner)) -> (g, m) `shouldBe` ("SimpleX Badges_1", "alice") - (cmd, r) -> expectationFailure $ "owner command " <> show cmd <> " parsed as " <> show r + [(_, Right (MemberRole g m GROwner)), (_, Right (AcceptMember g' m' GROwner))] -> [(g, m), (g', m')] `shouldBe` replicate 2 ("SimpleX Badges_1", "alice") + cmds -> expectationFailure $ "owner commands parsed as " <> show cmds it "orphan group" $ case parsedHint (orphanHint "SimpleX Badges_1") of - (_, Right (DeleteGroup g)) -> g `shouldBe` "SimpleX Badges_1" - (cmd, r) -> expectationFailure $ "delete command " <> show cmd <> " parsed as " <> show r + [(_, Right (DeleteGroup g))] -> g `shouldBe` "SimpleX Badges_1" + cmds -> expectationFailure $ "delete command parsed as " <> show cmds describe "classifying a group message" $ do it "answers an advertised command that does not parse" $ do map p @@ -113,11 +113,9 @@ badgeGroupTests = describe "badge group" $ do `shouldBe` [IgnoreMsg, bulkUsage, bulkUsage] map (`groupCmdAction` "/revoke not-a-badge-code") [GRMember, GRModerator, GRAdmin] `shouldBe` [IgnoreMsg, IgnoreMsg, revokeUsage] - it "runs a name code command only for an admin or owner, refusing a moderator" $ do + it "runs a name code command for a moderator and above, saying nothing to a member" $ do map (`groupCmdAction` "/issue name 6") [GRMember, GRModerator, GRAdmin, GROwner] - `shouldBe` [IgnoreMsg, ReplyText "Only admins can issue name codes.", RunCmd (GCIssueName 6 2), RunCmd (GCIssueName 6 2)] - map (`groupCmdAction` "/bulk name 6 count 2") [GRModerator, GRAdmin] - `shouldBe` [ReplyText "Only admins can issue name codes.", RunCmd (GCBulkName 6 2 2)] + `shouldBe` [IgnoreMsg, RunCmd (GCIssueName 6 2), RunCmd (GCIssueName 6 2), RunCmd (GCIssueName 6 2)] it "accepts a command padded with whitespace" $ do p " /issue supporter " `shouldBe` RunCmd (GCIssue BTSupporter 1 1) p "\t/bulk supporter count 1" `shouldBe` RunCmd (GCBulk BTSupporter 1 1) diff --git a/tests/Bots/StoreService/NamesCodesTests.hs b/tests/Bots/StoreService/NamesCodesTests.hs index c076f69c72..c61f5890ef 100644 --- a/tests/Bots/StoreService/NamesCodesTests.hs +++ b/tests/Bots/StoreService/NamesCodesTests.hs @@ -56,7 +56,7 @@ namesCodesTests = describe "StoreService names: prepaid codes" $ do it "lets one of two concurrent reservations of a code win, the other answered code_reserved" testConcurrentReserve it "releases and spends a code only after the write, and only for its holder" testReleaseSpend describe "group commands" $ do - it "parses /issue name and /bulk name with the length, years and count" testGroupNameCommands + it "parses /issue name with the length only, answering the usage to years and to /bulk name" testGroupNameCommands it "answers the usage to a bad length, years or count, or a badge option" testGroupNameCommandsRefused it "parses /revoke of a prepaid code, telling a moderator it was not revoked" testGroupRevokeNameCommand @@ -296,11 +296,10 @@ testGroupNameCommands :: IO () testGroupNameCommands = do let p = groupCmdAction GRAdmin p "/issue name 8" `shouldBe` RunCmd (GCIssueName 8 2) - p "/issue name 6 years 5" `shouldBe` RunCmd (GCIssueName 6 5) + p "/issue name 63" `shouldBe` RunCmd (GCIssueName 63 2) + p "/issue name 6 years 2" `shouldBe` RunCmd (GCIssueName 6 2) p "/issue name 63 years 10" `shouldBe` RunCmd (GCIssueName 63 10) - p "/bulk name 7 count 3" `shouldBe` RunCmd (GCBulkName 7 2 3) - p "/bulk name 12 years 3 count 100" `shouldBe` RunCmd (GCBulkName 12 3 100) - p "/bulk name 6 count 100" `shouldBe` RunCmd (GCBulkName 6 2 100) + map p ["/bulk name 7 count 3", "/bulk name 12 years 2 count 100", "/bulk name 6 count 1"] `shouldBe` replicate 3 bulkUsage groupCmdAction GRMember "/issue name 8" `shouldBe` IgnoreMsg testGroupRevokeNameCommand :: IO () @@ -314,9 +313,11 @@ testGroupRevokeNameCommand = do testGroupNameCommandsRefused :: IO () testGroupNameCommandsRefused = do let p = groupCmdAction GRModerator - issueUsage = ReplyText "Usage: /issue [] [years | months ] [uses ]\nname : length 6–63, years 2–10\nsupporter, legend, investor: months 1–255, uses 1–1000\ne.g. /issue name 6 years 5 · /issue supporter months 12" - bulkUsage = ReplyText "Usage: /bulk [] [years | months ] count \nname : length 6–63, years 2–10, count 1–100\nsupporter, legend, investor: months 1–255, count 1–100\ne.g. /bulk name 6 count 100 · /bulk legend months 3 count 10" forM_ ["name", "name 5", "name 64", "name 06", "name 8 years 1", "name 8 years 0", "name 8 years 11", "name 8 uses 2", "name 8 months 2", "name 8 paid", "name 8 years 18446744073709551618", "name 8 count 2", "name 8 count 1"] $ \args -> (args, p ("/issue " <> args)) `shouldBe` (args, issueUsage) forM_ ["name 8", "name 8 count 0", "name 8 count 101", "name 8 years 1 count 2", "name 5 count 2", "name 08 count 2", "name 8 months 2 count 2", "name 8 count 2 uses 2", "name 8 uses 2 count 2", "name 8 count 2 years 5", "name count 2"] $ \args -> (args, p ("/bulk " <> args)) `shouldBe` (args, bulkUsage) + +issueUsage, bulkUsage :: CmdAction +issueUsage = ReplyText "Usage: /issue [] [years | months ] [uses ]\nname : length 6–63, years 2–10\nsupporter, legend, investor: months 1–255, uses 1–1000\ne.g. /issue name 6 years 5 · /issue supporter months 12" +bulkUsage = ReplyText "Usage: /bulk [months ] count \nsupporter, legend, investor: months 1–255, count 1–100\ne.g. /bulk legend months 3 count 10" diff --git a/tests/Bots/StoreService/NamesOrdersTests.hs b/tests/Bots/StoreService/NamesOrdersTests.hs index aa44b78594..9b91240c37 100644 --- a/tests/Bots/StoreService/NamesOrdersTests.hs +++ b/tests/Bots/StoreService/NamesOrdersTests.hs @@ -933,7 +933,7 @@ testTermsCheckOrder = withOrders $ \ctx@Ctx {cNode = node} -> do issueCode :: Ctx -> Int -> Int -> (NewPrepaidCode -> NewPrepaidCode) -> IO (Text, PrepaidCode) issueCode Ctx {cStore, cRandom} len years change = do code <- randomNamePrepaidCode cRandom len - let new = change NewPrepaidCode {npcCodeHash = namePrepaidCodeHash code, npcMinLength = len, npcYears = years, npcPaymentStatus = CPSPaid, npcExpiresAt = Nothing} + let new = change NewPrepaidCode {npcCodeHash = namePrepaidCodeHash code, npcMinLength = len, npcYears = years, npcPaymentStatus = CPSPaid, npcExpiresAt = Nothing, npcIssuer = IssuedInConsole} stored <- withTransaction cStore $ \db -> insertPrepaidCode db new orderTime >>= \case Right codeId -> fromJust <$> getPrepaidCodeById db codeId diff --git a/tests/Bots/StoreService/NamesStoreTests.hs b/tests/Bots/StoreService/NamesStoreTests.hs index 32c0a59971..12a6db4b98 100644 --- a/tests/Bots/StoreService/NamesStoreTests.hs +++ b/tests/Bots/StoreService/NamesStoreTests.hs @@ -150,7 +150,7 @@ issue st n c = withTransaction st $ \db -> do fromJust <$> getPrepaidCodeById db codeId usable :: NewPrepaidCode -usable = NewPrepaidCode {npcCodeHash = "", npcMinLength = 8, npcYears = twoYears, npcPaymentStatus = CPSPaid, npcExpiresAt = Nothing} +usable = NewPrepaidCode {npcCodeHash = "", npcMinLength = 8, npcYears = twoYears, npcPaymentStatus = CPSPaid, npcExpiresAt = Nothing, npcIssuer = IssuedInConsole} placeCode :: DBStore -> Text -> Address -> OrderFields -> PrepaidCode -> IO (Either RegistrationRefused ()) placeCode st rid owner o code = withTransaction st $ \db -> createCodeRegistration db (newReg rid owner o) code t0