Skip to content

Commit

Permalink
remove decodeHTMLEntities utility
Browse files Browse the repository at this point in the history
  • Loading branch information
skiniks committed Nov 29, 2024
1 parent 758bb6f commit 5fbc886
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 45 deletions.
Binary file modified bun.lockb
Binary file not shown.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
"devDependencies": {
"@antfu/eslint-config": "^2.27.3",
"@atproto/api": "^0.12.29",
"@types/he": "^1.2.3",
"@types/node": "^20.17.9",
"@vercel/node": "^3.2.27",
"eslint": "^8.57.1",
"he": "^1.2.0",
"typescript": "^5.7.2"
}
}
73 changes: 35 additions & 38 deletions services/bsky.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { AppBskyFeedPost, AtpAgent, RichText } from '@atproto/api'
import { AtpAgent, RichText } from '@atproto/api'
import { BSKY_PASSWORD, BSKY_USERNAME } from '../config'
import { decodeHtmlEntities } from '../utils/decodeHtmlEntities'
import { getImageAsBuffer } from '../utils/getImageAsBuffer'
import { shortenUrl } from '../utils/shortenUrl'
import { getRandomIntro } from '../utils/getRandomIntro'
Expand All @@ -26,29 +25,51 @@ interface PetDetails {
}
}

interface BskyImage {
alt: string
image: {
$type: 'blob'
ref: { $link: string }
mimeType: string
size: number
}
}

function createAltText(details: PetDetails): string {
let breedStr = details.breeds.primary || 'unknown breed'
if (details.breeds.secondary)
breedStr += ` and ${details.breeds.secondary}`
else if (details.breeds.mixed && !breedStr.toLowerCase().includes('mix'))
breedStr += ' mix'

let species = details.species.toLowerCase()
if (breedStr.toLowerCase().includes(species))
species = ''
else species = `${species}, `

const location = `${details.contact.address.city}, ${details.contact.address.state}`
return `${details.name} is a ${breedStr} ${species}available for adoption in ${location}.`
}

export async function createPost(petDetails: PetDetails): Promise<boolean> {
try {
const agent = new AtpAgent({ service: 'https://bsky.social' })
await agent.login({ identifier: BSKY_USERNAME!, password: BSKY_PASSWORD! })

petDetails.name = decodeHtmlEntities(petDetails.name)
if (petDetails.description)
petDetails.description = decodeHtmlEntities(petDetails.description)

const imageBuffers = await Promise.all(petDetails.photoUrls.slice(0, 4).map(url => getImageAsBuffer(url)))
const images: Array<{ $type: string, alt: string, image: { $type: string, ref: string } }> = []
const images: BskyImage[] = []

for (const buffer of imageBuffers) {
if (buffer) {
const upload = await agent.api.com.atproto.repo.uploadBlob(buffer, { encoding: 'image/jpeg' })
const blobRef = {
$type: 'blob',
ref: upload.data.blob.ref,
}
images.push({
$type: 'app.bsky.embed.images#image',
alt: createAltText(petDetails),
image: blobRef,
image: {
$type: 'blob',
ref: upload.data.blob.ref,
mimeType: upload.data.blob.mimeType,
size: upload.data.blob.size,
},
})
}
else {
Expand All @@ -64,7 +85,7 @@ export async function createPost(petDetails: PetDetails): Promise<boolean> {
const rt = new RichText({ text: postText })
await rt.detectFacets(agent)

const postRecord: AppBskyFeedPost.Record = {
const postRecord = {
$type: 'app.bsky.feed.post',
text: rt.text,
facets: rt.facets,
Expand All @@ -75,12 +96,6 @@ export async function createPost(petDetails: PetDetails): Promise<boolean> {
createdAt: new Date().toISOString(),
}

const validation = AppBskyFeedPost.validateRecord(postRecord)
if (!validation.success) {
console.error('Invalid Post Record:', validation.error)
return false
}

await agent.api.com.atproto.repo.createRecord({
repo:
agent.session?.did
Expand All @@ -91,28 +106,10 @@ export async function createPost(petDetails: PetDetails): Promise<boolean> {
record: postRecord,
})

// eslint-disable-next-line no-console
console.log('Post created successfully:', postRecord)
return true
}
catch (err) {
console.error('Error creating post:', err)
return false
}
}

function createAltText(details: PetDetails): string {
let breedStr = details.breeds.primary || 'unknown breed'
if (details.breeds.secondary)
breedStr += ` and ${details.breeds.secondary}`
else if (details.breeds.mixed && !breedStr.toLowerCase().includes('mix'))
breedStr += ' mix'

let species = details.species.toLowerCase()
if (breedStr.toLowerCase().includes(species))
species = ''
else species = `${species}, `

const location = `${details.contact.address.city}, ${details.contact.address.state}`
return `${details.name} is a ${breedStr} ${species}available for adoption in ${location}.`
}
5 changes: 0 additions & 5 deletions utils/decodeHtmlEntities.ts

This file was deleted.

0 comments on commit 5fbc886

Please sign in to comment.