mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-04 14:38:45 +00:00
Merge branch 'master' into feat/sellout-app
This commit is contained in:
46 files changed
+4495
-587
No files matched your search
+60
-16
@@ -1,22 +1,66 @@
|
||||
import mysql from 'mysql2/promise';
|
||||
import mysql from "mysql2/promise";
|
||||
import { ConnectionStringParser } from "connection-string-parser";
|
||||
import {
|
||||
CONNECTION_STRING,
|
||||
DEFAULT_PORT,
|
||||
parseSemiColonFormat,
|
||||
getServerHost,
|
||||
getUserId,
|
||||
getPassword,
|
||||
} from "./db_utils";
|
||||
|
||||
const CONNECTION_STRING = 'mysql_connection_string';
|
||||
// we require set mysql_connection_string "mysql://root:password@localhost/es_extended?charset=utf8mb4" to be set in the config
|
||||
// we require set mysql_connection_string to be set in the config
|
||||
const mysqlConnectionString = GetConvar(CONNECTION_STRING, null);
|
||||
if (mysqlConnectionString === null) {
|
||||
throw new Error(`No connection string provided. make sure ${CONNECTION_STRING} is set in your config.`)
|
||||
if (mysqlConnectionString === null) {
|
||||
throw new Error(
|
||||
`No connection string provided. make sure "${CONNECTION_STRING} is set in your config.`
|
||||
);
|
||||
}
|
||||
|
||||
const connectionStringParser = new ConnectionStringParser({ scheme: 'mysql', hosts: []});
|
||||
const connectionOjbect = connectionStringParser .parse(mysqlConnectionString);
|
||||
/**
|
||||
* Most fivem servers utilize fivem-mysql-async (https://brouznouf.github.io/fivem-mysql-async/) and
|
||||
* define a environment variable "mysql_connection_string" in their configurations. We will try to
|
||||
* maintain backwards compatibility with this.
|
||||
*
|
||||
* fivem-mysql-async allows for two different connection string formats defined here:
|
||||
* https://brouznouf.github.io/fivem-mysql-async/config/ and we need to handle both of them.
|
||||
*/
|
||||
export function generateConnectionPool() {
|
||||
if (mysqlConnectionString.includes("database=")) {
|
||||
// This is checking for this format:
|
||||
// set mysql_connection_string "server=127.0.0.1;database=es_extended;userid=user;password=pass"
|
||||
const config = parseSemiColonFormat(mysqlConnectionString);
|
||||
return mysql.createPool({
|
||||
host: getServerHost(config),
|
||||
user: getUserId(config),
|
||||
port: config.port ? parseInt(config.port) : DEFAULT_PORT,
|
||||
password: getPassword(config),
|
||||
database: config.database,
|
||||
waitForConnections: true,
|
||||
connectionLimit: 10,
|
||||
queueLimit: 0,
|
||||
});
|
||||
} else {
|
||||
// This is checking for this format:
|
||||
// set mysql_connection_string "mysql://root:[email protected]/es_extended?charset=utf8mb4"
|
||||
const connectionStringParser = new ConnectionStringParser({
|
||||
scheme: "mysql",
|
||||
hosts: [],
|
||||
});
|
||||
const connectionOjbect = connectionStringParser.parse(
|
||||
mysqlConnectionString
|
||||
);
|
||||
|
||||
export const pool = mysql.createPool({
|
||||
host: connectionOjbect.hosts[0].host,
|
||||
user: connectionOjbect.username,
|
||||
password: connectionOjbect.password,
|
||||
database: connectionOjbect.endpoint,
|
||||
waitForConnections: true,
|
||||
connectionLimit: 10,
|
||||
queueLimit: 0
|
||||
});
|
||||
return mysql.createPool({
|
||||
host: connectionOjbect.hosts[0].host,
|
||||
user: connectionOjbect.username,
|
||||
password: connectionOjbect.password,
|
||||
database: connectionOjbect.endpoint,
|
||||
waitForConnections: true,
|
||||
connectionLimit: 10,
|
||||
queueLimit: 0,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export const pool = generateConnectionPool();
|
||||
@@ -0,0 +1,94 @@
|
||||
import * as db from "./db_utils";
|
||||
|
||||
const t = "test value";
|
||||
|
||||
describe("parseSemiColonFormat", () => {
|
||||
it("Raises a error with invalid string", () => {
|
||||
expect(() => db.parseSemiColonFormat("no_variables_here")).toThrowError();
|
||||
});
|
||||
|
||||
it("Correctly parses configuration options", () => {
|
||||
const test =
|
||||
"server=127.0.0.1;database=es_extended;userid=user;password=pass";
|
||||
const result = db.parseSemiColonFormat(test);
|
||||
|
||||
expect(result.server).toEqual("127.0.0.1");
|
||||
expect(result.database).toEqual("es_extended");
|
||||
expect(result.userid).toEqual("user");
|
||||
expect(result.password).toEqual("pass");
|
||||
});
|
||||
});
|
||||
|
||||
describe("getServerHost", () => {
|
||||
it("returns default", () => {
|
||||
expect(db.getServerHost({})).toEqual(db.DEFAULT_HOST);
|
||||
});
|
||||
|
||||
it("Correctly returns host", () => {
|
||||
expect(db.getServerHost({ host: t })).toEqual(t);
|
||||
});
|
||||
|
||||
it("Correctly returns server", () => {
|
||||
expect(db.getServerHost({ server: t })).toEqual(t);
|
||||
});
|
||||
|
||||
it("Correctly returns data source", () => {
|
||||
expect(db.getServerHost({ "data source": t })).toEqual(t);
|
||||
});
|
||||
|
||||
it("Correctly returns datasource", () => {
|
||||
expect(db.getServerHost({ datasource: t })).toEqual(t);
|
||||
});
|
||||
|
||||
it("Correctly returns addr", () => {
|
||||
expect(db.getServerHost({ addr: t })).toEqual(t);
|
||||
});
|
||||
|
||||
it("Correctly returns address", () => {
|
||||
expect(db.getServerHost({ address: t })).toEqual(t);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getUserId", () => {
|
||||
it("returns default", () => {
|
||||
expect(db.getUserId({})).toEqual(db.DEFAULT_USER);
|
||||
});
|
||||
|
||||
it("Correctly returns user", () => {
|
||||
expect(db.getUserId({ user: t })).toEqual(t);
|
||||
});
|
||||
|
||||
it("Correctly returns user id", () => {
|
||||
expect(db.getUserId({ "user id": t })).toEqual(t);
|
||||
});
|
||||
|
||||
it("Correctly returns userid", () => {
|
||||
expect(db.getUserId({ userid: t })).toEqual(t);
|
||||
});
|
||||
|
||||
it("Correctly returns user name", () => {
|
||||
expect(db.getUserId({ "user name": t })).toEqual(t);
|
||||
});
|
||||
|
||||
it("Correctly returns username", () => {
|
||||
expect(db.getUserId({ username: t })).toEqual(t);
|
||||
});
|
||||
|
||||
it("Correctly returns uid", () => {
|
||||
expect(db.getUserId({ uid: t })).toEqual(t);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getPassword", () => {
|
||||
it("returns default", () => {
|
||||
expect(() => db.getPassword({})).toThrowError();
|
||||
});
|
||||
|
||||
it("Correctly returns password", () => {
|
||||
expect(db.getPassword({ password: t })).toEqual(t);
|
||||
});
|
||||
|
||||
it("Correctly returns pwd", () => {
|
||||
expect(db.getPassword({ pwd: t })).toEqual(t);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,71 @@
|
||||
export const CONNECTION_STRING = "mysql_connection_string";
|
||||
export const DEFAULT_PORT = 3306;
|
||||
export const DEFAULT_HOST = "localhost";
|
||||
export const DEFAULT_USER = "root";
|
||||
|
||||
interface Map {
|
||||
[key: string]: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* parse the connection string from the format server=127.0.0.1;database=es_extended;userid=user;password=pass
|
||||
* @param connectionString - mysql_connection_string value
|
||||
*/
|
||||
export function parseSemiColonFormat(connectionString: string): Map {
|
||||
const parts = connectionString.split(";");
|
||||
if (parts.length === 1) {
|
||||
throw new Error(
|
||||
`Connection string ${connectionString} is in the incorrect format.`
|
||||
);
|
||||
}
|
||||
|
||||
return parts.reduce((connectionInfo: Map, part) => {
|
||||
const [key, value] = part.split("=");
|
||||
connectionInfo[key] = value;
|
||||
return connectionInfo;
|
||||
}, {});
|
||||
}
|
||||
|
||||
/**
|
||||
* allowed variable names: host | server | data source | datasource | addr | address
|
||||
* @param config - database config variables parsed from mysql_connection_string
|
||||
*/
|
||||
export function getServerHost(config: Map): string {
|
||||
return (
|
||||
config.host ||
|
||||
config.server ||
|
||||
config["data source"] ||
|
||||
config.datasource ||
|
||||
config.addr ||
|
||||
config.address ||
|
||||
DEFAULT_HOST
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* allowed variable names: user | user id | userid | user name | username | uid
|
||||
* @param config - database config variables parsed from mysql_connection_string
|
||||
*/
|
||||
export function getUserId(config: Map): string {
|
||||
return (
|
||||
config.user ||
|
||||
config["user id"] ||
|
||||
config.userid ||
|
||||
config["user name"] ||
|
||||
config.username ||
|
||||
config.uid ||
|
||||
DEFAULT_USER
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* allowed variable names: password | pwd
|
||||
* @param config - database config variables parsed from mysql_connection_string
|
||||
*/
|
||||
export function getPassword(config: Map): string {
|
||||
const password = config.password || config.pwd;
|
||||
if (!password) {
|
||||
throw new Error("Password is a required field");
|
||||
}
|
||||
return password;
|
||||
}
|
||||
@@ -8,6 +8,6 @@ import './sv_sellout';
|
||||
|
||||
export let ESX: ESXServer = null;
|
||||
|
||||
emit('esx:getSharedObject', (obj: ESXServer) => ESX = obj);
|
||||
emit("esx:getSharedObject", (obj: ESXServer) => (ESX = obj));
|
||||
|
||||
export const getSource = () => parseInt(source);
|
||||
export const getSource = () => parseInt(source);
|
||||
@@ -1,21 +1,27 @@
|
||||
import { ESX } from './server';
|
||||
import { pool } from './db';
|
||||
import events from '../utils/events';
|
||||
import { ESX } from "./server";
|
||||
import { pool } from "./db";
|
||||
import events from "../utils/events";
|
||||
|
||||
interface Contacts {
|
||||
display: string;
|
||||
number: string;
|
||||
}
|
||||
|
||||
async function fetchAllContacts(identifier: string) : Promise<Contacts[]> {
|
||||
async function fetchAllContacts(identifier: string): Promise<Contacts[]> {
|
||||
const query = "SELECT * FROM npwd_phone_contacts WHERE identifier = ?";
|
||||
const [ results ] = await pool.query(query, [identifier]);
|
||||
const [results] = await pool.query(query, [identifier]);
|
||||
const contacts = <Contacts[]>results;
|
||||
return contacts;
|
||||
}
|
||||
|
||||
async function addContact(identifier: string, number: string, display: string, avatar: string):Promise<any> {
|
||||
const query = "INSERT INTO npwd_phone_contacts (identifier, number, display, avatar) VALUES (?, ?, ?, ?)"
|
||||
async function addContact(
|
||||
identifier: string,
|
||||
number: string,
|
||||
display: string,
|
||||
avatar: string
|
||||
): Promise<any> {
|
||||
const query =
|
||||
"INSERT INTO npwd_phone_contacts (identifier, number, display, avatar) VALUES (?, ?, ?, ?)";
|
||||
|
||||
await pool.query(query, [identifier, number, display, avatar]);
|
||||
}
|
||||
@@ -27,9 +33,8 @@ onNet(events.CONTACTS_GET_CONTACTS, async () => {
|
||||
const _identifier = xPlayer.getIdentifier();
|
||||
|
||||
const contacts = await fetchAllContacts(_identifier);
|
||||
emitNet(events.CONTACTS_SEND_CONTACTS, _source, contacts)
|
||||
|
||||
} catch(error) {
|
||||
emitNet(events.CONTACTS_SEND_CONTACTS, _source, contacts);
|
||||
} catch (error) {
|
||||
console.log("Failed to fetch contacts: ", error);
|
||||
}
|
||||
})
|
||||
@@ -44,5 +49,5 @@ onNet(events.CONTACTS_ADD_CONTACT, (number: string, display: string, avatar: str
|
||||
|
||||
} catch(error) {
|
||||
console.log("Failed to add contact: ", error);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { ESX } from './server';
|
||||
import { pool } from './db';
|
||||
import { ESX } from "./server";
|
||||
import { pool } from "./db";
|
||||
|
||||
//db = DatabaseConfig //helper variable for use in server function
|
||||
|
||||
@@ -13,22 +13,22 @@ interface Credentials {
|
||||
phone_number: string;
|
||||
}
|
||||
|
||||
async function getCredentials(identifier: string) : Promise<string> {
|
||||
async function getCredentials(identifier: string): Promise<string> {
|
||||
const query = "SELECT phone_number FROM users WHERE identifier = ?";
|
||||
const [ result ] = await pool.query(query, [identifier]);
|
||||
const [result] = await pool.query(query, [identifier]);
|
||||
const number = <Credentials[]>result;
|
||||
if (number.length === 0) return "###-####";
|
||||
return number[0].phone_number;
|
||||
}
|
||||
|
||||
onNet('phone:getCredentials', async () => {
|
||||
onNet("phone:getCredentials", async () => {
|
||||
try {
|
||||
const _source = (global as any).source;
|
||||
const xPlayer = ESX.GetPlayerFromId(_source);
|
||||
const _identifier = xPlayer.getIdentifier();
|
||||
const number = await getCredentials(_identifier);
|
||||
emitNet('phone:sendCredentials', _source, number)
|
||||
emitNet("phone:sendCredentials", _source, number);
|
||||
} catch (error) {
|
||||
console.log('Failed to get number. ', error);
|
||||
};
|
||||
});
|
||||
console.log("Failed to get number. ", error);
|
||||
}
|
||||
});
|
||||
+161
-131
@@ -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,22 +79,46 @@ 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a tweet in the database
|
||||
* Creates a tweet in the database and then retrieves the tweet
|
||||
* @param identifier - player identifier
|
||||
* @param tweet - tweet to be created
|
||||
*/
|
||||
async function createTweet(identifier: string, tweet: Tweet): Promise<any> {
|
||||
const query = `
|
||||
async function createTweet(identifier: string, tweet: Tweet): Promise<Tweet> {
|
||||
const query = `
|
||||
INSERT INTO npwd_twitter_tweets (identifier, message, images)
|
||||
VALUES (?, ?, ?)
|
||||
`
|
||||
await pool.query(query, [identifier, tweet.message, tweet.images])
|
||||
`;
|
||||
const [results] = await pool.query(query, [
|
||||
identifier,
|
||||
tweet.message,
|
||||
tweet.images,
|
||||
]);
|
||||
const insertData = <any>results;
|
||||
const insertId = insertData.insertId;
|
||||
|
||||
const resultQuery = `
|
||||
SELECT
|
||||
npwd_twitter_profiles.profile_name,
|
||||
npwd_twitter_profiles.avatar_url,
|
||||
npwd_twitter_tweets.*
|
||||
FROM npwd_twitter_tweets
|
||||
LEFT OUTER JOIN npwd_twitter_profiles ON npwd_twitter_tweets.identifier = npwd_twitter_profiles.identifier
|
||||
WHERE npwd_twitter_tweets.id = ?
|
||||
`;
|
||||
const [createResults] = await pool.query(resultQuery, [insertId]);
|
||||
const tweets = <Tweet[]>createResults;
|
||||
|
||||
return tweets[0];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -99,14 +126,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 +141,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 +162,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 +177,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 +204,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 +217,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 +229,86 @@ 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();
|
||||
const createdTweet = await createTweet(identifier, tweet);
|
||||
|
||||
emitNet(events.TWITTER_CREATE_TWEET_RESULT, getSource(), true);
|
||||
emitNet(events.TWITTER_CREATE_TWEET_BROADCAST, -1, createdTweet);
|
||||
} 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());
|
||||
}
|
||||
});
|
||||
@@ -10,7 +10,7 @@
|
||||
"target": "es6",
|
||||
"allowJs": false,
|
||||
"lib": ["es2017"],
|
||||
"types": ["@citizenfx/server", "@types/node"],
|
||||
"types": ["@citizenfx/server", "@types/node", "jest"],
|
||||
"moduleResolution": "node",
|
||||
"emitDecoratorMetadata": true,
|
||||
"experimentalDecorators": true,
|
||||
|
||||
Reference in new issue
Block a user