Skip to content

Commit

Permalink
Added support for lemmy shortcut links: /c/community@instance (#406)
Browse files Browse the repository at this point in the history
  • Loading branch information
ktgd committed Jul 5, 2023
1 parent 7d6af23 commit 95a0484
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion helpers/LinkHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Alert } from "react-native";
import axios from "axios";
import { URL } from "react-native-url-polyfill";
import { writeToLog } from "./LogHelper";
import store from "../store";

const imageExtensions = [
"webp",
Expand All @@ -21,6 +22,7 @@ const imageExtensions = [
];

const videoExtensions = ["mp4", "mov", "m4a"];
const {accounts} = store.getState();

export interface LinkInfo {
extType?: ExtensionType;
Expand Down Expand Up @@ -65,7 +67,27 @@ export const isPotentialFedSite = (link: string) => {
};

export const isLemmySite = async (link: string) => {
const urlComponents = new URL(link);
let instanceUrl = accounts.currentAccount.instance;
if (!instanceUrl.startsWith("https://") && !instanceUrl.startsWith("http://")) {
instanceUrl = "https://" + instanceUrl;
}

// Handle shortcut links that are formatted: "/c/community@instance". Need to prepend the home instance url
if (link[0] === '/') {
if(instanceUrl === "") {
writeToLog(`Trying to open link: ${link} with instanceUrl: ${instanceUrl}`);
return false;
}
link = instanceUrl + link;
}

let urlComponents;
try {
urlComponents = new URL(link);
} catch (e){
writeToLog("Failed to make components from link: " + link + "Err: " + e.toString());
return false;
}

// Try lemmy api to verify this is a valid lemmy instance
const apiUrl = `${urlComponents.protocol}//${urlComponents.hostname}/api/v3/site`;
Expand Down

0 comments on commit 95a0484

Please sign in to comment.