commit
2e53e46537
4 changed files with 162 additions and 175 deletions
6
config.json
Normal file
6
config.json
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
{
|
||||||
|
"navbarBrandText": "Mastowall 1.0 - written by ChatGPT4 - Prompting: Ralf Stockmann (rstockm)",
|
||||||
|
"defaultServerUrl": "https://mastodon.social",
|
||||||
|
"navbarColor": "#333355",
|
||||||
|
"includeReplies": false
|
||||||
|
}
|
|
@ -9,7 +9,7 @@
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<nav class="navbar navbar-light">
|
<nav class="navbar navbar-light">
|
||||||
<a class="navbar-brand" href="#">Mastowall 0.2 - written by ChatGPT4 - Prompting: Ralf Stockmann (rstockm)</a>
|
<a class="navbar-brand" href="#" id="navbar-brand"></a>
|
||||||
<span class="navbar-info d-none d-md-block" id="hashtag-display"></span>
|
<span class="navbar-info d-none d-md-block" id="hashtag-display"></span>
|
||||||
</nav>
|
</nav>
|
||||||
|
|
||||||
|
@ -44,8 +44,6 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<div id="app-content" class="container">
|
<div id="app-content" class="container">
|
||||||
<div class="row masonry-grid" id="wall"></div>
|
<div class="row masonry-grid" id="wall"></div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -55,7 +53,7 @@
|
||||||
<span class="text-muted">Host your own Mastowall - check </span>
|
<span class="text-muted">Host your own Mastowall - check </span>
|
||||||
<a href="https://github.com/rstockm/mastowall" target="_blank">GitHub</a>
|
<a href="https://github.com/rstockm/mastowall" target="_blank">GitHub</a>
|
||||||
</div>
|
</div>
|
||||||
</footer>
|
</footer>
|
||||||
|
|
||||||
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
|
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
|
||||||
<script src="https://unpkg.com/masonry-layout@4/dist/masonry.pkgd.min.js"></script>
|
<script src="https://unpkg.com/masonry-layout@4/dist/masonry.pkgd.min.js"></script>
|
||||||
|
|
316
script.js
316
script.js
|
@ -1,47 +1,7 @@
|
||||||
|
// The existingPosts array is used to track already displayed posts
|
||||||
let existingPosts = [];
|
let existingPosts = [];
|
||||||
|
|
||||||
// Function to calculate relative time
|
// getUrlParameter helps to fetch URL parameters
|
||||||
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
|
|
||||||
function getUrlParameter(name) {
|
function getUrlParameter(name) {
|
||||||
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
|
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
|
||||||
var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
|
var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
|
||||||
|
@ -49,163 +9,181 @@ function getUrlParameter(name) {
|
||||||
return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
|
return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get hashtags from URL parameters
|
// secondsAgo calculates how many seconds have passed since the provided date
|
||||||
let hashtags = getUrlParameter('hashtags');
|
const secondsAgo = date => Math.floor((new Date() - date) / 1000);
|
||||||
|
|
||||||
// Split the hashtags string into an array
|
// timeAgo formats the time elapsed in a human readable format
|
||||||
let hashtagsArray = hashtags.split(',');
|
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 includeReplies;
|
||||||
let server = getUrlParameter('server') || 'https://mastodon.social';
|
|
||||||
|
|
||||||
// Function to fetch posts for a given hashtag
|
// fetchConfig fetches the configuration from the config.json file
|
||||||
const getPosts = function(hashtag) {
|
const fetchConfig = async function() {
|
||||||
return $.get(`${server}/api/v1/timelines/tag/${hashtag}?limit=20`);
|
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
|
// updateTimesOnPage updates the time information displayed for each post
|
||||||
const fetchAndDisplayPosts = function() {
|
const updateTimesOnPage = function() {
|
||||||
|
$('.card-text a').each(function() {
|
||||||
// Fetch posts for each hashtag
|
const date = new Date($(this).attr('data-time'));
|
||||||
$.when(...hashtagsArray.map(hashtag => getPosts(hashtag))).then(function(...hashtagPosts) {
|
const newTimeAgo = timeAgo(secondsAgo(date));
|
||||||
let allPosts;
|
$(this).text(newTimeAgo);
|
||||||
|
|
||||||
// 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 = `
|
|
||||||
<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="rounded-circle mr-2" width="50" height="50">
|
|
||||||
<h5 class="card-title m-0"><a href="${post.account.url}" target="_blank">${post.account.username}</a></h5>
|
|
||||||
</div>
|
|
||||||
<p class="card-text">${post.content}</p>
|
|
||||||
${post.media_attachments.length > 0 ? `<img src="${post.media_attachments[0].preview_url}" class="card-img-top mb-2" alt="Image">` : ''}
|
|
||||||
<p class="card-text text-right"><a href="${post.url}" target="_blank" data-time="${post.created_at}">${timeAgo(new Date(post.created_at))}</a></p>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
`;
|
|
||||||
|
|
||||||
// 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);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
$(document).ready(function() {
|
// displayPost creates and displays a post
|
||||||
// Initialize Masonry
|
const displayPost = function(post) {
|
||||||
|
if (existingPosts.includes(post.id) || (!includeReplies && post.in_reply_to_id !== null)) return;
|
||||||
|
|
||||||
|
existingPosts.push(post.id);
|
||||||
|
|
||||||
|
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">
|
||||||
|
<p class="m-0">${post.account.display_name}</p>
|
||||||
|
</div>
|
||||||
|
${post.media_attachments[0] ? `<img src="${post.media_attachments[0].url}" class="card-img-top mb-2">` : ''}
|
||||||
|
<p class="card-text">${post.content}</p>
|
||||||
|
${post.spoiler_text ? `<p class="card-text text-muted spoiler">${post.spoiler_text}</p>` : ''}
|
||||||
|
<p class="card-text"><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>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
|
||||||
|
let $card = $(cardHTML);
|
||||||
|
$('#wall').prepend($card);
|
||||||
|
$('.masonry-grid').masonry('prepended', $card);
|
||||||
|
};
|
||||||
|
|
||||||
|
// updateWall displays all posts
|
||||||
|
const updateWall = function(posts) {
|
||||||
|
if (!posts || posts.length === 0) return;
|
||||||
|
|
||||||
|
posts.sort((a, b) => new Date(a.created_at) - new Date(b.created_at));
|
||||||
|
posts.forEach(post => displayPost(post));
|
||||||
|
};
|
||||||
|
|
||||||
|
// updateHashtagsOnPage updates the displayed hashtags
|
||||||
|
const updateHashtagsOnPage = function(hashtagsArray) {
|
||||||
|
$('#hashtag-display').text(hashtagsArray.length > 0 ? `${hashtagsArray.map(hashtag => `#${hashtag}`).join(' ')}` : 'No hashtags set');
|
||||||
|
};
|
||||||
|
|
||||||
|
// handleHashtagDisplayClick handles the event when the hashtag display is clicked
|
||||||
|
const handleHashtagDisplayClick = function(serverUrl) {
|
||||||
|
$('#app-content').addClass('d-none');
|
||||||
|
$('#zero-state').removeClass('d-none');
|
||||||
|
|
||||||
|
const currentHashtags = getUrlParameter('hashtags').split(',');
|
||||||
|
|
||||||
|
for (let i = 0; i < currentHashtags.length; i++) {
|
||||||
|
$(`#hashtag${i+1}`).val(currentHashtags[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$('#serverUrl').val(serverUrl);
|
||||||
|
};
|
||||||
|
|
||||||
|
// handleHashtagFormSubmit handles the submission of the hashtag form
|
||||||
|
const handleHashtagFormSubmit = function(e, hashtagsArray) {
|
||||||
|
e.preventDefault();
|
||||||
|
|
||||||
|
let hashtags = [
|
||||||
|
$('#hashtag1').val(),
|
||||||
|
$('#hashtag2').val(),
|
||||||
|
$('#hashtag3').val()
|
||||||
|
];
|
||||||
|
|
||||||
|
hashtags = hashtags.filter(function(hashtag) {
|
||||||
|
return hashtag !== '' && /^[\w]+$/.test(hashtag);
|
||||||
|
});
|
||||||
|
|
||||||
|
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;
|
||||||
|
};
|
||||||
|
|
||||||
|
// On document ready, the script configures Masonry, handles events, fetches and displays posts
|
||||||
|
$(document).ready(async function() {
|
||||||
|
const defaultServerUrl = await fetchConfig();
|
||||||
$('.masonry-grid').masonry({
|
$('.masonry-grid').masonry({
|
||||||
itemSelector: '.col-sm-3',
|
itemSelector: '.col-sm-3',
|
||||||
columnWidth: '.col-sm-3',
|
columnWidth: '.col-sm-3',
|
||||||
percentPosition: true
|
percentPosition: true
|
||||||
});
|
});
|
||||||
|
|
||||||
// Re-arrange Masonry layout every 30 seconds
|
|
||||||
setInterval(function() {
|
setInterval(function() {
|
||||||
$('.masonry-grid').masonry('layout');
|
$('.masonry-grid').masonry('layout');
|
||||||
}, 10000);
|
}, 10000);
|
||||||
|
|
||||||
// Event listener for clicking on the hashtags
|
const hashtags = getUrlParameter('hashtags');
|
||||||
|
const hashtagsArray = hashtags ? hashtags.split(',') : [];
|
||||||
|
const serverUrl = getUrlParameter('server') || defaultServerUrl;
|
||||||
|
|
||||||
$('#hashtag-display').on('click', function() {
|
$('#hashtag-display').on('click', function() {
|
||||||
// Hide the main app content
|
handleHashtagDisplayClick(serverUrl);
|
||||||
$('#app-content').addClass('d-none');
|
|
||||||
|
|
||||||
// Show the form screen
|
|
||||||
$('#zero-state').removeClass('d-none');
|
|
||||||
|
|
||||||
// Get the current hashtags
|
|
||||||
let currentHashtags = $(this).text().split(' ');
|
|
||||||
|
|
||||||
// Pre-fill the form fields with the current hashtags
|
|
||||||
for (let i = 0; i < currentHashtags.length; i++) {
|
|
||||||
$(`#hashtag${i+1}`).val(currentHashtags[i].substring(1)); // Remove the leading '#'
|
|
||||||
}
|
|
||||||
|
|
||||||
// Pre-fill the server field with the current server
|
|
||||||
$('#serverUrl').val(server);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Check if hashtags are provided
|
if (hashtagsArray.length > 0 && hashtagsArray[0] !== '') {
|
||||||
if (hashtagsArray[0] !== '') {
|
const allPosts = await Promise.all(hashtagsArray.map(hashtag => fetchPosts(serverUrl, hashtag)));
|
||||||
// Fetch posts for each hashtag on page load
|
updateWall(allPosts.flat());
|
||||||
fetchAndDisplayPosts();
|
setInterval(async function() {
|
||||||
|
const newPosts = await Promise.all(hashtagsArray.map(hashtag => fetchPosts(serverUrl, hashtag)));
|
||||||
// Fetch posts for each hashtag every 10 seconds
|
updateWall(newPosts.flat());
|
||||||
setInterval(fetchAndDisplayPosts, 10000);
|
}, 10000);
|
||||||
} else {
|
} else {
|
||||||
// Show the zero state and hide the app content
|
|
||||||
$('#zero-state').removeClass('d-none');
|
$('#zero-state').removeClass('d-none');
|
||||||
$('#app-content').addClass('d-none');
|
$('#app-content').addClass('d-none');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the navbar info with the provided hashtags
|
updateHashtagsOnPage(hashtagsArray);
|
||||||
$('#hashtag-display').text(`${hashtagsArray.map(hashtag => `#${hashtag}`).join(' ')}`);
|
|
||||||
|
|
||||||
|
|
||||||
// Handle the form submit event
|
|
||||||
$('#hashtag-form').on('submit', function(e) {
|
$('#hashtag-form').on('submit', function(e) {
|
||||||
// Prevent the default form submission
|
handleHashtagFormSubmit(e, hashtagsArray);
|
||||||
e.preventDefault();
|
|
||||||
|
|
||||||
// Get the entered hashtags
|
|
||||||
let hashtags = [
|
|
||||||
$('#hashtag1').val(),
|
|
||||||
$('#hashtag2').val(),
|
|
||||||
$('#hashtag3').val()
|
|
||||||
];
|
|
||||||
|
|
||||||
// Filter out any empty strings and validate hashtag format
|
|
||||||
hashtags = hashtags.filter(function(hashtag) {
|
|
||||||
return hashtag !== '' && /^[\w]+$/.test(hashtag);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Get the entered server URL
|
|
||||||
let serverUrl = $('#serverUrl').val();
|
|
||||||
|
|
||||||
// Validate server URL format
|
|
||||||
if (!/^https:\/\/[\w.\-]+\/?$/.test(serverUrl)) {
|
|
||||||
alert('Invalid server URL.');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a new URL with the entered hashtags and server URL
|
|
||||||
let newUrl = window.location.origin + window.location.pathname + `?hashtags=${hashtags.join(',')}&server=${serverUrl}`;
|
|
||||||
|
|
||||||
// Reload the page with the new URL
|
|
||||||
window.location.href = newUrl;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
updateTimesOnPage();
|
||||||
// Update the times once when the page loads
|
setInterval(updateTimesOnPage, 60000);
|
||||||
updateTimes();
|
|
||||||
|
|
||||||
// Then update every 60 seconds
|
|
||||||
setInterval(updateTimes, 60000);
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -65,13 +65,13 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
.navbar-brand {
|
.navbar-brand {
|
||||||
color: #ffbbbb !important; /* change the text color */
|
color: rgba(255, 255, 255, 0.8) !important; /* change the text color */
|
||||||
margin: 0 auto; /* center the brand name */
|
margin: 0 auto; /* center the brand name */
|
||||||
font-size: 0.9em;
|
font-size: 0.9em;
|
||||||
}
|
}
|
||||||
|
|
||||||
.navbar-info {
|
.navbar-info {
|
||||||
color: #dddddd !important; /* change the text color */
|
color: rgba(255, 255, 255, 0.8) !important; /* change the text color */
|
||||||
margin: 0 auto; /* center the brand name */
|
margin: 0 auto; /* center the brand name */
|
||||||
font-size: 1.2em;
|
font-size: 1.2em;
|
||||||
display: block !important;
|
display: block !important;
|
||||||
|
@ -114,6 +114,11 @@ body {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.avatar-img {
|
||||||
|
width: 50px;
|
||||||
|
height: 50px;
|
||||||
|
}
|
||||||
|
|
||||||
.container {
|
.container {
|
||||||
max-width: 2000px !important;
|
max-width: 2000px !important;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue