Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add twitt connection to topics and guests #605

Merged
merged 3 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/network/fetchGraphData/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -116,4 +137,16 @@ describe('formatFetchNodes', () => {
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', () => {
const emptyInput = {
data_series: { title: 'test' },
exact: [tweetNode],
related: [],
}

const result = formatFetchNodes(emptyInput, '1', 'split')

expect(result.nodes.length).toBe(tweetNode.topics.length + 2)
})
})
61 changes: 45 additions & 16 deletions src/network/fetchGraphData/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,28 @@ function generateGuestNodesFromMap(
})
}

const generateGuestsMap = (
currentGuest: Guests,
id: string,
guestMap: Record<string, guestMapChild> = {},
): Record<string, guestMapChild> => {
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)
Expand Down Expand Up @@ -410,7 +432,7 @@ export const formatFetchNodes = (
let nodes: NodeExtended[] = []

const topicMap: TopicMap = {}
const guestMap: Record<string, guestMapChild> = {}
let guestMap: Record<string, guestMapChild> = {}

const dataSeries = Array.isArray(dataInit.data_series) ? dataInit.data_series : []

Expand All @@ -436,12 +458,22 @@ 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 && node.ref_id) {
const currentGuest = { ...node.posted_by, profile_picture: node.profile_picture } as Guests

const updatedGuestMap = generateGuestsMap(currentGuest, node.ref_id, guestMap)

guestMap = { ...guestMap, ...updatedGuestMap }
}
}

return
}

Expand All @@ -460,7 +492,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,
})
Expand All @@ -472,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 }
})
}
})
Expand All @@ -500,13 +527,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],
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -174,3 +175,9 @@ export type TopicFilter = {
export type SubmitErrRes = {
error?: { message?: string }
}

type PostedBy = {
name: string
ref_id: string
twitter_handle: string
}
Loading