mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-04 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
@@ -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);
|
||||
};
|
||||
Reference in new issue
Block a user