From e1a25c2e34e20b3b95126b8fe173f953e6054602 Mon Sep 17 00:00:00 2001 From: sora233 Date: Fri, 5 Nov 2021 11:01:30 +0800 Subject: [PATCH] update weibo model --- lsp/weibo/model.go | 17 ++++++++++++----- utils/helper.go | 6 ++++++ utils/helper_test.go | 22 ++++++++++++++++++++++ 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/lsp/weibo/model.go b/lsp/weibo/model.go index 3ff24fda..d2b73293 100644 --- a/lsp/weibo/model.go +++ b/lsp/weibo/model.go @@ -3,6 +3,7 @@ package weibo import ( "github.com/Sora233/DDBOT/lsp/concern_type" "github.com/Sora233/DDBOT/lsp/mmsg" + localutils "github.com/Sora233/DDBOT/utils" "github.com/sirupsen/logrus" ) @@ -70,14 +71,20 @@ func (c *ConcernNewsNotify) GetGroupCode() int64 { func (c *ConcernNewsNotify) ToMessage() (m *mmsg.MSG) { m = mmsg.NewMSG() + m.Textf("weibo-%v发布了新微博:\n%v", + c.GetName(), + c.Card.GetMblog().GetCreatedAt()) switch c.Card.GetCardType() { - //case CardType_Normal: + case CardType_Normal: + if len(c.Card.GetMblog().GetRawText()) > 0 { + m.Textf("\n%v", localutils.RemoveHtmlTag(c.Card.GetMblog().GetRawText())) + } else { + m.Textf("\n%v", localutils.RemoveHtmlTag(c.Card.GetMblog().GetText())) + } default: - m.Textf("weibo-%v发布了新微博:\n%v\n%v\n", - c.GetName(), - c.Card.GetMblog().GetCreatedAt(), - c.Card.GetSchema()) + c.Logger().Debug("found new card_types") } + m.Textf("\n%v", c.Card.GetSchema()) return } diff --git a/utils/helper.go b/utils/helper.go index 49f4df24..becf1ab0 100644 --- a/utils/helper.go +++ b/utils/helper.go @@ -226,3 +226,9 @@ func JoinInt64(ele []int64, sep string) string { } return strings.Join(s, sep) } + +var reHtmlTag = regexp.MustCompile(`<[^>]+>`) + +func RemoveHtmlTag(s string) string { + return reHtmlTag.ReplaceAllString(s, "") +} diff --git a/utils/helper_test.go b/utils/helper_test.go index 0abc8668..8e4d3f05 100644 --- a/utils/helper_test.go +++ b/utils/helper_test.go @@ -3,6 +3,7 @@ package utils import ( "github.com/guonaihong/gout" "github.com/stretchr/testify/assert" + "html" "io/ioutil" "os" "path/filepath" @@ -211,3 +212,24 @@ func TestToCamel(t *testing.T) { assert.EqualValues(t, "boy_loves_girl", toCamel("BoyLovesGirl")) assert.EqualValues(t, "dog_god", toCamel("DogGod")) } + +func TestRemoveHtmlTag(t *testing.T) { + var testCase = []string{ + "", + "aaa??qweqwe", + "qabcdwww", + "qwe", + html.EscapeString("qwe"), + } + var expected = []string{ + "", + "aaa??qweqwe", + "qabcdwww", + "qwe", + html.EscapeString("qwe"), + } + assert.EqualValues(t, len(expected), len(testCase)) + for idx := range testCase { + assert.EqualValues(t, expected[idx], RemoveHtmlTag(testCase[idx])) + } +}