Skip to content

Commit

Permalink
fix patreon login
Browse files Browse the repository at this point in the history
  • Loading branch information
vintikzzz committed Feb 18, 2024
1 parent d5b361c commit 577c064
Show file tree
Hide file tree
Showing 122 changed files with 2,555 additions and 1,979 deletions.
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.7.18
66 changes: 36 additions & 30 deletions api/dist/patreon.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,35 @@ const {
roles
} = require('./roles');

function makeUser(d, patreonCampaignId, patreonRoles) {
const user = {
id: d.data.id,
firstName: d.data.attributes.first_name,
lastName: d.data.attributes.last_name,
email: d.data.attributes.email,
role: roles.NOBODY
};
const tierIds = jp.query(d, `$.included[?(@.type=="member" && @.relationships.campaign.data.id == "${patreonCampaignId}")].relationships.currently_entitled_tiers..id`);

for (const [role, id] of Object.entries(patreonRoles)) {
if (tierIds.includes(id)) user.role = role;
}

const lastChargeStatus = jp.value(d, `$.included[?(@.type=="member" && @.relationships.campaign.data.id == "${patreonCampaignId}")].attributes.last_charge_status`);

if (lastChargeStatus && lastChargeStatus !== 'Paid') {
user.role = roles.NOBODY;
}

const isCreator = jp.value(d, `$.included[?(@.type=="campaign" && @.id == "${patreonCampaignId}")].relationships.creator.data.id`) == user.id;

if (isCreator) {
user.role = roles.SPARKLING_CREATOR;
}

return user;
}

