Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ player, and production-minded.
The welcome when maply is added answers the admin, as `/start` does, and
is not one of the three. A group added after its reveal gets a warm-up on
the archive's newest day and its result an hour later, the only scheduled
message of that day (`docs/design.md` → "Groups").
message of that day (`docs/design.md` → "Groups"). A group quiet for
seven days gets its question to keep playing in place of the intrigue,
and a paused group gets nothing (`chat_settings.paused_at`).
- Posts write the reveal with the chat's zone, "20:00 (Kyiv)". A chat's zone is
stored the first time maply needs it (`FixZone`), so a later change of
language cannot move a reveal. A DM's stored this way is marked automatic
Expand Down
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ See [`docs/versioning.md`](docs/versioning.md) for what the numbers mean and
supergroup, the welcome asks the admins once, with a button that requests
just that right. `/start` in a group offers the warm-up while it runs.
For the operator: migration 031 adds the `warmup` tables and job kind.
- maply stops posting into a group where nobody plays. After seven days
with the morning post and no answer from the group, it asks once, in
place of that day's intrigue, whether to keep sending places: "Keep
playing" (any member) or "Pause" (admins). With no press and no answer
for three days the group pauses itself and gets nothing until an admin
presses Resume under `/start`; the places come back the next morning.
Any answer from the group resets the count. For the operator: migration
032 adds the pause columns to `chat_settings`, and a pause is logged as
`group paused` with its cause.

## v0.0.1-alpha.20 - 2026-11-10

Expand Down
1 change: 1 addition & 0 deletions docs/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ In a DM the intrigue arrives only if the person has not answered yet. The "Chat
- after the reveal: a **warm-up**, since the group would otherwise wait until tomorrow morning. It plays the archive's newest day, normally yesterday's place: over in every zone, kept, and a panorama, as the free game's archive has it. Its button opens the Mini App with `startapp=w<payload>`, signed for the group and the day (version 3 of the payload), and membership is asked of Telegram as for a day's post. Each player's game is their own day from the archive (`free_archive`), so the warm-up counts in no day's table, no world, no profile, no streak and no rating, and someone who answered that day or had its place cannot take it, as in the archive. `warmup_game` only says which of those games came from the group. It takes answers for an hour (`schedule.WarmupFor`); then the `warmup` job sends its result: the place, the group's table and when the real game starts. Nobody played, no result. A group gets one warm-up ever; added back, it gets a welcome without one. The day's game for that date has no post and no result there.

