[🐴] send record via link in text (Record DMs - base PR) (#4227)
* send record via link in text * re-trim text after removing link
This commit is contained in:
+2
-13
@@ -4,7 +4,6 @@ import {
|
||||
AppBskyEmbedRecord,
|
||||
AppBskyEmbedRecordWithMedia,
|
||||
AppBskyFeedThreadgate,
|
||||
AppBskyRichtextFacet,
|
||||
BskyAgent,
|
||||
ComAtprotoLabelDefs,
|
||||
ComAtprotoRepoUploadBlob,
|
||||
@@ -15,7 +14,7 @@ import {AtUri} from '@atproto/api'
|
||||
import {logger} from '#/logger'
|
||||
import {ThreadgateSetting} from '#/state/queries/threadgate'
|
||||
import {isNetworkError} from 'lib/strings/errors'
|
||||
import {shortenLinks} from 'lib/strings/rich-text-manip'
|
||||
import {shortenLinks, stripInvalidMentions} from 'lib/strings/rich-text-manip'
|
||||
import {isNative, isWeb} from 'platform/detection'
|
||||
import {ImageModel} from 'state/models/media/image'
|
||||
import {LinkMeta} from '../link-meta/link-meta'
|
||||
@@ -81,17 +80,7 @@ export async function post(agent: BskyAgent, opts: PostOpts) {
|
||||
opts.onStateChange?.('Processing...')
|
||||
await rt.detectFacets(agent)
|
||||
rt = shortenLinks(rt)
|
||||
|
||||
// filter out any mention facets that didn't map to a user
|
||||
rt.facets = rt.facets?.filter(facet => {
|
||||
const mention = facet.features.find(feature =>
|
||||
AppBskyRichtextFacet.isMention(feature),
|
||||
)
|
||||
if (mention && !mention.did) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
})
|
||||
rt = stripInvalidMentions(rt)
|
||||
|
||||
// add quote embed if present
|
||||
if (opts.quote) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import {RichText, UnicodeString} from '@atproto/api'
|
||||
import {AppBskyRichtextFacet, RichText, UnicodeString} from '@atproto/api'
|
||||
|
||||
import {toShortUrl} from './url-helpers'
|
||||
|
||||
@@ -10,9 +10,7 @@ export function shortenLinks(rt: RichText): RichText {
|
||||
// enumerate the link facets
|
||||
if (rt.facets) {
|
||||
for (const facet of rt.facets) {
|
||||
const isLink = !!facet.features.find(
|
||||
f => f.$type === 'app.bsky.richtext.facet#link',
|
||||
)
|
||||
const isLink = !!facet.features.find(AppBskyRichtextFacet.isLink)
|
||||
if (!isLink) {
|
||||
continue
|
||||
}
|
||||
@@ -33,3 +31,21 @@ export function shortenLinks(rt: RichText): RichText {
|
||||
}
|
||||
return rt
|
||||
}
|
||||
|
||||
// filter out any mention facets that didn't map to a user
|
||||
export function stripInvalidMentions(rt: RichText): RichText {
|
||||
if (!rt.facets?.length) {
|
||||
return rt
|
||||
}
|
||||
rt = rt.clone()
|
||||
if (rt.facets) {
|
||||
rt.facets = rt.facets?.filter(facet => {
|
||||
const mention = facet.features.find(AppBskyRichtextFacet.isMention)
|
||||
if (mention && !mention.did) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
})
|
||||
}
|
||||
return rt
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user