Support Match Email condition.
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
parent
eee4278af4
commit
f3e25a0846
51
plugin.go
51
plugin.go
@ -136,22 +136,43 @@ func convertLocation(value string) (Location, bool) {
|
||||
}, false
|
||||
}
|
||||
|
||||
func parseTo(value, authorEmail string) (int64, bool) {
|
||||
ids := trimElement(strings.Split(value, ":"))
|
||||
func parseTo(to []string, authorEmail string, matchEmail bool) []int64 {
|
||||
var emails []int64
|
||||
var ids []int64
|
||||
attachChat := true
|
||||
|
||||
if len(ids) > 1 {
|
||||
if email := ids[1]; email != authorEmail {
|
||||
log.Println("email not match")
|
||||
return int64(0), false
|
||||
}
|
||||
}
|
||||
for _, value := range to {
|
||||
idArray := trimElement(strings.Split(value, ":"))
|
||||
|
||||
id, err := strconv.ParseInt(ids[0], 10, 64)
|
||||
// check id
|
||||
id, err := strconv.ParseInt(idArray[0], 10, 64)
|
||||
if err != nil {
|
||||
return int64(0), false
|
||||
continue
|
||||
}
|
||||
|
||||
return id, true
|
||||
// check match author email
|
||||
if len(idArray) > 1 {
|
||||
if email := idArray[1]; email != authorEmail {
|
||||
continue
|
||||
}
|
||||
|
||||
emails = append(emails, id)
|
||||
attachChat = false
|
||||
continue
|
||||
}
|
||||
|
||||
ids = append(ids, id)
|
||||
}
|
||||
|
||||
if matchEmail == true && attachChat == false {
|
||||
return emails
|
||||
}
|
||||
|
||||
for _, value := range emails {
|
||||
ids = append(ids, value)
|
||||
}
|
||||
|
||||
return ids
|
||||
}
|
||||
|
||||
// Exec executes the plugin.
|
||||
@ -180,6 +201,7 @@ func (p Plugin) Exec() error {
|
||||
|
||||
bot.Debug = p.Config.Debug
|
||||
|
||||
ids := parseTo(p.Config.To, p.Build.Email, p.Config.MatchEmail)
|
||||
photos := fileExist(trimElement(p.Config.Photo))
|
||||
documents := fileExist(trimElement(p.Config.Document))
|
||||
stickers := fileExist(trimElement(p.Config.Sticker))
|
||||
@ -190,12 +212,7 @@ func (p Plugin) Exec() error {
|
||||
venues := trimElement(p.Config.Venue)
|
||||
|
||||
// send message.
|
||||
for _, to := range p.Config.To {
|
||||
user, enable := parseTo(to, p.Build.Email)
|
||||
if !enable {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, user := range ids {
|
||||
for _, value := range trimElement(message) {
|
||||
txt, err := template.RenderTrim(value, p)
|
||||
if err != nil {
|
||||
|
@ -133,30 +133,19 @@ func TestTrimElement(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestParseTo(t *testing.T) {
|
||||
id, enable := parseTo("1234567890", "test@gmail.com")
|
||||
input := []string{"0", "1:1@gmail.com", "2:2@gmail.com", "3:3@gmail.com", "4", "5"}
|
||||
|
||||
assert.Equal(t, true, enable)
|
||||
assert.Equal(t, int64(1234567890), id)
|
||||
ids := parseTo(input, "1@gmail.com", false)
|
||||
assert.Equal(t, []int64{0, 4, 5, 1}, ids)
|
||||
|
||||
id, enable = parseTo("1234567890:test2@gmail.com", "test@gmail.com")
|
||||
ids = parseTo(input, "1@gmail.com", true)
|
||||
assert.Equal(t, []int64{1}, ids)
|
||||
|
||||
assert.Equal(t, false, enable)
|
||||
assert.Equal(t, int64(0), id)
|
||||
ids = parseTo(input, "a@gmail.com", false)
|
||||
assert.Equal(t, []int64{0, 4, 5}, ids)
|
||||
|
||||
id, enable = parseTo("1234567890:test@gmail.com", "test@gmail.com")
|
||||
|
||||
assert.Equal(t, true, enable)
|
||||
assert.Equal(t, int64(1234567890), id)
|
||||
|
||||
id, enable = parseTo("測試:test@gmail.com", "test@gmail.com")
|
||||
|
||||
assert.Equal(t, false, enable)
|
||||
assert.Equal(t, int64(0), id)
|
||||
|
||||
id, enable = parseTo("測試", "test@gmail.com")
|
||||
|
||||
assert.Equal(t, false, enable)
|
||||
assert.Equal(t, int64(0), id)
|
||||
ids = parseTo(input, "a@gmail.com", true)
|
||||
assert.Equal(t, []int64{0, 4, 5}, ids)
|
||||
}
|
||||
|
||||
func TestCheckFileExist(t *testing.T) {
|
||||
|
Loading…
Reference in New Issue
Block a user