mirror of
https://github.com/aljazceru/njump.git
synced 2026-01-31 11:44:34 +01:00
turn user-agent style possibilities into constants.
This commit is contained in:
2
pages.go
2
pages.go
@@ -203,7 +203,7 @@ type NotePage struct {
|
||||
ParentLink template.HTML
|
||||
Proxy string
|
||||
SeenOn []string
|
||||
Style string
|
||||
IsTwitter bool
|
||||
Subject string
|
||||
TextImageURL string
|
||||
Title string
|
||||
|
||||
@@ -81,7 +81,7 @@ func renderEvent(w http.ResponseWriter, r *http.Request) {
|
||||
useTextImage := (data.event.Kind == 1 || data.event.Kind == 30023) &&
|
||||
data.image == "" && data.video == "" && len(data.event.Content) > 133
|
||||
|
||||
if style == "telegram" || r.URL.Query().Get("tgiv") == "true" {
|
||||
if style == StyleTelegram || r.URL.Query().Get("tgiv") == "true" {
|
||||
// do telegram instant preview (only works on telegram mobile apps, not desktop)
|
||||
if data.event.Kind == 30023 || // do it for longform articles
|
||||
(data.event.Kind == 1 && len(data.event.Content) > 650) || // or very long notes
|
||||
@@ -90,7 +90,7 @@ func renderEvent(w http.ResponseWriter, r *http.Request) {
|
||||
data.templateId = TelegramInstantView
|
||||
useTextImage = false
|
||||
}
|
||||
} else if style == "slack" || style == "discord" {
|
||||
} else if style == StyleSlack || style == StyleDiscord {
|
||||
useTextImage = false
|
||||
}
|
||||
|
||||
@@ -261,7 +261,7 @@ func renderEvent(w http.ResponseWriter, r *http.Request) {
|
||||
},
|
||||
DetailsPartial: detailsData,
|
||||
ClientsPartial: ClientsPartial{
|
||||
Clients: generateClientList(code, data.event),
|
||||
Clients: generateClientList(style, code, data.event),
|
||||
},
|
||||
|
||||
AuthorLong: data.authorLong,
|
||||
@@ -275,7 +275,7 @@ func renderEvent(w http.ResponseWriter, r *http.Request) {
|
||||
Oembed: oembed,
|
||||
ParentLink: data.parentLink,
|
||||
Proxy: "https://" + host + "/njump/proxy?src=",
|
||||
Style: style,
|
||||
IsTwitter: style == StyleTwitter,
|
||||
Subject: subject,
|
||||
TextImageURL: textImageURL,
|
||||
Title: title,
|
||||
|
||||
@@ -151,14 +151,14 @@ func normalizeText(input []string, breakWords bool) []string {
|
||||
return lines
|
||||
}
|
||||
|
||||
func drawImage(lines []string, font *truetype.Font, style string) (image.Image, error) {
|
||||
func drawImage(lines []string, font *truetype.Font, style Style) (image.Image, error) {
|
||||
width := 700
|
||||
height := 525
|
||||
paddingLeft := 0
|
||||
switch style {
|
||||
case "telegram":
|
||||
case StyleTelegram:
|
||||
paddingLeft = 15
|
||||
case "twitter":
|
||||
case StyleTwitter:
|
||||
height = width * 268 / 512
|
||||
}
|
||||
|
||||
|
||||
@@ -41,7 +41,7 @@ func renderProfile(w http.ResponseWriter, r *http.Request, code string) {
|
||||
Kind: data.event.Kind,
|
||||
},
|
||||
ClientsPartial: ClientsPartial{
|
||||
Clients: generateClientList(code, data.event),
|
||||
Clients: generateClientList(getPreviewStyle(r), code, data.event),
|
||||
},
|
||||
|
||||
Metadata: data.metadata,
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<title>{{.TitleizedContent}}</title>
|
||||
|
||||
<meta property="og:title" content="{{.Title}}" />
|
||||
{{ if eq .Style "twitter" }}
|
||||
{{ if .IsTwitter }}
|
||||
<meta name="twitter:title" content="{{.TwitterTitle}}" />
|
||||
{{ end }}
|
||||
|
||||
@@ -97,7 +97,7 @@
|
||||
></div>
|
||||
|
||||
<article
|
||||
class="prose dark:prose-invert sm:prose-a:text-justify prose-headings:font-light prose-blockquote:mx-0 prose-blockquote:my-8 prose-blockquote:pl-4 prose-blockquote:pr-0 prose-blockquote:py-2 prose-blockquote:border-l-05rem prose-blockquote:border-solid prose-blockquote:border-l-gray-100 dark:prose-blockquote:border-l-zinc-800 prose-p:m-0 prose-p:mb-2 prose-cite:text-sm prose-ul:m-0 prose-ul:p-0 prose-ul:pl-4 prose-ol:m-0 prose-ol:p-0 prose-ol:pl-4 prose-li:mb-2 prose mb-6 leading-5"
|
||||
class="prose-cite:text-sm prose prose mb-6 leading-5 dark:prose-invert prose-headings:font-light prose-p:m-0 prose-p:mb-2 prose-blockquote:mx-0 prose-blockquote:my-8 prose-blockquote:border-l-05rem prose-blockquote:border-solid prose-blockquote:border-l-gray-100 prose-blockquote:py-2 prose-blockquote:pl-4 prose-blockquote:pr-0 prose-ol:m-0 prose-ol:p-0 prose-ol:pl-4 prose-ul:m-0 prose-ul:p-0 prose-ul:pl-4 prose-li:mb-2 dark:prose-blockquote:border-l-zinc-800 sm:prose-a:text-justify"
|
||||
>
|
||||
{{ if (not (eq "" .Subject))}}
|
||||
<h1 class="text-2xl">{{.Subject}}</h1>
|
||||
|
||||
44
utils.go
44
utils.go
@@ -123,7 +123,7 @@ type ClientReference struct {
|
||||
URL template.URL
|
||||
}
|
||||
|
||||
func generateClientList(code string, event *nostr.Event) []ClientReference {
|
||||
func generateClientList(style Style, code string, event *nostr.Event) []ClientReference {
|
||||
if event.Kind == 1 || event.Kind == 6 {
|
||||
return []ClientReference{
|
||||
{ID: "native", Name: "Your native client", URL: template.URL("nostr:" + code)},
|
||||
@@ -172,10 +172,26 @@ func generateRelayBrowserClientList(host string) []ClientReference {
|
||||
}
|
||||
}
|
||||
|
||||
func getPreviewStyle(r *http.Request) string {
|
||||
type Style string
|
||||
|
||||
const (
|
||||
StyleTelegram Style = "telegram"
|
||||
StyleTwitter = "twitter"
|
||||
StyleIos = "ios"
|
||||
StyleAndroid = "android"
|
||||
StyleMattermost = "mattermost"
|
||||
StyleSlack = "slack"
|
||||
StyleDiscord = "discord"
|
||||
StyleWhatsapp = "whatsapp"
|
||||
StyleIframely = "iframely"
|
||||
StyleNormal = "normal"
|
||||
StyleUnknown = "unknown"
|
||||
)
|
||||
|
||||
func getPreviewStyle(r *http.Request) Style {
|
||||
if style := r.URL.Query().Get("style"); style != "" {
|
||||
// debug mode
|
||||
return style
|
||||
return Style(style)
|
||||
}
|
||||
|
||||
ua := strings.ToLower(r.Header.Get("User-Agent"))
|
||||
@@ -183,23 +199,27 @@ func getPreviewStyle(r *http.Request) string {
|
||||
|
||||
switch {
|
||||
case strings.Contains(ua, "telegrambot"):
|
||||
return "telegram"
|
||||
return StyleTelegram
|
||||
case strings.Contains(ua, "twitterbot"):
|
||||
return "twitter"
|
||||
return StyleTwitter
|
||||
case strings.Contains(ua, "iphone"), strings.Contains(ua, "ipad"), strings.Contains(ua, "ipod"):
|
||||
return StyleIos
|
||||
case strings.Contains(ua, "android"):
|
||||
return StyleAndroid
|
||||
case strings.Contains(ua, "mattermost"):
|
||||
return "mattermost"
|
||||
return StyleMattermost
|
||||
case strings.Contains(ua, "slack"):
|
||||
return "slack"
|
||||
return StyleSlack
|
||||
case strings.Contains(ua, "discord"):
|
||||
return "discord"
|
||||
return StyleDiscord
|
||||
case strings.Contains(ua, "whatsapp"):
|
||||
return "whatsapp"
|
||||
return StyleWhatsapp
|
||||
case strings.Contains(ua, "iframely"):
|
||||
return "iframely"
|
||||
return StyleIframely
|
||||
case strings.Contains(accept, "text/html"):
|
||||
return "normal"
|
||||
return StyleNormal
|
||||
default:
|
||||
return "unknown"
|
||||
return StyleUnknown
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user