Skip to content

Commit

Permalink
Made Code like legos
Browse files Browse the repository at this point in the history
  • Loading branch information
ManeraKai committed Jul 14, 2023
1 parent 7bfb906 commit efae11b
Show file tree
Hide file tree
Showing 6 changed files with 459 additions and 466 deletions.
245 changes: 61 additions & 184 deletions src/pages/lib/lemmy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
Loading

0 comments on commit efae11b

Please sign in to comment.