1
0
Fork 0
mirror of https://github.com/benbusby/farside.git synced 2025-04-20 19:08:42 +00:00

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. # Define relation between available services and their parent service.
# This enables Farside to redirect with links such as: # This enables Farside to redirect with links such as:
# farside.link/https://www.youtube.com/watch?v=dQw4w9WgXcQ # 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 %{ @parent_services %{
~r/youtu(.be|be.com)/ => ["invidious", "piped"], @youtube_regex => ["invidious", "piped"],
~r/reddit.com/ => ["libreddit", "teddit"], @reddit_regex => ["libreddit", "teddit"],
~r/instagram.com/ => ["bibliogram"], @instagram_regex => ["bibliogram"],
~r/twitter.com/ => ["nitter"], @twitter_regex => ["nitter"],
~r/wikipedia.org/ => ["wikiless"], @wikipedia_regex => ["wikiless"],
~r/medium.com/ => ["scribe"], @medium_regex => ["scribe"],
~r/odysee.com/ => ["librarian"], @odysee_regex => ["librarian"],
~r/imgur.com/ => ["rimgo"], @imgur_regex => ["rimgo"],
~r/translate.google.com/ => ["lingva"] @gtranslate_regex => ["lingva"]
} }
def get_services_map do def get_services_map do
@ -127,10 +137,27 @@ defmodule Farside do
result result
end end
instance instance
end 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 def get_last_updated do
{:ok, last_updated} = {:ok, last_updated} =
Redix.command( Redix.command(

View file

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