From a52ffc4e79940a65667e18379de657353930474c Mon Sep 17 00:00:00 2001 From: Erik Date: Sat, 20 Mar 2021 18:17:14 -0400 Subject: [PATCH] Feature/twitter refresh (#160) * receive and process broadcasted tweets to the clients * removing profile fetch and cleaning up unused functions * defend against null profile * move defenseive check to top pof notifications * fix bug where delete was visible for non owners * remove polling implementation --- .../apps/twitter/components/TwitterApp.tsx | 16 --------- .../twitter/hooks/useTwitterNotifications.ts | 14 +++++--- .../apps/twitter/hooks/useTwitterService.ts | 36 ++++++++++++++++--- typings/twitter.ts | 9 +++++ 4 files changed, 50 insertions(+), 25 deletions(-) diff --git a/phone/src/apps/twitter/components/TwitterApp.tsx b/phone/src/apps/twitter/components/TwitterApp.tsx index d982637..aea19f1 100644 --- a/phone/src/apps/twitter/components/TwitterApp.tsx +++ b/phone/src/apps/twitter/components/TwitterApp.tsx @@ -34,28 +34,12 @@ const useStyles = makeStyles(() => ({ }, })); -const TWEETS_REFRESH_RATE = 15000; // TODO move this to twitter config - export const TwitterApp = () => { const classes = useStyles(); const { modalVisible, setModalVisible } = useModal(); const [activePage, setActivePage] = useState(0); const { profile } = useProfile(); - useEffect(() => { - // this is a polling implementation. It is possible that - // there is some interaction where, on a new tweet, all - // clients are sent the updated query data. Until that can - // be accomplished this is naive but robust. - // - // TODO don't call fetchTweets - implement a function that only - // returns tweets that we don't already have - const timeout = window.setTimeout(() => { - Nui.send('phone:fetchTweets', {}); - }, TWEETS_REFRESH_RATE); - return () => window.clearTimeout(timeout); - }, []); - // 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()); diff --git a/phone/src/apps/twitter/hooks/useTwitterNotifications.ts b/phone/src/apps/twitter/hooks/useTwitterNotifications.ts index c0a320f..046b35f 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, useRecoilValue } from 'recoil'; +import { Tweet, Profile } from '../../../../../typings/twitter'; import { useApp } from '../../../os/apps/hooks/useApps'; import { useNotifications } from '../../../os/notifications/hooks/useNotifications'; @@ -11,7 +12,7 @@ import { twitterState } from './state'; const NOTIFICATION_ID = 'twitter:broadcast'; function isMentioned(profileName: string, message: string) { - return !message.toLowerCase().includes(profileName.toLowerCase()); + return message.toLowerCase().includes(profileName.toLowerCase()); } export const useTwitterNotifications = () => { @@ -24,14 +25,19 @@ export const useTwitterNotifications = () => { const { icon, notificationIcon } = useApp('TWITTER'); const [unreadCount, setUnreadCount] = useRecoilState(twitterState.unreadTweetsCount); - const profile = useRecoilValue(twitterState.profile); + const profile: Profile | null = useRecoilValue(twitterState.profile); - const setNotification = ({ profile_name, message, isRetweet }) => { + const setNotification = ({ profile_name, message, isRetweet }: Tweet) => { const titleStr = isRetweet ? 'APPS_TWITTER_NEW_RETWEET_BROADCAST' : 'APPS_TWITTER_NEW_BROADCAST'; + // profile defaults to null, if for some reason it is not initialized + // defend against this case + if (!profile) return; + const currentProfileName = profile.profile_name; + // we don't want notifications of our own tweets if (currentProfileName === profile_name) return; @@ -39,7 +45,7 @@ export const useTwitterNotifications = () => { // mentioned in if ( settings.TWITTER_notiFilter.value === SETTING_MENTIONS && - isMentioned(currentProfileName, message) + !isMentioned(currentProfileName, message) ) return; diff --git a/phone/src/apps/twitter/hooks/useTwitterService.ts b/phone/src/apps/twitter/hooks/useTwitterService.ts index 3ead35d..b8834c6 100644 --- a/phone/src/apps/twitter/hooks/useTwitterService.ts +++ b/phone/src/apps/twitter/hooks/useTwitterService.ts @@ -1,4 +1,4 @@ -import { useSetRecoilState } from 'recoil'; +import { useRecoilState, useSetRecoilState } from 'recoil'; import { v4 as uuidv4 } from 'uuid'; import { useNuiEvent } from '../../../os/nui-events/hooks/useNuiEvent'; @@ -8,13 +8,14 @@ import { twitterState } from './state'; import { IAlert, useSnackbar } from '../../../ui/hooks/useSnackbar'; import { useTwitterNotifications } from './useTwitterNotifications'; import { useTranslation } from 'react-i18next'; +import { Tweet, FormattedTweet, Profile } from '../../../../../typings/twitter'; /** * Perform all necessary processing/transforms from the raw database * data to what the frontend expects * @param {object} tweet - full JSON returned from the server/database */ -function processTweet(tweet) { +function processTweet(tweet: Tweet): FormattedTweet { // we store images in the database as a varchar field with // comma separated links to images, so here we split them // back into their distinct members @@ -23,6 +24,23 @@ function processTweet(tweet) { return { ...tweet, images }; } +/** + * in the case a tweet is broadcasted we can make some assumptions + * that it is brand new and hasn't been liked. We should also need + * to overwrite isMine since that will be true (sourced from another + * player's tweet) +} + * @param tweet - tweet to process + * @param playerIdentifier - the current player's identifier + * @returns Formatted Tweet + */ +function processBroadcastedTweet(tweet: Tweet, profile: Profile): FormattedTweet { + const processedTweet = processTweet(tweet); + const isLiked = false; + const isMine = profile?.identifier === tweet.identifier; + return { ...processedTweet, isMine, isLiked }; +} + /** * Service to handle all NUI <> client interactions. We take * this opportunity to act as a middleware layer between the @@ -35,10 +53,10 @@ export const useTwitterService = () => { const { setNotification } = useTwitterNotifications(); const { t } = useTranslation(); - const setProfile = useSetRecoilState(twitterState.profile); + const [profile, setProfile] = useRecoilState(twitterState.profile); const setUpdateProfileLoading = useSetRecoilState(twitterState.updateProfileLoading); - const setTweets = useSetRecoilState(twitterState.tweets); + const [currentTweets, setTweets] = useRecoilState(twitterState.tweets); const setFilteredTweets = useSetRecoilState(twitterState.filteredTweets); const setCreateLoading = useSetRecoilState(twitterState.createTweetLoading); @@ -49,6 +67,14 @@ export const useTwitterService = () => { setFilteredTweets(tweets.map(processTweet)); }; + // these tweets are coming directly from other player clients + const handleTweetBroadcast = (tweet: Tweet) => { + setNotification(tweet); + const processedTweet = processBroadcastedTweet(tweet, profile); + const updatedTweets = [processedTweet].concat(currentTweets); + setTweets(updatedTweets); + }; + const handleAddAlert = ({ message, type }: IAlert) => { addAlert({ message: t(`APPS_${message}`), @@ -63,6 +89,6 @@ export const useTwitterService = () => { useNuiEvent(APP_TWITTER, 'fetchTweetsFiltered', _setFilteredTweets); useNuiEvent(APP_TWITTER, 'createTweetLoading', setCreateLoading); useNuiEvent(APP_TWITTER, 'createTweetResult', handleAddAlert); - useNuiEvent(APP_TWITTER, 'createTweetBroadcast', setNotification); + useNuiEvent(APP_TWITTER, 'createTweetBroadcast', handleTweetBroadcast); useNuiEvent(APP_TWITTER, 'phone:retweetExists', handleAddAlert); }; diff --git a/typings/twitter.ts b/typings/twitter.ts index 7f573db..3a23f69 100644 --- a/typings/twitter.ts +++ b/typings/twitter.ts @@ -11,6 +11,7 @@ export interface Tweet extends NewTweet { profile_name: string; profile_id: number; id: number; + identifier: string; isMine: boolean; isLiked: boolean; isReported: boolean; @@ -26,6 +27,14 @@ export interface Tweet extends NewTweet { updatedAt: string; } +export interface Image { + id: string; + link: string; +} +export interface FormattedTweet extends Omit { + images: Image[]; +} + export interface Profile { id: number; profile_name: string;