Prepend /u/ for Bibliogram redirects if not present

Bibliogram links require a "/u/" prefix when viewing a user's profile,
which does not match Instagram's URL paradigm. This adds a bit of logic
for Bibliogram links to prepend this prefix if the user is not visiting
a post (indicated by a "/p/" prefix, which Bibliogram and Instagram BOTH
use) or the home page of an instance (indicated by an empty path value).
This commit is contained in:
Ben Busby 2022-06-21 15:53:16 -06:00
parent 52833ef89e
commit d280f961f5
No known key found for this signature in database
GPG key ID: B9B7231E01D924A1
2 changed files with 39 additions and 10 deletions

View file

@ -6,16 +6,26 @@ defmodule Farside do
# Define relation between available services and their parent service.
# This enables Farside to redirect with links such as:
# farside.link/https://www.youtube.com/watch?v=dQw4w9WgXcQ
@youtube_regex ~r/youtu(.be|be.com)|invidious|piped/
@reddit_regex ~r/reddit.com|libreddit|teddit/
@instagram_regex ~r/instagram.com|bibliogram/
@twitter_regex ~r/twitter.com|nitter/
@wikipedia_regex ~r/wikipedia.org|wikiless/
@medium_regex ~r/medium.com|scribe/
@odysee_regex ~r/odysee.com|librarian/
@imgur_regex ~r/imgur.com|rimgo/
@gtranslate_regex ~r/translate.google.com|lingva/
@parent_services %{
~r/youtu(.be|be.com)/ => ["invidious", "piped"],
~r/reddit.com/ => ["libreddit", "teddit"],
~r/instagram.com/ => ["bibliogram"],
~r/twitter.com/ => ["nitter"],
~r/wikipedia.org/ => ["wikiless"],
~r/medium.com/ => ["scribe"],
~r/odysee.com/ => ["librarian"],
~r/imgur.com/ => ["rimgo"],
~r/translate.google.com/ => ["lingva"]
@youtube_regex => ["invidious", "piped"],
@reddit_regex => ["libreddit", "teddit"],
@instagram_regex => ["bibliogram"],
@twitter_regex => ["nitter"],
@wikipedia_regex => ["wikiless"],
@medium_regex => ["scribe"],
@odysee_regex => ["librarian"],
@imgur_regex => ["rimgo"],
@gtranslate_regex => ["lingva"]
}
def get_services_map do
@ -127,10 +137,27 @@ defmodule Farside do
result
end
instance
end
def amend_instance(instance, service, path) do
cond do
String.match?(service, @instagram_regex) ->
# Bibliogram doesn't have a 1:1 matching to Instagram URLs for users,
# so a "/u" is appended if the requested path doesn't explicitly include
# "/p" for a post or an empty path for the home page.
if String.length(path) > 0 and
!String.starts_with?(path, "p/") and
!String.starts_with?(path, "u/") do
"#{instance}/u"
else
instance
end
true ->
instance
end
end
def get_last_updated do
{:ok, last_updated} =
Redix.command(

View file

@ -66,9 +66,11 @@ defmodule Farside.Router do
conn.assigns[:throttle] != nil ->
Farside.get_service(service_name)
|> Farside.last_instance
|> Farside.amend_instance(service_name, path)
true ->
Farside.get_service(service_name)
|> Farside.pick_instance
|> Farside.amend_instance(service_name, path)
end
# Redirect to the available instance