Skip to content

Commit

Permalink
Improve Next.js JSON deserisation and Run object handling
Browse files Browse the repository at this point in the history
See this thread: vercel/next.js#11993
  • Loading branch information
KiwiKid committed Jan 22, 2022
1 parent b332680 commit 33915ef
Show file tree
Hide file tree
Showing 11 changed files with 249 additions and 118 deletions.
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"presets": ["next/babel"],
"plugins": ["react-optimized-image/plugin", "@babel/plugin-proposal-class-properties"]
"plugins": ["react-optimized-image/plugin", "@babel/plugin-proposal-class-properties", "superjson-next"]
}
59 changes: 34 additions & 25 deletions components/Locations/APIClients/NotionClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { Client } from '@notionhq/client'
import { getMinutesAgo, removeStringEnds } from '../../utils/utils';
import SocialPostRun from './SocialPostRun';

const mapNotionItemToOverride = (notionRow:any):LocationOverride => {

Expand All @@ -22,33 +23,41 @@ const getNotionRichText = (notionParam:any):string => {
// "[bla bla,another bla]" => ["bla bla", "another bla"]
const getNotionHackyArray = (notionParam:any):string[] => removeStringEnds(getNotionRichText(notionParam)).split(',')

const getNotionDate = (notionParam:any):Date|null => {
const getNotionDate = (notionParam:any):string => {
return notionParam.date ? notionParam.date.start : null
}


const getNotionMultiSelectFirst = (notionParam:any):string => {
return notionParam && notionParam.multi_select.length > 1 ? notionParam.multi_select[0].name : ''
}
/* END - Notion types ==> NZCovidMap types */


const mapNotionItemToRedditPostRun = (notionRow:any):RedditPostRun => {
const mapNotionItemToSocialPostRun = (notionRow:any):SocialPostRun => {

const props = notionRow.properties;
return {
primaryUrlParam: getNotionRichText(props.primaryUrlParam) ,
subreddit: getNotionRichText(props.subreddit),
textUrlParams: getNotionHackyArray(props.textUrlParams),
postId: getNotionRichText(props.currentPostId),
postTitle: getNotionRichText(props.currentPostTitle),
notionPageId: notionRow.id,
lastCheckTime: getNotionDate(props.lastCheckTime),
flareId: getNotionRichText(props.flareId)


return new SocialPostRun(
notionRow.id
, getNotionRichText(props.subreddit)
, getNotionRichText(props.primaryUrlParam)
, getNotionHackyArray(props.textUrlParams)
, getNotionMultiSelectFirst(props.type)//getSocialPostRunType(props.type)
, getNotionRichText(props.currentPostTitle)
, getNotionRichText(props.currentPostId)
, getNotionDate(props.lastCheckTime)
, getNotionRichText(props.flareId)

)
/* showInDrawer: props.showInDrawer.checkbox,
lat: props.lat.number,
lng: props.lng.number,
matchingMohCityString: ,
title: getNotionRichText(props.cityTitle),
urlParam: getNotionRichText(props.urlParam),
zoom: props.zoom.number*/

}
}

const mapNotionItemToLocationPreset = (notionRow:any):LocationPreset => {
Expand Down Expand Up @@ -77,8 +86,8 @@ class NotionClient {
cachedLocationPresets!:LocationPreset[];
cachedLocationPresetsUpdateTime!:Date;

cachedRedditPosts!:RedditPostRun[];
cachedRedditPostsUpdateTime!:Date;
cachedSocialPosts!:SocialPostRun[];
cachedSocialPostsUpdateTime!:Date;


constructor (){
Expand Down Expand Up @@ -172,13 +181,13 @@ class NotionClient {
}


getRedditPostRuns = async ():Promise<RedditPostRun[]> => {
getSocialPostRuns = async ():Promise<SocialPostRun[]> => {
if(!this.redditDbId){ throw 'No reddit posts db set'}

// This is mainly to keep the heat of the reddit API during local development.
if(!this.cachedRedditPosts
|| this.cachedRedditPosts.length === 0
|| getMinutesAgo(this.cachedRedditPostsUpdateTime) > 1
if(!this.cachedSocialPosts
|| this.cachedSocialPosts.length === 0
|| getMinutesAgo(this.cachedSocialPostsUpdateTime) > 1
){
return this.notionClient.databases.query({
database_id: this.redditDbId,
Expand All @@ -189,18 +198,18 @@ class NotionClient {
},
},
})
.then((r) => r.results.map(mapNotionItemToRedditPostRun))
.then((r) => r.results.map(mapNotionItemToSocialPostRun))
.then((rs) => {
this.cachedRedditPosts = rs;
this.cachedRedditPostsUpdateTime = new Date();
this.cachedSocialPosts = rs;
this.cachedSocialPostsUpdateTime = new Date();
return rs;
});
} else {
return this.cachedRedditPosts;
return this.cachedSocialPosts;
}
}

setRedditPostProcessed = async (notionPageId:string, checkTime:Date):Promise<void> => {
setSocialPostProcessed = async (notionPageId:string, checkTime:Date):Promise<void> => {
console.log(`Setting check time for notionPageId: ${notionPageId}`)
let newProps:any = {};

Expand All @@ -215,7 +224,7 @@ class NotionClient {
});
}

setRedditPostProcessedUpdated = async (notionPageId:string, checkTime:Date, postTitle:string, postId:string):Promise<void> => {
setSocialPostProcessedUpdated = async (notionPageId:string, checkTime:Date, postTitle:string, postId:string):Promise<void> => {
console.log(`Updating notion db entry with post details ${postTitle} ${postId} : ${notionPageId}`)
return this.notionClient.pages.update({
page_id: notionPageId,
Expand Down
50 changes: 29 additions & 21 deletions components/Locations/APIClients/RedditClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import snoowrap, { Submission } from 'snoowrap'
import getConfig from 'next/config';
import { getNodeEnv } from '../../utils/getNodeEnv';
import { debug, error } from 'console';
import RedditPostRunResult from './RedditPostRunResult';
import RedditPostRunResult from './SocialPostRunResult';
import { startOfDayNZ, todayNZ } from '../DateHandling';
import dayjs from 'dayjs';
import SocialPostRunResult from './SocialPostRunResult';
import SocialPostRun from './SocialPostRun';


class RedditClient {
Expand Down Expand Up @@ -38,10 +40,10 @@ class RedditClient {
this.r = r;
}
/*
updateRedditComment = async (run:RedditPostRun, title:string, text:string):Promise<RedditPostRunResult> => {
updateRedditComment = async (run:SocialPostRun, SubredditSubmissionTitleQuery:string, text:string):Promise<SocialPostRunResult> => {
try{
return this.r.getSubreddit(run.subreddit)
.search({time: 'day', sort: 'new', query: title })
.search({time: 'day', sort: 'new', query: SubredditSubmissionTitleQuery })
.first()
.map((thread:any) => this.r.getSubmission(thread).expandReplies().then((comments:any) => {
console.log(comments);
Expand All @@ -55,9 +57,9 @@ class RedditClient {
console.log(`edit ${postId}`);
try{
return this.r.getSubmission(postId).po
.then((r:any):RedditPostRunResult => new RedditPostRunResult(true, true, false, run, postId, title));
.then((r:any):SocialPostRunResult => new SocialPostRunResult(true, true, false, run, postId, title));
} catch(err) {
return new RedditPostRunResult(false, false, true, run, null, err); //{ success: false, update: false, subreddit: run.subreddit, error: err }
return new SocialPostRunResult(false, false, true, run, null, null, err); //{ success: false, update: false, subreddit: run.subreddit, error: err }
}
}else{
console.log(`submitSelfpost`);
Expand All @@ -79,23 +81,23 @@ class RedditClient {
}catch(err){
console.error('Update Reddit Submissions failed')
console.error(err);
return new RedditPostRunResult(false, false, true, run, undefined, err); //{ success: false, update: false, subreddit: run.subreddit, error: err }
return new RedditPostRunResult(false, false, true, run, undefined, undefined, err); //{ success: false, update: false, subreddit: run.subreddit, error: err }
}
}
*/
*/

updateRedditSubmissions = async (run:RedditPostRun, title:string, text:string):Promise<RedditPostRunResult> => {
updateRedditSubmissions = async (run:SocialPostRun, title:string, text:string):Promise<SocialPostRun> => {
try{
const isUpdate = run.lastCheckTime && startOfDayNZ(run.lastCheckTime) === startOfDayNZ(todayNZ())
if(isUpdate && run.postId){
console.log(`Reddit Submission - edit ${run.subreddit} ${run.postId}`);
return await this.r.getSubmission(run.postId)
const isUpdate = run.lastCheckTime && startOfDayNZ(new Date(run.lastCheckTime)) === startOfDayNZ(todayNZ())
if(isUpdate && run.existingPostId){
console.log(`Reddit Submission - edit ${run.subreddit} ${run.existingPostId}`);
return await this.r.getSubmission(run.existingPostId)
.edit(text)
.then((sub:any) => {
console.log('Reddit Submission edited')
return processRedditSubmission(true, true, false, run, sub.json.data.things[0].name, title)

.then(async (sub:any) => {
console.log('Reddit Submission edited');
run.setResults(await processRedditSubmission(true, true, false, run, sub.json.data.things[0].name, title));
return run;
})

//return new RedditPostRunResult(false, false, true, run, "FAKE", undefined);
Expand All @@ -106,27 +108,33 @@ class RedditClient {
subredditName: run.subreddit
, title: title
, text: text
, flairId: run.flareId
, flairId: run.flairId
})

//9bc8c692-2377-11ec-97a0-722625a13049
// Use https://www.reddit.com/r/[INSERT_SUBREDDIT_HERE]/api/link_flair_v2.json?raw_json=1
// to find the Flair id

return selfPost.then((sub:Submission) => processRedditSubmission(true, false, false, run, sub.name, title));
return selfPost.then(async (sub:Submission) => {
const res = await processRedditSubmission(true, false, false, run, sub.name, title);
run.setResults(res);
return run;
});
//return new RedditPostRunResult(false, false, true, run, "FAKE", undefined);
}

}catch(err){
console.error(`Update Reddit Submissions failed for r/${run.subreddit} ${run.textUrlParams}`)
console.error(err);
return new RedditPostRunResult(false, false, true, run, undefined, undefined, err); //{ isSuccess: false, isUpdate: false, subreddit: run.subreddit, error: err }
run.setError(err);
return run;
}
}

}

const processRedditSubmission = async (isSuccess:boolean, isUpdate:boolean, isSkipped: boolean, run:RedditPostRun, subId:string, title:string):Promise<RedditPostRunResult> => {
return new RedditPostRunResult(isSuccess, isUpdate, isSkipped, run, title, subId);
const processRedditSubmission = async (isSuccess:boolean, isUpdate:boolean, isSkipped: boolean, run:SocialPostRun, subId:string, title:string):Promise<RedditPostRunResult> => {
return new RedditPostRunResult(isSuccess, isUpdate, isSkipped, title, subId);
}

export default RedditClient;
63 changes: 63 additions & 0 deletions components/Locations/APIClients/SocialPostRun.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@

import SocialPostRunResult from './SocialPostRunResult';

// This class is pretty gross, but it works...
class SocialPostRun {
notionPageId:string
subreddit:string // eg newzealand
primaryUrlParam:string
textUrlParams:string[]

type:string // "Reddit_Comment", "Reddit_Comment"
createdDate:string
flairId?:string

existingPostTitle?:string
existingPostId?:string
lastCheckTime?:string

result?:SocialPostRunResult
error?:any

constructor (
notionPageId:string
,subreddit:string
,primaryUrlParam:string
, textUrlParams:string[]
,type:string
, existingPostTitle?:string
,existingPostId?:string
,lastCheckTime?:string
,flairId?:string
){

this.notionPageId = notionPageId;
this.subreddit = subreddit;
this.primaryUrlParam = primaryUrlParam;
this.textUrlParams = textUrlParams;
if(existingPostTitle){
this.existingPostTitle = existingPostTitle;
}
if(existingPostId){
this.existingPostId = existingPostId;
}
if(flairId){
this.flairId = flairId
}

this.createdDate = new Date().toISOString();
this.type = type
this.lastCheckTime = lastCheckTime;
}


setError(err:any) {
this.error(err);
}

setResults(result:SocialPostRunResult){
this.result = result
}
}

export default SocialPostRun;
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,28 @@ import snoowrap, { Submission } from 'snoowrap'
import getConfig from 'next/config';
import { getNodeEnv } from '../../utils/getNodeEnv';
import { error } from 'console';
import SocialPostRun from './SocialPostRun';


class RedditPostRunResult {

// This class is pretty gross, but it works...
class SocialPostRunResult {
isSuccess:boolean
isUpdate:boolean
isSkipped:boolean
postId?:string
postTitle?:string
run:RedditPostRun
error?:any
createdDate:Date

constructor (isSuccess:boolean,isUpdate:boolean,isSkipped:boolean, run:RedditPostRun, postTitle?:string, postId?:string, error?:any){
constructor (isSuccess:boolean,isUpdate:boolean,isSkipped:boolean, postTitle?:string, postId?:string){

this.isSuccess = isSuccess;
this.isSkipped = isSkipped;
this.isUpdate = isUpdate;
this.run = run;



if(postTitle){
this.postTitle = postTitle;
}
Expand All @@ -31,6 +36,11 @@ class RedditPostRunResult {
}
this.createdDate = new Date();
}


setError(err:any){
this.error(err);
}
}

export default RedditPostRunResult;
export default SocialPostRunResult;
4 changes: 4 additions & 0 deletions components/types/PostRunType.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
enum PostRunType {
RedditPost = "RedditPost",
RedditComment = "RedditComment",
}
11 changes: 0 additions & 11 deletions components/types/RedditPostRun.d.ts

This file was deleted.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"@types/lodash": "^4.14.177",
"@types/react-dom": "^17.0.11",
"axios": "^0.24.0",
"babel-plugin-superjson-next": "^0.4.2",
"chrome-aws-lambda": "^10.1.0",
"csv-parse": "^5.0.3",
"dayjs": "^1.10.7",
Expand All @@ -33,6 +34,7 @@
"sharp": "^0.29.3",
"sitemap": "^7.0.0",
"snoowrap": "^1.23.0",
"superjson": "^1.8.0",
"swr": "^1.1.0"
},
"devDependencies": {
Expand Down
Loading

0 comments on commit 33915ef

Please sign in to comment.