Skip to content

Commit

Permalink
feat: text with mentions
Browse files Browse the repository at this point in the history
  • Loading branch information
dalechyn committed Oct 28, 2024
1 parent 5019d44 commit 010e3f3
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 38 deletions.
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
text with embeds
message publishing
4 changes: 2 additions & 2 deletions playground/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ const client = Client.create(
// console.timeEnd('bio')

const castWithReactions = await Actions.Cast.getCastWithReactions(client, {
hash: '0xc87c75ab61c15b38ec72be6d6fd6d08c73d39155',
fid: 11517n,
hash: '0x62cda93dc3889a6f0c819cd4d9392b3af2cb452a',
fid: 862185n,
})
console.dir(castWithReactions, { depth: null })
// biome-ignore lint/suspicious/noConsoleLog: <explanation>
Expand Down
24 changes: 13 additions & 11 deletions src/Internal/Actions/Cast/getAllCastMessagesByFid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,19 @@ export async function Actions_Cast_getAllCastMessagesByFid(
return hex
})()
return {
messages: message.messages.map((message) => {
if (
message.data?.type === MessageType.CAST_REMOVE &&
message.data.body.case === 'castRemoveBody'
)
return {
type: 'removed' as const,
hash: Hex.fromBytes(message.data.body.value.targetHash),
}
return { type: 'casted', cast: Cast_fromMessage(message) }
}),
messages: await Promise.all(
message.messages.map(async (message) => {
if (
message.data?.type === MessageType.CAST_REMOVE &&
message.data.body.case === 'castRemoveBody'
)
return {
type: 'removed' as const,
hash: Hex.fromBytes(message.data.body.value.targetHash),
}
return { type: 'casted', cast: await Cast_fromMessage(client, message) }
}),
),
nextPageToken,
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Internal/Actions/Cast/getCast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export async function Actions_Cast_getCast(
options,
)

return Cast_fromMessage(message)
return Cast_fromMessage(client, message)
}

Actions_Cast_getCast.parseError = (error: unknown) =>
Expand Down
4 changes: 3 additions & 1 deletion src/Internal/Actions/Cast/getCastsByFid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ export async function Actions_Cast_getCastsByFid(
return hex
})()
return {
casts: message.messages.map(Cast_fromMessage),
casts: await Promise.all(
message.messages.map((message) => Cast_fromMessage(client, message)),
),
nextPageToken,
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/Internal/Actions/Cast/getCastsByMention.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ export async function Actions_Cast_getCastsByMention(
return hex
})()
return {
casts: message.messages.map(Cast_fromMessage),
casts: await Promise.all(
message.messages.map((message) => Cast_fromMessage(client, message)),
),
nextPageToken,
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/Internal/Actions/Cast/getCastsByParent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ export async function Actions_Cast_getCastsByParent(
return hex
})()
return {
casts: message.messages.map(Cast_fromMessage),
casts: await Promise.all(
message.messages.map((message) => Cast_fromMessage(client, message)),
),
nextPageToken,
}
}
Expand Down
43 changes: 33 additions & 10 deletions src/Internal/Cast/fromMessage.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Actions_UserData_getUserDataUsername } from '../Actions/UserData/getUserDataUsername.js'
import type { Client } from '../Client/types.js'
import { Embed_fromMessage } from '../Embed/fromMessage.js'
import type { Embed } from '../Embed/types.js'
import type { GlobalErrorType } from '../Errors/error.js'
Expand All @@ -10,9 +12,10 @@ import {
} from './errors.js'
import type { Cast } from './types.js'

export function Cast_fromMessage(
export async function Cast_fromMessage(
client: Client,
message: Message,
): Cast_fromMessage.ReturnType {
): Promise<Cast_fromMessage.ReturnType> {
const meta = Meta_fromMessage(message)

// @TODO: separate error here
Expand Down Expand Up @@ -47,19 +50,39 @@ export function Cast_fromMessage(
})()
const parent = Parent_fromMessage(message.data.body.value.parent)

const rawText = message.data.body.value.text
return {
meta,
isLong,
mentions,
embeds,
embedsDeprecated: (() => {
if (message.data.body.value.embedsDeprecated.length === 0)
return undefined
return message.data.body.value.embedsDeprecated
})(),
fid: message.data.fid,
timestamp: message.data.timestamp,
text: message.data.body.value.text,
text: {
value: await (async () => {
if (!mentions) return ''

let chars = rawText.split('')
const mentionsUsernames = await Promise.all(
mentions.map(async (mention) => ({
username: await Actions_UserData_getUserDataUsername(client, {
fid: mention.fid,
}),
...mention,
})),
)
for (const mention of mentionsUsernames.reverse()) {
chars = [
...chars.slice(0, mention.position),
'@',
mention.username,
...chars.slice(mention.position),
]
}
return chars.join('')
})(),
mentions,
embeds,
raw: rawText,
},
parent,
}
}
Expand Down
21 changes: 11 additions & 10 deletions src/Internal/Cast/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ export type Cast = {
meta: Meta
fid: bigint
timestamp: number
/** @deprecated Depcrecated by Farcaster. Use `embeds`. */
embedsDeprecated: string[] | undefined
embeds: Embed[] | undefined
mentions:
| {
fid: bigint
position: number
}[]
| undefined
parent: Parent | undefined
text: string
text: {
value: string
embeds: Embed[] | undefined
mentions:
| {
fid: bigint
position: number
}[]
| undefined
raw: string
}
isLong: boolean
}

0 comments on commit 010e3f3

Please sign in to comment.