mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-04 14:38:45 +00:00
handle all configuration cases
This commit is contained in:
1 parent
8000cc3d3f
commit
97f5c99cc0
12 files changed
+289
-157
No files matched your search
@@ -17,6 +17,7 @@ function fetchOnInitialize() {
|
||||
emitNet(events.MESSAGES_FETCH_MESSAGE_GROUPS);
|
||||
emitNet(events.TWITTER_GET_OR_CREATE_PROFILE);
|
||||
sendMessage('PHONE', 'setPhoneReady', isPhoneReady);
|
||||
sendMessage('PHONE', 'phoneConfig', config);
|
||||
}
|
||||
|
||||
RegisterKeyMapping('phone', 'Open Phone', 'keyboard', 'f1');
|
||||
|
||||
@@ -18,6 +18,24 @@ onNet(events.TWITTER_GET_OR_CREATE_PROFILE_FAILURE, () => {
|
||||
sendTwitterMessage(events.TWITTER_GET_OR_CREATE_PROFILE_FAILURE);
|
||||
});
|
||||
|
||||
onNet(events.TWITTER_GET_OR_CREATE_PROFILE_NULL, (defaultProfileNames: string[]): void => {
|
||||
sendTwitterMessage(events.TWITTER_GET_OR_CREATE_PROFILE_NULL, defaultProfileNames);
|
||||
});
|
||||
|
||||
/**
|
||||
* Twitter create profile
|
||||
*/
|
||||
RegisterNuiCallbackType(events.TWITTER_CREATE_PROFILE);
|
||||
on(`__cfx_nui:${events.TWITTER_CREATE_PROFILE}`, (data: any) => {
|
||||
sendTwitterMessage(events.TWITTER_UPDATE_PROFILE_LOADING, true);
|
||||
emitNet(events.TWITTER_CREATE_PROFILE, data);
|
||||
});
|
||||
|
||||
onNet(events.TWITTER_CREATE_PROFILE_RESULT, (alert: IAlertProps) => {
|
||||
sendTwitterMessage(events.TWITTER_CREATE_PROFILE_RESULT, alert);
|
||||
emitNet(events.TWITTER_GET_OR_CREATE_PROFILE);
|
||||
});
|
||||
|
||||
/**
|
||||
* Twitter update profile
|
||||
*/
|
||||
|
||||
@@ -89,6 +89,54 @@ export async function getPlayerInfo(identifier: string): Promise<PlayerQueryResp
|
||||
return { firstname, lastname, phone_number };
|
||||
}
|
||||
|
||||
const clean = (input: string) => (input ? input.replace(/[^0-9a-z]/gi, '') : input);
|
||||
|
||||
async function getCleanedPlayerInfo(identifier: string): Promise<PlayerQueryResp> {
|
||||
const { firstname, lastname, phone_number } = await getPlayerInfo(identifier);
|
||||
return {
|
||||
firstname: clean(firstname),
|
||||
lastname: clean(lastname),
|
||||
phone_number: clean(phone_number),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a profile name by the player's name and/or phone number
|
||||
* @param identifier - player's identifier
|
||||
*/
|
||||
export async function generateProfileName(identifier: string): Promise<string | null> {
|
||||
const { firstname, lastname, phone_number } = await getCleanedPlayerInfo(identifier);
|
||||
|
||||
if (firstname && lastname) {
|
||||
return `${firstname}_${lastname}`;
|
||||
} else if (firstname) {
|
||||
return firstname;
|
||||
} else if (lastname) {
|
||||
return lastname;
|
||||
} else if (phone_number) {
|
||||
return phone_number;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export async function getDefaultProfileNames(identifier: string): Promise<string[]> {
|
||||
const { firstname, lastname, phone_number } = await getCleanedPlayerInfo(identifier);
|
||||
let defaultProfileNames = [];
|
||||
|
||||
if (firstname && lastname) {
|
||||
defaultProfileNames.push(`${firstname}_${lastname}`);
|
||||
} else if (firstname) {
|
||||
defaultProfileNames.push(firstname);
|
||||
} else if (lastname) {
|
||||
defaultProfileNames.push(lastname);
|
||||
}
|
||||
|
||||
if (phone_number) {
|
||||
defaultProfileNames.push(phone_number);
|
||||
}
|
||||
return defaultProfileNames;
|
||||
}
|
||||
|
||||
// Attach to playerJoining event as this is when
|
||||
// the permanent source is assigned for the user
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { pool } from './db';
|
||||
import config from '../utils/config';
|
||||
import { getIdentifier, getSource } from './functions';
|
||||
import { mainLogger } from './sv_logger';
|
||||
|
||||
|
||||
@@ -6,6 +6,8 @@ import config from '../utils/config';
|
||||
import { reportTweetToDiscord } from './discord';
|
||||
import { mainLogger } from './sv_logger';
|
||||
import { ResultSetHeader } from 'mysql2';
|
||||
import { generateProfileName } from './players/sv_players';
|
||||
import { getDefaultProfileNames } from './players/sv_players';
|
||||
|
||||
const twitterLogger = mainLogger.child({ module: 'twitter' });
|
||||
|
||||
@@ -165,23 +167,19 @@ async function getProfile(identifier: string): Promise<Profile | null> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a twitter profile name by the player's name
|
||||
* Create a twitter profile
|
||||
* @param identifier - player's identifier
|
||||
* @param profileName - profile name to be created
|
||||
* @returns
|
||||
*/
|
||||
async function generateProfileName(identifier: string): Promise<string> {
|
||||
const defaultProfileName = '';
|
||||
if (!config.twitter.generateProfileNameFromUsers) return defaultProfileName;
|
||||
|
||||
async function createProfile(identifier: string, profileName: string): Promise<Profile> {
|
||||
const query = `
|
||||
SELECT CONCAT(firstname, '_', lastname) AS profile_name
|
||||
FROM users
|
||||
WHERE identifier = ? LIMIT 1
|
||||
INSERT INTO npwd_twitter_profiles (identifier, profile_name)
|
||||
VALUES (?, ?)
|
||||
`;
|
||||
const [results] = await pool.query(query, [identifier]);
|
||||
const profileNames = <ProfileName[]>results;
|
||||
|
||||
if (profileNames.length === 0) return defaultProfileName;
|
||||
return profileNames[0].profile_name || defaultProfileName;
|
||||
await pool.execute(query, [identifier, profileName]);
|
||||
return await getProfile(identifier);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -189,15 +187,17 @@ async function generateProfileName(identifier: string): Promise<string> {
|
||||
* that all player's will receive and can then customize.
|
||||
* @param identifier - player's identifier
|
||||
*/
|
||||
async function createDefaultProfile(identifier: string): Promise<Profile> {
|
||||
const profileName = await generateProfileName(identifier);
|
||||
const query = `
|
||||
INSERT INTO npwd_twitter_profiles (identifier, profile_name)
|
||||
VALUES (?, ?)
|
||||
`;
|
||||
async function createDefaultProfile(identifier: string): Promise<Profile | null> {
|
||||
// case where the server owner wants players to select their own names
|
||||
if (!config.twitter.generateProfileNameFromUsers) return null;
|
||||
|
||||
await pool.execute(query, [identifier, profileName]);
|
||||
return getProfile(identifier);
|
||||
const defaultProfileName = await generateProfileName(identifier);
|
||||
// case where we tried to generate a profile name but failed due to
|
||||
// some database misconfiguration or error
|
||||
if (!defaultProfileName) return null;
|
||||
|
||||
twitterLogger.info(`Creating default Twitter profile ${defaultProfileName} for ${identifier}`);
|
||||
return await createProfile(identifier, defaultProfileName);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -316,12 +316,41 @@ onNet(events.TWITTER_GET_OR_CREATE_PROFILE, async () => {
|
||||
|
||||
try {
|
||||
const profile = await getOrCreateProfile(identifier);
|
||||
emitNet(events.TWITTER_GET_OR_CREATE_PROFILE_SUCCESS, _source, profile);
|
||||
|
||||
// if we got null from getOrCreateProfile it means it doesn't exist and
|
||||
// we failed to create it. In this case we pass the UI some default
|
||||
// profile names it can choose from
|
||||
if (!profile) {
|
||||
const defaultProfileNames = await getDefaultProfileNames(identifier);
|
||||
emitNet(events.TWITTER_GET_OR_CREATE_PROFILE_NULL, _source, defaultProfileNames);
|
||||
} else {
|
||||
emitNet(events.TWITTER_GET_OR_CREATE_PROFILE_SUCCESS, _source, profile);
|
||||
}
|
||||
} catch (e) {
|
||||
twitterLogger.error(`Failed to get or create profile, ${e.message}`, {
|
||||
source: _source,
|
||||
});
|
||||
emitNet(events.TWITTER_GET_OR_CREATE_PROFILE_FAILURE, _source);
|
||||
emitNet(events.TWITTER_GET_OR_CREATE_PROFILE_FAILURE, _source, null);
|
||||
}
|
||||
});
|
||||
|
||||
onNet(events.TWITTER_CREATE_PROFILE, async (profile: Profile) => {
|
||||
const _source = getSource();
|
||||
try {
|
||||
const identifier = getIdentifier(_source);
|
||||
await createProfile(identifier, profile.profile_name);
|
||||
emitNet(events.TWITTER_CREATE_PROFILE_RESULT, _source, {
|
||||
message: 'TWITTER_CREATE_PROFILE_SUCCESS',
|
||||
type: 'success',
|
||||
});
|
||||
} catch (e) {
|
||||
twitterLogger.error(`Failed to create twitter profile: ${e.message}`, {
|
||||
source: _source,
|
||||
});
|
||||
emitNet(events.TWITTER_CREATE_PROFILE_RESULT, _source, {
|
||||
message: 'TWITTER_CREATE_PROFILE_FAILURE',
|
||||
type: 'error',
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
@@ -350,6 +379,13 @@ onNet(events.TWITTER_FETCH_TWEETS, async () => {
|
||||
try {
|
||||
const identifier = getIdentifier(_source);
|
||||
const profile = await getProfile(identifier);
|
||||
if (!profile) {
|
||||
twitterLogger.warn(
|
||||
`Aborted fetching tweets for user ${identifier} because they do not have a profile.`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const tweets = await fetchAllTweets(profile.id);
|
||||
emitNet(events.TWITTER_FETCH_TWEETS_SUCCESS, _source, tweets);
|
||||
} catch (e) {
|
||||
|
||||
@@ -8,7 +8,10 @@ export default {
|
||||
TWITTER_FETCH_TWEETS_FILTERED_FAILURE: 'fetchTweetsFilteredFailed',
|
||||
TWITTER_GET_OR_CREATE_PROFILE: 'phone:getOrCreateTwitterProfile',
|
||||
TWITTER_GET_OR_CREATE_PROFILE_SUCCESS: 'getOrCreateTwitterProfile',
|
||||
TWITTER_GET_OR_CREATE_PROFILE_NULL: 'getOrCreateTwitterProfileNull',
|
||||
TWITTER_GET_OR_CREATE_PROFILE_FAILURE: 'getOrCreateTwitterProfileFailed',
|
||||
TWITTER_CREATE_PROFILE: 'phone:createTwitterProfile',
|
||||
TWITTER_CREATE_PROFILE_RESULT: 'phone:createProfileResult',
|
||||
TWITTER_UPDATE_PROFILE: 'phone:updateTwitterProfile',
|
||||
TWITTER_UPDATE_PROFILE_LOADING: 'updateProfileLoading',
|
||||
TWITTER_UPDATE_PROFILE_RESULT: 'updateProfileResult',
|
||||
|
||||
Reference in new issue
Block a user