working tweet deletion

This commit is contained in:
erik-sn committed 2020-10-24 11:05:57 -04:00
1 parent 73272a6a43
commit c0dbf1a765
11 files changed
+200 -14

No files matched your search

+17
View File
@@ -88,6 +88,23 @@ onNet(events.TWITTER_CREATE_TWEET_BROADCAST, (tweet: any) => {
sendTwitterMessage(events.TWITTER_CREATE_TWEET_BROADCAST, tweet);
});
/**
* Twitter delete tweet
*/
RegisterNuiCallbackType(events.TWITTER_DELETE_TWEET);
on(`__cfx_nui:${events.TWITTER_DELETE_TWEET}`, (tweetId: number) => {
emitNet(events.TWITTER_DELETE_TWEET, tweetId);
});
onNet(events.TWITTER_DELETE_TWEET_SUCCESS, () => {
sendTwitterMessage(events.TWITTER_DELETE_TWEET_SUCCESS);
emitNet(events.TWITTER_FETCH_TWEETS);
});
onNet(events.TWITTER_DELETE_TWEET_FAILURE, () => {
sendTwitterMessage(events.TWITTER_DELETE_TWEET_FAILURE);
});
/**
* Twitter likes
*/
+44 -1
View File
@@ -1 +1,44 @@
<!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="%PUBLIC_URL%/favicon.ico"/><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap"/><link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#000000"/><meta name="description" content="Web site created using create-react-app"/><title>React App</title></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div><script src="main.js"></script></body></html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="/favicon.ico" />
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" />
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<!--
Notice the use of in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
<script src="dev.js"></script></body>
</html>
+32 -3
View File
@@ -21,6 +21,7 @@ interface ProfileName {
interface Tweet {
id: number;
profile_id?: number;
profile_name?: string;
avatar_url?: string;
message: string;
@@ -37,6 +38,7 @@ interface Tweet {
async function fetchAllTweets(profileId: number): Promise<Tweet[]> {
const query = `
SELECT
npwd_twitter_profiles.id AS profile_id,
npwd_twitter_profiles.profile_name,
npwd_twitter_profiles.avatar_url,
npwd_twitter_tweets.*,
@@ -51,7 +53,7 @@ async function fetchAllTweets(profileId: number): Promise<Tweet[]> {
`;
const [results] = await pool.query(query, [profileId]);
const tweets = <Tweet[]>results;
return tweets;
return tweets.map(tweet => ({ ...tweet, isMine: tweet.profile_id === profileId}));
}
/**
@@ -67,6 +69,7 @@ async function fetchTweetsFiltered(
const parameterizedSearchValue = `%${searchValue}%`;
const query = `
SELECT
npwd_twitter_profiles.id AS profile_id,
npwd_twitter_profiles.profile_name,
npwd_twitter_profiles.avatar_url,
npwd_twitter_tweets.*,
@@ -85,7 +88,7 @@ async function fetchTweetsFiltered(
parameterizedSearchValue,
]);
const tweets = <Tweet[]>results;
return tweets;
return tweets.map(tweet => ({ ...tweet, isMine: tweet.profile_id === profileId}));
}
/**
@@ -153,7 +156,7 @@ async function generateProfileName(identifier: string): Promise<string> {
const profileNames = <ProfileName[]>results;
if (profileNames.length === 0) return defaultProfileName;
return profileNames[0].profile_name;
return profileNames[0].profile_name || defaultProfileName;
}
/**
@@ -224,6 +227,21 @@ async function deleteLike(profileId: number, tweetId: number): Promise<void> {
await pool.execute(query, [profileId, tweetId]);
}
/**
* Delete a tweet from the database
* @param identifier - identifier of the source user
* @param tweetId - primary key of the tweet to remove the like from
*/
async function deleteTweet(identifier: string, tweetId: number): Promise<void> {
if (!config.twitter.allowDeleteTweets) return;
const query = `
DELETE FROM npwd_twitter_tweets
WHERE identifier = ? AND id = ?
`;
const [ result ] = await pool.execute(query, [identifier, tweetId]);
}
/**
* Check if a "like" relationship between a profile and a tweet exists
* @param profileId - primary key of the profile to check
@@ -297,6 +315,17 @@ onNet(events.TWITTER_CREATE_TWEET, async (tweet: Tweet) => {
}
});
onNet(events.TWITTER_DELETE_TWEET, async (tweetId: number) => {
try {
const identifier = ESX.GetPlayerFromId(getSource()).getIdentifier();
await deleteTweet(identifier, tweetId);
emitNet(events.TWITTER_DELETE_TWEET_SUCCESS, getSource());
} catch (e) {
emitNet(events.TWITTER_DELETE_TWEET_FAILURE, getSource());
}
});
onNet(events.TWITTER_TOGGLE_LIKE, async (tweetId: number) => {
try {
const identifier = ESX.GetPlayerFromId(getSource()).getIdentifier();
+3
View File
@@ -17,6 +17,9 @@ export default {
TWITTER_CREATE_TWEET_RESULT: "createTweetResult",
TWITTER_CREATE_TWEET_BROADCAST: "createTweetBroadcast",
TWITTER_CREATE_TWEET_FAILURE: "createTweetResultFailed",
TWITTER_DELETE_TWEET: "phone:deleteTweet",
TWITTER_DELETE_TWEET_SUCCESS: "deleteTweetSuccess",
TWITTER_DELETE_TWEET_FAILURE: "deleteTweetFailed",
TWITTER_TOGGLE_LIKE: "phone:toggleLike",
TWITTER_TOGGLE_LIKE_SUCCESS: "toggleLikeSuccess",
TWITTER_TOGGLE_LIKE_FAILURE: "toggleLikeFailed",