Clean up getProfilePageFromHandle

- Export the func
- Remove commented-out code
- Use log, not fmt for debug messages
- Remove named return parameters
- Use standard var naming schemes
- Fix spacing in queries and remove unnecessary chars
pull/195/head
Matt Baer 5 years ago
parent 68d63d3fef
commit bb63e64883
  1. 6
      collections.go
  2. 19
      database.go
  3. 4
      posts.go

@ -1,5 +1,5 @@
/* /*
* Copyright © 2018 A Bunch Tell LLC. * Copyright © 2018-2020 A Bunch Tell LLC.
* *
* This file is part of WriteFreely. * This file is part of WriteFreely.
* *
@ -861,13 +861,13 @@ func handleViewMention(app *App, w http.ResponseWriter, r *http.Request) error {
vars := mux.Vars(r) vars := mux.Vars(r)
handle := vars["handle"] handle := vars["handle"]
remoteUser, err := app.db.getProfilePageFromHandle(app, handle) remoteUser, err := app.db.GetProfilePageFromHandle(app, handle)
if err != nil { if err != nil {
log.Error("Couldn't find this user "+handle, err) log.Error("Couldn't find this user "+handle, err)
return nil return nil
} }
return impart.HTTPError{Status: http.StatusFound, Message: remoteUser} return impart.HTTPError{Status: http.StatusFound, Message: remoteUser}
} }
func handleViewCollectionTag(app *App, w http.ResponseWriter, r *http.Request) error { func handleViewCollectionTag(app *App, w http.ResponseWriter, r *http.Request) error {

@ -2557,18 +2557,17 @@ func handleFailedPostInsert(err error) error {
return err return err
} }
func (db *datastore) getProfilePageFromHandle(app *App, handle string) (actorIRI string, err error) { func (db *datastore) GetProfilePageFromHandle(app *App, handle string) (string, error) {
remoteuser, errRemoteUser := getRemoteUserFromHandle(app, handle) actorIRI := ""
if errRemoteUser != nil { remoteUser, err := getRemoteUserFromHandle(app, handle)
if err != nil {
// can't find using handle in the table but the table may already have this user without // can't find using handle in the table but the table may already have this user without
// handle from a previous version // handle from a previous version
actorIRI = RemoteLookup(handle) actorIRI = RemoteLookup(handle)
_, errRemoteUser := getRemoteUser(app, actorIRI) _, errRemoteUser := getRemoteUser(app, actorIRI)
// if it exists then we need to update the handle // if it exists then we need to update the handle
if errRemoteUser == nil { if errRemoteUser == nil {
// query := "UPDATE remoteusers SET handle='" + handle + "' WHERE actor_id='" + iri + "';" _, err := app.db.Exec("UPDATE remoteusers SET handle = ? WHERE actor_id = ?", handle, actorIRI)
// log.Info(query)
_, err := app.db.Exec("UPDATE remoteusers SET handle=? WHERE actor_id=?;", handle, actorIRI)
if err != nil { if err != nil {
log.Error("Can't update handle (" + handle + ") in database for user " + actorIRI) log.Error("Can't update handle (" + handle + ") in database for user " + actorIRI)
} }
@ -2579,15 +2578,17 @@ func (db *datastore) getProfilePageFromHandle(app *App, handle string) (actorIRI
if err != nil { if err != nil {
log.Error("Couldn't fetch remote actor", err) log.Error("Couldn't fetch remote actor", err)
} }
fmt.Println(actorIRI, remoteActor.GetInbox(), remoteActor.GetSharedInbox(), handle) if debugging {
_, err = app.db.Exec("INSERT INTO remoteusers (actor_id, inbox, shared_inbox, handle) VALUES( ?, ?, ?, ?)", actorIRI, remoteActor.GetInbox(), remoteActor.GetSharedInbox(), handle) log.Info("%s %s %s %s", actorIRI, remoteActor.GetInbox(), remoteActor.GetSharedInbox(), handle)
}
_, err = app.db.Exec("INSERT INTO remoteusers (actor_id, inbox, shared_inbox, handle) VALUES(?, ?, ?, ?)", actorIRI, remoteActor.GetInbox(), remoteActor.GetSharedInbox(), handle)
if err != nil { if err != nil {
log.Error("Can't insert remote user in database", err) log.Error("Can't insert remote user in database", err)
return "", err return "", err
} }
} }
} else { } else {
actorIRI = remoteuser.ActorID actorIRI = remoteUser.ActorID
} }
return actorIRI, nil return actorIRI, nil
} }

@ -1,5 +1,5 @@
/* /*
* Copyright © 2018-2019 A Bunch Tell LLC. * Copyright © 2018-2020 A Bunch Tell LLC.
* *
* This file is part of WriteFreely. * This file is part of WriteFreely.
* *
@ -1176,7 +1176,7 @@ func (p *PublicPost) ActivityObject(app *App) *activitystreams.Object {
mentions := mentionRegex.FindAllString(content, -1) mentions := mentionRegex.FindAllString(content, -1)
for _, handle := range mentions { for _, handle := range mentions {
actorIRI, err := app.db.getProfilePageFromHandle(app, handle) actorIRI, err := app.db.GetProfilePageFromHandle(app, handle)
if err != nil { if err != nil {
log.Info("Can't find this user either in the database nor in the remote instance") log.Info("Can't find this user either in the database nor in the remote instance")
return nil return nil

Loading…
Cancel
Save