Skip to content

Commit

Permalink
added passing userID to formatters
Browse files Browse the repository at this point in the history
  • Loading branch information
smaant committed Oct 14, 2019
1 parent a310e22 commit 05014a5
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 27 deletions.
4 changes: 2 additions & 2 deletions backend/app/cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,9 @@ func (s *ServerCommand) newServerApp() (*serverApp, error) {
}

imgProxy := &proxy.Image{Enabled: s.ImageProxy, RoutePath: "/api/v1/img", RemarkURL: s.RemarkURL}
emojiFmt := store.CommentConverterFunc(func(text string) string { return text })
emojiFmt := store.CommentConverterFunc(func(text string, userID string) string { return text })
if s.EnableEmoji {
emojiFmt = func(text string) string { return emoji.Sprint(text) }
emojiFmt = func(text string, userID string) string { return emoji.Sprint(text) }
}
commentFormatter := store.NewCommentFormatter(imgProxy, emojiFmt)

Expand Down
2 changes: 1 addition & 1 deletion backend/app/migrator/wordpress.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (w *wpTime) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
}

// Convert satisfies formatter.CommentConverter
func (w *WordPress) Convert(text string) string {
func (w *WordPress) Convert(text string, userID string) string {
return html.UnescapeString(text) // sanitize remains on comment create
}

Expand Down
2 changes: 1 addition & 1 deletion backend/app/rest/api/rest_private.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (s *private) updateCommentCtrl(w http.ResponseWriter, r *http.Request) {
}

editReq := service.EditRequest{
Text: s.commentFormatter.FormatText(edit.Text),
Text: s.commentFormatter.FormatText(edit.Text, user.ID),
Orig: edit.Text,
Summary: edit.Summary,
Delete: edit.Delete,
Expand Down
2 changes: 1 addition & 1 deletion backend/app/rest/proxy/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Image struct {
}

// Convert all img src links without https to proxied links
func (p Image) Convert(commentHTML string) string {
func (p Image) Convert(commentHTML string, userID string) string {
if !p.Enabled || strings.HasPrefix(p.RemarkURL, "http://") {
return commentHTML
}
Expand Down
8 changes: 4 additions & 4 deletions backend/app/rest/proxy/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,18 @@ func TestImage_RoutesTimedOut(t *testing.T) {

func TestPicture_Convert(t *testing.T) {
img := Image{Enabled: true, RoutePath: "/img"}
r := img.Convert(`<img src="http://radio-t.com/img3.png"/> xyz <img src="http://images.pexels.com/67636/img4.jpeg">`)
r := img.Convert(`<img src="http://radio-t.com/img3.png"/> xyz <img src="http://images.pexels.com/67636/img4.jpeg">`, "userID")
assert.Equal(t, `<img src="/img?src=aHR0cDovL3JhZGlvLXQuY29tL2ltZzMucG5n"/> xyz <img src="/img?src=aHR0cDovL2ltYWdlcy5wZXhlbHMuY29tLzY3NjM2L2ltZzQuanBlZw==">`, r)

r = img.Convert(`<img src="https://radio-t.com/img3.png"/> xyz <img src="http://images.pexels.com/67636/img4.jpeg">`)
r = img.Convert(`<img src="https://radio-t.com/img3.png"/> xyz <img src="http://images.pexels.com/67636/img4.jpeg">`, "userID")
assert.Equal(t, `<img src="https://radio-t.com/img3.png"/> xyz <img src="/img?src=aHR0cDovL2ltYWdlcy5wZXhlbHMuY29tLzY3NjM2L2ltZzQuanBlZw==">`, r)

img = Image{Enabled: true, RoutePath: "/img", RemarkURL: "http://example.com"}
r = img.Convert(`<img src="http://radio-t.com/img3.png"/> xyz`)
r = img.Convert(`<img src="http://radio-t.com/img3.png"/> xyz`, "userID")
assert.Equal(t, `<img src="http://radio-t.com/img3.png"/> xyz`, r, "http:// remark url, no proxy")

img = Image{Enabled: false, RoutePath: "/img"}
r = img.Convert(`<img src="http://radio-t.com/img3.png"/> xyz`)
r = img.Convert(`<img src="http://radio-t.com/img3.png"/> xyz`, "userID")
assert.Equal(t, `<img src="http://radio-t.com/img3.png"/> xyz`, r, "disabled, no proxy")
}

Expand Down
14 changes: 7 additions & 7 deletions backend/app/store/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ type CommentFormatter struct {
// CommentConverter defines interface to convert some parts of commentHTML
// Passed at creation time and does client-defined conversions, like image proxy link change
type CommentConverter interface {
Convert(text string) string
Convert(text string, userID string) string
}

// CommentConverterFunc functional struct implementing CommentConverter
type CommentConverterFunc func(text string) string
type CommentConverterFunc func(text string, userID string) string

// Convert calls func for given text
func (f CommentConverterFunc) Convert(text string) string {
return f(text)
func (f CommentConverterFunc) Convert(text string, userID string) string {
return f(text, userID)
}

// NewCommentFormatter makes CommentFormatter
Expand All @@ -34,12 +34,12 @@ func NewCommentFormatter(converters ...CommentConverter) *CommentFormatter {

// Format comment fields
func (f *CommentFormatter) Format(c Comment) Comment {
c.Text = f.FormatText(c.Text)
c.Text = f.FormatText(c.Text, c.User.ID)
return c
}

// FormatText converts text with markdown processor, applies external converters and shortens links
func (f *CommentFormatter) FormatText(txt string) (res string) {
func (f *CommentFormatter) FormatText(txt string, userID string) (res string) {
mdExt := bf.NoIntraEmphasis | bf.Tables | bf.FencedCode |
bf.Strikethrough | bf.SpaceHeadings | bf.HardLineBreak |
bf.BackslashLineBreak | bf.Autolink
Expand All @@ -52,7 +52,7 @@ func (f *CommentFormatter) FormatText(txt string) (res string) {
res = f.unEscape(res)

for _, conv := range f.converters {
res = conv.Convert(res)
res = conv.Convert(res, userID)
}
res = f.shortenAutoLinks(res, shortURLLen)
return res
Expand Down
24 changes: 13 additions & 11 deletions backend/app/store/formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,42 @@ import (

type mockConverter struct{}

func (m mockConverter) Convert(text string) string { return text + "!converted" }
func (m mockConverter) Convert(text string, userID string) string {
return text + "/" + userID + "!converted"
}

func TestFormatter_FormatText(t *testing.T) {
tbl := []struct {
in, out string
name string
}{
{"", "!converted", "empty"},
{"12345 abc", "<p>12345 abc</p>\n!converted", "simple"},
{"**xyz** _aaa_ - \"sfs\"", "<p><strong>xyz</strong> <em>aaa</em> – «sfs»</p>\n!converted", "format"},
{"", "/userID!converted", "empty"},
{"12345 abc", "<p>12345 abc</p>\n/userID!converted", "simple"},
{"**xyz** _aaa_ - \"sfs\"", "<p><strong>xyz</strong> <em>aaa</em> – «sfs»</p>\n/userID!converted", "format"},
{
"http://127.0.0.1/some-long-link/12345/678901234567890",
"<p><a href=\"http://127.0.0.1/some-long-link/12345/678901234567890\">http://127.0.0." +
"1/some-long-link/12345/6789012...</a></p>\n!converted", "links",
"1/some-long-link/12345/6789012...</a></p>\n/userID!converted", "links",
},
{"&mdash; not translated #354", "<p>— not translated #354</p>\n!converted", "mdash"},
{"&mdash; not translated #354", "<p>— not translated #354</p>\n/userID!converted", "mdash"},
}
f := NewCommentFormatter(mockConverter{})
for _, tt := range tbl {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.out, f.FormatText(tt.in))
assert.Equal(t, tt.out, f.FormatText(tt.in, "userID"))
})
}
}

func TestFormatter_FormatTextNoConverter(t *testing.T) {
f := NewCommentFormatter()
assert.Equal(t, "<p>12345</p>\n", f.FormatText("12345"))
assert.Equal(t, "<p>12345</p>\n", f.FormatText("12345", "userID"))
}

func TestFormatter_FormatTextConverterFunc(t *testing.T) {
fn := CommentConverterFunc(func(text string) string { return "zz!" + text })
fn := CommentConverterFunc(func(text string, userID string) string { return "zz!" + text + userID })
f := NewCommentFormatter(fn)
assert.Equal(t, "zz!<p>12345</p>\n", f.FormatText("12345"))
assert.Equal(t, "zz!<p>12345</p>\nuserID", f.FormatText("12345", "userID"))
}

func TestFormatter_FormatComment(t *testing.T) {
Expand All @@ -61,7 +63,7 @@ func TestFormatter_FormatComment(t *testing.T) {

f := NewCommentFormatter(mockConverter{})
exp := comment
exp.Text = "<p>blah</p>\n\n<p>xyz</p>\n!converted"
exp.Text = "<p>blah</p>\n\n<p>xyz</p>\n/username!converted"
assert.Equal(t, exp, f.Format(comment))
}

Expand Down

0 comments on commit 05014a5

Please sign in to comment.