Create & Init Project...

This commit is contained in:
2019-04-22 18:49:16 +08:00
commit fc4fa37393
25440 changed files with 4054998 additions and 0 deletions

View File

@ -0,0 +1,41 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = ["conf.go"],
importpath = "go-common/app/service/bbq/notice-service/internal/conf",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//library/cache/redis:go_default_library",
"//library/conf:go_default_library",
"//library/database/sql:go_default_library",
"//library/ecode/tip:go_default_library",
"//library/log:go_default_library",
"//library/log/infoc:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//library/net/http/blademaster/middleware/verify:go_default_library",
"//library/net/rpc/warden:go_default_library",
"//library/net/trace:go_default_library",
"//vendor/github.com/BurntSushi/toml:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@ -0,0 +1,106 @@
package conf
import (
"errors"
"flag"
"go-common/library/cache/redis"
"go-common/library/conf"
"go-common/library/database/sql"
ecode "go-common/library/ecode/tip"
"go-common/library/log"
"go-common/library/log/infoc"
bm "go-common/library/net/http/blademaster"
"go-common/library/net/http/blademaster/middleware/verify"
"go-common/library/net/rpc/warden"
"go-common/library/net/trace"
"github.com/BurntSushi/toml"
)
var (
confPath string
client *conf.Client
// Conf config
Conf = &Config{}
)
// Config .
type Config struct {
Log *log.Config
BM *bm.ServerConfig
Verify *verify.Config
Tracer *trace.Config
MySQL *sql.Config
Redis *redis.Config
Ecode *ecode.Config
GRPC *warden.ServerConfig
GRPCClient map[string]*GRPCClientConfig
L1PushStrategy map[string]*PushStrategy
L2PushStrategy map[string]*PushStrategy
Infoc *infoc.Config
}
// GRPCClientConfig .
type GRPCClientConfig struct {
WardenConf *warden.ClientConfig
Addr string
}
// PushStrategy .
type PushStrategy struct {
T int
A int
B int
}
func init() {
flag.StringVar(&confPath, "conf", "", "default config path")
}
// Init init conf
func Init() error {
if confPath != "" {
return local()
}
return remote()
}
func local() (err error) {
_, err = toml.DecodeFile(confPath, &Conf)
return
}
func remote() (err error) {
if client, err = conf.New(); err != nil {
return
}
if err = load(); err != nil {
return
}
go func() {
for range client.Event() {
log.Info("config reload")
if load() != nil {
log.Error("config reload error (%v)", err)
}
}
}()
return
}
func load() (err error) {
var (
s string
ok bool
tmpConf *Config
)
if s, ok = client.Toml2(); !ok {
return errors.New("load config center error")
}
if _, err = toml.Decode(s, &tmpConf); err != nil {
return errors.New("could not decode config")
}
*Conf = *tmpConf
return
}

View File

