forked from badlogic/skychat
-
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.
- Loading branch information
0 parents
commit dfa3da4
Showing
25 changed files
with
10,896 additions
and
0 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,8 @@ | ||
root = true | ||
|
||
[*] | ||
indent_style = space | ||
indent_size = 4 | ||
|
||
[{package.json,.prettierrc}] | ||
indent_size = 2 |
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,6 @@ | ||
node_modules | ||
.DS_Store | ||
/data/ | ||
site/build/ | ||
server/build/ | ||
build |
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,11 @@ | ||
data/ | ||
.github/ | ||
.vscode/ | ||
alasql.js | ||
charts.js | ||
latest-canonical*.json | ||
momentum-cart.json | ||
package-lock.json | ||
site/_templates | ||
docs/ | ||
stores/*.json |
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,5 @@ | ||
{ | ||
"singleQuote": false, | ||
"bracketSpacing": true, | ||
"printWidth": 150 | ||
} |
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 Mario Zechner | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,3 @@ | ||
# skychat | ||
|
||
Live-view of posts for a hashtag. Also lets you compose threads for that hashtag easily. Great for live "discussion" of an event. |
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,306 @@ | ||
// @ts-ignore | ||
import { Agent } from "@intrnl/bluesky-client/agent"; | ||
// @ts-ignore | ||
import type { DID } from "@intrnl/bluesky-client/atp-schema"; | ||
import { RichText } from "@atproto/api"; | ||
import { isWithinLastNumDays } from "./utils"; | ||
|
||
const agent = new Agent({ serviceUri: "https://api.bsky.app" }); | ||
|
||
export type BskyAuthor = { | ||
did: string; | ||
avatar?: string; | ||
displayName: string; | ||
handle?: string; | ||
followersCount: number; | ||
followsCount: number; | ||
}; | ||
|
||
export type BskyFacet = { | ||
features: { uri?: string; tag?: string }[]; | ||
index: { byteStart: number; byteEnd: number }; | ||
}; | ||
|
||
export type BskyRecord = { | ||
createdAt: string; | ||
text: string; | ||
facets?: BskyFacet[]; | ||
reply?: { | ||
parent?: { | ||
uri?: string; | ||
}; | ||
}; | ||
}; | ||
|
||
export type BskyImage = { | ||
thumb: string; | ||
fullsize: string; | ||
alt: string; | ||
aspectRatio?: { | ||
width: number; | ||
height: number; | ||
}; | ||
}; | ||
|
||
export type BskyViewRecord = { | ||
$type: "app.bsky.embed.record#viewRecord"; | ||
uri: string; | ||
cid: string; | ||
author: BskyAuthor; | ||
value?: BskyRecord; | ||
embeds: { | ||
media?: { images: BskyImage[] }; | ||
images?: BskyImage[]; | ||
external?: BskyExternalCard; | ||
record?: BskyViewRecord | BskyViewRecordWithMedia; | ||
}[]; | ||
}; | ||
|
||
export type BskyViewRecordWithMedia = { | ||
$type: "app.bsky.embed.record_with_media#viewRecord"; | ||
record: BskyViewRecord; | ||
}; | ||
|
||
export type BskyExternalCard = { | ||
uri: string; | ||
title: string; | ||
description: string; | ||
thumb?: string; | ||
}; | ||
|
||
export type BskyPost = { | ||
uri: string; | ||
cid: string; | ||
author: BskyAuthor; | ||
record: BskyRecord; | ||
embed?: { | ||
media?: { images: BskyImage[] }; | ||
images?: BskyImage[]; | ||
external?: BskyExternalCard; | ||
record?: BskyViewRecord | BskyViewRecordWithMedia; | ||
}; | ||
likeCount: number; | ||
replyCount: number; | ||
repostCount: number; | ||
}; | ||
|
||
export type BskyThreadPost = { | ||
parent?: BskyThreadPost; | ||
post: BskyPost; | ||
replies: BskyThreadPost[]; | ||
}; | ||
|
||
export type ViewType = "tree" | "embed" | "unroll"; | ||
|
||
export async function getAccount(handle: string): Promise<BskyAuthor | Error> { | ||
try { | ||
const response = await agent.rpc.get("app.bsky.actor.getProfile", { | ||
params: { | ||
actor: handle, | ||
}, | ||
}); | ||
if (!response.success) { | ||
return new Error("Couldn't resolve account " + handle); | ||
} | ||
return response.data as BskyAuthor; | ||
} catch (e) { | ||
return new Error("Couldn't resolve account " + handle); | ||
} | ||
} | ||
|
||
export async function getPosts(author: BskyAuthor, numDays: number = 30): Promise<BskyPost[] | Error> { | ||
const posts: BskyPost[] = []; | ||
try { | ||
let cursor: string | undefined = undefined; | ||
|
||
while (true) { | ||
const response: any = await agent.rpc.get( | ||
"app.bsky.feed.getAuthorFeed", | ||
cursor | ||
? { | ||
params: { | ||
actor: author.did, | ||
cursor, | ||
}, | ||
} | ||
: { | ||
params: { | ||
actor: author.did, | ||
}, | ||
} | ||
); | ||
if (!response.success) { | ||
return new Error("Couldn't get posts of account " + author.handle); | ||
} | ||
cursor = response.data.cursor; | ||
if (!cursor) break; | ||
let done = false; | ||
for (const post of response.data.feed) { | ||
if (post.post.author.did != author.did) continue; | ||
if (post.reason) continue; | ||
if (isWithinLastNumDays(post.post.record.createdAt, numDays)) { | ||
posts.push(post.post); | ||
} else { | ||
done = true; | ||
} | ||
} | ||
if (done) break; | ||
} | ||
return posts; | ||
} catch (e) { | ||
return new Error("Couldn't get posts of account " + author.handle); | ||
} | ||
} | ||
|
||
export async function getFollowers(handle: string): Promise<BskyAuthor | Error> { | ||
try { | ||
const response = await agent.rpc.get("app.bsky.graph.getFollowers", { | ||
params: { | ||
actor: handle, | ||
}, | ||
}); | ||
if (!response.success) { | ||
return new Error("Couldn't get followers of account " + handle); | ||
} | ||
return response.data as BskyAuthor; | ||
} catch (e) { | ||
return new Error("Couldn't get followers of account " + handle); | ||
} | ||
} | ||
|
||
export async function loadThread(url: string, viewType: ViewType): Promise<{ thread: BskyThreadPost; originalUri: string | undefined } | string> { | ||
try { | ||
const tokens = url.replace("https://", "").split("/"); | ||
const actor = tokens[2]; | ||
let rkey = tokens[4]; | ||
if (!actor || !rkey) { | ||
return "Sorry, couldn't load thread (invalid URL)"; | ||
} | ||
let did: DID; | ||
if (actor.startsWith("did:")) { | ||
did = actor as DID; | ||
} else { | ||
const response = await agent.rpc.get("com.atproto.identity.resolveHandle", { | ||
params: { | ||
handle: actor, | ||
}, | ||
}); | ||
|
||
if (!response.success) { | ||
return "Sorry, couldn't load thread (invalid handle)"; | ||
} | ||
did = response.data.did; | ||
} | ||
|
||
let originalUri = `at://${did}/app.bsky.feed.post/${rkey}`; | ||
let response: any | null = null; | ||
do { | ||
response = await agent.rpc.get("app.bsky.feed.getPostThread", { | ||
params: { | ||
uri: `at://${did}/app.bsky.feed.post/${rkey}`, | ||
parentHeight: 1, | ||
depth: 100, | ||
}, | ||
}); | ||
|
||
if (!response) { | ||
return "sorry, couldn't load thread (empty response)"; | ||
} | ||
|
||
if (!response.success) { | ||
return "Sorry, couldn't load thread (invalid response)"; | ||
} | ||
|
||
if (!response.data.thread) { | ||
return "Sorry, couldn't load thread (invalid data)"; | ||
} | ||
|
||
if (response.data.thread.parent && viewType != "embed") { | ||
const tokens = response.data.thread.parent.post.uri.replace("at://", "").split("/"); | ||
did = tokens[0]; | ||
rkey = tokens[2]; | ||
response = null; | ||
} | ||
} while (!response); | ||
|
||
let thread: BskyThreadPost = response.data.thread; | ||
if (!thread) { | ||
return "Sorry, couldn't load thread (invalid thread)"; | ||
} | ||
if (viewType == "embed") { | ||
if (thread.post.record.text.includes("@skyview.social") && thread.post.record.text.includes("embed") && thread.parent) { | ||
thread = thread.parent; | ||
} | ||
thread.replies = []; | ||
} | ||
|
||
if (viewType == "unroll") { | ||
const posts: BskyThreadPost[] = []; | ||
posts.push(thread); | ||
while (true) { | ||
const post = posts[posts.length - 1]; | ||
post.replies.sort((a, b) => a.post.record.createdAt.localeCompare(b.post.record.createdAt)); | ||
const next = post.replies.find( | ||
(reply) => | ||
reply.post.author.did == post.post.author.did && | ||
!reply.post.record.text.includes("@skyview.social") && | ||
!reply.post.record.text.includes("unroll") | ||
); | ||
if (!next) break; | ||
posts.push(next); | ||
} | ||
thread.replies = posts.length > 1 ? posts.slice(1, posts.length) : []; | ||
thread.replies.forEach((reply) => (reply.replies = [])); | ||
if (thread.replies.length > 0) { | ||
const lastPost = thread.replies[thread.replies.length - 1]; | ||
if (lastPost.post.record.text.includes("@skyview.social") && lastPost.post.record.text.includes("unroll")) { | ||
thread.replies.pop(); | ||
} | ||
} | ||
} | ||
|
||
return { thread, originalUri }; | ||
} catch (e) { | ||
return `Sorry, couldn't load thread (exception) ${(e as any).message ? "\n" + (e as any).message : ""}`; | ||
} | ||
} | ||
|
||
function replaceHandles(text: string): string { | ||
const handleRegex = /@([\p{L}_.-]+)/gu; | ||
const replacedText = text.replace(handleRegex, (match, handle) => { | ||
return `<a class="text-primary" href="https://bsky.app/profile/${handle}" target="_blank">@${handle}</a>`; | ||
}); | ||
|
||
return replacedText; | ||
} | ||
|
||
function applyFacets(record: BskyRecord) { | ||
if (!record.facets) { | ||
return record.text; | ||
} | ||
|
||
const rt = new RichText({ | ||
text: record.text, | ||
facets: record.facets as any, | ||
}); | ||
|
||
const text: string[] = []; | ||
|
||
for (const segment of rt.segments()) { | ||
if (segment.isMention()) { | ||
text.push(`<a class="text-primary" href="https:///profile/${segment.mention?.did}" target="_blank">${segment.text}</a>`); | ||
} else if (segment.isLink()) { | ||
text.push(`<a class="text-primary" href="${segment.link?.uri}" target="_blank">${segment.text}</a>`); | ||
} else if (segment.isTag()) { | ||
text.push(`<span class="text-blue-500">${segment.text}</span>`); | ||
} else { | ||
text.push(segment.text); | ||
} | ||
} | ||
const result = text.join(""); | ||
return result; | ||
} | ||
|
||
export function processText(record: BskyRecord) { | ||
return replaceHandles(applyFacets(record)).trim().replaceAll("\n", "<br/>"); | ||
} |
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,25 @@ | ||
#!/usr/bin/env node | ||
|
||
import esbuild from "esbuild"; | ||
|
||
let watch = process.argv.length >= 3 && process.argv[2] == "--watch"; | ||
|
||
const config = { | ||
entryPoints: { | ||
server: "server.ts", | ||
}, | ||
bundle: true, | ||
sourcemap: true, | ||
platform: "node", | ||
outdir: "build/", | ||
logLevel: "info", | ||
minify: !watch, | ||
}; | ||
|
||
if (!watch) { | ||
console.log("Building site"); | ||
await esbuild.build(config); | ||
} else { | ||
const buildContext = await esbuild.context(config); | ||
buildContext.watch(); | ||
} |
Oops, something went wrong.