Documentation
¶
Overview ¶
Package notify 实现系统通知(管理员公告 + per-user 取件通知)service。
分层:通过 dao.NotifyRepository 访问数据(与其他 service 一致), 不再直接持有 *gorm.DB。
Index ¶
- Variables
- func ParseID(s string) (uint, error)
- type CreateReq
- type ListData
- type ListItem
- type ListReq
- type SMTPMailer
- type Service
- func (s *Service) Active(ctx context.Context, typ string) ([]ListItem, error)
- func (s *Service) Create(ctx context.Context, req CreateReq) (*ListItem, error)
- func (s *Service) CreateForUser(ctx context.Context, userID uint, title, content, notifyType, level string) (*model.Notify, error)
- func (s *Service) CreateForUserSimple(ctx context.Context, userID uint, title, content, notifyType, level string) error
- func (s *Service) Delete(ctx context.Context, id uint) error
- func (s *Service) EmitP2pInvite(fromName, code, note string)
- func (s *Service) EmitShareFlagged(code, reason, ownerIP string)
- func (s *Service) Get(ctx context.Context, id uint) (*ListItem, error)
- func (s *Service) List(ctx context.Context, req ListReq) (*ListData, error)
- func (s *Service) ListForUser(ctx context.Context, userID uint, page, pageSize int) (*UserNotifyListData, error)
- func (s *Service) MarkAllReadForUser(ctx context.Context, userID uint) (int64, error)
- func (s *Service) SetMailer(m *SMTPMailer)
- func (s *Service) SetWebhookURL(url string)
- func (s *Service) UnreadCountForUser(ctx context.Context, userID uint) (int64, error)
- func (s *Service) Update(ctx context.Context, req UpdateReq) error
- type UpdateReq
- type UserEmailLookup
- type UserNotifyItem
- type UserNotifyListData
Constants ¶
This section is empty.
Variables ¶
var ( ErrNotifyNotFound = errors.New("notify not found") ErrInvalidParam = errors.New("invalid parameter") )
错误
Functions ¶
Types ¶
type CreateReq ¶
type CreateReq struct {
Title string
Content string
Type string
Level string
Status int
StartAt *time.Time
EndAt *time.Time
AuthorID uint
}
CreateReq 创建请求
type ListData ¶
type ListData struct {
Items []ListItem `json:"items"`
Total int64 `json:"total"`
Page int `json:"page"`
PageSize int `json:"page_size"`
}
ListData 列表数据
type ListItem ¶
type ListItem struct {
ID uint `json:"id"`
Title string `json:"title"`
Content string `json:"content"`
Type string `json:"type"`
Level string `json:"level"`
Status int `json:"status"`
StartAt *time.Time `json:"start_at,omitempty"`
EndAt *time.Time `json:"end_at,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
ListItem 列表项
type SMTPMailer ¶
type SMTPMailer struct {
// contains filtered or unexported fields
}
SMTPMailer 邮件发送器(notify service 持有,可选)
func NewSMTPMailer ¶
func NewSMTPMailer(host string, port int, username, password, from string, lookup UserEmailLookup) *SMTPMailer
NewSMTPMailer 构建邮件发送器(host 为空视为禁用;from 缺省取 username)。 lookup 为 nil 时 SendToUser 直接跳过(TestSMTPNow 等只用 SendTo 的场景)。
func (*SMTPMailer) SendTo ¶
func (m *SMTPMailer) SendTo(to, subject, body string) error
SendTo 导出发信(管理端 SMTP 测试端点用;mail 未启用时报错)
func (*SMTPMailer) SendToUser ¶
func (m *SMTPMailer) SendToUser(userID uint, subject, body string)
SendToUser 给指定用户发邮件(用户未登记邮箱时静默跳过)。 调用方以 goroutine fire-and-forget 使用。
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service 通知 service
func (*Service) Active ¶
Active 当前活跃通知(公开 API,供全站横幅)。 仅广播(target_user_id 空/0):定向站内信绝不能从无认证端点泄露, 否则取件码/投递详情会广播给匿名访客;定向通知走 /api/v1/notifies/mine。
func (*Service) CreateForUser ¶
func (s *Service) CreateForUser(ctx context.Context, userID uint, title, content, notifyType, level string) (*model.Notify, error)
CreateForUser 创建一条定向通知(owner 取件通知用)
func (*Service) CreateForUserSimple ¶
func (s *Service) CreateForUserSimple(ctx context.Context, userID uint, title, content, notifyType, level string) error
CreateForUserSimple 简化版(返回 error,匹配 share.NotifyServiceInterface)
func (*Service) EmitP2pInvite ¶ added in v0.21.0
EmitP2pInvite 设备直传邀请到达 webhook 推送(P3;best-effort,未配置 webhook 即短路)。 邀请面向 admin(收件箱在管理页轮询呈现),此事件供运维侧及时感知。
func (*Service) EmitShareFlagged ¶
EmitShareFlagged 分享命中审核钩子事件(share.flagged)——结构化 webhook 推送, 供外挂自动处置(如拉取后调管理端禁用接口)。异步推送,失败静默记日志。
func (*Service) ListForUser ¶
func (s *Service) ListForUser(ctx context.Context, userID uint, page, pageSize int) (*UserNotifyListData, error)
ListForUser 列出某用户的通知(含广播 + 定向给该用户)
func (*Service) MarkAllReadForUser ¶
MarkAllReadForUser 标记某用户所有通知为已读(只更新 read_at IS NULL 的)
func (*Service) SetMailer ¶
func (s *Service) SetMailer(m *SMTPMailer)
SetMailer 注入邮件发送器(bootstrap 从 notify.smtp 段构建;nil = 禁用)
运行期热重建会调用 ⇒ 必须原子换指针(见 Service.mailer 的说明)。
func (*Service) SetWebhookURL ¶
SetWebhookURL 注入 Webhook 推送地址(bootstrap 从 notify.webhook.url / PB_WEBHOOK_URL 注入; 空串 = 禁用)。推送失败只记日志,绝不影响站内信主流程。
func (*Service) UnreadCountForUser ¶
UnreadCountForUser 某用户未读数
type UpdateReq ¶
type UpdateReq struct {
ID uint
Title *string
Content *string
Type *string
Level *string
Status *int
StartAt *time.Time
EndAt *time.Time
}
UpdateReq 更新请求
type UserEmailLookup ¶ added in v0.16.0
UserEmailLookup 用户邮箱查询窄接口(SendToUser 用;装配点注入 dao 适配器, 本包不直依 repo——transport 等消费方传 nil 表示仅用 SendTo 直发模式)。
type UserNotifyItem ¶
type UserNotifyItem struct {
ID uint `json:"id"`
Title string `json:"title"`
Content string `json:"content"`
Type string `json:"type"`
Level string `json:"level"`
ReadAt *time.Time `json:"read_at,omitempty"`
CreatedAt time.Time `json:"created_at"`
IsRead bool `json:"is_read"`
}
UserNotifyItem 用户通知项
type UserNotifyListData ¶
type UserNotifyListData struct {
Items []UserNotifyItem `json:"items"`
Total int64 `json:"total"`
Unread int64 `json:"unread"`
Page int `json:"page"`
PageSize int `json:"page_size"`
TotalPages int64 `json:"total_pages"`
}
UserNotifyListData 用户通知列表