@ -0,0 +1,41 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"push.go",
"user.go",
],
importpath = "go-common/app/service/bbq/notice-service/internal/dao",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/service/bbq/notice-service/api/v1:go_default_library",
"//app/service/bbq/notice-service/internal/conf:go_default_library",
"//app/service/bbq/push/api/grpc/v1:go_default_library",
"//library/cache/redis:go_default_library",
"//library/database/sql:go_default_library",
"//library/log:go_default_library",
"//library/net/rpc/warden:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@ -0,0 +1,184 @@
package dao
import (
"context"
"fmt"
"go-common/app/service/bbq/notice-service/api/v1"
"go-common/app/service/bbq/notice-service/internal/conf"
push "go-common/app/service/bbq/push/api/grpc/v1"
"go-common/library/cache/redis"
xsql "go-common/library/database/sql"
"go-common/library/log"
"go-common/library/net/rpc/warden"
)
const (
_listSQL = "select id, mid, action_mid, svid, notice_type, title, text, jump_url, biz_type, biz_id, ctime from notice_%02d where mid = ? and notice_type = ? and id < ? order by id desc limit %d"
_insertSQL = "insert into notice_%02d (mid, action_mid, svid, notice_type, title, text, jump_url, biz_type, biz_id) values (?,?,?,?,?,?,?,?,?)"
_noticeLen = 10
_redisUnreadKey = "notice:unread:%d"
_redisExpireTime = 7776000 // 90days
)
// Dao dao
type Dao struct {
c *conf.Config
db *xsql.DB
redis *redis.Pool
pushClient push.PushClient
}
// New init mysql db
func New(c *conf.Config) (dao *Dao) {
dao = &Dao{
c: c,
db: xsql.NewMySQL(c.MySQL),
redis: redis.NewPool(c.Redis),
pushClient: newPushClient(c.GRPCClient["push"]),
}
return
}
// Close close the resource.
func (d *Dao) Close() {
d.db.Close()
}
// Ping dao ping
func (d *Dao) Ping(ctx context.Context) error {
// TODO: add mc,redis... if you use
return d.db.Ping(ctx)
}
func getTableIndex(id int64) int64 {
return id % 100
}
// newPushClient .
func newPushClient(cfg *conf.GRPCClientConfig) push.PushClient {
cc, err := warden.NewClient(cfg.WardenConf).Dial(context.Background(), cfg.Addr)
if err != nil {
panic(err)
}
return push.NewPushClient(cc)
}
// ListNotices 获取通知列表
func (d *Dao) ListNotices(ctx context.Context, mid, cursorID int64, noticeType int32) (list []*v1.NoticeBase, err error) {
querySQL := fmt.Sprintf(_listSQL, getTableIndex(mid), _noticeLen)
log.V(1).Infov(ctx, log.KV("mid", mid), log.KV("mid", mid), log.KV("notice_type", noticeType), log.KV("cursor_id", cursorID), log.KV("sql", querySQL))
rows, err := d.db.Query(ctx, querySQL, mid, noticeType, cursorID)
if err != nil {
log.Errorv(ctx, log.KV("log", "query mysql notice list fail"), log.KV("sql", querySQL), log.KV("mid", mid), log.KV("biz_type", noticeType), log.KV("cursor_id", cursorID))
return
}
defer rows.Close()
for rows.Next() {
var notice v1.NoticeBase
if err = rows.Scan(&notice.Id, &notice.Mid, &notice.ActionMid, &notice.SvId, &notice.NoticeType, &notice.Title, &notice.Text, &notice.JumpUrl, &notice.BizType, &notice.BizId, &notice.NoticeTime); err != nil {
log.Errorv(ctx, log.KV("log", "scan mysql notice list fail"), log.KV("sql", querySQL), log.KV("mid", mid), log.KV("biz_type", noticeType), log.KV("mid", mid), log.KV("cursor_id", cursorID))
return
}
list = append(list, &notice)
}
// 只要用户读取数据,即清理未读数
conn := d.redis.Get(ctx)
defer conn.Close()
redisKey := fmt.Sprintf(_redisUnreadKey, mid)
if _, tmpErr := conn.Do("HSET", redisKey, noticeType, 0); tmpErr != nil {
log.Warnv(ctx, log.KV("log", "clear unread info redis fail: key="+redisKey))
}
log.V(1).Infov(ctx, log.KV("req_size", _noticeLen), log.KV("rsp_size", len(list)))
return
}
// CreateNotice 创建通知
func (d *Dao) CreateNotice(ctx context.Context, notice *v1.NoticeBase) (id int64, err error) {
querySQL := fmt.Sprintf(_insertSQL, getTableIndex(notice.Mid))
res, err := d.db.Exec(ctx, querySQL, notice.Mid, notice.ActionMid, notice.SvId, notice.NoticeType, notice.Title, notice.Text, notice.JumpUrl, notice.BizType, notice.BizId)
if err != nil {
log.Errorv(ctx, log.KV("log", "exec mysql fail: create notice"), log.KV("sql", querySQL))
return
}
id, _ = res.LastInsertId()
return
}
// IncreaseUnread 增加未读
func (d *Dao) IncreaseUnread(ctx context.Context, mid int64, noticeType int32, num int64) (err error) {
conn := d.redis.Get(ctx)
defer conn.Close()
redisKey := fmt.Sprintf(_redisUnreadKey, mid)
expireResult, _ := redis.Int(conn.Do("EXPIRE", redisKey, _redisExpireTime))
if expireResult == 0 {
log.Infov(ctx, log.KV("log", "expire fail: key="+redisKey))
}
_, err = conn.Do("HINCRBY", redisKey, noticeType, num)
if err != nil {
log.Errorv(ctx, log.KV("log", "HINCRBY notice unread fail: err="+err.Error()))
return
}
log.V(1).Infov(ctx, log.KV("log", "hincrby notice unread : key="+redisKey), log.KV("notice_type", noticeType), log.KV("num", num))
return
}
// ClearUnread 清理未读
func (d *Dao) ClearUnread(ctx context.Context, mid int64, noticeType int32) (err error) {
conn := d.redis.Get(ctx)
defer conn.Close()
redisKey := fmt.Sprintf(_redisUnreadKey, mid)
expireResult, _ := redis.Int(conn.Do("EXPIRE", redisKey, _redisExpireTime))
if expireResult == 0 {
log.Infov(ctx, log.KV("log", "expire fail and return: key="+redisKey))
return
}
_, err = conn.Do("HSET", redisKey, noticeType, 0)
if err != nil {
log.Errorv(ctx, log.KV("log", "HSET notice unread fail: err="+err.Error()))
return
}
log.V(1).Infov(ctx, log.KV("log", "HSET clear notice unread : key="+redisKey), log.KV("notice_type", noticeType))
// 清理推送用户
err = d.ClearPushActionMid(ctx, mid, noticeType)
if err != nil {
log.Errorv(ctx, log.KV("log", "ClearPushActionMid fail: err="+err.Error()))
return
}
return
}
// GetUnreadInfo 获取未读情况
func (d *Dao) GetUnreadInfo(ctx context.Context, mid int64) (list []*v1.UnreadItem, err error) {
redisKey := fmt.Sprintf(_redisUnreadKey, mid)
conn := d.redis.Get(ctx)
defer conn.Close()
expireResult, _ := redis.Int(conn.Do("EXPIRE", redisKey, _redisExpireTime))
if expireResult == 0 {
log.V(1).Infov(ctx, log.KV("log", "expire fail: key="+redisKey))
return
}
result, err := redis.Int64s(conn.Do("HMGET", redisKey, 1, 2, 3, 4))
if err != nil {
log.Errorv(ctx, log.KV("log", "hmget notice unread fail: err="+err.Error()))
return
}
for i, val := range result {
var item v1.UnreadItem
item.NoticeType = int32(i + 1)
item.UnreadNum = val
list = append(list, &item)
}
return
}

