@@ -55,7 +53,7 @@
Host your own Mastowall - check GitHub
-
+
diff --git a/script.js b/script.js
index 24f0264..4ed0a8e 100644
--- a/script.js
+++ b/script.js
@@ -1,47 +1,7 @@
+// The existingPosts array is used to track already displayed posts
let existingPosts = [];
-// Function to calculate relative time
-const timeAgo = function(date) {
- let seconds = Math.floor((new Date() - date) / 1000);
- let interval = seconds / 31536000;
- if (interval > 1) {
- return Math.floor(interval) + " years ago";
- }
- interval = seconds / 2592000;
- if (interval > 1) {
- return Math.floor(interval) + " months ago";
- }
- interval = seconds / 86400;
- if (interval > 1) {
- return Math.floor(interval) + " days ago";
- }
- interval = seconds / 3600;
- if (interval > 1) {
- return Math.floor(interval) + " hours ago";
- }
- interval = seconds / 60;
- if (interval > 1) {
- return Math.floor(interval) + " minutes ago";
- }
- return Math.floor(seconds) + " seconds ago";
-}
-
-// Function to update times
-const updateTimes = function() {
- // Find each timestamp element in the DOM
- $('.card-text a').each(function() {
- // Get the original date of the post
- let date = new Date($(this).attr('data-time'));
-
- // Calculate the new relative time
- let newTimeAgo = timeAgo(date);
-
- // Update the timestamp with the new relative time
- $(this).text(newTimeAgo);
- });
-};
-
-// Function to get a parameter by name from URL
+// getUrlParameter helps to fetch URL parameters
function getUrlParameter(name) {
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
var regex = new RegExp('[\\?&]' + name + '=([^]*)');
@@ -49,163 +9,181 @@ function getUrlParameter(name) {
return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
}
-// Get hashtags from URL parameters
-let hashtags = getUrlParameter('hashtags');
+// secondsAgo calculates how many seconds have passed since the provided date
+const secondsAgo = date => Math.floor((new Date() - date) / 1000);
-// Split the hashtags string into an array
-let hashtagsArray = hashtags.split(',');
+// timeAgo formats the time elapsed in a human readable format
+const timeAgo = function(seconds) {
+ // An array of intervals for years, months, days, hours, and minutes.
+ const intervals = [
+ { limit: 31536000, text: 'years' },
+ { limit: 2592000, text: 'months' },
+ { limit: 86400, text: 'days' },
+ { limit: 3600, text: 'hours' },
+ { limit: 60, text: 'minutes' }
+ ];
+ // Loop through the intervals to find which one is the best fit.
+ for (let interval of intervals) {
+ if (seconds >= interval.limit) {
+ return Math.floor(seconds / interval.limit) + ` ${interval.text} ago`;
+ }
+ }
+ return Math.floor(seconds) + " seconds ago";
+};
-// Get server from URL parameters or use default
-let server = getUrlParameter('server') || 'https://mastodon.social';
+let includeReplies;
-// Function to fetch posts for a given hashtag
-const getPosts = function(hashtag) {
- return $.get(`${server}/api/v1/timelines/tag/${hashtag}?limit=20`);
+// fetchConfig fetches the configuration from the config.json file
+const fetchConfig = async function() {
+ try {
+ const config = await $.getJSON('config.json');
+ $('#navbar-brand').text(config.navbarBrandText);
+ $('.navbar').css('background-color', config.navbarColor);
+ includeReplies = config.includeReplies;
+ return config.defaultServerUrl;
+ } catch (error) {
+ console.error("Error loading config.json:", error);
+ }
}
+// fetchPosts fetches posts from the server using the given hashtag
+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);
+ }
+};
-// Function to fetch and display posts
-const fetchAndDisplayPosts = function() {
-
- // Fetch posts for each hashtag
- $.when(...hashtagsArray.map(hashtag => getPosts(hashtag))).then(function(...hashtagPosts) {
- let allPosts;
-
- // Check if there are multiple hashtags or just one
- if (hashtagsArray.length > 1) {
- // If there are multiple hashtags, `hashtagPosts` is an array of arrays
- // We use Array.prototype.flat() to combine them into one array
- allPosts = hashtagPosts.map(postData => postData[0]).flat();
- } else {
- // If there's only one hashtag, `hashtagPosts` is a single array
- allPosts = hashtagPosts[0];
- }
-
- // Sort the posts by date/time
- allPosts.sort((a, b) => new Date(a.created_at) - new Date(b.created_at));
-
- // Loop through the sorted posts
- $.each(allPosts, function(i, post) {
- // Check if the post is not already displayed and is not a mention
- if (!existingPosts.includes(post.id) && post.in_reply_to_id === null) {
- // Add the post id to existingPosts
- existingPosts.push(post.id);
-
- let cardHTML = `
-
- `;
-
- // Convert the HTML string into a jQuery object
- let $card = $(cardHTML);
-
- // Prepend the new card to the wall
- $('#wall').prepend($card);
-
- // Refresh Masonry layout after all new cards have been added
- $('.masonry-grid').masonry('prepended', $card);
- }
- });
+// updateTimesOnPage updates the time information displayed for each post
+const updateTimesOnPage = function() {
+ $('.card-text a').each(function() {
+ const date = new Date($(this).attr('data-time'));
+ const newTimeAgo = timeAgo(secondsAgo(date));
+ $(this).text(newTimeAgo);
});
};
-$(document).ready(function() {
- // Initialize Masonry
+// displayPost creates and displays a post
+const displayPost = function(post) {
+ if (existingPosts.includes(post.id) || (!includeReplies && post.in_reply_to_id !== null)) return;
+
+ existingPosts.push(post.id);
+
+ let cardHTML = `
+