Pārlūkot izejas kodu

Merge pull request #282 from writeas/patch-ap-mentions

Clean up ActivityPub mentions
pull/288/head
Matt Baer pirms 4 gadiem
committed by GitHub
vecāks
revīzija
1d25b38eb7
Šim parakstam datu bāzē netika atrasta zināma atslēga GPG atslēgas ID: 4AEE18F83AFDEB23
5 mainītis faili ar 16 papildinājumiem un 13 dzēšanām
  1. +4
    -1
      activitypub.go
  2. +4
    -3
      database.go
  3. +2
    -2
      migrations/v6.go
  4. +3
    -4
      posts.go
  5. +3
    -3
      webfinger.go

+ 4
- 1
activitypub.go Parādīt failu

@@ -708,7 +708,8 @@ func federatePost(app *App, p *PublicPost, collID int64, isUpdate bool) error {

func getRemoteUser(app *App, actorID string) (*RemoteUser, error) {
u := RemoteUser{ActorID: actorID}
err := app.db.QueryRow("SELECT id, inbox, shared_inbox, handle FROM remoteusers WHERE actor_id = ?", actorID).Scan(&u.ID, &u.Inbox, &u.SharedInbox, &u.Handle)
var handle sql.NullString
err := app.db.QueryRow("SELECT id, inbox, shared_inbox, handle FROM remoteusers WHERE actor_id = ?", actorID).Scan(&u.ID, &u.Inbox, &u.SharedInbox, &handle)
switch {
case err == sql.ErrNoRows:
return nil, impart.HTTPError{http.StatusNotFound, "No remote user with that ID."}
@@ -717,6 +718,8 @@ func getRemoteUser(app *App, actorID string) (*RemoteUser, error) {
return nil, err
}

u.Handle = handle.String

return &u, nil
}



+ 4
- 3
database.go Parādīt failu

@@ -2643,6 +2643,7 @@ func handleFailedPostInsert(err error) error {
}

func (db *datastore) GetProfilePageFromHandle(app *App, handle string) (string, error) {
handle = strings.TrimLeft(handle, "@")
actorIRI := ""
remoteUser, err := getRemoteUserFromHandle(app, handle)
if err != nil {
@@ -2655,21 +2656,21 @@ func (db *datastore) GetProfilePageFromHandle(app *App, handle string) (string,
if errRemoteUser == nil {
_, 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)
log.Error("Couldn't update handle '%s' for user %s", handle, actorIRI)
}
} else {
// this probably means we don't have the user in the table so let's try to insert it
// here we need to ask the server for the inboxes
remoteActor, err := activityserve.NewRemoteActor(actorIRI)
if err != nil {
log.Error("Couldn't fetch remote actor", err)
log.Error("Couldn't fetch remote actor: %v", err)
}
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)
log.Error("Couldn't insert remote user: %v", err)
return "", err
}
}


+ 2
- 2
migrations/v6.go Parādīt failu

@@ -1,5 +1,5 @@
/*
* Copyright © 2019 A Bunch Tell LLC.
* Copyright © 2019-2020 A Bunch Tell LLC.
*
* This file is part of WriteFreely.
*
@@ -13,7 +13,7 @@ package migrations
func supportActivityPubMentions(db *datastore) error {
t, err := db.Begin()

_, err = t.Exec(`ALTER TABLE remoteusers ADD COLUMN handle ` + db.typeVarChar(255) + ` DEFAULT '' NOT NULL`)
_, err = t.Exec(`ALTER TABLE remoteusers ADD COLUMN handle ` + db.typeVarChar(255) + ` NULL`)
if err != nil {
t.Rollback()
return err


+ 3
- 4
posts.go Parādīt failu

@@ -1175,14 +1175,13 @@ func (p *PublicPost) ActivityObject(app *App) *activitystreams.Object {

stripper := bluemonday.StrictPolicy()
content := stripper.Sanitize(p.Content)
mentionRegex := regexp.MustCompile(`@[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]+\b`)
mentions := mentionRegex.FindAllString(content, -1)
mentions := mentionReg.FindAllString(content, -1)

for _, handle := range mentions {
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
log.Info("Couldn't find user '%s' locally or remotely", handle)
continue
}
mentionedUsers[handle] = actorIRI
}


+ 3
- 3
webfinger.go Parādīt failu

@@ -101,20 +101,20 @@ func RemoteLookup(handle string) string {
parts := strings.Split(handle, "@")
resp, err := http.Get("https://" + parts[1] + "/.well-known/webfinger?resource=acct:" + handle)
if err != nil {
log.Error("Error performing webfinger request", err)
log.Error("Error on webfinger request: %v", err)
return ""
}

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Error("Error reading webfinger response", err)
log.Error("Error on webfinger response: %v", err)
return ""
}

var result webfinger.Resource
err = json.Unmarshal(body, &result)
if err != nil {
log.Error("Unsupported webfinger response received: %v", err)
log.Error("Unable to parse webfinger response: %v", err)
return ""
}



Notiek ielāde…
Atcelt
Saglabāt