View File

@ -0,0 +1,225 @@
package dao
import (
"context"
"database/sql"
"fmt"
"strconv"
"strings"
"time"
"go-common/app/service/bbq/notice-service/api/v1"
push "go-common/app/service/bbq/push/api/grpc/v1"
"go-common/library/cache/redis"
"go-common/library/log"
)
const (
_queryUserPushDev = "select `reg_id`, `sdk`, `platform` from `user_push_device` where `mid` = ? and `state` = 0 order by `last_login_time` desc limit 1;"
_queryUserName = "select `uname` from `user_base` where mid in %s;"
)
// PushNotice .
func (d *Dao) PushNotice(c context.Context, req *push.NotificationRequest) (result []*push.PushResult, err error) {
resp, err := d.pushClient.AsyncNotification(c, req)
if err != nil {
log.Errorv(c, log.KV("log", "push notification error"), log.KV("error", err))
return
}
result = resp.Result
for _, item := range result {
if item.Error != nil {
log.Errorv(c, log.KV("log", "push device notifiaction error"), log.KV("error", item.Error))
}
}
return
}
// PushMessage .
func (d *Dao) PushMessage(c context.Context, req *push.MessageRequest) (result []*push.PushResult, err error) {
resp, err := d.pushClient.AsyncMessage(c, req)
if err != nil {
log.Errorv(c, log.KV("log", "push message error"), log.KV("error", err))
return
}
result = resp.Result
for _, item := range result {
if item.Error != nil {
log.Errorv(c, log.KV("log", "push device message error"), log.KV("error", item.Error))
}
}
return nil, err
}
// FetchPushDev .
func (d *Dao) FetchPushDev(c context.Context, mid int64) (result *push.Device, err error) {
result = &push.Device{}
err = d.db.QueryRow(c, _queryUserPushDev, mid).Scan(&result.RegisterID, &result.SDK, &result.Platform)
if err == sql.ErrNoRows {
err = nil
log.Warnw(c, "log", "no row in push", "sql", _queryUserPushDev, "mid", mid)
}
return
}
// IncrDailyPushCount .
func (d *Dao) IncrDailyPushCount(c context.Context, mid int64) (count int, err error) {
dt := time.Now().Format("20060102")
key := fmt.Sprintf("bbq:push:u:%d:%s:count", mid, dt)
conn := d.redis.Get(c)
defer conn.Close()
count, err = redis.Int(conn.Do("INCR", key, 1))
if err != nil {
return
}
_, err = conn.Do("EXPIRE", 86400)
return
}
// IncrHourPushAction .
func (d *Dao) IncrHourPushAction(c context.Context, mid int64, noticeType int32, t int) (count int, err error) {
dt := time.Now().Format("2006010215")
key := fmt.Sprintf("bbq:push:u:%d:%s:action:%d", mid, dt, noticeType)
conn := d.redis.Get(c)
defer conn.Close()
count, err = redis.Int(conn.Do("INCR", key))
if err != nil {
return
}
_, err = conn.Do("EXPIRE", t)
return
}
// ClearHourPushAction .
func (d *Dao) ClearHourPushAction(c context.Context, mid int64, noticeType int32) error {
dt := time.Now().Format("2006010215")
key := fmt.Sprintf("bbq:push:u:%d:%s:action:%d", mid, dt, noticeType)
conn := d.redis.Get(c)
defer conn.Close()
_, err := conn.Do("DEL", key)
return err
}
// SetPushActionMid .
func (d *Dao) SetPushActionMid(c context.Context, mid int64, actionMid int64, noticeType int32) error {
dt := time.Now().Format("2006010215")
key := fmt.Sprintf("bbq:v1:push:u:%d:%s:action:%d", mid, dt, noticeType)
conn := d.redis.Get(c)
defer conn.Close()
values, _ := redis.Values(conn.Do("HGETALL", key))
if len(values) >= 4 {
return nil
}
_, err := conn.Do("HMSET", key, actionMid, actionMid)
return err
}
// GetPushActionMid .
func (d *Dao) GetPushActionMid(c context.Context, mid int64, noticeType int32) ([]int64, error) {
dt := time.Now().Format("2006010215")
key := fmt.Sprintf("bbq:v1:push:u:%d:%s:action:%d", mid, dt, noticeType)
conn := d.redis.Get(c)
defer conn.Close()
m, err := redis.Int64Map(conn.Do("HGETALL", key))
if err != nil {
return nil, err
}
result := make([]int64, 0)
for _, v := range m {
result = append(result, v)
}
return result, nil
}
// ClearPushActionMid .
func (d *Dao) ClearPushActionMid(c context.Context, mid int64, noticeType int32) error {
conn := d.redis.Get(c)
defer conn.Close()
dt := time.Now().Format("2006010215")
key := fmt.Sprintf("bbq:v1:push:u:%d:%s:action:%d", mid, dt, noticeType)
_, err := conn.Do("DEL", key)
return err
}
// GetUserName .
func (d *Dao) GetUserName(c context.Context, midList []int64, count int) (names []string, err error) {
mids := []string{}
list := midList
if len(midList) > count {
list = midList[:count]
}
for _, v := range list {
mids = append(mids, strconv.Itoa(int(v)))
}
where := "(" + strings.Join(mids, ",") + ")"
row, err := d.db.Query(c, fmt.Sprintf(_queryUserName, where))
if err != nil {
return
}
for row.Next() {
n := ""
err = row.Scan(&n)
if err != nil {
return
}
names = append(names, n)
}
return
}
// FetchUserPushDev .
func (d *Dao) FetchUserPushDev(c context.Context, mid int64, buvid string) (result *v1.UserPushDev, err error) {
querySQL := "select `id` from `user_push_device` where `mid` = ? and `buvid` = ?"
row := d.db.QueryRow(c, querySQL, mid, buvid)
result = &v1.UserPushDev{}
row.Scan(&result.Id)
return
}
// InsertUserPushDev .
func (d *Dao) InsertUserPushDev(c context.Context, req *v1.UserPushDev) (int64, error) {
_insertUserPushDev := "insert into `user_push_device` (`mid`, `reg_id`, `buvid`, `sdk`, `platform`) values (?, ?, ?, ?, ?);"
result, err := d.db.Exec(c, _insertUserPushDev, req.Mid, req.RegisterId, req.Buvid, req.Sdk, req.Platform)
if err != nil {
return 0, err
}
return result.LastInsertId()
}
// UpdateUserPushDev .
func (d *Dao) UpdateUserPushDev(c context.Context, req *v1.UserPushDev) (int64, error) {
_updateUserPushDev := "update `user_push_device` set `reg_id`=?,`sdk`=?,`platform`=?,`state`=?,`last_login_time`=now() where `mid`=? and `buvid`=?"
result, err := d.db.Exec(c, _updateUserPushDev, req.RegisterId, req.Sdk, req.Platform, req.State, req.Mid, req.Buvid)
if err != nil {
return 0, err
}
return result.RowsAffected()
}
// DeleteUserPushDev .
func (d *Dao) DeleteUserPushDev(c context.Context, req *v1.UserPushDev) (int64, error) {
_updateUserPushDev := "update `user_push_device` set `state`=1 where `mid`=? and `buvid`=?"
result, err := d.db.Exec(c, _updateUserPushDev, req.Mid, req.Buvid)
if err != nil {
return 0, err
}
return result.RowsAffected()
}

