From 58e2949740bdee9ebd46021850d0734c44405d90 Mon Sep 17 00:00:00 2001 From: Umputun Date: Sat, 17 Feb 2018 22:40:08 -0600 Subject: [PATCH] clean avatar related from disqus import, extend missing --- app/migrator/disqus.go | 10 ++++------ app/migrator/disqus_test.go | 3 +-- app/migrator/migrator.go | 11 +++++------ app/rest/auth/avatar.go | 7 ++++++- app/rest/server/admin.go | 16 +++++++++++----- app/rest/server/rest.go | 15 ++++++++++----- 6 files changed, 37 insertions(+), 25 deletions(-) diff --git a/app/migrator/disqus.go b/app/migrator/disqus.go index 7dc0d073..5574fd9c 100644 --- a/app/migrator/disqus.go +++ b/app/migrator/disqus.go @@ -14,8 +14,7 @@ import ( // Disqus implements Importer from disqus xml type Disqus struct { - DataStore store.Interface - DefaultAvatarURL string + DataStore store.Interface } type disqusThread struct { @@ -122,10 +121,9 @@ func (d *Disqus) convert(r io.Reader, siteID string) (ch chan store.Comment) { ID: comment.UID, Locator: store.Locator{URL: postsMap[comment.Tid.Val], SiteID: siteID}, User: store.User{ - ID: "disqus_" + comment.AuthorUserName, - Name: comment.AuthorName, - Picture: d.DefaultAvatarURL, - IP: comment.IP, + ID: "disqus_" + comment.AuthorUserName, + Name: comment.AuthorName, + IP: comment.IP, }, Text: d.cleanText(comment.Message), Timestamp: comment.CreatedAt, diff --git a/app/migrator/disqus_test.go b/app/migrator/disqus_test.go index da887f88..e2a7cd9f 100644 --- a/app/migrator/disqus_test.go +++ b/app/migrator/disqus_test.go @@ -16,7 +16,7 @@ func TestDisqus_Import(t *testing.T) { defer os.Remove("/tmp/remark-test.db") dataStore, err := store.NewBoltDB(store.BoltSite{FileName: "/tmp/remark-test.db", SiteID: "test"}) require.Nil(t, err, "create store") - d := Disqus{DataStore: dataStore, DefaultAvatarURL: "http://localhost:8080/avatar.png"} + d := Disqus{DataStore: dataStore} err = d.Import(strings.NewReader(xmlTest), "test") assert.Nil(t, err) @@ -32,7 +32,6 @@ func TestDisqus_Import(t *testing.T) { assert.Equal(t, "Dmitry Noname", c.User.Name) assert.Equal(t, "disqus_google-74b9e7568ef6860e93862c5d77590123", c.User.ID) assert.Equal(t, "89.89.89.139", c.User.IP) - assert.Equal(t, "http://localhost:8080/avatar.png", c.User.Picture) posts, err := dataStore.List("test") assert.Nil(t, err) diff --git a/app/migrator/migrator.go b/app/migrator/migrator.go index 254f2ff7..6b820b07 100644 --- a/app/migrator/migrator.go +++ b/app/migrator/migrator.go @@ -25,11 +25,10 @@ type Exporter interface { // ImportParams defines everything needed to run import type ImportParams struct { - DataStore store.Interface - InputFile string - Provider string - SiteID string - DefaultAvatarURL string + DataStore store.Interface + InputFile string + Provider string + SiteID string } // ImportComments imports from given provider format and saves to store @@ -39,7 +38,7 @@ func ImportComments(p ImportParams) error { var importer Importer switch p.Provider { case "disqus": - importer = &Disqus{DataStore: p.DataStore, DefaultAvatarURL: p.DefaultAvatarURL} + importer = &Disqus{DataStore: p.DataStore} case "native": importer = &Remark{DataStore: p.DataStore} default: diff --git a/app/rest/auth/avatar.go b/app/rest/auth/avatar.go index 4f337e80..bbd4c554 100644 --- a/app/rest/auth/avatar.go +++ b/app/rest/auth/avatar.go @@ -35,7 +35,7 @@ func (p *AvatarProxy) Put(u store.User) (avatarURL string, err error) { if u.Picture == "" { if p.DefaultAvatar != "" { - return p.RemarkURL + p.RoutePath + "/" + p.DefaultAvatar, nil + return p.Default(), nil } return "", errors.Errorf("no picture for %s", u.ID) } @@ -120,6 +120,11 @@ func (p *AvatarProxy) Routes() (string, chi.Router) { return p.RoutePath, router } +// Default returns full default avatar url +func (p *AvatarProxy) Default() string { + return strings.TrimRight(p.RemarkURL, "/") + p.RoutePath + "/" + p.DefaultAvatar +} + // encodeID hashes user id to sha1 func (p *AvatarProxy) encodeID(id string) string { h := sha1.New() diff --git a/app/rest/server/admin.go b/app/rest/server/admin.go index 02e69d09..20b1b8c0 100644 --- a/app/rest/server/admin.go +++ b/app/rest/server/admin.go @@ -18,10 +18,11 @@ import ( // admin provides router for all requests available for admin users only type admin struct { - dataService store.Service - exporter migrator.Exporter - importer migrator.Importer - cache rest.LoadingCache + dataService store.Service + exporter migrator.Exporter + importer migrator.Importer + cache rest.LoadingCache + defAvatarURL string } func (a *admin) routes(middlewares ...func(http.Handler) http.Handler) chi.Router { @@ -127,7 +128,7 @@ func (a *admin) checkBlocked(siteID string, user store.User) bool { // processes comments and hides text of all comments for blocked users. // resets score and votes too. Also hides sensitive info for non-admin users -func (a *admin) maskInfo(comments []store.Comment, r *http.Request) (res []store.Comment) { +func (a *admin) alterComments(comments []store.Comment, r *http.Request) (res []store.Comment) { res = make([]store.Comment, len(comments)) user, err := rest.GetUserInfo(r) @@ -141,6 +142,11 @@ func (a *admin) maskInfo(comments []store.Comment, r *http.Request) (res []store c.User.Blocked = true } + // set default avatar + if c.User.Picture == "" { + c.User.Picture = a.defAvatarURL + } + // hide info from non-admins if !isAdmin { c.User.IP = "" diff --git a/app/rest/server/rest.go b/app/rest/server/rest.go index 576dc9ed..2f4cf589 100644 --- a/app/rest/server/rest.go +++ b/app/rest/server/rest.go @@ -95,7 +95,12 @@ func (s *Rest) Run(port int) { rauth.Put("/notify", s.notifyActionCtrl) rauth.Get("/notify", s.notifyStatusCtrl) // admin routes, admin users only - s.mod = admin{dataService: s.DataService, exporter: s.Exporter, cache: s.Cache} + s.mod = admin{ + dataService: s.DataService, + exporter: s.Exporter, + cache: s.Cache, + defAvatarURL: s.Authenticator.AvatarProxy.Default(), + } rauth.Mount("/admin", s.mod.routes(s.Authenticator.AdminOnly)) }) }) @@ -223,7 +228,7 @@ func (s *Rest) findCommentsCtrl(w http.ResponseWriter, r *http.Request) { if e != nil { return nil, e } - maskedComments := s.mod.maskInfo(comments, r) + maskedComments := s.mod.alterComments(comments, r) var b []byte switch r.URL.Query().Get("format") { case "tree": @@ -256,7 +261,7 @@ func (s *Rest) lastCommentsCtrl(w http.ResponseWriter, r *http.Request) { if e != nil { return nil, e } - comments = s.mod.maskInfo(comments, r) + comments = s.mod.alterComments(comments, r) return encodeJSONWithHTML(comments) }) @@ -281,7 +286,7 @@ func (s *Rest) commentByIDCtrl(w http.ResponseWriter, r *http.Request) { rest.SendErrorJSON(w, r, http.StatusBadRequest, err, "can't get comment by id") return } - comment = s.mod.maskInfo([]store.Comment{comment}, r)[0] + comment = s.mod.alterComments([]store.Comment{comment}, r)[0] render.Status(r, http.StatusOK) renderJSONWithHTML(w, r, comment) } @@ -304,7 +309,7 @@ func (s *Rest) findUserCommentsCtrl(w http.ResponseWriter, r *http.Request) { if e != nil { return nil, e } - comments = s.mod.maskInfo(comments, r) + comments = s.mod.alterComments(comments, r) resp.Comments, resp.Count = comments, count return encodeJSONWithHTML(resp) })