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

+19
View File
@@ -65,14 +65,33 @@ CREATE TABLE IF NOT EXISTS npwd_notes (
CREATE TABLE IF NOT EXISTS npwd_sellout_listings (
id int(11) NOT NULL AUTO_INCREMENT,
identifier varchar(255) DEFAULT NULL,
username varchar(255) DEFAULT NULL,
name varchar(50) DEFAULT NULL,
number varchar(255) NOT NULL,
title varchar(255) DEFAULT NULL,
url varchar(255) DEFAULT NULL,
description varchar(255) NOT NULL,
createdAt timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
updatedAt timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id)
)
ENGINE = INNODB,
AUTO_INCREMENT = 31,
AVG_ROW_LENGTH = 2048,
CHARACTER SET utf8mb4,
COLLATE utf8mb4_general_ci;
);
CREATE TABLE IF NOT EXISTS npwd_marketplace_reports (
id int(11) NOT NULL AUTO_INCREMENT,
listing_id int(11) DEFAULT NULL,
profile varchar(255) DEFAULT NULL,
PRIMARY KEY (id)
)
ENGINE = INNODB,
CHARACTER SET utf8mb4,
COLLATE utf8mb4_general_ci;
CREATE TABLE IF NOT EXISTS `npwd_twitter_reports` (
`id` int NOT NULL AUTO_INCREMENT,
`profile_id` int NOT NULL,
@@ -34,7 +34,7 @@ InjectDebugData([
{
id: 1,
name: 'Some guy',
number: '123-4567',
number: '111-1134',
title: 'Car',
description: 'Selling this cool car',
url: 'https://i.imgur.com/ROmGTwi.jpeg',
@@ -0,0 +1,84 @@
import { Box, Tooltip, Button } from '@material-ui/core';
import { makeStyles, Theme } from '@material-ui/core/styles';
import React from 'react';
import { MarketplaceListing } from '../../../../common/typings/marketplace';
import { useSimcard } from '../../../../os/simcard/hooks/useSimcard';
import DeleteIcon from '@material-ui/icons/Delete';
import ReportIcon from '@material-ui/icons/Report';
import ChatIcon from '@material-ui/icons/Chat';
import PhoneIcon from '@material-ui/icons/Phone';
import Nui from '../../../../os/nui-events/utils/Nui';
import { useTranslation } from 'react-i18next';
import { useHistory } from 'react-router';
const useStyles = makeStyles((theme: Theme) => ({
icon: {
color: theme.palette.primary.main,
},
}));
export const ListingActions = (listing: MarketplaceListing) => {
const classes = useStyles();
const { number: myNumber } = useSimcard();
const { t } = useTranslation();
const history = useHistory();
const handleDeleteListing = () => {
Nui.send('phone:marketplaceDeleteListing', {
id: listing.id,
});
};
const handleReportListing = () => {
Nui.send('phone:reportListing', {
listing,
});
};
const handleCall = () => {
Nui.send('phone:beginCall', {
number: listing.number,
});
};
const handleMessage = () => {
history.push(`/messages/new/${listing.number}`);
};
return (
<Box flex justifyContent="space-between" alignItems="center">
<div style={{ float: 'left' }}>
{listing.number !== myNumber ? (
<>
<Tooltip title={t('GENERIC_MESSAGE')}>
<Button onClick={handleMessage}>
<ChatIcon className={classes.icon} />
</Button>
</Tooltip>
<Tooltip title={`${t('GENERIC_CALL')}: ${listing.number}`}>
<Button onClick={handleCall}>
<PhoneIcon className={classes.icon} />
</Button>
</Tooltip>
</>
) : null}
</div>
<div style={{ float: 'right' }}>
{listing.number === myNumber ? (
<Tooltip title={t('GENERIC_DELETE')}>
<Button onClick={handleDeleteListing}>
<DeleteIcon />
</Button>
</Tooltip>
) : (
<Tooltip title={t('GENERIC_REPORT')}>
<Button onClick={handleReportListing}>
<ReportIcon />
</Button>
</Tooltip>
)}
</div>
</Box>
);
};
@@ -1,10 +1,10 @@
import React from 'react';
import { makeStyles } from '@material-ui/core/styles';
import { Button, Typography, Paper } from '@material-ui/core';
import { Typography, Paper } from '@material-ui/core';
import { ListItem } from '../../../../ui/components/ListItem';
import ChatIcon from '@material-ui/icons/Chat';
import PhoneIcon from '@material-ui/icons/Phone';
import { PictureResponsive } from '../../../../ui/components/PictureResponsive';
import { MarketplaceListing } from '../../../../common/typings/marketplace';
import { ListingActions } from './ListingActions';
const useStyles = makeStyles((theme) => ({
root: {
@@ -30,42 +30,26 @@ const useStyles = makeStyles((theme) => ({
background: theme.palette.background.default,
marginBottom: 20,
},
icon: {
color: theme.palette.primary.main,
},
header: {
margin: 10,
},
headerTypo: {
margin: 5,
},
desc: {
padding: 10,
},
}));
export const SelloutItem = (listing) => {
export const SelloutItem = (listing: MarketplaceListing) => {
const classes = useStyles();
return (
<ListItem className={classes.root}>
<div className={classes.content}>
<Paper elevation={2} className={classes.paper}>
<div className={classes.header}>
<Typography className={classes.headerTypo} variant="h5">
<div style={{ margin: 10 }}>
<Typography style={{ margin: 5 }} variant="h5">
{listing.name}
</Typography>
<Typography
className={classes.headerTypo}
style={{ borderBottom: '1px solid #f44336' }}
>
{listing.number}
</Typography>
<Typography variant="h5" style={{ padding: 5 }}>
{listing.title}
</Typography>
</div>
{listing.url ? (
<PictureResponsive src={listing.url} alt={`${listing.name} image`} />
<PictureResponsive src={listing.url} alt={`${listing.name}`} />
) : (
<Typography style={{ margin: 10 }}>
No image provided{' '}
@@ -74,17 +58,11 @@ export const SelloutItem = (listing) => {
</span>
</Typography>
)}
<Typography variant="h6" className={classes.desc}>
<Typography variant="h6" style={{ padding: 10 }}>
{listing.description}
</Typography>
<div>
<Button>
<ChatIcon className={classes.icon} />
</Button>
<Button>
<PhoneIcon className={classes.icon} />
</Button>
</div>
<ListingActions {...listing} />
</Paper>
</div>
</ListItem>
@@ -1,8 +1,11 @@
import React from 'react';
import { List } from '../../../../ui/components/List';
import { useListing } from '../../hooks/useListing';
import { SelloutItem } from './SelloutItem';
export const SelloutList = ({ listings }) => {
export const SelloutList = () => {
const listings = useListing();
return (
<List>
{listings.map((listing) => (
@@ -1,8 +1,6 @@
import React from 'react';
import { useListing } from '../../hooks/useListing';
import { SelloutList } from './SelloutList';
export const SelloutListContainer = () => {
const { listing } = useListing();
return <SelloutList listings={listing} />;
return <SelloutList />;
};
+2 -2
View File
@@ -1,7 +1,7 @@
import { useRecoilValue } from 'recoil';
import { MarketplaceListing } from '../../../common/typings/marketplace';
import { selloutState } from './state';
export const useListing = () => {
const listing = useRecoilValue(selloutState.listing);
return { listing };
return useRecoilValue<MarketplaceListing[]>(selloutState.listing);
};
+1
View File
@@ -1,6 +1,7 @@
export interface MarketplaceListing {
id: number;
identifier?: string;
username: string;
name: string;
number: string;
title: string;
+7
View File
@@ -123,11 +123,18 @@
"APPS_MARKETPLACE_CREATE_LISTING_SUCCESS": "Successfully created a new listing",
"APPS_MARKETPALCE_CREATE_LISTING_FAILED": "Failed to create a new listing",
"APPS_MARKETPLACE_DELETE_LISTING_SUCCESS": "Successfully delete listing",
"APPS_MARKETPLACE_DELETE_LISTING_FAILED": "Failed to delete listing",
"APPS_MARKETPLACE_REPORT_LISTING_FAILED": "This listing has already been reported",
"APPS_MARKETPLACE_REPORT_LISTING_SUCCESS": "Successfully reported listing",
"GENERIC_WAIT": "Please wait...",
"GENERIC_SAVE": "Save",
"GENERIC_UPDATE": "Update",
"GENERIC_DELETE": "Delete",
"GENERIC_REPORT": "Report",
"GENERIC_CALL": "Call",
"GENERIC_MESSAGE": "Message",
"GENERIC_CLOSE": "Close",
"GENERIC_CANCEL": "Cancel",
"GENERIC_TITLE": "Title",
+17 -3
View File
@@ -13,17 +13,31 @@ onNet(events.SELLOUT_SEND_LISTING, (listing: MarketplaceListing) => {
});
RegisterNuiCallbackType(events.SELLOUT_ADD_LISTING);
on(`__cfx_nui:${events.SELLOUT_ADD_LISTING}`, (data: MarketplaceListing) => {
on(`__cfx_nui:${events.SELLOUT_ADD_LISTING}`, (data: MarketplaceListing, cb: Function) => {
emitNet(events.SELLOUT_ADD_LISTING, data);
cb();
});
// this will be triggerd when
// the adding of a new listing is successfully
RegisterNuiCallbackType(events.SELLOUT_DELETE_LISTING);
on(`__cfx_nui:${events.SELLOUT_DELETE_LISTING}`, (data: MarketplaceListing, cb: Function) => {
emitNet(events.SELLOUT_DELETE_LISTING, data.id);
cb();
});
RegisterNuiCallbackType(events.SELLOUT_REPORT_LISTING);
on(`__cfx_nui:${events.SELLOUT_REPORT_LISTING}`, (data: MarketplaceListing, cb: Function) => {
emitNet(events.SELLOUT_SEND_LISTING, data);
cb();
});
onNet(events.SELLOUT_ADD_LISTING_SUCCESS, () => {
emitNet(events.SELLOUT_FETCH_LISTING);
});
onNet(events.SELLOUT_DELETE_LISTING_SUCCESS, () => {
emitNet(events.SELLOUT_FETCH_LISTING);
});
onNet(events.SELLOUT_ACTION_RESULT, (alert: IAlertProps) => {
SendNuiMessage(
JSON.stringify({
+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,
});
}
});
+6
View File
@@ -44,11 +44,17 @@ export default {
CONTACTS_ACTION_RESULT: 'phone:contactActionResult',
// Sellout
// these event keys needs to be changed to 'MARKETPLACE'
SELLOUT_ADD_LISTING: 'phone:addListing',
SELLOUT_ADD_LISTING_SUCCESS: 'phone:addListingSuccess',
SELLOUT_FETCH_LISTING: 'phone:fetchAllListings',
SELLOUT_SEND_LISTING: 'phone:sendAllListings',
SELLOUT_ACTION_RESULT: 'phone:selloutActionResult',
SELLOUT_DELETE_LISTING: 'phone:marketplaceDeleteListing',
SELLOUT_DELETE_LISTING_SUCCESS: 'phone:marketplaceDeleteListingSuccess',
SELLOUT_REPORT_LISTING: 'phone:reportListing',
SELLOUT_REPORT_LISTING_SUCCESS: 'phone:reportListingSuccess',
SELLOUT_REPORT_LISTING_SUCESS: 'phone:reportListingFailed',
//
BANK_ADD_TRANSFER: 'phone:addTransfer',