Add matching of includeRegex

This commit is contained in:
Peter Stuifzand 2018-07-16 22:24:50 +02:00
parent 09e5a81227
commit 023209c5d5
3 changed files with 35 additions and 16 deletions

View File

@ -376,8 +376,24 @@ func (b *memoryBackend) Fetch3(channel, fetchURL string) (*http.Response, error)
func (b *memoryBackend) channelAddItem(conn redis.Conn, channel string, item microsub.Item) error {
zchannelKey := fmt.Sprintf("zchannel:%s:posts", channel)
var excludeRegex regexp.Regexp
testExcludeRegex := false
for _, setting := range b.Settings {
if setting.IncludeRegex != "" {
includeRegex, err := regexp.Compile(setting.IncludeRegex)
if err != nil {
log.Printf("error in regexp: %q\n", includeRegex)
} else {
if item.Content != nil && includeRegex.MatchString(item.Content.Text) {
log.Printf("Excluded %#v\n", item)
return nil
}
if includeRegex.MatchString(item.Name) {
log.Printf("Excluded %#v\n", item)
return nil
}
}
}
}
if setting, e := b.Settings[channel]; e {
if setting.ExcludeRegex != "" {
@ -385,23 +401,19 @@ func (b *memoryBackend) channelAddItem(conn redis.Conn, channel string, item mic
if err != nil {
log.Printf("error in regexp: %q\n", excludeRegex)
} else {
testExcludeRegex = true
if item.Content != nil && excludeRegex.MatchString(item.Content.Text) {
log.Printf("Excluded %#v\n", item)
return nil
}
if excludeRegex.MatchString(item.Name) {
log.Printf("Excluded %#v\n", item)
return nil
}
}
}
}
if testExcludeRegex {
if item.Content != nil && excludeRegex.MatchString(item.Content.Text) {
log.Printf("Excluded %#v\n", item)
return nil
}
if excludeRegex.MatchString(item.Name) {
log.Printf("Excluded %#v\n", item)
return nil
}
}
if item.Published == "" {
item.Published = time.Now().Format(time.RFC3339)
}

View File

@ -48,6 +48,7 @@ type memoryBackend struct {
type channelSetting struct {
ExcludeRegex string
IncludeRegex string
}
type Debug interface {

View File

@ -57,10 +57,16 @@
<input type="hidden" name="uid" value="{{ .CurrentChannel.UID }}" />
<div class="field">
<div class="control">
<label class="label">Exclude regex</label>
<label class="label">Blocking Regex</label>
<input type="text" class="input" name="exclude_regex" value="{{ .CurrentSetting.ExcludeRegex }}" placeholder="enter regex to block" />
</div>
</div>
<div class="field">
<div class="control">
<label class="label">Tracking Regex</label>
<input type="text" class="input" name="include_regex" value="{{ .CurrentSetting.IncludeRegex }}" placeholder="enter regex to track items" />
</div>
</div>
<div class="field">
<button type="submit" class="button is-primary">Save</button>
</div>