View File

@ -0,0 +1,16 @@
package dao
import (
"context"
)
const (
_queryUserFansNum = "select `fan_total` from `user_statistics` where mid = ?;"
)
// FetchUserFansNum .
func (d *Dao) FetchUserFansNum(c context.Context, mid int64) (num int, err error) {
row := d.db.QueryRow(c, _queryUserFansNum, mid)
err = row.Scan(&num)
return
}

View File

@ -0,0 +1,31 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = [
"model.go",
"push.go",
],
importpath = "go-common/app/service/bbq/notice-service/internal/model",
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@ -0,0 +1 @@
package model

View File

@ -0,0 +1,34 @@
package model
// 推送消息类型
const (
NoticeTypeLike = 1
NoticeTypeComment = 2
NoticeTypeFan = 3
NoticeTypeSysMsg = 4
)
// 推送业务类型
const (
NoticeBizTypeSv = 1
NoticeBizTypeComment = 2
NoticeBizTypeUser = 3
NoticeBizTypeCmsReview = 4
)
// 推送文案
const (
PushMsgVideoLike = "%s%s赞了你的作品"
PushMsgVideoComment = "%s%s评论了你的作品"
PushMsgCommentLike = "%s%s赞了你的评论"
PushMsgCommentReply = "%s%s回复了你的评论"
PushMsgFollow = "%s%s关注了你"
)
// 推送跳转schema
const (
PushSchemaVideo = "qing://videoplayer?svid=%d"
PushSchemaComment = "qing://commentdetail?svid=%d&rootid=%s"
PushSchemaUser = "qing://profile?mid=%d"
PushSchemaNotice = "qing://notification?type=%d"
)

