-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/bsky-webhook: add support for facets
Reference: https://docs.bsky.app/docs/advanced-guides/post-richtext Fixes #9 Signed-off-by: Erisa A <[email protected]>
- Loading branch information
Showing
3 changed files
with
129 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"slices" | ||
"strings" | ||
) | ||
|
||
func bskyMessageToSlackMarkup(bskyMessage BskyMessage) (string, error) { | ||
var slackStringBuilder strings.Builder | ||
|
||
fragments, err := facetsToFragments(bskyMessage) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
for _, fragment := range fragments { | ||
if fragment.Features == nil { | ||
slackStringBuilder.WriteString(fragment.Text) | ||
} else { | ||
uri := "" | ||
for _, feature := range fragment.Features { | ||
if feature.Type == "app.bsky.richtext.facet#link" { | ||
uri = feature.Uri | ||
break | ||
} else if feature.Type == "app.bsky.richtext.facet#mention" { | ||
uri = fmt.Sprintf("https://bsky.app/profile/%s", feature.Did) | ||
break | ||
} else if feature.Type == "app.bsky.richtext.facet#tag" { | ||
uri = fmt.Sprintf("https://bsky.app/hashtag/%s", feature.Tag) | ||
} | ||
} | ||
if uri != "" { | ||
slackStringBuilder.WriteString(fmt.Sprintf("<%s|%s>", uri, fragment.Text)) | ||
} else { | ||
slackStringBuilder.WriteString(fragment.Text) | ||
} | ||
} | ||
} | ||
|
||
return slackStringBuilder.String(), nil | ||
} | ||
|
||
func facetsToFragments(bskyMessage BskyMessage) ([]BskyTextFragment, error) { | ||
facets := bskyMessage.Commit.Record.Facets | ||
runes := []rune(bskyMessage.Commit.Record.Text) | ||
|
||
fragments := []BskyTextFragment{} | ||
|
||
slices.SortStableFunc(facets, func(a, b BskyFacet) int { | ||
return a.Index.ByteStart - b.Index.ByteStart | ||
}) | ||
|
||
textCursor := 0 | ||
facetCursor := 0 | ||
|
||
for facetCursor < len(facets) { | ||
currentFacet := facets[facetCursor] | ||
|
||
if textCursor < currentFacet.Index.ByteStart { | ||
fragments = append(fragments, BskyTextFragment{Text: string(runes[textCursor:currentFacet.Index.ByteStart])}) | ||
} else if textCursor > currentFacet.Index.ByteStart { | ||
facetCursor++ | ||
continue | ||
} | ||
|
||
if currentFacet.Index.ByteStart < currentFacet.Index.ByteEnd { | ||
fragmentText := string(runes[currentFacet.Index.ByteStart:currentFacet.Index.ByteEnd]) | ||
|
||
// dont add the features if the text is blank | ||
if strings.TrimSpace(fragmentText) == "" { | ||
fragments = append(fragments, BskyTextFragment{ | ||
Text: string(runes[currentFacet.Index.ByteStart:currentFacet.Index.ByteEnd]), | ||
}) | ||
} else { | ||
fragments = append(fragments, BskyTextFragment{ | ||
Text: string(runes[currentFacet.Index.ByteStart:currentFacet.Index.ByteEnd]), | ||
Features: currentFacet.Features, | ||
}) | ||
} | ||
} | ||
textCursor = currentFacet.Index.ByteEnd | ||
facetCursor++ | ||
} | ||
if textCursor < len(runes) { | ||
fragments = append(fragments, BskyTextFragment{Text: string(runes[textCursor:])}) | ||
} | ||
|
||
return fragments, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters