feat(resources/server): move some functions to 'misc'

This commit is contained in:
Christopher committed 2021-06-18 22:05:06 +02:00
1 parent 4029e8c7a1
commit b033eae51e
7 files changed
+65 -21

No files matched your search

+128
View File
@@ -0,0 +1,128 @@
import axios, { AxiosResponse } from 'axios';
import { Tweet, Profile } from '../../../typings/twitter';
import { IMAGE_DELIMITER } from '../../../phone/src/apps/twitter/utils/images';
import { mainLogger } from '../sv_logger';
import { MarketplaceListing } from '../../../typings/marketplace';
const discordLogger = mainLogger.child({ module: 'discord' });
const DISCORD_WEBHOOK_ENV_VAR = 'NPWD_DISCORD_TOKEN';
const DISCORD_WEBHOOK = GetConvar(DISCORD_WEBHOOK_ENV_VAR, '');
/**
* https://discord.com/developers/docs/resources/channel#embed-object-embed-field-structure
*/
interface DiscordEmbedField {
name: string;
value: string;
inline?: boolean;
}
interface DiscordEmbed {
title: string;
color: number;
description: string;
timestamp: string;
fields: DiscordEmbedField[];
}
interface DiscordMessage {
username: string;
embeds: DiscordEmbed[];
}
const postToWebhook = async (content: DiscordMessage): Promise<AxiosResponse | void> => {
// If convar isnt set
if (!DISCORD_WEBHOOK) {
discordLogger.warn(
'Got a request to report a listing but discord is not configures. See README on how to configure discord endpoints.',
);
return;
}
const resp = await axios.post(DISCORD_WEBHOOK, { ...content });
// If we get a bad request throw error
if (resp.status >= 200 && resp.status < 300)
throw new Error(`Discord Error: ${resp.status} Error - ${resp.statusText}`);
};
const createDiscordMsgObj = (type: string, message: string, fields: DiscordEmbedField[]) => {
// Get ISO 8601 as its required by Discord API
const curTime = new Date().toISOString();
return {
username: 'NPWD Report',
embeds: [
{
title: `${type} REPORT`,
color: 0xe74c3c,
description: message,
timestamp: curTime,
fields,
},
],
};
};
export async function reportTweetToDiscord(tweet: Tweet, reportingProfile: Profile): Promise<any> {
// TODO: Add image report functionality
const fields = [
{
name: 'Reporting User',
value: `${tweet.profile_name} (${tweet.profile_id})`,
},
{
name: 'Reported User',
value: `${tweet.profile_name} (${tweet.profile_id})`,
},
{
name: 'Tweet',
value: tweet.message,
},
];
// If the tweet has images add it to the field array
if (tweet.images)
fields.concat({
name: 'Images',
value: tweet.images.split(IMAGE_DELIMITER).join('\n'),
});
const msgObj = createDiscordMsgObj('TWITTER', `Received a report for a tweet`, fields);
try {
await postToWebhook(msgObj);
} catch (e) {
discordLogger.error(e.message);
}
}
export async function reportListingToDiscord(
listing: MarketplaceListing,
reportingProfile: string,
): Promise<void> {
const embedObj = createDiscordMsgObj('MARKETPLACE', 'Received a report for a listing', [
{
name: 'Reported By',
value: reportingProfile,
inline: true,
},
{
name: 'Reported User',
value: `${listing.name} - (${listing.username})`,
inline: true,
},
{
name: 'Reported Listing Title',
value: listing.title,
},
{
name: 'Reported Listing Desc.',
value: listing.description,
},
]);
try {
await postToWebhook(embedObj);
} catch (e) {
discordLogger.error(e.message);
}
}
+37
View File
@@ -0,0 +1,37 @@
import { pool } from '../db';
import { config } from '../server';
import { mainLogger } from '../sv_logger';
import { getRandomPhoneNumber } from '../utils/getRandomPhoneNumber';
import PlayerService from '../players/player.service';
export async function generatePhoneNumber(identifier: string): Promise<string> {
const getQuery = `SELECT phone_number FROM ${config.database.playerTable} WHERE ${config.database.identifierColumn} = ?`;
const [results] = await pool.query(getQuery, [identifier]);
const result = <any[]>results;
const phoneNumber = result[0]?.phone_number;
// Generate a new phone number if there isn't one present
if (!phoneNumber)
if (!phoneNumber) {
let existingId;
let newNumber;
do {
newNumber = getRandomPhoneNumber();
try {
existingId = await PlayerService.getIdentifierByPhoneNumber(newNumber);
} catch (e) {
existingId = false;
}
} while (existingId);
mainLogger.debug(
`Generated a new number for identifier: ${identifier}, number: ${phoneNumber}`,
);
const query = `UPDATE ${config.database.playerTable} SET phone_number = ? WHERE ${config.database.identifierColumn} = ?`;
await pool.query(query, [newNumber, identifier]);
return newNumber;
}
return phoneNumber;
}