From baa43bdb40d44758cd38bb20a6a5a0ba25b7b4e6 Mon Sep 17 00:00:00 2001 From: JP Hastings-Spital Date: Fri, 10 May 2024 10:03:10 +0100 Subject: [PATCH] Little hack to prevent webmentions overwriting --- .vscode/settings.json | 3 ++- tools/import/webmentionio/main.go | 25 +++++++++++++++++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 6ea7d95b..c17e55a3 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -13,7 +13,8 @@ "shortener", "shorteners", "shortlink", - "shortlinks" + "shortlinks", + "Webmention" ], "cSpell.language": "en-GB" } \ No newline at end of file diff --git a/tools/import/webmentionio/main.go b/tools/import/webmentionio/main.go index 651e4d5f..78bfa717 100644 --- a/tools/import/webmentionio/main.go +++ b/tools/import/webmentionio/main.go @@ -8,6 +8,7 @@ import ( "net/http" "os" "path" + "slices" "sort" "strings" "time" @@ -20,6 +21,10 @@ import ( const webmentions = "https://webmention.io/api/mentions.jf2?domain=%s&token=%s" const siteTarget = "https://www.byjp.me/" +var myURLs = []string{ + "https://bsky.app/profile/byjp.me", +} + type Config struct { Domain string Token string @@ -39,7 +44,7 @@ func main() { switch err { case nil: // Proceed - case ErrNoEntry, ErrIsPrivate, ErrIncorrectTarget: + case ErrNoEntry, ErrIsPrivate, ErrIncorrectTarget, ErrIsMine: // Skip continue default: @@ -89,6 +94,14 @@ func addInteraction(jsonPath string, newIn synd.Interaction) error { added := false for idx, in := range inf.Interactions { if in.GUID == newIn.GUID { + // A hack because webmentions.io can't process emoji properly currently + if strings.Contains(newIn.Author.Name, "????") { + newIn.Author.Name = in.Author.Name + } + if strings.Contains(newIn.Comment, "????") { + newIn.Comment = in.Comment + } + inf.Interactions[idx] = newIn added = true } @@ -115,6 +128,7 @@ func addInteraction(jsonPath string, newIn synd.Interaction) error { var ErrNoEntry = errors.New("JF2 item isn't an entry") var ErrIsPrivate = errors.New("JF2 item is set to private") var ErrIncorrectTarget = errors.New("JF2 item describes a different target site") +var ErrIsMine = errors.New("JF2 item was created by the author") func parseJF2(m Mention, isValidPath func(string) bool) (string, synd.Interaction, error) { if m.Type != "entry" { @@ -135,6 +149,14 @@ func parseJF2(m Mention, isValidPath func(string) bool) (string, synd.Interactio return "", synd.Interaction{}, ErrIncorrectTarget } + if len(sitePath) == 0 { + return "", synd.Interaction{}, ErrIncorrectTarget + } + + if slices.Contains(myURLs, m.Author.URL) { + return "", synd.Interaction{}, ErrIsMine + } + i := synd.Interaction{ GUID: fmt.Sprintf("webmentions.io#%d", m.WebmentionID), URL: m.URL, @@ -169,7 +191,6 @@ func parseJF2(m Mention, isValidPath func(string) bool) (string, synd.Interactio case "in-reply-to": i.Emoji = "💬" case "mention-of": - i.Comment = "_A wibble_" i.Emoji = "🗣️" }