Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,438 changes: 2,311 additions & 127 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,14 @@
"express": "^4.17.1",
"express-validator": "^7.0.1",
"express-ws": "^5.0.2",
"form-data": "^4.0.5",
"geoip-lite": "^1.4.10",
"googleapis": "^100.0.0",
"isomorphic-fetch": "^3.0.0",
"jsonwebtoken": "^9.0.0",
"lodash": "^4.17.21",
"masto": "^7.4.0",
"mastodon-api": "^1.3.0",
"moment": "^2.29.4",
"moment-timezone": "^0.5.35",
"mongodb": "^3.7.3",
Expand Down
238 changes: 238 additions & 0 deletions src/controllers/mastodonPostController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
const axios = require('axios');
const FormData = require('form-data');
const MastodonSchedule = require('../models/mastodonSchedule');

const MASTODON_ENDPOINT = process.env.MASTODON_ENDPOINT || 'https://mastodon.social';
const ACCESS_TOKEN = process.env.MASTODON_ACCESS_TOKEN;

// Simple: get bearer headers or throw if missing
function getAuthHeaders() {
if (!ACCESS_TOKEN) throw new Error('MASTODON_ACCESS_TOKEN not set');
return { Authorization: `Bearer ${ACCESS_TOKEN}` };
}

// Upload image to Mastodon with optional alt text and get media ID
async function uploadMedia(base64Image, altText = null) {
try {
// Convert base64 to buffer
const base64Data = base64Image.replace(/^data:image\/\w+;base64,/, '');
const buffer = Buffer.from(base64Data, 'base64');

// Step 1: Upload the media file
const formData = new FormData();
formData.append('file', buffer, {
filename: 'upload.png',
contentType: 'image/png',
});

const uploadUrl = `${MASTODON_ENDPOINT}/api/v1/media`;
const uploadHeaders = {
...getAuthHeaders(),
...formData.getHeaders(),
};

const uploadResponse = await axios.post(uploadUrl, formData, { headers: uploadHeaders });
const mediaId = uploadResponse.data.id;

console.log('Image uploaded, media ID:', mediaId);

// Step 2: Update the media with alt text if provided
if (altText && altText.trim()) {
console.log('Updating media with alt text...');
const updateUrl = `${MASTODON_ENDPOINT}/api/v1/media/${mediaId}`;
const updateHeaders = {
...getAuthHeaders(),
'Content-Type': 'application/json',
};

await axios.put(
updateUrl,
{
description: altText.trim(),
},
{ headers: updateHeaders },
);

console.log('Alt text updated successfully');
}

return mediaId;
} catch (err) {
console.error('Media upload failed:', err.response?.data || err.message);
throw new Error('Failed to upload image to Mastodon');
}
}

// Build post body (with optional image handling)
async function buildPostData({ title, description, imgType, mediaItems, mediaAltText }) {
const text = description || title;
if (!text?.trim()) throw new Error("Post content can't be empty");

const postData = {
status: text.trim(),
visibility: 'public',
};

// Handle image upload
if (imgType === 'FILE' && mediaItems?.startsWith('data:')) {
try {
const mediaId = await uploadMedia(mediaItems, mediaAltText);
// eslint-disable-next-line camelcase
postData.media_ids = [mediaId];
// Store base64 and alt text for scheduled posts
// eslint-disable-next-line camelcase
postData.local_media_base64 = mediaItems;
if (mediaAltText) {
postData.mediaAltText = mediaAltText;
}
} catch (err) {
console.error('Image upload failed, posting without image:', err.message);
// Continue without image rather than failing the entire post
}
} else if (imgType === 'URL' && mediaItems) {
// For URL type, store for future use
// eslint-disable-next-line camelcase
postData.local_media_url = mediaItems;
}

return postData;
}

// Post immediately to Mastodon
async function postImmediately(postData) {
const url = `${MASTODON_ENDPOINT}/api/v1/statuses`;
const headers = getAuthHeaders();

// Remove local_media fields and mediaAltText before sending to Mastodon
// eslint-disable-next-line camelcase
const { local_media_base64, local_media_url, mediaAltText, ...mastodonPostData } = postData;

return axios.post(url, mastodonPostData, { headers, responseType: 'json' });
}

