about summary refs log tree commit diff
path: root/bskyweb
diff options
context:
space:
mode:
Diffstat (limited to 'bskyweb')
-rw-r--r--bskyweb/cmd/bskyweb/filters.go28
-rw-r--r--bskyweb/cmd/bskyweb/filters_test.go61
-rw-r--r--bskyweb/templates/base.html1
-rw-r--r--bskyweb/templates/home.html1
-rw-r--r--bskyweb/templates/post.html7
-rw-r--r--bskyweb/templates/profile.html2
6 files changed, 96 insertions, 4 deletions
diff --git a/bskyweb/cmd/bskyweb/filters.go b/bskyweb/cmd/bskyweb/filters.go
new file mode 100644
index 000000000..a92cc606b
--- /dev/null
+++ b/bskyweb/cmd/bskyweb/filters.go
@@ -0,0 +1,28 @@
+package main
+
+import (
+	"net/url"
+
+	"github.com/flosch/pongo2/v6"
+)
+
+func init() {
+	pongo2.RegisterFilter("canonicalize_url", filterCanonicalizeURL)
+}
+
+func filterCanonicalizeURL(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
+	urlStr := in.String()
+
+	parsedURL, err := url.Parse(urlStr)
+	if err != nil {
+		// If parsing fails, return the original URL
+		return in, nil
+	}
+
+	// Remove query parameters and fragment
+	parsedURL.RawQuery = ""
+	parsedURL.Fragment = ""
+
+	// Return the cleaned URL
+	return pongo2.AsValue(parsedURL.String()), nil
+}
diff --git a/bskyweb/cmd/bskyweb/filters_test.go b/bskyweb/cmd/bskyweb/filters_test.go
new file mode 100644
index 000000000..a63ad0317
--- /dev/null
+++ b/bskyweb/cmd/bskyweb/filters_test.go
@@ -0,0 +1,61 @@
+package main
+
+import (
+	"testing"
+
+	"github.com/flosch/pongo2/v6"
+)
+
+func TestCanonicalizeURLFilter(t *testing.T) {
+	tests := []struct {
+		name     string
+		input    string
+		expected string
+	}{
+		{
+			name:     "clean URL",
+			input:    "https://bsky.app/profile/user",
+			expected: "https://bsky.app/profile/user",
+		},
+		{
+			name:     "URL with query params",
+			input:    "https://bsky.app/profile/user?utm_source=test",
+			expected: "https://bsky.app/profile/user",
+		},
+		{
+			name:     "URL with multiple params",
+			input:    "https://bsky.app/profile/user?utm_source=twitter&utm_campaign=test",
+			expected: "https://bsky.app/profile/user",
+		},
+		{
+			name:     "URL with fragment",
+			input:    "https://bsky.app/profile/user#section",
+			expected: "https://bsky.app/profile/user",
+		},
+		{
+			name:     "URL with both params and fragment",
+			input:    "https://bsky.app/profile/user?param=1#section",
+			expected: "https://bsky.app/profile/user",
+		},
+		{
+			name:     "malformed URL",
+			input:    "not-a-url",
+			expected: "not-a-url", // Should return original on error
+		},
+	}
+
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			inputValue := pongo2.AsValue(tt.input)
+			result, err := filterCanonicalizeURL(inputValue, nil)
+			if err != nil {
+				t.Errorf("filterCanonicalizeURL() error = %v", err)
+				return
+			}
+
+			if result.String() != tt.expected {
+				t.Errorf("filterCanonicalizeURL() = %v, want %v", result.String(), tt.expected)
+			}
+		})
+	}
+}
diff --git a/bskyweb/templates/base.html b/bskyweb/templates/base.html
index d72494da1..715b39fd2 100644
--- a/bskyweb/templates/base.html
+++ b/bskyweb/templates/base.html
@@ -95,6 +95,7 @@
   <meta name="application-name" content="Bluesky">
   <meta name="generator" content="bskyweb">
   <meta property="og:site_name" content="Bluesky Social" />
+  <meta name="twitter:site" content="@bluesky" />
   <link type="application/activity+json" href="" />
 
   {% block html_head_extra -%}{%- endblock %}
diff --git a/bskyweb/templates/home.html b/bskyweb/templates/home.html
index 9e5c670a2..0e148b37e 100644
--- a/bskyweb/templates/home.html
+++ b/bskyweb/templates/home.html
@@ -17,7 +17,6 @@
   <meta property="og:image" content="https://bsky.app/static/social-card-default-gradient.png" />
   <meta property="twitter:image" content="https://bsky.app/static/social-card-default-gradient.png"  />
 
-  <meta name="twitter:site" content="@bluesky" />
   <meta name="twitter:card" content="summary_large_image" />
 {%- endblock %}
 
diff --git a/bskyweb/templates/post.html b/bskyweb/templates/post.html
index 963af2065..6473bc284 100644
--- a/bskyweb/templates/post.html
+++ b/bskyweb/templates/post.html
@@ -14,7 +14,7 @@
   <meta property="profile:username" content="{{ profileView.Handle }}">
   {%- if requestURI %}
   <meta property="og:url" content="{{ requestURI }}">
-  <link rel="canonical" href="{{ requestURI }}" />
+  <link rel="canonical" href="{{ requestURI|canonicalize_url }}" />
   {% endif -%}
   {%- if postView.Author.DisplayName %}
   <meta property="og:title" content="{{ postView.Author.DisplayName }} (@{{ postView.Author.Handle }})">
@@ -24,15 +24,18 @@
   {%- if postText %}
   <meta name="description" content="{{ postText }}">
   <meta property="og:description" content="{{ postText }}">
+  <meta property="twitter:description" content="{{ postText }}">
   {% endif -%}
   {%- if imgThumbUrls %}
   {% for imgThumbUrl in imgThumbUrls %}
   <meta property="og:image" content="{{ imgThumbUrl }}">
+  <meta property="twitter:image" content="{{ imgThumbUrl }}">
   {% endfor %}
   <meta name="twitter:card" content="summary_large_image">
   {%- elif postView.Author.Avatar %}
-  {# Don't use avatar image in cards; usually looks bad #}
   <meta name="twitter:card" content="summary">
+  <meta property="og:image" content="{{ postView.Author.Avatar }}">
+  <meta property="twitter:image" content="{{ postView.Author.Avatar }}">
   {% endif %}
   <meta name="twitter:label1" content="Posted At">
   <meta name="twitter:value1" content="{{ postView.IndexedAt }}">
diff --git a/bskyweb/templates/profile.html b/bskyweb/templates/profile.html
index af4180dc1..8506a9cff 100644
--- a/bskyweb/templates/profile.html
+++ b/bskyweb/templates/profile.html
@@ -15,7 +15,7 @@
   <meta property="profile:username" content="{{ profileView.Handle }}">
   {%- if requestURI %}
   <meta property="og:url" content="{{ requestURI }}">
-  <link rel="canonical" href="{{ requestURI }}" />
+  <link rel="canonical" href="{{ requestURI|canonicalize_url }}" />
   {% endif -%}
   {%- if profileView.DisplayName %}
   <meta property="og:title" content="{{ profileView.DisplayName }} (@{{ profileView.Handle }})">