clean avatar related from disqus import, extend missing

This commit is contained in:
Umputun
2018-02-17 22:40:08 -06:00
parent 9e1c0d0640
commit 58e2949740
6 changed files with 37 additions and 25 deletions
+4 -6
View File
@@ -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,
+1 -2
View File
@@ -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)
+5 -6
View File
@@ -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:
+6 -1
View File
@@ -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()
+11 -5
View File
@@ -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 = ""
+10 -5
View File
@@ -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)
})