Skip to content

Commit

Permalink
Merge pull request #807 from near/develop
Browse files Browse the repository at this point in the history
weekly promotion of develop to main
  • Loading branch information
charleslavon authored May 9, 2024
2 parents 1322be7 + fb8825c commit 157a757
Show file tree
Hide file tree
Showing 8 changed files with 341 additions and 108 deletions.
66 changes: 66 additions & 0 deletions indexers/feed/relationships.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,70 @@
{
"moderated_posts_with_reposts_feed": {
"object_relationships": [
{
"name": "account",
"using": {
"manual_configuration": {
"column_mapping": {
"account_id": "account_id"
},
"insertion_order": null,
"remote_table": {
"name": "accounts",
"schema": "dataplatform_near_accounts"
}
}
}
},
{
"name": "verifications",
"using": {
"manual_configuration": {
"column_mapping": {
"account_id": "account_id"
},
"insertion_order": null,
"remote_table": {
"name": "account",
"schema": "dataplatform_near_verifications"
}
}
}
}
],
"array_relationships": [
{
"name": "comments",
"using": {
"manual_configuration": {
"column_mapping": {
"id": "post_id"
},
"insertion_order": null,
"remote_table": {
"name": "moderated_comments",
"schema": "dataplatform_near_feed"
}
}
}
},
{
"name": "post_likes",
"using": {
"manual_configuration": {
"column_mapping": {
"id": "post_id"
},
"insertion_order": null,
"remote_table": {
"name": "post_likes",
"schema": "dataplatform_near_social_feed"
}
}
}
}
]
},
"moderated_posts": {
"object_relationships": [
{
Expand Down
108 changes: 94 additions & 14 deletions indexers/social_feed/social_feed.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Block } from "@near-lake/primitives";
import * as primitives from "@near-lake/primitives";
/**
* Note: We only support javascript at the moment. We will support Rust, Typescript in a further release.
*/
Expand All @@ -12,9 +12,7 @@ import { Block } from "@near-lake/primitives";
*
* @param {block} Block - A Near Protocol Block
*/
async function getBlock(block: Block) {
console.log(block.actions());

async function getBlock(block: primitives.Block) {
function base64decode(encodedValue) {
let buff = Buffer.from(encodedValue, "base64");
return JSON.parse(buff.toString("utf-8"));
Expand Down Expand Up @@ -243,17 +241,13 @@ async function getBlock(block: Block) {
}
}

async function handlePromotion(
async function handlePromotion(
accountId,
blockHeight,
blockTimestamp,
receiptId,
promoteString
) {

try {


const promotion = JSON.parse(promoteString);
console.log("Promotion", promotion);
const promotionOperation = promotion.value.operation;
Expand All @@ -271,27 +265,25 @@ async function getBlock(block: Block) {
} else {
// if an operation is implemented, we can handle it here
console.log("Operation not implemented");
} } catch (error) {
console.log("Failed to parse promotion content. Skipping...", error);
}
}

async function _handleAddPromotion(
async function _handleAddPromotion(
promotion,
accountId,
blockHeight,
blockTimestamp,
receiptId
) {

try {
// Add your code here
const postAuthor = promotion.value.post.path.split("/")[0];
const postBlockHeight = promotion.value.post.blockHeight;
const promotionType = promotion.value.type;

console.log("Post Author", postAuthor);
console.log("Post Block Height", postBlockHeight);
console.log("Promotion Type", promotionType);
try {
const posts = await context.db.Posts.select(
{ account_id: postAuthor, block_height: postBlockHeight },
1
Expand Down Expand Up @@ -325,6 +317,78 @@ async function getBlock(block: Block) {
}
}

async function handleRepost(
accountId,
blockHeight,
blockTimestamp,
receiptId,
repostContent
) {
try {

const content = JSON.parse(repostContent);

if (content[1]?.value?.type !== "repost") {
console.log("Skipping non-repost content", content);
return;
}

const postAuthor = content[1].key.path.split("/")[0];
const postBlockHeight = content[1].key.blockHeight;

try {
const posts = await context.db.Posts.select(
{ account_id: postAuthor, block_height: postBlockHeight },
1
);
console.log(`posts: ${JSON.stringify(posts)}`, posts);
if (posts.length == 0) {
return;
}

const post = posts[0];
const accountsReposted =
post.accounts_reposted.length === 0
? post.accounts_reposted
: JSON.parse(post.accounts_reposted);
if (accountsReposted.indexOf(accountId) === -1) {
accountsReposted.push(accountId);
}

try {
const repostData = {
post_id: post.id,
account_id: accountId,
content: JSON.stringify(content),
block_height: blockHeight,
block_timestamp: blockTimestamp,
receipt_id: receiptId,
};
// Call GraphQL mutation to insert a new repost
await context.db.Reposts.insert(repostData);
console.log(`Repost by ${accountId} has been added to the database`);

// Call GraphQL mutation to update a post's reposted accounts list
await context.db.Posts.update(
{ id: post.id },
{ accounts_reposted: JSON.stringify(accountsReposted) }
);
console.log(`Repost by ${accountId} has been added to the database`);
} catch (e) {
console.log(
`Failed to store repost to the post ${postAuthor}/${postBlockHeight} by ${accountId} perhaps it has already been stored. Error ${e}`
);
}
} catch (e) {
console.log(
`Failed to store repost to the post ${postAuthor}/${postBlockHeight} as we don't have it stored in the first place. Error ${e}`
);
}
} catch (error) {
console.log("Failed to parse repost content. Skipping...", error);
}
}

// Add your code here
const SOCIAL_DB = "social.near";

Expand Down Expand Up @@ -450,6 +514,22 @@ async function getBlock(block: Block) {
postAction.args.data[accountId].index.promote
);
}

// Probably repost action is happening
if (
Object.keys(postAction.args.data[accountId].index).includes(
"repost"
)
) {
console.log("handling repost");
await handleRepost(
accountId,
blockHeight,
blockTimestamp,
postAction.receiptId,
postAction.args.data[accountId].index.repost
);
}
}
})
);
Expand Down
16 changes: 14 additions & 2 deletions indexers/social_feed/social_feed.sql
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ CREATE TABLE
"receipt_id" VARCHAR NOT NULL,
CONSTRAINT "post_likes_pkey" PRIMARY KEY ("post_id", "account_id")
);

CREATE TABLE
"promote" (
"id" SERIAL NOT NULL,
Expand All @@ -42,7 +42,19 @@ CREATE TABLE
"block_timestamp" DECIMAL(20, 0) NOT NULL,
"promotion_type" TEXT NOT NULL,
"post_id" SERIAL NOT NULL
);
);

CREATE TABLE
"reposts" (
"id" SERIAL NOT NULL,
"post_id" SERIAL NOT NULL,
"account_id" VARCHAR NOT NULL,
"content" TEXT NOT NULL,
"block_height" DECIMAL(58, 0) NOT NULL,
"block_timestamp" DECIMAL(20, 0) NOT NULL,
"receipt_id" VARCHAR NOT NULL,
CONSTRAINT "reposts_pkey" PRIMARY KEY ("post_id", "account_id")
);

CREATE UNIQUE INDEX "posts_account_id_block_height_key" ON "posts" ("account_id" ASC, "block_height" ASC);

Expand Down
10 changes: 6 additions & 4 deletions src/ActivityFeeds/PostsFeedControls.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ const createQuery = (type, isUpdate) => {

return `
query FeedQuery($offset: Int, $limit: Int) {
dataplatform_near_feed_moderated_posts(${queryFilter} order_by: [${querySortOption} { block_height: desc }], offset: $offset, limit: $limit) {
dataplatform_near_feed_moderated_posts_with_reposts_feed(${queryFilter} order_by: [${querySortOption} { block_height: desc }], offset: $offset, limit: $limit) {
account_id
block_height
block_timestamp
Expand All @@ -183,11 +183,13 @@ query FeedQuery($offset: Int, $limit: Int) {
tags
}
}
dataplatform_near_feed_moderated_posts_aggregate(${queryFilter} order_by: {id: asc}) {
dataplatform_near_feed_moderated_posts_with_reposts_feed_aggregate(${queryFilter} order_by: {id: asc}) {
aggregate {
count
}
}
}
`;
};
Expand Down Expand Up @@ -217,8 +219,8 @@ const loadMorePosts = (isUpdate) => {
}
let data = result.body.data;
if (data) {
const newPosts = data.dataplatform_near_feed_moderated_posts;
const postsCountLeft = data.dataplatform_near_feed_moderated_posts_aggregate.aggregate.count;
const newPosts = data.dataplatform_near_feed_moderated_posts_with_reposts_feed;
const postsCountLeft = data.dataplatform_near_feed_moderated_posts_with_reposts_feed_aggregate.aggregate.count;
if (newPosts.length > 0) {
let filteredPosts = newPosts.filter((i) => !shouldFilter(i, postsModerationKey));
filteredPosts = filteredPosts.map((post) => {
Expand Down
15 changes: 13 additions & 2 deletions src/Posts/Feed.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ const loadMorePosts = props.loadMorePosts;
const hasMore = props.hasMore || false;
const posts = props.posts || [];

console.log("posts", posts);

const Post = styled.div`
border-bottom: 1px solid #eceef0;
padding: 24px 0 12px;
Expand All @@ -21,6 +19,17 @@ const renderItem = (item) => {
if (item.accounts_liked.length !== 0) {
item.accounts_liked = JSON.parse(item.accounts_liked);
}

if (item.content.includes("repost")) {
const repostData = JSON.parse(item.content);
const fullPath = repostData[0].value.item.path.split("/");
item.repostData = {
reposted_by: item.account_id,
reposted_content: JSON.parse(item.content),
original_post_accountId: fullPath[0],
original_post_blockHeight: repostData[0].value.item.blockHeight,
};
}
return (
<Post className="post" key={item.block_height + "_" + item.account_id}>
<Widget
Expand All @@ -36,6 +45,8 @@ const renderItem = (item) => {
verifications: item.verifications,
showFlagAccountFeature: props.showFlagAccountFeature ?? false,
profile: item.profile,
isRespost: item.isRespost,
repostData: item.repostData,
}}
/>
</Post>
Expand Down
Loading

0 comments on commit 157a757

Please sign in to comment.