View File

@ -0,0 +1,33 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = ["server.go"],
importpath = "go-common/app/service/bbq/notice-service/internal/server/grpc",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/service/bbq/notice-service/api/v1:go_default_library",
"//app/service/bbq/notice-service/internal/service:go_default_library",
"//library/net/rpc/warden:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@ -0,0 +1,18 @@
package grpc
import (
pb "go-common/app/service/bbq/notice-service/api/v1"
"go-common/app/service/bbq/notice-service/internal/service"
"go-common/library/net/rpc/warden"
)
// New new warden rpc server
func New(c *warden.ServerConfig, svc *service.Service) *warden.Server {
ws := warden.NewServer(c)
pb.RegisterNoticeServer(ws.Server(), svc)
ws, err := ws.Start()
if err != nil {
panic(err)
}
return ws
}

View File

@ -0,0 +1,37 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = ["http.go"],
importpath = "go-common/app/service/bbq/notice-service/internal/server/http",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/service/bbq/notice-service/api/v1:go_default_library",
"//app/service/bbq/notice-service/internal/conf:go_default_library",
"//app/service/bbq/notice-service/internal/service:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//library/net/http/blademaster/middleware/verify:go_default_library",
"//vendor/github.com/pkg/errors:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@ -0,0 +1,106 @@
package http
import (
"net/http"
"github.com/pkg/errors"
"go-common/library/log"
bm "go-common/library/net/http/blademaster"
"go-common/library/net/http/blademaster/middleware/verify"
"go-common/app/service/bbq/notice-service/api/v1"
"go-common/app/service/bbq/notice-service/internal/conf"
"go-common/app/service/bbq/notice-service/internal/service"
)
var (
vfy *verify.Verify
svc *service.Service
)
// Init init
func Init(c *conf.Config, s *service.Service) {
svc = s
vfy = verify.New(c.Verify)
engine := bm.DefaultServer(c.BM)
route(engine)
if err := engine.Start(); err != nil {
log.Error("bm Start error(%v)", err)
panic(err)
}
}
func route(e *bm.Engine) {
e.Ping(ping)
e.Register(register)
g := e.Group("/x/notice-service")
{
g.GET("/notice/list", listNotices)
g.GET("/notice/unread", unreadInfo)
g.POST("/notice/create", createNotice)
g.POST("/push/login", login)
g.POST("/push/logout", logout)
}
}
func ping(ctx *bm.Context) {
if err := svc.Ping(ctx); err != nil {
log.Error("ping error(%v)", err)
ctx.AbortWithStatus(http.StatusServiceUnavailable)
}
}
func register(c *bm.Context) {
c.JSON(map[string]interface{}{}, nil)
}
func createNotice(c *bm.Context) {
arg := &v1.NoticeBase{}
if err := c.Bind(arg); err != nil {
errors.Wrap(err, "参数验证失败")
return
}
resp, err := svc.CreateNotice(c, arg)
c.JSON(resp, err)
}
func listNotices(c *bm.Context) {
arg := &v1.ListNoticesReq{}
if err := c.Bind(arg); err != nil {
errors.Wrap(err, "参数验证失败")
return
}
resp, err := svc.ListNotices(c, arg)
c.JSON(resp, err)
}
func unreadInfo(c *bm.Context) {
arg := &v1.GetUnreadInfoRequest{}
if err := c.Bind(arg); err != nil {
errors.Wrap(err, "参数验证失败")
return
}
resp, err := svc.GetUnreadInfo(c, arg)
c.JSON(resp, err)
}
func login(c *bm.Context) {
arg := &v1.UserPushDev{}
if err := c.Bind(arg); err != nil {
errors.Wrap(err, "参数验证失败")
return
}
resp, err := svc.PushLogin(c, arg)
c.JSON(resp, err)
}
func logout(c *bm.Context) {
arg := &v1.UserPushDev{}
if err := c.Bind(arg); err != nil {
errors.Wrap(err, "参数验证失败")
return
}
resp, err := svc.PushLogout(c, arg)
c.JSON(resp, err)
}

