-
Notifications
You must be signed in to change notification settings - Fork 132
[C-1522] Update instagram routes #4509
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,39 +16,19 @@ const verifiedUserReporter = new VerifiedUserReporter({ | |
| source: 'instagram' | ||
| }) | ||
|
|
||
| const getInstagramURL = (username) => { | ||
| const instagramProfileUrl = | ||
| config.get('instagramProfileUrl') || | ||
| 'https://www.instagram.com/%USERNAME%/channel/?__a=1' | ||
| return instagramProfileUrl.replace('%USERNAME%', username) | ||
| } | ||
| const generalAdmissionAddress = config.get('generalAdmissionAddress') | ||
|
|
||
| /** | ||
| * This file contains the instagram endpoints for oauth | ||
| * For official documentation on the instagram oauth flow check out their site | ||
| * https://www.instagram.com/developer/authentication/ | ||
| */ | ||
| module.exports = function (app) { | ||
| app.get( | ||
| '/instagram/profile', | ||
| handleResponse(async (req, res, next) => { | ||
| const username = req.query.username | ||
|
|
||
| if (!username) { | ||
| return errorResponseBadRequest('Missing username parameter') | ||
| } | ||
| try { | ||
| const res = await doRequest(getInstagramURL(username)) | ||
| const json = JSON.parse(res) | ||
| return successResponse(json) | ||
| } catch (e) { | ||
| return errorResponseServerError( | ||
| `Failed to fetch instagram profile for ${username}` | ||
| ) | ||
| } | ||
| }) | ||
| ) | ||
|
|
||
| /** | ||
| * The first leg of the Instagram Oauth. Given an oauth code, it first | ||
| * validates that the user owns the claimed account. Then it sends a | ||
| * request to go pull the instagram graph API full user data. | ||
| */ | ||
| app.post( | ||
| '/instagram', | ||
| handleResponse(async (req, res, next) => { | ||
|
|
@@ -96,15 +76,25 @@ module.exports = function (app) { | |
| `Another Audius profile has already been authenticated with Instagram user @${igUser.username}!` | ||
| ) | ||
| } else { | ||
| // Store the access token, user id, and current profile for user in db | ||
| // Fetch the instagram full profile | ||
| const igProfileReqObj = { | ||
| method: 'get', | ||
| url: `${generalAdmissionAddress}/social/instagram/${igUser.username}` | ||
| } | ||
|
|
||
| try { | ||
| const instagramProfileRes = await doRequest(reqObj) | ||
| const instagramProfile = JSON.parse(instagramProfileRes) | ||
|
|
||
| // Store the access token, user id, and current profile for user in db | ||
| await models.InstagramUser.upsert({ | ||
| uuid: igUser.username, | ||
| profile: igUser, | ||
| profile: instagramProfile, | ||
| verified: instagramProfile.is_verified, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🙌 |
||
| accessToken | ||
| }) | ||
|
|
||
| return successResponse(igUser) | ||
| return successResponse(instagramProfile) | ||
| } catch (err) { | ||
| return errorResponseBadRequest(err) | ||
| } | ||
|
|
@@ -115,39 +105,6 @@ module.exports = function (app) { | |
| }) | ||
| ) | ||
|
|
||
| app.post( | ||
| '/instagram/profile', | ||
| handleResponse(async (req, res, next) => { | ||
| const { profile } = req.body | ||
| try { | ||
| const checkFields = ['id', 'username'] | ||
| const hasMinimumFields = checkFields.every((field) => field in profile) | ||
| if (!hasMinimumFields) throw new Error('Invalid profile') | ||
|
|
||
| try { | ||
| // Verify the user user id exists in the DB before updating it | ||
| const igUser = await models.InstagramUser.findOne({ | ||
| where: { | ||
| uuid: profile.username | ||
| } | ||
| }) | ||
| if (!igUser) | ||
| throw new Error( | ||
| `Could not find matching ig user in the db: ${profile.username}` | ||
| ) | ||
| igUser.profile = profile | ||
| await igUser.save() | ||
|
|
||
| return successResponse(profile) | ||
| } catch (err) { | ||
| return errorResponseBadRequest(err) | ||
| } | ||
| } catch (err) { | ||
| return errorResponseBadRequest(err) | ||
| } | ||
| }) | ||
| ) | ||
|
|
||
| /** | ||
| * After the user finishes onboarding in the client app and has a blockchain userId, we need to associate | ||
| * the blockchainUserId with the instagram profile | ||
|
|
@@ -168,7 +125,8 @@ module.exports = function (app) { | |
| const isUnassociated = instagramObj && !instagramObj.blockchainUserId | ||
| const handlesMatch = | ||
| instagramObj && | ||
| instagramObj.profile.username.toLowerCase() === user.handle | ||
| instagramObj.profile.username.toLowerCase() === | ||
| user.handle.toLowerCase() | ||
| // only set blockchainUserId if not already set | ||
| if (isUnassociated && handlesMatch) { | ||
| instagramObj.blockchainUserId = userId | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
address*
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thank you 😮💨