// Express controller: immediate post
async function createStatus(req, res) {
try {
const postData = await buildPostData(req.body);
const response = await postImmediately(postData);
res.status(200).json(response.data);
} catch (err) {
console.error('Mastodon post failed:', err.response?.data || err.message);
const status = err.response?.status || 500;
const msg = err.response?.data?.message || err.message || 'Failed to create Mastodon status';
res.status(status).json({ error: msg });
}
}

// Schedule a post
async function scheduleStatus(req, res) {
try {
// Don't upload the image yet for scheduled posts
// Just store the base64 data and alt text
const text = req.body.description || req.body.title;
if (!text?.trim()) throw new Error("Post content can't be empty");

const postData = {
status: text.trim(),
visibility: 'public',
};

// Store image data and alt text for later upload
if (req.body.imgType === 'FILE' && req.body.mediaItems) {
// eslint-disable-next-line camelcase
postData.local_media_base64 = req.body.mediaItems;
if (req.body.mediaAltText) {
postData.mediaAltText = req.body.mediaAltText;
}
} else if (req.body.imgType === 'URL' && req.body.mediaItems) {
// eslint-disable-next-line camelcase
postData.local_media_url = req.body.mediaItems;
}

const { scheduledTime } = req.body;
await MastodonSchedule.create({
postData: JSON.stringify(postData),
scheduledTime,
});
res.sendStatus(200);
} catch (err) {
console.error('Schedule failed:', err.message);
res.status(500).json({ error: err.message });
}
}

// Fetch scheduled posts
async function fetchScheduledStatus(_req, res) {
try {
const scheduled = await MastodonSchedule.find();
res.json(scheduled);
} catch (err) {
res.status(500).send('Failed to fetch scheduled pins');
}
}

// Delete scheduled post
async function deleteScheduledStatus(req, res) {
try {
await MastodonSchedule.deleteOne({ _id: req.params.id });
res.send('Scheduled post deleted successfully');
} catch {
res.status(500).send('Failed to delete scheduled post');
}
}

// Fetch post history from Mastodon
async function fetchPostHistory(req, res) {
try {
const headers = getAuthHeaders();

// Get the account ID first
const accountUrl = `${MASTODON_ENDPOINT}/api/v1/accounts/verify_credentials`;
const accountResponse = await axios.get(accountUrl, { headers });
const accountId = accountResponse.data.id;

// Fetch statuses for this account (limit to last 20 posts)
const limit = req.query.limit || 20;
const statusesUrl = `${MASTODON_ENDPOINT}/api/v1/accounts/${accountId}/statuses`;
const statusesResponse = await axios.get(statusesUrl, {
headers,
params: {
limit,
exclude_replies: true,
exclude_reblogs: true,
},
});

// Format the response
const posts = statusesResponse.data.map((status) => ({
id: status.id,
content: status.content,
// eslint-disable-next-line camelcase
created_at: status.created_at,
url: status.url,
// eslint-disable-next-line camelcase
media_attachments: status.media_attachments || [],
// eslint-disable-next-line camelcase
favourites_count: status.favourites_count || 0,
// eslint-disable-next-line camelcase
reblogs_count: status.reblogs_count || 0,
}));

res.json(posts);
} catch (err) {
console.error('Failed to fetch post history:', err.response?.data || err.message);
const status = err.response?.status || 500;
const msg = err.response?.data?.error || err.message || 'Failed to fetch post history';
res.status(status).json({ error: msg });
}
}

module.exports = {
createPin: createStatus,
schedulePin: scheduleStatus,
fetchScheduledPin: fetchScheduledStatus,
deletedScheduledPin: deleteScheduledStatus,
fetchPostHistory,
postImmediately,
uploadMedia,
};
88 changes: 88 additions & 0 deletions src/cronjobs/mastodonScheduleJob.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
const cron = require('node-cron');
const axios = require('axios');
const MastodonSchedule = require('../models/mastodonSchedule');
const { uploadMedia } = require('../controllers/mastodonPostController');

const MASTODON_ENDPOINT = process.env.MASTODON_ENDPOINT || 'https://mastodon.social';
const ACCESS_TOKEN = process.env.MASTODON_ACCESS_TOKEN;

function getAuthHeaders() {
if (!ACCESS_TOKEN) throw new Error('MASTODON_ACCESS_TOKEN not set');
return { Authorization: `Bearer ${ACCESS_TOKEN}` };
}

async function postToMastodon(postData) {
const url = `${MASTODON_ENDPOINT}/api/v1/statuses`;
const headers = getAuthHeaders();

// Parse if string
const data = typeof postData === 'string' ? JSON.parse(postData) : postData;

// Create the actual post data for Mastodon
const mastodonData = {
status: data.status,
visibility: data.visibility || 'public',
};

// Handle image upload if present
// eslint-disable-next-line camelcase
if (data.local_media_base64) {
try {
console.log('Uploading image from scheduled post...');
// eslint-disable-next-line camelcase
const altText = data.mediaAltText || null;
// eslint-disable-next-line camelcase
const mediaId = await uploadMedia(data.local_media_base64, altText);
console.log('Image uploaded, media ID:', mediaId);
// eslint-disable-next-line camelcase
mastodonData.media_ids = [mediaId];
} catch (err) {
console.error('Image upload failed in cron job:', err.message);
// Continue without image
}
}

console.log('Posting to Mastodon:', `${mastodonData.status.substring(0, 50)}...`);

return axios.post(url, mastodonData, { headers, responseType: 'json' });
}

async function processScheduledPosts() {
try {
const now = new Date();
const scheduled = await MastodonSchedule.find({
scheduledTime: { $lte: now },
});

if (scheduled.length > 0) {
console.log(`Found ${scheduled.length} scheduled posts to process`);
}

// Use Promise.all with map instead of for-of loop
await Promise.all(
scheduled.map(async (post) => {
try {
console.log(`Processing scheduled post ${post._id}`);
await postToMastodon(post.postData);
await MastodonSchedule.deleteOne({ _id: post._id });
console.log(`✅ Posted scheduled Mastodon post: ${post._id}`);
} catch (err) {
console.error(`❌ Failed to post scheduled Mastodon post ${post._id}:`, err.message);
if (err.response?.data) {
console.error('Mastodon API error:', err.response.data);
}
}
}),
);
} catch (err) {
console.error('Error processing scheduled Mastodon posts:', err.message);
}
}

// Run every minute
function startMastodonScheduleJob() {
cron.schedule('* * * * *', processScheduledPosts);
console.log('✅ Mastodon schedule cron job started (runs every minute)');
}

module.exports = { startMastodonScheduleJob, processScheduledPosts };
10 changes: 10 additions & 0 deletions src/models/mastodonSchedule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const mongoose = require('mongoose');

const { Schema } = mongoose;

const mastodonSchedule = new Schema({
postData: { type: String, required: true },
scheduledTime: { type: Date, required: true },
});

module.exports = mongoose.model('mastodonSchedule', mastodonSchedule);
34 changes: 34 additions & 0 deletions src/routes/mastodonRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// const express = require('express');
// const {
// createPin,
// schedulePin,
// fetchScheduledPin,
// deletedScheduledPin,
// } = require('../controllers/mastodonPostController');

// const mastodonRouter = express.Router();

// mastodonRouter.post('/mastodon/createPin', createPin);
// mastodonRouter.post('/mastodon/schedule', schedulePin);
// mastodonRouter.get('/mastodon/schedule', fetchScheduledPin);
// mastodonRouter.delete('/mastodon/schedule/:id', deletedScheduledPin);

// module.exports = mastodonRouter;
const express = require('express');
const {
createPin,
schedulePin,
fetchScheduledPin,
deletedScheduledPin,
fetchPostHistory,
} = require('../controllers/mastodonPostController');

const mastodonRouter = express.Router();

mastodonRouter.post('/mastodon/createPin', createPin);
mastodonRouter.post('/mastodon/schedule', schedulePin);
mastodonRouter.get('/mastodon/schedule', fetchScheduledPin);
mastodonRouter.delete('/mastodon/schedule/:id', deletedScheduledPin);
mastodonRouter.get('/mastodon/history', fetchPostHistory);

module.exports = mastodonRouter;
1 change: 1 addition & 0 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require('./startup/db')();
require('./cronjobs/userProfileJobs')();
require('./jobs/analyticsAggregation').scheduleDaily();
require('./cronjobs/bidWinnerJobs')();
require('./cronjobs/mastodonScheduleJob').startMastodonScheduleJob();
const websocketRouter = require('./websockets/webSocketRouter');

const port = process.env.PORT || 4500;
Expand Down
Loading