From 507acc7e1ca97e3260ed50f973acde28d1d35def Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Mon, 8 Jun 2020 13:50:43 -0400 Subject: [PATCH 1/2] Support AP-style mentions of centralized social media accounts This allows users to mention users on the following non-ActivityPub social media sites: - twitter.com - medium.com It also adds missing error handling in federatePost(). --- activitypub.go | 4 ++++ database.go | 11 +++++++++++ silobridge.go | 10 ++++++++++ 3 files changed, 25 insertions(+) create mode 100644 silobridge.go diff --git a/activitypub.go b/activitypub.go index 328284f..d34e70c 100644 --- a/activitypub.go +++ b/activitypub.go @@ -699,6 +699,10 @@ func federatePost(app *App, p *PublicPost, collID int64, isUpdate bool) error { // I don't believe we'd ever have too many mentions in a single post that this // could become a burden. remoteUser, err := getRemoteUser(app, tag.HRef) + if err != nil { + log.Error("Unable to find remote user %s. Skipping: %v", tag.HRef, err) + continue + } err = makeActivityPost(app.cfg.App.Host, actor, remoteUser.Inbox, activity) if err != nil { log.Error("Couldn't post! %v", err) diff --git a/database.go b/database.go index 0eee612..a878010 100644 --- a/database.go +++ b/database.go @@ -2652,6 +2652,17 @@ func handleFailedPostInsert(err error) error { func (db *datastore) GetProfilePageFromHandle(app *App, handle string) (string, error) { handle = strings.TrimLeft(handle, "@") actorIRI := "" + parts := strings.Split(handle, "@") + if len(parts) != 2 { + return "", fmt.Errorf("invalid handle format") + } + domain := parts[1] + + // Check non-AP instances + if prefix, ok := fakeAPInstances[domain]; ok { + return "https://" + domain + "/" + prefix + parts[0], 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 diff --git a/silobridge.go b/silobridge.go new file mode 100644 index 0000000..dcd4c9f --- /dev/null +++ b/silobridge.go @@ -0,0 +1,10 @@ +package writefreely + +// fakeAPInstances contains a list of sites that we allow writers to mention +// with the @handle@instance.tld syntax, plus the corresponding prefix to +// insert between `https://instance.tld/` and `handle` (e.g. +// https://medium.com/@handle) +var fakeAPInstances = map[string]string{ + "twitter.com": "", + "medium.com": "@", +} From 7fa78c225586be887abb0d5069e2430a54fc2ed0 Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Thu, 13 Aug 2020 12:33:35 -0400 Subject: [PATCH 2/2] Move fakeAPInstances to web-core/silobridge package This adds support for mentioning profiles on the following sites: - deviantart.com - facebook.com - flickr.com - github.com - instagram.com - reddit.com - wattpad.com - youtube.com --- database.go | 5 +++-- go.mod | 2 +- go.sum | 2 ++ silobridge.go | 10 ---------- 4 files changed, 6 insertions(+), 13 deletions(-) delete mode 100644 silobridge.go diff --git a/database.go b/database.go index a878010..87b0398 100644 --- a/database.go +++ b/database.go @@ -14,6 +14,7 @@ import ( "context" "database/sql" "fmt" + "github.com/writeas/web-core/silobridge" wf_db "github.com/writeas/writefreely/db" "net/http" "strings" @@ -2659,8 +2660,8 @@ func (db *datastore) GetProfilePageFromHandle(app *App, handle string) (string, domain := parts[1] // Check non-AP instances - if prefix, ok := fakeAPInstances[domain]; ok { - return "https://" + domain + "/" + prefix + parts[0], nil + if siloProfileURL := silobridge.Profile(parts[0], domain); siloProfileURL != "" { + return siloProfileURL, nil } remoteUser, err := getRemoteUserFromHandle(app, handle) diff --git a/go.mod b/go.mod index 1989258..57d4fd2 100644 --- a/go.mod +++ b/go.mod @@ -46,7 +46,7 @@ require ( github.com/writeas/nerds v1.0.0 github.com/writeas/saturday v1.7.2-0.20200427193424-392b95a03320 github.com/writeas/slug v1.2.0 - github.com/writeas/web-core v1.2.0 + github.com/writeas/web-core v1.2.1-0.20200813161734-68a680d1b03c github.com/writefreely/go-nodeinfo v1.2.0 golang.org/x/crypto v0.0.0-20200109152110-61a87790db17 golang.org/x/lint v0.0.0-20181217174547-8f45f776aaf1 // indirect diff --git a/go.sum b/go.sum index 87b5281..46df602 100644 --- a/go.sum +++ b/go.sum @@ -167,6 +167,8 @@ github.com/writeas/slug v1.2.0 h1:EMQ+cwLiOcA6EtFwUgyw3Ge18x9uflUnOnR6bp/J+/g= github.com/writeas/slug v1.2.0/go.mod h1:RE8shOqQP3YhsfsQe0L3RnuejfQ4Mk+JjY5YJQFubfQ= github.com/writeas/web-core v1.2.0 h1:CYqvBd+byi1cK4mCr1NZ6CjILuMOFmiFecv+OACcmG0= github.com/writeas/web-core v1.2.0/go.mod h1:vTYajviuNBAxjctPp2NUYdgjofywVkxUGpeaERF3SfI= +github.com/writeas/web-core v1.2.1-0.20200813161734-68a680d1b03c h1:/aPb8WKtC+Ga/xUEcME0iX3VKBeeJ02kXCaROaZ21SE= +github.com/writeas/web-core v1.2.1-0.20200813161734-68a680d1b03c/go.mod h1:vTYajviuNBAxjctPp2NUYdgjofywVkxUGpeaERF3SfI= github.com/writefreely/go-nodeinfo v1.2.0 h1:La+YbTCvmpTwFhBSlebWDDL81N88Qf/SCAvRLR7F8ss= github.com/writefreely/go-nodeinfo v1.2.0/go.mod h1:UTvE78KpcjYOlRHupZIiSEFcXHioTXuacCbHU+CAcPg= golang.org/x/crypto v0.0.0-20180527072434-ab813273cd59 h1:hk3yo72LXLapY9EXVttc3Z1rLOxT9IuAPPX3GpY2+jo= diff --git a/silobridge.go b/silobridge.go deleted file mode 100644 index dcd4c9f..0000000 --- a/silobridge.go +++ /dev/null @@ -1,10 +0,0 @@ -package writefreely - -// fakeAPInstances contains a list of sites that we allow writers to mention -// with the @handle@instance.tld syntax, plus the corresponding prefix to -// insert between `https://instance.tld/` and `handle` (e.g. -// https://medium.com/@handle) -var fakeAPInstances = map[string]string{ - "twitter.com": "", - "medium.com": "@", -}