Skip to content

Commit

Permalink
Massive overhaul and cleanup of how daily smartblocks work (Closes #64)…
Browse files Browse the repository at this point in the history
… (#81)

* Massive overhaul and cleanup of how daily smartblocks work (Closes #64)

* allocating daily config methods separately

* handle null daily config

* transform null to default

* Setting enabled toggle

* next run tracker

* Daily config component next run

* Save last run logic

* Set only run on this device

* 1.4.0
  • Loading branch information
dvargas92495 authored Jun 7, 2023
1 parent 84cc88a commit 1ce9c6b
Show file tree
Hide file tree
Showing 15 changed files with 385 additions and 348 deletions.
2 changes: 1 addition & 1 deletion lambdas/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ export const toStatus = (s: string) =>
process.env.NODE_ENV === "development" ? `${s} DEV` : s;
export const fromStatus = (s = "") => s.replace(/ DEV$/, "");

export const isInvalid = (workflow: string) =>
export const isInvalid = (workflow = '') =>
/<%((J(A(VASCRIPT(ASYNC)?)?)?)|(ONBLOCKEXIT)|(IF(TRUE)?)):/.test(workflow);
44 changes: 0 additions & 44 deletions lambdas/smartblocks-daily_put.ts

This file was deleted.

52 changes: 25 additions & 27 deletions lambdas/smartblocks-store_get.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
import { APIGatewayProxyHandler } from "aws-lambda";
import { DynamoDB } from "aws-sdk";
import nanoid from "nanoid";
import {
s3,
dynamo,
headers,
toStatus,
fromStatus,
isInvalid,
} from "./common";
import { s3, dynamo, headers, toStatus, fromStatus, isInvalid } from "./common";

const getWorkflow = ({
item,
graph,
installs,
installs = 0,
}: {
item: DynamoDB.AttributeMap;
item?: DynamoDB.AttributeMap;
graph: string;
installs: number;
installs?: number;
}) =>
s3
.getObject({
Bucket: "roamjs-smartblocks",
Key: `${item.uuid.S}/${item.workflow.S}.json`,
Key: `${item?.uuid.S}/${item?.workflow.S}.json`,
})
.promise()
.then((r) =>
Expand All @@ -31,10 +24,11 @@ const getWorkflow = ({
TableName: "RoamJSSmartBlocks",
Item: {
uuid: { S: nanoid() },
name: { S: item.uuid.S },
name: { S: item?.uuid.S },
author: { S: graph },
workflow: { S: item.workflow.S },
workflow: { S: item?.workflow.S },
status: { S: toStatus("INSTALLED") },
installed: { S: new Date().toJSON() }, // let's see if anyone is actually using this
},
})
.promise()
Expand All @@ -43,7 +37,7 @@ const getWorkflow = ({
.updateItem({
TableName: "RoamJSSmartBlocks",
Key: {
uuid: { S: item.uuid.S },
uuid: { S: item?.uuid.S },
},
UpdateExpression: "SET #s = :s",
ExpressionAttributeNames: {
Expand All @@ -57,7 +51,7 @@ const getWorkflow = ({
)
.then(() => ({
statusCode: 200,
body: JSON.stringify({ workflow: r.Body.toString() }),
body: JSON.stringify({ workflow: r.Body?.toString() }),
headers,
}))
);
Expand Down Expand Up @@ -110,11 +104,11 @@ export const handler: APIGatewayProxyHandler = async (event) => {
const invalid = await s3
.getObject({
Bucket: "roamjs-smartblocks",
Key: `${r.Item.uuid.S}/${r.Item.workflow.S}.json`,
Key: `${r.Item?.uuid.S}/${r.Item?.workflow.S}.json`,
})
.promise()
.then((d) => isInvalid(d.Body.toString()));
if (graph === r.Item.author.S) {
.then((d) => isInvalid(d.Body?.toString()));
if (graph === r.Item?.author.S) {
return {
statusCode: 200,
body: JSON.stringify({
Expand Down Expand Up @@ -145,7 +139,7 @@ export const handler: APIGatewayProxyHandler = async (event) => {
dynamo
.getItem({
TableName: "RoamJSSmartBlocks",
Key: { uuid: { S: r.Item.author.S } },
Key: { uuid: { S: r.Item?.author.S } },
})
.promise(),
]).then(async ([link, publisher]) => ({
Expand All @@ -157,13 +151,15 @@ export const handler: APIGatewayProxyHandler = async (event) => {
installed: !!link.Count,
updatable:
!!link.Count &&
link.Items.reduce(
link.Items?.reduce(
(prev, cur) =>
cur.workflow.S.localeCompare(prev.workflow.S) > 0
(cur.workflow.S || "").localeCompare(
prev.workflow.S || ""
) > 0
? cur
: prev,
{ workflow: { S: "0000" } }
).workflow?.S !== r.Item.workflow.S,
).workflow?.S !== r.Item?.workflow.S,
}),
headers,
}));
Expand Down Expand Up @@ -197,7 +193,7 @@ export const handler: APIGatewayProxyHandler = async (event) => {
.promise()
.then((is) => {
const uuids = Array.from(
new Set(is.Items.map((u) => u.name.S))
new Set((is.Items || []).map((u) => u.name.S))
);
const batches = Math.ceil(uuids.length / 100);
const requests = new Array(batches)
Expand All @@ -218,7 +214,9 @@ export const handler: APIGatewayProxyHandler = async (event) => {
.promise()
)
).then((all) => {
return all.flatMap((a) => a.Responses.RoamJSSmartBlocks);
return all.flatMap(
(a) => a.Responses?.RoamJSSmartBlocks || []
);
});
})
: filterTab === "published"
Expand Down Expand Up @@ -270,7 +268,7 @@ export const handler: APIGatewayProxyHandler = async (event) => {
.then(([items, users]) => ({
statusCode: 200,
body: JSON.stringify({
smartblocks: items
smartblocks: (items || [])
.sort((a, b) => {
const scoreDiff =
Number(b.score?.N || 0) - Number(a.score?.N || 0);
Expand All @@ -286,7 +284,7 @@ export const handler: APIGatewayProxyHandler = async (event) => {
.map(([k, v]) => [k, v.N ? Number(v.N) : v.S || v.SS])
)
),
users: users.map((i) => ({
users: (users || []).map((i) => ({
author: i.uuid.S,
displayName: i.description?.S || "",
})),
Expand Down
6 changes: 3 additions & 3 deletions lambdas/smartblocks-store_put.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export const handler: APIGatewayProxyHandler = awsGetRoamJSUser(
})
.promise()
.then(async (r) => {
const existingWorkflow = r.Items.find(
const existingWorkflow = (r.Items || []).find(
(i) => fromStatus(i.status.S) !== "USER"
);
if (existingWorkflow && existingWorkflow?.uuid?.S !== uuid) {
Expand All @@ -100,7 +100,7 @@ export const handler: APIGatewayProxyHandler = awsGetRoamJSUser(
.promise()
.then((a) => a.Item);
const limit = Number(existingAuthor?.limit?.N) || 5;
const putItem = (existingDisplayName: string) =>
const putItem = (existingDisplayName = "") =>
dynamo
.query({
TableName: "RoamJSSmartBlocks",
Expand All @@ -117,7 +117,7 @@ export const handler: APIGatewayProxyHandler = awsGetRoamJSUser(
})
.promise()
.then((qr) => {
return qr.Count >= limit
return (qr.Count || 0) >= limit
? {
statusCode: 401,
body: `Not allowed to publish more than ${limit} workflows. Reach out to [email protected] about increasing your limit.`,
Expand Down
4 changes: 0 additions & 4 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ module "roamjs_lambda" {
path = "smartblocks-store",
method = "put"
},
{
path = "smartblocks-daily",
method = "put"
},
{
path = "smartblocks-store",
method = "delete"
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "smartblocks",
"version": "1.3.7",
"version": "1.4.0",
"description": "Create custom and programmable templates from within Roam!",
"main": "./build/main.js",
"scripts": {
Expand Down
89 changes: 0 additions & 89 deletions src/DailyConfig.tsx

This file was deleted.

Loading

0 comments on commit 1ce9c6b

Please sign in to comment.