mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-03 14:38:45 +00:00
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
This commit is contained in:
4 files changed
+50
-25
No files matched your search
@@ -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());
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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<Profile>(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);
|
||||
};
|
||||
@@ -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<Tweet, 'images'> {
|
||||
images: Image[];
|
||||
}
|
||||
|
||||
export interface Profile {
|
||||
id: number;
|
||||
profile_name: string;
|
||||
|
||||
Reference in new issue
Block a user