mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-03 06:28:51 +00:00
Feature/Twitter Notification Filters (#147)
Feature: Twitter notification update
This commit is contained in:
15 files changed
+85
-23
No files matched your search
+1
-1
@@ -1,4 +1,4 @@
|
||||
import React from 'react';
|
||||
import React, { useEffect } from 'react';
|
||||
import './Phone.css';
|
||||
import './i18n';
|
||||
import { Route } from 'react-router-dom';
|
||||
|
||||
@@ -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 }) => (
|
||||
<ListSubheader color="primary" component="div" disableSticky>
|
||||
@@ -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 = () => {
|
||||
/>
|
||||
</List>
|
||||
<List disablePadding subheader={<SubHeaderComp text={t('APPS_TWITTER')} />}>
|
||||
<SettingItem
|
||||
label={t('APPS_SETTINGS_OPTION_NOTIFICATION_FILTER')}
|
||||
value={settings.TWITTER_notiFilter.label}
|
||||
options={twitterNotificationFilters}
|
||||
onClick={openMenu}
|
||||
icon={<FilterList />}
|
||||
/>
|
||||
<SettingItem
|
||||
label={t('APPS_SETTINGS_OPTION_NOTIFICATION')}
|
||||
value={settings.TWITTER_notiSound.label}
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import { atom, DefaultValue, useRecoilState } from 'recoil';
|
||||
import { atom, DefaultValue, SetterOrUpdater, useRecoilState } from 'recoil';
|
||||
|
||||
import { SETTINGS_ALL_TWEETS, SETTING_MENTIONS } from '../../../common/typings/twitter';
|
||||
import config from '../../../config/default.json';
|
||||
import { SettingOption } from '../../../ui/hooks/useContextMenu';
|
||||
|
||||
@@ -32,11 +34,12 @@ export interface IPhoneSettings {
|
||||
ringtoneVol: number;
|
||||
notiSound: SettingOption;
|
||||
notiSoundVol: number;
|
||||
TWITTER_notiFilter: SettingOption<SETTING_MENTIONS | SETTINGS_ALL_TWEETS>;
|
||||
TWITTER_notiSound: SettingOption;
|
||||
TWITTER_notiSoundVol: number;
|
||||
TWITTER_notifyNewFeedTweet: boolean;
|
||||
}
|
||||
|
||||
export const useSettings = () => {
|
||||
export const useSettings = (): [IPhoneSettings, SetterOrUpdater<any>] => {
|
||||
return useRecoilState(settingsState);
|
||||
};
|
||||
@@ -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
|
||||
|
||||
@@ -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';
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ import { atom } from 'recoil';
|
||||
export const twitterState = {
|
||||
profile: atom({
|
||||
key: 'profile',
|
||||
default: {},
|
||||
default: null,
|
||||
}),
|
||||
tweets: atom({
|
||||
key: 'tweets',
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
@@ -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';
|
||||
@@ -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;
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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",
|
||||
|
||||
|
||||
@@ -12,9 +12,9 @@ export const MapStringOptions = (current, onClick) => (string) => {
|
||||
};
|
||||
};
|
||||
|
||||
export interface SettingOption {
|
||||
export interface SettingOption<T = any> {
|
||||
label: string;
|
||||
value: string | number;
|
||||
value: T | string | number;
|
||||
}
|
||||
|
||||
export const MapSettingItem = (current: SettingOption, onClick: Function) => (
|
||||
|
||||
@@ -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<string> {
|
||||
|
||||
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,
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -199,11 +199,12 @@ async function createDefaultProfile(identifier: string): Promise<Profile> {
|
||||
* not, create it and return it.
|
||||
* @param identifier - player's identifier
|
||||
*/
|
||||
async function getOrCreateProfile(identifier: string): Promise<Profile> {
|
||||
export async function getOrCreateProfile(identifier: string): Promise<Profile> {
|
||||
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<bo
|
||||
|
||||
onNet(events.TWITTER_GET_OR_CREATE_PROFILE, async () => {
|
||||
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) {
|
||||
|
||||
Reference in new issue
Block a user