friendship ended with social-app. php is my new best friend

make replies work on single post pages; get correct-ish reply counts

Changed files
+52 -39
lib
templates
+10 -8
index.php
···
$bskyToucher = new BskyToucher();
$post = $bskyToucher->getPost($handle, $rkey, Flight::get('userAuth') === null);
$atUri = 'at://'.$post->author->did.'/app.bsky.feed.post/'.$rkey;
-
$likes = $bskyToucher->getLikeUsers($atUri);
-
$reposts = $bskyToucher->getRepostUsers($atUri);
-
$quotes = $bskyToucher->getQuoteRecords($atUri);
-
$replies = $bskyToucher->getReplyRecords($atUri);
+
$interactions = await(all([
+
'likes' => async(fn() => $bskyToucher->getLikeUsers($atUri)),
+
'reposts' => async(fn() => $bskyToucher->getRepostUsers($atUri)),
+
'quotes' => async(fn() => $bskyToucher->getQuoteRecords($atUri)),
+
'replies' => async(fn() => $bskyToucher->getReplyRecords($atUri))
+
]));
$latte = new Latte\Engine;
$latte->render('./templates/single.latte', array_merge(Flight::get('standardParams'), [
'mainClass' => 'post',
'post' => $post,
-
'likes' => $likes,
-
'reposts' => $reposts,
-
'quotes' => $quotes,
-
'replies' => $replies,
+
'likes' => $interactions['likes'],
+
'reposts' => $interactions['reposts'],
+
'quotes' => $interactions['quotes'],
+
'replies' => $interactions['replies'],
'ogtitle' => SITE_TITLE." | ".$post->author->displayName." (@".$post->author->handle.")",
'ogdesc' => $post->content,
'ogimage' => getPostOgImage($post),
+41 -30
lib/bskyToucher.php
···
}
function getConstellationLinkData(string $target, string $collection, string $path, int $limit = 50, int $offset = 0, string $endpoint = '/xrpc/blue.microcosm.links.getBacklinks'): object|bool {
+
$query = [
+
'offset' => $offset
+
];
+
if ($endpoint === '/xrpc/blue.microcosm.links.getBacklinks') {
+
$query['subject'] = $target;
+
$query['source'] = $collection.":".$path;
+
} else {
+
$query['target'] = $target;
+
$query['collection'] = $collection;
+
$query['path'] = ".".$path;
+
}
$ret = $this->makeRequest("GET", $this->constellationBase.$endpoint, [
-
'query' => [
-
'target' => $target,
-
'collection' => $collection,
-
'path' => $path,
-
'offset' => $offset
-
]
+
'query' => $query
]);
-
if ($ret) {
+
if ($ret && json_decode($ret->getBody())) {
return json_decode($ret->getBody());
}
···
}
function getLikes(string $post):int {
-
$ret = $this->getConstellationLinkData($post, "app.bsky.feed.like", ".subject.uri", 50, 0, '/links/count');
-
if ($ret) return $ret->total;
+
$ret = $this->getConstellationLinkData($post, "app.bsky.feed.like", "subject.uri", 50, 0, '/links/count');
+
if ($ret && property_exists($ret, 'total')) return $ret->total;
return 0;
}
function getLikeUsers(string $post):array {
-
/*$ret = $this->getConstellationLinkData($post, "app.bsky.feed.like", ".subject.uri", 50, 0, "/links/distinct-dids");
-
if ($ret) {
+
$ret = $this->getConstellationLinkData($post, "app.bsky.feed.like", "subject.uri", 50, 0, "/links/distinct-dids");
+
if ($ret && property_exists($ret, 'linking_dids')) {
return array_map(function($user) {
return $this->getUserInfo($user, 'did');
}, $ret->linking_dids);
-
}*/
+
}
return [];
}
function getReposts(string $post):int {
-
$ret = $this->getConstellationLinkData($post, "app.bsky.feed.repost", ".subject.uri", 50, 0, '/links/count');
-
if ($ret) return $ret->total;
+
$ret = $this->getConstellationLinkData($post, "app.bsky.feed.repost", "subject.uri", 50, 0, '/links/count');
+
if ($ret && property_exists($ret, 'total')) return $ret->total || 0;
return 0;
}
function getRepostUsers(string $post):array {
-
/*$ret = $this->getConstellationLinkData($post, "app.bsky.feed.repost", ".subject.uri", 50, 0, "/links/distinct-dids");
-
if ($ret) {
+
$ret = $this->getConstellationLinkData($post, "app.bsky.feed.repost", "subject.uri", 50, 0, "/links/distinct-dids");
+
if ($ret && property_exists($ret, 'linking_dids')) {
return array_map(function($user) {
return $this->getUserInfo($user, 'did');
}, $ret->linking_dids);
-
}*/
+
}
return [];
}
function getQuotes(string $post):int {
-
$ret = $this->getConstellationLinkData($post, "app.bsky.feed.post", ".embed.record.record.uri", 50, 0, '/links/count');
-
if ($ret) return $ret->total;
+
$ret = $this->getConstellationLinkData($post, "app.bsky.feed.post", "embed.record.record.uri", 50, 0, '/links/count');
+
if ($ret && property_exists($ret, 'total')) return $ret->total || 0;
return 0;
}
function getQuoteRecords(string $post):array {
-
/*$ret = $this->getConstellationLinkData($post, "app.bsky.feed.post", ".embed.record.record.uri");
-
if ($ret) {
-
return $ret->linking_records;
-
}*/
+
$ret = $this->getConstellationLinkData($post, "app.bsky.feed.post", "embed.record.record.uri");
+
if ($ret && property_exists($ret, 'linking_records')) {
+
return $ret->records;
+
}
return [];
}
function getReplies(string $post):int {
-
$ret = $this->getConstellationLinkData($post, "app.bsky.feed.post", ".reply.root.uri", 50, 0, '/links/count');
-
if ($ret) return $ret->total;
+
$ret = $this->getConstellationLinkData($post, "app.bsky.feed.post", "reply.root.uri");
+
if ($ret && property_exists($ret, 'total')) return $ret->total;
return 0;
}
function getReplyRecords(string $post):array {
-
/*$ret = $this->getConstellationLinkData($post, "app.bsky.feed.post", ".reply.root.uri");
-
if ($ret) {
+
$ret = $this->getConstellationLinkData($post, "app.bsky.feed.post", "reply.root.uri");
+
if ($ret && property_exists($ret, 'records')) {
+
$rec = await(all(array_map(function ($rec) {
+
//$slingshotRecord = $this->getSlingshotData($rec->did, $rec->collection, $rec->rkey);
+
return async(fn() => $this->sanitizePost($this->getSlingshotData($rec->did, $rec->collection, $rec->rkey), true));
+
}, $ret->records)));
+
return $rec;
return array_map(function ($rec) {
-
$slingshotRecord = $this->getSlingshotData($rec->did, $rec->collection, $rec->rkey, ['reverse' => true]);
+
$slingshotRecord = $this->getSlingshotData($rec->did, $rec->collection, $rec->rkey);
return $this->sanitizePost($slingshotRecord, true);
-
}, $ret->linking_records);
-
}*/
+
}, $ret->records);
+
}
return [];
}
+1 -1
templates/single.latte
···
<div id="likes" popover>
<div class="inner">
<div n:if="count($likes) === 0">
-
<p>No reposts yet!</p>
+
<p>No likes yet!</p>
</div>
{foreach $likes as $like}
{include '_partials/profile_mini.latte', displayName: $like->displayName, handle: $like->handle, did: $like->did, avatar: $like->avatar, profileLink: '/u/'.$like->handle}