diff --git a/README.md b/README.md index 725deb3..77ae7ae 100644 --- a/README.md +++ b/README.md @@ -132,7 +132,8 @@ specifies it. that only the owner sees, once. The owner adds it to a team, and channel members add it to channels. Unless it is set to see every message, a bot gets commands (`/echo hi`), mentions of it, replies in its threads and its - direct messages. + direct messages. A bot may write to a person in its teams first, in a + direct conversation, for reminders and alerts. - A bot's commands show in the composer as people type `/`, and its messages may carry buttons: a press reaches the bot as a `CallbackQuery`, and its answer shows to the person who pressed. @@ -720,7 +721,9 @@ Secrets that should come from the environment: `services.updates` sets how long the update logs keep updates (`retention`, a week by default), and `services.bots` how many bots a person may own and how webhooks are delivered. Webhooks go only to public addresses unless -`services.bots.allow_private_webhooks` is set, for development. +`services.bots.allow_private_webhooks` is set, for development. A bot opens +up to `services.directory.max_bot_conversations_per_hour` new direct +conversations an hour, 30 by default. `services.previews` sets link previews: `enabled` (on by default), the fetch limits, and `allow_hosts` and `deny_hosts`, host names that cover @@ -812,10 +815,11 @@ The end-to-end suite in [test/e2e](test/e2e) drives a running stack through the web gateway: invites, real-time delivery, typing, attachments, voice messages and their channel setting, voice channels (with Pion peers that talk through the voice server), the client API the apps use, bots (commands, buttons, -catching up after a restart, the HTTP Bot API and token resets through -[pkg/gochat](pkg/gochat)), link previews (none for a private address; with -`GOCHAT_E2E_PREVIEW_URL` set to a public page, its card and picture), -private channel isolation, CSRF, TOTP with recovery codes, session +writing to a person first, catching up after a restart, the HTTP Bot API +and token resets through [pkg/gochat](pkg/gochat)), link previews (none for +a private address; with `GOCHAT_E2E_PREVIEW_URL` set to a public page, its +card and picture), private channel isolation, CSRF, TOTP with recovery +codes, session revocation, deactivation and storage quotas. It needs an existing admin account and team, or with `GOCHAT_E2E_BOOTSTRAP=1` and the server's token in `GOCHAT_E2E_SETUP_TOKEN` creates them on a fresh server: diff --git a/cmd/serve/default.yaml b/cmd/serve/default.yaml index 01f963a..9741b9b 100644 --- a/cmd/serve/default.yaml +++ b/cmd/serve/default.yaml @@ -116,6 +116,7 @@ services: invite_ttl: 168h max_invites: 100 max_teams_per_day: 5 + max_bot_conversations_per_hour: 30 channels: idle_timeout: 10m op_timeout: 10s diff --git a/docs/protocol.md b/docs/protocol.md index 9bba377..3786778 100644 --- a/docs/protocol.md +++ b/docs/protocol.md @@ -244,7 +244,8 @@ A bot connects as a person does, with its token in the hello, and receives **everything in its account's sequence**: a bot keeps one number and asks for nothing else. Its owner adds it to teams they may invite people to; then people add it to channels as they add anyone (a private channel's -admins only), and anyone in its teams may write to it directly. +admins only), and anyone in its teams may write to it directly. A bot may +also [write to a person first](#writing-to-a-person-first). ### What a bot receives @@ -269,7 +270,7 @@ what the bot had when it was added; add it again to change it. | `AuthService` | `Me` | | `UserService` | `GetUser`, `UpdateProfile` | | `TeamService` | `ListTeams`, `GetTeam`, `ListTeamMembers` | -| `ChannelService` | `ListChannels`, `GetChannel`, `ListChannelMembers`, `JoinChannel`, `LeaveChannel` | +| `ChannelService` | `ListChannels`, `GetChannel`, `ListChannelMembers`, `JoinChannel`, `LeaveChannel`, `OpenDirectChannel` | | `MessageService` | `PostMessage`, `EditMessage`, `DeleteMessage` (its own), `GetMessage`, `ListMessages`, `ListThread` (with `sees_all`), `SendTyping`, `React`, `RemoveLinkPreview` (its own) | | `UpdatesService` | `GetState`, `GetDifference` | | `BotService` | `SetCommands`, `GetCommands`, `AnswerCallback`, `SetWebhook`, `DeleteWebhook`, `GetWebhookInfo`, `ConfirmUpdates` | @@ -277,6 +278,27 @@ what the bot had when it was added; add it again to change it. Any other call answers code 12. +### Writing to a person first + +`ChannelService.OpenDirectChannel` with `team_id` and `user_id` opens the +bot's direct conversation with a person, for reminders, alerts and +notices; the bot then posts in it as in any channel. The person is an +active member of the team, as the bot is, or the call answers code 5, +which is also the answer for a deactivated or deleted account. A person +who blocked the bot refuses it with code 7, in the words of a refused +post. A bot opens no group conversation, and none with another bot +(code 3). + +A bot opens up to 30 new conversations an hour, as the server's +`services.directory.max_bot_conversations_per_hour` sets; past that the +call answers code 8. Opening a conversation that exists already, one the +bot opened or the person did, costs nothing and returns it. + +The person sees the conversation as any other direct one, with the bot's +Bot badge. They may mute it, block the bot, which stops its posts there, +or delete it for themselves. The bot receives every message of it, as of +all its direct conversations. + ### Commands `SetCommands` lists the commands a bot offers, up to 100, each a name of @@ -333,6 +355,7 @@ proto field names), and the answer is always JSON: | `setCommands` / `getCommands` | `BotService/SetCommands` / `GetCommands` | | `listTeams` | `TeamService/ListTeams` | | `listChannels` / `getChannel` | `ChannelService/ListChannels` / `GetChannel` | +| `openDirect` | `ChannelService/OpenDirectChannel` | | `setWebhook` / `deleteWebhook` / `getWebhookInfo` | `BotService/...` | Any call of the [table above](#what-bots-may-call) may also be made by its diff --git a/examples/echobot/main.go b/examples/echobot/main.go index 475cf44..b531621 100644 --- a/examples/echobot/main.go +++ b/examples/echobot/main.go @@ -1,5 +1,6 @@ // Command echobot is a small GoChat bot: /echo repeats what follows it under -// a Thanks button, and /ping answers pong. +// a Thanks button, /ping answers pong, and /remindme 10m stand up writes +// "stand up" to whoever asked in a direct message ten minutes later. // // GOCHAT_URL=https://chat.example.com GOCHAT_BOT_TOKEN=b_... go run ./examples/echobot // @@ -17,6 +18,7 @@ import ( "strconv" "strings" "syscall" + "time" pb "github.com/ssenerg/gochat/api/protobuf/gen/gochat/v1" "github.com/ssenerg/gochat/pkg/gochat" @@ -47,6 +49,7 @@ func run(ctx context.Context, url, token, state string) error { err = c.Call(ctx, "gochat.v1.BotService/SetCommands", &pb.SetCommandsRequest{Commands: []*pb.BotCommand{ {Name: "echo", Description: "Repeat what follows"}, {Name: "ping", Description: "Answer pong"}, + {Name: "remindme", Description: "Remind you in a direct message: /remindme 10m stand up"}, }}, &pb.SetCommandsResponse{}) if err != nil { return err @@ -75,7 +78,8 @@ func handle(ctx context.Context, c *gochat.Client, me *pb.User, u gochat.Update) log.Print("updates were lost while the bot was away") return nil } - if m := u.Event.GetMessagePosted().GetMessage(); m != nil && m.GetAuthor().GetId() != me.GetId() { + posted := u.Event.GetMessagePosted() + if m := posted.GetMessage(); m != nil && m.GetAuthor().GetId() != me.GetId() { name, args, ok := gochat.Command(m.Content, me.GetUsername()) if !ok { return nil @@ -92,6 +96,8 @@ func handle(ctx context.Context, c *gochat.Client, me *pb.User, u gochat.Update) }}}} case "ping": reply.Content = "pong" + case "remindme": + reply.Content = remind(ctx, c, posted.GetTeamId(), m.GetAuthor().GetId(), args) default: return nil } @@ -104,6 +110,36 @@ func handle(ctx context.Context, c *gochat.Client, me *pb.User, u gochat.Update) return nil } +// maxRemind bounds how far off a reminder is: the bot keeps reminders in +// memory, and a restart forgets them. +const maxRemind = 24 * time.Hour + +// remind writes text to userID in their direct conversation in teamID once +// the duration args start with is over, and answers what it set. +func remind(ctx context.Context, c *gochat.Client, teamID, userID, args string) string { + when, text, _ := strings.Cut(args, " ") + d, err := time.ParseDuration(when) + text = strings.TrimSpace(text) + if err != nil || d <= 0 || d > maxRemind || text == "" { + return "Say when, up to 24h, and what: /remindme 10m stand up." + } + time.AfterFunc(d, func() { + if ctx.Err() != nil { + return + } + ctx, cancel := context.WithTimeout(ctx, 30*time.Second) + defer cancel() + dm, err := c.OpenDirect(ctx, teamID, userID) + if err == nil { + err = c.Call(ctx, "gochat.v1.MessageService/PostMessage", &pb.PostMessageRequest{ChannelId: dm.GetId(), Content: text}, &pb.PostMessageResponse{}) + } + if err != nil { + log.Printf("sending a reminder: %v", err) + } + }) + return "I'll remind you in a direct message in " + d.String() + "." +} + func load(path string) int64 { if path == "" { return 0 diff --git a/internal/repos/scylla/bots_test.go b/internal/repos/scylla/bots_test.go index 8505d4e..8e87551 100644 --- a/internal/repos/scylla/bots_test.go +++ b/internal/repos/scylla/bots_test.go @@ -10,6 +10,7 @@ import ( "github.com/ssenerg/gochat/internal/core/ids" "github.com/ssenerg/gochat/internal/core/models" "github.com/ssenerg/gochat/internal/core/secure" + ierrors "github.com/ssenerg/gochat/internal/errors" ) func TestBotsLive(t *testing.T) { @@ -88,3 +89,19 @@ func TestBotsLive(t *testing.T) { t.Fatal("the buttons stayed") } } + +func TestDirectKeyHolderLive(t *testing.T) { + r := liveRepository(t) + ctx := context.Background() + key := ConversationKey(ids.New(), ids.New().String()+":"+ids.New().String()) + if _, err := r.DirectKeyHolder(ctx, key); !ierrors.Is(err, ierrors.CodeNotFound) { + t.Fatalf("an unclaimed key: %v", err) + } + id := ids.New() + if got, err := r.ClaimDirectKey(ctx, key, id); err != nil || got != id { + t.Fatalf("claimed %v, %v", got, err) + } + if got, err := r.DirectKeyHolder(ctx, key); err != nil || got != id { + t.Fatalf("the key's holder %v, %v", got, err) + } +} diff --git a/internal/repos/scylla/channels.go b/internal/repos/scylla/channels.go index 871431c..169110a 100644 --- a/internal/repos/scylla/channels.go +++ b/internal/repos/scylla/channels.go @@ -105,6 +105,15 @@ func (r *Repository) ClaimDirectKey(ctx context.Context, key string, id gocql.UU return holder, nil } +// DirectKeyHolder returns the conversation holding key, or NotFound. +func (r *Repository) DirectKeyHolder(ctx context.Context, key string) (gocql.UUID, error) { + var id gocql.UUID + if err := r.query(ctx, "SELECT channel_id FROM direct_channels WHERE pair = ?", key).Scan(&id); err != nil { + return gocql.UUID{}, wrap(err) + } + return id, nil +} + // ConversationKey is the key of a team's conversation of the people a // conversation's name lists; those from before teams owned them have the // people alone. diff --git a/internal/server/drpc/middleware.go b/internal/server/drpc/middleware.go index a11904a..a04bd24 100644 --- a/internal/server/drpc/middleware.go +++ b/internal/server/drpc/middleware.go @@ -36,8 +36,8 @@ var streaming = map[string]bool{ } // botCalls are the calls open to bots: reading and writing in the -// channels they are in, about themselves, and their updates. Any other is -// refused, whatever its own checks. +// channels they are in, opening direct conversations, about themselves, +// and their updates. Any other is refused, whatever its own checks. var botCalls = map[string]bool{ "/gochat.v1.AuthService/Me": true, "/gochat.v1.UserService/GetUser": true, @@ -50,6 +50,7 @@ var botCalls = map[string]bool{ "/gochat.v1.ChannelService/ListChannelMembers": true, "/gochat.v1.ChannelService/JoinChannel": true, "/gochat.v1.ChannelService/LeaveChannel": true, + "/gochat.v1.ChannelService/OpenDirectChannel": true, "/gochat.v1.MessageService/PostMessage": true, "/gochat.v1.MessageService/EditMessage": true, "/gochat.v1.MessageService/DeleteMessage": true, diff --git a/internal/server/drpc/middleware_test.go b/internal/server/drpc/middleware_test.go index 6de27c9..74d3d08 100644 --- a/internal/server/drpc/middleware_test.go +++ b/internal/server/drpc/middleware_test.go @@ -87,13 +87,16 @@ func TestBotsMakeOnlyTheirCalls(t *testing.T) { ctx := drpcmetadata.AddPairs(context.Background(), map[string]string{MetaGatewayKey: key, MetaSession: token}) return guard.HandleRPC(&metaStream{ctx: ctx}, rpc) } - for _, rpc := range []string{"/gochat.v1.MessageService/PostMessage", "/gochat.v1.UpdatesService/GetDifference", "/gochat.v1.BotService/AnswerCallback"} { + for _, rpc := range []string{ + "/gochat.v1.MessageService/PostMessage", "/gochat.v1.UpdatesService/GetDifference", "/gochat.v1.BotService/AnswerCallback", + "/gochat.v1.ChannelService/OpenDirectChannel", + } { if err := call("bot", rpc); err != nil || !reached { t.Errorf("a bot's %s: %v", rpc, err) } } for _, rpc := range []string{ - "/gochat.v1.MessageService/SearchMessages", "/gochat.v1.ChannelService/OpenDirectChannel", "/gochat.v1.TeamService/CreateInvite", + "/gochat.v1.MessageService/SearchMessages", "/gochat.v1.ChannelService/OpenGroupChannel", "/gochat.v1.TeamService/CreateInvite", "/gochat.v1.AuthService/ChangePassword", "/gochat.v1.UpdatesService/GetChannelDifference", "/gochat.v1.BotService/CreateBot", "/gochat.v1.MessageService/PressButton", "/gochat.v1.SafetyService/BlockUser", "/gochat.v1.SafetyService/Report", "/gochat.v1.SafetyService/ListReports", "/gochat.v1.SafetyService/CountReports", diff --git a/internal/services/channels/bots_test.go b/internal/services/channels/bots_test.go index 60c077e..cb8eacf 100644 --- a/internal/services/channels/bots_test.go +++ b/internal/services/channels/bots_test.go @@ -295,3 +295,72 @@ func TestPushLeavesBotsOut(t *testing.T) { t.Fatalf("handed %+v", p) } } + +func TestABotWritesInTheConversationItOpened(t *testing.T) { + person, bot := ids.New(), ids.New() + // opened is the conversation, its members as the directory writes + // those of one a bot opens. + opened := func(list blockList) *channel { + a, _ := blockChannel(t, models.ChannelDirect, list, person, bot) + members := []*models.ChannelMember{ + {ChannelID: a.id, UserID: bot, TeamID: a.ch.TeamID, Bot: true, BotSeesAll: true}, + {ChannelID: a.id, UserID: person, TeamID: a.ch.TeamID}, + } + if err := a.loadBots(context.Background(), members); err != nil { + t.Fatal(err) + } + a.snapshot() + return a + } + a := opened(nil) + seen := &pushes{} + a.Updates = a.Engine.SpawnFunc(func(c *actor.Context) { + if p, ok := c.Message().(updates.Push); ok { + seen.mu.Lock() + seen.got = append(seen.got, p) + seen.mu.Unlock() + } + }, "updates") + pushed := make(chan push.Posted, 4) + a.Push = a.Engine.SpawnFunc(func(c *actor.Context) { + if p, ok := c.Message().(push.Posted); ok { + pushed <- p + } + }, "push") + post := func(p *models.Principal, text string) error { + _, err := a.post(Post{Target: Target{P: p, ChannelID: a.id}, Content: text}) + return err + } + asBot := &models.Principal{UserID: bot, Role: models.SystemRoleUser, Bot: true} + + // The bot's message is pushed to the person as any other. + if err := post(asBot, "stand up"); err != nil { + t.Fatal(err) + } + select { + case p := <-pushed: + if _, in := p.Members[person.String()]; !in || !p.Conversation { + t.Fatalf("pushed %+v", p) + } + if _, in := p.Members[bot.String()]; in { + t.Fatal("the bot was pushed its own message") + } + case <-time.After(5 * time.Second): + t.Fatal("the bot's message was not pushed") + } + // It gets every message of it, and reads its history. + if err := post(&models.Principal{UserID: person, Role: models.SystemRoleUser}, "done"); err != nil { + t.Fatal(err) + } + if got := posts(t, seen.take(), bot); len(got) != 1 || got[0] != "done" { + t.Fatalf("the bot got %q", got) + } + if err := a.readsHistory(asBot); err != nil { + t.Fatalf("the bot reads its conversation: %v", err) + } + // Once the person blocks it, its posts stop. + a = opened(blockList{person: {bot}}) + if err := post(asBot, "again"); ierrors.As(err).Message != ErrNotTaking.Message { + t.Fatalf("a bot the person blocked posted: %v", err) + } +} diff --git a/internal/services/directory/config.go b/internal/services/directory/config.go index 39fdfec..f9d515f 100644 --- a/internal/services/directory/config.go +++ b/internal/services/directory/config.go @@ -16,6 +16,9 @@ type Config struct { // MaxTeamsPerDay caps the teams one person creates a day; system admins // create any number. MaxTeamsPerDay int `mapstructure:"max_teams_per_day"` + // MaxBotConversationsPerHour caps the new direct conversations one bot + // opens an hour. + MaxBotConversationsPerHour int `mapstructure:"max_bot_conversations_per_hour"` } var _ config.Config = (*Config)(nil) @@ -36,5 +39,8 @@ func (c *Config) Validate() error { if c.MaxTeamsPerDay < 1 { return errors.New("max_teams_per_day must be at least 1") } + if c.MaxBotConversationsPerHour < 1 { + return errors.New("max_bot_conversations_per_hour must be at least 1") + } return nil } diff --git a/internal/services/directory/conversations_test.go b/internal/services/directory/conversations_test.go index 8c2adf6..4db45f8 100644 --- a/internal/services/directory/conversations_test.go +++ b/internal/services/directory/conversations_test.go @@ -17,6 +17,7 @@ import ( "github.com/ssenerg/gochat/internal/authz" "github.com/ssenerg/gochat/internal/core/ids" "github.com/ssenerg/gochat/internal/core/models" + "github.com/ssenerg/gochat/internal/core/window" ierrors "github.com/ssenerg/gochat/internal/errors" "github.com/ssenerg/gochat/internal/repos/scylla" "github.com/ssenerg/gochat/internal/services/blocks" @@ -49,6 +50,16 @@ func (f *teamsRepo) person(name string) gocql.UUID { return id } +func (f *teamsRepo) bot(name string) gocql.UUID { + id := f.person(name) + f.users[id].Bot = true + return id +} + +func botPrincipal(id gocql.UUID) *models.Principal { + return &models.Principal{UserID: id, Role: models.SystemRoleUser, Bot: true} +} + // join puts people in a team, as of when. func (f *teamsRepo) join(team gocql.UUID, when time.Time, people ...gocql.UUID) { if f.teams[team] == nil { @@ -111,6 +122,15 @@ func (f *teamsRepo) ClaimDirectKey(_ context.Context, key string, id gocql.UUID) return id, nil } +func (f *teamsRepo) DirectKeyHolder(_ context.Context, key string) (gocql.UUID, error) { + f.mu.Lock() + defer f.mu.Unlock() + if holder, ok := f.keys[key]; ok { + return holder, nil + } + return gocql.UUID{}, ierrors.NotFound("not found") +} + func (f *teamsRepo) CreateDirectChannel(ctx context.Context, key string, ch *models.Channel, members []*models.ChannelMember) (gocql.UUID, error) { id, _ := f.ClaimDirectKey(ctx, key, ch.ID) f.mu.Lock() @@ -218,6 +238,7 @@ func newTeamsWorker(t *testing.T, repo *teamsRepo) (*worker, *channelLog) { return &worker{Deps: &Deps{ Logger: zap.NewNop(), Cfg: &Config{OpTimeout: time.Second}, Repo: repo, Authz: az, Users: users.NewCache(repo, 100, time.Minute), Engine: engine, Hub: sink, Channels: chans, + botOpened: window.New(30, time.Hour), }}, log } @@ -554,3 +575,109 @@ func TestBlockedPeopleOpenNoConversation(t *testing.T) { t.Fatalf("carol put me, who blocked her, in a group: %v", err) } } + +func TestABotOpensADirectConversation(t *testing.T) { + repo := newTeamsRepo() + bot, other := repo.bot("helper_bot"), repo.bot("other_bot") + me, bob, carol, gone, deleted := repo.person("me"), repo.person("bob"), repo.person("carol"), repo.person("gone"), repo.person("deleted") + repo.users[gone].Deactivated = true + repo.users[deleted].Deactivated, repo.users[deleted].DeletedAt = true, time.Now() + acme, beta := ids.New(), ids.New() + repo.join(acme, time.Now(), bot, other, me, bob, gone, deleted) + repo.join(beta, time.Now(), me, carol) + w, _ := newTeamsWorker(t, repo) + // Bob blocked the bot. + w.Blocks = blocks.NewCache(blockList{bob: {bot}}, 10, 10, time.Minute) + ctx := context.Background() + open := func(team gocql.UUID, people ...gocql.UUID) (*pb.Channel, error) { + raw := make([]string, len(people)) + for i, id := range people { + raw[i] = id.String() + } + return w.openGroup(ctx, OpenGroup{P: botPrincipal(bot), TeamID: team.String(), UserIDs: raw}) + } + ch, err := w.openDirect(ctx, OpenDirect{P: botPrincipal(bot), TeamID: acme.String(), UserID: me.String()}) + if err != nil { + t.Fatal(err) + } + dm := repo.channels[idOf(ch.Id)] + if dm.Kind != models.ChannelDirect || dm.TeamID != acme || dm.CreatedBy != bot { + t.Fatalf("the bot opened %+v", dm) + } + // The bot gets every message of it; the person is a person. + for _, m := range repo.members[dm.ID] { + if want := m.UserID == bot; m.Bot != want || m.BotSeesAll != want { + t.Fatalf("member %s: bot %v, sees all %v", repo.users[m.UserID].Username, m.Bot, m.BotSeesAll) + } + } + if again, err := open(acme, me); err != nil || again.Id != ch.Id { + t.Fatalf("opening it again: %v, %v", again, err) + } + + for name, tc := range map[string]struct { + team gocql.UUID + people []gocql.UUID + code ierrors.Code + }{ + "a group": {acme, []gocql.UUID{me, bob}, ierrors.CodeInvalidArgument}, + "another bot": {acme, []gocql.UUID{other}, ierrors.CodeInvalidArgument}, + "someone outside the team": {acme, []gocql.UUID{carol}, ierrors.CodeNotFound}, + "a team the bot is not in": {beta, []gocql.UUID{carol}, ierrors.CodeNotFound}, + "a deactivated person": {acme, []gocql.UUID{gone}, ierrors.CodeNotFound}, + "a deleted person": {acme, []gocql.UUID{deleted}, ierrors.CodeNotFound}, + "someone who blocked the bot": {acme, []gocql.UUID{bob}, ierrors.CodePermissionDenied}, + } { + if _, err := open(tc.team, tc.people...); !ierrors.Is(err, tc.code) { + t.Errorf("%s: %v, want code %d", name, err, tc.code) + } + } + if _, err := open(acme, bob); ierrors.As(err).Message != channels.ErrNotTaking.Message { + t.Fatalf("someone who blocked the bot: %v", err) + } + // People still open theirs with a bot. + if _, err := w.openDirect(ctx, OpenDirect{P: principal(me), TeamID: acme.String(), UserID: other.String()}); err != nil { + t.Fatalf("a person's conversation with a bot: %v", err) + } +} + +func TestBotConversationsAreLimited(t *testing.T) { + repo := newTeamsRepo() + bot, other := repo.bot("helper_bot"), repo.bot("other_bot") + ann, bob, cat := repo.person("ann"), repo.person("bob"), repo.person("cat") + acme := ids.New() + repo.join(acme, time.Now(), bot, other, ann, bob, cat) + w, _ := newTeamsWorker(t, repo) + w.botOpened = window.New(2, time.Hour) + ctx := context.Background() + open := func(p *models.Principal, to gocql.UUID) error { + _, err := w.openDirect(ctx, OpenDirect{P: p, TeamID: acme.String(), UserID: to.String()}) + return err + } + for _, to := range []gocql.UUID{ann, bob} { + if err := open(botPrincipal(bot), to); err != nil { + t.Fatal(err) + } + } + if err := open(botPrincipal(bot), cat); !ierrors.Is(err, ierrors.CodeResourceExhausted) { + t.Fatalf("a new conversation past the limit: %v", err) + } + // One it has already is free, one the person opened too. + if err := open(botPrincipal(bot), ann); err != nil { + t.Fatalf("an existing conversation past the limit: %v", err) + } + if err := open(principal(cat), bot); err != nil { + t.Fatal(err) + } + if err := open(botPrincipal(bot), cat); err != nil { + t.Fatalf("a conversation the person opened: %v", err) + } + // Each bot has its own allowance, and people none. + if err := open(botPrincipal(other), ann); err != nil { + t.Fatalf("another bot: %v", err) + } + for _, to := range []gocql.UUID{bob, cat} { + if err := open(principal(ann), to); err != nil { + t.Fatalf("a person: %v", err) + } + } +} diff --git a/internal/services/directory/directory.go b/internal/services/directory/directory.go index 52d359e..0714706 100644 --- a/internal/services/directory/directory.go +++ b/internal/services/directory/directory.go @@ -74,6 +74,7 @@ type Repo interface { CreateChannel(ctx context.Context, ch *models.Channel, creator *models.ChannelMember) error CreateDirectChannel(ctx context.Context, key string, ch *models.Channel, members []*models.ChannelMember) (gocql.UUID, error) ClaimDirectKey(ctx context.Context, key string, id gocql.UUID) (gocql.UUID, error) + DirectKeyHolder(ctx context.Context, key string) (gocql.UUID, error) ChannelsAfter(ctx context.Context, after gocql.UUID, limit int) ([]*models.Channel, error) ListChannelMembers(ctx context.Context, channelID gocql.UUID) ([]*models.ChannelMember, error) GetState(ctx context.Context, key string) (string, error) @@ -110,6 +111,8 @@ type Deps struct { Blocks *blocks.Cache invited *window.Counter created *window.Counter + // botOpened counts the new conversations each bot opens. + botOpened *window.Counter // teamLocks serializes the changes that could leave a team without an // admin. teamLocks *keylock.Locks @@ -124,6 +127,7 @@ var defaultChannels = []struct{ name, display, purpose string }{ func Pool(d *Deps) actor.Producer { d.invited = window.New(d.Cfg.MaxInvites, d.Cfg.InviteTTL) d.created = window.New(d.Cfg.MaxTeamsPerDay, 24*time.Hour) + d.botOpened = window.New(d.Cfg.MaxBotConversationsPerHour, time.Hour) d.teamLocks = keylock.New() return actors.NewPool(d.Cfg.Workers, func() actor.Receiver { return &worker{Deps: d} }, actor.WithMiddleware(actors.Recover(d.Logger))) diff --git a/internal/services/directory/groups.go b/internal/services/directory/groups.go index 7203a59..ea11083 100644 --- a/internal/services/directory/groups.go +++ b/internal/services/directory/groups.go @@ -40,7 +40,8 @@ func (w *worker) openDirect(ctx context.Context, msg OpenDirect) (*pb.Channel, e // openGroup opens the conversation of the caller and the people they chose // in a team they all belong to: a direct one with one person, a group one // with more. The same people find the same conversation in a team, and -// another one in each other team. +// another one in each other team. A bot opens direct conversations with +// people alone, and only so many new ones an hour. func (w *worker) openGroup(ctx context.Context, msg OpenGroup) (*pb.Channel, error) { if !w.Authz.Can(authz.ObjDirect, authz.ActCreate, string(msg.P.Role)) { return nil, ierrors.PermissionDenied() @@ -64,16 +65,21 @@ func (w *worker) openGroup(ctx context.Context, msg OpenGroup) (*pb.Channel, err return nil, ierrors.InvalidArgument("pick someone to talk to") case len(members) > maxGroup: return nil, ierrors.InvalidArgument("a group conversation holds at most 8 people") + case msg.P.Bot && len(members) > 2: + return nil, ierrors.InvalidArgument("bots are not part of group conversations") } if _, err := w.membership(ctx, teamID, msg.P.UserID); err != nil { return nil, err } - bots := map[gocql.UUID]bool{} + bots := map[gocql.UUID]bool{msg.P.UserID: msg.P.Bot} for _, id := range members[1:] { bot, err := w.reachable(ctx, teamID, id) if err != nil { return nil, err } + if bot && msg.P.Bot { + return nil, ierrors.InvalidArgument("bots do not open conversations with bots") + } bots[id] = bot if err := w.mayReach(ctx, msg.P.UserID, id, kindOf(len(members))); err != nil { return nil, err @@ -97,10 +103,19 @@ func (w *worker) openGroup(ctx context.Context, msg OpenGroup) (*pb.Channel, err // A bot sees everything of a conversation it is in. cms[i] = &models.ChannelMember{ChannelID: ch.ID, UserID: id, TeamID: teamID, Role: models.ChannelRoleMember, JoinedAt: now, Bot: bots[id], BotSeesAll: bots[id]} } - chID, err := w.Repo.CreateDirectChannel(ctx, scylla.ConversationKey(teamID, ch.Name), ch, cms) + key := scylla.ConversationKey(teamID, ch.Name) + if msg.P.Bot { + if err := w.botMayOpen(ctx, msg.P.UserID, key); err != nil { + return nil, err + } + } + chID, err := w.Repo.CreateDirectChannel(ctx, key, ch, cms) if err != nil { return nil, err } + if msg.P.Bot && chID == ch.ID { + w.botOpened.Add(msg.P.UserID.String()) + } target := channels.Target{P: msg.P, ChannelID: chID} if chID != ch.ID { // Removals from the team took people out of it. @@ -124,6 +139,20 @@ func (w *worker) openGroup(ctx context.Context, msg OpenGroup) (*pb.Channel, err return view, nil } +// botMayOpen refuses bot the conversation of key once it opened its new +// ones for the hour; one it has already is free. +func (w *worker) botMayOpen(ctx context.Context, bot gocql.UUID, key string) error { + if !w.botOpened.Blocked(bot.String()) { + return nil + } + if _, err := w.Repo.DirectKeyHolder(ctx, key); err == nil { + return nil + } else if !ierrors.Is(err, ierrors.CodeNotFound) { + return err + } + return ierrors.ResourceExhausted("the bot opened too many conversations this hour, try again later") +} + // kindOf is the kind of a conversation of n people. func kindOf(n int) models.ChannelKind { if n == 2 { diff --git a/internal/services/push/push_test.go b/internal/services/push/push_test.go index e106b92..f3ebcf5 100644 --- a/internal/services/push/push_test.go +++ b/internal/services/push/push_test.go @@ -764,3 +764,23 @@ func testBlockedPeoplePushNothing(t *testing.T, copied bool) { t.Fatalf("cat's mention pushed to %v", got) } } + +func TestABotsDirectMessagePushesAsAPersons(t *testing.T) { + f := newFixture(t) + bot, ann := f.person("helper_bot"), f.person("ann") + f.user(bot).Bot = true + f.register(ann, pb.PushPlatform_PUSH_PLATFORM_FCM, fcmToken("ann")) + m := message(f.user(bot), ids.New(), "stand up") + m.Author.Bot = true + // A channel's members leave its bots out. + dm := Posted{Message: m, TeamID: ids.New(), Conversation: true, Members: members(ann)} + f.post(dm) + if got := f.fcm.take(); len(got) != 1 || got[0].token != fcmToken("ann") || got[0].n.Body != "stand up" { + t.Fatalf("a bot's direct message pushed %+v", got) + } + f.deps.Blocks = blocks.NewCache(blockList{ann.UserID: {bot.UserID}}, 10, 10, time.Minute) + f.post(dm) + if got := tokens(f.fcm.take()); len(got) != 0 { + t.Fatalf("a bot ann blocked pushed to %v", got) + } +} diff --git a/internal/web/botapi.go b/internal/web/botapi.go index a669465..81cac35 100644 --- a/internal/web/botapi.go +++ b/internal/web/botapi.go @@ -41,6 +41,7 @@ var botMethods = map[string]string{ "listTeams": "gochat.v1.TeamService/ListTeams", "listChannels": "gochat.v1.ChannelService/ListChannels", "getChannel": "gochat.v1.ChannelService/GetChannel", + "openDirect": "gochat.v1.ChannelService/OpenDirectChannel", "setWebhook": "gochat.v1.BotService/SetWebhook", "deleteWebhook": "gochat.v1.BotService/DeleteWebhook", "getWebhookInfo": "gochat.v1.BotService/GetWebhookInfo", diff --git a/internal/web/botapi_test.go b/internal/web/botapi_test.go index 991df29..5e19199 100644 --- a/internal/web/botapi_test.go +++ b/internal/web/botapi_test.go @@ -7,13 +7,17 @@ import ( "net/http" "net/http/httptest" "strings" + "sync" "testing" "time" "go.uber.org/zap" + "storj.io/drpc/drpcmetadata" + "storj.io/drpc/drpcmux" pb "github.com/ssenerg/gochat/api/protobuf/gen/gochat/v1" "github.com/ssenerg/gochat/internal/core/secure" + ierrors "github.com/ssenerg/gochat/internal/errors" "github.com/ssenerg/gochat/internal/web/gateway" ) @@ -86,3 +90,53 @@ func TestBotAPIWantsABotToken(t *testing.T) { t.Fatalf("an unknown method: %d", w.Code) } } + +// directChannels opens a bot's direct conversations while it has some +// left. +type directChannels struct { + pb.DRPCChannelServiceUnimplementedServer + token string + mu sync.Mutex + left int + opened []*pb.OpenDirectChannelRequest +} + +func (f *directChannels) OpenDirectChannel(ctx context.Context, req *pb.OpenDirectChannelRequest) (*pb.OpenDirectChannelResponse, error) { + if md, _ := drpcmetadata.Get(ctx); md["x-session"] != f.token { + return nil, rpcErr(ierrors.CodeUnauthenticated, "sign in") + } + f.mu.Lock() + defer f.mu.Unlock() + if f.left == 0 { + return nil, rpcErr(ierrors.CodeResourceExhausted, "the bot opened too many conversations this hour, try again later") + } + f.left-- + f.opened = append(f.opened, req) + return &pb.OpenDirectChannelResponse{Channel: &pb.Channel{Id: testChannel, TeamId: req.TeamId, Type: pb.ChannelType_CHANNEL_TYPE_DIRECT}}, nil +} + +func TestBotAPIOpensADirectConversation(t *testing.T) { + token := secure.BotToken() + f := &directChannels{token: token, left: 1} + b := newGateway(t, func(mux *drpcmux.Mux) error { return pb.DRPCRegisterChannelService(mux, f) }) + open := func() (int, botReply) { + r := httptest.NewRequest(http.MethodPost, flowOrigin+"/api/v2/bot/openDirect", strings.NewReader(`{"team_id": "`+testTeam+`", "user_id": "`+testPeer+`"}`)) + r.Header.Set("Authorization", "Bearer "+token) + r.Header.Set("Content-Type", "application/json") + w := httptest.NewRecorder() + b.h.ServeHTTP(w, r) + var reply botReply + _ = json.Unmarshal(w.Body.Bytes(), &reply) + return w.Code, reply + } + status, reply := open() + if status != http.StatusOK || !reply.OK || !strings.Contains(string(reply.Result), `"id":"`+testChannel+`"`) { + t.Fatalf("openDirect: %d %+v", status, reply) + } + if len(f.opened) != 1 || f.opened[0].TeamId != testTeam || f.opened[0].UserId != testPeer { + t.Fatalf("asked %v", f.opened) + } + if status, reply := open(); status != http.StatusTooManyRequests || reply.OK || reply.ErrorCode != http.StatusTooManyRequests || !strings.Contains(reply.Description, "too many conversations") { + t.Fatalf("openDirect past the limit: %d %+v", status, reply) + } +} diff --git a/internal/web/gateway/session.go b/internal/web/gateway/session.go index c107b45..ad99e78 100644 --- a/internal/web/gateway/session.go +++ b/internal/web/gateway/session.go @@ -788,6 +788,15 @@ func (s *session) mentioned(channelID string) { s.fragment("badge", map[string]any{"ID": channelID, "Count": s.mentions[channelID], "OOB": true}) } +// joined notes a direct conversation of the team the user was brought +// into, such as one a bot opens to write at once, so that its first +// message notifies before the sidebar shows it. +func (s *session) joined(ch *pb.Channel) { + if ch.GetType() == pb.ChannelType_CHANNEL_TYPE_DIRECT && ch.GetTeamId() == s.teamID && s.directs != nil { + s.directs[ch.GetId()] = true + } +} + // direct reports whether a sidebar channel is a direct conversation. func (s *session) direct(channelID string) bool { return s.directs[channelID] @@ -975,6 +984,7 @@ func (s *session) event(c *actor.Context, ev *pb.Event) { time.AfterFunc(typingLinger, func() { engine.Send(self, clearTyping{channelID: ch, seq: seq}) }) case *pb.Event_ChannelJoined: if s.teamID != "" { + s.joined(p.ChannelJoined.Channel) s.loadSidebar(c, true) } case *pb.Event_ChannelLeft: diff --git a/internal/web/gateway/session_test.go b/internal/web/gateway/session_test.go index 98a96aa..bf95062 100644 --- a/internal/web/gateway/session_test.go +++ b/internal/web/gateway/session_test.go @@ -107,6 +107,28 @@ func TestOtherTeamsConversationsStayThere(t *testing.T) { } } +func TestAConversationNotifiesBeforeTheSidebarShowsIt(t *testing.T) { + s := quietSession(t) + s.teamID = "acme" + for id, kind := range map[string]pb.ChannelType{"dm": pb.ChannelType_CHANNEL_TYPE_DIRECT, "group": pb.ChannelType_CHANNEL_TYPE_GROUP} { + s.joined(&pb.Channel{Id: id, Type: kind, TeamId: "acme"}) + } + for channel, want := range map[string]bool{"dm": true, "group": false} { + e := posted("1", channel) + e.GetMessagePosted().TeamId, e.GetMessagePosted().Conversation = "acme", true + s.event(nil, e) + s.out.close(websocket.StatusNormalClosure) + notified := false + for _, f := range s.frames() { + notified = notified || strings.Contains(f, `id="notify"`) + } + if notified != want { + t.Errorf("%s: notified %v", channel, notified) + } + s.out = newOutbox(1 << 20) + } +} + func answer(id, text string) *pb.Event { return &pb.Event{Payload: &pb.Event_CallbackAnswer{CallbackAnswer: &pb.CallbackAnswer{ QueryId: id, Text: text, Bot: &pb.User{Id: "bot", DisplayName: "Echo"}, diff --git a/pkg/gochat/client.go b/pkg/gochat/client.go index 5eb98ed..6660d1f 100644 --- a/pkg/gochat/client.go +++ b/pkg/gochat/client.go @@ -199,6 +199,17 @@ func (c *Client) Call(ctx context.Context, method string, in, out proto.Message) } } +// OpenDirect opens the direct conversation with userID in teamID, such as +// the team of a message the person posted. A conversation they have +// already is found again. +func (c *Client) OpenDirect(ctx context.Context, teamID, userID string) (*pb.Channel, error) { + res := &pb.OpenDirectChannelResponse{} + if err := c.Call(ctx, "gochat.v1.ChannelService/OpenDirectChannel", &pb.OpenDirectChannelRequest{TeamId: teamID, UserId: userID}, res); err != nil { + return nil, err + } + return res.Channel, nil +} + func (c *Client) call(ctx context.Context, method string, payload []byte) (*pb.Result, error) { c.mu.Lock() ready := c.ready diff --git a/pkg/gochat/client_test.go b/pkg/gochat/client_test.go index 921bd5d..21974e2 100644 --- a/pkg/gochat/client_test.go +++ b/pkg/gochat/client_test.go @@ -281,3 +281,38 @@ func TestACallTheConnectionDropsUnderIsNotMadeAgain(t *testing.T) { case <-time.After(time.Second): } } + +func TestOpenDirect(t *testing.T) { + url := fakeServer(t, func(f *fakeConn) { + f.read() + f.send(welcome(1, false)) + for _, fail := range []bool{false, true} { + call := f.read().GetCall() + var req pb.OpenDirectChannelRequest + _ = proto.Unmarshal(call.GetPayload(), &req) + if call.GetMethod() != "gochat.v1.ChannelService/OpenDirectChannel" || req.TeamId != "team" || req.UserId != "ann" { + t.Errorf("call %s %v", call.GetMethod(), &req) + } + res := &pb.Result{Id: call.GetId()} + if fail { + res.Outcome = &pb.Result_Error{Error: &pb.ApiError{Code: 8, Message: "too many"}} + } else { + b, _ := proto.Marshal(&pb.OpenDirectChannelResponse{Channel: &pb.Channel{Id: "dm"}}) + res.Outcome = &pb.Result_Payload{Payload: b} + } + f.send(&pb.Down{Frame: &pb.Down_Result{Result: res}}) + } + }) + c, err := Dial(context.Background(), Options{URL: url, Token: "b_token"}) + if err != nil { + t.Fatal(err) + } + defer func() { _ = c.Close() }() + if ch, err := c.OpenDirect(context.Background(), "team", "ann"); err != nil || ch.GetId() != "dm" { + t.Fatalf("opened %v: %v", ch, err) + } + var e *Error + if _, err := c.OpenDirect(context.Background(), "team", "ann"); !errors.As(err, &e) || e.Code != 8 { + t.Fatalf("a refusal: %v", err) + } +} diff --git a/test/e2e/bots_test.go b/test/e2e/bots_test.go index e0cd251..a829c0f 100644 --- a/test/e2e/bots_test.go +++ b/test/e2e/bots_test.go @@ -3,6 +3,7 @@ package e2e import ( "context" "encoding/json" + "errors" "net/http" "net/url" "regexp" @@ -94,7 +95,7 @@ func testBots(t *testing.T, admin *client, adminWS *socket, teamID, town string, } adminAPI := newApp(t) - adminAPI.login(adminName, adminPass) + adminUser := adminAPI.login(adminName, adminPass) adminAPI.must("gochat.v1.ChannelService/AddChannelMember", &pb.AddChannelMemberRequest{ChannelId: town, UserId: botID}, &pb.AddChannelMemberResponse{}) botNext(t, c, "its joining", func(ev *pb.Event) bool { return ev.GetChannelJoined().GetChannel().GetId() == town }) @@ -163,6 +164,61 @@ func testBots(t *testing.T, admin *client, adminWS *socket, teamID, town string, } }) + step("the bot writes to a person first", func(t *testing.T) { + dm, err := c.OpenDirect(ctx, teamID, adminUser.Id) + if err != nil { + t.Fatal(err) + } + if dm.Type != pb.ChannelType_CHANNEL_TYPE_DIRECT { + t.Fatalf("the bot opened %v", dm) + } + if err := c.Call(ctx, "gochat.v1.MessageService/PostMessage", &pb.PostMessageRequest{ChannelId: dm.Id, Content: "reminder " + tag}, &pb.PostMessageResponse{}); err != nil { + t.Fatal(err) + } + // The person gets the conversation in their sidebar and a notice + // of its first message, in either order. + var listed, noticed bool + deadline := time.After(10 * time.Second) + for !listed || !noticed { + select { + case f, ok := <-adminWS.in: + if !ok { + t.Fatal("the socket closed") + } + listed = listed || (strings.Contains(f, `id="channel-lists"`) && strings.Contains(f, dm.Id)) + noticed = noticed || (strings.Contains(f, `id="notify"`) && strings.Contains(f, "reminder "+tag)) + case <-deadline: + t.Fatalf("listed %v, noticed %v", listed, noticed) + } + } + if again, err := c.OpenDirect(ctx, teamID, adminUser.Id); err != nil || again.Id != dm.Id { + t.Fatalf("opening it again: %v %v", again, err) + } + // The bot gets every message of it. + adminWS.send(map[string]string{"op": "post", "channel_id": dm.Id, "content": "thanks " + tag}) + botNext(t, c, "the answer", postedBy(botID, func(s string) bool { return s == "thanks "+tag })) + + var e *gochat.Error + err = c.Call(ctx, "gochat.v1.ChannelService/OpenGroupChannel", &pb.OpenGroupChannelRequest{TeamId: teamID, UserIds: []string{adminUser.Id}}, &pb.OpenGroupChannelResponse{}) + if !errors.As(err, &e) || e.Code != 12 { + t.Fatalf("a bot opened a group: %v", err) + } + // Blocked, it neither opens the conversation nor writes in it. + adminAPI.must("gochat.v1.SafetyService/BlockUser", &pb.BlockUserRequest{UserId: botID}, &pb.BlockUserResponse{}) + open := map[string]string{"team_id": teamID, "user_id": adminUser.Id} + if status, r := botHTTP(t, token, "openDirect", open); status != http.StatusForbidden || r.OK { + t.Fatalf("openDirect to someone who blocked the bot: %d %+v", status, r) + } + err = c.Call(ctx, "gochat.v1.MessageService/PostMessage", &pb.PostMessageRequest{ChannelId: dm.Id, Content: "blocked " + tag}, &pb.PostMessageResponse{}) + if !errors.As(err, &e) || e.Code != 7 { + t.Fatalf("a blocked bot posted: %v", err) + } + adminAPI.must("gochat.v1.SafetyService/UnblockUser", &pb.UnblockUserRequest{UserId: botID}, &pb.UnblockUserResponse{}) + if status, r := botHTTP(t, token, "openDirect", open); status != http.StatusOK || !r.OK || !strings.Contains(string(r.Result), dm.Id) { + t.Fatalf("openDirect: %d %+v", status, r) + } + }) + step("a bot that was away gets what it missed", func(t *testing.T) { pts := c.Pts() _ = c.Close()