From 0203ebbe51d4107fdda9527f9249043b5ceea63e Mon Sep 17 00:00:00 2001 From: Erik Date: Sun, 7 Mar 2021 10:25:10 -0500 Subject: [PATCH] Feature/Twitter Notification Filters (#147) Feature: Twitter notification update --- phone/src/Phone.tsx | 2 +- .../apps/settings/components/SettingsApp.tsx | 15 ++++++++- phone/src/apps/settings/hooks/useSettings.ts | 7 ++-- .../apps/twitter/components/TwitterApp.tsx | 1 - .../twitter/components/profile/Profile.tsx | 2 +- phone/src/apps/twitter/hooks/state.ts | 2 +- .../twitter/hooks/useTwitterNotifications.ts | 33 ++++++++++++++----- .../apps/twitter/hooks/useTwitterService.ts | 4 ++- phone/src/apps/twitter/utils/constants.ts | 3 ++ phone/src/common/typings/twitter.ts | 4 ++- phone/src/config/default.json | 14 ++++++++ phone/src/locale/en.json | 1 + phone/src/ui/hooks/useContextMenu.tsx | 4 +-- resources/server/sv_main.ts | 10 ++++-- resources/server/sv_twitter.ts | 6 ++-- 15 files changed, 85 insertions(+), 23 deletions(-) diff --git a/phone/src/Phone.tsx b/phone/src/Phone.tsx index 62db9b2..593d6fb 100644 --- a/phone/src/Phone.tsx +++ b/phone/src/Phone.tsx @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { useEffect } from 'react'; import './Phone.css'; import './i18n'; import { Route } from 'react-router-dom'; diff --git a/phone/src/apps/settings/components/SettingsApp.tsx b/phone/src/apps/settings/components/SettingsApp.tsx index ff5b48d..2cbd013 100644 --- a/phone/src/apps/settings/components/SettingsApp.tsx +++ b/phone/src/apps/settings/components/SettingsApp.tsx @@ -11,6 +11,7 @@ import { SettingItem, SettingItemIconAction, SettingItemSlider } from './Setting import { useTranslation } from 'react-i18next'; import { + FilterList, Brush, Wallpaper, Phone, @@ -23,7 +24,6 @@ import { import { ListSubheader } from '@material-ui/core'; import { useSettings } from '../hooks/useSettings'; -// import { useSnackbar } from '../../../ui/hooks/useSnackbar'; const SubHeaderComp = (props: { text: string }) => ( @@ -73,6 +73,12 @@ export const SettingsApp = () => { ), ); + const twitterNotificationFilters = config.notiFilters.map( + MapSettingItem(settings.TWITTER_notiFilter, (val: SettingOption) => + handleSettingChange('TWITTER_notiFilter', val), + ), + ); + const handleCopyPhoneNumber = () => { // TODO: Dependent on #132 merge // setClipboard(simcard.number) @@ -159,6 +165,13 @@ export const SettingsApp = () => { /> }> + } + /> ; TWITTER_notiSound: SettingOption; TWITTER_notiSoundVol: number; TWITTER_notifyNewFeedTweet: boolean; } -export const useSettings = () => { +export const useSettings = (): [IPhoneSettings, SetterOrUpdater] => { return useRecoilState(settingsState); }; diff --git a/phone/src/apps/twitter/components/TwitterApp.tsx b/phone/src/apps/twitter/components/TwitterApp.tsx index 200f1e0..09151b1 100644 --- a/phone/src/apps/twitter/components/TwitterApp.tsx +++ b/phone/src/apps/twitter/components/TwitterApp.tsx @@ -43,7 +43,6 @@ export const TwitterApp = () => { const { profile } = useProfile(); useEffect(() => { - Nui.send('phone:getOrCreateTwitterProfile', {}); // this is a polling implementation. It is possible that // there is some interaction where, on a new tweet, all diff --git a/phone/src/apps/twitter/components/profile/Profile.tsx b/phone/src/apps/twitter/components/profile/Profile.tsx index 326e002..9eb27f1 100644 --- a/phone/src/apps/twitter/components/profile/Profile.tsx +++ b/phone/src/apps/twitter/components/profile/Profile.tsx @@ -1,4 +1,4 @@ -import React, { useState } from 'react'; +import React, { useState, useEffect } from 'react'; import { makeStyles } from '@material-ui/core/styles'; import { useTranslation } from 'react-i18next'; diff --git a/phone/src/apps/twitter/hooks/state.ts b/phone/src/apps/twitter/hooks/state.ts index cd3aa07..e97d497 100644 --- a/phone/src/apps/twitter/hooks/state.ts +++ b/phone/src/apps/twitter/hooks/state.ts @@ -3,7 +3,7 @@ import { atom } from 'recoil'; export const twitterState = { profile: atom({ key: 'profile', - default: {}, + default: null, }), tweets: atom({ key: 'tweets', diff --git a/phone/src/apps/twitter/hooks/useTwitterNotifications.ts b/phone/src/apps/twitter/hooks/useTwitterNotifications.ts index a1d7b6d..87cccd8 100644 --- a/phone/src/apps/twitter/hooks/useTwitterNotifications.ts +++ b/phone/src/apps/twitter/hooks/useTwitterNotifications.ts @@ -1,32 +1,49 @@ import { useTranslation } from 'react-i18next'; import { useHistory } from 'react-router-dom'; -import { useRecoilState } from 'recoil'; import { Tweet } from '../../../common/typings/twitter'; +import { useRecoilState, useRecoilValue } from 'recoil'; + import { useApp } from '../../../os/apps/hooks/useApps'; import { useNotifications } from '../../../os/notifications/hooks/useNotifications'; +import { useSettings } from '../../settings/hooks/useSettings'; +import { SETTING_MENTIONS } from '../utils/constants'; import { twitterState } from './state'; const NOTIFICATION_ID = 'twitter:broadcast'; +function isMentioned(profileName: string, message: string) { + return !message.toLowerCase().includes(profileName.toLowerCase()); +} + export const useTwitterNotifications = () => { + const [settings] = useSettings(); const { t } = useTranslation(); const history = useHistory(); - const { - addNotificationAlert, - addNotification, - hasNotification, - removeId, - } = useNotifications(); + const { addNotificationAlert, addNotification, hasNotification, removeId } = useNotifications(); const { icon, notificationIcon } = useApp('TWITTER'); const [unreadCount, setUnreadCount] = useRecoilState(twitterState.unreadTweetsCount); + const profile = useRecoilValue(twitterState.profile); const setNotification = ({ profile_name, message, isRetweet }) => { - const titleStr = isRetweet + const titleStr = isRetweet ? 'APPS_TWITTER_NEW_RETWEET_BROADCAST' : 'APPS_TWITTER_NEW_BROADCAST'; + + const currentProfileName = profile.profile_name; + // we don't want notifications of our own tweets + if (currentProfileName === profile_name) return; + + // if the player only wants notifications on tweets they are + // mentioned in + if ( + settings.TWITTER_notiFilter.value === SETTING_MENTIONS && + isMentioned(currentProfileName, message) + ) + return; + const notification = { app: 'TWITTER', id: NOTIFICATION_ID, diff --git a/phone/src/apps/twitter/hooks/useTwitterService.ts b/phone/src/apps/twitter/hooks/useTwitterService.ts index 0cd2938..30cfb0b 100644 --- a/phone/src/apps/twitter/hooks/useTwitterService.ts +++ b/phone/src/apps/twitter/hooks/useTwitterService.ts @@ -1,7 +1,9 @@ +import { useEffect } from 'react'; import { useSetRecoilState } from 'recoil'; import { v4 as uuidv4 } from 'uuid'; import { useNuiEvent } from '../../../os/nui-events/hooks/useNuiEvent'; +import Nui from '../../../os/nui-events/utils/Nui'; import { IMAGE_DELIMITER } from '../utils/images'; import { APP_TWITTER } from '../utils/constants'; import { twitterState } from './state'; @@ -64,5 +66,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) + useNuiEvent(APP_TWITTER, 'phone:retweetExists', handleAddAlert); }; diff --git a/phone/src/apps/twitter/utils/constants.ts b/phone/src/apps/twitter/utils/constants.ts index 42aed19..ec04269 100644 --- a/phone/src/apps/twitter/utils/constants.ts +++ b/phone/src/apps/twitter/utils/constants.ts @@ -1,3 +1,6 @@ export const IMG_DEFAULT_AVATAR = './media/twitter/egg.png'; export const IMG_INVALID_AVATAR = './media/twitter/invalid.png'; export const APP_TWITTER = 'TWITTER'; + +export const SETTING_MENTIONS = 'mentions'; +export const SETTINGS_ALL_TWEETS = 'all'; diff --git a/phone/src/common/typings/twitter.ts b/phone/src/common/typings/twitter.ts index 7acd4e5..7f573db 100644 --- a/phone/src/common/typings/twitter.ts +++ b/phone/src/common/typings/twitter.ts @@ -1,10 +1,12 @@ - export interface NewTweet { message: string; images?: string; retweet?: number; } +export type SETTING_MENTIONS = 'mentions'; +export type SETTINGS_ALL_TWEETS = 'all'; + export interface Tweet extends NewTweet { profile_name: string; profile_id: number; diff --git a/phone/src/config/default.json b/phone/src/config/default.json index 70c7abb..fdc03a0 100644 --- a/phone/src/config/default.json +++ b/phone/src/config/default.json @@ -87,6 +87,16 @@ "value": "99kr" } ], + "notiFilters": [ + { + "label": "All Tweets", + "value": "all" + }, + { + "label": "Only Mentions", + "value": "mentions" + } + ], "defaultSettings": { "wallpaper": { "label": "Project Error", @@ -114,6 +124,10 @@ }, "ringtoneVol": 50, "notiSoundVol": 50, + "TWITTER_notiFilter": { + "label": "All Tweets", + "value": "all" + }, "TWITTER_notiSound": { "label": "Online", "value": "online" diff --git a/phone/src/locale/en.json b/phone/src/locale/en.json index e46ad63..72e30e6 100644 --- a/phone/src/locale/en.json +++ b/phone/src/locale/en.json @@ -117,6 +117,7 @@ "APPS_SETTINGS_OPTION_ZOOM": "Zoom", "APPS_SETTINGS_OPTION_RINGTONE": "Ringtone", "APPS_SETTINGS_OPTION_RINGTONEVOL": "Ringtone Volume", + "APPS_SETTINGS_OPTION_NOTIFICATION_FILTER": "Notification Filters", "APPS_SETTINGS_OPTION_NOTIFICATION": "Notification", "APPS_SETTINGS_OPTION_NOTIFICATIONVOL": "Notification Volume", diff --git a/phone/src/ui/hooks/useContextMenu.tsx b/phone/src/ui/hooks/useContextMenu.tsx index 2a20649..f15723f 100644 --- a/phone/src/ui/hooks/useContextMenu.tsx +++ b/phone/src/ui/hooks/useContextMenu.tsx @@ -12,9 +12,9 @@ export const MapStringOptions = (current, onClick) => (string) => { }; }; -export interface SettingOption { +export interface SettingOption { label: string; - value: string | number; + value: T | string | number; } export const MapSettingItem = (current: SettingOption, onClick: Function) => ( diff --git a/resources/server/sv_main.ts b/resources/server/sv_main.ts index b588ec3..1258f62 100644 --- a/resources/server/sv_main.ts +++ b/resources/server/sv_main.ts @@ -2,7 +2,9 @@ import { ESX } from './server'; import { pool } from './db'; import config from '../utils/config'; import { getIdentifier, getIdentifierByPhoneNumber, getSource, usePhoneNumber } from './functions'; +import { getOrCreateProfile } from './sv_twitter'; import { mainLogger } from './sv_logger'; +import events from '../utils/events'; //db = DatabaseConfig //helper variable for use in server function @@ -63,14 +65,18 @@ async function getCredentials(identifier: string): Promise { onNet('esx:playerLoaded', async (playerId: number, xPlayer: any) => { const _source = getSource(); + const identifier = xPlayer.identifier; try { - const identifier = xPlayer.identifier; await generatePhoneNumber(identifier); } catch (e) { mainLogger.error(`Failed to generate a phone number, ${e.message}`, { source: _source, }); } + + // make sure a twitter profile exists for this user before + // we load the phone + await getOrCreateProfile(identifier); }); onNet('phone:getCredentials', async () => { @@ -84,4 +90,4 @@ onNet('phone:getCredentials', async () => { source: _source, }); } -}); +}); \ No newline at end of file diff --git a/resources/server/sv_twitter.ts b/resources/server/sv_twitter.ts index 358b1e5..ca3af99 100644 --- a/resources/server/sv_twitter.ts +++ b/resources/server/sv_twitter.ts @@ -199,11 +199,12 @@ async function createDefaultProfile(identifier: string): Promise { * not, create it and return it. * @param identifier - player's identifier */ -async function getOrCreateProfile(identifier: string): Promise { +export async function getOrCreateProfile(identifier: string): Promise { const profile = await getProfile(identifier); return profile || (await createDefaultProfile(identifier)); } + async function updateProfile(identifier: string, profile: Profile) { const { avatar_url, profile_name, bio, location, job } = profile; const query = ` @@ -306,8 +307,9 @@ async function doesRetweetExist(tweetId: number, identifier: string): Promise { const _source = getSource(); + const identifier = getIdentifier(_source); + try { - const identifier = getIdentifier(_source); const profile = await getOrCreateProfile(identifier); emitNet(events.TWITTER_GET_OR_CREATE_PROFILE_SUCCESS, _source, profile); } catch (e) {