fix(tvl-ebooks): Update code to bring up to date with reality

Fixes include:

1) users can now opt out of being mkov'd, instead their messages
will be ascii blocked out.

2) highlights are avoided, by learning names from the main tvl
channel and adding a dot after the first char, for example:

> 21:31:35 <•eta-eb> m.ulti: bas1l we quickly connect controller is mostly agreed

3) highlight avoidance is now stored in redis, to avoid restarts
from destroying the map and causing a bunch of highlights
upon restart

Change-Id: I1055992aab3a06aa1f4ba937fc3ef45f2f78cedc
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2054
Tested-by: BuildkiteCI
Reviewed-by: cynthia <cynthia@tvl.fyi>
Reviewed-by: tazjin <mail@tazj.in>
Reviewed-by: ben <tvl@benjojo.co.uk>
This commit is contained in:
Ben Cartwright-Cox 2020-10-20 22:53:37 +01:00 committed by ben
parent e9e42d5d20
commit d4fc093383

View file

@ -20,7 +20,8 @@ type incomingIRC struct {
User string `json:"User"` User string `json:"User"`
} }
var supressionUsernames map[string]bool var suppressionUsernames map[string]bool
var noMkov map[string]bool
func main() { func main() {
redisc := redis.NewClient(&redis.Options{ redisc := redis.NewClient(&redis.Options{
@ -30,7 +31,27 @@ func main() {
}) })
fireaway := make(chan incomingIRC, 10) fireaway := make(chan incomingIRC, 10)
supressionUsernames = make(map[string]bool) suppressionUsernames = make(map[string]bool)
suppressionList := redisc.HGetAll("suppressionList")
suppressionListA, _ := suppressionList.Result()
suppressionListMap, _ := stringMaptoIntMap(suppressionListA)
for v, _ := range suppressionListMap {
suppressionUsernames[v] = true
suppressionUsernames[strings.ToLower(v)] = true
}
noMkov = make(map[string]bool)
noMkovRedis := redisc.HGetAll("nomkov")
noMkovRedisA, _ := noMkovRedis.Result()
noMkovMap, _ := stringMaptoIntMap(noMkovRedisA)
for v, _ := range noMkovMap {
noMkov[v] = true
noMkov[strings.ToLower(v)] = true
}
go func() { go func() {
for { for {
@ -81,8 +102,11 @@ func main() {
func generateMesasge(msg incomingIRC, redisc *redis.Client) string { func generateMesasge(msg incomingIRC, redisc *redis.Client) string {
text := msg.Params[1] text := msg.Params[1]
username := strings.ToLower(msg.Name) username := strings.ToLower(msg.Name)
supressionUsernames[username] = true suppressionUsernames[username] = true
supressionUsernames[username+":"] = true suppressionUsernames[username+":"] = true
suppressionUsernames[msg.Name] = true
suppressionUsernames[msg.Name+":"] = true
redisc.HIncrBy("suppressionList", msg.Name, 1)
text = strings.ToLower(text) text = strings.ToLower(text)
text = strings.Replace(text, ",", "", -1) text = strings.Replace(text, ",", "", -1)
@ -92,16 +116,19 @@ func generateMesasge(msg incomingIRC, redisc *redis.Client) string {
text = strings.Replace(text, "?", "", -1) text = strings.Replace(text, "?", "", -1)
words := strings.Split(text, " ") words := strings.Split(text, " ")
lastWord := propwords(username, words[0], redisc) lastWord := propwords(msg.Name, words[0], redisc)
if supressionUsernames[words[0]] { if noMkov[username] {
if len(words[0]) < 2 { lastWord = blockoutWord(lastWord)
words[0] = "vee" words[0] = blockoutWord(words[0])
}
words[0] = fmt.Sprintf("%s.%s", string(words[0][0]), words[0][1:])
} }
lastWord = filterHighlights(lastWord)
if lastWord == "_END_" { if lastWord == "_END_" {
if noMkov[username] {
return blockoutWord(words[0])
}
return words[0] return words[0]
} }
outputMsg := words[0] + " " + lastWord + " " outputMsg := words[0] + " " + lastWord + " "
@ -112,13 +139,12 @@ func generateMesasge(msg incomingIRC, redisc *redis.Client) string {
return outputMsg return outputMsg
} }
if supressionUsernames[lastWord] { if noMkov[username] {
if len(lastWord) < 2 { lastWord = blockoutWord(lastWord)
lastWord = "vee"
}
lastWord = fmt.Sprintf("%s.%s", string(lastWord[0]), lastWord[1:])
} }
lastWord = filterHighlights(lastWord)
outputMsg += lastWord + " " outputMsg += lastWord + " "
if len(outputMsg) > 100 { if len(outputMsg) > 100 {
return outputMsg return outputMsg
@ -126,6 +152,28 @@ func generateMesasge(msg incomingIRC, redisc *redis.Client) string {
} }
} }
// filterHighlights: tries to prevent highlights by checking against
// a map called suppressionUsernames
func filterHighlights(in string) string {
for username := range suppressionUsernames {
if strings.Contains(in, username) {
if len(in) < 2 {
in = fmt.Sprintf("%s.%s", string(in[0]), in[1:])
return in
}
}
}
return in
}
func blockoutWord(in string) string {
block := ""
for i := 0; i < len(in); i++ {
block += "█"
}
return block
}
func propwords(username string, start string, redisc *redis.Client) string { func propwords(username string, start string, redisc *redis.Client) string {
userHash := redisc.HGetAll(fmt.Sprintf("%s-%s", username, start)) userHash := redisc.HGetAll(fmt.Sprintf("%s-%s", username, start))
userHashMap, err := userHash.Result() userHashMap, err := userHash.Result()
@ -190,7 +238,9 @@ func learnFromMessage(msg incomingIRC, redisc *redis.Client) {
nextWord = words[k+1] nextWord = words[k+1]
} }
redisc.HIncrBy(fmt.Sprintf("%s-%s", username, word), nextWord, 1) if !noMkov[username] {
redisc.HIncrBy(fmt.Sprintf("%s-%s", username, word), nextWord, 1)
}
redisc.HIncrBy(fmt.Sprintf("generic-%s", word), nextWord, 1) redisc.HIncrBy(fmt.Sprintf("generic-%s", word), nextWord, 1)
} }
} }