Skip to content

Commit

Permalink
handle invalid idPost
Browse files Browse the repository at this point in the history
  • Loading branch information
HamdiBenK committed Jul 17, 2023
2 parents 166f8d3 + dc34dbb commit a00bbc0
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 6 deletions.
99 changes: 95 additions & 4 deletions controllers/profile.controller.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var rp = require('axios');
const validator = require('validator')


const {
User,
GoogleProfile,
Expand Down Expand Up @@ -885,11 +886,12 @@ module.exports.confrimChangeMail = async (req, res) => {
}
}

module.exports.checkInsta = async (req, res) => {
module.exports.checkThreads = async (req, res) => {
try{
let instaAccount = await FbPage.exists({UserId : req.user._id, instagram_username : {$exists : true}});
return makeResponseData(res, 201, instaAccount)

let instaAccount = await FbPage.findOne({UserId : req.user._id, instagram_username : {$exists : true}});
if(!instaAccount) return makeResponseData(res, 200,'instagram_not_found')
if(instaAccount.threads_id) return makeResponseData(res, 200, 'threads_already_added')
return makeResponseData(res, 200, true)
}catch (err) {
return makeResponseError(
res,
Expand Down Expand Up @@ -1137,3 +1139,92 @@ module.exports.ProfilPrivacy = async (req, res) => {
)
}
}


module.exports.addThreadsAccount = async (req,res) => {
try {
const instaAccount = await FbPage.findOne({UserId : req.user._id, instagram_username : {$exists : true}});
if(!instaAccount) return makeResponseData(res, 200,'instagram_not_found')
if(instaAccount.threads_id) return makeResponseData(res, 200,'threads_already_added')
const user = await axios.get(`https://www.threads.net/@${instaAccount.instagram_username}`);
let text = user.data.replace(/\s/g, '').replace(/\s/g, '');
const userID = text.match(/"user_id":"(\d+)"/)?.[1]
if(!userID) return makeResponseData(res, 200,'threads_not_found')
const lsdToken = await getLsdToken(text)
const currentUser = await fetchUserThreadData(lsdToken, userID);
if(currentUser) {
const userPicture = await axios.get(currentUser.profile_pic_url, { responseType: 'arraybuffer' })
const base64String = Buffer.from(userPicture.data, 'binary').toString('base64');
await FbPage.updateOne({
instagram_username: instaAccount.instagram_username,

}, {threads_id: currentUser.pk, threads_picture: base64String ? base64String : currentUser.profile_pic_url})
return makeResponseData(res, 200, 'threads_account_added', {username: instaAccount.instagram_username, picture: base64String ? base64String : currentUser.profile_pic_url, id: currentUser.pk})
}
return makeResponseData(res, 200, 'error')
} catch(err) {
return makeResponseError(
res,
500,
err.message ? err.message : err.error
)
}
}



module.exports.removeThreadsAccount = async (req,res) => {
const instaAccount = await FbPage.findOne({UserId : req.user._id, instagram_username : {$exists : true}});
if(!instaAccount) return makeResponseData(res, 200,'instagram_not_found')
if(instaAccount.threads_id) {
await FbPage.updateOne({ UserId: req.user._id }, {$unset: {threads_id:1, threads_picture:1}})
return makeResponseData(res, 200, 'deleted successfully')
} return makeResponseData(res, 200,'no_threads_found')

}









const getLsdToken = async (text) => {
const lsdTokenMatch = text.match(/"LSD",\[\],{"token":"(\w+)"},\d+\]/)?.[1];
return lsdTokenMatch;
};

const fetchUserThreadData = async (token, userID) => {
const data = {
lsd: token,
variables: `{"userID": ${userID}}`,
doc_id: '23996318473300828',
};

const headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Cache-Control': 'no-cache',
'Pragma': 'no-cache',
'X-ASBD-ID': '129477',
'X-FB-LSD': token,
'X-IG-App-ID': '5587632691339264',
};

const response = await axios.post(
'https://www.threads.net/api/graphql',
data,
{
headers: headers,
transformRequest: [(data) => {
return Object.entries(data)
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
.join('&');
}],
}
);

const user = response?.data?.data?.userData?.user
return user;
}
1 change: 1 addition & 0 deletions model/fbPage.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const fbPageSchema = mongoose.Schema(
id: { type: String },
instagram_id: { type: String },
threads_id : String,
threads_picture : String,
instagram_username: { type: String },
name: { type: String },
picture: { type: String },
Expand Down
11 changes: 9 additions & 2 deletions routes/profile.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ const {
ShareByActivity,
tiktokApiAbos,
ProfilPrivacy,
checkInsta
checkThreads,
addThreadsAccount,
removeThreadsAccount
} = require('../controllers/profile.controller')
const {
addFacebookChannel,
Expand Down Expand Up @@ -1394,6 +1396,11 @@ router.get('/linkedin/ShareByActivity/:activity', verifyAuth, ShareByActivityVal
router.get('/Tiktok/ProfilPrivacy', verifyAuth, ProfilPrivacy)


router.get('/check/insta',verifyAuth,checkInsta)
router.get('/check/threads-account',verifyAuth,checkThreads)

Check failure

Code scanning / CodeQL

Missing rate limiting High

This route handler performs
authorization
, but is not rate-limited.

Check failure

Code scanning / CodeQL

Missing rate limiting High

This route handler performs
a database access
, but is not rate-limited.

router.get('/add/threads-account', verifyAuth, addThreadsAccount)

Check failure

Code scanning / CodeQL

Missing rate limiting High

This route handler performs
authorization
, but is not rate-limited.

Check failure

Code scanning / CodeQL

Missing rate limiting High

This route handler performs
a database access
, but is not rate-limited.
This route handler performs
a database access
, but is not rate-limited.

router.delete('/remove/threads-account', verifyAuth, removeThreadsAccount)

Check failure

Code scanning / CodeQL

Missing rate limiting High

This route handler performs
authorization
, but is not rate-limited.

Check failure

Code scanning / CodeQL

Missing rate limiting High

This route handler performs
a database access
, but is not rate-limited.
This route handler performs
a database access
, but is not rate-limited.


module.exports = router

0 comments on commit a00bbc0

Please sign in to comment.