From b6e4aca7283bc5d755ab27f8ff70a7639e4af9cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A0=D0=B0=D1=81=D1=83=D0=BB?= Date: Thu, 16 Nov 2023 19:23:33 +0300 Subject: [PATCH 1/3] feat: add twitt connection to topics and guests --- src/network/fetchGraphData/index.ts | 31 ++++++++++++++++++++++------- src/types/index.ts | 7 +++++++ 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/network/fetchGraphData/index.ts b/src/network/fetchGraphData/index.ts index 7c54c8712..de2254e57 100644 --- a/src/network/fetchGraphData/index.ts +++ b/src/network/fetchGraphData/index.ts @@ -436,12 +436,27 @@ export const formatFetchNodes = ( nodes.push({ ...node, scale: getNodeScale(node), - id: node.tweet_id || `${node.unique_id}_${index}`, - ref_id: node.tweet_id || `${node.unique_id}_${index}`, + id: node.ref_id || `${node.unique_id}_${index}`, + ref_id: node.ref_id || `${node.unique_id}_${index}`, image_url: imageUrlsMapper[node.node_type], type: node.type || node.node_type, }) + if (node.node_type === 'tweet') { + if (node.posted_by) { + const currentGuest = { ...node.posted_by, profile_picture: node.profile_picture } as Guests + + if (currentGuest.name && currentGuest.ref_id && node.ref_id) { + guestMap[currentGuest.ref_id] = { + children: [...(guestMap[currentGuest.ref_id]?.children || []), node.ref_id], + imageUrl: currentGuest.profile_picture || '', + name: currentGuest.name, + twitterHandle: currentGuest.twitter_handle, + } + } + } + } + return } @@ -460,7 +475,7 @@ export const formatFetchNodes = ( nodes.push({ ...node, scale: getNodeScale(node), - id: node.ref_id || node.tweet_id || node.id, + id: node.ref_id || node.id, image_url: smallImageUrl, type: node.type || node.node_type, }) @@ -500,13 +515,15 @@ export const formatFetchNodes = ( return } - if (showTitle) { - if (topicMap[topic] && !topicMap[topic].children.includes(refId || showTitle)) { - topicMap[topic].children.push(refId || showTitle) + const value = refId || showTitle + + if (value) { + if (topicMap[topic] && !topicMap[topic].children.includes(value)) { + topicMap[topic].children.push(value) } else { topicMap[topic] = { position: new Vector3(0, 0, 0), - children: [refId || showTitle], + children: [value], } } } diff --git a/src/types/index.ts b/src/types/index.ts index 459071669..04055acfb 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -64,6 +64,7 @@ export type Node = { type?: string weight?: number tweet_id?: string + posted_by?: PostedBy twitter_handle?: string profile_picture?: string verified?: boolean @@ -174,3 +175,9 @@ export type TopicFilter = { export type SubmitErrRes = { error?: { message?: string } } + +type PostedBy = { + name: string + ref_id: string + twitter_handle: string +} From 842fc37d80164b33856bb75fc16ef8f3cc384a82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A0=D0=B0=D1=81=D1=83=D0=BB?= Date: Fri, 17 Nov 2023 03:20:39 +0300 Subject: [PATCH 2/3] feat: add unit test for topic nodes --- src/network/fetchGraphData/__tests__/index.ts | 50 ++++++++++++++++--- src/network/fetchGraphData/index.ts | 48 +++++++++++------- 2 files changed, 74 insertions(+), 24 deletions(-) diff --git a/src/network/fetchGraphData/__tests__/index.ts b/src/network/fetchGraphData/__tests__/index.ts index 8578b91fc..2553a234c 100644 --- a/src/network/fetchGraphData/__tests__/index.ts +++ b/src/network/fetchGraphData/__tests__/index.ts @@ -10,6 +10,27 @@ describe('formatFetchNodes', () => { node_type: 'test', } + const tweetNode = { + date: 1700158742, + latitude: null, + longitude: null, + name: 'Yan | swan.com', + node_type: 'tweet', + posted_by: { + name: 'Marty Bent', + ref_id: '655bdcb1-21c3-411c-aa24-fe7e4c7a248c', + twitter_handle: 'MartyBent', + }, + profile_picture: 'https://pbs.twimg.com/profile_images/1599281471679766530/RhszStuB_normal.jpg', + ref_id: 'a1317497-7ebe-401b-8cfd-fe709bf04e46', + text: '@adam3us @JSeyff @SGJohnsson Where’s the Vitalik presale video where he talks about finding a friendly jurisdiction?', + topics: ['Vitalik', 'Bitcoin', 'presale', 'friendly jurisdiction'], + tweet_id: '1725216924051587516', + twitter_handle: 'skwp', + verified: false, + weight: 2, + } + const dataSeriesNode = { x: 1, y: 1, @@ -108,12 +129,29 @@ describe('formatFetchNodes', () => { expect(formattedResponse.nodes[1].scale).toBe(1.5) expect(formattedResponse.nodes[1].type).toBe('data_series') expect(formattedResponse.nodes[1].index).toBe(1) + }) + + it('should give us the expected output for two different nodes', () => { + const emptyInput = { + data_series: { title: 'test' }, + exact: [node1, dataSeriesNode], + related: [node1, dataSeriesNode], + } + + const formattedResponse = formatFetchNodes(emptyInput) + + expect(formattedResponse.links).toStrictEqual([]) + }) + + it('should create topic nodes for each topic from tweet, and guest node for posted_by', () => { + const emptyInput = { + data_series: { title: 'test' }, + exact: [tweetNode], + related: [], + } + + const result = formatFetchNodes(emptyInput, '1', 'split') - expect(formattedResponse.nodes[2].label).toBe('test2') - expect(formattedResponse.nodes[2].name).toBe('test2') - expect(formattedResponse.nodes[2].node_type).toBe('data_series') - expect(formattedResponse.nodes[2].scale).toBe(1.5) - expect(formattedResponse.nodes[2].type).toBe('data_series') - expect(formattedResponse.nodes[2].index).toBe(2) + expect(result.nodes.length).toBe(tweetNode.topics.length + 2) }) }) diff --git a/src/network/fetchGraphData/index.ts b/src/network/fetchGraphData/index.ts index de2254e57..92507ec78 100644 --- a/src/network/fetchGraphData/index.ts +++ b/src/network/fetchGraphData/index.ts @@ -315,6 +315,28 @@ function generateGuestNodesFromMap( }) } +const generateGuestsMap = ( + currentGuest: Guests, + id: string, + guestMap: Record = {}, +): Record => { + let updatedGuestMap = { ...guestMap } + + if (currentGuest.name && currentGuest.ref_id && id) { + updatedGuestMap = { + ...updatedGuestMap, + [currentGuest.ref_id]: { + children: [...(updatedGuestMap[currentGuest.ref_id]?.children || []), id], + imageUrl: currentGuest.profile_picture || '', + name: currentGuest.name, + twitterHandle: currentGuest.twitter_handle, + }, + } + } + + return updatedGuestMap // Return the new variable +} + export const getGraphData = async (searchterm: string, graphStyle: 'split' | 'force' | 'sphere' | 'earth') => { try { const dataInit = await fetchNodes(searchterm) @@ -410,7 +432,7 @@ export const formatFetchNodes = ( let nodes: NodeExtended[] = [] const topicMap: TopicMap = {} - const guestMap: Record = {} + let guestMap: Record = {} const dataSeries = Array.isArray(dataInit.data_series) ? dataInit.data_series : [] @@ -443,17 +465,12 @@ export const formatFetchNodes = ( }) if (node.node_type === 'tweet') { - if (node.posted_by) { + if (node.posted_by && node.ref_id) { const currentGuest = { ...node.posted_by, profile_picture: node.profile_picture } as Guests - if (currentGuest.name && currentGuest.ref_id && node.ref_id) { - guestMap[currentGuest.ref_id] = { - children: [...(guestMap[currentGuest.ref_id]?.children || []), node.ref_id], - imageUrl: currentGuest.profile_picture || '', - name: currentGuest.name, - twitterHandle: currentGuest.twitter_handle, - } - } + const updatedGuestMap = generateGuestsMap(currentGuest, node.ref_id, guestMap) + + guestMap = { ...guestMap, ...updatedGuestMap } } } @@ -487,14 +504,9 @@ export const formatFetchNodes = ( guestList.forEach((guest) => { const currentGuest = guest as Guests - if (currentGuest.name && currentGuest.ref_id && node.ref_id) { - guestMap[currentGuest.ref_id] = { - children: [...(guestMap[currentGuest.ref_id]?.children || []), node.ref_id], - imageUrl: currentGuest.profile_picture || '', - name: currentGuest.name, - twitterHandle: currentGuest.twitter_handle, - } - } + const updatedGuestMap = generateGuestsMap(currentGuest, node.ref_id!, guestMap) + + guestMap = { ...guestMap, ...updatedGuestMap } }) } }) From a1a91f20e4a64c21f917031816bdf7f65c8ecfb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A0=D0=B0=D1=81=D1=83=D0=BB?= Date: Mon, 20 Nov 2023 01:29:02 +0300 Subject: [PATCH 3/3] fix: rollback removed unit tests --- src/network/fetchGraphData/__tests__/index.ts | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/network/fetchGraphData/__tests__/index.ts b/src/network/fetchGraphData/__tests__/index.ts index 2553a234c..e465dc6c2 100644 --- a/src/network/fetchGraphData/__tests__/index.ts +++ b/src/network/fetchGraphData/__tests__/index.ts @@ -129,18 +129,13 @@ describe('formatFetchNodes', () => { expect(formattedResponse.nodes[1].scale).toBe(1.5) expect(formattedResponse.nodes[1].type).toBe('data_series') expect(formattedResponse.nodes[1].index).toBe(1) - }) - it('should give us the expected output for two different nodes', () => { - const emptyInput = { - data_series: { title: 'test' }, - exact: [node1, dataSeriesNode], - related: [node1, dataSeriesNode], - } - - const formattedResponse = formatFetchNodes(emptyInput) - - expect(formattedResponse.links).toStrictEqual([]) + expect(formattedResponse.nodes[2].label).toBe('test2') + expect(formattedResponse.nodes[2].name).toBe('test2') + expect(formattedResponse.nodes[2].node_type).toBe('data_series') + expect(formattedResponse.nodes[2].scale).toBe(1.5) + expect(formattedResponse.nodes[2].type).toBe('data_series') + expect(formattedResponse.nodes[2].index).toBe(2) }) it('should create topic nodes for each topic from tweet, and guest node for posted_by', () => {