Tweak/marketplace additions (#137)

Checks for being the OP of the listing. Calling, Message, Delete and Report. 

Updates to Locale is needed.
This commit is contained in:
Christopher Gjelten authored and GitHub committed 2021-03-07 16:30:58 +01:00
1 parent 0203ebbe51
commit 6b6080c4b2
13 files changed
+277 -47

No files matched your search

+26
View File
@@ -3,6 +3,7 @@ import axios from 'axios';
import { Tweet, Profile } from '../../phone/src/common/typings/twitter';
import { IMAGE_DELIMITER } from '../../phone/src/apps/twitter/utils/images';
import { mainLogger } from './sv_logger';
import { MarketplaceListing } from '../../phone/src/common/typings/marketplace';
const discordLogger = mainLogger.child({ module: 'discord' });
@@ -37,3 +38,28 @@ export async function reportTweetToDiscord(tweet: Tweet, reportingProfile: Profi
return await axios.post(POST_CHANNEL_URL, { content }, { headers: DISCORD_HEADERS });
}
export async function reportListingToDiscord(
listing: MarketplaceListing,
reportingProfile: string,
): Promise<any> {
if (!DISCORD_TOKEN || !DISCORD_CHANNEL_ID) {
discordLogger.warn(
'Got a request to report a listing but discord is not configures. See README o how to configure discord endpoints.',
);
return;
}
const content = `**REPORTED LISTING**
-------------------------------------------------
**Reporting User**: ${reportingProfile}
**Reported User** ${listing.name} (${listing.username})
-------------------------------------------------
**Reported Listing**
${listing.title}
----
${listing.description}
`;
return await axios.post(POST_CHANNEL_URL, { content }, { headers: DISCORD_HEADERS });
}
+97 -3
View File
@@ -1,10 +1,11 @@
import events from '../utils/events';
import { ESX } from './server';
import { getSource } from './functions';
import { getIdentifier, getSource } from './functions';
import { pool } from './db';
import { usePhoneNumber } from './functions';
import { mainLogger } from './sv_logger';
import { MarketplaceListing } from '../../phone/src/common/typings/marketplace';
import { reportListingToDiscord } from './discord';
const selloutLogger = mainLogger.child({ module: 'sellout' });
@@ -19,14 +20,16 @@ async function fetchAllListings(): Promise<MarketplaceListing[]> {
async function addListing(
identifier: string,
username: string,
name: string,
number: any,
listing: MarketplaceListing,
): Promise<void> {
const query =
'INSERT INTO npwd_sellout_listings (identifier, name, number, title, url, description) VALUES (?, ?, ?, ?, ?, ?)';
'INSERT INTO npwd_sellout_listings (identifier, username, name, number, title, url, description) VALUES (?, ?, ?, ?, ?, ?, ?)';
await pool.query(query, [
identifier,
username,
name,
number,
listing.title,
@@ -35,6 +38,35 @@ async function addListing(
]);
}
async function deleteListing(listingId: number, identifier: string): Promise<void> {
const query = 'DELETE FROM npwd_sellout_listings WHERE id = ? AND identifier = ?';
await pool.query(query, [listingId, identifier]);
}
async function getListing(listingId: number): Promise<MarketplaceListing> {
const query = `SELECT * FROM npwd_sellout_listings WHERE id = ?`;
const [results] = await pool.query(query, [listingId]);
const listings = <MarketplaceListing[]>results;
const listing = listings[0];
return listing;
}
async function reportListing(listingId: number, profile: string): Promise<void> {
const query = `INSERT INTO npwd_marketplace_reports (listing_id, profile) VALUES (?, ?)`;
await pool.query(query, [listingId, profile]);
}
async function doesReportExist(listingId: number, profile: string): Promise<boolean> {
const query = `SELECT * FROM npwd_marketplace_reports WHERE listing_id = ? AND profile = ?`;
const results = await pool.query(query, [listingId, profile]);
const result = <any[]>results;
return result.length > 0;
}
onNet(events.SELLOUT_FETCH_LISTING, async () => {
const _source = getSource();
try {
@@ -54,8 +86,11 @@ onNet(events.SELLOUT_ADD_LISTING, async (listing: MarketplaceListing) => {
const _identifier = xPlayer.getIdentifier();
const name = xPlayer.getName();
// This is used as username for the reports
const playerName = GetPlayerName(_source);
const phoneNumber = await usePhoneNumber(_identifier);
await addListing(_identifier, name, phoneNumber, listing);
await addListing(_identifier, playerName, name, phoneNumber, listing);
emitNet(events.SELLOUT_ADD_LISTING_SUCCESS, _source);
emitNet(events.SELLOUT_ACTION_RESULT, _source, {
@@ -73,3 +108,62 @@ onNet(events.SELLOUT_ADD_LISTING, async (listing: MarketplaceListing) => {
});
}
});
onNet(events.SELLOUT_DELETE_LISTING, async (listingId: number) => {
const pSource = getSource();
try {
const identifier = getIdentifier(pSource);
await deleteListing(listingId, identifier);
// fetches the listings again
emitNet(events.SELLOUT_DELETE_LISTING_SUCCESS, pSource);
emitNet(events.SELLOUT_ACTION_RESULT, pSource, {
message: 'MARKETPLACE_DELETE_LISTING_SUCCESS',
type: 'success',
});
} catch (e) {
selloutLogger.error(`Failed to delete listing ${e.message}`, {
source: pSource,
});
emitNet(events.SELLOUT_ACTION_RESULT, pSource, {
message: 'MARKETPLACE_DELETE_LISTING_FAILED',
type: 'error',
});
}
});
onNet(events.SELLOUT_REPORT_LISTING, async (listing: MarketplaceListing) => {
const pSource = getSource();
try {
const rListing = await getListing(listing.id);
const reportExists = await doesReportExist(listing.id, listing.name);
// gets the player name (steam) of the player that is reporting
const reportingPlayer = GetPlayerName(pSource);
if (reportExists) {
// send an info alert
selloutLogger.error(`This listing has already been reported`);
emitNet(events.SELLOUT_ACTION_RESULT, pSource, {
message: 'MARKETPLACE_REPORT_LISTING_FAILED',
type: 'info',
});
} else {
await reportListing(rListing.id, rListing.name);
await reportListingToDiscord(rListing, reportingPlayer);
emitNet(events.SELLOUT_ACTION_RESULT, pSource, {
message: 'MARKETPLACE_REPORT_LISTING_SUCCESS',
type: 'success',
});
}
} catch (e) {
selloutLogger.error(`Failed to report listing ${e.message}`, {
source: pSource,
});
}
});