diff --git a/src/pages/lib/lemmy.js b/src/pages/lib/lemmy.js index ff3c983..c8eb54b 100644 --- a/src/pages/lib/lemmy.js +++ b/src/pages/lib/lemmy.js @@ -12,7 +12,7 @@ const regex = { function isLemmy(url) { return new Promise(resolve => { const req = new XMLHttpRequest(); - req.open("GET", `${url.protocol}//${url.hostname}/api/v3/community/list`, false); + req.open("GET", `${url.protocol}//${url.hostname}/api/v3/community/list`, true); req.onreadystatechange = () => { if (req.readyState == 4) { if (req.status == 200) resolve(true) @@ -24,225 +24,102 @@ function isLemmy(url) { }) } -function resolveObject(q, type, options) { - return new Promise(resolve => { - const req = new XMLHttpRequest(); - req.open("GET", `${utils.protocolHost(options.lemmy.instance)}/api/v3/resolve_object?q=${encodeURIComponent(q)}&auth=${encodeURIComponent(options.lemmy.auth)}`, false) - req.onload = () => { - switch (type) { - case 'post': { - const id = JSON.parse(req.responseText)['post']['post']['id'] - resolve(`${utils.protocolHost(options.lemmy.instance)}/post/${id}`) - return - } - case 'comment': { - const id = JSON.parse(req.responseText)['comment']['comment']['id'] - resolve(`${utils.protocolHost(options.lemmy.instance)}/comment/${id}`) - return - } - } - } - req.send(); - }) +function get_username(url) { + const userFederatedRegex = url.pathname.match(regex.userFederated) + if (userFederatedRegex) return `${userFederatedRegex[1]}@${userFederatedRegex[2]}` + + const userLocalRegex = url.pathname.match(regex.userLocal) + if (userLocalRegex) return `${userLocalRegex[1]}@${url.hostname}` } -function can_lemmy_to_lemmy(url, options) { - for (const regexItem of [regex.communityFederated, regex.communityLocal, regex.userFederated, regex.userLocal]) { - if (url.pathname.match(regexItem)) { - if (!options.lemmy || !options.lemmy.instance) return 'instance' - return true - } - } - for (const regexItem of [regex.post, regex.comment]) { - if (url.pathname.match(regexItem)) { - if (!options.lemmy || !options.lemmy.instance || !options.lemmy.jwt) return 'credentials' - return true - } - } - return false +function redirect_username(username, options) { + return `${utils.protocolHost(options.lemmy.instance)}/u/${username}` } -function lemmy_get_original_post(url, old_post_id) { - return new Promise(resolve => { - const req = new XMLHttpRequest(); - req.open("GET", `${url.protocol}//${url.hostname}/api/v3/post?id=${old_post_id}`, false); - req.onload = async () => { - const ap_id = JSON.parse(req.responseText)['post_view']['post']['ap_id'] - resolve(ap_id) - } - req.send() - }) +function get_community(url) { + const communityFederatedRegex = url.pathname.match(regex.communityFederated) + if (communityFederatedRegex) return `${communityFederatedRegex[1]}@${communityFederatedRegex[2]}` + + const CommunityLocalRegex = url.pathname.match(regex.communityLocal) + if (CommunityLocalRegex) return `${CommunityLocalRegex[1]}@${url.hostname}` +} + +function redirect_community(community, options) { + return `${utils.protocolHost(options.lemmy.instance)}/c/${community}` } -function lemmy_to_lemmy(url, options) { - return new Promise(async resolve => { +function get_post(url) { + return new Promise(resolve => { const postRegex = url.pathname.match(regex.post) if (postRegex) { const req = new XMLHttpRequest(); req.open("GET", `${url.protocol}//${url.hostname}/api/v3/post?id=${postRegex[1]}`, false); req.onload = async () => { - const ap_id = JSON.parse(req.responseText)['post_view']['post']['ap_id'] - resolve(await resolveObject(ap_id, 'post', options)) - return + resolve(JSON.parse(req.responseText)['post_view']['post']['ap_id']) } - req.send(); - return - } - - const commentRegex = url.pathname.match(regex.comment) - if (commentRegex) { - const req = new XMLHttpRequest(); - req.open("GET", `${url.protocol}//${url.hostname}/api/v3/comment?id=${commentRegex[1]}`, false); - req.onload = async () => { - const ap_id = JSON.parse(req.responseText)['comment_view']['comment']['ap_id'] - resolve(await resolveObject(ap_id, 'comment', options)) - return - } - req.send(); - return - } - - const CommunityFederatedRegex = url.pathname.match(regex.communityFederated) - if (CommunityFederatedRegex) { - resolve(`${utils.protocolHost(options.lemmy.instance)}/c/${CommunityFederatedRegex[1]}@${CommunityFederatedRegex[2]}`) - return - } - const CommunityLocalRegex = url.pathname.match(regex.communityLocal) - if (CommunityLocalRegex) { - resolve(`${utils.protocolHost(options.lemmy.instance)}/c/${CommunityLocalRegex[1]}@${url.hostname}`) - return - } - - const userFederatedRegex = url.pathname.match(regex.userFederated) - if (userFederatedRegex) { - resolve(`${utils.protocolHost(options.lemmy.instance)}/u/${userFederatedRegex[1]}@${userFederatedRegex[2]}`) - return - } - - const userLocalRegex = url.pathname.match(regex.userLocal) - if (userLocalRegex) { - resolve(`${utils.protocolHost(options.lemmy.instance)}/u/${userLocalRegex[1]}@${url.hostname}`) - return + req.send() + } else { + resolve() } }) } -function can_lemmy_to_mastodon(url, options) { - for (const regexItem of [regex.userFederated, regex.userLocal, regex.communityFederated, regex.communityLocal, regex.post]) { - if (url.pathname.match(regexItem)) { - if (!options.mastodon || !options.mastodon.instance) return 'instance' - return true - } - } - for (const regexItem of [regex.post]) { - if (url.pathname.match(regexItem)) { - if (!options.mastodon || !options.mastodon.instance || !options.mastodon.access_token) return 'credentials' - return true - } - } - return false -} - -function lemmy_to_mastodon(url, options) { - return new Promise(async resolve => { - const userFederatedRegex = url.pathname.match(regex.userFederated) - if (userFederatedRegex) { - resolve(`${utils.protocolHost(options.mastodon.instance)}/@${userFederatedRegex[1]}@${userFederatedRegex[2]}`) - return - } - const userLocalRegex = url.pathname.match(regex.userLocal) - if (userLocalRegex) { - resolve(`${utils.protocolHost(options.mastodon.instance)}/@${userLocalRegex[1]}@${url.hostname}`) - return - } - - const communityFederatedRegex = url.pathname.match(regex.communityFederated) - if (communityFederatedRegex) { - resolve(`${utils.protocolHost(options.mastodon.instance)}/@${communityFederatedRegex[1]}@${communityFederatedRegex[2]}`) - return - } - const communityLocalRegex = url.pathname.match(regex.communityLocal) - if (communityLocalRegex) { - resolve(`${utils.protocolHost(options.mastodon.instance)}/@${communityLocalRegex[1]}@${url.hostname}`) +function redirect_post(post, options) { + return new Promise(resolve => { + const req = new XMLHttpRequest(); + req.open("GET", `${utils.protocolHost(options.lemmy.instance)}/api/v3/resolve_object?q=${encodeURIComponent(post)}&auth=${encodeURIComponent(options.lemmy.auth)}`, false) + req.onload = () => { + const id = JSON.parse(req.responseText)['post']['post']['id'] + resolve(`${utils.protocolHost(options.lemmy.instance)}/post/${id}`) return } + req.send(); + }) +} - const postRegex = url.pathname.match(regex.post) - if (postRegex) { - const q = await lemmy_get_original_post(url, postRegex[1]) +function get_comment(url) { + return new Promise(resolve => { + const commentRegex = url.pathname.match(regex.comment) + if (commentRegex) { const req = new XMLHttpRequest(); - req.open("GET", `${options.mastodon.instance}/api/v2/search?q=${encodeURIComponent(q)}&resolve=true&limit=1`, false); - req.setRequestHeader('Authorization', `Bearer ${options.mastodon.access_token}`) + req.open("GET", `${url.protocol}//${url.hostname}/api/v3/comment?id=${commentRegex[1]}`, false); req.onload = async () => { - const data = JSON.parse(req.responseText)['statuses'][0] - const post_id = data['id'] - const username = data['account']['username'] - resolve(`${utils.protocolHost(options.mastodon.instance)}/@${username}@${url.hostname}/${post_id}`) + resolve(JSON.parse(req.responseText)['comment_view']['comment']['ap_id']) } - req.send(); - return + req.send() + } else { + resolve() } }) } -function can_lemmy_to_soapbox(url, options) { - for (const regexItem of [regex.userFederated, regex.userLocal, regex.post]) { - if (url.pathname.match(regexItem)) { - if (!options.mastodon || !options.mastodon.instance) return 'instance' - return true - } - } - for (const regexItem of [regex.post]) { - if (url.pathname.match(regexItem)) { - if (!options.mastodon || !options.mastodon.instance || !options.mastodon.access_token) return 'credentials' - return true - } - } - return false -} - -function lemmy_to_soapbox(url, options) { - return new Promise(async resolve => { - const userFederatedRegex = url.pathname.match(regex.userFederated) - if (userFederatedRegex) { - resolve(`${utils.protocolHost(options.soapbox.instance)}/@${userFederatedRegex[1]}@${userFederatedRegex[2]}`) - return - } - const userLocalRegex = url.pathname.match(regex.userLocal) - if (userLocalRegex) { - resolve(`${utils.protocolHost(options.soapbox.instance)}/@${userLocalRegex[1]}@${url.hostname}`) - return - } - - const postRegex = url.pathname.match(regex.post) - if (postRegex) { - const q = await lemmy_get_original_post(url, postRegex[1]) - const req = new XMLHttpRequest(); - req.open("GET", `${options.soapbox.instance}/api/v2/search?q=${encodeURIComponent(q)}&resolve=true&limit=1`, false); - req.setRequestHeader('Authorization', `Bearer ${options.soapbox.access_token}`) - req.onload = async () => { - const data = JSON.parse(req.responseText)['statuses'][0] - const post_id = data['id'] - const username = data['account']['username'] - resolve(`${utils.protocolHost(options.soapbox.instance)}/@${username}@${url.hostname}/posts/${post_id}`) - } - req.send(); +function redirect_comment(comment, options) { + return new Promise(resolve => { + const req = new XMLHttpRequest(); + req.open("GET", `${utils.protocolHost(options.lemmy.instance)}/api/v3/resolve_object?q=${encodeURIComponent(comment)}&auth=${encodeURIComponent(options.lemmy.auth)}`, false) + req.onload = () => { + const id = JSON.parse(req.responseText)['comment']['comment']['id'] + resolve(`${utils.protocolHost(options.lemmy.instance)}/comment/${id}`) return } + req.send(); }) } export default { isLemmy, - can_lemmy_to_lemmy, - lemmy_to_lemmy, + get_username, + redirect_username, + + get_community, + redirect_community, - can_lemmy_to_mastodon, - lemmy_to_mastodon, + get_post, + redirect_post, - can_lemmy_to_soapbox, - lemmy_to_soapbox, + get_comment, + redirect_comment, regex } \ No newline at end of file diff --git a/src/pages/lib/mastodon.js b/src/pages/lib/mastodon.js index 181ff21..2fef22e 100644 --- a/src/pages/lib/mastodon.js +++ b/src/pages/lib/mastodon.js @@ -28,106 +28,18 @@ function isMastodon(url) { }) } -function can_mastodon_to_lemmy(url, options) { - for (const regexItem of [regex.userFederated, regex.userLocal]) { - if (url.pathname.match(regexItem)) { - if (!options.lemmy || !options.lemmy.instance) return 'instance' - return true - } - } - return false -} - -function mastodon_to_lemmy(url, options) { - return new Promise(async resolve => { - console.log(url.pathname) - const userFederatedRegex = url.pathname.match(regex.userFederated) - if (userFederatedRegex) { - resolve(`${utils.protocolHost(options.lemmy.instance)}/u/${userFederatedRegex[1]}@${userFederatedRegex[2]}`) - return - } - const userLocalRegex = url.pathname.match(regex.userLocal) - if (userLocalRegex) { - resolve(`${utils.protocolHost(options.lemmy.instance)}/u/${userLocalRegex[1]}@${url.hostname}`) - return - } - }) -} - -function can_mastodon_to_mastodon(url, options) { - for (const regexItem of [regex.userFederated, regex.userLocal]) { - if (url.pathname.match(regexItem)) { - if (!options.mastodon || !options.mastodon.instance || !options.mastodon.access_token) return 'credentials' - return true - } - } - for (const regexItem of [regex.postLocal, regex.postFederated, regex.userFederated, regex.userLocal]) { - if (url.pathname.match(regexItem)) { - if (!options.mastodon || !options.mastodon.instance) return 'instance' - return true - } - } - return false -} - -function mastodon_to_mastodon(url, options) { - return new Promise(async resolve => { - const localPostRegex = url.pathname.match(regex.postLocal) - const federatedPostRegex = url.pathname.match(regex.postFederated) - if (localPostRegex || federatedPostRegex) { - let q - if (localPostRegex) { - q = url.href - } else if (federatedPostRegex) { - q = await get_original_url(url, federatedPostRegex[3]) - } - const req = new XMLHttpRequest(); - req.open("GET", `${options.mastodon.instance}/api/v2/search?q=${encodeURIComponent(q)}&resolve=true&limit=1`, false); - req.setRequestHeader('Authorization', `Bearer ${options.mastodon.access_token}`) - req.onload = async () => { - const post_id = JSON.parse(req.responseText)['statuses'][0]['id'] - if (localPostRegex) { - resolve(`${utils.protocolHost(options.mastodon.instance)}/@${localPostRegex[1]}@${url.hostname}/${post_id}`) - } - else if (federatedPostRegex) { - resolve(`${utils.protocolHost(options.mastodon.instance)}/@${federatedPostRegex[1]}@${federatedPostRegex[2]}/${post_id}`) - } - } - req.send(); - return - } +function get_username(url) { + const userFederatedRegex = url.pathname.match(regex.userFederated) + if (userFederatedRegex) return `${userFederatedRegex[1]}@${userFederatedRegex[2]}` - const userFederatedRegex = url.pathname.match(regex.userFederated) - if (userFederatedRegex) { - resolve(`${utils.protocolHost(options.mastodon.instance)}/@${userFederatedRegex[1]}@${userFederatedRegex[2]}`) - return - } - const userLocalRegex = url.pathname.match(regex.userLocal) - if (userLocalRegex) { - resolve(`${utils.protocolHost(options.mastodon.instance)}/@${userLocalRegex[1]}@${url.hostname}`) - return - } - }) + const userLocalRegex = url.pathname.match(regex.userLocal) + if (userLocalRegex) return `${userLocalRegex[1]}@${url.hostname}` } - -function can_mastodon_to_soapbox(url, options) { - for (const regexItem of [regex.userFederated, regex.userLocal]) { - if (url.pathname.match(regexItem)) { - if (!options.mastodon || !options.mastodon.instance || !options.mastodon.access_token) return 'credentials' - return true - } - } - for (const regexItem of [regex.postLocal, regex.postFederated, regex.userFederated, regex.userLocal]) { - if (url.pathname.match(regexItem)) { - if (!options.mastodon || !options.mastodon.instance) return 'instance' - return true - } - } - return false +function redirect_username(username, options) { + return `${utils.protocolHost(options.mastodon.instance)}/@${username}` } - function get_original_url(url, old_post_id) { return new Promise(resolve => { const req = new XMLHttpRequest() @@ -140,58 +52,45 @@ function get_original_url(url, old_post_id) { }) } -function mastodon_to_soapbox(url, options) { +function get_post_comment(url) { return new Promise(async resolve => { const localPostRegex = url.pathname.match(regex.postLocal) - const federatedPostRegex = url.pathname.match(regex.postFederated) - if (localPostRegex || federatedPostRegex) { - let q - if (localPostRegex) { - q = url.href - } else if (federatedPostRegex) { - q = await get_original_url(url, federatedPostRegex[3]) - } - const req = new XMLHttpRequest() - req.open("GET", `${options.soapbox.instance}/api/v2/search?q=${encodeURIComponent(q)}&resolve=true&limit=1`, true); - req.setRequestHeader('Authorization', `Bearer ${options.soapbox.access_token}`) - req.onload = () => { - const post_id = JSON.parse(req.responseText)['statuses'][0]['id'] - if (localPostRegex) { - resolve(`${utils.protocolHost(options.soapbox.instance)}/@${localPostRegex[1]}@${url.hostname}/posts/${post_id}`) - } - else if (federatedPostRegex) { - resolve(`${utils.protocolHost(options.soapbox.instance)}/@${federatedPostRegex[1]}@${federatedPostRegex[2]}/posts/${post_id}`) - } - } - req.send() - return - } - - const userFederatedRegex = url.pathname.match(regex.userFederated) - if (userFederatedRegex) { - resolve(`${utils.protocolHost(options.mastodon.instance)}/@${userFederatedRegex[1]}@${userFederatedRegex[2]}`) + if (localPostRegex) { + resolve(url.href) return } - const userLocalRegex = url.pathname.match(regex.userLocal) - if (userLocalRegex) { - resolve(`${utils.protocolHost(options.mastodon.instance)}/@${userLocalRegex[1]}@${url.hostname}`) + const federatedPostRegex = url.pathname.match(regex.postFederated) + if (federatedPostRegex) { + resolve(await get_original_url(url, federatedPostRegex[3])) return } + resolve() }) } +function redirect_post_comment(post_comment, options) { + return new Promise(resolve => { + const req = new XMLHttpRequest(); + req.open("GET", `${options.mastodon.instance}/api/v2/search?q=${encodeURIComponent(post_comment)}&resolve=true&limit=1`, false); + req.setRequestHeader('Authorization', `Bearer ${options.mastodon.access_token}`) + req.onload = async () => { + const data = JSON.parse(req.responseText)['statuses'][0] + const post_id = data['id'] + const username = data['account']['acct'] + resolve(`${utils.protocolHost(options.mastodon.instance)}/@${username}/${post_id}`) + } + req.send(); + }) +} export default { isMastodon, - can_mastodon_to_lemmy, - mastodon_to_lemmy, - - can_mastodon_to_mastodon, - mastodon_to_mastodon, + get_username, + redirect_username, - can_mastodon_to_soapbox, - mastodon_to_soapbox, + get_post_comment, + redirect_post_comment, regex } \ No newline at end of file diff --git a/src/pages/lib/redirector.js b/src/pages/lib/redirector.js new file mode 100644 index 0000000..8473d0a --- /dev/null +++ b/src/pages/lib/redirector.js @@ -0,0 +1,314 @@ +import lemmy from './lemmy.js' +import mastodon from './mastodon.js' +import soapbox from './soapbox.js' + +function can_soapbox_to_soapbox(url, options) { + for (const regexItem of [soapbox.regex.userFederated, soapbox.regex.userLocal]) { + if (url.pathname.match(regexItem)) { + if (!options.soapbox || !options.soapbox.instance) return 'instance' + return true + } + } + for (const regexItem of [soapbox.regex.postLocal, soapbox.regex.postFederated]) { + if (url.pathname.match(regexItem)) { + if (!options.soapbox || !options.soapbox.instance || !options.soapbox.access_token) return 'credentials' + return true + } + } + return false +} + +function soapbox_to_soapbox(url, options) { + return new Promise(async resolve => { + const post_comment = await soapbox.get_post_comment(url, options) + if (post_comment) { + resolve(await soapbox.redirect_post_comment(post_comment, options)) + return + } + + const username = soapbox.get_username(url) + if (username) { + resolve(soapbox.redirect_username(username, options)) + return + } + }) +} + +function can_soapbox_to_lemmy(url, options) { + for (const regexItem of [soapbox.regex.userFederated, soapbox.regex.userLocal]) { + if (url.pathname.match(regexItem)) { + if (!options.lemmy || !options.lemmy.instance) return 'instance' + return true + } + } + return false +} + +function soapbox_to_lemmy(url, options) { + return new Promise(async resolve => { + const username = soapbox.get_username(url) + if (username) { + resolve(lemmy.redirect_username(username, options)) + return + } + }) +} + +function can_soapbox_to_mastodon(url, options) { + for (const regexItem of [soapbox.regex.userFederated, soapbox.regex.userLocal]) { + if (url.pathname.match(regexItem)) { + if (!options.mastodon || !options.mastodon.instance) return 'instance' + return true + } + } + for (const regexItem of [soapbox.regex.postLocal, soapbox.regex.postFederated]) { + if (url.pathname.match(regexItem)) { + if (!options.mastodon || !options.mastodon.instance || !options.mastodon.access_token) return 'credentials' + return true + } + } + return false +} + +function soapbox_to_mastodon(url, options) { + return new Promise(async resolve => { + const post_comment = await soapbox.get_post_comment(url, options) + if (post_comment) { + resolve(await mastodon.redirect_post_comment(post_comment, options)) + return + } + + const username = soapbox.get_username(url) + if (username) { + resolve(mastodon.redirect_username(username, options)) + return + } + }) +} + +function can_mastodon_to_mastodon(url, options) { + for (const regexItem of [mastodon.regex.userFederated, mastodon.regex.userLocal]) { + if (url.pathname.match(regexItem)) { + if (!options.mastodon || !options.mastodon.instance) return 'instance' + return true + } + } + for (const regexItem of [mastodon.regex.postFederated, mastodon.regex.postLocal]) { + if (url.pathname.match(regexItem)) { + if (!options.mastodon || !options.mastodon.instance || !options.mastodon.access_token) return 'credentials' + return true + } + } + return false +} + +function mastodon_to_mastodon(url, options) { + return new Promise(async resolve => { + const post_comment = await mastodon.get_post_comment(url, options) + if (post_comment) { + resolve(await mastodon.redirect_post_comment(post_comment, options)) + return + } + + const username = mastodon.get_username(url) + if (username) { + resolve(mastodon.redirect_username(username, options)) + return + } + }) +} + +function can_mastodon_to_soapbox(url, options) { + for (const regexItem of [mastodon.regex.userFederated, mastodon.regex.userLocal]) { + if (url.pathname.match(regexItem)) { + if (!options.soapbox || !options.soapbox.instance) return 'instance' + return true + } + } + for (const regexItem of [mastodon.regex.postLocal, mastodon.regex.postFederated]) { + if (url.pathname.match(regexItem)) { + if (!options.soapbox || !options.soapbox.instance || !options.soapbox.access_token) return 'credentials' + return true + } + } + return false +} + +function mastodon_to_soapbox(url, options) { + return new Promise(async resolve => { + const post_comment = await mastodon.get_post_comment(url, options) + if (post_comment) { + resolve(await soapbox.redirect_post_comment(post_comment, options)) + return + } + + const username = mastodon.get_username(url) + if (username) { + resolve(soapbox.redirect_username(username, options)) + return + } + }) +} + +function can_mastodon_to_lemmy(url, options) { + for (const regexItem of [mastodon.regex.userFederated, mastodon.regex.userLocal]) { + if (url.pathname.match(regexItem)) { + if (!options.lemmy || !options.lemmy.instance) return 'instance' + return true + } + } + return false +} + +function mastodon_to_lemmy(url, options) { + return new Promise(async resolve => { + const username = mastodon.get_username(url) + if (username) { + resolve(lemmy.redirect_username(username, options)) + return + } + }) +} + +function can_lemmy_to_lemmy(url, options) { + for (const regexItem of [lemmy.regex.communityFederated, lemmy.regex.communityLocal, lemmy.regex.userFederated, lemmy.regex.userLocal]) { + if (url.pathname.match(regexItem)) { + if (!options.lemmy || !options.lemmy.instance) return 'instance' + return true + } + } + for (const regexItem of [lemmy.regex.post, lemmy.regex.comment]) { + if (url.pathname.match(regexItem)) { + if (!options.lemmy || !options.lemmy.instance || !options.lemmy.auth) return 'credentials' + return true + } + } + return false +} + +function lemmy_to_lemmy(url, options) { + return new Promise(async resolve => { + const post = await lemmy.get_post(url) + if (post) { + resolve(await lemmy.redirect_post(post, options)) + return + } + + const comment = await lemmy.get_comment(url) + if (comment) { + resolve(await lemmy.redirect_comment(comment, options)) + return + } + + const username = lemmy.get_username(url) + if (username) { + resolve(lemmy.redirect_username(username, options)) + return + } + + const community = lemmy.get_community(url) + if (community) { + resolve(lemmy.redirect_community(community, options)) + return + } + }) +} + +function can_lemmy_to_mastodon(url, options) { + for (const regexItem of [lemmy.regex.userFederated, lemmy.regex.userLocal, lemmy.regex.communityFederated, lemmy.regex.communityLocal]) { + if (url.pathname.match(regexItem)) { + if (!options.mastodon || !options.mastodon.instance) return 'instance' + return true + } + } + for (const regexItem of [lemmy.regex.post]) { + if (url.pathname.match(regexItem)) { + if (!options.mastodon || !options.mastodon.instance || !options.mastodon.access_token) return 'credentials' + return true + } + } + return false +} + +function lemmy_to_mastodon(url, options) { + return new Promise(async resolve => { + const username = lemmy.get_username(url) + if (username) { + resolve(mastodon.redirect_username(username, options)) + return + } + + const community = lemmy.get_community(url) + if (community) { + resolve(mastodon.redirect_username(community, options)) + return + } + + const post = await lemmy.get_post(url, options) + if (post) { + resolve(await mastodon.redirect_post_comment(post, options)) + return + } + }) +} + +function can_lemmy_to_soapbox(url, options) { + for (const regexItem of [lemmy.regex.userFederated, lemmy.regex.userLocal]) { + if (url.pathname.match(regexItem)) { + if (!options.soapbox || !options.soapbox.instance) return 'instance' + return true + } + } + for (const regexItem of [lemmy.regex.post]) { + if (url.pathname.match(regexItem)) { + if (!options.soapbox || !options.soapbox.instance || !options.soapbox.access_token) return 'credentials' + return true + } + } + return false +} + +function lemmy_to_soapbox(url, options) { + return new Promise(async resolve => { + const username = lemmy.get_username(url) + if (username) { + resolve(soapbox.redirect_username(username, options)) + return + } + + const post = await lemmy.get_post(url, options) + if (post) { + resolve(await soapbox.redirect_post_comment(post.options)) + return + } + }) +} + +export default { + can_soapbox_to_soapbox, + soapbox_to_soapbox, + + can_soapbox_to_mastodon, + soapbox_to_mastodon, + + can_soapbox_to_lemmy, + soapbox_to_lemmy, + + can_mastodon_to_mastodon, + mastodon_to_mastodon, + + can_mastodon_to_soapbox, + mastodon_to_soapbox, + + can_mastodon_to_lemmy, + mastodon_to_lemmy, + + can_lemmy_to_lemmy, + lemmy_to_lemmy, + + can_lemmy_to_mastodon, + lemmy_to_mastodon, + + can_lemmy_to_soapbox, + lemmy_to_soapbox, +} \ No newline at end of file diff --git a/src/pages/lib/soapbox.js b/src/pages/lib/soapbox.js index 1e64e90..0caf7b4 100644 --- a/src/pages/lib/soapbox.js +++ b/src/pages/lib/soapbox.js @@ -32,51 +32,22 @@ function isSoapbox(url) { }) } -function can_soapbox_to_lemmy(url, options) { - for (const regexItem of [regex.userFederated, regex.userLocal]) { - if (url.pathname.match(regexItem)) { - if (!options.lemmy || !options.lemmy.instance) return 'instance' - return true - } - } - return false -} +function get_username(url) { + const userFederatedRegex = url.pathname.match(regex.userFederated) + if (userFederatedRegex) return `${userFederatedRegex[1]}@${userFederatedRegex[2]}` -function soapbox_to_lemmy(url, options) { - return new Promise(async resolve => { - const userFederatedRegex = url.pathname.match(regex.userFederated) - if (userFederatedRegex) { - resolve(`${utils.protocolHost(options.lemmy.instance)}/u/${userFederatedRegex[1]}@${userFederatedRegex[2]}`) - return - } - const userLocalRegex = url.pathname.match(regex.userLocal) - if (userLocalRegex) { - resolve(`${utils.protocolHost(options.lemmy.instance)}/u/${userLocalRegex[1]}@${url.hostname}`) - return - } - }) + const userLocalRegex = url.pathname.match(regex.userLocal) + if (userLocalRegex) return `${userLocalRegex[1]}@${url.hostname}` } -function can_soapbox_to_soapbox(url, options) { - for (const regexItem of [regex.userFederated, regex.userLocal]) { - if (url.pathname.match(regexItem)) { - if (!options.soapbox || !options.soapbox.instance) return 'instance' - return true - } - } - for (const regexItem of [regex.postLocal, regex.postFederated]) { - if (url.pathname.match(regexItem)) { - if (!options.soapbox || !options.soapbox.instance || !options.soapbox.access_token) return 'credentials' - return true - } - } - return false +function redirect_username(username, options) { + return `${utils.protocolHost(options.soapbox.instance)}/@${username}` } function get_original_url(url, old_post_id) { return new Promise(resolve => { const req = new XMLHttpRequest() - req.open("GET", `${url.protocol}//${url.hostname}/api/v1/statuses/${old_post_id}`, false) + req.open("GET", `${url.protocol}//${url.hostname}/api/v1/statuses/${old_post_id}`, true) req.onload = () => { const data = JSON.parse(req.responseText) resolve(data.url) @@ -85,114 +56,46 @@ function get_original_url(url, old_post_id) { }) } -function soapbox_to_soapbox(url, options) { +function get_post_comment(url) { return new Promise(async resolve => { const localPostRegex = url.pathname.match(regex.postLocal) - const federatedPostRegex = url.pathname.match(regex.postFederated) - if (localPostRegex || federatedPostRegex) { - let q - if (localPostRegex) { - q = url.href - } else if (federatedPostRegex) { - q = await get_original_url(url, federatedPostRegex[3]) - } - const req = new XMLHttpRequest() - req.open("GET", `${options.soapbox.instance}/api/v2/search?q=${encodeURIComponent(q)}&resolve=true&limit=1`, true); - req.setRequestHeader('Authorization', `Bearer ${options.soapbox.access_token}`) - req.onload = () => { - const post_id = JSON.parse(req.responseText)['statuses'][0]['id'] - if (localPostRegex) { - resolve(`${utils.protocolHost(options.soapbox.instance)}/@${localPostRegex[1]}@${url.hostname}/posts/${post_id}`) - } - else if (federatedPostRegex) { - resolve(`${utils.protocolHost(options.soapbox.instance)}/@${federatedPostRegex[1]}@${federatedPostRegex[2]}/posts/${post_id}`) - } - } - req.send() - return - } - - const userFederatedRegex = url.pathname.match(regex.userFederated) - if (userFederatedRegex) { - resolve(`${utils.protocolHost(options.soapbox.instance)}/@${userFederatedRegex[1]}@${userFederatedRegex[2]}`) + if (localPostRegex) { + resolve(url.href) return } - const userLocalRegex = url.pathname.match(regex.userLocal) - if (userLocalRegex) { - resolve(`${utils.protocolHost(options.soapbox.instance)}/@${userLocalRegex[1]}@${url.hostname}`) + const federatedPostRegex = url.pathname.match(regex.postFederated) + if (federatedPostRegex) { + resolve(await get_original_url(url, federatedPostRegex[3])) return } + resolve() }) } -function can_soapbox_to_mastodon(url, options) { - for (const regexItem of [regex.userFederated, regex.userLocal]) { - if (url.pathname.match(regexItem)) { - if (!options.soapbox || !options.soapbox.instance) return 'instance' - return true - } - } - for (const regexItem of [regex.postLocal, regex.postFederated]) { - if (url.pathname.match(regexItem)) { - if (!options.soapbox || !options.soapbox.instance || !options.soapbox.access_token) return 'credentials' - return true - } - } - return false -} - -function soapbox_to_mastodon(url, options) { - return new Promise(async resolve => { - const localPostRegex = url.pathname.match(regex.postLocal) - const federatedPostRegex = url.pathname.match(regex.postFederated) - if (localPostRegex || federatedPostRegex) { - let q - if (localPostRegex) { - q = url.href - } else if (federatedPostRegex) { - q = await get_original_url(url, federatedPostRegex[3]) - } - const req = new XMLHttpRequest(); - req.open("GET", `${options.mastodon.instance}/api/v2/search?q=${encodeURIComponent(q)}&resolve=true&limit=1`, false); - req.setRequestHeader('Authorization', `Bearer ${options.mastodon.access_token}`) - req.onload = async () => { - const data = JSON.parse(req.responseText)['statuses'][0] - const post_id = data['id'] - const username = data['account']['username'] - if (localPostRegex) { - resolve(`${utils.protocolHost(options.mastodon.instance)}/@${username}@${url.hostname}/${post_id}`) - } else if (federatedPostRegex) { - resolve(`${utils.protocolHost(options.mastodon.instance)}/@${username}@${federatedPostRegex[2]}/${post_id}`) - } - } - req.send() - return - } - - const userFederatedRegex = url.pathname.match(regex.userFederated) - if (userFederatedRegex) { - resolve(`${utils.protocolHost(options.mastodon.instance)}/@${userFederatedRegex[1]}@${userFederatedRegex[2]}`) - return - } - const userLocalRegex = url.pathname.match(regex.userLocal) - if (userLocalRegex) { - resolve(`${utils.protocolHost(options.mastodon.instance)}/@${userLocalRegex[1]}@${url.hostname}`) - return +function redirect_post_comment(post_comment, options) { + return new Promise(resolve => { + const req = new XMLHttpRequest() + req.open("GET", `${options.soapbox.instance}/api/v2/search?q=${encodeURIComponent(post_comment)}&resolve=true&limit=1`, true); + req.setRequestHeader('Authorization', `Bearer ${options.soapbox.access_token}`) + req.onload = () => { + const data = JSON.parse(req.responseText)['statuses'][0] + const post_id = data['id'] + const username = data['account']['acct'] + resolve(`${utils.protocolHost(options.soapbox.instance)}/@${username}/posts/${post_id}`) } + req.send() }) } + export default { isSoapbox, - can_soapbox_to_lemmy, - soapbox_to_lemmy, - - can_soapbox_to_soapbox, - soapbox_to_soapbox, + get_username, + redirect_username, - can_soapbox_to_mastodon, - soapbox_to_mastodon, + get_post_comment, + redirect_post_comment, regex } \ No newline at end of file diff --git a/src/pages/lib/utils.js b/src/pages/lib/utils.js index 3b92835..8bdb3a9 100644 --- a/src/pages/lib/utils.js +++ b/src/pages/lib/utils.js @@ -23,8 +23,8 @@ function getOptions() { } function isMyInstance(url, software) { - const instance = new URL(options[software].instance) - return url.hostname == instance.hostname + const instance = new URL(options[software].instance) + return url.hostname == instance.hostname } function getBlacklist(options) { diff --git a/src/pages/popup/popup.js b/src/pages/popup/popup.js index b02122d..373b239 100644 --- a/src/pages/popup/popup.js +++ b/src/pages/popup/popup.js @@ -1,4 +1,5 @@ import utils from '../../pages/lib/utils.js' +import redirector from '../../pages/lib/redirector.js' import lemmy from '../../pages/lib/lemmy.js' import mastodon from '../lib/mastodon.js' import soapbox from '../lib/soapbox.js' @@ -9,11 +10,11 @@ let options browser.tabs.query({ active: true, currentWindow: true }, async (tabs) => { let url = new URL(tabs[0].url); if (await lemmy.isLemmy(url)) { - switch (lemmy.can_lemmy_to_lemmy(url, options)) { + switch (redirector.can_lemmy_to_lemmy(url, options)) { case true: document.getElementById("redirect_to_lemmy").disabled = false document.getElementById("redirect_to_lemmy").addEventListener("click", async () => { - const newUrl = await lemmy.lemmy_to_lemmy(url, options) + const newUrl = await redirector.lemmy_to_lemmy(url, options) if (newUrl) { browser.tabs.update({ url: newUrl }) close() @@ -29,12 +30,12 @@ let options default: document.getElementById("redirect_to_lemmy").title = 'URL not supported' } - switch (lemmy.can_lemmy_to_mastodon(url, options)) { + switch (redirector.can_lemmy_to_mastodon(url, options)) { case true: document.getElementById("redirect_to_mastodon").disabled = false document.getElementById("redirect_to_mastodon").title = '' document.getElementById("redirect_to_mastodon").addEventListener("click", async () => { - const newUrl = await lemmy.lemmy_to_mastodon(url, options) + const newUrl = await redirector.lemmy_to_mastodon(url, options) if (newUrl) { browser.tabs.update({ url: newUrl }) close() @@ -50,12 +51,12 @@ let options default: document.getElementById("redirect_to_mastodon").title = 'Url not supported' } - switch (lemmy.can_lemmy_to_soapbox(url, options)) { + switch (redirector.can_lemmy_to_soapbox(url, options)) { case true: document.getElementById("redirect_to_soapbox").disabled = false document.getElementById("redirect_to_soapbox").title = '' document.getElementById("redirect_to_soapbox").addEventListener("click", async () => { - const newUrl = await lemmy.lemmy_to_soapbox(url, options) + const newUrl = await redirector.lemmy_to_soapbox(url, options) if (newUrl) browser.tabs.update({ url: newUrl }) }) break @@ -70,12 +71,12 @@ let options } } if (await mastodon.isMastodon(url)) { - switch (mastodon.can_mastodon_to_lemmy(url, options)) { + switch (redirector.can_mastodon_to_lemmy(url, options)) { case true: document.getElementById("redirect_to_lemmy").disabled = false document.getElementById("redirect_to_mastodon").title = '' document.getElementById("redirect_to_lemmy").addEventListener("click", async () => { - const newUrl = await mastodon.mastodon_to_lemmy(url, options) + const newUrl = await redirector.mastodon_to_lemmy(url, options) if (newUrl) browser.tabs.update({ url: newUrl }) }) break @@ -88,12 +89,12 @@ let options default: document.getElementById("redirect_to_mastodon").title = 'Url not supported' } - switch (mastodon.can_mastodon_to_mastodon(url, options)) { + switch (redirector.can_mastodon_to_mastodon(url, options)) { case true: document.getElementById("redirect_to_mastodon").disabled = false document.getElementById("redirect_to_mastodon").title = '' document.getElementById("redirect_to_mastodon").addEventListener("click", async () => { - const newUrl = await mastodon.mastodon_to_mastodon(url, options) + const newUrl = await redirector.mastodon_to_mastodon(url, options) if (newUrl) browser.tabs.update({ url: newUrl }) }) break @@ -106,12 +107,12 @@ let options default: document.getElementById("redirect_to_mastodon").title = 'Url not supported' } - switch (mastodon.can_mastodon_to_soapbox(url, options)) { + switch (redirector.can_mastodon_to_soapbox(url, options)) { case true: document.getElementById("redirect_to_soapbox").disabled = false document.getElementById("redirect_to_soapbox").title = '' document.getElementById("redirect_to_soapbox").addEventListener("click", async () => { - const newUrl = await mastodon.mastodon_to_soapbox(url, options) + const newUrl = await redirector.mastodon_to_soapbox(url, options) if (newUrl) browser.tabs.update({ url: newUrl }) }) break @@ -125,14 +126,13 @@ let options document.getElementById("redirect_to_soapbox").title = 'Url not supported' } } - if (await soapbox.isSoapbox(url)) { - switch (soapbox.can_soapbox_to_lemmy(url, options)) { + switch (redirector.can_soapbox_to_lemmy(url, options)) { case true: document.getElementById("redirect_to_lemmy").disabled = false document.getElementById("redirect_to_mastodon").title = '' document.getElementById("redirect_to_lemmy").addEventListener("click", async () => { - const newUrl = await soapbox.soapbox_to_lemmy(url, options) + const newUrl = await redirector.soapbox_to_lemmy(url, options) if (newUrl) browser.tabs.update({ url: newUrl }) }) break @@ -145,12 +145,12 @@ let options default: document.getElementById("redirect_to_soapbox").title = 'Url not supported' } - switch (soapbox.can_soapbox_to_soapbox(url, options)) { + switch (redirector.can_soapbox_to_soapbox(url, options)) { case true: document.getElementById("redirect_to_soapbox").disabled = false document.getElementById("redirect_to_mastodon").title = '' document.getElementById("redirect_to_soapbox").addEventListener("click", async () => { - const newUrl = await soapbox.soapbox_to_soapbox(url, options) + const newUrl = await redirector.soapbox_to_soapbox(url, options) if (newUrl) browser.tabs.update({ url: newUrl }) }) break @@ -164,12 +164,12 @@ let options document.getElementById("redirect_to_soapbox").title = 'Url not supported' } - switch (soapbox.can_soapbox_to_mastodon(url, options)) { + switch (redirector.can_soapbox_to_mastodon(url, options)) { case true: document.getElementById("redirect_to_mastodon").disabled = false document.getElementById("redirect_to_mastodon").title = '' document.getElementById("redirect_to_mastodon").addEventListener("click", async () => { - const newUrl = await soapbox.soapbox_to_mastodon(url, options) + const newUrl = await redirector.soapbox_to_mastodon(url, options) if (newUrl) browser.tabs.update({ url: newUrl }) }) break