From 8000cc3d3f45619347b700a95ebe803ae8b21e4e Mon Sep 17 00:00:00 2001 From: Kire Date: Tue, 16 Mar 2021 18:40:36 -0400 Subject: [PATCH 1/6] added UI for choosing a default profile name --- .../components/modal/MessageModal.tsx | 2 +- .../apps/twitter/components/TwitterApp.tsx | 11 ++- .../profile/DefaultProfilePrompt.tsx | 78 +++++++++++++++++++ phone/src/locale/en.json | 1 + 4 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 phone/src/apps/twitter/components/profile/DefaultProfilePrompt.tsx diff --git a/phone/src/apps/messages/components/modal/MessageModal.tsx b/phone/src/apps/messages/components/modal/MessageModal.tsx index debf82b..1aef3bc 100644 --- a/phone/src/apps/messages/components/modal/MessageModal.tsx +++ b/phone/src/apps/messages/components/modal/MessageModal.tsx @@ -157,7 +157,7 @@ export const MessageModal = () => { ) : null} {!activeMessageGroup?.isGroupChat && - getDisplayByNumber(targetNumber) == targetNumber ? ( + getDisplayByNumber(targetNumber) === targetNumber ? ( diff --git a/phone/src/apps/twitter/components/TwitterApp.tsx b/phone/src/apps/twitter/components/TwitterApp.tsx index aea19f1..18fcbc0 100644 --- a/phone/src/apps/twitter/components/TwitterApp.tsx +++ b/phone/src/apps/twitter/components/TwitterApp.tsx @@ -20,6 +20,7 @@ import { useProfile } from '../hooks/useProfile'; import ProfilePrompt from './profile/ProfilePrompt'; import InjectDebugData from '../../../os/debug/InjectDebugData'; import { TwitterThemeProvider } from '../providers/TwitterThemeProvider'; +import DefaultProfilePrompt from './profile/DefaultProfilePrompt'; const useStyles = makeStyles(() => ({ backgroundModal: { @@ -42,12 +43,15 @@ export const TwitterApp = () => { // before any other action can be taken by the user we force // them have a profile name - const promptProfileName = profile && (!profile.profile_name || !profile.profile_name.trim()); + let promptProfileName = profile && (!profile.profile_name || !profile.profile_name.trim()); + promptProfileName = true; const openModal = () => setModalVisible(true); const handlePageChange = (e, page) => setActivePage(page); const showTweetButton = !promptProfileName && activePage === 0; + const defaultProfileNames = ['test_user', '123-456-7890']; + return ( @@ -56,7 +60,10 @@ export const TwitterApp = () => { {promptProfileName ? ( - + ) : ( diff --git a/phone/src/apps/twitter/components/profile/DefaultProfilePrompt.tsx b/phone/src/apps/twitter/components/profile/DefaultProfilePrompt.tsx new file mode 100644 index 0000000..50db3fb --- /dev/null +++ b/phone/src/apps/twitter/components/profile/DefaultProfilePrompt.tsx @@ -0,0 +1,78 @@ +import React, { useState } from 'react'; +import { makeStyles } from '@material-ui/core/styles'; +import InputLabel from '@material-ui/core/InputLabel'; +import MenuItem from '@material-ui/core/MenuItem'; +import FormControl from '@material-ui/core/FormControl'; +import Select from '@material-ui/core/Select'; +import { useTranslation } from 'react-i18next'; + +import Nui from '../../../../os/nui-events/utils/Nui'; +import { useProfile } from '../../hooks/useProfile'; +import ProfileUpdateButton from '../buttons/ProfileUpdateButton'; + +interface IProps { + defaultProfileNames: string[]; + updateEvent: string; +} + +const useStyles = makeStyles((theme) => ({ + root: { + position: 'relative', + width: '100%', + height: '100%', + padding: '15px', + }, + formControl: { + // margin: theme.spacing(1), + width: '100%', + }, +})); + +export function DefaultProfilePrompt({ defaultProfileNames, updateEvent }: IProps): JSX.Element { + const classes = useStyles(); + const { t } = useTranslation(); + const { profile } = useProfile(); + + const hasDefaults = defaultProfileNames.length > 0; + const defaultProfileName = hasDefaults ? defaultProfileNames[0] : ''; + + const [profileName, setProfileName] = useState(defaultProfileName); + + const handleUpdate = () => { + const data = { + ...profile, + profile_name: profileName, + }; + Nui.send(updateEvent, data); + }; + + const handleChange = (event: React.ChangeEvent): void => { + setProfileName(event.target.value); + }; + + return ( +
+ + + {t('APPS_TWITTER_EDIT_DEFAULT_PROFILE_NAME')} + + + + +
+ ); +} + +export default DefaultProfilePrompt; diff --git a/phone/src/locale/en.json b/phone/src/locale/en.json index 106fdb9..3722dff 100644 --- a/phone/src/locale/en.json +++ b/phone/src/locale/en.json @@ -77,6 +77,7 @@ "APPS_TWITTER_CREATE_FAILED": "Failed to send your tweet", "APPS_TWITTER_EDIT_PROFILE_AVATAR": "Avatar URL", "APPS_TWITTER_EDIT_PROFILE_NAME": "Profile name", + "APPS_TWITTER_EDIT_DEFAULT_PROFILE_NAME": "Choose Profile Name", "APPS_TWITTER_EDIT_PROFILE_BIO": "Bio", "APPS_TWITTER_EDIT_PROFILE_LOCATION": "Location", "APPS_TWITTER_EDIT_PROFILE_JOB": "Job", From 97f5c99cc04ee879e3dd7c09e866b88397884677 Mon Sep 17 00:00:00 2001 From: Kire Date: Tue, 16 Mar 2021 23:17:25 -0400 Subject: [PATCH 2/6] handle all configuration cases --- .../apps/twitter/components/TwitterApp.tsx | 215 +++++++++--------- .../profile/DefaultProfilePrompt.tsx | 32 ++- .../components/profile/ProfilePrompt.tsx | 39 +++- phone/src/apps/twitter/hooks/state.ts | 4 + .../apps/twitter/hooks/useTwitterService.ts | 3 + phone/src/locale/en.json | 2 + resources/client/cl_main.ts | 1 + resources/client/cl_twitter.ts | 18 ++ resources/server/players/sv_players.ts | 48 ++++ resources/server/sv_main.ts | 1 - resources/server/sv_twitter.ts | 80 +++++-- resources/utils/events.ts | 3 + 12 files changed, 289 insertions(+), 157 deletions(-) diff --git a/phone/src/apps/twitter/components/TwitterApp.tsx b/phone/src/apps/twitter/components/TwitterApp.tsx index 18fcbc0..2f6f12d 100644 --- a/phone/src/apps/twitter/components/TwitterApp.tsx +++ b/phone/src/apps/twitter/components/TwitterApp.tsx @@ -21,6 +21,9 @@ import ProfilePrompt from './profile/ProfilePrompt'; import InjectDebugData from '../../../os/debug/InjectDebugData'; import { TwitterThemeProvider } from '../providers/TwitterThemeProvider'; import DefaultProfilePrompt from './profile/DefaultProfilePrompt'; +import { useRecoilValue } from 'recoil'; +import { twitterState } from '../hooks/state'; +import { Profile } from '../../../../../typings/twitter'; const useStyles = makeStyles(() => ({ backgroundModal: { @@ -42,16 +45,13 @@ export const TwitterApp = () => { const { profile } = useProfile(); // before any other action can be taken by the user we force - // them have a profile name - let promptProfileName = profile && (!profile.profile_name || !profile.profile_name.trim()); - promptProfileName = true; + // them have a profile name. + const promptProfileName = !profile || !profile.profile_name || !profile.profile_name.trim(); const openModal = () => setModalVisible(true); const handlePageChange = (e, page) => setActivePage(page); const showTweetButton = !promptProfileName && activePage === 0; - const defaultProfileNames = ['test_user', '123-456-7890']; - return ( @@ -60,10 +60,7 @@ export const TwitterApp = () => { {promptProfileName ? ( - + ) : ( @@ -81,104 +78,104 @@ export const TwitterApp = () => { ); }; -InjectDebugData([ - { - app: 'TWITTER', - method: 'getOrCreateTwitterProfile', - data: { - profile_name: 'Angular_Dev', - bio: 'Angular FTW', - job: 'Googler @ Google', - location: '1600 Amphitheatre Parkway, CA, USA', - avatar_url: 'https://i.tasoagc.dev/i9Ig', - }, - }, - { - app: 'TWITTER', - method: 'fetchTweets', - data: [ - { - id: 59, - message: 'XSS\n\nBro', - avatar_url: 'http://i.tasoagc.dev/jtKL', - seconds_since_tweet: 500, - isMine: false, - isRetweetedByPlayer: true, - updateAt: '2021-02-08 14:37:46', - visible: 1, - likes: 50000, - profile_name: 'jQuery Main', - images: 'http://i.tasoagc.dev/0xdp', - }, - { - id: 2, - message: 'GCPhone will never die', - avatar_url: 'http://i.tasoagc.dev/jtKL', - seconds_since_tweet: 500, - isMine: false, - updateAt: '2021-02-08 14:37:46', - visible: 1, - likes: 50000, - profile_name: 'GCPhone_v9999', - images: 'http://i.tasoagc.dev/0xdp', - }, - { - id: 1, - message: 'I love angular', - avatar_url: 'https://i.tasoagc.dev/i9Ig', - seconds_since_tweet: 800, - isMine: true, - updateAt: '2021-02-08 14:37:46', - visible: 1, - likes: 50000, - profile_name: 'Angular_Dev', - images: 'http://i.tasoagc.dev/i9Ig', - }, - { - id: 3, - message: - 'Anyways uhm... I bought a whole bunch of shungite, rocks, ' + - 'do you know what shungite is? Anybody know what shungite is. No, no ' + - 'Suge Knight, I think hes locked up in prison.', - avatar_url: 'http://i.tasoagc.dev/0v64', - seconds_since_tweet: 800, - isMine: true, - updateAt: '2021-02-08 14:37:46', - visible: 1, - likes: 50000, - profile_name: 'The_Doc', - }, - ], - }, -]); +// InjectDebugData([ +// { +// app: 'TWITTER', +// method: 'getOrCreateTwitterProfile', +// data: { +// profile_name: 'Angular_Dev', +// bio: 'Angular FTW', +// job: 'Googler @ Google', +// location: '1600 Amphitheatre Parkway, CA, USA', +// avatar_url: 'https://i.tasoagc.dev/i9Ig', +// }, +// }, +// { +// app: 'TWITTER', +// method: 'fetchTweets', +// data: [ +// { +// id: 59, +// message: 'XSS\n\nBro', +// avatar_url: 'http://i.tasoagc.dev/jtKL', +// seconds_since_tweet: 500, +// isMine: false, +// isRetweetedByPlayer: true, +// updateAt: '2021-02-08 14:37:46', +// visible: 1, +// likes: 50000, +// profile_name: 'jQuery Main', +// images: 'http://i.tasoagc.dev/0xdp', +// }, +// { +// id: 2, +// message: 'GCPhone will never die', +// avatar_url: 'http://i.tasoagc.dev/jtKL', +// seconds_since_tweet: 500, +// isMine: false, +// updateAt: '2021-02-08 14:37:46', +// visible: 1, +// likes: 50000, +// profile_name: 'GCPhone_v9999', +// images: 'http://i.tasoagc.dev/0xdp', +// }, +// { +// id: 1, +// message: 'I love angular', +// avatar_url: 'https://i.tasoagc.dev/i9Ig', +// seconds_since_tweet: 800, +// isMine: true, +// updateAt: '2021-02-08 14:37:46', +// visible: 1, +// likes: 50000, +// profile_name: 'Angular_Dev', +// images: 'http://i.tasoagc.dev/i9Ig', +// }, +// { +// id: 3, +// message: +// 'Anyways uhm... I bought a whole bunch of shungite, rocks, ' + +// 'do you know what shungite is? Anybody know what shungite is. No, no ' + +// 'Suge Knight, I think hes locked up in prison.', +// avatar_url: 'http://i.tasoagc.dev/0v64', +// seconds_since_tweet: 800, +// isMine: true, +// updateAt: '2021-02-08 14:37:46', +// visible: 1, +// likes: 50000, +// profile_name: 'The_Doc', +// }, +// ], +// }, +// ]); -InjectDebugData( - [ - { - app: 'TWITTER', - method: 'createTweetBroadcast', - data: { - id: 111, - profile_id: 111, - profile_name: 'Kidz', - isMine: false, - isReported: false, - message: 'I have tweeted this because Im badass', - }, - }, - { - app: 'TWITTER', - method: 'createTweetBroadcast', - data: { - id: 111, - profile_id: 111, - profile_name: 'ItsChip', - isMine: false, - isReported: false, - message: - 'I have tweeted this because Im even more badass... And I need this to be longer so I will continue to write', - }, - }, - ], - 3000, -); +// InjectDebugData( +// [ +// { +// app: 'TWITTER', +// method: 'createTweetBroadcast', +// data: { +// id: 111, +// profile_id: 111, +// profile_name: 'Kidz', +// isMine: false, +// isReported: false, +// message: 'I have tweeted this because Im badass', +// }, +// }, +// { +// app: 'TWITTER', +// method: 'createTweetBroadcast', +// data: { +// id: 111, +// profile_id: 111, +// profile_name: 'ItsChip', +// isMine: false, +// isReported: false, +// message: +// 'I have tweeted this because Im even more badass... And I need this to be longer so I will continue to write', +// }, +// }, +// ], +// 3000, +// ); diff --git a/phone/src/apps/twitter/components/profile/DefaultProfilePrompt.tsx b/phone/src/apps/twitter/components/profile/DefaultProfilePrompt.tsx index 50db3fb..304e6a2 100644 --- a/phone/src/apps/twitter/components/profile/DefaultProfilePrompt.tsx +++ b/phone/src/apps/twitter/components/profile/DefaultProfilePrompt.tsx @@ -11,8 +11,10 @@ import { useProfile } from '../../hooks/useProfile'; import ProfileUpdateButton from '../buttons/ProfileUpdateButton'; interface IProps { + handleUpdate: () => void; + profileName: string; + setProfileName: (name: string) => void; defaultProfileNames: string[]; - updateEvent: string; } const useStyles = makeStyles((theme) => ({ @@ -28,25 +30,17 @@ const useStyles = makeStyles((theme) => ({ }, })); -export function DefaultProfilePrompt({ defaultProfileNames, updateEvent }: IProps): JSX.Element { +export function DefaultProfilePrompt({ + profileName, + setProfileName, + handleUpdate, + defaultProfileNames, +}: IProps): JSX.Element { const classes = useStyles(); const { t } = useTranslation(); - const { profile } = useProfile(); - - const hasDefaults = defaultProfileNames.length > 0; - const defaultProfileName = hasDefaults ? defaultProfileNames[0] : ''; - - const [profileName, setProfileName] = useState(defaultProfileName); - - const handleUpdate = () => { - const data = { - ...profile, - profile_name: profileName, - }; - Nui.send(updateEvent, data); - }; const handleChange = (event: React.ChangeEvent): void => { + event.preventDefault(); setProfileName(event.target.value); }; @@ -65,8 +59,10 @@ export function DefaultProfilePrompt({ defaultProfileNames, updateEvent }: IProp }} > + {defaultProfileNames.map((name) => ( + ))} diff --git a/phone/src/apps/twitter/components/profile/ProfilePrompt.tsx b/phone/src/apps/twitter/components/profile/ProfilePrompt.tsx index 7fc408f..27286b9 100644 --- a/phone/src/apps/twitter/components/profile/ProfilePrompt.tsx +++ b/phone/src/apps/twitter/components/profile/ProfilePrompt.tsx @@ -5,7 +5,12 @@ import { useTranslation } from 'react-i18next'; import Nui from '../../../../os/nui-events/utils/Nui'; import { useProfile } from '../../hooks/useProfile'; import ProfileField from './ProfileField'; +import DefaultProfilePrompt from './DefaultProfilePrompt'; import ProfileUpdateButton from '../buttons/ProfileUpdateButton'; +import { useRecoilValue } from 'recoil'; +import { twitterState } from '../../hooks/state'; +import { useSettings } from '../../../settings/hooks/useSettings'; +import { usePhone } from '../../../../os/phone/hooks/usePhone'; const useStyles = makeStyles((theme) => ({ root: { @@ -23,23 +28,43 @@ export function ProfilePrompt() { const classes = useStyles(); const { t } = useTranslation(); const { profile } = useProfile(); + const defaultProfileNames = useRecoilValue(twitterState.defaultProfileNames); + const [profileName, setProfileName] = useState(profile?.profile_name || ''); + const { config } = usePhone(); - const [name, handleNameChange] = useState(profile.profile_name || ''); + const showDefaultProfileNames = !config.twitter.allowEdtiableProfileName && !profile; + const eventName = showDefaultProfileNames + ? 'phone:createTwitterProfile' + : 'phone:updateTwitterProfile'; const handleUpdate = () => { - const data = { + Nui.send(eventName, { ...profile, - profile_name: name, - }; - Nui.send('phone:updateTwitterProfile', data); + profile_name: profileName, + }); }; + // case where profile doesn't exist, couldn't be created automatically + // and the player is not allowed to edit their own profile name + if (showDefaultProfileNames) { + return ( + + ); + } + + // case where profile doesn't exist, couldn't be created automatically + // but the player IS allowed to edit their own profile name return (
diff --git a/phone/src/apps/twitter/hooks/state.ts b/phone/src/apps/twitter/hooks/state.ts index e97d497..a97685b 100644 --- a/phone/src/apps/twitter/hooks/state.ts +++ b/phone/src/apps/twitter/hooks/state.ts @@ -5,6 +5,10 @@ export const twitterState = { key: 'profile', default: null, }), + defaultProfileNames: atom({ + key: 'defaultProfileNames', + default: null, + }), tweets: atom({ key: 'tweets', default: null, diff --git a/phone/src/apps/twitter/hooks/useTwitterService.ts b/phone/src/apps/twitter/hooks/useTwitterService.ts index b8834c6..29fbb5c 100644 --- a/phone/src/apps/twitter/hooks/useTwitterService.ts +++ b/phone/src/apps/twitter/hooks/useTwitterService.ts @@ -59,6 +59,7 @@ export const useTwitterService = () => { const [currentTweets, setTweets] = useRecoilState(twitterState.tweets); const setFilteredTweets = useSetRecoilState(twitterState.filteredTweets); const setCreateLoading = useSetRecoilState(twitterState.createTweetLoading); + const setDefaultProfileNames = useSetRecoilState(twitterState.defaultProfileNames); const _setTweets = (tweets) => { setTweets(tweets.map(processTweet)); @@ -83,6 +84,8 @@ export const useTwitterService = () => { }; useNuiEvent(APP_TWITTER, 'getOrCreateTwitterProfile', setProfile); + useNuiEvent(APP_TWITTER, 'getOrCreateTwitterProfileNull', setDefaultProfileNames); + useNuiEvent(APP_TWITTER, 'phone:createProfileResult', handleAddAlert); useNuiEvent(APP_TWITTER, 'updateProfileLoading', setUpdateProfileLoading); useNuiEvent(APP_TWITTER, 'updateProfileResult', handleAddAlert); useNuiEvent(APP_TWITTER, 'fetchTweets', _setTweets); diff --git a/phone/src/locale/en.json b/phone/src/locale/en.json index 3722dff..45c25a1 100644 --- a/phone/src/locale/en.json +++ b/phone/src/locale/en.json @@ -81,6 +81,8 @@ "APPS_TWITTER_EDIT_PROFILE_BIO": "Bio", "APPS_TWITTER_EDIT_PROFILE_LOCATION": "Location", "APPS_TWITTER_EDIT_PROFILE_JOB": "Job", + "APPS_TWITTER_CREATE_PROFILE_SUCCESS": "Successfully created your profile", + "APPS_TWITTER_CREATE_PROFILE_FAILURE": "Failed to create your profile", "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!", diff --git a/resources/client/cl_main.ts b/resources/client/cl_main.ts index 0d26289..d97aac9 100644 --- a/resources/client/cl_main.ts +++ b/resources/client/cl_main.ts @@ -17,6 +17,7 @@ function fetchOnInitialize() { emitNet(events.MESSAGES_FETCH_MESSAGE_GROUPS); emitNet(events.TWITTER_GET_OR_CREATE_PROFILE); sendMessage('PHONE', 'setPhoneReady', isPhoneReady); + sendMessage('PHONE', 'phoneConfig', config); } RegisterKeyMapping('phone', 'Open Phone', 'keyboard', 'f1'); diff --git a/resources/client/cl_twitter.ts b/resources/client/cl_twitter.ts index 58dbc29..ef4624c 100644 --- a/resources/client/cl_twitter.ts +++ b/resources/client/cl_twitter.ts @@ -18,6 +18,24 @@ onNet(events.TWITTER_GET_OR_CREATE_PROFILE_FAILURE, () => { sendTwitterMessage(events.TWITTER_GET_OR_CREATE_PROFILE_FAILURE); }); +onNet(events.TWITTER_GET_OR_CREATE_PROFILE_NULL, (defaultProfileNames: string[]): void => { + sendTwitterMessage(events.TWITTER_GET_OR_CREATE_PROFILE_NULL, defaultProfileNames); +}); + +/** + * Twitter create profile + */ +RegisterNuiCallbackType(events.TWITTER_CREATE_PROFILE); +on(`__cfx_nui:${events.TWITTER_CREATE_PROFILE}`, (data: any) => { + sendTwitterMessage(events.TWITTER_UPDATE_PROFILE_LOADING, true); + emitNet(events.TWITTER_CREATE_PROFILE, data); +}); + +onNet(events.TWITTER_CREATE_PROFILE_RESULT, (alert: IAlertProps) => { + sendTwitterMessage(events.TWITTER_CREATE_PROFILE_RESULT, alert); + emitNet(events.TWITTER_GET_OR_CREATE_PROFILE); +}); + /** * Twitter update profile */ diff --git a/resources/server/players/sv_players.ts b/resources/server/players/sv_players.ts index 11b41f0..4d69350 100644 --- a/resources/server/players/sv_players.ts +++ b/resources/server/players/sv_players.ts @@ -89,6 +89,54 @@ export async function getPlayerInfo(identifier: string): Promise (input ? input.replace(/[^0-9a-z]/gi, '') : input); + +async function getCleanedPlayerInfo(identifier: string): Promise { + const { firstname, lastname, phone_number } = await getPlayerInfo(identifier); + return { + firstname: clean(firstname), + lastname: clean(lastname), + phone_number: clean(phone_number), + }; +} + +/** + * Generate a profile name by the player's name and/or phone number + * @param identifier - player's identifier + */ +export async function generateProfileName(identifier: string): Promise { + const { firstname, lastname, phone_number } = await getCleanedPlayerInfo(identifier); + + if (firstname && lastname) { + return `${firstname}_${lastname}`; + } else if (firstname) { + return firstname; + } else if (lastname) { + return lastname; + } else if (phone_number) { + return phone_number; + } + return null; +} + +export async function getDefaultProfileNames(identifier: string): Promise { + const { firstname, lastname, phone_number } = await getCleanedPlayerInfo(identifier); + let defaultProfileNames = []; + + if (firstname && lastname) { + defaultProfileNames.push(`${firstname}_${lastname}`); + } else if (firstname) { + defaultProfileNames.push(firstname); + } else if (lastname) { + defaultProfileNames.push(lastname); + } + + if (phone_number) { + defaultProfileNames.push(phone_number); + } + return defaultProfileNames; +} + // Attach to playerJoining event as this is when // the permanent source is assigned for the user diff --git a/resources/server/sv_main.ts b/resources/server/sv_main.ts index 9ed0de0..8f50896 100644 --- a/resources/server/sv_main.ts +++ b/resources/server/sv_main.ts @@ -1,5 +1,4 @@ import { pool } from './db'; -import config from '../utils/config'; import { getIdentifier, getSource } from './functions'; import { mainLogger } from './sv_logger'; diff --git a/resources/server/sv_twitter.ts b/resources/server/sv_twitter.ts index 0fc9f5a..dd3d151 100644 --- a/resources/server/sv_twitter.ts +++ b/resources/server/sv_twitter.ts @@ -6,6 +6,8 @@ import config from '../utils/config'; import { reportTweetToDiscord } from './discord'; import { mainLogger } from './sv_logger'; import { ResultSetHeader } from 'mysql2'; +import { generateProfileName } from './players/sv_players'; +import { getDefaultProfileNames } from './players/sv_players'; const twitterLogger = mainLogger.child({ module: 'twitter' }); @@ -165,23 +167,19 @@ async function getProfile(identifier: string): Promise { } /** - * Generate a twitter profile name by the player's name + * Create a twitter profile * @param identifier - player's identifier + * @param profileName - profile name to be created + * @returns */ -async function generateProfileName(identifier: string): Promise { - const defaultProfileName = ''; - if (!config.twitter.generateProfileNameFromUsers) return defaultProfileName; - +async function createProfile(identifier: string, profileName: string): Promise { const query = ` - SELECT CONCAT(firstname, '_', lastname) AS profile_name - FROM users - WHERE identifier = ? LIMIT 1 + INSERT INTO npwd_twitter_profiles (identifier, profile_name) + VALUES (?, ?) `; - const [results] = await pool.query(query, [identifier]); - const profileNames = results; - if (profileNames.length === 0) return defaultProfileName; - return profileNames[0].profile_name || defaultProfileName; + await pool.execute(query, [identifier, profileName]); + return await getProfile(identifier); } /** @@ -189,15 +187,17 @@ async function generateProfileName(identifier: string): Promise { * that all player's will receive and can then customize. * @param identifier - player's identifier */ -async function createDefaultProfile(identifier: string): Promise { - const profileName = await generateProfileName(identifier); - const query = ` - INSERT INTO npwd_twitter_profiles (identifier, profile_name) - VALUES (?, ?) - `; +async function createDefaultProfile(identifier: string): Promise { + // case where the server owner wants players to select their own names + if (!config.twitter.generateProfileNameFromUsers) return null; - await pool.execute(query, [identifier, profileName]); - return getProfile(identifier); + const defaultProfileName = await generateProfileName(identifier); + // case where we tried to generate a profile name but failed due to + // some database misconfiguration or error + if (!defaultProfileName) return null; + + twitterLogger.info(`Creating default Twitter profile ${defaultProfileName} for ${identifier}`); + return await createProfile(identifier, defaultProfileName); } /** @@ -316,12 +316,41 @@ onNet(events.TWITTER_GET_OR_CREATE_PROFILE, async () => { try { const profile = await getOrCreateProfile(identifier); - emitNet(events.TWITTER_GET_OR_CREATE_PROFILE_SUCCESS, _source, profile); + + // if we got null from getOrCreateProfile it means it doesn't exist and + // we failed to create it. In this case we pass the UI some default + // profile names it can choose from + if (!profile) { + const defaultProfileNames = await getDefaultProfileNames(identifier); + emitNet(events.TWITTER_GET_OR_CREATE_PROFILE_NULL, _source, defaultProfileNames); + } else { + emitNet(events.TWITTER_GET_OR_CREATE_PROFILE_SUCCESS, _source, profile); + } } catch (e) { twitterLogger.error(`Failed to get or create profile, ${e.message}`, { source: _source, }); - emitNet(events.TWITTER_GET_OR_CREATE_PROFILE_FAILURE, _source); + emitNet(events.TWITTER_GET_OR_CREATE_PROFILE_FAILURE, _source, null); + } +}); + +onNet(events.TWITTER_CREATE_PROFILE, async (profile: Profile) => { + const _source = getSource(); + try { + const identifier = getIdentifier(_source); + await createProfile(identifier, profile.profile_name); + emitNet(events.TWITTER_CREATE_PROFILE_RESULT, _source, { + message: 'TWITTER_CREATE_PROFILE_SUCCESS', + type: 'success', + }); + } catch (e) { + twitterLogger.error(`Failed to create twitter profile: ${e.message}`, { + source: _source, + }); + emitNet(events.TWITTER_CREATE_PROFILE_RESULT, _source, { + message: 'TWITTER_CREATE_PROFILE_FAILURE', + type: 'error', + }); } }); @@ -350,6 +379,13 @@ onNet(events.TWITTER_FETCH_TWEETS, async () => { try { const identifier = getIdentifier(_source); const profile = await getProfile(identifier); + if (!profile) { + twitterLogger.warn( + `Aborted fetching tweets for user ${identifier} because they do not have a profile.`, + ); + return; + } + const tweets = await fetchAllTweets(profile.id); emitNet(events.TWITTER_FETCH_TWEETS_SUCCESS, _source, tweets); } catch (e) { diff --git a/resources/utils/events.ts b/resources/utils/events.ts index b2cfe88..ba9615b 100644 --- a/resources/utils/events.ts +++ b/resources/utils/events.ts @@ -8,7 +8,10 @@ export default { TWITTER_FETCH_TWEETS_FILTERED_FAILURE: 'fetchTweetsFilteredFailed', TWITTER_GET_OR_CREATE_PROFILE: 'phone:getOrCreateTwitterProfile', TWITTER_GET_OR_CREATE_PROFILE_SUCCESS: 'getOrCreateTwitterProfile', + TWITTER_GET_OR_CREATE_PROFILE_NULL: 'getOrCreateTwitterProfileNull', TWITTER_GET_OR_CREATE_PROFILE_FAILURE: 'getOrCreateTwitterProfileFailed', + TWITTER_CREATE_PROFILE: 'phone:createTwitterProfile', + TWITTER_CREATE_PROFILE_RESULT: 'phone:createProfileResult', TWITTER_UPDATE_PROFILE: 'phone:updateTwitterProfile', TWITTER_UPDATE_PROFILE_LOADING: 'updateProfileLoading', TWITTER_UPDATE_PROFILE_RESULT: 'updateProfileResult', From 21a2f8912d07fb9b22af241462b75df80cf5713c Mon Sep 17 00:00:00 2001 From: Kire Date: Tue, 16 Mar 2021 23:20:33 -0400 Subject: [PATCH 3/6] initial PR review changes --- .../apps/twitter/components/TwitterApp.tsx | 200 +++++++++--------- .../profile/DefaultProfilePrompt.tsx | 1 - resources/server/sv_twitter.ts | 6 +- 3 files changed, 103 insertions(+), 104 deletions(-) diff --git a/phone/src/apps/twitter/components/TwitterApp.tsx b/phone/src/apps/twitter/components/TwitterApp.tsx index 2f6f12d..a95722f 100644 --- a/phone/src/apps/twitter/components/TwitterApp.tsx +++ b/phone/src/apps/twitter/components/TwitterApp.tsx @@ -78,104 +78,104 @@ export const TwitterApp = () => { ); }; -// InjectDebugData([ -// { -// app: 'TWITTER', -// method: 'getOrCreateTwitterProfile', -// data: { -// profile_name: 'Angular_Dev', -// bio: 'Angular FTW', -// job: 'Googler @ Google', -// location: '1600 Amphitheatre Parkway, CA, USA', -// avatar_url: 'https://i.tasoagc.dev/i9Ig', -// }, -// }, -// { -// app: 'TWITTER', -// method: 'fetchTweets', -// data: [ -// { -// id: 59, -// message: 'XSS\n\nBro', -// avatar_url: 'http://i.tasoagc.dev/jtKL', -// seconds_since_tweet: 500, -// isMine: false, -// isRetweetedByPlayer: true, -// updateAt: '2021-02-08 14:37:46', -// visible: 1, -// likes: 50000, -// profile_name: 'jQuery Main', -// images: 'http://i.tasoagc.dev/0xdp', -// }, -// { -// id: 2, -// message: 'GCPhone will never die', -// avatar_url: 'http://i.tasoagc.dev/jtKL', -// seconds_since_tweet: 500, -// isMine: false, -// updateAt: '2021-02-08 14:37:46', -// visible: 1, -// likes: 50000, -// profile_name: 'GCPhone_v9999', -// images: 'http://i.tasoagc.dev/0xdp', -// }, -// { -// id: 1, -// message: 'I love angular', -// avatar_url: 'https://i.tasoagc.dev/i9Ig', -// seconds_since_tweet: 800, -// isMine: true, -// updateAt: '2021-02-08 14:37:46', -// visible: 1, -// likes: 50000, -// profile_name: 'Angular_Dev', -// images: 'http://i.tasoagc.dev/i9Ig', -// }, -// { -// id: 3, -// message: -// 'Anyways uhm... I bought a whole bunch of shungite, rocks, ' + -// 'do you know what shungite is? Anybody know what shungite is. No, no ' + -// 'Suge Knight, I think hes locked up in prison.', -// avatar_url: 'http://i.tasoagc.dev/0v64', -// seconds_since_tweet: 800, -// isMine: true, -// updateAt: '2021-02-08 14:37:46', -// visible: 1, -// likes: 50000, -// profile_name: 'The_Doc', -// }, -// ], -// }, -// ]); +InjectDebugData([ + { + app: 'TWITTER', + method: 'getOrCreateTwitterProfile', + data: { + profile_name: 'Angular_Dev', + bio: 'Angular FTW', + job: 'Googler @ Google', + location: '1600 Amphitheatre Parkway, CA, USA', + avatar_url: 'https://i.tasoagc.dev/i9Ig', + }, + }, + { + app: 'TWITTER', + method: 'fetchTweets', + data: [ + { + id: 59, + message: 'XSS\n\nBro', + avatar_url: 'http://i.tasoagc.dev/jtKL', + seconds_since_tweet: 500, + isMine: false, + isRetweetedByPlayer: true, + updateAt: '2021-02-08 14:37:46', + visible: 1, + likes: 50000, + profile_name: 'jQuery Main', + images: 'http://i.tasoagc.dev/0xdp', + }, + { + id: 2, + message: 'GCPhone will never die', + avatar_url: 'http://i.tasoagc.dev/jtKL', + seconds_since_tweet: 500, + isMine: false, + updateAt: '2021-02-08 14:37:46', + visible: 1, + likes: 50000, + profile_name: 'GCPhone_v9999', + images: 'http://i.tasoagc.dev/0xdp', + }, + { + id: 1, + message: 'I love angular', + avatar_url: 'https://i.tasoagc.dev/i9Ig', + seconds_since_tweet: 800, + isMine: true, + updateAt: '2021-02-08 14:37:46', + visible: 1, + likes: 50000, + profile_name: 'Angular_Dev', + images: 'http://i.tasoagc.dev/i9Ig', + }, + { + id: 3, + message: + 'Anyways uhm... I bought a whole bunch of shungite, rocks, ' + + 'do you know what shungite is? Anybody know what shungite is. No, no ' + + 'Suge Knight, I think hes locked up in prison.', + avatar_url: 'http://i.tasoagc.dev/0v64', + seconds_since_tweet: 800, + isMine: true, + updateAt: '2021-02-08 14:37:46', + visible: 1, + likes: 50000, + profile_name: 'The_Doc', + }, + ], + }, +]); -// InjectDebugData( -// [ -// { -// app: 'TWITTER', -// method: 'createTweetBroadcast', -// data: { -// id: 111, -// profile_id: 111, -// profile_name: 'Kidz', -// isMine: false, -// isReported: false, -// message: 'I have tweeted this because Im badass', -// }, -// }, -// { -// app: 'TWITTER', -// method: 'createTweetBroadcast', -// data: { -// id: 111, -// profile_id: 111, -// profile_name: 'ItsChip', -// isMine: false, -// isReported: false, -// message: -// 'I have tweeted this because Im even more badass... And I need this to be longer so I will continue to write', -// }, -// }, -// ], -// 3000, -// ); +InjectDebugData( + [ + { + app: 'TWITTER', + method: 'createTweetBroadcast', + data: { + id: 111, + profile_id: 111, + profile_name: 'Kidz', + isMine: false, + isReported: false, + message: 'I have tweeted this because Im badass', + }, + }, + { + app: 'TWITTER', + method: 'createTweetBroadcast', + data: { + id: 111, + profile_id: 111, + profile_name: 'ItsChip', + isMine: false, + isReported: false, + message: + 'I have tweeted this because Im even more badass... And I need this to be longer so I will continue to write', + }, + }, + ], + 3000, +); diff --git a/phone/src/apps/twitter/components/profile/DefaultProfilePrompt.tsx b/phone/src/apps/twitter/components/profile/DefaultProfilePrompt.tsx index 304e6a2..0b4ecce 100644 --- a/phone/src/apps/twitter/components/profile/DefaultProfilePrompt.tsx +++ b/phone/src/apps/twitter/components/profile/DefaultProfilePrompt.tsx @@ -25,7 +25,6 @@ const useStyles = makeStyles((theme) => ({ padding: '15px', }, formControl: { - // margin: theme.spacing(1), width: '100%', }, })); diff --git a/resources/server/sv_twitter.ts b/resources/server/sv_twitter.ts index dd3d151..221b0bd 100644 --- a/resources/server/sv_twitter.ts +++ b/resources/server/sv_twitter.ts @@ -1,3 +1,5 @@ +import { ResultSetHeader } from 'mysql2'; + import { pool } from './db'; import { getIdentifier, getSource } from './functions'; import { NewTweet, Tweet, Profile } from '../../typings/twitter'; @@ -5,9 +7,7 @@ import events from '../utils/events'; import config from '../utils/config'; import { reportTweetToDiscord } from './discord'; import { mainLogger } from './sv_logger'; -import { ResultSetHeader } from 'mysql2'; -import { generateProfileName } from './players/sv_players'; -import { getDefaultProfileNames } from './players/sv_players'; +import { generateProfileName, getDefaultProfileNames } from './players/sv_players'; const twitterLogger = mainLogger.child({ module: 'twitter' }); From f3f055944ca6d96543329eadc91b8ac66ad82b20 Mon Sep 17 00:00:00 2001 From: Kire Date: Tue, 16 Mar 2021 23:40:53 -0400 Subject: [PATCH 4/6] handle twitter profile updating --- resources/client/cl_twitter.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/resources/client/cl_twitter.ts b/resources/client/cl_twitter.ts index ef4624c..c484582 100644 --- a/resources/client/cl_twitter.ts +++ b/resources/client/cl_twitter.ts @@ -33,6 +33,7 @@ on(`__cfx_nui:${events.TWITTER_CREATE_PROFILE}`, (data: any) => { onNet(events.TWITTER_CREATE_PROFILE_RESULT, (alert: IAlertProps) => { sendTwitterMessage(events.TWITTER_CREATE_PROFILE_RESULT, alert); + sendTwitterMessage(events.TWITTER_UPDATE_PROFILE_LOADING, false); emitNet(events.TWITTER_GET_OR_CREATE_PROFILE); }); @@ -47,6 +48,7 @@ on(`__cfx_nui:${events.TWITTER_UPDATE_PROFILE}`, (data: string) => { onNet(events.TWITTER_UPDATE_PROFILE_RESULT, (alert: IAlertProps) => { sendTwitterMessage(events.TWITTER_UPDATE_PROFILE_RESULT, alert); + sendTwitterMessage(events.TWITTER_UPDATE_PROFILE_LOADING, false); emitNet(events.TWITTER_GET_OR_CREATE_PROFILE); }); From c8f680fdfb72c6881687b84170f7ba64a1bb4566 Mon Sep 17 00:00:00 2001 From: Kire Date: Wed, 17 Mar 2021 18:11:57 -0400 Subject: [PATCH 5/6] update for PR feedback --- phone/src/apps/twitter/components/TwitterApp.tsx | 4 ---- .../apps/twitter/components/profile/DefaultProfilePrompt.tsx | 5 +---- phone/src/apps/twitter/components/profile/ProfilePrompt.tsx | 1 - resources/server/sv_twitter.ts | 2 +- 4 files changed, 2 insertions(+), 10 deletions(-) diff --git a/phone/src/apps/twitter/components/TwitterApp.tsx b/phone/src/apps/twitter/components/TwitterApp.tsx index a95722f..860b4b9 100644 --- a/phone/src/apps/twitter/components/TwitterApp.tsx +++ b/phone/src/apps/twitter/components/TwitterApp.tsx @@ -20,10 +20,6 @@ import { useProfile } from '../hooks/useProfile'; import ProfilePrompt from './profile/ProfilePrompt'; import InjectDebugData from '../../../os/debug/InjectDebugData'; import { TwitterThemeProvider } from '../providers/TwitterThemeProvider'; -import DefaultProfilePrompt from './profile/DefaultProfilePrompt'; -import { useRecoilValue } from 'recoil'; -import { twitterState } from '../hooks/state'; -import { Profile } from '../../../../../typings/twitter'; const useStyles = makeStyles(() => ({ backgroundModal: { diff --git a/phone/src/apps/twitter/components/profile/DefaultProfilePrompt.tsx b/phone/src/apps/twitter/components/profile/DefaultProfilePrompt.tsx index 0b4ecce..5dea5a7 100644 --- a/phone/src/apps/twitter/components/profile/DefaultProfilePrompt.tsx +++ b/phone/src/apps/twitter/components/profile/DefaultProfilePrompt.tsx @@ -1,13 +1,10 @@ -import React, { useState } from 'react'; +import React from 'react'; import { makeStyles } from '@material-ui/core/styles'; import InputLabel from '@material-ui/core/InputLabel'; -import MenuItem from '@material-ui/core/MenuItem'; import FormControl from '@material-ui/core/FormControl'; import Select from '@material-ui/core/Select'; import { useTranslation } from 'react-i18next'; -import Nui from '../../../../os/nui-events/utils/Nui'; -import { useProfile } from '../../hooks/useProfile'; import ProfileUpdateButton from '../buttons/ProfileUpdateButton'; interface IProps { diff --git a/phone/src/apps/twitter/components/profile/ProfilePrompt.tsx b/phone/src/apps/twitter/components/profile/ProfilePrompt.tsx index 27286b9..5d98dd1 100644 --- a/phone/src/apps/twitter/components/profile/ProfilePrompt.tsx +++ b/phone/src/apps/twitter/components/profile/ProfilePrompt.tsx @@ -9,7 +9,6 @@ import DefaultProfilePrompt from './DefaultProfilePrompt'; import ProfileUpdateButton from '../buttons/ProfileUpdateButton'; import { useRecoilValue } from 'recoil'; import { twitterState } from '../../hooks/state'; -import { useSettings } from '../../../settings/hooks/useSettings'; import { usePhone } from '../../../../os/phone/hooks/usePhone'; const useStyles = makeStyles((theme) => ({ diff --git a/resources/server/sv_twitter.ts b/resources/server/sv_twitter.ts index 221b0bd..f2f5271 100644 --- a/resources/server/sv_twitter.ts +++ b/resources/server/sv_twitter.ts @@ -330,7 +330,7 @@ onNet(events.TWITTER_GET_OR_CREATE_PROFILE, async () => { twitterLogger.error(`Failed to get or create profile, ${e.message}`, { source: _source, }); - emitNet(events.TWITTER_GET_OR_CREATE_PROFILE_FAILURE, _source, null); + emitNet(events.TWITTER_GET_OR_CREATE_PROFILE_FAILURE, _source); } }); From fe1e685f6f4f05afa30bd72d75a07ece925e4a10 Mon Sep 17 00:00:00 2001 From: Kire Date: Mon, 22 Mar 2021 10:37:56 -0400 Subject: [PATCH 6/6] fix(phone/twitter): Fix crash on broadcast with no profile set --- phone/src/apps/twitter/hooks/useTwitterNotifications.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/phone/src/apps/twitter/hooks/useTwitterNotifications.ts b/phone/src/apps/twitter/hooks/useTwitterNotifications.ts index 046b35f..a754b20 100644 --- a/phone/src/apps/twitter/hooks/useTwitterNotifications.ts +++ b/phone/src/apps/twitter/hooks/useTwitterNotifications.ts @@ -36,7 +36,9 @@ export const useTwitterNotifications = () => { // defend against this case if (!profile) return; - const currentProfileName = profile.profile_name; + // Prevents a crash if there is no current profile + // set and a Twitter broadcast occurs. + const currentProfileName = profile?.profile_name; // we don't want notifications of our own tweets if (currentProfileName === profile_name) return;