Merge branch 'master' into feature/sticky-notes

This commit is contained in:
Christopher Gjelten authored and GitHub committed 2020-10-30 14:33:07 +01:00
commit 18be1a7d0f
22 files changed
+466 -107

No files matched your search

+38
View File
@@ -0,0 +1,38 @@
import axios from 'axios';
import { Tweet, Profile } from '../../phone/src/common/interfaces/twitter';
import { IMAGE_DELIMITER } from '../../phone/src/apps/twitter/utils/images';
const DISCORD_TOKEN_ENV_VAR = 'discord_bot_token';
const DISCORD_TOKEN = GetConvar(DISCORD_TOKEN_ENV_VAR, "");
const DISCORD_CHANNEL_ENV_VAR = 'discord_channel_id';
const DISCORD_CHANNEL_ID = GetConvar(DISCORD_CHANNEL_ENV_VAR, "");
const DISCORD_HEADERS = { authorization: `Bot ${DISCORD_TOKEN}` };
const BASE_URL = 'https://discord.com/api';
const POST_CHANNEL_URL = `${BASE_URL}/channels/${DISCORD_CHANNEL_ID}/messages`;
export async function reportTweetToDiscord(tweet: Tweet, reportingProfile: Profile ): Promise<any> {
if (!DISCORD_TOKEN || !DISCORD_CHANNEL_ID) {
console.warn(`Got a request to report a tweet but discord is not
configured. See README on how to configure discord endpoints.`);
return;
}
const content = `**REPORTED TWEET**
----------------------------------------------------------------------
**Reporting User**: ${reportingProfile.profile_name} (${reportingProfile.id})
**Reported User**: ${tweet.profile_name} (${tweet.profile_id})
**Reported Tweet timestamp**: ${tweet.createdAt}
----------------------------------------------------------------------
**Reported Message**:
${tweet.message}
**Reported Images**:
${tweet.images.split(IMAGE_DELIMITER).join('\n')}
`;
return await axios.post(POST_CHANNEL_URL, { content }, { headers: DISCORD_HEADERS });
}
+119 -29
View File
@@ -3,6 +3,7 @@ import { ESX, getSource } from "./server";
import { Tweet, Profile } from '../../phone/src/common/interfaces/twitter';
import events from "../utils/events";
import config from "../utils/config";
import { reportTweetToDiscord } from './discord';
interface ProfileName {
@@ -15,22 +16,25 @@ interface ProfileName {
*/
async function fetchAllTweets(profileId: number): Promise<Tweet[]> {
const query = `
SELECT
npwd_twitter_profiles.profile_name,
npwd_twitter_profiles.avatar_url,
npwd_twitter_tweets.*,
npwd_twitter_likes.id IS NOT NULL AS isLiked,
TIME_TO_SEC(TIMEDIFF( NOW(), npwd_twitter_tweets.createdAt)) AS seconds_since_tweet
SELECT
npwd_twitter_profiles.id AS profile_id,
npwd_twitter_profiles.profile_name,
npwd_twitter_profiles.avatar_url,
npwd_twitter_tweets.*,
npwd_twitter_likes.id IS NOT NULL AS isLiked,
npwd_twitter_reports.id IS NOT NULL AS isReported,
TIME_TO_SEC(TIMEDIFF( NOW(), npwd_twitter_tweets.createdAt)) AS seconds_since_tweet
FROM npwd_twitter_tweets
LEFT OUTER JOIN npwd_twitter_profiles ON npwd_twitter_tweets.identifier = npwd_twitter_profiles.identifier
LEFT OUTER JOIN npwd_twitter_likes ON npwd_twitter_tweets.id = npwd_twitter_likes.tweet_id AND npwd_twitter_likes.profile_id = ?
LEFT OUTER JOIN npwd_twitter_reports ON npwd_twitter_tweets.id = npwd_twitter_reports.tweet_id AND npwd_twitter_reports.profile_id = ?
WHERE visible = 1
ORDER BY npwd_twitter_tweets.createdAt DESC
LIMIT 100
`;
const [results] = await pool.query(query, [profileId]);
const [results] = await pool.query(query, [profileId, profileId]);
const tweets = <Tweet[]>results;
return tweets;
return tweets.map(tweet => ({ ...tweet, isMine: tweet.profile_id === profileId}));
}
/**
@@ -46,25 +50,51 @@ async function fetchTweetsFiltered(
const parameterizedSearchValue = `%${searchValue}%`;
const query = `
SELECT
npwd_twitter_profiles.profile_name,
npwd_twitter_profiles.avatar_url,
npwd_twitter_tweets.*,
npwd_twitter_likes.id IS NOT NULL AS isLiked,
TIME_TO_SEC(TIMEDIFF( NOW(), npwd_twitter_tweets.createdAt)) AS seconds_since_tweet
npwd_twitter_profiles.id AS profile_id,
npwd_twitter_profiles.profile_name,
npwd_twitter_profiles.avatar_url,
npwd_twitter_tweets.*,
npwd_twitter_likes.id IS NOT NULL AS isLiked,
npwd_twitter_reports.id IS NOT NULL AS isReported,
TIME_TO_SEC(TIMEDIFF( NOW(), npwd_twitter_tweets.createdAt)) AS seconds_since_tweet
FROM npwd_twitter_tweets
LEFT OUTER JOIN npwd_twitter_profiles ON npwd_twitter_tweets.identifier = npwd_twitter_profiles.identifier
LEFT OUTER JOIN npwd_twitter_likes ON npwd_twitter_tweets.id = npwd_twitter_likes.tweet_id AND npwd_twitter_likes.profile_id = ?
LEFT OUTER JOIN npwd_twitter_reports ON npwd_twitter_tweets.id = npwd_twitter_reports.tweet_id AND npwd_twitter_reports.profile_id = ?
WHERE visible = 1 AND (npwd_twitter_profiles.profile_name LIKE ? OR npwd_twitter_tweets.message LIKE ?)
ORDER BY npwd_twitter_tweets.createdAt DESC
LIMIT 100
`;
const [results] = await pool.query(query, [
profileId,
profileId,
parameterizedSearchValue,
parameterizedSearchValue,
]);
const tweets = <Tweet[]>results;
return tweets;
return tweets.map(tweet => ({ ...tweet, isMine: tweet.profile_id === profileId}));
}
/**
* Retrieve a Tweet by ID
* @param tweetId - primary key of the tweet ID
*/
async function getTweet(profileId: number, tweetId: 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.*
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 [results] = await pool.query(query, [tweetId]);
const tweets = <Tweet[]>results;
const tweet = tweets[0];
return { ...tweet, isMine: tweet.profile_id === profileId};
}
/**
@@ -73,6 +103,7 @@ async function fetchTweetsFiltered(
* @param tweet - tweet to be created
*/
async function createTweet(identifier: string, tweet: Tweet): Promise<Tweet> {
const profile = await getProfile(identifier);
const query = `
INSERT INTO npwd_twitter_tweets (identifier, message, images)
VALUES (?, ?, ?)
@@ -83,21 +114,15 @@ async function createTweet(identifier: string, tweet: Tweet): Promise<Tweet> {
tweet.images,
]);
const insertData = <any>results;
const insertId = insertData.insertId;
return await getTweet(profile.id, 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];
async function createTweetReport(tweetId: number, profileId: number): Promise<void> {
const query = `
INSERT INTO npwd_twitter_reports (tweet_id, profile_id)
VALUES (?, ?)
`;
await pool.execute(query, [tweetId, profileId]);
}
/**
@@ -132,7 +157,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;
}
/**
@@ -203,6 +228,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
@@ -222,6 +262,25 @@ async function doesLikeExist(
return likes.length > 0;
}
/**
* Check if a tweet has been reported by this profile already
* @param tweetId - primary key of the tweet to check
* @param profileId - primary key of the profile to check
*/
async function doesReportExist(
tweetId: number,
profileId: number
): Promise<boolean> {
const query = `
SELECT * FROM npwd_twitter_reports
WHERE tweet_id = ? AND profile_id = ?
LIMIT 1
`;
const [results] = await pool.query(query, [tweetId, profileId]);
const reports = <any[]>results;
return reports.length > 0;
}
onNet(events.TWITTER_GET_OR_CREATE_PROFILE, async () => {
try {
const identifier = ESX.GetPlayerFromId(getSource()).getIdentifier();
@@ -276,6 +335,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();
@@ -291,3 +361,23 @@ onNet(events.TWITTER_TOGGLE_LIKE, async (tweetId: number) => {
emitNet(events.TWITTER_TOGGLE_LIKE_FAILURE, getSource());
}
});
onNet(events.TWITTER_REPORT, async (tweetId: number) => {
try {
const identifier = ESX.GetPlayerFromId(getSource()).getIdentifier();
const profile = await getProfile(identifier);
const tweet = await getTweet(profile.id, tweetId);
const reportExists = await doesReportExist(tweet.id, profile.id);
if (reportExists) {
console.warn('This profile has already reported this tweet');
} else {
await createTweetReport(tweet.id, profile.id);
await reportTweetToDiscord(tweet, profile);
}
emitNet(events.TWITTER_REPORT_SUCCESS, getSource());
} catch (e) {
emitNet(events.TWITTER_REPORT_FAILURE, getSource());
}
});
+1 -1
View File
@@ -9,7 +9,7 @@
"module": "commonjs",
"target": "es6",
"allowJs": false,
"lib": ["es2017"],
"lib": ["es2015", "DOM"],
"types": ["@citizenfx/server", "@types/node", "jest"],
"moduleResolution": "node",
"emitDecoratorMetadata": true,