Skip to content
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

Fix twitter token outdated #25

Merged
merged 3 commits into from
Jul 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
242 changes: 185 additions & 57 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"build": "nuxt-ts build && backpack build",
"start": "cross-env NODE_ENV=production node build/main.js",
"start-elasticbeanstalk": "NODE_ENV=production node build/main.js",
"generate": "nuxt-ts generate"
"generate": "nuxt-ts generate",
"postinstall": "patch-package"
},
"dependencies": {
"axios": "^0.21.1",
Expand All @@ -39,6 +40,7 @@
"morgan": "~1.10.0",
"nuxt": "^2.14.6",
"nuxt-property-decorator": "^2.8.8",
"patch-package": "^6.4.7",
"pg": "^8.3.3",
"sass-loader": "^10.0.2",
"sequelize": "^6.3.5",
Expand Down
16 changes: 16 additions & 0 deletions patches/sequelize+6.3.5.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
diff --git a/node_modules/sequelize/types/lib/model.d.ts b/node_modules/sequelize/types/lib/model.d.ts
index cad5ff3..146d87e 100644
--- a/node_modules/sequelize/types/lib/model.d.ts
+++ b/node_modules/sequelize/types/lib/model.d.ts
@@ -875,6 +875,11 @@ export interface UpdateOptions<TAttributes = any> extends Logging, Transactionab
* Return the affected rows (only for postgres)
*/
returning?: boolean;
+ /**
+ * Return the affected rows without sequelize wrapping (only for postgres when using returning)
+ */
+ raw?: boolean
+

/**
* How many rows to update (only for mysql and mariadb)
1 change: 1 addition & 0 deletions scripts/postService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AxiosInstance, AxiosRequestConfig } from 'axios';
import { flintError } from '~/types/flintgg';

const apiurl: string = '/api/';

Expand Down
112 changes: 81 additions & 31 deletions server/easy-share/db-queries.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,46 @@
import { Transaction, Sequelize, Op } from 'sequelize';
import { flintId } from '~/types/flintgg';
import { asyncForEach } from '../../scripts/helper/helperFunctions';
import { sequelize } from '../db';
import {
easyshareHashtag,
trackedUser,
gphotosTokens,
switch_share_user_type,
switchShareUser,
easyshareEvent,
switch_share_user_type_with_ph,
switch_share_user_type_without_ph,
switchShareUserWithPh,
switchShareUserWithoutPh,
switchStat,
easyshareAccountType,
getSwitchHashtagNumbers,
easyshareSource,
} from './enums';
import { switch_share_user, switch_share_events } from './models';

export const cachedUsers = new Map<flintId, switch_share_user_type>();
export const cachedUsers = new Map<flintId, switchShareUser>();

export async function updateUserInCache(
userid: flintId,
passedUser?: switchShareUser | null,
) {
let user = passedUser;
if (user === undefined) {
user = (await switch_share_user.findByPk(userid, {
raw: true,
})) as switchShareUser | null;
}
if (user) {
cachedUsers.set(user.id, user);
} else {
cachedUsers.delete(userid);
}
}

export async function fillCache() {
const allUsers = await switch_share_user.findAll({ raw: true });
allUsers.forEach((u) => cachedUsers.set(u.id, u as switch_share_user_type));
await asyncForEach(allUsers, async (u) => {
await updateUserInCache(u.id, u as switchShareUser);
});
}

export async function addEvent(
Expand All @@ -44,7 +64,7 @@ export async function addEvent(

export async function getUser(id: flintId) {
const user = await switch_share_user.findByPk(id);
return user as switch_share_user_type | null;
return user;
}

export async function getUserStats(author: flintId) {
Expand Down Expand Up @@ -135,7 +155,7 @@ export async function createUser(
) {
const now = new Date();
const hashtags = [easyshareHashtag.NintendoSwitch, easyshareHashtag.PS4share];
const user: switch_share_user_type = {
const user: switchShareUser = {
id,
type,
created: now,
Expand All @@ -145,15 +165,15 @@ export async function createUser(
token: tw.oauth_token,
token_secret: tw.oauth_token_secret,
name: tw.screen_name,
ph_album: undefined,
ph_refresh_token: undefined,
ph_token: undefined,
ph_token_expiry: undefined,
ph_album: null,
ph_refresh_token: null,
ph_token: null,
ph_token_expiry: null,
};
await sequelize.transaction(async (t) => {
await switch_share_user.upsert(user, { transaction: t });
await addEvent(id, easyshareEvent.signup, easyshareSource.webclient, t);
cachedUsers.set(id, user);
await updateUserInCache(id, user);
});
return user;
}
Expand All @@ -166,7 +186,7 @@ export async function removeUser(id: flintId) {
transaction: t,
});
await switch_share_user.destroy({ where: { id }, transaction: t });
cachedUsers.delete(id);
await updateUserInCache(id, null);
});
}

Expand All @@ -186,9 +206,14 @@ export async function connectPhotos(
ph_album: album,
ph_token_expiry: new Date(ph.expiry_date),
},
{ where: { id }, returning: true, transaction: t },
{
where: { id },
returning: true,
raw: true,
transaction: t,
},
);
const user = response[1][0] as switch_share_user_type_with_ph;
const user = response[1][0] as switchShareUserWithPh;
if (!onlyUpdatingTokens) {
await addEvent(
id,
Expand All @@ -197,7 +222,7 @@ export async function connectPhotos(
t,
);
}
cachedUsers.set(id, user);
await updateUserInCache(id, user);
return user;
});
}
Expand All @@ -213,16 +238,21 @@ export async function disconnectPhotos(id: flintId) {
ph_album: null,
ph_token_expiry: null,
},
{ where: { id }, returning: true, transaction: t },
{
where: { id },
returning: true,
raw: true,
transaction: t,
},
);
const user = response[1][0] as switch_share_user_type_without_ph;
const user = response[1][0] as switchShareUserWithoutPh;
await addEvent(
id,
easyshareEvent.unlinkPhotos,
easyshareSource.webclient,
t,
);
cachedUsers.set(id, user);
await updateUserInCache(id, user);
return user;
});
}
Expand All @@ -237,10 +267,15 @@ export async function cleanOutdatedHashtags() {
{
hashtags: u.hashtags.filter((ht) => getSwitchHashtagNumbers().includes(ht)),
},
{ where: { id: u.id }, returning: true, transaction: t },
{
where: { id: u.id },
returning: true,
raw: true,
transaction: t,
},
);
const user = response[1][0] as switch_share_user_type;
cachedUsers.set(u.id, user);
const user = response[1][0] as switchShareUser;
await updateUserInCache(u.id, user);
});
console.info(
`[Hashtag cleanup] Cleaned up hashtags of ${potentiallyOutdatedUsers.length} users.`,
Expand All @@ -261,16 +296,21 @@ export async function updateConfiguration(
hashtags,
autoDelete,
},
{ where: { id }, returning: true, transaction: t },
{
where: { id },
returning: true,
raw: true,
transaction: t,
},
);
const user = response[1][0] as switch_share_user_type;
const user = response[1][0] as switchShareUser;
await addEvent(
id,
easyshareEvent.changeSettings,
easyshareSource.webclient,
t,
);
cachedUsers.set(id, user);
await updateUserInCache(id, user);
return user;
});
}
Expand All @@ -283,16 +323,21 @@ export async function addUserEmail(id: flintId, email: string) {
updated: now,
email,
},
{ where: { id }, returning: true, transaction: t },
{
where: { id },
returning: true,
raw: true,
transaction: t,
},
);
const user = response[1][0] as switch_share_user_type;
const user = response[1][0] as switchShareUser;
await addEvent(
user.id,
easyshareEvent.updateEmail,
easyshareSource.webclient,
t,
);
cachedUsers.set(id, user);
await updateUserInCache(id, user);
return user;
});
}
Expand All @@ -305,16 +350,21 @@ export async function removeUserEmail(id: flintId) {
updated: now,
email: null,
},
{ where: { id }, returning: true, transaction: t },
{
where: { id },
returning: true,
raw: true,
transaction: t,
},
);
const user = response[1][0] as switch_share_user_type;
const user = response[1][0] as switchShareUser;
await addEvent(
user.id,
easyshareEvent.updateEmail,
easyshareSource.webclient,
t,
);
cachedUsers.set(id, user);
await updateUserInCache(id, user);
return user;
});
}
27 changes: 15 additions & 12 deletions server/easy-share/enums.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { flintId } from '~/types/flintgg';

export enum easyshareAccountType {
'twitter' = 1,
'facebook',
Expand All @@ -18,7 +20,10 @@ export enum easyshareHashtag {
}

export function getSwitchHashtagNumbers() {
return Object.values(easyshareHashtag).filter((ht) => !isNaN(ht as any));
return Object.values(easyshareHashtag).filter(
// eslint-disable-next-line no-restricted-globals
(ht) => !isNaN(ht as number),
) as Array<easyshareHashtag>;
}

export enum easyshareEvent {
Expand Down Expand Up @@ -49,7 +54,7 @@ export type gphotosTokens = {

export type linkedUser = gphotosTokens & { albumId?: string };

type base_share_user_type = {
type switchShareUserBase = {
created: Date;
updated: Date;
id: flintId;
Expand All @@ -59,26 +64,24 @@ type base_share_user_type = {
token_secret: string;
name: string;
type: easyshareAccountType;
email?: string;
email?: string | null;
};

export type switch_share_user_type_with_ph = base_share_user_type & {
export type switchShareUserWithPh = switchShareUserBase & {
ph_token: string;
ph_refresh_token: string;
ph_album: string;
ph_token_expiry: Date;
};

export type switch_share_user_type_without_ph = base_share_user_type & {
ph_token: undefined;
ph_refresh_token: undefined;
ph_album: undefined;
ph_token_expiry: undefined;
export type switchShareUserWithoutPh = switchShareUserBase & {
ph_token: undefined | null;
ph_refresh_token: undefined | null;
ph_album: undefined | null;
ph_token_expiry: undefined | null;
};

export type switch_share_user_type =
| switch_share_user_type_without_ph
| switch_share_user_type_with_ph;
export type switchShareUser = switchShareUserWithoutPh | switchShareUserWithPh;

export type switchStat = {
amount: number;
Expand Down
11 changes: 6 additions & 5 deletions server/easy-share/gphotos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import flintURL from '../../scripts/flintURL';
import {
gphotosTokens,
easyshareEvent,
switch_share_user_type,
switch_share_user_type_with_ph,
switchShareUser,
switchShareUserWithPh,
easyshareSource,
} from './enums';
import { addEvent, connectPhotos } from './db-queries';
import { flintId } from '~/types/flintgg';

const { PHOTOS_CLIENT_SECRET } = process.env;

Expand Down Expand Up @@ -63,7 +64,7 @@ async function getOrCreatePhotosAlbum(user: gphotosTokens) {
return album;
}

async function refreshToken(user: switch_share_user_type) {
async function refreshToken(user: switchShareUser) {
const response = await Axios.post<{
access_token: string;
expires_in: number;
Expand All @@ -88,7 +89,7 @@ async function refreshToken(user: switch_share_user_type) {
} as gphotosTokens;
}

async function getAPI(user: switch_share_user_type_with_ph) {
async function getAPI(user: switchShareUserWithPh) {
// if it runs out in 30 seconds or less
const needsToRefresh = user.ph_token_expiry.getTime() < Date.now() - 30 * 1000;
let u = user;
Expand Down Expand Up @@ -152,7 +153,7 @@ async function downloadImageWithRetry(
}

export async function uploadMedia(
user: switch_share_user_type_with_ph,
user: switchShareUserWithPh,
fileURLs: Array<string>,
source: easyshareSource,
) {
Expand Down
2 changes: 2 additions & 0 deletions server/easy-share/mailchimp.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import axios from 'axios';
import md5 from 'md5';
import { flintId } from '~/types/flintgg';
import { mailchimpResponse } from '~/types/mailchimp';
import { mailchimpSubscribe } from '../../types/enums';
import { getUser, addUserEmail } from './db-queries';

Expand Down
Loading