2020-05-01 22:46:15 +08:00
// .replace(":id", "")
// GET
const API _FOLLOWERS = "/api/v1/accounts/:id/followers"
// GET
const API _FOLLOWING = "/api/v1/accounts/:id/following"
// GET
2020-05-05 23:50:48 +08:00
const API _GET _ACCOUNT = '/api/v1/accounts/:id'
const NUMBER _PER _REQUEST = 20
2020-05-01 22:46:15 +08:00
// [
// {
// "id": "1020382",
// "username": "atul13061987",
// "acct": "atul13061987",
// "display_name": "",
// "locked": false,
// "bot": false,
// "created_at": "2019-12-04T07:17:02.745Z",
// "note": "<p></p>",
// "url": "https://mastodon.social/@atul13061987",
// "avatar": "https://mastodon.social/avatars/original/missing.png",
// "avatar_static": "https://mastodon.social/avatars/original/missing.png",
// "header": "https://mastodon.social/headers/original/missing.png",
// "header_static": "https://mastodon.social/headers/original/missing.png",
// "followers_count": 0,
// "following_count": 2,
// "statuses_count": 0,
// "last_status_at": null,
// "emojis": [],
// "fields": []
// },
// {
// "id": "1020381",
// "username": "linuxliner",
// "acct": "linuxliner",
// "display_name": "",
// "locked": false,
// "bot": false,
// "created_at": "2019-12-04T07:15:56.426Z",
// "note": "<p></p>",
// "url": "https://mastodon.social/@linuxliner",
// "avatar": "https://mastodon.social/avatars/original/missing.png",
// "avatar_static": "https://mastodon.social/avatars/original/missing.png",
// "header": "https://mastodon.social/headers/original/missing.png",
// "header_static": "https://mastodon.social/headers/original/missing.png",
// "followers_count": 0,
// "following_count": 2,
// "statuses_count": 0,
// "last_status_at": null,
// "emojis": [],
// "fields": []
// }
// ]
2022-11-09 13:56:50 -05:00
async function getFollowers ( id , mastodonURI , token , callback ) {
const url = mastodonURI + API _FOLLOWERS . replace ( ":id" , id ) ;
var response ;
try {
response = await fetch ( url + '?limit=' + NUMBER _PER _REQUEST , { headers : { 'Authorization' : 'Bearer ' + token } } ) ;
} catch ( e ) {
console . error ( 'loading followers failed.' ) ;
return ;
}
const json = await response . json ( ) ;
let nextUrl = null ;
let links = response . headers . get ( 'link' ) ;
if ( links ) {
links . split ( ',' ) . forEach ( link => {
if ( link . includes ( 'next' ) ) {
let regex = /<(.*?)>/ ;
nextUrl = link . match ( regex ) [ 1 ] ;
}
} ) ;
}
callback ( json , nextUrl ) ;
2020-05-01 22:46:15 +08:00
}
2022-11-09 13:56:50 -05:00
async function getFollowing ( id , mastodonURI , token , callback ) {
const url = mastodonURI + API _FOLLOWING . replace ( ":id" , id ) ;
var response ;
try {
response = await fetch ( url + '?limit=' + NUMBER _PER _REQUEST , { headers : { 'Authorization' : 'Bearer ' + token } } ) ;
} catch ( e ) {
console . error ( 'loading following failed.' ) ;
return ;
}
const json = await response . json ( ) ;
let nextUrl = null ;
let links = response . headers . get ( 'link' ) ;
if ( links ) {
links . split ( ',' ) . forEach ( link => {
if ( link . includes ( 'next' ) ) {
let regex = /<(.*?)>/ ;
nextUrl = link . match ( regex ) [ 1 ] ;
}
} ) ;
}
callback ( json , nextUrl ) ;
2020-05-01 22:46:15 +08:00
}
// {
// "id": "1",
// "username": "Gargron",
// "acct": "Gargron",
// "display_name": "Eugen",
// "locked": false,
// "bot": false,
// "created_at": "2016-03-16T14:34:26.392Z",
// "note": "<p>Developer of Mastodon and administrator of mastodon.social. I post service announcements, development updates, and personal stuff.</p>",
// "url": "https://mastodon.social/@Gargron",
// "avatar": "https://files.mastodon.social/accounts/avatars/000/000/001/original/d96d39a0abb45b92.jpg",
// "avatar_static": "https://files.mastodon.social/accounts/avatars/000/000/001/original/d96d39a0abb45b92.jpg",
// "header": "https://files.mastodon.social/accounts/headers/000/000/001/original/c91b871f294ea63e.png",
// "header_static": "https://files.mastodon.social/accounts/headers/000/000/001/original/c91b871f294ea63e.png",
// "followers_count": 318699,
// "following_count": 453,
// "statuses_count": 61013,
// "last_status_at": "2019-11-30T20:02:08.277Z",
// "emojis": [],
// "fields": [
// {
// "name": "Patreon",
// "value": "<a href=\"https://www.patreon.com/mastodon\" rel=\"me nofollow noopener noreferrer\" target=\"_blank\"><span class=\"invisible\">https://www.</span><span class=\"\">patreon.com/mastodon</span><span class=\"invisible\"></span></a>",
// "verified_at": null
// },
// {
// "name": "Homepage",
// "value": "<a href=\"https://zeonfederated.com\" rel=\"me nofollow noopener noreferrer\" target=\"_blank\"><span class=\"invisible\">https://</span><span class=\"\">zeonfederated.com</span><span class=\"invisible\"></span></a>",
// "verified_at": "2019-07-15T18:29:57.191+00:00"
// }
// ]
// }
function getUserInfo ( id , mastodonURI , token , callback ) {
2020-05-05 23:50:48 +08:00
let url = mastodonURI + API _GET _ACCOUNT . replace ( ":id" , id ) ;
2020-05-01 22:46:15 +08:00
$ . ajax ( {
url : url ,
method : 'GET' ,
headers : {
'Authorization' : 'Bearer ' + token ,
} ,
success : function ( data ) {
callback ( data ) ;
} ,
} ) ;
}
function getEmojiDict ( emoji _list ) {
let dict = new Object ;
emoji _list . forEach ( pair => {
dict [ ":" + pair . shortcode + ":" ] = pair . url ;
} ) ;
return dict ;
}
2020-07-03 15:36:23 +08:00
function translateEmojis ( text , emoji _list , large ) {
2020-05-01 22:46:15 +08:00
let dict = getEmojiDict ( emoji _list ) ;
let regex = /:(.*?):/g ;
2020-07-03 15:36:23 +08:00
let translation = null
if ( large ) {
translation = text . replace ( regex , function ( match ) {
return "<img src=" + dict [ match ] + " class=emoji--large alt=" + match + ">" ;
} ) ;
} else {
translation = text . replace ( regex , function ( match ) {
return "<img src=" + dict [ match ] + " class=emoji alt=" + match + ">" ;
} ) ;
}
2020-05-01 22:46:15 +08:00
return translation ;
}