package handlers
import (
"sync"
config "git.zio.sh/astra/telegram-approval-join/config"
api "github.com/OvyFlash/telegram-bot-api"
)
const (
AdminJoinRequestMsg = "New join #request from %s [%d]\n\nJoin reason: %s"
AdminApprovedMsg = "✅ Join #request approved for %s [%d]\n\nJoin reason: %s\nApproved by: %s\nApproved at: %s"
AdminDeclinedMsg = "❌ Join #request declined for %s [%d]\n\nJoin reason: %s\nDeclined by: %s\nDeclined at: %s\nDeclined reason: %s"
AdminFailedMsg = "⚠️ Join #request failed for %s [%d]\n\nJoin reason: %s\nFailure reason: %s"
)
// Types shared by handler files
type ExtendedChatJoinRequest struct {
*api.ChatJoinRequest
JoinReason string
JoinRequestMessageID int
}
type Bot struct {
API *api.BotAPI
Config config.Config
mu sync.RWMutex
WaitingForApproval map[int64]*ExtendedChatJoinRequest
}
// GetPendingUser retrieves a pending user request (read-safe).
func (bot *Bot) GetPendingUser(userID int64) *ExtendedChatJoinRequest {
bot.mu.RLock()
defer bot.mu.RUnlock()
user := bot.WaitingForApproval[userID]
if user == nil {
return nil
}
return user
}
// SetPendingUser stores a pending user request (write-safe).
func (bot *Bot) SetPendingUser(userID int64, user *ExtendedChatJoinRequest) {
bot.mu.Lock()
defer bot.mu.Unlock()
if _, ok := bot.WaitingForApproval[userID]; !ok {
bot.WaitingForApproval = make(map[int64]*ExtendedChatJoinRequest)
}
bot.WaitingForApproval[userID] = user
}
// DeletePendingUser removes a pending user request (write-safe).
func (bot *Bot) DeletePendingUser(userID int64) {
bot.mu.Lock()
defer bot.mu.Unlock()
delete(bot.WaitingForApproval, userID)
}