From bb63e648835f58a3e5938b266d1da0461340e9bb Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Sat, 8 Feb 2020 12:10:47 -0500 Subject: [PATCH] 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 --- collections.go | 6 +++--- database.go | 19 ++++++++++--------- posts.go | 4 ++-- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/collections.go b/collections.go index 447d657..8ee88f1 100644 --- a/collections.go +++ b/collections.go @@ -1,5 +1,5 @@ /* - * Copyright © 2018 A Bunch Tell LLC. + * Copyright © 2018-2020 A Bunch Tell LLC. * * 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) handle := vars["handle"] - remoteUser, err := app.db.getProfilePageFromHandle(app, handle) + remoteUser, err := app.db.GetProfilePageFromHandle(app, handle) if err != nil { log.Error("Couldn't find this user "+handle, err) 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 { diff --git a/database.go b/database.go index 7d4bfc0..eca09d7 100644 --- a/database.go +++ b/database.go @@ -2557,18 +2557,17 @@ func handleFailedPostInsert(err error) error { return err } -func (db *datastore) getProfilePageFromHandle(app *App, handle string) (actorIRI string, err error) { - remoteuser, errRemoteUser := getRemoteUserFromHandle(app, handle) - if errRemoteUser != nil { +func (db *datastore) GetProfilePageFromHandle(app *App, handle string) (string, error) { + actorIRI := "" + 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 // handle from a previous version actorIRI = RemoteLookup(handle) _, errRemoteUser := getRemoteUser(app, actorIRI) // if it exists then we need to update the handle if errRemoteUser == nil { - // query := "UPDATE remoteusers SET handle='" + handle + "' WHERE actor_id='" + iri + "';" - // log.Info(query) - _, err := app.db.Exec("UPDATE remoteusers SET handle=? WHERE actor_id=?;", handle, actorIRI) + _, err := app.db.Exec("UPDATE remoteusers SET handle = ? WHERE actor_id = ?", handle, actorIRI) if err != nil { 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 { log.Error("Couldn't fetch remote actor", err) } - fmt.Println(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 debugging { + 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 { log.Error("Can't insert remote user in database", err) return "", err } } } else { - actorIRI = remoteuser.ActorID + actorIRI = remoteUser.ActorID } return actorIRI, nil } diff --git a/posts.go b/posts.go index 5115c92..a918531 100644 --- a/posts.go +++ b/posts.go @@ -1,5 +1,5 @@ /* - * Copyright © 2018-2019 A Bunch Tell LLC. + * Copyright © 2018-2020 A Bunch Tell LLC. * * This file is part of WriteFreely. * @@ -1176,7 +1176,7 @@ func (p *PublicPost) ActivityObject(app *App) *activitystreams.Object { mentions := mentionRegex.FindAllString(content, -1) for _, handle := range mentions { - actorIRI, err := app.db.getProfilePageFromHandle(app, handle) + actorIRI, err := app.db.GetProfilePageFromHandle(app, handle) if err != nil { log.Info("Can't find this user either in the database nor in the remote instance") return nil