ShreyanJain9 / bskyrb

Ruby Gem for interacting with BlueSky/AT Protocol
MIT License
53 stars 8 forks source link

Links don't seem to be clickable #10

Open chipotle opened 9 months ago

chipotle commented 9 months ago

I've been trying to write an autoposter that just posts "#{title} - #{link}" style posts from a blog, and I absolutely cannot get clickable links to be posted.

As far as I can tell, the link_pattern in the detect_facets method isn't working. I did some tests in local code with a much simpler regex pattern and your function, and this seems to be getting the right response. Unfortunately, I can't for the life of me figure out how to get this code into a local copy of the library that my autoposter will actually work with, because I am apparently hopelessly confused by Ruby's dependency management or how to build this specific gem or something. (I used your provided install-local.sh script with my change to the gem, but no matter what I do I get a uninitialized constant Bskyrb::Credentials (NameError) unless I load from the published Gem, which works flawlessly, except that the facets aren't being processed correctly and the links aren't clickable.)

This is the simple dumb pattern I used and the test. It basically just assumes anything that starts with https:// or http:// is a URL up until the next whitespace character. The only change to the enum_for.each loop is in the URI.parse, where I removed the trailing / (if one exists it will be grabbed by the regex).

text = 'New post: "Foobar" https://mywebsite.net/foo-bar/ and also https://other.com/thing and so on'
link_pattern = /(https?):\/\/(\S+)/

facets = []

text.enum_for(:scan, link_pattern).each do |m|
  index_start = Regexp.last_match.offset(0).first
  index_end = Regexp.last_match.offset(0).last
  m.compact!
  path = "#{m[1]}#{m[2..].join("")}".strip
  facets.push(
    "$type" => "app.bsky.richtext.facet",
    "index" => {
      "byteStart" => index_start,
      "byteEnd" => index_end,
    },
    "features" => [
      {
        "uri" => URI.parse("#{m[0]}://#{path}").normalize.to_s, # this is the matched link
        "$type" => "app.bsky.richtext.facet#link",
      },
    ],
  )
end

puts facets.inspect
ShreyanJain9 commented 9 months ago

Sorry, I haven't looked at this for a while and I know a lot of things might be a bit broken. I'll probably try to fix this over the weekend. Do you mind if I just use your regex?

chipotle commented 9 months ago

Certainly use it. But please give it a few tests to make sure I’m not missing something. I am not much of a regex wizard. :)

derdennis commented 7 months ago

Hello everybody. I found bskyrb with the same intention of posting "Hey, new blogpost at $URL" posts, got everything up and running and stumbled into the same issue with the non-clickable links. I too did not find the correct place to somehow patch things up with the regex from @chipotle.

@ShreyanJain9: Can you already tell, when you will incorporate this into the official gem?

Thank you very much for your great work so far.

ShreyanJain9 commented 7 months ago

Unfortunately I haven't had time to maintain this gem for a while, though I do plan to come back to it at some point in the future. In the meantime you may want to look at @mackuba's minisky tool, which is lower-level and will take some more effort to use on your part, but Kuba is very helpful and can probably help with a lot of the higher-level pieces you'll have to implement.