From 6b6080c4b28a5f3482b8e5a533496e161a768fcc Mon Sep 17 00:00:00 2001 From: Christopher Gjelten Date: Sun, 7 Mar 2021 16:30:58 +0100 Subject: [PATCH] Tweak/marketplace additions (#137) Checks for being the OP of the listing. Calling, Message, Delete and Report. Updates to Locale is needed. --- import.sql | 19 ++++ .../apps/sellout/components/SelloutApp.tsx | 2 +- .../components/SelloutList/ListingActions.tsx | 84 +++++++++++++++ .../components/SelloutList/SelloutItem.tsx | 46 +++----- .../components/SelloutList/SelloutList.tsx | 5 +- .../SelloutList/SelloutListContainer.tsx | 4 +- phone/src/apps/sellout/hooks/useListing.ts | 4 +- phone/src/common/typings/marketplace.ts | 1 + phone/src/locale/en.json | 7 ++ resources/client/cl_sellout.ts | 20 +++- resources/server/discord.ts | 26 +++++ resources/server/sv_sellout.ts | 100 +++++++++++++++++- resources/utils/events.ts | 6 ++ 13 files changed, 277 insertions(+), 47 deletions(-) create mode 100644 phone/src/apps/sellout/components/SelloutList/ListingActions.tsx diff --git a/import.sql b/import.sql index 5728288..be4255b 100644 --- a/import.sql +++ b/import.sql @@ -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, diff --git a/phone/src/apps/sellout/components/SelloutApp.tsx b/phone/src/apps/sellout/components/SelloutApp.tsx index d6590b7..5d36bd0 100644 --- a/phone/src/apps/sellout/components/SelloutApp.tsx +++ b/phone/src/apps/sellout/components/SelloutApp.tsx @@ -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', diff --git a/phone/src/apps/sellout/components/SelloutList/ListingActions.tsx b/phone/src/apps/sellout/components/SelloutList/ListingActions.tsx new file mode 100644 index 0000000..e518aef --- /dev/null +++ b/phone/src/apps/sellout/components/SelloutList/ListingActions.tsx @@ -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 ( + +
+ {listing.number !== myNumber ? ( + <> + + + + + + + + ) : null} +
+ +
+ {listing.number === myNumber ? ( + + + + ) : ( + + + + )} +
+
+ ); +}; diff --git a/phone/src/apps/sellout/components/SelloutList/SelloutItem.tsx b/phone/src/apps/sellout/components/SelloutList/SelloutItem.tsx index eedb6a1..059c0f6 100644 --- a/phone/src/apps/sellout/components/SelloutList/SelloutItem.tsx +++ b/phone/src/apps/sellout/components/SelloutList/SelloutItem.tsx @@ -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 (
-
- +
+ {listing.name} - - {listing.number} - {listing.title}
+ {listing.url ? ( - + ) : ( No image provided{' '} @@ -74,17 +58,11 @@ export const SelloutItem = (listing) => { )} - + + {listing.description} -
- - -
+
diff --git a/phone/src/apps/sellout/components/SelloutList/SelloutList.tsx b/phone/src/apps/sellout/components/SelloutList/SelloutList.tsx index 2f68711..3fc753c 100644 --- a/phone/src/apps/sellout/components/SelloutList/SelloutList.tsx +++ b/phone/src/apps/sellout/components/SelloutList/SelloutList.tsx @@ -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 ( {listings.map((listing) => ( diff --git a/phone/src/apps/sellout/components/SelloutList/SelloutListContainer.tsx b/phone/src/apps/sellout/components/SelloutList/SelloutListContainer.tsx index 570b3c0..eccda41 100644 --- a/phone/src/apps/sellout/components/SelloutList/SelloutListContainer.tsx +++ b/phone/src/apps/sellout/components/SelloutList/SelloutListContainer.tsx @@ -1,8 +1,6 @@ import React from 'react'; -import { useListing } from '../../hooks/useListing'; import { SelloutList } from './SelloutList'; export const SelloutListContainer = () => { - const { listing } = useListing(); - return ; + return ; }; diff --git a/phone/src/apps/sellout/hooks/useListing.ts b/phone/src/apps/sellout/hooks/useListing.ts index bed1de6..97448dd 100644 --- a/phone/src/apps/sellout/hooks/useListing.ts +++ b/phone/src/apps/sellout/hooks/useListing.ts @@ -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(selloutState.listing); }; diff --git a/phone/src/common/typings/marketplace.ts b/phone/src/common/typings/marketplace.ts index 385a160..46c9c4e 100644 --- a/phone/src/common/typings/marketplace.ts +++ b/phone/src/common/typings/marketplace.ts @@ -1,6 +1,7 @@ export interface MarketplaceListing { id: number; identifier?: string; + username: string; name: string; number: string; title: string; diff --git a/phone/src/locale/en.json b/phone/src/locale/en.json index 72e30e6..0c29dea 100644 --- a/phone/src/locale/en.json +++ b/phone/src/locale/en.json @@ -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", diff --git a/resources/client/cl_sellout.ts b/resources/client/cl_sellout.ts index d62de49..dacd83e 100644 --- a/resources/client/cl_sellout.ts +++ b/resources/client/cl_sellout.ts @@ -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({ diff --git a/resources/server/discord.ts b/resources/server/discord.ts index 929d237..d5abab7 100644 --- a/resources/server/discord.ts +++ b/resources/server/discord.ts @@ -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 { + 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 }); +} diff --git a/resources/server/sv_sellout.ts b/resources/server/sv_sellout.ts index 72d300a..6cdc69e 100644 --- a/resources/server/sv_sellout.ts +++ b/resources/server/sv_sellout.ts @@ -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 { async function addListing( identifier: string, + username: string, name: string, number: any, listing: MarketplaceListing, ): Promise { 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 { + const query = 'DELETE FROM npwd_sellout_listings WHERE id = ? AND identifier = ?'; + + await pool.query(query, [listingId, identifier]); +} + +async function getListing(listingId: number): Promise { + const query = `SELECT * FROM npwd_sellout_listings WHERE id = ?`; + const [results] = await pool.query(query, [listingId]); + const listings = results; + const listing = listings[0]; + + return listing; +} + +async function reportListing(listingId: number, profile: string): Promise { + 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 { + const query = `SELECT * FROM npwd_marketplace_reports WHERE listing_id = ? AND profile = ?`; + const results = await pool.query(query, [listingId, profile]); + const result = 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, + }); + } +}); diff --git a/resources/utils/events.ts b/resources/utils/events.ts index aa46fd1..1ced4af 100644 --- a/resources/utils/events.ts +++ b/resources/utils/events.ts @@ -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',