diff --git a/import.sql b/import.sql index 522c6d0..5728288 100644 --- a/import.sql +++ b/import.sql @@ -22,6 +22,7 @@ CREATE TABLE IF NOT EXISTS `npwd_twitter_tweets` ( `identifier` varchar(40) NOT NULL, `visible` tinyint NOT NULL DEFAULT '1', `images` varchar(1000) DEFAULT '', + `retweet` int DEFAULT NULL, PRIMARY KEY (`id`) ); diff --git a/phone/src/apps/twitter/components/AddTweetModal.tsx b/phone/src/apps/twitter/components/AddTweetModal.tsx index 13263c7..e06d790 100644 --- a/phone/src/apps/twitter/components/AddTweetModal.tsx +++ b/phone/src/apps/twitter/components/AddTweetModal.tsx @@ -15,6 +15,7 @@ import ControlButtons from './buttons/ControlButtons'; import IconButtons from './buttons/IconButtons'; import { usePhone } from '../../../os/phone/hooks/usePhone'; import { getNewLineCount } from '../utils/message'; +import { NewTweet } from '../../../common/typings/twitter'; const useStyles = makeStyles((theme) => ({ root: { @@ -104,11 +105,10 @@ export const AddTweetModal = () => { if (cleanedMessage.length === 0) return; if (!isValidMessage(cleanedMessage)) return; - const data = { + const data: NewTweet = { message, images: images && images.length > 0 ? images.map((image) => image.link).join(IMAGE_DELIMITER) : '', - realUser: 'testUser', }; Nui.send('phone:createTweet', data); _handleClose(); diff --git a/phone/src/apps/twitter/components/TwitterApp.tsx b/phone/src/apps/twitter/components/TwitterApp.tsx index dfaa02b..200f1e0 100644 --- a/phone/src/apps/twitter/components/TwitterApp.tsx +++ b/phone/src/apps/twitter/components/TwitterApp.tsx @@ -19,6 +19,7 @@ import 'emoji-mart/css/emoji-mart.css'; import { useProfile } from '../hooks/useProfile'; import ProfilePrompt from './profile/ProfilePrompt'; import InjectDebugData from '../../../os/debug/InjectDebugData'; +import { TwitterThemeProvider } from '../providers/TwitterThemeProvider'; const useStyles = makeStyles(() => ({ backgroundModal: { @@ -66,26 +67,28 @@ export const TwitterApp = () => { const showTweetButton = !promptProfileName && activePage === 0; return ( - - -
- - - {promptProfileName ? ( - - ) : ( - - - - - + + + +
+ + + {promptProfileName ? ( + + ) : ( + + + + + + )} + + {showTweetButton && } + {!promptProfileName && ( + )} - - {showTweetButton && } - {!promptProfileName && ( - - )} - + + ); }; diff --git a/phone/src/apps/twitter/components/buttons/QuoteButton.tsx b/phone/src/apps/twitter/components/buttons/QuoteButton.tsx deleted file mode 100644 index f5ce1ce..0000000 --- a/phone/src/apps/twitter/components/buttons/QuoteButton.tsx +++ /dev/null @@ -1,26 +0,0 @@ -import React from 'react'; -import { useSetRecoilState } from 'recoil'; -import { Button } from '@material-ui/core'; -import QuoteIcon from '@material-ui/icons/FormatQuote'; - -import { twitterState } from '../../hooks/state'; - -export const QuoteButton = ({ message }) => { - const setModalVisible = useSetRecoilState(twitterState.showCreateTweetModal); - const setMessage = useSetRecoilState(twitterState.modalMessage); - - const handleClick = () => { - setMessage(`> ${message} - -`); - setModalVisible(true); - }; - - return ( - - ); -}; - -export default QuoteButton; diff --git a/phone/src/apps/twitter/components/buttons/RetweetButton.tsx b/phone/src/apps/twitter/components/buttons/RetweetButton.tsx new file mode 100644 index 0000000..4d98fa7 --- /dev/null +++ b/phone/src/apps/twitter/components/buttons/RetweetButton.tsx @@ -0,0 +1,65 @@ +import React, { useState } from 'react'; +import { CircularProgress, makeStyles, Button } from '@material-ui/core'; +import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; +import { faRetweet } from '@fortawesome/free-solid-svg-icons'; + +import { ITwitterTheme } from '../../twitter.theme'; +import Nui from '../../../../os/nui-events/utils/Nui'; + +interface IProps { + tweetId: number; + retweetId: string; + isRetweet: boolean | number; +} + +const useStyles = makeStyles((theme: ITwitterTheme) => ({ + icon: {}, // no styles on default icon + iconFilled: { + color: theme.palette.twitter.main, + } +})); + +const LOADING_TIME = 1250; + +export const RetweetButton = ({ tweetId, isRetweet, retweetId }: IProps) => { + const [retweeted, setRetweeted] = useState(false); + const [loading, setLoading] = useState(false); + const classes = useStyles(); + + const handleClick = () => { + // don't allow someone to spam retweet + // note that this is enforced by the server currently, not the UI + // so if a player refreshes the page they won't see the filled + // button anymore + if (retweeted) return; + + // if someone is retweeting something that is itself a retweet + // then we want to retweet the original post (if we haven't already) + const idToRetweet = isRetweet ? retweetId : tweetId; + Nui.send('phone:retweet', idToRetweet); + setLoading(true); + window.setTimeout(() => { + setRetweeted(true); + setLoading(false); + }, LOADING_TIME); + }; + + + if (loading) { + return ( + + ); + } + + const className = retweeted ? classes.iconFilled : classes.icon; + + return ( + + ); +}; + +export default RetweetButton; diff --git a/phone/src/apps/twitter/components/tweet/Retweet.tsx b/phone/src/apps/twitter/components/tweet/Retweet.tsx new file mode 100644 index 0000000..c55a4ea --- /dev/null +++ b/phone/src/apps/twitter/components/tweet/Retweet.tsx @@ -0,0 +1,35 @@ +import React from 'react'; +import { useTranslation } from 'react-i18next'; +import { makeStyles } from '@material-ui/core/styles'; +import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; +import { faRetweet } from '@fortawesome/free-solid-svg-icons'; + +import { ITwitterTheme } from '../../twitter.theme'; + +interface IProps { + profileName: string; +} + +const useStyles = makeStyles((theme: ITwitterTheme) => ({ + root: { + color: theme.palette.secondary.main, + fontSize: '16px', + justifyContent: 'flex-start', + width: '100%', + paddingBottom: '8px', + marginBottom: '6px', + marginTop: '-6px' + }, +})); + +function Retweet({ profileName }: IProps) { + const { t } = useTranslation(); + const classes = useStyles(); + return ( +
+ {profileName} {t('APPS_TWITTER_RETWEETED')} +
+ ) +} + +export default Retweet; \ No newline at end of file diff --git a/phone/src/apps/twitter/components/tweet/Tweet.tsx b/phone/src/apps/twitter/components/tweet/Tweet.tsx index cedb339..019cd83 100644 --- a/phone/src/apps/twitter/components/tweet/Tweet.tsx +++ b/phone/src/apps/twitter/components/tweet/Tweet.tsx @@ -1,25 +1,33 @@ import React, { memo } from 'react'; +import DOMPurify from 'dompurify'; import { useTranslation } from 'react-i18next'; import { makeStyles } from '@material-ui/core/styles'; import { ListItemAvatar, Avatar as MuiAvatar, ListItem, Typography } from '@material-ui/core'; + +import { Tweet as ITweet } from '../../../../common/typings/twitter'; 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 { RetweetButton } from '../buttons/RetweetButton'; import { usePhone } from '../../../../os/phone/hooks/usePhone'; -import DOMPurify from 'dompurify'; +import Retweet from './Retweet'; +import ShowMore from './ShowMore'; -const useStyles = makeStyles(() => ({ +const useStyles = makeStyles({ root: { overflowX: 'hidden', + display: 'flex', + flexFlow: 'column nowrap', + width: '100%', + marginTop: '6px', + }, + container: { display: 'flex', flexFlow: 'row nowrap', alignItems: 'flex-start', width: '100%', - marginTop: '6px', }, content: { display: 'flex', @@ -53,19 +61,24 @@ const useStyles = makeStyles(() => ({ marginLeft: '-20px', // easier to do this here than override the MUI styles marginTop: '3px', }, -})); +}); -export const Tweet = (tweet) => { +export const Tweet = (tweet: ITweet) => { const { id, message, images, avatar_url, profile_name, + retweetProfileName, + retweetAvatarUrl, + retweetId, seconds_since_tweet, isLiked, isMine, isReported, + isRetweet, + isRetweetedByPlayer, } = tweet; const classes = useStyles(); const { t } = useTranslation(); @@ -86,41 +99,52 @@ export const Tweet = (tweet) => { ALLOWED_TAGS: ['br'], }); - const profileName = profile_name ? `@${profile_name}` : ''; + const profileName = isRetweet ? retweetProfileName : profile_name; + const formattedProfileName = profileName ? `@${profileName}` : ''; + + const avatarUrl = isRetweet ? retweetAvatarUrl : avatar_url; return ( - {enableAvatars && ( - - - - - - )} -
-
-
{profileName}
- - {secondsToHumanReadable(t, seconds_since_tweet)} - -
-
- {enableImages && } -
- - - - + {isRetweet && } +
+ {enableAvatars && ( + + + + + + )} +
+
+
{formattedProfileName}
+ + {secondsToHumanReadable(t, seconds_since_tweet)} + +
+
+ {enableImages && } +
+ + + {!isMine && ( + )} + +
diff --git a/phone/src/apps/twitter/hooks/useTwitterNotifications.ts b/phone/src/apps/twitter/hooks/useTwitterNotifications.ts index f6686b1..a1d7b6d 100644 --- a/phone/src/apps/twitter/hooks/useTwitterNotifications.ts +++ b/phone/src/apps/twitter/hooks/useTwitterNotifications.ts @@ -1,6 +1,7 @@ import { useTranslation } from 'react-i18next'; import { useHistory } from 'react-router-dom'; import { useRecoilState } from 'recoil'; +import { Tweet } from '../../../common/typings/twitter'; import { useApp } from '../../../os/apps/hooks/useApps'; import { useNotifications } from '../../../os/notifications/hooks/useNotifications'; import { twitterState } from './state'; @@ -22,12 +23,15 @@ export const useTwitterNotifications = () => { const [unreadCount, setUnreadCount] = useRecoilState(twitterState.unreadTweetsCount); - const setNotification = ({ profile_name, message }) => { + const setNotification = ({ profile_name, message, isRetweet }) => { + const titleStr = isRetweet + ? 'APPS_TWITTER_NEW_RETWEET_BROADCAST' + : 'APPS_TWITTER_NEW_BROADCAST'; const notification = { app: 'TWITTER', id: NOTIFICATION_ID, sound: true, - title: t('APPS_TWITTER_NEW_BROADCAST', { profile_name }), + title: t(titleStr, { profile_name }), onClick: () => history.push('/twitter'), content: message, icon, diff --git a/phone/src/apps/twitter/hooks/useTwitterService.ts b/phone/src/apps/twitter/hooks/useTwitterService.ts index 903d9b9..0cd2938 100644 --- a/phone/src/apps/twitter/hooks/useTwitterService.ts +++ b/phone/src/apps/twitter/hooks/useTwitterService.ts @@ -64,4 +64,5 @@ export const useTwitterService = () => { useNuiEvent(APP_TWITTER, 'createTweetLoading', setCreateLoading); useNuiEvent(APP_TWITTER, 'createTweetResult', handleAddAlert); useNuiEvent(APP_TWITTER, 'createTweetBroadcast', setNotification); + useNuiEvent(APP_TWITTER, 'phone:retweetExists', handleAddAlert) }; diff --git a/phone/src/apps/twitter/providers/TwitterThemeProvider.tsx b/phone/src/apps/twitter/providers/TwitterThemeProvider.tsx new file mode 100644 index 0000000..018e03c --- /dev/null +++ b/phone/src/apps/twitter/providers/TwitterThemeProvider.tsx @@ -0,0 +1,5 @@ +import { createAppThemeProvider } from '../../../os/apps/utils/createAppThemeProvider'; + +import twitterTheme from '../twitter.theme'; + +export const TwitterThemeProvider = createAppThemeProvider(twitterTheme); diff --git a/phone/src/apps/twitter/twitter.theme.ts b/phone/src/apps/twitter/twitter.theme.ts new file mode 100644 index 0000000..9ea4925 --- /dev/null +++ b/phone/src/apps/twitter/twitter.theme.ts @@ -0,0 +1,28 @@ +import { blue, common } from '@material-ui/core/colors'; + +export const CONTACTS_APP_PRIMARY_COLOR = blue[500]; +export const CONTACTS_APP_TEXT_COLOR = common.white; + +export interface ITwitterTheme { + palette: { + twitter: { + main: string; + }, + secondary: { + main: string; + } + } +} + +const theme: ITwitterTheme = { + palette: { + twitter: { + main: "#00acee", + }, + secondary: { + main: '#999', + } + }, +}; + +export default theme; diff --git a/phone/src/common/typings/twitter.ts b/phone/src/common/typings/twitter.ts index d6fa3fb..7acd4e5 100644 --- a/phone/src/common/typings/twitter.ts +++ b/phone/src/common/typings/twitter.ts @@ -1,15 +1,27 @@ -export interface Tweet { - id: number; - profile_name?: string; - profile_id: number; - isMine: boolean; - isReported: boolean; - avatar_url?: string; + +export interface NewTweet { message: string; - createdAt?: string; - updatedAt?: string; images?: string; - likes?: number; + retweet?: number; +} + +export interface Tweet extends NewTweet { + profile_name: string; + profile_id: number; + id: number; + isMine: boolean; + isLiked: boolean; + isReported: boolean; + avatar_url: string; + isRetweet: number | boolean; + isRetweetedByPlayer?: boolean; + retweetIdentifier: string; + retweetId: string; + seconds_since_tweet: number; + retweetProfileName: string; + retweetAvatarUrl: string; + createdAt: string; + updatedAt: string; } export interface Profile { diff --git a/phone/src/locale/en.json b/phone/src/locale/en.json index c2c8892..e46ad63 100644 --- a/phone/src/locale/en.json +++ b/phone/src/locale/en.json @@ -35,6 +35,7 @@ "APPS_TWITTER_DELETE": "Delete", "APPS_TWITTER_REPORT": "Report", "APPS_TWITTER_REPORTED": " Reported", + "APPS_TWITTER_RETWEETED": "Retweeted", "APPS_TWITTER_JUST_NOW": "Just now", "APPS_TWITTER_SEARCH_TWEETS": "Search for tweets", "APPS_TWITTER_SEARCH_TWEETS_PLACEHOLDER": "Search by profile name or message", @@ -49,11 +50,13 @@ "APPS_TWITTER_EDIT_PROFILE_JOB": "Job", "APPS_TWITTER_EDIT_PROFILE_SUCCESS": "Successfully updated your profile", "APPS_TWITTER_EDIT_PROFILE_FAILURE": "Failed to update your profile", + "APPS_TWITTER_RETWEET_EXISTS": "You have already retweeted this tweet!", "APPS_TWITTER_TIME_JUST_NOW": "Just now", "APPS_TWITTER_TIME_MINUTES": "m", "APPS_TWITTER_TIME_HOURS": "h", "APPS_TWITTER_TIME_DAYS": "d", "APPS_TWITTER_NEW_BROADCAST": "New tweet from @{{ profile_name }}!", + "APPS_TWITTER_NEW_RETWEET_BROADCAST": "@{{ profile_name }} retweeted someone!", "APPS_TWITTER_UNREAD_MESSAGES": "You have {{ count }} new tweets in your timeline!", "APPS_BANK_TRANSFER": "Transfer", diff --git a/resources/client/cl_twitter.ts b/resources/client/cl_twitter.ts index df4b7bb..7cd40d6 100644 --- a/resources/client/cl_twitter.ts +++ b/resources/client/cl_twitter.ts @@ -111,6 +111,20 @@ on(`__cfx_nui:${events.TWITTER_TOGGLE_LIKE}`, (tweetId: number) => { emitNet(events.TWITTER_TOGGLE_LIKE, tweetId); }); +/** + * Twitter retweets + */ +RegisterNuiCallbackType(events.TWITTER_RETWEET); +on(`__cfx_nui:${events.TWITTER_RETWEET}`, (tweetId: number, cb: Function) => { + emitNet(events.TWITTER_RETWEET, tweetId); + cb(); +}); + +onNet(events.TWITTER_RETWEET_EXISTS, (alert: IAlertProps) => { + sendTwitterMessage(events.TWITTER_RETWEET_EXISTS, alert); +}); + + /** * Twitter reporting tweets */ diff --git a/resources/server/sv_twitter.ts b/resources/server/sv_twitter.ts index 885a816..358b1e5 100644 --- a/resources/server/sv_twitter.ts +++ b/resources/server/sv_twitter.ts @@ -1,7 +1,7 @@ import { pool } from './db'; import { ESX } from './server'; import { getIdentifier, getSource } from './functions'; -import { Tweet, Profile } from '../../phone/src/common/typings/twitter'; +import { NewTweet, Tweet, Profile } from '../../phone/src/common/typings/twitter'; import events from '../utils/events'; import config from '../utils/config'; import { reportTweetToDiscord } from './discord'; @@ -13,34 +13,54 @@ interface ProfileName { profile_name: string; } +const formatTweets = (profileId: number) => (tweet: Tweet): Tweet => ({ + ...tweet, + isMine: tweet.profile_id === profileId, + isRetweet: tweet.isRetweet === 1, +}); + +const SELECT_FIELDS = ` + npwd_twitter_tweets.id, + npwd_twitter_tweets.identifier, + npwd_twitter_profiles.id AS profile_id, + npwd_twitter_profiles.profile_name, + npwd_twitter_profiles.avatar_url, + npwd_twitter_tweets.likes, + npwd_twitter_tweets.visible, + IFNULL(COALESCE(retweets.message, npwd_twitter_tweets.message), '') AS message, + IFNULL(COALESCE(retweets.images, npwd_twitter_tweets.images), '') AS images, + npwd_twitter_tweets.retweet IS NOT NULL AS isRetweet, + retweets.id AS retweetId, + retweets_profiles.profile_name AS retweetProfileName, + retweets_profiles.avatar_url AS retweetAvatarUrl, + npwd_twitter_likes.id IS NOT NULL AS isLiked, + npwd_twitter_reports.id IS NOT NULL AS isReported, + npwd_twitter_tweets.createdAt, + npwd_twitter_tweets.updatedAt, + TIME_TO_SEC(TIMEDIFF( NOW(), npwd_twitter_tweets.createdAt)) AS seconds_since_tweet +` + /** * Retrieve the latest 100 tweets * @param profileId - twitter profile id of the player */ 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.*, - npwd_twitter_likes.id IS NOT NULL AS isLiked, - npwd_twitter_reports.id IS NOT NULL AS isReported, - TIME_TO_SEC(TIMEDIFF( NOW(), npwd_twitter_tweets.createdAt)) AS seconds_since_tweet - FROM npwd_twitter_tweets - LEFT OUTER JOIN npwd_twitter_profiles ON npwd_twitter_tweets.identifier = npwd_twitter_profiles.identifier - LEFT OUTER JOIN npwd_twitter_likes ON npwd_twitter_tweets.id = npwd_twitter_likes.tweet_id AND npwd_twitter_likes.profile_id = ? - LEFT OUTER JOIN npwd_twitter_reports ON npwd_twitter_tweets.id = npwd_twitter_reports.tweet_id AND npwd_twitter_reports.profile_id = ? - WHERE visible = 1 - ORDER BY npwd_twitter_tweets.createdAt DESC - LIMIT 100 - `; + SELECT + ${SELECT_FIELDS} + FROM npwd_twitter_tweets + LEFT OUTER JOIN npwd_twitter_profiles ON npwd_twitter_tweets.identifier = npwd_twitter_profiles.identifier + LEFT OUTER JOIN npwd_twitter_likes ON npwd_twitter_tweets.id = npwd_twitter_likes.tweet_id AND npwd_twitter_likes.profile_id = ? + LEFT OUTER JOIN npwd_twitter_reports ON npwd_twitter_tweets.id = npwd_twitter_reports.tweet_id AND npwd_twitter_reports.profile_id = ? + LEFT OUTER JOIN npwd_twitter_tweets AS retweets ON retweets.id = npwd_twitter_tweets.retweet + LEFT OUTER JOIN npwd_twitter_profiles AS retweets_profiles ON retweets.identifier = retweets_profiles.identifier + WHERE npwd_twitter_tweets.visible = 1 + ORDER BY npwd_twitter_tweets.createdAt DESC + LIMIT 100 + `; const [results] = await pool.query(query, [profileId, profileId]); const tweets = results; - return tweets.map((tweet) => ({ - ...tweet, - isMine: tweet.profile_id === profileId, - })); + return tweets.map(formatTweets(profileId)); } /** @@ -53,18 +73,14 @@ async function fetchTweetsFiltered(profileId: number, searchValue: string): Prom 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.*, - npwd_twitter_likes.id IS NOT NULL AS isLiked, - npwd_twitter_reports.id IS NOT NULL AS isReported, - TIME_TO_SEC(TIMEDIFF( NOW(), npwd_twitter_tweets.createdAt)) AS seconds_since_tweet + ${SELECT_FIELDS} FROM npwd_twitter_tweets LEFT OUTER JOIN npwd_twitter_profiles ON npwd_twitter_tweets.identifier = npwd_twitter_profiles.identifier LEFT OUTER JOIN npwd_twitter_likes ON npwd_twitter_tweets.id = npwd_twitter_likes.tweet_id AND npwd_twitter_likes.profile_id = ? LEFT OUTER JOIN npwd_twitter_reports ON npwd_twitter_tweets.id = npwd_twitter_reports.tweet_id AND npwd_twitter_reports.profile_id = ? - WHERE visible = 1 AND (npwd_twitter_profiles.profile_name LIKE ? OR npwd_twitter_tweets.message LIKE ?) + LEFT OUTER JOIN npwd_twitter_tweets AS retweets ON retweets.id = npwd_twitter_tweets.retweet + LEFT OUTER JOIN npwd_twitter_profiles AS retweets_profiles ON retweets.identifier = retweets_profiles.identifier + WHERE npwd_twitter_tweets.visible = 1 AND (npwd_twitter_profiles.profile_name LIKE ? OR npwd_twitter_tweets.message LIKE ?) ORDER BY npwd_twitter_tweets.createdAt DESC LIMIT 100 `; @@ -75,10 +91,7 @@ async function fetchTweetsFiltered(profileId: number, searchValue: string): Prom parameterizedSearchValue, ]); const tweets = results; - return tweets.map((tweet) => ({ - ...tweet, - isMine: tweet.profile_id === profileId, - })); + return tweets.map(formatTweets(profileId)); } /** @@ -88,18 +101,18 @@ async function fetchTweetsFiltered(profileId: number, searchValue: string): Prom async function getTweet(profileId: number, tweetId: 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.* + ${SELECT_FIELDS} FROM npwd_twitter_tweets + LEFT OUTER JOIN npwd_twitter_likes ON npwd_twitter_tweets.id = npwd_twitter_likes.tweet_id AND npwd_twitter_likes.profile_id = ? + LEFT OUTER JOIN npwd_twitter_reports ON npwd_twitter_tweets.id = npwd_twitter_reports.tweet_id AND npwd_twitter_reports.profile_id = ? + LEFT OUTER JOIN npwd_twitter_tweets AS retweets ON retweets.id = npwd_twitter_tweets.retweet + LEFT OUTER JOIN npwd_twitter_profiles AS retweets_profiles ON retweets.identifier = retweets_profiles.identifier LEFT OUTER JOIN npwd_twitter_profiles ON npwd_twitter_tweets.identifier = npwd_twitter_profiles.identifier WHERE npwd_twitter_tweets.id = ? `; - const [results] = await pool.query(query, [tweetId]); + const [results] = await pool.query(query, [profileId, profileId, tweetId]); const tweets = results; - const tweet = tweets[0]; - return { ...tweet, isMine: tweet.profile_id === profileId }; + return tweets.map(formatTweets(profileId))[0]; } /** @@ -107,13 +120,18 @@ async function getTweet(profileId: number, tweetId: number): Promise { * @param identifier - player identifier * @param tweet - tweet to be created */ -async function createTweet(identifier: string, tweet: Tweet): Promise { +async function createTweet(identifier: string, tweet: NewTweet): Promise { const profile = await getProfile(identifier); const query = ` - INSERT INTO npwd_twitter_tweets (identifier, message, images) - VALUES (?, ?, ?) + INSERT INTO npwd_twitter_tweets (identifier, message, images, retweet) + VALUES (?, ?, ?, ?) `; - const [results] = await pool.query(query, [identifier, tweet.message, tweet.images]); + const [results] = await pool.query(query, [ + identifier, + tweet.message, + tweet.images, + tweet.retweet, + ]); const insertData = results; return await getTweet(profile.id, insertData.insertId); } @@ -269,6 +287,23 @@ async function doesReportExist(tweetId: number, profileId: number): Promise 0; } +/** + * Check to see if this player has already retweeted this tweet or + * is the original poster of a retweeted tweet + * @param tweetId - ID of the tweet in question + * @param identifier - player identifier of the person performing the retweet + */ +async function doesRetweetExist(tweetId: number, identifier: string): Promise { + const query = ` + SELECT COUNT(id) as count + FROM npwd_twitter_tweets + WHERE (id = ? OR retweet = ?) AND identifier = ? + ` + const [results] = await pool.query(query, [tweetId, tweetId, identifier]); + const counts = results; + return counts[0].count > 0; +} + onNet(events.TWITTER_GET_OR_CREATE_PROFILE, async () => { const _source = getSource(); try { @@ -385,6 +420,38 @@ onNet(events.TWITTER_TOGGLE_LIKE, async (tweetId: number) => { } }); +onNet(events.TWITTER_RETWEET, async (tweetId: number) => { + const _source = getSource(); + try { + const identifier = getIdentifier(_source); + + // alert the player that they have already retweeted + // this post (or that they are the original poster) + if(await doesRetweetExist(tweetId, identifier)) { + emitNet(events.TWITTER_RETWEET_EXISTS, _source, { + message: 'TWITTER_RETWEET_EXISTS', + type: 'error', + }); + return; + } + + // our message for this row is blank because when we + // query for this tweet it will join off of the retweet + // column and fetch the message from the related tweet + const retweet: NewTweet = { message: '', retweet: tweetId }; + const createdTweet = await createTweet(identifier, retweet); + + const profile = await getProfile(identifier); + const tweet = await getTweet(profile.id, createdTweet.id); + emitNet(events.TWITTER_CREATE_TWEET_BROADCAST, -1, tweet); + } catch (e) { + twitterLogger.error(`Retweet failed, ${e.message}`, { + source: _source, + }); + emitNet(events.TWITTER_RETWEET_FAILURE, _source); + } +}); + onNet(events.TWITTER_REPORT, async (tweetId: number) => { const _source = getSource(); try { diff --git a/resources/utils/events.ts b/resources/utils/events.ts index f5d54d6..aa46fd1 100644 --- a/resources/utils/events.ts +++ b/resources/utils/events.ts @@ -23,6 +23,10 @@ export default { TWITTER_TOGGLE_LIKE: 'phone:toggleLike', TWITTER_TOGGLE_LIKE_SUCCESS: 'toggleLikeSuccess', TWITTER_TOGGLE_LIKE_FAILURE: 'toggleLikeFailed', + TWITTER_RETWEET: 'phone:retweet', + TWITTER_RETWEET_SUCCESS: 'phone:retweetSuccess', + TWITTER_RETWEET_EXISTS: 'phone:retweetExists', + TWITTER_RETWEET_FAILURE: 'phone:retweetFailed', TWITTER_REPORT: 'phone:reportTweet', TWITTER_REPORT_SUCCESS: 'reportTweetSuccess', TWITTER_REPORT_FAILURE: 'reportTweetFailed',