diff options
author | Wim <wim@42.be> | 2021-05-27 21:45:23 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-27 21:45:23 +0200 |
commit | c86137449e733fc643337f33eaa33daf9e16d73c (patch) | |
tree | 711cbfd7f64edfbf1780285b0f177afc2b3c472b /bridge/helper | |
parent | efec01a92f3d7c3aec3ada8bf873d728e349eee6 (diff) | |
download | matterbridge-msglm-c86137449e733fc643337f33eaa33daf9e16d73c.tar.gz matterbridge-msglm-c86137449e733fc643337f33eaa33daf9e16d73c.tar.bz2 matterbridge-msglm-c86137449e733fc643337f33eaa33daf9e16d73c.zip |
Add a MessageClipped option to set your own clipped message. Closes #1359 (#1487)
Diffstat (limited to 'bridge/helper')
-rw-r--r-- | bridge/helper/helper.go | 13 | ||||
-rw-r--r-- | bridge/helper/helper_test.go | 157 |
2 files changed, 88 insertions, 82 deletions
diff --git a/bridge/helper/helper.go b/bridge/helper/helper.go index 0ad31457..1bdd8a40 100644 --- a/bridge/helper/helper.go +++ b/bridge/helper/helper.go @@ -82,8 +82,10 @@ func DownloadFileAuthRocket(url, token, userID string) (*[]byte, error) { // TODO: The current implementation has the inconvenient that it disregards // word boundaries when splitting but this is hard to solve without potentially // breaking formatting and other stylistic effects. -func GetSubLines(message string, maxLineLength int) []string { - const clippingMessage = " <clipped message>" +func GetSubLines(message string, maxLineLength int, clippingMessage string) []string { + if clippingMessage == "" { + clippingMessage = " <clipped message>" + } var lines []string for _, line := range strings.Split(strings.TrimSpace(message), "\n") { @@ -193,8 +195,11 @@ func RemoveEmptyNewLines(msg string) string { // ClipMessage trims a message to the specified length if it exceeds it and adds a warning // to the message in case it does so. -func ClipMessage(text string, length int) string { - const clippingMessage = " <clipped message>" +func ClipMessage(text string, length int, clippingMessage string) string { + if clippingMessage == "" { + clippingMessage = " <clipped message>" + } + if len(text) > length { text = text[:length-len(clippingMessage)] if r, size := utf8.DecodeLastRuneInString(text); r == utf8.RuneError { diff --git a/bridge/helper/helper_test.go b/bridge/helper/helper_test.go index 48f33b10..76e548e4 100644 --- a/bridge/helper/helper_test.go +++ b/bridge/helper/helper_test.go @@ -10,98 +10,96 @@ import ( const testLineLength = 64 -var ( - lineSplittingTestCases = map[string]struct { - input string - splitOutput []string - nonSplitOutput []string - }{ - "Short single-line message": { - input: "short", - splitOutput: []string{"short"}, - nonSplitOutput: []string{"short"}, +var lineSplittingTestCases = map[string]struct { + input string + splitOutput []string + nonSplitOutput []string +}{ + "Short single-line message": { + input: "short", + splitOutput: []string{"short"}, + nonSplitOutput: []string{"short"}, + }, + "Long single-line message": { + input: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", + splitOutput: []string{ + "Lorem ipsum dolor sit amet, consectetur adipis <clipped message>", + "cing elit, sed do eiusmod tempor incididunt ut <clipped message>", + " labore et dolore magna aliqua.", }, - "Long single-line message": { - input: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", - splitOutput: []string{ - "Lorem ipsum dolor sit amet, consectetur adipis <clipped message>", - "cing elit, sed do eiusmod tempor incididunt ut <clipped message>", - " labore et dolore magna aliqua.", - }, - nonSplitOutput: []string{"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."}, + nonSplitOutput: []string{"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."}, + }, + "Short multi-line message": { + input: "I\ncan't\nget\nno\nsatisfaction!", + splitOutput: []string{ + "I", + "can't", + "get", + "no", + "satisfaction!", }, - "Short multi-line message": { - input: "I\ncan't\nget\nno\nsatisfaction!", - splitOutput: []string{ - "I", - "can't", - "get", - "no", - "satisfaction!", - }, - nonSplitOutput: []string{ - "I", - "can't", - "get", - "no", - "satisfaction!", - }, + nonSplitOutput: []string{ + "I", + "can't", + "get", + "no", + "satisfaction!", }, - "Long multi-line message": { - input: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n" + - "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\n" + - "Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.\n" + - "Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", - splitOutput: []string{ - "Lorem ipsum dolor sit amet, consectetur adipis <clipped message>", - "cing elit, sed do eiusmod tempor incididunt ut <clipped message>", - " labore et dolore magna aliqua.", - "Ut enim ad minim veniam, quis nostrud exercita <clipped message>", - "tion ullamco laboris nisi ut aliquip ex ea com <clipped message>", - "modo consequat.", - "Duis aute irure dolor in reprehenderit in volu <clipped message>", - "ptate velit esse cillum dolore eu fugiat nulla <clipped message>", - " pariatur.", - "Excepteur sint occaecat cupidatat non proident <clipped message>", - ", sunt in culpa qui officia deserunt mollit an <clipped message>", - "im id est laborum.", - }, - nonSplitOutput: []string{ - "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", - "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.", - "Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.", - "Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", - }, + }, + "Long multi-line message": { + input: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n" + + "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\n" + + "Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.\n" + + "Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", + splitOutput: []string{ + "Lorem ipsum dolor sit amet, consectetur adipis <clipped message>", + "cing elit, sed do eiusmod tempor incididunt ut <clipped message>", + " labore et dolore magna aliqua.", + "Ut enim ad minim veniam, quis nostrud exercita <clipped message>", + "tion ullamco laboris nisi ut aliquip ex ea com <clipped message>", + "modo consequat.", + "Duis aute irure dolor in reprehenderit in volu <clipped message>", + "ptate velit esse cillum dolore eu fugiat nulla <clipped message>", + " pariatur.", + "Excepteur sint occaecat cupidatat non proident <clipped message>", + ", sunt in culpa qui officia deserunt mollit an <clipped message>", + "im id est laborum.", }, - "Message ending with new-line.": { - input: "Newline ending\n", - splitOutput: []string{"Newline ending"}, - nonSplitOutput: []string{"Newline ending"}, + nonSplitOutput: []string{ + "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", + "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.", + "Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.", + "Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", }, - "Long message containing UTF-8 multi-byte runes": { - input: "不布人個我此而及單石業喜資富下我河下日沒一我臺空達的常景便物沒為……子大我別名解成?生賣的全直黑,我自我結毛分洲了世當,是政福那是東;斯說", - splitOutput: []string{ - "不布人個我此而及單石業喜資富下 <clipped message>", - "我河下日沒一我臺空達的常景便物 <clipped message>", - "沒為……子大我別名解成?生賣的 <clipped message>", - "全直黑,我自我結毛分洲了世當, <clipped message>", - "是政福那是東;斯說", - }, - nonSplitOutput: []string{"不布人個我此而及單石業喜資富下我河下日沒一我臺空達的常景便物沒為……子大我別名解成?生賣的全直黑,我自我結毛分洲了世當,是政福那是東;斯說"}, + }, + "Message ending with new-line.": { + input: "Newline ending\n", + splitOutput: []string{"Newline ending"}, + nonSplitOutput: []string{"Newline ending"}, + }, + "Long message containing UTF-8 multi-byte runes": { + input: "不布人個我此而及單石業喜資富下我河下日沒一我臺空達的常景便物沒為……子大我別名解成?生賣的全直黑,我自我結毛分洲了世當,是政福那是東;斯說", + splitOutput: []string{ + "不布人個我此而及單石業喜資富下 <clipped message>", + "我河下日沒一我臺空達的常景便物 <clipped message>", + "沒為……子大我別名解成?生賣的 <clipped message>", + "全直黑,我自我結毛分洲了世當, <clipped message>", + "是政福那是東;斯說", }, - } -) + nonSplitOutput: []string{"不布人個我此而及單石業喜資富下我河下日沒一我臺空達的常景便物沒為……子大我別名解成?生賣的全直黑,我自我結毛分洲了世當,是政福那是東;斯說"}, + }, +} func TestGetSubLines(t *testing.T) { for testname, testcase := range lineSplittingTestCases { - splitLines := GetSubLines(testcase.input, testLineLength) + splitLines := GetSubLines(testcase.input, testLineLength, "") assert.Equalf(t, testcase.splitOutput, splitLines, "'%s' testcase should give expected lines with splitting.", testname) for _, splitLine := range splitLines { byteLength := len([]byte(splitLine)) assert.True(t, byteLength <= testLineLength, "Splitted line '%s' of testcase '%s' should not exceed the maximum byte-length (%d vs. %d).", splitLine, testcase, byteLength, testLineLength) } - nonSplitLines := GetSubLines(testcase.input, 0) + nonSplitLines := GetSubLines(testcase.input, 0, "") assert.Equalf(t, testcase.nonSplitOutput, nonSplitLines, "'%s' testcase should give expected lines without splitting.", testname) } } @@ -110,16 +108,19 @@ func TestConvertWebPToPNG(t *testing.T) { if os.Getenv("LOCAL_TEST") == "" { t.Skip() } + input, err := ioutil.ReadFile("test.webp") if err != nil { t.Fail() } + d := &input err = ConvertWebPToPNG(d) if err != nil { t.Fail() } - err = ioutil.WriteFile("test.png", *d, 0644) + + err = ioutil.WriteFile("test.png", *d, 0o644) // nolint:gosec if err != nil { t.Fail() } |