mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-04 14:38:45 +00:00
Mysql connection strings, unit tests, prettier on server
This adds support for the other type of Mysql connection string specified by mysql async. Support for both strings and their respective options are necessary to support backwards compatibility of existing server configurations. This also adds support for jest & unit testing on the server/client and some unit tests for some of the connection string parsing. It runs these test and prettier in a pre-commit hook.
This commit is contained in:
1 parent
8d2e0d9edb
commit
a5d1e54b56
21 files changed
+4136
-505
No files matched your search
+137
-129
@@ -1,33 +1,33 @@
|
||||
import { pool } from './db';
|
||||
import { ESX, getSource } from './server';
|
||||
import events from '../utils/events';
|
||||
import config from '../utils/config';
|
||||
import { pool } from "./db";
|
||||
import { ESX, getSource } from "./server";
|
||||
import events from "../utils/events";
|
||||
import config from "../utils/config";
|
||||
|
||||
interface Profile {
|
||||
id: number;
|
||||
profile_name: string;
|
||||
identifier: string;
|
||||
avatar_url?: string;
|
||||
bio?: string;
|
||||
location?: string;
|
||||
job?: string;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
id: number;
|
||||
profile_name: string;
|
||||
identifier: string;
|
||||
avatar_url?: string;
|
||||
bio?: string;
|
||||
location?: string;
|
||||
job?: string;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
}
|
||||
|
||||
interface ProfileName {
|
||||
profile_name: string;
|
||||
profile_name: string;
|
||||
}
|
||||
|
||||
interface Tweet {
|
||||
id: number;
|
||||
profile_name?: string;
|
||||
avatar_url?: string;
|
||||
message: string;
|
||||
createdAt?: string;
|
||||
updatedAt?: string;
|
||||
images?: string;
|
||||
likes?: number;
|
||||
id: number;
|
||||
profile_name?: string;
|
||||
avatar_url?: string;
|
||||
message: string;
|
||||
createdAt?: string;
|
||||
updatedAt?: string;
|
||||
images?: string;
|
||||
likes?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -35,7 +35,7 @@ interface Tweet {
|
||||
* @param profileId - twitter profile id of the player
|
||||
*/
|
||||
async function fetchAllTweets(profileId: number): Promise<Tweet[]> {
|
||||
const query = `
|
||||
const query = `
|
||||
SELECT
|
||||
npwd_twitter_profiles.profile_name,
|
||||
npwd_twitter_profiles.avatar_url,
|
||||
@@ -49,9 +49,9 @@ async function fetchAllTweets(profileId: number): Promise<Tweet[]> {
|
||||
ORDER BY npwd_twitter_tweets.createdAt DESC
|
||||
LIMIT 100
|
||||
`;
|
||||
const [ results ] = await pool.query(query, [profileId]);
|
||||
const tweets = <Tweet[]>results;
|
||||
return tweets;
|
||||
const [results] = await pool.query(query, [profileId]);
|
||||
const tweets = <Tweet[]>results;
|
||||
return tweets;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -60,9 +60,12 @@ async function fetchAllTweets(profileId: number): Promise<Tweet[]> {
|
||||
* @param profileId - twitter profile id of the player
|
||||
* @param searchValue - value to search
|
||||
*/
|
||||
async function fetchTweetsFiltered(profileId: number, searchValue: string): Promise<Tweet[]> {
|
||||
const parameterizedSearchValue = `%${searchValue}%`;
|
||||
const query = `
|
||||
async function fetchTweetsFiltered(
|
||||
profileId: number,
|
||||
searchValue: string
|
||||
): Promise<Tweet[]> {
|
||||
const parameterizedSearchValue = `%${searchValue}%`;
|
||||
const query = `
|
||||
SELECT
|
||||
npwd_twitter_profiles.profile_name,
|
||||
npwd_twitter_profiles.avatar_url,
|
||||
@@ -76,9 +79,13 @@ async function fetchTweetsFiltered(profileId: number, searchValue: string): Prom
|
||||
ORDER BY npwd_twitter_tweets.createdAt DESC
|
||||
LIMIT 100
|
||||
`;
|
||||
const [ results ] = await pool.query(query, [profileId, parameterizedSearchValue, parameterizedSearchValue]);
|
||||
const tweets = <Tweet[]>results;
|
||||
return tweets;
|
||||
const [results] = await pool.query(query, [
|
||||
profileId,
|
||||
parameterizedSearchValue,
|
||||
parameterizedSearchValue,
|
||||
]);
|
||||
const tweets = <Tweet[]>results;
|
||||
return tweets;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -87,11 +94,11 @@ async function fetchTweetsFiltered(profileId: number, searchValue: string): Prom
|
||||
* @param tweet - tweet to be created
|
||||
*/
|
||||
async function createTweet(identifier: string, tweet: Tweet): Promise<any> {
|
||||
const query = `
|
||||
const query = `
|
||||
INSERT INTO npwd_twitter_tweets (identifier, message, images)
|
||||
VALUES (?, ?, ?)
|
||||
`
|
||||
await pool.query(query, [identifier, tweet.message, tweet.images])
|
||||
`;
|
||||
await pool.query(query, [identifier, tweet.message, tweet.images]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -99,14 +106,14 @@ async function createTweet(identifier: string, tweet: Tweet): Promise<any> {
|
||||
* @param identifier - player identifier
|
||||
*/
|
||||
async function getProfile(identifier: string): Promise<Profile | null> {
|
||||
const query = `
|
||||
const query = `
|
||||
SELECT * FROM npwd_twitter_profiles
|
||||
WHERE identifier = ?
|
||||
LIMIT 1
|
||||
`;
|
||||
const [ results ] = await pool.query(query, [identifier]);
|
||||
const profiles = <Profile[]>results;
|
||||
return profiles.length > 0 ? profiles[0] : null;
|
||||
const [results] = await pool.query(query, [identifier]);
|
||||
const profiles = <Profile[]>results;
|
||||
return profiles.length > 0 ? profiles[0] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -114,19 +121,19 @@ async function getProfile(identifier: string): Promise<Profile | null> {
|
||||
* @param identifier - player's identifier
|
||||
*/
|
||||
async function generateProfileName(identifier: string): Promise<string> {
|
||||
const defaultProfileName = '';
|
||||
if (!config.twitter.generateProfileNameFromUsers) return defaultProfileName;
|
||||
const defaultProfileName = "";
|
||||
if (!config.twitter.generateProfileNameFromUsers) return defaultProfileName;
|
||||
|
||||
const query = `
|
||||
const query = `
|
||||
SELECT CONCAT(firstname, '_', lastname) AS profile_name
|
||||
FROM users
|
||||
WHERE identifier = ? LIMIT 1
|
||||
`
|
||||
const [ results ] = await pool.query(query, [identifier]);
|
||||
const profileNames = <ProfileName[]>results;
|
||||
`;
|
||||
const [results] = await pool.query(query, [identifier]);
|
||||
const profileNames = <ProfileName[]>results;
|
||||
|
||||
if (profileNames.length === 0) return defaultProfileName;
|
||||
return profileNames[0].profile_name;
|
||||
if (profileNames.length === 0) return defaultProfileName;
|
||||
return profileNames[0].profile_name;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -135,13 +142,13 @@ async function generateProfileName(identifier: string): Promise<string> {
|
||||
* @param identifier - player's identifier
|
||||
*/
|
||||
async function createDefaultProfile(identifier: string): Promise<Profile> {
|
||||
const profileName = await generateProfileName(identifier);
|
||||
const query = `
|
||||
const profileName = await generateProfileName(identifier);
|
||||
const query = `
|
||||
INSERT INTO npwd_twitter_profiles (identifier, profile_name)
|
||||
VALUES (?, ?)
|
||||
`
|
||||
const [ result ] = await pool.execute(query, [identifier, profileName]);
|
||||
return getProfile(identifier);
|
||||
`;
|
||||
const [result] = await pool.execute(query, [identifier, profileName]);
|
||||
return getProfile(identifier);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -150,25 +157,25 @@ async function createDefaultProfile(identifier: string): Promise<Profile> {
|
||||
* @param identifier - player's identifier
|
||||
*/
|
||||
async function getOrCreateProfile(identifier: string): Promise<Profile> {
|
||||
const profile = await getProfile(identifier);
|
||||
return profile || await createDefaultProfile(identifier);
|
||||
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 = `
|
||||
const { avatar_url, profile_name, bio, location, job } = profile;
|
||||
const query = `
|
||||
UPDATE npwd_twitter_profiles
|
||||
SET avatar_url = ?, profile_name = ?, bio = ?, location = ?, job = ?
|
||||
WHERE identifier = ?
|
||||
`
|
||||
await pool.execute(query, [
|
||||
avatar_url,
|
||||
profile_name,
|
||||
bio,
|
||||
location,
|
||||
job,
|
||||
identifier
|
||||
]);
|
||||
`;
|
||||
await pool.execute(query, [
|
||||
avatar_url,
|
||||
profile_name,
|
||||
bio,
|
||||
location,
|
||||
job,
|
||||
identifier,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -177,11 +184,11 @@ async function updateProfile(identifier: string, profile: Profile) {
|
||||
* @param tweetId - primary key of the tweet being liked
|
||||
*/
|
||||
async function createLike(profileId: number, tweetId: number): Promise<void> {
|
||||
const query = `
|
||||
const query = `
|
||||
INSERT INTO npwd_twitter_likes (profile_id, tweet_id)
|
||||
VALUES (?, ?)
|
||||
`
|
||||
await pool.execute(query, [profileId, tweetId]);
|
||||
`;
|
||||
await pool.execute(query, [profileId, tweetId]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -190,11 +197,11 @@ async function createLike(profileId: number, tweetId: number): Promise<void> {
|
||||
* @param tweetId - primary key of the tweet to remove the like from
|
||||
*/
|
||||
async function deleteLike(profileId: number, tweetId: number): Promise<void> {
|
||||
const query = `
|
||||
const query = `
|
||||
DELETE FROM npwd_twitter_likes
|
||||
WHERE profile_id = ? AND tweet_id = ?
|
||||
`
|
||||
await pool.execute(query, [profileId, tweetId]);
|
||||
`;
|
||||
await pool.execute(query, [profileId, tweetId]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -202,83 +209,84 @@ async function deleteLike(profileId: number, tweetId: number): Promise<void> {
|
||||
* @param profileId - primary key of the profile to check
|
||||
* @param tweetId - primary key of the tweet to check
|
||||
*/
|
||||
async function doesLikeExist(profileId: number, tweetId: number): Promise<boolean> {
|
||||
const query = `
|
||||
async function doesLikeExist(
|
||||
profileId: number,
|
||||
tweetId: number
|
||||
): Promise<boolean> {
|
||||
const query = `
|
||||
SELECT * FROM npwd_twitter_likes
|
||||
WHERE profile_id = ? AND tweet_id = ?
|
||||
LIMIT 1
|
||||
`
|
||||
const [ results ] = await pool.query(query, [profileId, tweetId]);
|
||||
const likes = <any[]>results;
|
||||
return likes.length > 0;
|
||||
`;
|
||||
const [results] = await pool.query(query, [profileId, tweetId]);
|
||||
const likes = <any[]>results;
|
||||
return likes.length > 0;
|
||||
}
|
||||
|
||||
|
||||
onNet(events.TWITTER_GET_OR_CREATE_PROFILE, async () => {
|
||||
try {
|
||||
const identifier = ESX.GetPlayerFromId(getSource()).getIdentifier();
|
||||
const profile = await getOrCreateProfile(identifier)
|
||||
emitNet(events.TWITTER_GET_OR_CREATE_PROFILE_SUCCESS, getSource(), profile);
|
||||
} catch (e) {
|
||||
emitNet(events.TWITTER_GET_OR_CREATE_PROFILE_FAILURE, getSource());
|
||||
}
|
||||
try {
|
||||
const identifier = ESX.GetPlayerFromId(getSource()).getIdentifier();
|
||||
const profile = await getOrCreateProfile(identifier);
|
||||
emitNet(events.TWITTER_GET_OR_CREATE_PROFILE_SUCCESS, getSource(), profile);
|
||||
} catch (e) {
|
||||
emitNet(events.TWITTER_GET_OR_CREATE_PROFILE_FAILURE, getSource());
|
||||
}
|
||||
});
|
||||
|
||||
onNet(events.TWITTER_UPDATE_PROFILE, async (profile: Profile) => {
|
||||
try {
|
||||
const identifier = ESX.GetPlayerFromId(getSource()).getIdentifier();
|
||||
await updateProfile(identifier, profile)
|
||||
emitNet(events.TWITTER_UPDATE_PROFILE_RESULT, getSource(), true);
|
||||
} catch (e) {
|
||||
emitNet(events.TWITTER_UPDATE_PROFILE_RESULT, getSource(), false);
|
||||
}
|
||||
try {
|
||||
const identifier = ESX.GetPlayerFromId(getSource()).getIdentifier();
|
||||
await updateProfile(identifier, profile);
|
||||
emitNet(events.TWITTER_UPDATE_PROFILE_RESULT, getSource(), true);
|
||||
} catch (e) {
|
||||
emitNet(events.TWITTER_UPDATE_PROFILE_RESULT, getSource(), false);
|
||||
}
|
||||
});
|
||||
|
||||
onNet(events.TWITTER_FETCH_TWEETS, async () => {
|
||||
try {
|
||||
const identifier = ESX.GetPlayerFromId(getSource()).getIdentifier();
|
||||
const profile = await getProfile(identifier);
|
||||
const tweets = await fetchAllTweets(profile.id);
|
||||
emitNet(events.TWITTER_FETCH_TWEETS_SUCCESS, getSource(), tweets);
|
||||
} catch (e) {
|
||||
emitNet(events.TWITTER_FETCH_TWEETS_FAILURE, getSource());
|
||||
}
|
||||
try {
|
||||
const identifier = ESX.GetPlayerFromId(getSource()).getIdentifier();
|
||||
const profile = await getProfile(identifier);
|
||||
const tweets = await fetchAllTweets(profile.id);
|
||||
emitNet(events.TWITTER_FETCH_TWEETS_SUCCESS, getSource(), tweets);
|
||||
} catch (e) {
|
||||
emitNet(events.TWITTER_FETCH_TWEETS_FAILURE, getSource());
|
||||
}
|
||||
});
|
||||
|
||||
onNet(events.TWITTER_FETCH_TWEETS_FILTERED, async (searchValue: string) => {
|
||||
try {
|
||||
const identifier = ESX.GetPlayerFromId(getSource()).getIdentifier();
|
||||
const profile = await getProfile(identifier);
|
||||
const tweets = await fetchTweetsFiltered(profile.id, searchValue);
|
||||
emitNet(events.TWITTER_FETCH_TWEETS_FILTERED_SUCCESS, getSource(), tweets);
|
||||
} catch (e) {
|
||||
emitNet(events.TWITTER_FETCH_TWEETS_FILTERED_FAILURE, getSource());
|
||||
}
|
||||
try {
|
||||
const identifier = ESX.GetPlayerFromId(getSource()).getIdentifier();
|
||||
const profile = await getProfile(identifier);
|
||||
const tweets = await fetchTweetsFiltered(profile.id, searchValue);
|
||||
emitNet(events.TWITTER_FETCH_TWEETS_FILTERED_SUCCESS, getSource(), tweets);
|
||||
} catch (e) {
|
||||
emitNet(events.TWITTER_FETCH_TWEETS_FILTERED_FAILURE, getSource());
|
||||
}
|
||||
});
|
||||
|
||||
onNet(events.TWITTER_CREATE_TWEET, async (tweet: Tweet) => {
|
||||
try {
|
||||
const identifier = ESX.GetPlayerFromId(getSource()).getIdentifier();
|
||||
await createTweet(identifier, tweet);
|
||||
emitNet(events.TWITTER_CREATE_TWEET_RESULT, getSource(), true);
|
||||
} catch (e) {
|
||||
emitNet(events.TWITTER_CREATE_TWEET_RESULT, getSource(), false);
|
||||
}
|
||||
try {
|
||||
const identifier = ESX.GetPlayerFromId(getSource()).getIdentifier();
|
||||
await createTweet(identifier, tweet);
|
||||
emitNet(events.TWITTER_CREATE_TWEET_RESULT, getSource(), true);
|
||||
} catch (e) {
|
||||
emitNet(events.TWITTER_CREATE_TWEET_RESULT, getSource(), false);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
onNet(events.TWITTER_TOGGLE_LIKE, async (tweetId: number) => {
|
||||
try {
|
||||
const identifier = ESX.GetPlayerFromId(getSource()).getIdentifier();
|
||||
const profile = await getOrCreateProfile(identifier)
|
||||
const likeExists = await doesLikeExist(profile.id, tweetId);
|
||||
if (likeExists) {
|
||||
await deleteLike(profile.id, tweetId);
|
||||
} else {
|
||||
await createLike(profile.id, tweetId);
|
||||
}
|
||||
emitNet(events.TWITTER_TOGGLE_LIKE_SUCCESS, getSource());
|
||||
} catch (e) {
|
||||
emitNet(events.TWITTER_TOGGLE_LIKE_FAILURE, getSource());
|
||||
try {
|
||||
const identifier = ESX.GetPlayerFromId(getSource()).getIdentifier();
|
||||
const profile = await getOrCreateProfile(identifier);
|
||||
const likeExists = await doesLikeExist(profile.id, tweetId);
|
||||
if (likeExists) {
|
||||
await deleteLike(profile.id, tweetId);
|
||||
} else {
|
||||
await createLike(profile.id, tweetId);
|
||||
}
|
||||
});
|
||||
emitNet(events.TWITTER_TOGGLE_LIKE_SUCCESS, getSource());
|
||||
} catch (e) {
|
||||
emitNet(events.TWITTER_TOGGLE_LIKE_FAILURE, getSource());
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user