mirror of
https://github.com/Mezeporta/Erupe.git
synced 2026-03-21 23:22:34 +01:00
Introduce MailService as a convenience layer between handlers/services and MailRepo. Provides Send, SendSystem, SendGuildInvite, and BroadcastToGuild methods that encapsulate the boolean flag combinations. GuildService now depends on MailService instead of MailRepo directly, simplifying its mail-sending calls from verbose SendMail(..., false, true) to clean SendSystem(recipientID, subject, body). Guild mail broadcast logic moved from handleMsgMhfSendMail into MailService.BroadcastToGuild.
56 lines
2.0 KiB
Go
56 lines
2.0 KiB
Go
package channelserver
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
// MailService encapsulates mail-sending business logic, sitting between
|
|
// handlers/services and the MailRepo. It provides convenient methods for
|
|
// common mail patterns (system notifications, guild broadcasts, player mail)
|
|
// so callers don't need to specify boolean flags directly.
|
|
type MailService struct {
|
|
mailRepo MailRepo
|
|
guildRepo GuildRepo
|
|
logger *zap.Logger
|
|
}
|
|
|
|
// NewMailService creates a new MailService.
|
|
func NewMailService(mr MailRepo, gr GuildRepo, log *zap.Logger) *MailService {
|
|
return &MailService{
|
|
mailRepo: mr,
|
|
guildRepo: gr,
|
|
logger: log,
|
|
}
|
|
}
|
|
|
|
// Send sends a player-to-player mail with an optional item attachment.
|
|
func (svc *MailService) Send(senderID, recipientID uint32, subject, body string, itemID, quantity uint16) error {
|
|
return svc.mailRepo.SendMail(senderID, recipientID, subject, body, itemID, quantity, false, false)
|
|
}
|
|
|
|
// SendSystem sends a system notification mail (no item, flagged as system message).
|
|
func (svc *MailService) SendSystem(recipientID uint32, subject, body string) error {
|
|
return svc.mailRepo.SendMail(0, recipientID, subject, body, 0, 0, false, true)
|
|
}
|
|
|
|
// SendGuildInvite sends a guild invitation mail (flagged as guild invite).
|
|
func (svc *MailService) SendGuildInvite(senderID, recipientID uint32, subject, body string) error {
|
|
return svc.mailRepo.SendMail(senderID, recipientID, subject, body, 0, 0, true, false)
|
|
}
|
|
|
|
// BroadcastToGuild sends a mail from senderID to all members of the specified guild.
|
|
func (svc *MailService) BroadcastToGuild(senderID, guildID uint32, subject, body string) error {
|
|
members, err := svc.guildRepo.GetMembers(guildID, false)
|
|
if err != nil {
|
|
return fmt.Errorf("get guild members for broadcast: %w", err)
|
|
}
|
|
for _, m := range members {
|
|
if err := svc.mailRepo.SendMail(senderID, m.CharID, subject, body, 0, 0, false, false); err != nil {
|
|
return fmt.Errorf("send guild broadcast to char %d: %w", m.CharID, err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|