2020-12-25 01:59:12 +00:00
|
|
|
// I'm not a React developer. Be advised!
|
|
|
|
// If you are, and want to rewrite this the "right way", go ahead and do it :)
|
|
|
|
// This is plain Javascript and only requires basic calls to implement customizations
|
2020-12-25 00:42:30 +00:00
|
|
|
|
|
|
|
|
2020-12-25 01:59:12 +00:00
|
|
|
// Do simple XHR requests to the API on Mastodon
|
|
|
|
// make sure you control errors correctly, as a Mastodon Server might not setup CORS correctly
|
2020-12-25 14:21:04 +00:00
|
|
|
function mastodon_get(path, payload, callbk) { return mastodon_request('GET', path, payload, callbk); }
|
|
|
|
function mastodon_post(path, payload, callbk) { return mastodon_request('POST', path, payload, callbk); }
|
|
|
|
function mastodon_request(method, path, payload, callbk) {
|
2020-12-25 01:59:12 +00:00
|
|
|
payload = payload || null;
|
2020-12-27 18:12:03 +00:00
|
|
|
var url = 'https://'+fediloveApi.getCurrentInstance()+path;
|
2020-12-25 01:59:12 +00:00
|
|
|
|
|
|
|
var oReq = new XMLHttpRequest();
|
|
|
|
oReq.addEventListener("load", function() { callbk(this.responseText); });
|
2020-12-25 14:21:04 +00:00
|
|
|
oReq.open(method, url);
|
2020-12-27 18:12:03 +00:00
|
|
|
oReq.setRequestHeader('Authorization', 'Bearer '+fediloveApi.getAccessToken());
|
2020-12-25 01:59:12 +00:00
|
|
|
oReq.send(payload);
|
|
|
|
}
|
|
|
|
|
2020-12-25 14:21:04 +00:00
|
|
|
function api_status_fav(dom, status_id) {
|
|
|
|
var dislike = false;
|
|
|
|
var path = `/api/v1/statuses/${status_id}/favourite`;
|
|
|
|
if (dom.getAttribute('data-liked') === "1") {
|
|
|
|
path = `/api/v1/statuses/${status_id}/unfavourite`;
|
|
|
|
dislike = true;
|
|
|
|
}
|
|
|
|
mastodon_post(path, {}, function(data) {
|
|
|
|
// to-do: check "data" if the POST succeded (for now we expect it did xD)
|
|
|
|
if (!dislike) {
|
|
|
|
$(dom).addClass('liked-msg');
|
|
|
|
$(dom).attr('data-liked', 1);
|
|
|
|
dom.style.animation = "spin2 .5s linear 1";
|
|
|
|
} else {
|
|
|
|
$(dom).removeClass('liked-msg');
|
|
|
|
$(dom).attr('aria-label', '0 '+$(dom).attr('aria-label'));
|
|
|
|
$(dom).removeAttr('data-liked');
|
|
|
|
dom.style.animation = undefined;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-12-29 15:13:04 +00:00
|
|
|
// The api to send a message on the current chat thread has been improved a lot
|
|
|
|
// and now is much more rock-solid, and we reuse the React functions
|
|
|
|
function api_send_message() {
|
2020-12-25 17:08:21 +00:00
|
|
|
|
2020-12-29 15:13:04 +00:00
|
|
|
// the message is composed from the current chat Acct (@user@domain) + the compose textarea value
|
2020-12-29 16:49:35 +00:00
|
|
|
const text = fediloveData.chatAvatarCache.acct + ' ' + $('div#chat-compose-global textarea').val();
|
2020-12-25 17:08:21 +00:00
|
|
|
|
2020-12-29 15:13:04 +00:00
|
|
|
//async function postStatus(realm, text, inReplyToId, mediaIds, sensitive, spoilerText, visibility, mediaDescriptions, inReplyToUuid, poll, mediaFocalPoints)
|
|
|
|
const lastStatus = fediloveApi.getChatLastMessageId(true);
|
|
|
|
fediloveFunctions.postStatus(lastStatus.id, text, lastStatus.id, [], false, undefined,
|
|
|
|
'direct', undefined, lastStatus.uuid, undefined, undefined);
|
2020-12-25 17:08:21 +00:00
|
|
|
|
2020-12-29 15:13:04 +00:00
|
|
|
// empty the current compose box
|
|
|
|
$('div#chat-compose-global textarea').val('');
|
2020-12-25 17:08:21 +00:00
|
|
|
|
2020-12-29 15:13:04 +00:00
|
|
|
// scroll to the end as all chat Apps do
|
|
|
|
fediloveUI.scrollChatToLastItem();
|
2020-12-25 17:08:21 +00:00
|
|
|
}
|
|
|
|
|
2020-12-27 18:12:03 +00:00
|
|
|
var fediloveUI = {
|
|
|
|
scrollChatToLastItem: function() {
|
2020-12-28 11:26:04 +00:00
|
|
|
const startLen = $('div.the-list > div').length;
|
|
|
|
var count = 0;
|
|
|
|
var _this = setInterval(function() {
|
|
|
|
count++;
|
|
|
|
var newLen = $('div.the-list > div').length;
|
|
|
|
if (count >= 100 || newLen != startLen) {
|
|
|
|
document.querySelector('div.the-list > div:last-child').scrollIntoView();
|
|
|
|
clearInterval(_this);
|
|
|
|
}
|
|
|
|
}, 150);
|
2020-12-29 00:07:16 +00:00
|
|
|
},
|
2020-12-29 16:49:35 +00:00
|
|
|
paintChatAvatarAndName: function(accid, acct, avatar, name) {
|
|
|
|
accid = accid || null;
|
|
|
|
acct = acct || null;
|
2020-12-29 00:07:16 +00:00
|
|
|
avatar = avatar || null;
|
|
|
|
name = name || null;
|
|
|
|
|
2020-12-29 16:49:35 +00:00
|
|
|
if ((accid+acct+avatar+name) === 0) {
|
|
|
|
accid = 0;
|
2020-12-29 00:07:16 +00:00
|
|
|
avatar = '/missing.png';
|
|
|
|
name = '...';
|
2020-12-29 15:13:04 +00:00
|
|
|
} else {
|
2020-12-29 16:49:35 +00:00
|
|
|
fediloveData.chatAvatarCache = {
|
|
|
|
id: accid, acct: acct,
|
|
|
|
avatar: avatar, name: name
|
|
|
|
};
|
2020-12-29 00:07:16 +00:00
|
|
|
}
|
|
|
|
|
2020-12-29 16:49:35 +00:00
|
|
|
// to-do: check is it XSS safe to add it like this :) ??
|
2020-12-29 00:07:16 +00:00
|
|
|
$('div#chat-party-global > div#image > img').attr('src', avatar);
|
2020-12-29 16:49:35 +00:00
|
|
|
$('div#chat-party-global > div#name > a').html(name);
|
|
|
|
$('div#chat-party-global > div#name > span').text(acct);
|
|
|
|
if (accid != 0) $('div#chat-party-global > div#name > a').attr('href', `/accounts/${accid}`);
|
2020-12-25 17:53:20 +00:00
|
|
|
}
|
2020-12-27 18:12:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// objects to access from React code
|
|
|
|
var fediloveApi = {
|
2020-12-29 15:13:04 +00:00
|
|
|
getChatMessageId: function() {
|
|
|
|
var parts = window.location.pathname.split('/');
|
|
|
|
return parts[parts.length-1];
|
|
|
|
},
|
|
|
|
getChatLastMessageId: function(andUuid) {
|
|
|
|
andUuid = andUuid || false;
|
2020-12-29 16:49:35 +00:00
|
|
|
const lastUuid = $('div.the-list article.status-article').last().attr('id');
|
2020-12-29 15:13:04 +00:00
|
|
|
if (lastUuid === undefined) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
const parts = lastUuid.split('/');
|
|
|
|
return andUuid? { id: parts[parts.length-1], uuid: lastUuid } : parts[parts.length-1];
|
|
|
|
},
|
2020-12-27 18:12:03 +00:00
|
|
|
getAccessToken: function() {
|
|
|
|
var instance = fediloveApi.getCurrentInstance();
|
|
|
|
return JSON.parse(localStorage.store_loggedInInstances)[instance].access_token;
|
|
|
|
},
|
|
|
|
getCurrentInstance: function() {
|
|
|
|
return JSON.parse(localStorage.store_currentInstance);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
var fediloveFunctions = {};
|
|
|
|
var fediloveData = {
|
2020-12-29 15:13:04 +00:00
|
|
|
chatAvatarCache: undefined,
|
2020-12-29 00:07:16 +00:00
|
|
|
gotEmojifyTextFunction: false
|
2020-12-27 18:12:03 +00:00
|
|
|
};
|
|
|
|
var fediloveEvents = {
|
2020-12-29 00:07:16 +00:00
|
|
|
onGotEmojifyTextFunction: function() {
|
|
|
|
fediloveData.gotEmojifyTextFunction = true;
|
|
|
|
},
|
2020-12-29 15:13:04 +00:00
|
|
|
onChatGetData: function(data) {
|
|
|
|
// dont do anything if avatar and name is cached
|
|
|
|
if (fediloveData.chatAvatarCache !== undefined) return;
|
|
|
|
|
|
|
|
// waits for the React code to call the "onGotEmojifyTextFunction" so we can use it
|
2020-12-29 16:49:35 +00:00
|
|
|
var waitForEmojifyAndDo = function(accid, acct, avatar, dname, emojis) {
|
2020-12-29 00:07:16 +00:00
|
|
|
var count = 0;
|
|
|
|
var _this = setInterval(function() {
|
|
|
|
if (count > 100) {
|
|
|
|
clearInterval(_this);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (fediloveData.gotEmojifyTextFunction) {
|
2020-12-29 16:49:35 +00:00
|
|
|
fediloveUI.paintChatAvatarAndName(accid, acct, avatar, fediloveFunctions.emojifyText(dname, emojis));
|
2020-12-29 00:07:16 +00:00
|
|
|
clearInterval(_this);
|
|
|
|
}
|
|
|
|
count++;
|
|
|
|
}, 150);
|
2020-12-29 15:13:04 +00:00
|
|
|
};
|
|
|
|
if (data) {
|
|
|
|
// if the message is mine, search which account is doing it for,
|
|
|
|
// and do the same with the party's data
|
2020-12-29 17:02:52 +00:00
|
|
|
if (localStorage.store_userAccountId == data.account.id) {
|
2020-12-29 16:49:35 +00:00
|
|
|
var account_id = data.in_reply_to_account_id;
|
|
|
|
if (account_id === null && data.mentions.length > 0) {
|
|
|
|
account_id = data.mentions[0].id;
|
|
|
|
}
|
|
|
|
if (account_id === null || account_id === undefined) return;
|
|
|
|
mastodon_get(`/api/v1/accounts/${account_id}`, {}, function(newData) {
|
2020-12-29 15:13:04 +00:00
|
|
|
var json = JSON.parse(newData);
|
|
|
|
if (json !== undefined) {
|
2020-12-29 16:49:35 +00:00
|
|
|
waitForEmojifyAndDo( account_id, `@${json.acct}`, json.avatar, json.display_name, json.emojis );
|
2020-12-29 15:13:04 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// this means the message we are loading is from the other party, so we continue as normal
|
2020-12-29 16:49:35 +00:00
|
|
|
waitForEmojifyAndDo( data.account.id, `@${data.account.acct}`, data.account.avatar,
|
|
|
|
data.account.display_name, data.account.emojis );
|
2020-12-29 15:13:04 +00:00
|
|
|
}
|
2020-12-29 00:07:16 +00:00
|
|
|
}
|
|
|
|
},
|
2020-12-28 14:01:15 +00:00
|
|
|
onEmojiPicked: function(emoji) {
|
|
|
|
var $txt = $("#chat-compose-global textarea");
|
|
|
|
var caretPos = $txt[0].selectionStart;
|
|
|
|
var textAreaTxt = $txt.val();
|
|
|
|
$txt.val(textAreaTxt.substring(0, caretPos) + emoji + textAreaTxt.substring(caretPos));
|
2020-12-28 11:26:04 +00:00
|
|
|
},
|
2020-12-27 18:12:03 +00:00
|
|
|
onNewNotification: function (dataItems) {
|
2020-12-28 11:26:04 +00:00
|
|
|
if (window.location.pathname.startsWith('/statuses/')) {
|
|
|
|
if (fediloveFunctions.updateStatusAndThread !== undefined) {
|
|
|
|
fediloveFunctions.updateStatusAndThread( fediloveApi.getCurrentInstance(), fediloveApi.getAccessToken(),
|
2020-12-29 16:49:35 +00:00
|
|
|
window.location.pathname, fediloveApi.getChatMessageId() );
|
2020-12-28 11:26:04 +00:00
|
|
|
fediloveUI.scrollChatToLastItem();
|
|
|
|
}
|
2020-12-27 18:12:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-12-25 14:21:04 +00:00
|
|
|
|
2020-12-25 01:59:12 +00:00
|
|
|
// this is our URL-based customizations made by JavaScript
|
|
|
|
var intervalChatCssChange = null;
|
2020-12-25 00:42:30 +00:00
|
|
|
function fedilove_customization() {
|
2020-12-28 22:07:37 +00:00
|
|
|
document.body.classList = '';
|
2020-12-25 00:42:30 +00:00
|
|
|
document.querySelector('.main-content').classList = "main-content";
|
|
|
|
document.querySelector('#chat-compose-global').style = 'visibility: collapse';
|
2020-12-28 22:07:37 +00:00
|
|
|
document.querySelector('#chat-party-hide').style = 'display: none !important';
|
|
|
|
document.querySelector('nav#main-nav > ul.main-nav-ul').style = '';
|
2020-12-25 00:42:30 +00:00
|
|
|
|
2020-12-25 01:59:12 +00:00
|
|
|
if (window.location.pathname.startsWith('/statuses/'))
|
|
|
|
{
|
2020-12-25 14:21:04 +00:00
|
|
|
$('div.main-content').addClass('chat');
|
2020-12-28 22:07:37 +00:00
|
|
|
$('body').addClass('chat');
|
2020-12-25 00:42:30 +00:00
|
|
|
document.querySelector('#chat-compose-global').style = '';
|
2020-12-28 22:07:37 +00:00
|
|
|
document.querySelector('#chat-party-hide').style = '';
|
|
|
|
document.querySelector('nav#main-nav > ul.main-nav-ul').style = 'display: none !important';
|
2020-12-25 01:59:12 +00:00
|
|
|
|
2020-12-29 15:13:04 +00:00
|
|
|
// *******
|
|
|
|
// load cached avatars or paint it empty (automated process after this will fill it)
|
|
|
|
if (fediloveData.chatAvatarCache !== undefined) {
|
2020-12-29 16:49:35 +00:00
|
|
|
fediloveUI.paintChatAvatarAndName( fediloveData.chatAvatarCache.id, fediloveData.chatAvatarCache.acct,
|
|
|
|
fediloveData.chatAvatarCache.avatar, fediloveData.chatAvatarCache.name );
|
2020-12-29 15:13:04 +00:00
|
|
|
} else {
|
|
|
|
fediloveUI.paintChatAvatarAndName();
|
|
|
|
}
|
2020-12-29 00:07:16 +00:00
|
|
|
|
2020-12-25 14:21:04 +00:00
|
|
|
// add some animations
|
|
|
|
var style = document.createElement('style');
|
|
|
|
style.innerHTML = '@keyframes spin2 { 100% { transform:rotate(-360deg); } }';
|
|
|
|
document.body.appendChild(style);
|
|
|
|
|
2020-12-25 01:59:12 +00:00
|
|
|
// this function changes the css class on articles (messages)
|
|
|
|
// that match the given account_id
|
2020-12-25 14:21:04 +00:00
|
|
|
var makeMessageUIModifications = function(account_id) {
|
|
|
|
if (intervalChatCssChange !== null) return;
|
|
|
|
var theint = 150;
|
|
|
|
setInterval(function()
|
|
|
|
{
|
2020-12-25 01:59:12 +00:00
|
|
|
intervalChatCssChange = this;
|
2020-12-25 14:21:04 +00:00
|
|
|
|
|
|
|
// paint MY messages as mine
|
|
|
|
$('div.main-content.chat article.status-article').each(function(i) {
|
|
|
|
if ($(this).find('a.status-author-name').attr('href').endsWith(`/accounts/${account_id}`)) {
|
|
|
|
$(this).addClass('mymsg');
|
|
|
|
theint = 250;
|
2020-12-28 22:07:37 +00:00
|
|
|
} else {
|
|
|
|
$(this).addClass('partymsg');
|
2020-12-25 01:59:12 +00:00
|
|
|
}
|
2020-12-25 14:21:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// paint LIKES
|
|
|
|
$('a.status-favs-reblogs.status-favs').each(function(i) {
|
|
|
|
// easy, aria-label contains the times this status was fav
|
|
|
|
// so, we use this as the input source to search for "0",
|
|
|
|
// if no "0" found in text, we mark element as liked so we can apply styles and "undo like" function
|
|
|
|
if ($(this).attr('aria-label').search(/0/) == -1) {
|
|
|
|
$(this).addClass('liked-msg');
|
|
|
|
$(this).attr('data-liked', 1);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-12-28 22:07:37 +00:00
|
|
|
// remove liking functionality from our own messages
|
|
|
|
$('div.the-list article.status-article.mymsg a.status-favs-reblogs').each(function(i) {
|
|
|
|
$(this).attr('onclick', 'return false;');
|
|
|
|
});
|
|
|
|
|
2020-12-30 11:04:02 +00:00
|
|
|
// resize likes acording to the outer parent
|
|
|
|
$('div.main-content.chat div.the-list article.status-article').each(function(e) {
|
|
|
|
$(this).find('div.like-div').width($(this).width());
|
|
|
|
});
|
|
|
|
|
2020-12-25 14:21:04 +00:00
|
|
|
}, theint);
|
2020-12-25 01:59:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// get the userAccountId to check against the href /account/NUM on the messages
|
|
|
|
// so we can apply different style to my messages
|
|
|
|
if (localStorage.store_userAccountId === undefined) {
|
2020-12-25 14:21:04 +00:00
|
|
|
mastodon_get('/api/v1/accounts/verify_credentials', {}, function(data) {
|
2020-12-25 01:59:12 +00:00
|
|
|
var json = JSON.parse(data);
|
|
|
|
if (json !== undefined) {
|
|
|
|
localStorage.store_userAccountId = json.id;
|
2020-12-25 14:21:04 +00:00
|
|
|
makeMessageUIModifications(json.id);
|
2020-12-25 01:59:12 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
2020-12-25 14:21:04 +00:00
|
|
|
makeMessageUIModifications(localStorage.store_userAccountId);
|
2020-12-25 01:59:12 +00:00
|
|
|
}
|
2020-12-25 00:42:30 +00:00
|
|
|
}
|
2020-12-28 14:01:15 +00:00
|
|
|
else if (window.location.pathname == '/direct') {
|
|
|
|
$('div.main-content').addClass('direct');
|
|
|
|
}
|
2020-12-27 18:12:03 +00:00
|
|
|
else if (window.location.pathname.startsWith('/notifications/mentions')) {
|
|
|
|
$('nav.notification-filters li > a.focus-fix').attr('onclick', 'return false;');
|
|
|
|
}
|
2020-12-29 15:13:04 +00:00
|
|
|
|
|
|
|
if (!window.location.pathname.startsWith('/statuses/')) {
|
|
|
|
fediloveData.chatAvatarCache = undefined;
|
|
|
|
}
|
2020-12-25 00:42:30 +00:00
|
|
|
}
|
|
|
|
|
2020-12-25 01:59:12 +00:00
|
|
|
// we inject this script.js into the React framework at timelines.js
|
|
|
|
// Watch for URL changes every 1/2 seconds (this is efficient, don't worry about it!)
|
|
|
|
// and dispatch a call to "fedilove_customization" to load page customizations depending on URL context
|
2020-12-25 00:42:30 +00:00
|
|
|
var theurl = null;
|
|
|
|
setInterval(function() {
|
|
|
|
var newurl = window.location.pathname;
|
|
|
|
if (newurl != theurl) {
|
|
|
|
fedilove_customization();
|
|
|
|
theurl = newurl;
|
|
|
|
}
|
2020-12-25 14:21:04 +00:00
|
|
|
}, 250);
|
2020-12-25 00:42:30 +00:00
|
|
|
|