From c0dbf1a765719bb47b2d59665a43d4f98ceff9ad Mon Sep 17 00:00:00 2001 From: erik-sn Date: Sat, 24 Oct 2020 11:05:57 -0400 Subject: [PATCH] working tweet deletion --- README.md | 5 +- common/interfaces/config.ts | 1 + config.json | 1 + .../twitter/components/tweet/ShowMore.tsx | 85 +++++++++++++++++++ .../apps/twitter/components/tweet/Tweet.tsx | 19 +++-- phone/src/locale/en.json | 2 + phone/src/os/interfaces/serverConfig.ts | 1 + resources/client/cl_twitter.ts | 17 ++++ resources/html/index.html | 45 +++++++++- resources/server/sv_twitter.ts | 35 +++++++- resources/utils/events.ts | 3 + 11 files changed, 200 insertions(+), 14 deletions(-) create mode 100644 phone/src/apps/twitter/components/tweet/ShowMore.tsx diff --git a/README.md b/README.md index 09706cc..302cdf4 100644 --- a/README.md +++ b/README.md @@ -19,11 +19,12 @@ Below is the list of configuration options: | RunRate | 10 | interval in seconds to check phone destruction | | DestoryChance | 100 | Percent chance that phone destruction will occur | | DestroyPhoneReCheck | 3 | Time in minutes before another phone destruction attempt will happen | -| notificationPosition.horizontal | right | Horizontal position of phone notifications. `left`, `center` or `right` | -| notificationPosition.vertical | bottom | Vertical positon of phone notifications. `top` or `bottom` | +| notificationPosition.horizontal | right | Horizontal position of phone notifications. `left`, `center` or `right` | +| notificationPosition.vertical | bottom | Vertical positon of phone notifications. `top` or `bottom` | | twitter.showNotifications | true | Whether or not to show twitter notifications | | twitter.generateProfileNameFromUsers | true | Whether or not the server should automtically generate a player's profile name from their user in the database | | twitter.allowEdtiableProfileName | true | Whether or not players can edit their profile name | +| twitter.allowDeleteTweets | true | Whether or not players can delete their own tweets | | twitter.characterLimit | 240 | Limit on how many characters tweets can contain | | twitter.enableAvatars | true | Whether or not player's can select avatars for their twitter profile | | twitter.enableEmojis | true | Whether or not player's can use emojis in tweets | diff --git a/common/interfaces/config.ts b/common/interfaces/config.ts index 22e46b3..0d2f9e1 100644 --- a/common/interfaces/config.ts +++ b/common/interfaces/config.ts @@ -2,6 +2,7 @@ interface TwitterConfig { showNotifications: boolean; generateProfileNameFromUsers: boolean; allowEdtiableProfileName: boolean; + allowDeleteTweets: boolean; characterLimit: number; enableAvatars: boolean; enableEmojis: boolean; diff --git a/config.json b/config.json index 439e0ef..8160add 100644 --- a/config.json +++ b/config.json @@ -15,6 +15,7 @@ "showNotifications": true, "generateProfileNameFromUsers": true, "allowEdtiableProfileName": true, + "allowDeleteTweets": true, "characterLimit": 240, "enableAvatars": true, "enableEmojis": true, diff --git a/phone/src/apps/twitter/components/tweet/ShowMore.tsx b/phone/src/apps/twitter/components/tweet/ShowMore.tsx new file mode 100644 index 0000000..6533011 --- /dev/null +++ b/phone/src/apps/twitter/components/tweet/ShowMore.tsx @@ -0,0 +1,85 @@ +import React, { useState } from "react"; +import { useTranslation } from "react-i18next"; +import { + Button, + Menu, + MenuItem +} from "@material-ui/core"; +import MoreIcon from "@material-ui/icons/MoreVert"; +import { usePhone } from "../../../../os/phone/hooks/usePhone"; +import Nui from "../../../../os/nui-events/utils/Nui"; + + +/** + * Ok this is some bullshit. Due to either scaling issues or the NUI + * overlay being different than the display resolution just letting + * material-ui determine what the anchor positioning of the popover + * is not reliable and has some error that appears to scale vertically. + * + * This vertical offset raises the popover positioning implementing the + * transformOrigin prop that material-ui's Popover element provides. + * + * These coefficients are NOT based on any known offsets or calc() + * functions. They are empiricallly determined. The actual scaling issue + * does NOT appear to be linear, but over the comparatively low height + * of the phone compared to the window it produces results that are within + * acceptable tolerances from the player's perspective. + * @param top - the computed distance from the top of the window to the top + * of the anchor element + */ +function calculateVerticalOffset(top: number): number { + return top * 0.20 - 38; +} + +export const ShowMore = ({ id, isMine }) => { + const [anchorEl, setAnchorEl] = useState(null); + const [verticalOffset, setVerticalOffset] = useState(0); + const { t } = useTranslation(); + const { config } = usePhone(); + + if (!config) return null; + + const handleClick = (event: React.MouseEvent) => { + const element = event.currentTarget + setAnchorEl(element); + + const top = window.pageYOffset + element.getBoundingClientRect().top + setVerticalOffset(calculateVerticalOffset(top)); + }; + + const handleClose = () => { + setAnchorEl(null); + setVerticalOffset(0); + }; + + const handleDeleteTweet = () => { + Nui.send("phone:deleteTweet", id); + handleClose(); + } + + const allowedToDelete = config.twitter.allowDeleteTweets && isMine; + + return ( + <> + + + {allowedToDelete && {t('APPS_TWITTER_DELETE')}} + {t('APPS_TWITTER_REPORT')} + + + ); +}; + +export default ShowMore; diff --git a/phone/src/apps/twitter/components/tweet/Tweet.tsx b/phone/src/apps/twitter/components/tweet/Tweet.tsx index 8d91505..6db0b48 100644 --- a/phone/src/apps/twitter/components/tweet/Tweet.tsx +++ b/phone/src/apps/twitter/components/tweet/Tweet.tsx @@ -1,22 +1,27 @@ -import React from "react"; +import React, { useState, memo } from "react"; import { useTranslation } from "react-i18next"; import { makeStyles } from "@material-ui/core/styles"; import { ListItemAvatar, Avatar as MuiAvatar, Button, + List, + ListItem, + ListItemText, + Paper, Typography, } from "@material-ui/core"; import MoreIcon from "@material-ui/icons/MoreVert"; -import { ListItem } from "../../../../ui/components/ListItem"; import { secondsToHumanReadable } from "../../utils/time"; import LikeButton from "../buttons/LikeButton"; import ReplyButton from "../buttons/ReplyButton"; import ImageDisplay from "../images/ImageDisplay"; import Avatar from "../Avatar"; +import ShowMore from './ShowMore'; import { QuoteButton } from "../buttons/QuoteButton"; import { usePhone } from "../../../../os/phone/hooks/usePhone"; +import Nui from "../../../../os/nui-events/utils/Nui"; const useStyles = makeStyles(() => ({ root: { @@ -58,7 +63,7 @@ const useStyles = makeStyles(() => ({ width: "100%", marginLeft: "-20px", // easier to do this here than override the MUI styles marginTop: "3px", - }, + } })); export const Tweet = (tweet) => { @@ -70,6 +75,7 @@ export const Tweet = (tweet) => { profile_name, seconds_since_tweet, isLiked, + isMine, } = tweet; const classes = useStyles(); const { t } = useTranslation(); @@ -81,7 +87,6 @@ export const Tweet = (tweet) => { // this is a workaround to transfer string new lines and returns that // are stored into the database into html for the UI to render const formattedMessage = message.replace(/\n\r?/g, "
"); - const profileName = profile_name ? `@${profile_name}` : ""; return ( @@ -114,13 +119,11 @@ export const Tweet = (tweet) => { - + ); }; -export default Tweet; +export default memo(Tweet); diff --git a/phone/src/locale/en.json b/phone/src/locale/en.json index dd19257..fcb0a23 100644 --- a/phone/src/locale/en.json +++ b/phone/src/locale/en.json @@ -20,6 +20,8 @@ "APPS_TWITTER_SUBMIT_IMAGE": "Add Media", "APPS_TWITTER_CLOSE_IMAGE": "Close", "APPS_TWITTER_CLOSE": "Close", + "APPS_TWITTER_DELETE": "Delete", + "APPS_TWITTER_REPORT": "Report", "APPS_TWITTER_JUST_NOW": "Just now", "APPS_TWITTER_SEARCH_TWEETS": "Search for tweets", "APPS_TWITTER_SEARCH_TWEETS_PLACEHOLDER": "Search by profile name or message", diff --git a/phone/src/os/interfaces/serverConfig.ts b/phone/src/os/interfaces/serverConfig.ts index 6c2bf27..3a48bea 100644 --- a/phone/src/os/interfaces/serverConfig.ts +++ b/phone/src/os/interfaces/serverConfig.ts @@ -3,6 +3,7 @@ interface TwitterConfig { generateProfileNameFromUsers: boolean; characterLimit: number; allowEdtiableProfileName: boolean; + allowDeleteTweets: boolean; enableAvatars: boolean; enableEmojis: boolean; enableImages: boolean; diff --git a/resources/client/cl_twitter.ts b/resources/client/cl_twitter.ts index 5276c3e..7d96378 100644 --- a/resources/client/cl_twitter.ts +++ b/resources/client/cl_twitter.ts @@ -88,6 +88,23 @@ onNet(events.TWITTER_CREATE_TWEET_BROADCAST, (tweet: any) => { sendTwitterMessage(events.TWITTER_CREATE_TWEET_BROADCAST, tweet); }); +/** + * Twitter delete tweet + */ +RegisterNuiCallbackType(events.TWITTER_DELETE_TWEET); +on(`__cfx_nui:${events.TWITTER_DELETE_TWEET}`, (tweetId: number) => { + emitNet(events.TWITTER_DELETE_TWEET, tweetId); +}); + +onNet(events.TWITTER_DELETE_TWEET_SUCCESS, () => { + sendTwitterMessage(events.TWITTER_DELETE_TWEET_SUCCESS); + emitNet(events.TWITTER_FETCH_TWEETS); +}); + +onNet(events.TWITTER_DELETE_TWEET_FAILURE, () => { + sendTwitterMessage(events.TWITTER_DELETE_TWEET_FAILURE); +}); + /** * Twitter likes */ diff --git a/resources/html/index.html b/resources/html/index.html index 9c511ba..d196fe9 100644 --- a/resources/html/index.html +++ b/resources/html/index.html @@ -1 +1,44 @@ -React App
\ No newline at end of file + + + + + + + + + + + + + + React App + + + +
+ + + diff --git a/resources/server/sv_twitter.ts b/resources/server/sv_twitter.ts index 6b92899..f496289 100644 --- a/resources/server/sv_twitter.ts +++ b/resources/server/sv_twitter.ts @@ -21,6 +21,7 @@ interface ProfileName { interface Tweet { id: number; + profile_id?: number; profile_name?: string; avatar_url?: string; message: string; @@ -37,6 +38,7 @@ interface Tweet { async function fetchAllTweets(profileId: number): Promise { const query = ` SELECT + npwd_twitter_profiles.id AS profile_id, npwd_twitter_profiles.profile_name, npwd_twitter_profiles.avatar_url, npwd_twitter_tweets.*, @@ -51,7 +53,7 @@ async function fetchAllTweets(profileId: number): Promise { `; const [results] = await pool.query(query, [profileId]); const tweets = results; - return tweets; + return tweets.map(tweet => ({ ...tweet, isMine: tweet.profile_id === profileId})); } /** @@ -67,6 +69,7 @@ async function fetchTweetsFiltered( const parameterizedSearchValue = `%${searchValue}%`; const query = ` SELECT + npwd_twitter_profiles.id AS profile_id, npwd_twitter_profiles.profile_name, npwd_twitter_profiles.avatar_url, npwd_twitter_tweets.*, @@ -85,7 +88,7 @@ async function fetchTweetsFiltered( parameterizedSearchValue, ]); const tweets = results; - return tweets; + return tweets.map(tweet => ({ ...tweet, isMine: tweet.profile_id === profileId})); } /** @@ -153,7 +156,7 @@ async function generateProfileName(identifier: string): Promise { const profileNames = results; if (profileNames.length === 0) return defaultProfileName; - return profileNames[0].profile_name; + return profileNames[0].profile_name || defaultProfileName; } /** @@ -224,6 +227,21 @@ async function deleteLike(profileId: number, tweetId: number): Promise { await pool.execute(query, [profileId, tweetId]); } +/** + * Delete a tweet from the database + * @param identifier - identifier of the source user + * @param tweetId - primary key of the tweet to remove the like from + */ +async function deleteTweet(identifier: string, tweetId: number): Promise { + if (!config.twitter.allowDeleteTweets) return; + + const query = ` + DELETE FROM npwd_twitter_tweets + WHERE identifier = ? AND id = ? + `; + const [ result ] = await pool.execute(query, [identifier, tweetId]); +} + /** * Check if a "like" relationship between a profile and a tweet exists * @param profileId - primary key of the profile to check @@ -297,6 +315,17 @@ onNet(events.TWITTER_CREATE_TWEET, async (tweet: Tweet) => { } }); +onNet(events.TWITTER_DELETE_TWEET, async (tweetId: number) => { + try { + const identifier = ESX.GetPlayerFromId(getSource()).getIdentifier(); + await deleteTweet(identifier, tweetId); + + emitNet(events.TWITTER_DELETE_TWEET_SUCCESS, getSource()); + } catch (e) { + emitNet(events.TWITTER_DELETE_TWEET_FAILURE, getSource()); + } +}); + onNet(events.TWITTER_TOGGLE_LIKE, async (tweetId: number) => { try { const identifier = ESX.GetPlayerFromId(getSource()).getIdentifier(); diff --git a/resources/utils/events.ts b/resources/utils/events.ts index 07a0451..1bac517 100644 --- a/resources/utils/events.ts +++ b/resources/utils/events.ts @@ -17,6 +17,9 @@ export default { TWITTER_CREATE_TWEET_RESULT: "createTweetResult", TWITTER_CREATE_TWEET_BROADCAST: "createTweetBroadcast", TWITTER_CREATE_TWEET_FAILURE: "createTweetResultFailed", + TWITTER_DELETE_TWEET: "phone:deleteTweet", + TWITTER_DELETE_TWEET_SUCCESS: "deleteTweetSuccess", + TWITTER_DELETE_TWEET_FAILURE: "deleteTweetFailed", TWITTER_TOGGLE_LIKE: "phone:toggleLike", TWITTER_TOGGLE_LIKE_SUCCESS: "toggleLikeSuccess", TWITTER_TOGGLE_LIKE_FAILURE: "toggleLikeFailed",