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,48 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = ["message_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/job/main/reply/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = ["message.go"],
importpath = "go-common/app/job/main/reply/dao/message",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/job/main/reply/conf:go_default_library",
"//library/ecode:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//library/xstr: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,175 @@
package message
import (
"context"
"fmt"
"net/url"
"strconv"
"strings"
"time"
"go-common/app/job/main/reply/conf"
"go-common/library/ecode"
"go-common/library/log"
xhttp "go-common/library/net/http/blademaster"
"go-common/library/xstr"
)
const (
// 1 main site related
// 1_1 reply related
// 1_2 at related
// 1_3 report related
//_codeReplyGet = "1_1_1"
_codeReplyDelete = "1_1_2"
_codeReplyLike = "1_1_3"
_codeAt = "1_2_1"
_codeReport = "1_3_1"
_dataTypeReply = 1
_dataTypeAt = 2
_dataTypeLike = 3
_dataTypeSystem = 4
_notifyTypeCnt = 2
)
// Dao message dao.
type Dao struct {
httpCli *xhttp.Client
apiURL string
}
// NewMessageDao new a message dao and return.
func NewMessageDao(c *conf.Config) *Dao {
return &Dao{
httpCli: xhttp.NewClient(c.HTTPClient),
apiURL: c.Host.Message + "/api/notify/send.user.notify.do",
}
}
// Like send a like message.
func (dao *Dao) Like(c context.Context, mid, tomid int64, title, msg, extraInfo string, now time.Time) (err error) {
return dao.send(c, _codeReplyLike, "", title, msg, _dataTypeLike, mid, []int64{tomid}, extraInfo, now.Unix())
}
// Reply send a reply message.
func (dao *Dao) Reply(c context.Context, mc, resID string, mid, tomid int64, title, msg, extraInfo string, now time.Time) (err error) {
return dao.send(c, mc, resID, title, msg, _dataTypeReply, mid, []int64{tomid}, extraInfo, now.Unix())
}
// DeleteReply send delete reply message.
func (dao *Dao) DeleteReply(c context.Context, mid int64, title, msg string, now time.Time) (err error) {
return dao.send(c, _codeReplyDelete, "", title, msg, _dataTypeSystem, 0, []int64{mid}, "", now.Unix())
}
// At send a at message.
func (dao *Dao) At(c context.Context, mid int64, mids []int64, title, msg, extraInfo string, now time.Time) (err error) {
if len(mids) == 0 {
return
}
return dao.send(c, _codeAt, "", title, msg, _dataTypeAt, mid, mids, extraInfo, now.Unix())
}
// AcceptReport send accept report message.
func (dao *Dao) AcceptReport(c context.Context, mid int64, title, msg string, now time.Time) (err error) {
return dao.send(c, _codeReport, "", title, msg, _dataTypeSystem, 0, []int64{mid}, "", now.Unix())
}
// System send a system message.
func (dao *Dao) System(c context.Context, mc, resID string, mid int64, title, msg, info string, now time.Time) (err error) {
return dao.send(c, mc, resID, title, msg, _dataTypeSystem, 0, []int64{mid}, info, now.Unix())
}
func (dao *Dao) send(c context.Context, mc, resID, title, msg string, tp int, pub int64, mids []int64, info string, ts int64) (err error) {
params := url.Values{}
params.Set("type", "json")
params.Set("source", "1")
params.Set("mc", mc)
params.Set("title", title)
params.Set("data_type", strconv.Itoa(tp))
params.Set("context", msg)
params.Set("mid_list", xstr.JoinInts(mids))
params.Set("publisher", strconv.FormatInt(pub, 10))
params.Set("ext_info", info)
if resID != "" {
params.Set("notify_type", fmt.Sprint(_notifyTypeCnt))
params.Set("res_id", resID)
}
var res struct {
Code int `json:"code"`
}
if err = dao.httpCli.Post(c, dao.apiURL, "", params, &res); err != nil {
log.Error("message url(%s) error(%v)", dao.apiURL+"?"+params.Encode(), err)
return
}
if res.Code != ecode.OK.Code() {
log.Error("message url(%s) error(%v)", dao.apiURL+"?"+params.Encode(), res.Code)
err = fmt.Errorf("message send failed")
return
}
log.Info("sendmessage success:%v;code:%d", params, res.Code)
if tp != _dataTypeSystem {
params.Set("mobi_app", "android_i")
if tp == _dataTypeAt {
params.Set("title", converAt(title))
} else if tp == _dataTypeLike {
params.Set("context", convertMsg(msg))
} else if tp == _dataTypeReply {
params.Set("title", convertMsg(title))
params.Set("context", convertMsg(msg))
}
var res1 struct {
Code int `json:"code"`
}
if err = dao.httpCli.Post(c, dao.apiURL, "", params, &res1); err != nil {
log.Error("message url(%s) error(%v)", dao.apiURL+"?"+params.Encode(), err)
return
}
if res1.Code != ecode.OK.Code() {
log.Error("message url(%s) error(%v)", dao.apiURL+"?"+params.Encode(), res1.Code)
err = fmt.Errorf("message send failed")
return
}
log.Info("send international message success:%v;code:%d", params, res1.Code)
}
return
}
func converAt(title string) string {
return strings.Replace(title, "评论中@了你", "評論中@了你", -1)
}
func convertMsg(msg string) string {
rmsg := []rune(msg)
for i, c := range rmsg {
switch c {
case '评':
rmsg[i] = '評'
case '论':
rmsg[i] = '論'
case '赞':
rmsg[i] = '讚'
case '条':
rmsg[i] = '條'
case '专':
rmsg[i] = '專'
case '栏':
rmsg[i] = '欄'
case '数':
rmsg[i] = '數'
case '达':
rmsg[i] = '達'
case '应':
rmsg[i] = '應'
case '点':
rmsg[i] = '點'
case '击':
rmsg[i] = '擊'
default:
continue
}
}
return string(rmsg)
}

View File

@@ -0,0 +1,229 @@
package message
import (
"context"
"flag"
"go-common/app/job/main/reply/conf"
"os"
"testing"
"time"
"github.com/smartystreets/goconvey/convey"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.community.reply-job")
flag.Set("conf_token", "5deea0665f8a7670b22a719337a39c7d")
flag.Set("tree_id", "2123")
flag.Set("conf_version", "docker-1")
flag.Set("deploy_env", "uat")
flag.Set("conf_host", "config.bilibili.co")
flag.Set("conf_path", "/tmp")
flag.Set("region", "sh")
flag.Set("zone", "sh001")
} else {
flag.Set("conf", "../../cmd/reply-job-test.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = NewMessageDao(conf.Conf)
os.Exit(m.Run())
}
func TestMessageNewMessageDao(t *testing.T) {
convey.Convey("NewMessageDao", t, func(ctx convey.C) {
var (
c = conf.Conf
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
p1 := NewMessageDao(c)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestMessageLike(t *testing.T) {
convey.Convey("Like", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
tomid = int64(0)
title = ""
msg = ""
extraInfo = ""
now = time.Now()
err error
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
err = d.Like(c, mid, tomid, title, msg, extraInfo, now)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(msg, convey.ShouldBeBlank)
})
})
})
}
func TestMessageReply(t *testing.T) {
convey.Convey("Reply", t, func(ctx convey.C) {
var (
c = context.Background()
mc = ""
resID = ""
mid = int64(0)
tomid = int64(0)
title = ""
msg = ""
extraInfo = ""
now = time.Now()
err error
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
err = d.Reply(c, mc, resID, mid, tomid, title, msg, extraInfo, now)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(msg, convey.ShouldBeBlank)
})
})
})
}
func TestMessageDeleteReply(t *testing.T) {
convey.Convey("DeleteReply", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
title = ""
msg = ""
now = time.Now()
err error
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
err = d.DeleteReply(c, mid, title, msg, now)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(msg, convey.ShouldBeBlank)
})
})
})
}
func TestMessageAt(t *testing.T) {
convey.Convey("At", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
mids = []int64{}
title = ""
msg = ""
extraInfo = ""
now = time.Now()
err error
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
err = d.At(c, mid, mids, title, msg, extraInfo, now)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(msg, convey.ShouldBeBlank)
})
})
})
}
func TestMessageAcceptReport(t *testing.T) {
convey.Convey("AcceptReport", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
title = ""
msg = ""
now = time.Now()
err error
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
err = d.AcceptReport(c, mid, title, msg, now)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(msg, convey.ShouldBeBlank)
})
})
})
}
func TestMessageSystem(t *testing.T) {
convey.Convey("System", t, func(ctx convey.C) {
var (
c = context.Background()
mc = ""
resID = ""
mid = int64(0)
title = ""
msg = ""
info = ""
now = time.Now()
err error
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
err = d.System(c, mc, resID, mid, title, msg, info, now)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(msg, convey.ShouldBeBlank)
})
})
})
}
func TestMessagesend(t *testing.T) {
convey.Convey("send", t, func(ctx convey.C) {
var (
c = context.Background()
mc = ""
resID = ""
title = ""
msg = ""
tp = int(0)
pub = int64(0)
mids = []int64{}
info = ""
ts = int64(0)
err error
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
err = d.send(c, mc, resID, title, msg, tp, pub, mids, info, ts)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(msg, convey.ShouldBeBlank)
})
})
})
}
func TestMessageconverAt(t *testing.T) {
convey.Convey("converAt", t, func(ctx convey.C) {
var (
title = ""
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
p1 := converAt(title)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldBeBlank)
})
})
})
}
func TestMessageconvertMsg(t *testing.T) {
convey.Convey("convertMsg", t, func(ctx convey.C) {
var (
msg = "评"
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
p1 := convertMsg(msg)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldEqual, "評")
})
})
})
}