Add checkbox for telegram notifications

This commit is contained in:
blankhex committed 2026-10-25 10:01:20 +03:00
1 parent e65d1ac46b
commit d4257e7dab
10 files changed
+74 -15

No files matched your search

+5 -1
View File
@@ -37,7 +37,11 @@ func Migrations() []migration.Migration {
{
Version: 4,
SQL: `CREATE INDEX IF NOT EXISTS idx_meeting_votes_meeting
ON meeting_votes(meeting_id);`,
ON meeting_votes(meeting_id);`,
},
{
Version: 5,
SQL: `ALTER TABLE meetings ADD COLUMN has_notification INTEGER NOT NULL DEFAULT 1;`,
},
}
}
+5
View File
@@ -19,6 +19,11 @@ type Meeting struct {
CreatedBy int64
CreatedAt time.Time
// HasNotification reports whether the meeting should be announced by
// notification consumers such as the Telegram bot. When false the meeting
// is created silently.
HasNotification bool
// Fields below are filled by the repository and are relative to the user
// requesting the meeting: the author's display name, the display names of
// the users who voted to join or not join, the resulting tallies and the
+4 -4
View File
@@ -24,7 +24,7 @@ type Repository struct {
// the requesting user's own vote. The first placeholder is the viewer's user id,
// used for the my_vote column. Voter names and tallies are attached separately
// by attachVoters.
const meetingColumns = `m.id, m.title, m.intent, m.room, m.scheduled_at, m.created_by, m.created_at,
const meetingColumns = `m.id, m.title, m.intent, m.room, m.scheduled_at, m.created_by, m.created_at, m.has_notification,
COALESCE(NULLIF(u.fullname, ''), u.login, '') AS created_by_name,
COALESCE((SELECT v.vote FROM meeting_votes v WHERE v.meeting_id = m.id AND v.user_id = ?), '') AS my_vote`
@@ -44,7 +44,7 @@ func scanMeeting(row rowScanner) (*Meeting, error) {
m := &Meeting{}
var scheduledAt, createdAt time.Time
err := row.Scan(
&m.Id, &m.Title, &m.Intent, &m.Room, &scheduledAt, &m.CreatedBy, &createdAt,
&m.Id, &m.Title, &m.Intent, &m.Room, &scheduledAt, &m.CreatedBy, &createdAt, &m.HasNotification,
&m.CreatedByName, &m.MyVote,
)
if err != nil {
@@ -116,8 +116,8 @@ ORDER BY u.login ASC`
}
func (r *Repository) CreateMeeting(ctx context.Context, m *Meeting) (*Meeting, error) {
query := "INSERT INTO meetings(title, intent, room, scheduled_at, created_by, created_at) VALUES(?, ?, ?, ?, ?, ?) RETURNING id"
err := r.q.QueryRowContext(ctx, query, m.Title, m.Intent, m.Room, m.ScheduledAt, m.CreatedBy, m.CreatedAt).Scan(&m.Id)
query := "INSERT INTO meetings(title, intent, room, scheduled_at, created_by, created_at, has_notification) VALUES(?, ?, ?, ?, ?, ?, ?) RETURNING id"
err := r.q.QueryRowContext(ctx, query, m.Title, m.Intent, m.Room, m.ScheduledAt, m.CreatedBy, m.CreatedAt, m.HasNotification).Scan(&m.Id)
if err != nil {
slog.Error("create meeting", slog.String("op", "repo.CreateMeeting"), slog.Int64("user_id", m.CreatedBy), slog.Any("error", err))
return nil, ErrInternal
+11 -8
View File
@@ -23,8 +23,10 @@ func NewService(r *Repository) (*Service, error) {
// Create validates and stores a new meeting authored by u. When autoJoin is
// true the author's "join" vote is cast immediately so the meeting reflects
// their intent to attend.
func (s *Service) Create(ctx context.Context, u *user.User, title, intent, room string, scheduledAt time.Time, autoJoin bool) (*Meeting, error) {
// their intent to attend. When hasNotification is true the meeting is eligible
// for announcement by notification consumers such as the Telegram bot; when
// false it is created silently.
func (s *Service) Create(ctx context.Context, u *user.User, title, intent, room string, scheduledAt time.Time, autoJoin, hasNotification bool) (*Meeting, error) {
if u == nil {
slog.Error("nil user", slog.String("op", "service.Create"))
return nil, ErrInternal
@@ -38,12 +40,13 @@ func (s *Service) Create(ctx context.Context, u *user.User, title, intent, room
}
created, err := s.r.CreateMeeting(ctx, &Meeting{
Title: title,
Intent: intent,
Room: room,
ScheduledAt: scheduledAt.UTC(),
CreatedBy: u.Id,
CreatedAt: time.Now().UTC(),
Title: title,
Intent: intent,
Room: room,
ScheduledAt: scheduledAt.UTC(),
CreatedBy: u.Id,
CreatedAt: time.Now().UTC(),
HasNotification: hasNotification,
})
if err != nil {
return nil, err
+17 -1
View File
@@ -16,6 +16,10 @@ type createMeetingRequest struct {
// AutoJoin asks the server to record the author's own "join" vote so the
// meeting carries their intent to attend from the moment it is created.
AutoJoin bool `json:"autoJoin"`
// HasNotification controls whether notification consumers (such as the
// Telegram bot) announce the meeting. It is a pointer so an omitted field
// can be distinguished from an explicit false and defaults to enabled.
HasNotification *bool `json:"hasNotification"`
}
type voteMeetingRequest struct {
@@ -36,6 +40,9 @@ type meetingResponse struct {
JoinCount int `json:"joinCount"`
DeclineCount int `json:"declineCount"`
MyVote string `json:"myVote"`
// HasNotification mirrors the stored flag so clients can show whether the
// meeting will be announced.
HasNotification bool `json:"hasNotification"`
}
func (h *Handler) listMeetings(w http.ResponseWriter, r *http.Request) {
@@ -71,7 +78,14 @@ func (h *Handler) createMeeting(w http.ResponseWriter, r *http.Request) {
return
}
created, err := h.meetings.Create(r.Context(), u, req.Title, req.Intent, req.Room, scheduledAt, req.AutoJoin)
// Omitted hasNotification defaults to true so existing clients keep
// announcing meetings unless they explicitly opt out.
hasNotification := true
if req.HasNotification != nil {
hasNotification = *req.HasNotification
}
created, err := h.meetings.Create(r.Context(), u, req.Title, req.Intent, req.Room, scheduledAt, req.AutoJoin, hasNotification)
if err != nil {
h.fail(w, err)
return
@@ -154,5 +168,7 @@ func newMeetingResponse(m *meeting.Meeting) meetingResponse {
JoinCount: m.JoinCount,
DeclineCount: m.DeclineCount,
MyVote: m.MyVote,
HasNotification: m.HasNotification,
}
}