{
"function_name": "notification_v2",
"code": "\n const nearDBIndexUpdates = block\n .actions()\n .filter((action) => action.receiverId === \"social.near\")\n .flatMap((action) =>\n action.operations\n .map((operation) => operation[\"FunctionCall\"])\n .filter((operation) => operation?.methodName === \"set\")\n .map((functionCallOperation) => ({\n ...functionCallOperation,\n args: base64decode(functionCallOperation.args),\n receiptId: action.receiptId,\n }))\n .filter((functionCall) => {\n if (\n !functionCall ||\n !functionCall.args ||\n !functionCall.args.data ||\n !Object.keys(functionCall.args.data) ||\n !Object.keys(functionCall.args.data)[0]\n ) {\n console.log(\n \"Set operation did not have arg data in expected format\"\n );\n return;\n }\n const accountId = Object.keys(functionCall.args.data)[0];\n if (!functionCall.args.data[accountId]) {\n console.log(\n \"Set operation did not have arg data in expected format\"\n );\n return;\n }\n return Object.keys(functionCall.args.data[accountId]).includes(\n \"index\"\n );\n })\n );\n\n if (nearDBIndexUpdates.length > 0) {\n console.log(`Found Index keys in Block #${block.blockHeight} ...`);\n const blockHeight = block.blockHeight + \"\";\n await Promise.all(\n nearDBIndexUpdates.map(async (writeAction) => {\n const accountId = Object.keys(writeAction.args.data)[0];\n console.log(`ACCOUNT_ID: ${accountId}`);\n\n if (Object.keys(writeAction.args.data[accountId]).includes(\"index\")) {\n if (!writeAction.args.data[accountId].index) {\n console.log(\"Wrong notify format, index is null\");\n return;\n }\n if (\n Object.keys(writeAction.args.data[accountId].index).includes(\n \"notify\"\n )\n ) {\n console.log(\"Handle notify\", accountId, blockHeight);\n await handleNotify(\n accountId,\n blockHeight,\n writeAction.args.data[accountId].index.notify,\n writeAction.receiptId\n );\n }\n }\n })\n );\n }\n\n async function handleNotify(\n accountId,\n blockHeight,\n notifyPayload,\n receiptId\n ) {\n try {\n let data = JSON.parse(notifyPayload);\n data = Array.isArray(data) ? data : [data];\n const mutationPayload = data.map((notification) => {\n const { key, value } = notification;\n const {\n message,\n item = {},\n type: valueType,\n post: devhubPostId,\n } = value;\n const { path, type: itemType, blockHeight: actionAtBlockHeight } = item;\n // actionAtBlockHeight related to \"like\" itemType\n\n return {\n initiatedBy: accountId,\n receiver: key,\n message,\n valueType,\n itemType,\n path,\n blockHeight,\n devhubPostId,\n actionAtBlockHeight,\n receiptId,\n };\n });\n\n console.log(`handling notification triggered by ${accountId}`);\n\n //send graphql mutation\n await context.db.Notifications.upsert(\n mutationPayload,\n [\"receiptId\"],\n [\n \"initiatedBy\",\n \"receiver\",\n \"message\",\n \"valueType\",\n \"itemType\",\n \"path\",\n \"devhubPostId\",\n \"actionAtBlockHeight\",\n ]\n );\n console.log(\"Successfully stored Notify record\");\n } catch (e) {\n console.log(\"Failed to store Notify record\");\n console.log(\"error: \", e);\n }\n }\n\n function base64decode(encodedValue) {\n try {\n const buff = Buffer.from(encodedValue, \"base64\");\n const str = buff.toString(\"utf-8\").replace(/\\\\xa0/g, \" \");\n return JSON.parse(str);\n } catch (e) {\n console.log(\"Error while decoding value\", e);\n return null;\n }\n }\n",
"schema": "CREATE TABLE\n \"notifications\" (\n \"id\" SERIAL,\n \"blockHeight\" DECIMAL(58, 0) NOT NULL,\n \"initiatedBy\" TEXT NOT NULL,\n \"receiver\" TEXT NOT NULL,\n \"valueType\" TEXT NOT NULL,\n \"message\" TEXT NULL,\n \"itemType\" TEXT NULL,\n \"path\" TEXT NULL,\n \"devhubPostId\" INT NULL,\n \"actionAtBlockHeight\" INT NULL,\n \"receiptId\" VARCHAR NULL,\n PRIMARY KEY (\"id\")\n )\n",
"start_block": {
"HEIGHT": 107269518
},
"rule": {
"kind": "ACTION_ANY",
"affected_account_id": "social.near",
"status": "SUCCESS"
}
}