mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-04 22:16:40 +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 './Phone.css';
|
||||||
import './i18n';
|
import './i18n';
|
||||||
import { Route } from 'react-router-dom';
|
import { Route } from 'react-router-dom';
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import { SettingItem, SettingItemIconAction, SettingItemSlider } from './Setting
|
|||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
|
FilterList,
|
||||||
Brush,
|
Brush,
|
||||||
Wallpaper,
|
Wallpaper,
|
||||||
Phone,
|
Phone,
|
||||||
@@ -23,7 +24,6 @@ import {
|
|||||||
|
|
||||||
import { ListSubheader } from '@material-ui/core';
|
import { ListSubheader } from '@material-ui/core';
|
||||||
import { useSettings } from '../hooks/useSettings';
|
import { useSettings } from '../hooks/useSettings';
|
||||||
// import { useSnackbar } from '../../../ui/hooks/useSnackbar';
|
|
||||||
|
|
||||||
const SubHeaderComp = (props: { text: string }) => (
|
const SubHeaderComp = (props: { text: string }) => (
|
||||||
<ListSubheader color="primary" component="div" disableSticky>
|
<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 = () => {
|
const handleCopyPhoneNumber = () => {
|
||||||
// TODO: Dependent on #132 merge
|
// TODO: Dependent on #132 merge
|
||||||
// setClipboard(simcard.number)
|
// setClipboard(simcard.number)
|
||||||
@@ -159,6 +165,13 @@ export const SettingsApp = () => {
|
|||||||
/>
|
/>
|
||||||
</List>
|
</List>
|
||||||
<List disablePadding subheader={<SubHeaderComp text={t('APPS_TWITTER')} />}>
|
<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
|
<SettingItem
|
||||||
label={t('APPS_SETTINGS_OPTION_NOTIFICATION')}
|
label={t('APPS_SETTINGS_OPTION_NOTIFICATION')}
|
||||||
value={settings.TWITTER_notiSound.label}
|
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 config from '../../../config/default.json';
|
||||||
import { SettingOption } from '../../../ui/hooks/useContextMenu';
|
import { SettingOption } from '../../../ui/hooks/useContextMenu';
|
||||||
|
|
||||||
@@ -32,11 +34,12 @@ export interface IPhoneSettings {
|
|||||||
ringtoneVol: number;
|
ringtoneVol: number;
|
||||||
notiSound: SettingOption;
|
notiSound: SettingOption;
|
||||||
notiSoundVol: number;
|
notiSoundVol: number;
|
||||||
|
TWITTER_notiFilter: SettingOption<SETTING_MENTIONS | SETTINGS_ALL_TWEETS>;
|
||||||
TWITTER_notiSound: SettingOption;
|
TWITTER_notiSound: SettingOption;
|
||||||
TWITTER_notiSoundVol: number;
|
TWITTER_notiSoundVol: number;
|
||||||
TWITTER_notifyNewFeedTweet: boolean;
|
TWITTER_notifyNewFeedTweet: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useSettings = () => {
|
export const useSettings = (): [IPhoneSettings, SetterOrUpdater<any>] => {
|
||||||
return useRecoilState(settingsState);
|
return useRecoilState(settingsState);
|
||||||
};
|
};
|
||||||
@@ -43,7 +43,6 @@ export const TwitterApp = () => {
|
|||||||
const { profile } = useProfile();
|
const { profile } = useProfile();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
Nui.send('phone:getOrCreateTwitterProfile', {});
|
|
||||||
|
|
||||||
// this is a polling implementation. It is possible that
|
// this is a polling implementation. It is possible that
|
||||||
// there is some interaction where, on a new tweet, all
|
// 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 { makeStyles } from '@material-ui/core/styles';
|
||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { atom } from 'recoil';
|
|||||||
export const twitterState = {
|
export const twitterState = {
|
||||||
profile: atom({
|
profile: atom({
|
||||||
key: 'profile',
|
key: 'profile',
|
||||||
default: {},
|
default: null,
|
||||||
}),
|
}),
|
||||||
tweets: atom({
|
tweets: atom({
|
||||||
key: 'tweets',
|
key: 'tweets',
|
||||||
|
|||||||
@@ -1,32 +1,49 @@
|
|||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
import { useHistory } from 'react-router-dom';
|
import { useHistory } from 'react-router-dom';
|
||||||
import { useRecoilState } from 'recoil';
|
|
||||||
import { Tweet } from '../../../common/typings/twitter';
|
import { Tweet } from '../../../common/typings/twitter';
|
||||||
|
import { useRecoilState, useRecoilValue } from 'recoil';
|
||||||
|
|
||||||
import { useApp } from '../../../os/apps/hooks/useApps';
|
import { useApp } from '../../../os/apps/hooks/useApps';
|
||||||
import { useNotifications } from '../../../os/notifications/hooks/useNotifications';
|
import { useNotifications } from '../../../os/notifications/hooks/useNotifications';
|
||||||
|
import { useSettings } from '../../settings/hooks/useSettings';
|
||||||
|
import { SETTING_MENTIONS } from '../utils/constants';
|
||||||
import { twitterState } from './state';
|
import { twitterState } from './state';
|
||||||
|
|
||||||
const NOTIFICATION_ID = 'twitter:broadcast';
|
const NOTIFICATION_ID = 'twitter:broadcast';
|
||||||
|
|
||||||
|
function isMentioned(profileName: string, message: string) {
|
||||||
|
return !message.toLowerCase().includes(profileName.toLowerCase());
|
||||||
|
}
|
||||||
|
|
||||||
export const useTwitterNotifications = () => {
|
export const useTwitterNotifications = () => {
|
||||||
|
const [settings] = useSettings();
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const history = useHistory();
|
const history = useHistory();
|
||||||
|
|
||||||
const {
|
const { addNotificationAlert, addNotification, hasNotification, removeId } = useNotifications();
|
||||||
addNotificationAlert,
|
|
||||||
addNotification,
|
|
||||||
hasNotification,
|
|
||||||
removeId,
|
|
||||||
} = useNotifications();
|
|
||||||
|
|
||||||
const { icon, notificationIcon } = useApp('TWITTER');
|
const { icon, notificationIcon } = useApp('TWITTER');
|
||||||
|
|
||||||
const [unreadCount, setUnreadCount] = useRecoilState(twitterState.unreadTweetsCount);
|
const [unreadCount, setUnreadCount] = useRecoilState(twitterState.unreadTweetsCount);
|
||||||
|
const profile = useRecoilValue(twitterState.profile);
|
||||||
|
|
||||||
const setNotification = ({ profile_name, message, isRetweet }) => {
|
const setNotification = ({ profile_name, message, isRetweet }) => {
|
||||||
const titleStr = isRetweet
|
const titleStr = isRetweet
|
||||||
? 'APPS_TWITTER_NEW_RETWEET_BROADCAST'
|
? 'APPS_TWITTER_NEW_RETWEET_BROADCAST'
|
||||||
: 'APPS_TWITTER_NEW_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 = {
|
const notification = {
|
||||||
app: 'TWITTER',
|
app: 'TWITTER',
|
||||||
id: NOTIFICATION_ID,
|
id: NOTIFICATION_ID,
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
|
import { useEffect } from 'react';
|
||||||
import { useSetRecoilState } from 'recoil';
|
import { useSetRecoilState } from 'recoil';
|
||||||
import { v4 as uuidv4 } from 'uuid';
|
import { v4 as uuidv4 } from 'uuid';
|
||||||
|
|
||||||
import { useNuiEvent } from '../../../os/nui-events/hooks/useNuiEvent';
|
import { useNuiEvent } from '../../../os/nui-events/hooks/useNuiEvent';
|
||||||
|
import Nui from '../../../os/nui-events/utils/Nui';
|
||||||
import { IMAGE_DELIMITER } from '../utils/images';
|
import { IMAGE_DELIMITER } from '../utils/images';
|
||||||
import { APP_TWITTER } from '../utils/constants';
|
import { APP_TWITTER } from '../utils/constants';
|
||||||
import { twitterState } from './state';
|
import { twitterState } from './state';
|
||||||
@@ -64,5 +66,5 @@ export const useTwitterService = () => {
|
|||||||
useNuiEvent(APP_TWITTER, 'createTweetLoading', setCreateLoading);
|
useNuiEvent(APP_TWITTER, 'createTweetLoading', setCreateLoading);
|
||||||
useNuiEvent(APP_TWITTER, 'createTweetResult', handleAddAlert);
|
useNuiEvent(APP_TWITTER, 'createTweetResult', handleAddAlert);
|
||||||
useNuiEvent(APP_TWITTER, 'createTweetBroadcast', setNotification);
|
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_DEFAULT_AVATAR = './media/twitter/egg.png';
|
||||||
export const IMG_INVALID_AVATAR = './media/twitter/invalid.png';
|
export const IMG_INVALID_AVATAR = './media/twitter/invalid.png';
|
||||||
export const APP_TWITTER = 'TWITTER';
|
export const APP_TWITTER = 'TWITTER';
|
||||||
|
|
||||||
|
export const SETTING_MENTIONS = 'mentions';
|
||||||
|
export const SETTINGS_ALL_TWEETS = 'all';
|
||||||
@@ -1,10 +1,12 @@
|
|||||||
|
|
||||||
export interface NewTweet {
|
export interface NewTweet {
|
||||||
message: string;
|
message: string;
|
||||||
images?: string;
|
images?: string;
|
||||||
retweet?: number;
|
retweet?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type SETTING_MENTIONS = 'mentions';
|
||||||
|
export type SETTINGS_ALL_TWEETS = 'all';
|
||||||
|
|
||||||
export interface Tweet extends NewTweet {
|
export interface Tweet extends NewTweet {
|
||||||
profile_name: string;
|
profile_name: string;
|
||||||
profile_id: number;
|
profile_id: number;
|
||||||
|
|||||||
@@ -87,6 +87,16 @@
|
|||||||
"value": "99kr"
|
"value": "99kr"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
"notiFilters": [
|
||||||
|
{
|
||||||
|
"label": "All Tweets",
|
||||||
|
"value": "all"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "Only Mentions",
|
||||||
|
"value": "mentions"
|
||||||
|
}
|
||||||
|
],
|
||||||
"defaultSettings": {
|
"defaultSettings": {
|
||||||
"wallpaper": {
|
"wallpaper": {
|
||||||
"label": "Project Error",
|
"label": "Project Error",
|
||||||
@@ -114,6 +124,10 @@
|
|||||||
},
|
},
|
||||||
"ringtoneVol": 50,
|
"ringtoneVol": 50,
|
||||||
"notiSoundVol": 50,
|
"notiSoundVol": 50,
|
||||||
|
"TWITTER_notiFilter": {
|
||||||
|
"label": "All Tweets",
|
||||||
|
"value": "all"
|
||||||
|
},
|
||||||
"TWITTER_notiSound": {
|
"TWITTER_notiSound": {
|
||||||
"label": "Online",
|
"label": "Online",
|
||||||
"value": "online"
|
"value": "online"
|
||||||
|
|||||||
@@ -117,6 +117,7 @@
|
|||||||
"APPS_SETTINGS_OPTION_ZOOM": "Zoom",
|
"APPS_SETTINGS_OPTION_ZOOM": "Zoom",
|
||||||
"APPS_SETTINGS_OPTION_RINGTONE": "Ringtone",
|
"APPS_SETTINGS_OPTION_RINGTONE": "Ringtone",
|
||||||
"APPS_SETTINGS_OPTION_RINGTONEVOL": "Ringtone Volume",
|
"APPS_SETTINGS_OPTION_RINGTONEVOL": "Ringtone Volume",
|
||||||
|
"APPS_SETTINGS_OPTION_NOTIFICATION_FILTER": "Notification Filters",
|
||||||
"APPS_SETTINGS_OPTION_NOTIFICATION": "Notification",
|
"APPS_SETTINGS_OPTION_NOTIFICATION": "Notification",
|
||||||
"APPS_SETTINGS_OPTION_NOTIFICATIONVOL": "Notification Volume",
|
"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;
|
label: string;
|
||||||
value: string | number;
|
value: T | string | number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const MapSettingItem = (current: SettingOption, onClick: Function) => (
|
export const MapSettingItem = (current: SettingOption, onClick: Function) => (
|
||||||
|
|||||||
@@ -2,7 +2,9 @@ import { ESX } from './server';
|
|||||||
import { pool } from './db';
|
import { pool } from './db';
|
||||||
import config from '../utils/config';
|
import config from '../utils/config';
|
||||||
import { getIdentifier, getIdentifierByPhoneNumber, getSource, usePhoneNumber } from './functions';
|
import { getIdentifier, getIdentifierByPhoneNumber, getSource, usePhoneNumber } from './functions';
|
||||||
|
import { getOrCreateProfile } from './sv_twitter';
|
||||||
import { mainLogger } from './sv_logger';
|
import { mainLogger } from './sv_logger';
|
||||||
|
import events from '../utils/events';
|
||||||
|
|
||||||
//db = DatabaseConfig //helper variable for use in server function
|
//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) => {
|
onNet('esx:playerLoaded', async (playerId: number, xPlayer: any) => {
|
||||||
const _source = getSource();
|
const _source = getSource();
|
||||||
|
const identifier = xPlayer.identifier;
|
||||||
try {
|
try {
|
||||||
const identifier = xPlayer.identifier;
|
|
||||||
await generatePhoneNumber(identifier);
|
await generatePhoneNumber(identifier);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
mainLogger.error(`Failed to generate a phone number, ${e.message}`, {
|
mainLogger.error(`Failed to generate a phone number, ${e.message}`, {
|
||||||
source: _source,
|
source: _source,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// make sure a twitter profile exists for this user before
|
||||||
|
// we load the phone
|
||||||
|
await getOrCreateProfile(identifier);
|
||||||
});
|
});
|
||||||
|
|
||||||
onNet('phone:getCredentials', async () => {
|
onNet('phone:getCredentials', async () => {
|
||||||
@@ -84,4 +90,4 @@ onNet('phone:getCredentials', async () => {
|
|||||||
source: _source,
|
source: _source,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -199,11 +199,12 @@ async function createDefaultProfile(identifier: string): Promise<Profile> {
|
|||||||
* not, create it and return it.
|
* not, create it and return it.
|
||||||
* @param identifier - player's identifier
|
* @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);
|
const profile = await getProfile(identifier);
|
||||||
return profile || (await createDefaultProfile(identifier));
|
return profile || (await createDefaultProfile(identifier));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
async function updateProfile(identifier: string, profile: Profile) {
|
async function updateProfile(identifier: string, profile: Profile) {
|
||||||
const { avatar_url, profile_name, bio, location, job } = profile;
|
const { avatar_url, profile_name, bio, location, job } = profile;
|
||||||
const query = `
|
const query = `
|
||||||
@@ -306,8 +307,9 @@ async function doesRetweetExist(tweetId: number, identifier: string): Promise<bo
|
|||||||
|
|
||||||
onNet(events.TWITTER_GET_OR_CREATE_PROFILE, async () => {
|
onNet(events.TWITTER_GET_OR_CREATE_PROFILE, async () => {
|
||||||
const _source = getSource();
|
const _source = getSource();
|
||||||
|
const identifier = getIdentifier(_source);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const identifier = getIdentifier(_source);
|
|
||||||
const profile = await getOrCreateProfile(identifier);
|
const profile = await getOrCreateProfile(identifier);
|
||||||
emitNet(events.TWITTER_GET_OR_CREATE_PROFILE_SUCCESS, _source, profile);
|
emitNet(events.TWITTER_GET_OR_CREATE_PROFILE_SUCCESS, _source, profile);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|||||||
Reference in new issue
Block a user