Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Guppe support [WIP] #65

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/background/modules/AutoRemoteFollow.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as MastodonDetect from "./Detect/Mastodon.js";
import * as GnuSocialDetect from "./Detect/GnuSocial.js";
import * as PleromaDetect from "./Detect/Pleroma.js";
import * as FriendicaDetect from "./Detect/Friendica.js";
import * as GuppeDetect from "./Detect/Guppe.js";

import * as NetworkTools from "/common/modules/NetworkTools.js";
import * as MastodonRedirect from "./MastodonRedirect.js";
Expand All @@ -22,12 +23,15 @@ import * as Notifications from "/common/modules/Notifications.js";

const FEDIVERSE_TYPE = Object.freeze({
MASTODON: Symbol("Mastodon"),
GUPPE: Symbol("Gup.pe"),
FRIENDICA: Symbol("Friendica"),
GNU_SOCIAL: Symbol("GNU Social"),
PLEROMA: Symbol("Pleroma"),
FRIENDICA: Symbol("Friendica")
});
const FEDIVERSE_MODULE = Object.freeze({
[FEDIVERSE_TYPE.MASTODON]: MastodonDetect,
[FEDIVERSE_TYPE.GUPPE]: GuppeDetect,
[FEDIVERSE_TYPE.FRIENDICA]: FriendicaDetect,
[FEDIVERSE_TYPE.GNU_SOCIAL]: GnuSocialDetect,
[FEDIVERSE_TYPE.PLEROMA]: PleromaDetect,
[FEDIVERSE_TYPE.FRIENDICA]: FriendicaDetect
Expand All @@ -48,10 +52,8 @@ async function handleWebRequest(requestDetails) {
return Promise.reject(new Error("URL info not available"));
}

const url = new URL(requestDetails.url);

// detect, which network/software it uses
const [software, interaction] = getInteractionType(url);
const [software, interaction] = getInteractionType(requestDetails.url);

if (software === null) {
// ignore unrelated sites, resolves so error handling is not triggered
Expand All @@ -66,6 +68,7 @@ async function handleWebRequest(requestDetails) {
MastodonRedirect.enableLoadReplace(detectModule.shouldLoadReplace.bind(null, requestDetails));

// and get data and pass to redirect
const url = new URL(requestDetails.url);
switch (interaction) {
case INTERACTION_TYPE.FOLLOW: {
const remoteUser = await detectModule.getUsername(url, requestDetails);
Expand Down Expand Up @@ -129,13 +132,13 @@ async function handleError(error) {
*
* @function
* @private
* @param {URL} url
* @param {string} fullUrl the full URL as a string
* @returns {[FEDIVERSE_TYPE, Symbol]|[null, null]}
*/
function getInteractionType(url) {
function getInteractionType(fullUrl) {
for (const fedType of Object.values(FEDIVERSE_TYPE)) {
for (const [checkRegEx, interactionType] of FEDIVERSE_MODULE[fedType].CATCH_URLS) {
if (url.pathname.match(checkRegEx)) {
if (fullUrl.match(checkRegEx)) {
return [fedType, interactionType];
}
}
Expand Down
78 changes: 78 additions & 0 deletions src/background/modules/Detect/Guppe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* Module, that detects Gup.pe (https://a.gup.pe) and returns the required values.
*
* @module Detect/AGuppe
*/

import {NotSupportedError} from "/common/modules/Errors.js";
import {INTERACTION_TYPE} from "../data/INTERACTION_TYPE.js";

// https://regex101.com/r/lKOGPf/1
const REMOTE_FOLLOW_REGEX = /^https:\/\/a\.gup\.pe\/u\/(\w+)$/;

/** The URLs to intercept and pass to this module. */
export const CATCH_URLS = new Map();
CATCH_URLS.set(REMOTE_FOLLOW_REGEX, INTERACTION_TYPE.FOLLOW);

/**
* Whether to enable replacing the previous site when redirecting or not.
*
* @private
* @type {boolean}
*/
const ENABLE_LOAD_REPLACE = false;

/**
* Determinates whether the redirect should replace the site before or not.
*
* @public
* @returns {boolean}
*/
export function shouldLoadReplace() {
return ENABLE_LOAD_REPLACE;
}

/**
* Determinates which tab should be redirected.
*
* @public
* @param {Object} requestDetails
* @returns {int}
*/
export function getTabToModify(requestDetails) {
return requestDetails.tabId;
}

/**
* Find the follow URL.
*
* @public
* @returns {Promise}
*/
export function getTootUrl() {
throw new NotSupportedError("getTootUrl() is not supported");
}

/**
* Returns the username of the follow page.
*
* @private
* @function
* @param {URL} url
* @returns {string|undefined}
*/
export function getUsername(url) {
const match = REMOTE_FOLLOW_REGEX.exec(url.toString());
return match[1];
}

/**
* Returns the server from the required URL.
*
* @function
* @param {URL} url
* @returns {string|undefined}
*/
export function getServer(url) {
return url.host;
}