Skip to content

Commit

Permalink
fix: image link is now included in text
Browse files Browse the repository at this point in the history
  • Loading branch information
Preloading committed Jan 16, 2025
1 parent f4857b9 commit d02c093
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
7 changes: 7 additions & 0 deletions bluesky/blueskyapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ type PostReason struct {
type Embed struct {
Type string `json:"$type"`
Images []Image `json:"images,omitempty"`
Video `json:"omitempty"`
}

type Image struct {
Expand All @@ -88,6 +89,12 @@ type Image struct {
Image Blob `json:"image"`
}

type Video struct {
Alt string `json:"alt"`
AspectRatio AspectRatio `json:"aspectRatio"`
Video Blob `json:"video"`
}

type AspectRatio struct {
Height int `json:"height"`
Width int `json:"width"`
Expand Down
16 changes: 16 additions & 0 deletions config.sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@
# BAD: http://127.0.0.1:3000/
CDN_URL: 'http://127.0.0.1:3000'

# The URL that will be displayed in the text
# This is the URL that will be displayed in the text of the tweet
# This is not the link seen by the viewer, not where it will actually go to.

# You can also set it to nothing if you don't want a URL to be displayed.
# This does break older clients, and has a useless URL area in the tweet.

# {shortblob} is the last few letters of the bluesky cdn blob
# {fullblob} is the full bluesky blob
# {user_did} is the image owner's DID

IMG_DISPLAY_TEXT: 'pic.twitter.com/{shortblob}'

# Same as the above, but for videos
VID_DISPLAY_TEXT: 'pic.twitter.com/{shortblob}'

# SERVER_PORT is the port the server will listen on.
SERVER_PORT: 3000

Expand Down
5 changes: 5 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ type Config struct {
DatabasePath string `mapstructure:"DATABASE_PATH"`

UseXForwardedFor bool `mapstructure:"USE_X_FORWARDED_FOR"`

ImgDisplayText string `mapstructure:"IMG_DISPLAY_TEXT"`
VidDisplayText string `mapstructure:"VID_DISPLAY_TEXT"`
}

// Loads our config files.
Expand All @@ -46,6 +49,8 @@ func LoadConfig() (*Config, error) {
viper.SetDefault("TRACK_ANALYTICS", true)
viper.SetDefault("CDN_URL", "http://localhost:3000")
viper.SetDefault("USE_X_FORWARDED_FOR", false)
viper.SetDefault("IMG_DISPLAY_TEXT", "pic.twitter.com/{shortblob}")
viper.SetDefault("VID_DISPLAY_TEXT", "pic.twitter.com/{shortblob}")

// Read config file
if err := viper.ReadInConfig(); err != nil {
Expand Down
38 changes: 37 additions & 1 deletion twitterv1/post_viewing.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/url"
"slices"
"strconv"
"strings"
"time"

blueskyapi "github.com/Preloading/TwitterAPIBridge/bluesky"
Expand Down Expand Up @@ -297,14 +298,38 @@ func TranslatePostToTweet(tweet blueskyapi.Post, replyMsgBskyURI string, replyUs

id := 1
for _, image := range tweet.Record.Embed.Images {
//fmt.Println("Image:", image.Image.Ref.Link)
// Add the image "url" to the text
startLen, endLen := 0, 0
displayURL := configData.ImgDisplayText
if displayURL != "" {
displayURL = strings.ReplaceAll(displayURL, "{shortblob}", image.Image.Ref.Link[len(image.Image.Ref.Link)-6:])
displayURL = strings.ReplaceAll(displayURL, "{fullblob}", image.Image.Ref.Link)
displayURL = strings.ReplaceAll(displayURL, "{user_did}", tweet.Author.DID)

if len(processedText) == 0 {
endLen = len(displayURL)

processedText = displayURL
} else {
startLen = len(processedText) + 1
endLen = (len(processedText) + 1) + len(displayURL)

processedText = processedText + " " + displayURL
}
}

// Process each image
tweetEntities.Media = append(tweetEntities.Media, bridge.Media{
Type: "photo",
ID: int64(id),
IDStr: strconv.Itoa(id),
MediaURL: configData.CdnURL + "/cdn/img/bsky/" + tweet.Author.DID + "/" + image.Image.Ref.Link + ".jpg",
MediaURLHttps: configData.CdnURL + "/cdn/img/bsky/" + tweet.Author.DID + "/" + image.Image.Ref.Link + ".jpg",

DisplayURL: displayURL,
ExpandedURL: configData.CdnURL + "/cdn/img/bsky/" + tweet.Author.DID + "/" + image.Image.Ref.Link + ".jpg",
URL: configData.CdnURL + "/cdn/img/bsky/" + tweet.Author.DID + "/" + image.Image.Ref.Link + ".jpg",

Sizes: bridge.MediaSize{
Thumb: func() bridge.Size {
w, h := image.AspectRatio.Width, image.AspectRatio.Height
Expand Down Expand Up @@ -357,9 +382,20 @@ func TranslatePostToTweet(tweet blueskyapi.Post, replyMsgBskyURI string, replyUs
Resize: "fit",
},
},

Start: startLen,
End: endLen,
Indices: []int{
startLen,
endLen,
},
})
id++
}

// Videos
// TODO

for _, faucet := range tweet.Record.Facets {
// I haven't seen this exceed 1 element yet
// if len(faucet.Features) > 1 {
Expand Down

0 comments on commit d02c093

Please sign in to comment.