View File

@ -0,0 +1,43 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = [
"push.go",
"service.go",
],
importpath = "go-common/app/service/bbq/notice-service/internal/service",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/service/bbq/notice-service/api/v1:go_default_library",
"//app/service/bbq/notice-service/internal/conf:go_default_library",
"//app/service/bbq/notice-service/internal/dao:go_default_library",
"//app/service/bbq/notice-service/internal/model:go_default_library",
"//app/service/bbq/push/api/grpc/v1:go_default_library",
"//library/log:go_default_library",
"//library/log/infoc:go_default_library",
"//library/net/trace:go_default_library",
"//vendor/github.com/json-iterator/go:go_default_library",
"@io_bazel_rules_go//proto/wkt:empty_go_proto",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@ -0,0 +1,210 @@
package service
import (
"context"
"fmt"
"strings"
"time"
"github.com/json-iterator/go"
"go-common/app/service/bbq/notice-service/api/v1"
"go-common/app/service/bbq/notice-service/internal/conf"
"go-common/app/service/bbq/notice-service/internal/model"
push "go-common/app/service/bbq/push/api/grpc/v1"
"go-common/library/log"
"go-common/library/net/trace"
)
func (s *Service) needPush(c context.Context, notice *v1.NoticeBase) bool {
// TODO: 获取粉丝数量
F, err := s.dao.FetchUserFansNum(c, notice.Mid)
if err != nil {
log.Errorv(c, log.KV("log", "FetchUserFansNum error"), log.KV("error", err))
}
var strategyMap *map[string]*conf.PushStrategy
if F <= 1000 {
strategyMap = &s.c.L1PushStrategy
} else {
strategyMap = &s.c.L2PushStrategy
}
var strategy *conf.PushStrategy
switch notice.NoticeType {
case model.NoticeTypeLike:
strategy = (*strategyMap)["like"]
case model.NoticeTypeComment:
strategy = (*strategyMap)["comment"]
case model.NoticeTypeFan:
strategy = (*strategyMap)["follow"]
case model.NoticeTypeSysMsg:
strategy = (*strategyMap)["sysmsg"]
}
// A = -1 and B = -1 无限制
if strategy.A == -1 && strategy.B == -1 {
return true
}
// A = -1 触发无限制
if strategy.A != -1 {
count, err := s.dao.IncrHourPushAction(c, notice.Mid, notice.NoticeType, strategy.T)
if count < strategy.A || err != nil {
return false
}
}
// B = -1 全天无限制
if strategy.B != -1 {
count, err := s.dao.IncrDailyPushCount(c, notice.Mid)
if count > strategy.B || err != nil {
return false
}
}
// 推送触发条件T小时内触发A次且当日推送次数小于等于B
return true
}
func (s *Service) pushNotification(c context.Context, nid int64, notice *v1.NoticeBase) (err error) {
// redis push action_mid
err = s.dao.SetPushActionMid(c, notice.Mid, notice.ActionMid, notice.NoticeType)
if err != nil {
log.Errorv(c, log.KV("log", "SetPushActionMid error"), log.KV("error", err))
return
}
if !s.needPush(c, notice) {
return
}
var title, content string
switch notice.NoticeType {
case model.NoticeTypeLike:
if notice.BizType == model.NoticeBizTypeSv {
// 视频点赞
content = model.PushMsgVideoLike
} else if notice.BizType == model.NoticeBizTypeComment {
// 评论点赞
content = model.PushMsgCommentLike
}
case model.NoticeTypeComment:
if notice.BizType == model.NoticeBizTypeSv {
// 视频评论
content = model.PushMsgVideoComment
} else if notice.BizType == model.NoticeBizTypeComment {
// 评论回复
content = model.PushMsgCommentReply
}
case model.NoticeTypeFan:
// 关注
content = model.PushMsgFollow
case model.NoticeTypeSysMsg:
// 系统消息
// return s.pushMessage(c, nid, notice)
if notice.BizType == model.NoticeBizTypeCmsReview {
// 审核类通知不推送
return
}
content = notice.Text
}
// 填写内容详情
midList, err := s.dao.GetPushActionMid(c, notice.Mid, notice.NoticeType)
if err != nil || len(midList) == 0 {
log.Errorv(c, log.KV("log", "GetPushActionMid error"), log.KV("error", err))
return
}
nameList, err := s.dao.GetUserName(c, midList, 2)
if err != nil {
log.Errorv(c, log.KV("log", "GetUserName error"), log.KV("error", err))
return
}
unreadInfo, err := s.dao.GetUnreadInfo(c, notice.Mid)
if err != nil {
log.Errorv(c, log.KV("log", "GetUnreadInfo error"), log.KV("error", err))
return
}
if len(nameList) > 1 {
tmp := fmt.Sprintf("等%d人", unreadInfo[int(notice.NoticeType)-1].UnreadNum)
content = fmt.Sprintf(content, strings.Join(nameList, ","), tmp)
} else {
content = fmt.Sprintf(content, strings.Join(nameList, ","), "")
}
schema := fmt.Sprintf(model.PushSchemaNotice, notice.NoticeType)
ext := make(map[string]string)
ext["scheme"] = schema
extStr, _ := jsoniter.Marshal(ext)
dev, err := s.dao.FetchPushDev(c, notice.Mid)
if err != nil {
log.Errorv(c, log.KV("log", "FetchPushDev error"), log.KV("error", err))
return
}
dev.SendNo = nid
devs := []*push.Device{dev}
body := &push.NotificationBody{
Title: title,
Content: content,
Extra: string(extStr),
}
req := &push.NotificationRequest{
Dev: devs,
Body: body,
}
result, err := s.dao.PushNotice(c, req)
if err != nil {
log.Errorv(c, log.KV("log", "PushNotice error"), log.KV("error", err), log.KV("result", result))
return
}
err = s.dao.ClearHourPushAction(c, notice.Mid, notice.NoticeType)
if err != nil {
log.Errorv(c, log.KV("log", "hour push action clear error"), log.KV("error", err), log.KV("notice_type", notice.NoticeType))
}
// 埋点
tracer, _ := trace.FromContext(c)
s.Infoc.Info(tracer, notice.Mid, notice.Buvid, nid, notice.NoticeType, notice.BizId, notice.BizType, time.Now().Unix(), result)
return
}
// func (s *Service) pushMessage(c context.Context, nid int64, notice *v1.NoticeBase) (err error) {
// dev, err := s.dao.FetchPushDev(c, notice.Mid)
// if err != nil {
// return
// }
// dev.SendNo = nid
// devs := []*push.Device{dev}
// schema := fmt.Sprintf(model.PushSchemaNotice, notice.NoticeType)
// ext := make(map[string]string)
// ext["shcema"] = schema
// extStr, _ := jsoniter.Marshal(ext)
// body := &push.MessageBody{
// Title: notice.Title,
// Content: notice.Text,
// ContentType: "text",
// Extra: string(extStr),
// }
// req := &push.MessageRequest{
// Dev: devs,
// Body: body,
// }
// result, err := s.dao.PushMessage(c, req)
// if err != nil {
// log.Errorv(c, log.KV("log", "PushMessage error"), log.KV("error", err), log.KV("result", result))
// return
// }
// err = s.dao.ClearHourPushAction(c, notice.Mid, notice.NoticeType)
// if err != nil {
// log.Errorv(c, log.KV("log", "hour push action clear error"), log.KV("error", err), log.KV("notice_type", notice.NoticeType))
// }
// // 埋点
// tracer, _ := trace.FromContext(c)
// s.Infoc.Info(tracer, notice.Mid, notice.Buvid, nid, notice.NoticeType, notice.BizId, notice.BizType, time.Now().Unix(), result)
// return
// }

