2023-05-19 16:16:49 +02:00
|
|
|
// The existingPosts array is used to track already displayed posts
|
2023-05-17 15:46:48 +02:00
|
|
|
let existingPosts = [];
|
2023-05-17 02:13:01 +02:00
|
|
|
|
2023-05-19 16:16:49 +02:00
|
|
|
// getUrlParameter helps to fetch URL parameters
|
2023-05-19 14:58:02 +02:00
|
|
|
function getUrlParameter(name) {
|
|
|
|
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
|
|
|
|
var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
|
|
|
|
var results = regex.exec(location.search);
|
|
|
|
return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
|
|
|
|
}
|
|
|
|
|
2023-05-19 16:16:49 +02:00
|
|
|
// secondsAgo calculates how many seconds have passed since the provided date
|
2023-05-19 14:58:02 +02:00
|
|
|
const secondsAgo = date => Math.floor((new Date() - date) / 1000);
|
2023-05-19 16:16:49 +02:00
|
|
|
|
|
|
|
// timeAgo formats the time elapsed in a human readable format
|
2023-05-19 14:58:02 +02:00
|
|
|
const timeAgo = function(seconds) {
|
2023-05-19 16:16:49 +02:00
|
|
|
// An array of intervals for years, months, days, hours, and minutes.
|
2023-05-19 14:58:02 +02:00
|
|
|
const intervals = [
|
|
|
|
{ limit: 31536000, text: 'years' },
|
|
|
|
{ limit: 2592000, text: 'months' },
|
|
|
|
{ limit: 86400, text: 'days' },
|
|
|
|
{ limit: 3600, text: 'hours' },
|
|
|
|
{ limit: 60, text: 'minutes' }
|
|
|
|
];
|
|
|
|
|
2023-05-19 16:16:49 +02:00
|
|
|
// Loop through the intervals to find which one is the best fit.
|
2023-05-19 14:58:02 +02:00
|
|
|
for (let interval of intervals) {
|
|
|
|
if (seconds >= interval.limit) {
|
|
|
|
return Math.floor(seconds / interval.limit) + ` ${interval.text} ago`;
|
|
|
|
}
|
2023-05-17 15:46:48 +02:00
|
|
|
}
|
|
|
|
return Math.floor(seconds) + " seconds ago";
|
2023-05-19 14:58:02 +02:00
|
|
|
};
|
2023-05-17 02:13:01 +02:00
|
|
|
|
2023-05-19 16:16:49 +02:00
|
|
|
let includeReplies;
|
|
|
|
|
|
|
|
// fetchConfig fetches the configuration from the config.json file
|
2023-05-19 14:58:02 +02:00
|
|
|
const fetchConfig = async function() {
|
|
|
|
try {
|
|
|
|
const config = await $.getJSON('config.json');
|
|
|
|
$('#navbar-brand').text(config.navbarBrandText);
|
2023-05-19 16:16:49 +02:00
|
|
|
$('.navbar').css('background-color', config.navbarColor);
|
|
|
|
includeReplies = config.includeReplies;
|
2023-05-19 14:58:02 +02:00
|
|
|
return config.defaultServerUrl;
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error loading config.json:", error);
|
|
|
|
}
|
|
|
|
}
|
2023-05-18 02:43:37 +02:00
|
|
|
|
2023-05-19 16:16:49 +02:00
|
|
|
// fetchPosts fetches posts from the server using the given hashtag
|
2023-05-19 14:58:02 +02:00
|
|
|
const fetchPosts = async function(serverUrl, hashtag) {
|
|
|
|
try {
|
|
|
|
const posts = await $.get(`${serverUrl}/api/v1/timelines/tag/${hashtag}?limit=20`);
|
|
|
|
return posts;
|
|
|
|
} catch (error) {
|
|
|
|
console.error(`Error loading posts for hashtag #${hashtag}:`, error);
|
|
|
|
}
|
|
|
|
};
|
2023-05-18 02:43:37 +02:00
|
|
|
|
2023-05-19 16:16:49 +02:00
|
|
|
// updateTimesOnPage updates the time information displayed for each post
|
2023-05-19 14:58:02 +02:00
|
|
|
const updateTimesOnPage = function() {
|
|
|
|
$('.card-text a').each(function() {
|
|
|
|
const date = new Date($(this).attr('data-time'));
|
|
|
|
const newTimeAgo = timeAgo(secondsAgo(date));
|
2023-05-18 02:43:37 +02:00
|
|
|
$(this).text(newTimeAgo);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-05-19 16:16:49 +02:00
|
|
|
// displayPost creates and displays a post
|
2023-05-19 14:58:02 +02:00
|
|
|
const displayPost = function(post) {
|
2023-05-19 16:16:49 +02:00
|
|
|
if (existingPosts.includes(post.id) || (!includeReplies && post.in_reply_to_id !== null)) return;
|
2023-05-17 23:58:12 +02:00
|
|
|
|
2023-05-19 14:58:02 +02:00
|
|
|
existingPosts.push(post.id);
|
2023-05-17 23:58:12 +02:00
|
|
|
|
2023-05-19 14:58:02 +02:00
|
|
|
let cardHTML = `
|
|
|
|
<div class="col-sm-3">
|
|
|
|
<div class="card m-2 p-2">
|
|
|
|
<div class="d-flex align-items-center mb-2">
|
|
|
|
<img src="${post.account.avatar}" class="avatar-img rounded-circle mr-2">
|
2023-05-26 10:12:47 +02:00
|
|
|
<p class="m-0">${DOMPurify.sanitize(post.account.display_name)}</p>
|
2023-05-19 14:58:02 +02:00
|
|
|
</div>
|
|
|
|
${post.media_attachments[0] ? `<img src="${post.media_attachments[0].url}" class="card-img-top mb-2">` : ''}
|
2023-05-26 10:12:47 +02:00
|
|
|
<p class="card-text">${DOMPurify.sanitize(post.content)}</p>
|
|
|
|
${post.spoiler_text ? `<p class="card-text text-muted spoiler">${DOMPurify.sanitize(post.spoiler_text)}</p>` : ''}
|
2023-05-20 23:56:10 +02:00
|
|
|
<p class="card-text text-right"><small class="text-muted"><a href="${post.url}" target="_blank" data-time="${post.created_at}">${timeAgo(secondsAgo(new Date(post.created_at)))}</a></small></p>
|
2023-05-19 14:58:02 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
|
|
|
|
let $card = $(cardHTML);
|
|
|
|
$('#wall').prepend($card);
|
|
|
|
$('.masonry-grid').masonry('prepended', $card);
|
|
|
|
};
|
2023-05-17 23:58:12 +02:00
|
|
|
|
2023-05-19 16:16:49 +02:00
|
|
|
// updateWall displays all posts
|
2023-05-19 14:58:02 +02:00
|
|
|
const updateWall = function(posts) {
|
|
|
|
if (!posts || posts.length === 0) return;
|
2023-05-17 23:58:12 +02:00
|
|
|
|
2023-05-19 14:58:02 +02:00
|
|
|
posts.sort((a, b) => new Date(a.created_at) - new Date(b.created_at));
|
|
|
|
posts.forEach(post => displayPost(post));
|
|
|
|
};
|
2023-05-18 09:48:01 +02:00
|
|
|
|
2023-05-19 16:16:49 +02:00
|
|
|
// updateHashtagsOnPage updates the displayed hashtags
|
2023-05-19 14:58:02 +02:00
|
|
|
const updateHashtagsOnPage = function(hashtagsArray) {
|
|
|
|
$('#hashtag-display').text(hashtagsArray.length > 0 ? `${hashtagsArray.map(hashtag => `#${hashtag}`).join(' ')}` : 'No hashtags set');
|
|
|
|
};
|
2023-05-17 02:13:01 +02:00
|
|
|
|
2023-05-19 16:16:49 +02:00
|
|
|
// handleHashtagDisplayClick handles the event when the hashtag display is clicked
|
2023-05-19 14:58:02 +02:00
|
|
|
const handleHashtagDisplayClick = function(serverUrl) {
|
|
|
|
$('#app-content').addClass('d-none');
|
|
|
|
$('#zero-state').removeClass('d-none');
|
2023-05-17 23:58:12 +02:00
|
|
|
|
2023-05-19 14:58:02 +02:00
|
|
|
const currentHashtags = getUrlParameter('hashtags').split(',');
|
|
|
|
|
|
|
|
for (let i = 0; i < currentHashtags.length; i++) {
|
|
|
|
$(`#hashtag${i+1}`).val(currentHashtags[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$('#serverUrl').val(serverUrl);
|
|
|
|
};
|
2023-05-17 02:13:01 +02:00
|
|
|
|
2023-05-19 16:16:49 +02:00
|
|
|
// handleHashtagFormSubmit handles the submission of the hashtag form
|
2023-05-19 14:58:02 +02:00
|
|
|
const handleHashtagFormSubmit = function(e, hashtagsArray) {
|
|
|
|
e.preventDefault();
|
2023-05-17 02:13:01 +02:00
|
|
|
|
2023-05-19 14:58:02 +02:00
|
|
|
let hashtags = [
|
|
|
|
$('#hashtag1').val(),
|
|
|
|
$('#hashtag2').val(),
|
|
|
|
$('#hashtag3').val()
|
|
|
|
];
|
2023-05-17 02:13:01 +02:00
|
|
|
|
2023-05-19 14:58:02 +02:00
|
|
|
hashtags = hashtags.filter(function(hashtag) {
|
|
|
|
return hashtag !== '' && /^[\w]+$/.test(hashtag);
|
2023-05-17 15:46:48 +02:00
|
|
|
});
|
2023-05-19 14:58:02 +02:00
|
|
|
|
|
|
|
let serverUrl = $('#serverUrl').val();
|
|
|
|
|
|
|
|
if (!/^https:\/\/[\w.\-]+\/?$/.test(serverUrl)) {
|
|
|
|
alert('Invalid server URL.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const newUrl = window.location.origin + window.location.pathname + `?hashtags=${hashtags.join(',')}&server=${serverUrl}`;
|
|
|
|
|
|
|
|
window.location.href = newUrl;
|
2023-05-17 15:46:48 +02:00
|
|
|
};
|
2023-05-17 02:13:01 +02:00
|
|
|
|
2023-05-19 16:16:49 +02:00
|
|
|
// On document ready, the script configures Masonry, handles events, fetches and displays posts
|
2023-05-19 14:58:02 +02:00
|
|
|
$(document).ready(async function() {
|
|
|
|
const defaultServerUrl = await fetchConfig();
|
2023-05-17 15:46:48 +02:00
|
|
|
$('.masonry-grid').masonry({
|
|
|
|
itemSelector: '.col-sm-3',
|
|
|
|
columnWidth: '.col-sm-3',
|
|
|
|
percentPosition: true
|
2023-05-17 02:13:01 +02:00
|
|
|
});
|
2023-05-17 15:46:48 +02:00
|
|
|
|
2023-05-18 10:49:55 +02:00
|
|
|
setInterval(function() {
|
|
|
|
$('.masonry-grid').masonry('layout');
|
|
|
|
}, 10000);
|
|
|
|
|
2023-05-19 14:58:02 +02:00
|
|
|
const hashtags = getUrlParameter('hashtags');
|
|
|
|
const hashtagsArray = hashtags ? hashtags.split(',') : [];
|
|
|
|
const serverUrl = getUrlParameter('server') || defaultServerUrl;
|
2023-05-18 09:48:01 +02:00
|
|
|
|
2023-05-19 14:58:02 +02:00
|
|
|
$('#hashtag-display').on('click', function() {
|
|
|
|
handleHashtagDisplayClick(serverUrl);
|
2023-05-18 09:48:01 +02:00
|
|
|
});
|
|
|
|
|
2023-05-19 14:58:02 +02:00
|
|
|
if (hashtagsArray.length > 0 && hashtagsArray[0] !== '') {
|
|
|
|
const allPosts = await Promise.all(hashtagsArray.map(hashtag => fetchPosts(serverUrl, hashtag)));
|
|
|
|
updateWall(allPosts.flat());
|
|
|
|
setInterval(async function() {
|
|
|
|
const newPosts = await Promise.all(hashtagsArray.map(hashtag => fetchPosts(serverUrl, hashtag)));
|
|
|
|
updateWall(newPosts.flat());
|
|
|
|
}, 10000);
|
2023-05-18 02:43:37 +02:00
|
|
|
} else {
|
|
|
|
$('#zero-state').removeClass('d-none');
|
|
|
|
$('#app-content').addClass('d-none');
|
|
|
|
}
|
|
|
|
|
2023-05-19 14:58:02 +02:00
|
|
|
updateHashtagsOnPage(hashtagsArray);
|
2023-05-18 02:43:37 +02:00
|
|
|
|
2023-05-18 09:48:01 +02:00
|
|
|
$('#hashtag-form').on('submit', function(e) {
|
2023-05-19 14:58:02 +02:00
|
|
|
handleHashtagFormSubmit(e, hashtagsArray);
|
2023-05-18 09:48:01 +02:00
|
|
|
});
|
|
|
|
|
2023-05-19 14:58:02 +02:00
|
|
|
updateTimesOnPage();
|
|
|
|
setInterval(updateTimesOnPage, 60000);
|
2023-05-17 02:13:01 +02:00
|
|
|
});
|