Skip to content

Commit

Permalink
Launch Blacksky
Browse files Browse the repository at this point in the history
  • Loading branch information
rudyfraser committed May 24, 2023
1 parent 9395b18 commit 3935863
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 35 deletions.
Binary file added .DS_Store
Binary file not shown.
14 changes: 0 additions & 14 deletions .env.example

This file was deleted.

4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ web_modules/
.env.test.local
.env.production.local
.env.local
.env.prod

# parcel-bundler cache (https://parceljs.org/)
.cache
Expand Down Expand Up @@ -119,6 +120,9 @@ dist
# TernJS port file
.tern-port

# database
tmp/

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

Expand Down
Binary file added scripts/blacksky.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 6 additions & 6 deletions scripts/publishFeedGen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,28 @@ const run = async () => {

// YOUR bluesky handle
// Ex: user.bsky.social
const handle = ''
const handle = process.env.IDENTIFIER

// YOUR bluesky password, or preferably an App Password (found in your client settings)
// Ex: abcd-1234-efgh-5678
const password = ''
const password = process.env.PASSWORD

// A short name for the record that will show in urls
// Lowercase with no spaces.
// Ex: whats-hot
const recordName = ''
const recordName = 'blacksky'

// A display name for your feed
// Ex: What's Hot
const displayName = ''
const displayName = 'Blacksky'

// (Optional) A description of your feed
// Ex: Top trending content from the whole network
const description = ''
const description = 'Posts from the Black users of Bluesky, based on the original thread (https://bsky.app/profile/did:plc:xgjcudwc5yk4z2uex5v6e7bl/post/3jtypsmfaoh2x)'

// (Optional) The path to an image to be used as your feed's avatar
// Ex: ~/path/to/avatar.jpeg
const avatar: string = ''
const avatar: string = 'blacksky.png'

// -------------------------------------
// NO NEED TO TOUCH ANYTHING BELOW HERE
Expand Down
2 changes: 1 addition & 1 deletion src/algos/whats-alf.ts → src/algos/blacksky.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { InvalidRequestError } from '@atproto/xrpc-server'
import { QueryParams } from '../lexicon/types/app/bsky/feed/getFeedSkeleton'
import { AppContext } from '../config'

export const uri = 'at://did:example:alice/app.bsky.feed.generator/whats-alf'
export const uri = 'at://did:plc:w4xbfzo7kqfes5zb7r6qv3rw/app.bsky.feed.generator/blacksky'

export const handler = async (ctx: AppContext, params: QueryParams) => {
let builder = ctx.db
Expand Down
4 changes: 2 additions & 2 deletions src/algos/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import {
QueryParams,
OutputSchema as AlgoOutput,
} from '../lexicon/types/app/bsky/feed/getFeedSkeleton'
import * as whatsAlf from './whats-alf'
import * as blacksky from './blacksky'

type AlgoHandler = (ctx: AppContext, params: QueryParams) => Promise<AlgoOutput>

const algos: Record<string, AlgoHandler> = {
[whatsAlf.uri]: whatsAlf.handler,
[blacksky.uri]: blacksky.handler,
}

export default algos
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const run = async () => {
const serviceDid =
maybeStr(process.env.FEEDGEN_SERVICE_DID) ?? `did:web:${hostname}`
const server = FeedGenerator.create({
port: maybeInt(process.env.FEEDGEN_PORT) ?? 3000,
port: maybeInt(process.env.PORT) ?? 3000,
sqliteLocation: maybeStr(process.env.FEEDGEN_SQLITE_LOCATION) ?? ':memory:',
subscriptionEndpoint:
maybeStr(process.env.FEEDGEN_SUBSCRIPTION_ENDPOINT) ??
Expand Down
44 changes: 34 additions & 10 deletions src/subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,52 @@ import {
OutputSchema as RepoEvent,
isCommit,
} from './lexicon/types/com/atproto/sync/subscribeRepos'
import {
OutputSchema as ThreadPost
} from './lexicon/types/app/bsky/feed/getPostThread'
import { FirehoseSubscriptionBase, getOpsByType } from './util/subscription'
import { AtpAgent, BlobRef } from '@atproto/api'
import dotenv from 'dotenv'

function parseReplies(threadPost) {
let identifiers: any[] = [];
if (threadPost.replies) {
identifiers = [...new Set(threadPost.replies
.map((reply) => {
return parseReplies(reply)
}))]
}
identifiers.push(threadPost.post.author.did)
return identifiers.flat()
}

export class FirehoseSubscription extends FirehoseSubscriptionBase {
async handleEvent(evt: RepoEvent) {
if (!isCommit(evt)) return
const ops = await getOpsByType(evt)
dotenv.config()

// This logs the text of every post off the firehose.
// Just for fun :)
// Delete before actually using
for (const post of ops.posts.creates) {
console.log(post.record.text)
}
const handle = process.env.IDENTIFIER ?? ''
const password = process.env.PASSWORD ?? ''
const uri = process.env.BLACKSKYTHREAD ?? ''

const agent = new AtpAgent({ service: 'https://bsky.social' })
await agent.login({ identifier: handle, password })

const blackskyThread = await agent.api.app.bsky.feed.getPostThread({uri})
const blacksky = new Set(parseReplies(blackskyThread.data.thread))

const ops = await getOpsByType(evt)

const postsToDelete = ops.posts.deletes.map((del) => del.uri)
const postsToCreate = ops.posts.creates
.filter((create) => {
// only alf-related posts
return create.record.text.toLowerCase().includes('alf')
// Filter for authors from the blacksky thread
return blacksky.has(create.author)
})
.map((create) => {
// map alf-related posts to a db row
// Create Blacksky posts in db
console.log(`Blacksky author ${create.author}!`)
console.log(create)
return {
uri: create.uri,
cid: create.cid,
Expand Down
2 changes: 1 addition & 1 deletion src/util/subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export const getOpsByType = async (evt: Commit): Promise<OperationsByType> => {
for (const op of evt.ops) {
const uri = `at://${evt.repo}/${op.path}`
const [collection] = op.path.split('/')

if (op.action === 'update') continue // updates not supported yet

if (op.action === 'create') {
Expand Down

0 comments on commit 3935863

Please sign in to comment.