View File

@ -0,0 +1,140 @@
package service
import (
"context"
"fmt"
"time"
"go-common/app/service/bbq/notice-service/api/v1"
"go-common/app/service/bbq/notice-service/internal/conf"
"go-common/app/service/bbq/notice-service/internal/dao"
"go-common/library/log"
"go-common/library/log/infoc"
"github.com/golang/protobuf/ptypes/empty"
)
// Service struct
type Service struct {
c *conf.Config
dao *dao.Dao
Infoc *infoc.Infoc
}
// New init
func New(c *conf.Config) (s *Service) {
s = &Service{
c: c,
dao: dao.New(c),
Infoc: infoc.New(c.Infoc),
}
return s
}
// Ping Service
func (s *Service) Ping(ctx context.Context) (err error) {
return s.dao.Ping(ctx)
}
// Close Service
func (s *Service) Close() {
s.dao.Close()
}
// ListNotices 获取通知列表
func (s *Service) ListNotices(c context.Context, req *v1.ListNoticesReq) (res *v1.ListNoticesReply, err error) {
res = new(v1.ListNoticesReply)
res.Mid = req.Mid
res.List, err = s.dao.ListNotices(c, req.Mid, req.CursorId, req.NoticeType)
if err != nil {
log.Errorv(c, log.KV("log", "get list notice fail"))
return
}
// 清理未读数
s.dao.ClearUnread(c, req.Mid, req.NoticeType)
return
}
// CreateNotice 创建消息
func (s *Service) CreateNotice(c context.Context, req *v1.NoticeBase) (res *empty.Empty, err error) {
res = new(empty.Empty)
id, err := s.dao.CreateNotice(c, req)
if err != nil {
log.Warnv(c, log.KV("log", "create notice fail"))
return
}
// 未读数+1
err = s.dao.IncreaseUnread(c, req.Mid, req.NoticeType, 1)
if err != nil {
log.Warnv(c, log.KV("log", "increase notice unread fail: req="+req.String()))
err = nil
}
// TODO:推送
s.pushNotification(c, id, req)
return
}
// GetUnreadInfo 获取未读情况
func (s *Service) GetUnreadInfo(ctx context.Context, req *v1.GetUnreadInfoRequest) (res *v1.UnreadInfo, err error) {
res = new(v1.UnreadInfo)
res.List, err = s.dao.GetUnreadInfo(ctx, req.Mid)
if err != nil {
log.Warnv(ctx, log.KV("log", "get notice unread info fail"), log.KV("req", req.String()))
return
}
log.V(1).Infov(ctx, log.KV("log", "get unread info: res="+res.String()))
return
}
// PushCallback 推送回调
func (s *Service) PushCallback(ctx context.Context, req *v1.PushCallbackRequest) (res *empty.Empty, err error) {
res = new(empty.Empty)
// 埋点
s.Infoc.Info(req.Tid, req.Mid, req.Buvid, req.Nid, "", "", "", time.Now().Unix(), "")
return
}
// PushLogout 推送回调
func (s *Service) PushLogout(ctx context.Context, req *v1.UserPushDev) (res *empty.Empty, err error) {
res = new(empty.Empty)
var rowsAffected int64
if rowsAffected, err = s.dao.DeleteUserPushDev(ctx, req); err != nil {
log.Errorv(ctx, log.KV("log", fmt.Sprintf("delete user push dev fail: req=%s", req.String())))
return
}
if rowsAffected == 0 {
log.Warnv(ctx, log.KV("log", fmt.Sprintf("delete user push dev fail due to affected rows is zero: req=%s", req.String())))
}
return
}
// PushLogin login
func (s *Service) PushLogin(ctx context.Context, req *v1.UserPushDev) (res *empty.Empty, err error) {
res = new(empty.Empty)
// 获取数据库是否存在当前的mid & buvid
dev, err := s.dao.FetchUserPushDev(ctx, req.Mid, req.Buvid)
if err != nil {
log.Errorv(ctx, log.KV("log", fmt.Sprintf("fetch user push dev fail: req=%s, err=%s", req.String(), err.Error())))
return
}
// 插入user_push_device
if dev.Id == 0 {
if _, err = s.dao.InsertUserPushDev(ctx, req); err != nil {
log.Errorv(ctx, log.KV("log", fmt.Sprintf("insert user push dev fail: req=%s", req.String())))
return
}
} else {
// 更新user_push_device
if _, err = s.dao.UpdateUserPushDev(ctx, req); err != nil {
log.Errorv(ctx, log.KV("log", fmt.Sprintf("insert user push dev fail: req=%s", req.String())))
return
}
}
return
}