Help us shape the future of Go! Take the 2026 Go Developer Survey

notify

package
v0.32.34 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 30, 2026 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Overview

Package notify 实现系统通知(管理员公告 + per-user 取件通知)service。

分层:通过 dao.NotifyRepository 访问数据(与其他 service 一致), 不再直接持有 *gorm.DB。

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotifyNotFound = errors.New("notify not found")
	ErrInvalidParam   = errors.New("invalid parameter")
)

错误

Functions

func ParseID

func ParseID(s string) (uint, error)

ParseID 解析 path 中的 id

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 ListReq

type ListReq struct {
	Page     int
	PageSize int
	Type     string
	Level    string
	Status   *int
}

ListReq 列表请求

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) Enabled

func (m *SMTPMailer) Enabled() bool

Enabled 是否已配置

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 NewService

func NewService(gormDB *gorm.DB) *Service

NewService 创建 service(repo 构造注入,去全局化终态)

func (*Service) Active

func (s *Service) Active(ctx context.Context, typ string) ([]ListItem, error)

Active 当前活跃通知(公开 API,供全站横幅)。 仅广播(target_user_id 空/0):定向站内信绝不能从无认证端点泄露, 否则取件码/投递详情会广播给匿名访客;定向通知走 /api/v1/notifies/mine。

func (*Service) Create

func (s *Service) Create(ctx context.Context, req CreateReq) (*ListItem, error)

Create 创建

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) Delete

func (s *Service) Delete(ctx context.Context, id uint) error

Delete 删除

func (*Service) EmitP2pInvite added in v0.21.0

func (s *Service) EmitP2pInvite(fromName, code, note string)

EmitP2pInvite 设备直传邀请到达 webhook 推送(P3;best-effort,未配置 webhook 即短路)。 邀请面向 admin(收件箱在管理页轮询呈现),此事件供运维侧及时感知。

func (*Service) EmitShareFlagged

func (s *Service) EmitShareFlagged(code, reason, ownerIP string)

EmitShareFlagged 分享命中审核钩子事件(share.flagged)——结构化 webhook 推送, 供外挂自动处置(如拉取后调管理端禁用接口)。异步推送,失败静默记日志。

func (*Service) Get

func (s *Service) Get(ctx context.Context, id uint) (*ListItem, error)

Get 单条

func (*Service) List

func (s *Service) List(ctx context.Context, req ListReq) (*ListData, error)

List 列表

func (*Service) ListForUser

func (s *Service) ListForUser(ctx context.Context, userID uint, page, pageSize int) (*UserNotifyListData, error)

ListForUser 列出某用户的通知(含广播 + 定向给该用户)

func (*Service) MarkAllReadForUser

func (s *Service) MarkAllReadForUser(ctx context.Context, userID uint) (int64, error)

MarkAllReadForUser 标记某用户所有通知为已读(只更新 read_at IS NULL 的)

func (*Service) SetMailer

func (s *Service) SetMailer(m *SMTPMailer)

SetMailer 注入邮件发送器(bootstrap 从 notify.smtp 段构建;nil = 禁用)

func (*Service) SetWebhookURL

func (s *Service) SetWebhookURL(url string)

SetWebhookURL 注入 Webhook 推送地址(bootstrap 从 notify.webhook.url / PB_WEBHOOK_URL 注入; 空串 = 禁用)。推送失败只记日志,绝不影响站内信主流程。

func (*Service) UnreadCountForUser

func (s *Service) UnreadCountForUser(ctx context.Context, userID uint) (int64, error)

UnreadCountForUser 某用户未读数

func (*Service) Update

func (s *Service) Update(ctx context.Context, req UpdateReq) error

Update 更新

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

type UserEmailLookup interface {
	EmailByID(ctx context.Context, id uint) (string, error)
}

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 用户通知列表

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL