Skip to content

Commit

Permalink
adds admin conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotBraem committed Dec 17, 2024
1 parent 1921f18 commit 7a8130a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/config/admins.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const ADMIN_ACCOUNTS: string[] = [
// Add admin Twitter IDs here
// Example: "1234567890"
"elliot_braem"
// Add admin Twitter handles here (without @)
// Example: "TwitterDev"
];
36 changes: 32 additions & 4 deletions src/services/twitter/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export class TwitterService {
private checkInterval: NodeJS.Timeout | null = null;
private lastCheckedTweetId: string | null = null;
private cacheDir: string;
private adminIdCache: Map<string, string> = new Map();

constructor(config: TwitterConfig) {
this.client = new Scraper();
Expand Down Expand Up @@ -141,6 +142,22 @@ export class TwitterService {
}
}

private async initializeAdminIds() {
for (const handle of ADMIN_ACCOUNTS) {
try {
const userId = await this.client.getUserIdByScreenName(handle);
this.adminIdCache.set(handle, userId);
logger.info(`Cached admin ID for @${handle}: ${userId}`);
} catch (error) {
logger.error(`Failed to fetch ID for admin handle @${handle}:`, error);
}
}
}

private isAdmin(userId: string): boolean {
return Array.from(this.adminIdCache.values()).includes(userId);
}

async initialize() {
try {
// Ensure cache directory exists
Expand Down Expand Up @@ -183,6 +200,9 @@ export class TwitterService {
await new Promise((resolve) => setTimeout(resolve, 2000));
}

// Initialize admin IDs after successful login
await this.initializeAdminIds();

this.isInitialized = true;
logger.info('Successfully logged in to Twitter');
} catch (error) {
Expand Down Expand Up @@ -255,8 +275,10 @@ export class TwitterService {

try {
if (this.isSubmission(tweet)) {
logger.info("Received new submission.");
await this.handleSubmission(tweet);
} else if (this.isModeration(tweet)) {
logger.info("Received new moderation.");
await this.handleModeration(tweet);
}
} catch (error) {
Expand Down Expand Up @@ -299,7 +321,7 @@ export class TwitterService {
tweet.id,
"You've reached your daily submission limit. Please try again tomorrow."
);
logger.info(`User ${userId} had reached limit, replied to submission.`);
logger.info(`User ${userId} has reached limit, replied to submission.`);
return;
}

Expand All @@ -319,26 +341,32 @@ export class TwitterService {
tweet.id,
"Successfully submitted to publicgoods.news!"
);
logger.info(`Received submission, replied to User: ${userId}.`)
logger.info(`Successfully submitted. Replied to User: ${userId}.`)
}

private async handleModeration(tweet: Tweet): Promise<void> {
const userId = tweet.userId;
if (!userId || !tweet.id) return;

// Verify admin status
if (!ADMIN_ACCOUNTS.includes(userId)) {
logger.info(`Handling moderation for ${JSON.stringify(tweet)}`);

// Verify admin status using cached ID
if (!this.isAdmin(userId)) {
logger.info(`User ${userId} is not admin.`)
return; // Silently ignore non-admin moderation attempts
}

// Get the original submission tweet this is in response to
const inReplyToId = tweet.inReplyToStatusId;
logger.info(`It was a reply to ${tweet.inReplyToStatusId}`);
if (!inReplyToId) return;

const submission = this.submissions.get(inReplyToId);
logger.info(`Got the original submission: ${JSON.stringify(submission)}`);
if (!submission) return;

const action = this.getModerationAction(tweet);
logger.info(`Determined the action: ${action}`);
if (!action) return;

// Check if this admin has already moderated this submission
Expand Down

0 comments on commit 7a8130a

Please sign in to comment.