The welcome answers an admin adding maply, as a `/start` panel does, and is none of the day's three messages; the warm-up's result is the only scheduled message of a day the group has no round of. `/start` in a group shows the warm-up's button while it takes answers.
- **A group where nobody plays** (#94, 16 November 2026). A quiet day is a day the group got its morning post and nobody answered from the group (`guess.chat_id`); a warm-up is free play and is no answer, and a day without a post neither counts nor breaks the run. After seven quiet days in a row the group gets, in place of the eighth day's intrigue, one question: "Nobody here has guessed a place for a week. Keep sending them?" with "Keep playing" and "Pause". The question takes the intrigue's slot, so the day still has three messages; a group with the intrigue turned off has the slot too, half an hour after the post when its post comes too late for 16:00–18:00. Any member may press "Keep playing", which counts the quiet days again from the day after the question; "Pause" is an admin's, checked with Telegram on the press. Any answer from the group, before or after the question, ends the run and takes the question back. With no press and no answer, the third morning after the question pauses the group: no post that morning, and the question is edited to say how to turn maply back on. A paused group gets nothing from the scheduler, no post, intrigue or result, and is not an active chat; it keeps maply and still counts for its members' answer window. `/start` there says maply is paused, with "Resume" for an admin; resuming starts with the first morning post after the press, so a group resumed after its post time waits for the next morning. The pause is logged with its cause (`paused_by`: admin or silence), and so are the question, a press of "Keep playing" and a resume. maply removed and added back starts afresh: no pause and no quiet days. Migration 032 adds the columns to `chat_settings`.
- **Permissions.** The game works without the right to pin messages. The bot tells the admins once that it is more fun with a pin: in the welcome, when maply joins a supergroup without being its admin, with a button `t.me/<bot>?startgroup=pin&admin=pin_messages` that asks Telegram for that one right; otherwise in the first post that could not be pinned. In a basic group every member may pin unless the admins said otherwise, so the welcome does not ask there.
- **`/rival`.** Links two chats into a duel: a shared season score and the "Rival" intrigue.
- **Seasons.** A calendar month. At the end the chat gets a champion of the month, and the ranking resets.
Expand Down
5 changes: 5 additions & 0 deletions internal/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ type Store interface {
PlanJob(ctx context.Context, kind string, chatID int64, d round.Date, runAt time.Time) error
StartWarmup(ctx context.Context, chatID int64, closed round.Date, resultAt time.Time) (db.Warmup, bool, error)
WarmupOf(ctx context.Context, chatID int64) (db.Warmup, bool, error)

Paused(ctx context.Context, chatID int64) (bool, error)
KeepPlaying(ctx context.Context, chatID int64) (bool, error)
PauseGroup(ctx context.Context, chatID int64, by string) (int64, bool, error)
ResumeGroup(ctx context.Context, chatID int64, at time.Time) (bool, error)
}

type Telegram interface {
Expand Down
31 changes: 30 additions & 1 deletion internal/bot/bot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,41 @@ type fakeStore struct {
warmups map[int64]db.Warmup
jobs []string
pinWarned map[int64]bool
// paused is the groups maply is paused in; asked those with the quiet
// question out.
paused map[int64]bool
asked map[int64]bool
}

func newStore() *fakeStore {
return &fakeStore{userLang: map[int64]string{}, chatLang: map[int64]string{}, zones: map[int64]string{},
settings: map[int64]db.Settings{}, rounds: map[string]bool{},
warmups: map[int64]db.Warmup{}, pinWarned: map[int64]bool{}}
warmups: map[int64]db.Warmup{}, pinWarned: map[int64]bool{},
paused: map[int64]bool{}, asked: map[int64]bool{}}
}

func (f *fakeStore) Paused(_ context.Context, id int64) (bool, error) { return f.paused[id], nil }

func (f *fakeStore) KeepPlaying(_ context.Context, id int64) (bool, error) {
ok := f.asked[id] && !f.paused[id]
if ok {
f.asked[id] = false
}
return ok, nil
}

func (f *fakeStore) PauseGroup(_ context.Context, id int64, _ string) (int64, bool, error) {
if f.paused[id] {
return 0, false, nil
}
f.paused[id], f.asked[id] = true, false
return 0, true, nil
}

func (f *fakeStore) ResumeGroup(_ context.Context, id int64, _ time.Time) (bool, error) {
ok := f.paused[id]
f.paused[id] = false
return ok, nil
}

func (f *fakeStore) Offset(context.Context) (int64, error) {
Expand Down
3 changes: 2 additions & 1 deletion internal/bot/menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const (
actGroupAuto = "glangauto"
actPost = "post"
actSwitch = "sw"
actResume = "resume"
actClose = "close"
)

Expand All @@ -62,7 +63,7 @@ var postSteps = map[int]bool{-60: true, -15: true, 15: true, 60: true}
func adminOnly(action string) bool {
head, _, _ := strings.Cut(action, ":")
switch head {
case actSettings, actZone, actGroupLang, actGroupAuto, actPost, actSwitch:
case actSettings, actZone, actGroupLang, actGroupAuto, actPost, actSwitch, actResume:
return true
}
return false
Expand Down
34 changes: 34 additions & 0 deletions internal/bot/panels.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ func (b *Bot) sendHome(ctx context.Context, m *tg.Message) error {
warm = b.openWarmup(ctx, m.Chat.ID)
}
text, markup := v.home(b.opts.AppURL, admin, round.Name(zone, zoneLang), warm)
if v.sc == scopeGroup && b.groupPaused(ctx, m.Chat.ID) {
text, markup = v.paused(admin)
}
if _, err := b.tg.SendTextIn(ctx, m.Chat.ID, threadOf(*m), text, markup); err != nil {
return fmt.Errorf("send home to %d: %w", m.Chat.ID, err)
}
Expand Down Expand Up @@ -221,6 +224,9 @@ func (b *Bot) onCallback(ctx context.Context, cq *tg.CallbackQuery) error {
if d, ok := schedule.WorldCallback(cq.Data); ok {
return b.onRefreshWorld(ctx, cq, d)
}
if isQuietCB(cq.Data) {
return b.onQuiet(ctx, cq)
}
owner, action, ok := parseMenuCB(cq.Data)
if !ok {
return b.tg.AnswerCallbackQuery(ctx, cq.ID, "")
Expand Down Expand Up @@ -353,8 +359,36 @@ func (b *Bot) runAction(ctx context.Context, cq *tg.CallbackQuery, owner int64,
warm := ""
if v.sc == scopeGroup {
warm = b.openWarmup(ctx, chat.ID)
if b.groupPaused(ctx, chat.ID) {
return edit(v.paused(admin))
}
}
return edit(v.home(b.opts.AppURL, admin, round.Name(zone, zoneLang), warm))
case actResume:
if v.sc != scopeGroup {
return "", nil
}
ok, err := b.store.ResumeGroup(ctx, chat.ID, b.opts.Now())
if err != nil {
return "", err
}
if ok {
b.log.Info("group resumed", "chat", chat.ID)
}
zone, err := b.store.ChatZone(ctx, chat.ID)
if err != nil {
return "", err
}
st, err := b.store.ChatSettings(ctx, chat.ID)
if err != nil {
return "", err
}
// The home, saying when the next place comes: the first morning
// post after now (schedule's resting), which is today's only
// before its time.
_, markup := v.home(b.opts.AppURL, true, round.Name(zone, zoneLang), "")
text := panel(productName, "", v.t("home.hint"), quote(v.t("home.resumed", "post", fmt.Sprintf("%02d:%02d", st.PostHour, st.PostMin))))
return edit(text, markup)
case actHelp:
return edit(v.help())
case actAbout:
Expand Down
105 changes: 105 additions & 0 deletions internal/bot/quiet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// SPDX-License-Identifier: Apache-2.0
package bot

import (
"context"
"fmt"

"github.com/FreshLabDev/tg"

"github.com/FreshLabDev/maply/internal/db"
"github.com/FreshLabDev/maply/internal/i18n"
"github.com/FreshLabDev/maply/internal/schedule"
"github.com/FreshLabDev/maply/internal/telegram"
)

func isQuietCB(data string) bool { return data == schedule.QuietKeep || data == schedule.QuietPause }

// onQuiet is a press under the question a quiet group gets (see
// schedule.askQuiet). Any member may keep the game going; only an admin may
// pause it, and the check runs on every press.
func (b *Bot) onQuiet(ctx context.Context, cq *tg.CallbackQuery) error {
m := cq.Message
if scopeOf(m.Chat) != scopeGroup || !isPanel(m) || !pressedHere(m, cq.Data) {
return b.tg.AnswerCallbackQuery(ctx, cq.ID, "")
}
raw, err := b.personLang(ctx, cq.From)
if err != nil {
_ = b.tg.AnswerCallbackQuery(ctx, cq.ID, "")
return err
}
_, lang, err := b.groupLang(ctx, m.Chat.ID, raw)
if err != nil {
_ = b.tg.AnswerCallbackQuery(ctx, cq.ID, "")
return err
}
text := ""
switch cq.Data {
case schedule.QuietKeep:
kept, err := b.store.KeepPlaying(ctx, m.Chat.ID)
if err != nil {
_ = b.tg.AnswerCallbackQuery(ctx, cq.ID, "")
return err
}
text = i18n.T(lang, "quiet.kept")
if kept {
b.log.Info("quiet group kept", "chat", m.Chat.ID)
} else if paused, err := b.store.Paused(ctx, m.Chat.ID); err != nil {
_ = b.tg.AnswerCallbackQuery(ctx, cq.ID, "")
return err
} else if paused {
// Too late: the group paused itself. /start turns it back on.
text = i18n.T(lang, "quiet.paused")
}
case schedule.QuietPause:
admin, err := b.isAdmin(ctx, m.Chat.ID, cq.From.ID)
if err != nil {
_ = b.tg.AnswerCallbackQuery(ctx, cq.ID, "")
return err
}
if !admin {
return b.tg.AnswerCallbackQueryAlert(ctx, cq.ID, i18n.T(i18n.Resolve(raw), "msg.admin_only"))
}
if _, ok, err := b.store.PauseGroup(ctx, m.Chat.ID, db.PausedByAdmin); err != nil {
_ = b.tg.AnswerCallbackQuery(ctx, cq.ID, "")
return err
} else if ok {
b.log.Info("group paused", "chat", m.Chat.ID, "by", db.PausedByAdmin)
}
text = i18n.T(lang, "quiet.paused")
}
err = b.tg.EditText(ctx, m.Chat.ID, m.MessageID, text, nil)
if err != nil && !tg.IsMessageNotModified(err) {
_ = b.tg.AnswerCallbackQuery(ctx, cq.ID, "")
return fmt.Errorf("edit quiet question in %d: %w", m.Chat.ID, err)
}
return b.tg.AnswerCallbackQuery(ctx, cq.ID, "")
}

// paused is the home of a paused group: what happened, and Resume for an
// admin, who is the only one who can press it, over Settings.
func (v view) paused(admin bool) (string, telegram.Markup) {
text := panel(productName, "", v.t("home.hint"), quote(v.t("home.paused")))
var rows telegram.Markup
if admin {
b := v.button("btn.resume", actResume)
b.Style = tg.StylePrimary
rows = append(rows, []telegram.Button{b}, []telegram.Button{v.button("btn.settings", actSettings)})
}
rows = append(rows,
[]telegram.Button{v.button("btn.help", actHelp), v.button("btn.about", actAbout)},
[]telegram.Button{v.closeButton()},
)
return text, rows
}

// groupPaused reads whether the group is paused for its home. A failed read
// shows the usual home: Resume is only offered when the pause is known.
func (b *Bot) groupPaused(ctx context.Context, chatID int64) bool {
paused, err := b.store.Paused(ctx, chatID)
if err != nil {
b.log.Warn("pause not read", "chat", chatID, "error", err)
return false
}
return paused
}
59 changes: 59 additions & 0 deletions internal/bot/quiet_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// SPDX-License-Identifier: Apache-2.0
package bot

import (
"context"
"strings"
"testing"

"github.com/FreshLabDev/maply/internal/i18n"
"github.com/FreshLabDev/maply/internal/schedule"
)

// Pause and Resume are an admin's: a member's press is refused on the
// press itself, whatever the callback says, and changes nothing.
func TestOnlyAnAdminPausesOrResumes(t *testing.T) {
b, store, client := testBot(t)
ctx := context.Background()
refused := i18n.T("ru", "msg.admin_only")

store.chatLang[groupID] = "en"
store.asked[groupID] = true
b.handle(ctx, press(group, member, schedule.QuietPause))
if store.paused[groupID] || len(client.alerts) != 1 || client.alerts[0] != refused || len(client.edits) != 0 {
t.Fatalf("a member paused: paused %v, alerts %v, edits %d", store.paused[groupID], client.alerts, len(client.edits))
}
// Keep playing is anyone's.
b.handle(ctx, press(group, member, schedule.QuietKeep))
if store.asked[groupID] || len(client.edits) != 1 || client.edits[0].text != i18n.T("en", "quiet.kept") || len(client.edits[0].markup) != 0 {
t.Fatalf("keep playing: asked %v, edits %+v", store.asked[groupID], client.edits)
}
b.handle(ctx, press(group, admin, schedule.QuietPause))
if !store.paused[groupID] || len(client.edits) != 2 || client.edits[1].text != i18n.T("en", "quiet.paused") {
t.Fatalf("an admin's pause: paused %v, edits %+v", store.paused[groupID], client.edits)
}

// /start in the paused group says so; Resume is on the admin's panel only.
b.handle(ctx, command(group, member, "/start"))
text, markup := client.sends[len(client.sends)-1].text, client.sends[len(client.sends)-1].markup
if !strings.Contains(text, i18n.T("en", "home.paused")) || has(markup, actResume) {
t.Fatalf("member's /start in a paused group = %q, %+v", text, markup)
}
b.handle(ctx, command(group, admin, "/start"))
if markup := client.sends[len(client.sends)-1].markup; !has(markup, actResume) {
t.Fatalf("admin's /start in a paused group has no Resume: %+v", markup)
}

// A member forging Resume into their own panel is refused.
b.handle(ctx, press(group, member, cb(userID, actResume)))
if !store.paused[groupID] || len(client.alerts) != 2 || client.alerts[1] != refused {
t.Fatalf("a member resumed: paused %v, alerts %v", store.paused[groupID], client.alerts)
}
b.handle(ctx, press(group, admin, cb(adminID, actResume)))
if store.paused[groupID] {
t.Fatal("an admin's Resume left the group paused")
}
if last := client.edits[len(client.edits)-1]; !strings.Contains(last.text, i18n.T("en", "home.resumed", "post", "07:00")) || has(last.markup, actResume) {
t.Fatalf("after Resume the panel = %q, %+v", last.text, last.markup)
}
}
Loading
Loading