Skip to content

Commit

Permalink
update weibo model
Browse files Browse the repository at this point in the history
  • Loading branch information
Sora233 committed Nov 5, 2021
1 parent 5fe724b commit e1a25c2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
17 changes: 12 additions & 5 deletions lsp/weibo/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}

Expand Down
6 changes: 6 additions & 0 deletions utils/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "")
}
22 changes: 22 additions & 0 deletions utils/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package utils
import (
"github.com/guonaihong/gout"
"github.com/stretchr/testify/assert"
"html"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -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{
"</a>",
"aaa??qweqwe",
"qabcd<a></a>www",
"qwe<html></html>",
html.EscapeString("qwe<html></html>"),
}
var expected = []string{
"",
"aaa??qweqwe",
"qabcdwww",
"qwe",
html.EscapeString("qwe<html></html>"),
}
assert.EqualValues(t, len(expected), len(testCase))
for idx := range testCase {
assert.EqualValues(t, expected[idx], RemoveHtmlTag(testCase[idx]))
}
}

0 comments on commit e1a25c2

Please sign in to comment.