module.exports = function (config, app, passport) {
app.use((req, res, next) => {
req.config.patreon = config.patreon !== undefined;
Expand All @@ -26,12 +55,13 @@ module.exports = function (config, app, passport) {
}

const patreonCampaignId = config.patreon.campaignId;
const patreonTiers = [roles.BRONZE_BACKER, roles.SILVER_BACKER, roles.GOLD_BACKER, roles.EMERALD_SPONSOR, roles.RUBY_SPONSOR];
const patreonRoles = {};
patreonRoles[config.patreon.tiers[0]] = roles.BRONZE_BACKER;
patreonRoles[config.patreon.tiers[1]] = roles.SILVER_BACKER;
patreonRoles[config.patreon.tiers[2]] = roles.GOLD_BACKER;
patreonRoles[config.patreon.tiers[3]] = roles.EMERALD_SPONSOR;
patreonRoles[config.patreon.tiers[4]] = roles.RUBY_SPONSOR;

for (let i = 0; i < config.patreon.tiers.length; i++) {
patreonRoles[patreonTiers[i]] = config.patreon.tiers[i];
}

passport.use('patreon', new OAuth2Strategy({
authorizationURL: baseUrl + '/oauth2/authorize',
tokenURL: baseUrl + '/api/oauth2/token',
Expand All @@ -57,31 +87,7 @@ module.exports = function (config, app, passport) {

const d = JSON.parse(res.body);
console.log('patreon json body', res.body);
const user = {
id: d.data.id,
firstName: d.data.attributes.first_name,
lastName: d.data.attributes.last_name,
email: d.data.attributes.email,
role: roles.NOBODY
};
const tierIds = jp.query(d, `$.included[?(@.type=="member" && @.relationships.campaign.data.id == "${patreonCampaignId}")].relationships.currently_entitled_tiers..id`);

for (const id in patreonRoles) {
if (tierIds.includes(id)) user.role = patreonRoles[id];
}

const lastChargeStatus = jp.value(d, `$.included[?(@.type=="member" && @.relationships.campaign.data.id == "${patreonCampaignId}")].attributes.last_charge_status`);

if (lastChargeStatus && lastChargeStatus !== 'Paid') {
user.role = roles.NOBODY;
}

const isCreator = jp.value(d, `$.included[?(@.type=="campaign" && @.id == "${patreonCampaignId}")].relationships.creator.data.id`) == user.id;

if (isCreator) {
user.role = roles.SPARKLING_CREATOR;
}

const user = makeUser(d, patreonCampaignId, patreonRoles);
console.log('patreon user', user);
done(null, user);
});
Expand Down
96 changes: 96 additions & 0 deletions api/dist/test/patreon.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
"use strict";

const assert = require('assert');

const rewire = require('rewire');

const patreon = rewire('../patreon.js');

const {
roles
} = require('../roles');

const tiers = ["3981231", "3972747", "3981014", "3981042", "3981061"];
const patreonCampaignId = 3166075;
const patreonTiers = [roles.BRONZE_BACKER, roles.SILVER_BACKER, roles.GOLD_BACKER, roles.EMERALD_SPONSOR, roles.RUBY_SPONSOR];
const patreonRoles = {};

for (let i = 0; i < tiers.length; i++) {
patreonRoles[patreonTiers[i]] = tiers[i];
}

const makeUser = patreon.__get__('makeUser');

describe('makeUser function', () => {
it('should return user with valid patreonCampaignId and patreonRoles', () => {
const d = {
"data": {
"attributes": {
"first_name": "John",
"last_name": "Doe",
"email": "[email protected]"
},
"id": "123",
"relationships": {
"memberships": {
"data": [{
"id": "9e3b60ce-25d4-470c-a0a1-f907b34ece89",
"type": "member"
}]
},
"type": "user"
}
},
"included": [{
"attributes": {
"last_charge_status": "Paid"
},
"id": "9e3b60ce-25d4-470c-a0a1-f907b34ece89",
"relationships": {
"campaign": {
"data": {
"id": "3166075",
"type": "campaign"
},
"links": {
"related": "https://www.patreon.com/api/oauth2/v2/campaigns/3166075"
}
},
"currently_entitled_tiers": {
"data": [{
"id": "3972747",
"type": "tier"
}, {
"id": "3981231",
"type": "tier"
}]
}
},
"type": "member"
}, {
"attributes": {},
"id": "3166075",
"type": "campaign"
}, {
"attributes": {},
"id": "3972747",
"type": "tier"
}, {
"attributes": {},
"id": "3981231",
"type": "tier"
}],
"links": {
"self": "https://www.patreon.com/api/oauth2/v2/user/123"
}
};
const user = makeUser(d, patreonCampaignId, patreonRoles);
assert.deepStrictEqual(user, {
id: '123',
firstName: 'John',
lastName: 'Doe',
email: '[email protected]',
role: 'SILVER_BACKER'
});
});
});
59 changes: 34 additions & 25 deletions api/src/patreon.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,34 @@ const querystring = require('querystring');
const request = require('request');
const { roles } = require('./roles');

function makeUser(d, patreonCampaignId, patreonRoles) {
const user = {
id: d.data.id,
firstName: d.data.attributes.first_name,
lastName: d.data.attributes.last_name,
email: d.data.attributes.email,
role: roles.NOBODY,
};

const tierIds = jp.query(d, `$.included[?(@.type=="member" && @.relationships.campaign.data.id == "${patreonCampaignId}")].relationships.currently_entitled_tiers..id`);
for (const [role, id] of Object.entries(patreonRoles)) {
if (tierIds.includes(id)) user.role = role;
}

const lastChargeStatus = jp.value(d, `$.included[?(@.type=="member" && @.relationships.campaign.data.id == "${patreonCampaignId}")].attributes.last_charge_status`);
if (lastChargeStatus && lastChargeStatus !== 'Paid') {
user.role = roles.NOBODY;
}

const isCreator = jp.value(d, `$.included[?(@.type=="campaign" && @.id == "${patreonCampaignId}")].relationships.creator.data.id`) == user.id;
if (isCreator) {
user.role = roles.SPARKLING_CREATOR;
}

return user;
}


module.exports = function (config, app, passport) {
app.use((req, res, next) => {
req.config.patreon = config.patreon !== undefined;
Expand All @@ -16,12 +44,11 @@ module.exports = function (config, app, passport) {
callbackHost = `https://${config.domain}`;
}
const patreonCampaignId = config.patreon.campaignId;
const patreonTiers = [roles.BRONZE_BACKER, roles.SILVER_BACKER, roles.GOLD_BACKER, roles.EMERALD_SPONSOR, roles.RUBY_SPONSOR];
const patreonRoles = {}
patreonRoles[config.patreon.tiers[0]] = roles.BRONZE_BACKER;
patreonRoles[config.patreon.tiers[1]] = roles.SILVER_BACKER;
patreonRoles[config.patreon.tiers[2]] = roles.GOLD_BACKER;
patreonRoles[config.patreon.tiers[3]] = roles.EMERALD_SPONSOR;
patreonRoles[config.patreon.tiers[4]] = roles.RUBY_SPONSOR;
for (let i = 0; i < config.patreon.tiers.length; i++) {
patreonRoles[patreonTiers[i]] = config.patreon.tiers[i];
}
passport.use('patreon', new OAuth2Strategy({
authorizationURL: baseUrl + '/oauth2/authorize',
tokenURL: baseUrl + '/api/oauth2/token',
Expand All @@ -47,25 +74,7 @@ module.exports = function (config, app, passport) {
}
const d = JSON.parse(res.body);
console.log('patreon json body', res.body);
const user = {
id: d.data.id,
firstName: d.data.attributes.first_name,
lastName: d.data.attributes.last_name,
email: d.data.attributes.email,
role: roles.NOBODY,
};
const tierIds = jp.query(d, `$.included[?(@.type=="member" && @.relationships.campaign.data.id == "${patreonCampaignId}")].relationships.currently_entitled_tiers..id`);
for (const id in patreonRoles) {
if (tierIds.includes(id)) user.role = patreonRoles[id];
}
const lastChargeStatus = jp.value(d, `$.included[?(@.type=="member" && @.relationships.campaign.data.id == "${patreonCampaignId}")].attributes.last_charge_status`);
if (lastChargeStatus && lastChargeStatus !== 'Paid') {
user.role = roles.NOBODY;
}
const isCreator = jp.value(d, `$.included[?(@.type=="campaign" && @.id == "${patreonCampaignId}")].relationships.creator.data.id`) == user.id;
if (isCreator) {
user.role = roles.SPARKLING_CREATOR;
}
const user = makeUser(d, patreonCampaignId, patreonRoles);
console.log('patreon user', user);
done(null, user);
});
Expand All @@ -77,4 +86,4 @@ module.exports = function (config, app, passport) {
passport.deserializeUser(function (id, done) {
done(null, id);
});
}
}
104 changes: 104 additions & 0 deletions api/src/test/patreon.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
const assert = require('assert');
const rewire = require('rewire');
const patreon = rewire('../patreon.js');
const { roles } = require('../roles');
const tiers = [
"3981231",
"3972747",
"3981014",
"3981042",
"3981061"
];
const patreonCampaignId = 3166075;
const patreonTiers = [roles.BRONZE_BACKER, roles.SILVER_BACKER, roles.GOLD_BACKER, roles.EMERALD_SPONSOR, roles.RUBY_SPONSOR];
const patreonRoles = {};
for (let i = 0; i < tiers.length; i++) {
patreonRoles[patreonTiers[i]] = tiers[i];
}
const makeUser = patreon.__get__('makeUser');

describe('makeUser function', () => {
it('should return user with valid patreonCampaignId and patreonRoles', () => {
const d = {
"data": {
"attributes": {
"first_name": "John",
"last_name": "Doe",
"email": "[email protected]"
},
"id": "123",
"relationships": {
"memberships": {
"data": [
{
"id": "9e3b60ce-25d4-470c-a0a1-f907b34ece89",
"type": "member"
}
]
},
"type": "user"
}
},
"included": [
{
"attributes": {
"last_charge_status": "Paid"
},
"id": "9e3b60ce-25d4-470c-a0a1-f907b34ece89",
"relationships": {
"campaign": {
"data": {
"id": "3166075",
"type": "campaign"
},
"links": {
"related": "https://www.patreon.com/api/oauth2/v2/campaigns/3166075"
}
},
"currently_entitled_tiers": {
"data": [
{
"id": "3972747",
"type": "tier"
},
{
"id": "3981231",
"type": "tier"
}
]
}
},
"type": "member"
},
{
"attributes": {},
"id": "3166075",
"type": "campaign"
},
{
"attributes": {},
"id": "3972747",
"type": "tier"
},
{
"attributes": {},
"id": "3981231",
"type": "tier"
}
],
"links": {
"self": "https://www.patreon.com/api/oauth2/v2/user/123"
}
};

const user = makeUser(d, patreonCampaignId, patreonRoles);
assert.deepStrictEqual(user, {
id: '123',
firstName: 'John',
lastName: 'Doe',
email: '[email protected]',
role: 'SILVER_BACKER',
});
});

});
Loading

0 comments on commit 577c064

Please sign in to comment.