chore: import upstream snapshot with attribution
E2E Headed Chrome / e2e-headed (macos-15) (push) Has been cancelled
E2E Headed Chrome / e2e-headed (ubuntu-latest) (push) Has been cancelled
E2E Headed Chrome / e2e-headed (windows-latest) (push) Has been cancelled
CI / build (macos-latest) (push) Has been cancelled
CI / build (ubuntu-latest) (push) Has been cancelled
CI / build (windows-latest) (push) Has been cancelled
CI / unit-test (push) Has been cancelled
CI / bun-test (push) Has been cancelled
CI / adapter-test (push) Has been cancelled
CI / smoke-test (macos-latest) (push) Has been cancelled
CI / smoke-test (ubuntu-latest) (push) Has been cancelled
Security Audit / audit (push) Has been cancelled
Build Chrome Extension / build (push) Has been cancelled
Trigger Website Rebuild (Docs Updated) / dispatch (push) Has been cancelled
E2E Headed Chrome / e2e-headed (macos-15) (push) Has been cancelled
E2E Headed Chrome / e2e-headed (ubuntu-latest) (push) Has been cancelled
E2E Headed Chrome / e2e-headed (windows-latest) (push) Has been cancelled
CI / build (macos-latest) (push) Has been cancelled
CI / build (ubuntu-latest) (push) Has been cancelled
CI / build (windows-latest) (push) Has been cancelled
CI / unit-test (push) Has been cancelled
CI / bun-test (push) Has been cancelled
CI / adapter-test (push) Has been cancelled
CI / smoke-test (macos-latest) (push) Has been cancelled
CI / smoke-test (ubuntu-latest) (push) Has been cancelled
Security Audit / audit (push) Has been cancelled
Build Chrome Extension / build (push) Has been cancelled
Trigger Website Rebuild (Docs Updated) / dispatch (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
import { CommandExecutionError, EmptyResultError } from '@jackwener/opencli/errors';
|
||||
export const SITE = 'lesswrong';
|
||||
export const DOMAIN = 'www.lesswrong.com';
|
||||
const GRAPHQL_URL = `https://${DOMAIN}/graphql`;
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- GraphQL responses vary per query
|
||||
export async function gqlRequest(query) {
|
||||
const resp = await fetch(GRAPHQL_URL, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Accept: 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ query }),
|
||||
signal: AbortSignal.timeout(15000),
|
||||
});
|
||||
if (!resp.ok) {
|
||||
throw new CommandExecutionError(`LessWrong API returned HTTP ${resp.status}`);
|
||||
}
|
||||
const json = (await resp.json());
|
||||
if (json.errors?.length) {
|
||||
throw new CommandExecutionError(json.errors[0]?.message ?? 'Unknown GraphQL error');
|
||||
}
|
||||
return json.data;
|
||||
}
|
||||
export function gqlEscape(str) {
|
||||
return str.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
|
||||
}
|
||||
export function stripHtml(html) {
|
||||
if (!html)
|
||||
return '';
|
||||
return html
|
||||
.replace(/<script[^>]*>.*?<\/script>/gis, ' ')
|
||||
.replace(/<style[^>]*>.*?<\/style>/gis, ' ')
|
||||
.replace(/<[^>]+>/g, ' ')
|
||||
.replace(/\s+/g, ' ')
|
||||
.trim();
|
||||
}
|
||||
export function daysAgo(n) {
|
||||
const d = new Date();
|
||||
d.setDate(d.getDate() - n);
|
||||
return d.toISOString();
|
||||
}
|
||||
export async function resolveTagId(slug) {
|
||||
const normalized = gqlEscape(slug.toLowerCase().trim().replace(/\s+/g, '-'));
|
||||
const query = `query TagBySlug {
|
||||
tags(input: {terms: {view: "tagBySlug", slug: "${normalized}"}}) {
|
||||
results { _id name slug }
|
||||
}
|
||||
}`;
|
||||
const data = await gqlRequest(query);
|
||||
const tag = data?.tags?.results?.[0];
|
||||
if (!tag?._id || !tag?.name)
|
||||
return null;
|
||||
return { _id: tag._id, name: tag.name };
|
||||
}
|
||||
export function resolveUserId(slug) {
|
||||
const normalized = gqlEscape(slug.toLowerCase());
|
||||
const query = `query UserProfile {
|
||||
user(input: {selector: {slug: "${normalized}"}}) {
|
||||
result { _id displayName slug }
|
||||
}
|
||||
}`;
|
||||
return gqlRequest(query).then((data) => {
|
||||
const user = data?.user?.result;
|
||||
if (!user?._id) {
|
||||
throw new EmptyResultError(`lesswrong user ${slug}`, 'Check the username — LessWrong slugs are lowercase (e.g. "zvi", "eliezer-yudkowsky")');
|
||||
}
|
||||
return { _id: user._id, displayName: (user.displayName ?? '') };
|
||||
});
|
||||
}
|
||||
export function parsePostId(urlOrId) {
|
||||
const trimmed = urlOrId.trim();
|
||||
const match = trimmed.match(/posts\/([a-zA-Z0-9]+)/);
|
||||
return match ? match[1] : trimmed;
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
import { cli, Strategy } from '@jackwener/opencli/registry';
|
||||
import { EmptyResultError } from '@jackwener/opencli/errors';
|
||||
import { DOMAIN, SITE, gqlEscape, gqlRequest, parsePostId, stripHtml, } from './_helpers.js';
|
||||
cli({
|
||||
site: SITE,
|
||||
name: 'comments',
|
||||
access: 'read',
|
||||
description: 'Top comments on a post',
|
||||
domain: DOMAIN,
|
||||
strategy: Strategy.PUBLIC,
|
||||
browser: false,
|
||||
args: [
|
||||
{
|
||||
name: 'url-or-id',
|
||||
type: 'string',
|
||||
required: true,
|
||||
positional: true,
|
||||
help: 'Post URL or LessWrong post ID',
|
||||
},
|
||||
{ name: 'limit', type: 'int', default: 5, help: 'Number of comments' },
|
||||
],
|
||||
columns: ['rank', 'score', 'author', 'text'],
|
||||
func: async (kwargs) => {
|
||||
const postId = gqlEscape(parsePostId(String(kwargs['url-or-id'])));
|
||||
const limit = Number(kwargs.limit ?? 5);
|
||||
// Fetch post title and comments in parallel
|
||||
const [postData, commentsData] = await Promise.all([
|
||||
gqlRequest(`query PostTitle {
|
||||
post(input: {selector: {documentId: "${postId}"}}) {
|
||||
result { _id title slug }
|
||||
}
|
||||
}`),
|
||||
gqlRequest(`query Comments {
|
||||
comments(input: {terms: {view: "postCommentsTop", postId: "${postId}", limit: ${limit}}}) {
|
||||
results { _id user { displayName } baseScore htmlBody postedAt }
|
||||
}
|
||||
}`),
|
||||
]);
|
||||
const post = postData?.post?.result;
|
||||
if (!post?._id) {
|
||||
throw new EmptyResultError('lesswrong comments', `Post "${postId}" not found`);
|
||||
}
|
||||
const comments = (commentsData?.comments?.results ?? []);
|
||||
const rows = [];
|
||||
// First row: post context
|
||||
rows.push({
|
||||
rank: '',
|
||||
score: '',
|
||||
author: '',
|
||||
text: `Comments on: ${post.title ?? 'Untitled'} (https://${DOMAIN}/posts/${post._id}/${post.slug})`,
|
||||
});
|
||||
for (let i = 0; i < comments.length; i++) {
|
||||
const item = comments[i];
|
||||
const user = item.user;
|
||||
const raw = stripHtml(item.htmlBody ?? '');
|
||||
rows.push({
|
||||
rank: i + 1,
|
||||
score: item.baseScore ?? 0,
|
||||
author: user?.displayName ?? '',
|
||||
text: raw.length > 500 ? `${raw.slice(0, 500)}...` : raw,
|
||||
});
|
||||
}
|
||||
return rows;
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,31 @@
|
||||
import { cli, Strategy } from '@jackwener/opencli/registry';
|
||||
import { DOMAIN, SITE, gqlRequest } from './_helpers.js';
|
||||
cli({
|
||||
site: SITE,
|
||||
name: 'curated',
|
||||
access: 'read',
|
||||
description: "Curated editor's picks",
|
||||
domain: DOMAIN,
|
||||
strategy: Strategy.PUBLIC,
|
||||
browser: false,
|
||||
args: [{ name: 'limit', type: 'int', default: 10, help: 'Number of results' }],
|
||||
columns: ['rank', 'title', 'author', 'karma', 'comments', 'url'],
|
||||
func: async (kwargs) => {
|
||||
const limit = Number(kwargs.limit ?? 10);
|
||||
const query = `query PostsList {
|
||||
posts(input: {terms: {view: "curated", limit: ${limit}}}) {
|
||||
results { _id title user { displayName } baseScore commentCount slug postedAt tags { name } }
|
||||
}
|
||||
}`;
|
||||
const data = await gqlRequest(query);
|
||||
const posts = (data?.posts?.results ?? []);
|
||||
return posts.map((item, i) => ({
|
||||
rank: i + 1,
|
||||
title: item.title ?? '',
|
||||
author: item.user?.displayName ?? '',
|
||||
karma: item.baseScore ?? 0,
|
||||
comments: item.commentCount ?? 0,
|
||||
url: `https://${DOMAIN}/posts/${item._id}/${item.slug}`,
|
||||
}));
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,31 @@
|
||||
import { cli, Strategy } from '@jackwener/opencli/registry';
|
||||
import { DOMAIN, SITE, gqlRequest } from './_helpers.js';
|
||||
cli({
|
||||
site: SITE,
|
||||
name: 'frontpage',
|
||||
access: 'read',
|
||||
description: 'Algorithmic frontpage',
|
||||
domain: DOMAIN,
|
||||
strategy: Strategy.PUBLIC,
|
||||
browser: false,
|
||||
args: [{ name: 'limit', type: 'int', default: 10, help: 'Number of results' }],
|
||||
columns: ['rank', 'title', 'author', 'karma', 'comments', 'url'],
|
||||
func: async (kwargs) => {
|
||||
const limit = Number(kwargs.limit ?? 10);
|
||||
const query = `query PostsList {
|
||||
posts(input: {terms: {view: "frontpage", limit: ${limit}}}) {
|
||||
results { _id title user { displayName } baseScore commentCount slug postedAt tags { name } }
|
||||
}
|
||||
}`;
|
||||
const data = await gqlRequest(query);
|
||||
const posts = (data?.posts?.results ?? []);
|
||||
return posts.map((item, i) => ({
|
||||
rank: i + 1,
|
||||
title: item.title ?? '',
|
||||
author: item.user?.displayName ?? '',
|
||||
karma: item.baseScore ?? 0,
|
||||
comments: item.commentCount ?? 0,
|
||||
url: `https://${DOMAIN}/posts/${item._id}/${item.slug}`,
|
||||
}));
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,37 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { getRegistry } from '@jackwener/opencli/registry';
|
||||
|
||||
const { gqlRequestMock } = vi.hoisted(() => ({ gqlRequestMock: vi.fn() }));
|
||||
vi.mock('./_helpers.js', async () => {
|
||||
const actual = await vi.importActual('./_helpers.js');
|
||||
return { ...actual, gqlRequest: gqlRequestMock };
|
||||
});
|
||||
|
||||
import './frontpage.js';
|
||||
|
||||
describe('lesswrong frontpage', () => {
|
||||
beforeEach(() => {
|
||||
gqlRequestMock.mockReset();
|
||||
});
|
||||
|
||||
it('emits empty-string for missing user.displayName instead of a sentinel', async () => {
|
||||
const command = getRegistry().get('lesswrong/frontpage');
|
||||
expect(command?.func).toBeDefined();
|
||||
gqlRequestMock.mockResolvedValueOnce({
|
||||
posts: {
|
||||
results: [
|
||||
{ _id: 'a1', slug: 'post-a', title: 'Has author', user: { displayName: 'Real Person' }, baseScore: 10, commentCount: 3 },
|
||||
{ _id: 'b2', slug: 'post-b', title: 'Deleted user', user: null, baseScore: 5, commentCount: 0 },
|
||||
{ _id: 'c3', slug: 'post-c', title: 'Missing name', user: {}, baseScore: 7, commentCount: 1 },
|
||||
],
|
||||
},
|
||||
});
|
||||
const rows = await command.func({ limit: 3 });
|
||||
expect(rows).toHaveLength(3);
|
||||
expect(rows[0]).toMatchObject({ rank: 1, title: 'Has author', author: 'Real Person', karma: 10, comments: 3 });
|
||||
expect(rows[1].author).toBe('');
|
||||
expect(rows[1].title).toBe('Deleted user');
|
||||
expect(rows[2].author).toBe('');
|
||||
expect(rows[2].title).toBe('Missing name');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,31 @@
|
||||
import { cli, Strategy } from '@jackwener/opencli/registry';
|
||||
import { DOMAIN, SITE, gqlRequest } from './_helpers.js';
|
||||
cli({
|
||||
site: SITE,
|
||||
name: 'new',
|
||||
access: 'read',
|
||||
description: 'Latest posts',
|
||||
domain: DOMAIN,
|
||||
strategy: Strategy.PUBLIC,
|
||||
browser: false,
|
||||
args: [{ name: 'limit', type: 'int', default: 10, help: 'Number of results' }],
|
||||
columns: ['rank', 'title', 'author', 'karma', 'comments', 'url'],
|
||||
func: async (kwargs) => {
|
||||
const limit = Number(kwargs.limit ?? 10);
|
||||
const query = `query PostsList {
|
||||
posts(input: {terms: {view: "new", limit: ${limit}}}) {
|
||||
results { _id title user { displayName } baseScore commentCount slug postedAt tags { name } }
|
||||
}
|
||||
}`;
|
||||
const data = await gqlRequest(query);
|
||||
const posts = (data?.posts?.results ?? []);
|
||||
return posts.map((item, i) => ({
|
||||
rank: i + 1,
|
||||
title: item.title ?? '',
|
||||
author: item.user?.displayName ?? '',
|
||||
karma: item.baseScore ?? 0,
|
||||
comments: item.commentCount ?? 0,
|
||||
url: `https://${DOMAIN}/posts/${item._id}/${item.slug}`,
|
||||
}));
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,46 @@
|
||||
import { cli, Strategy } from '@jackwener/opencli/registry';
|
||||
import { EmptyResultError } from '@jackwener/opencli/errors';
|
||||
import { DOMAIN, SITE, gqlEscape, gqlRequest, parsePostId, stripHtml, } from './_helpers.js';
|
||||
cli({
|
||||
site: SITE,
|
||||
name: 'read',
|
||||
access: 'read',
|
||||
description: 'Read full post by URL or ID',
|
||||
domain: DOMAIN,
|
||||
strategy: Strategy.PUBLIC,
|
||||
browser: false,
|
||||
args: [
|
||||
{
|
||||
name: 'url-or-id',
|
||||
type: 'string',
|
||||
required: true,
|
||||
positional: true,
|
||||
help: 'Post URL or LessWrong post ID',
|
||||
},
|
||||
],
|
||||
columns: ['title', 'author', 'karma', 'comments', 'tags', 'content', 'url'],
|
||||
func: async (kwargs) => {
|
||||
const postId = parsePostId(String(kwargs['url-or-id']));
|
||||
const query = `query PostsSingle {
|
||||
post(input: {selector: {documentId: "${gqlEscape(postId)}"}}) {
|
||||
result { _id title user { displayName } baseScore commentCount htmlBody slug postedAt tags { name } }
|
||||
}
|
||||
}`;
|
||||
const data = await gqlRequest(query);
|
||||
const post = data?.post?.result;
|
||||
if (!post?._id) {
|
||||
throw new EmptyResultError('lesswrong read', `Post "${postId}" not found`);
|
||||
}
|
||||
return [
|
||||
{
|
||||
title: post.title ?? '',
|
||||
author: post.user?.displayName ?? '',
|
||||
karma: post.baseScore ?? 0,
|
||||
comments: post.commentCount ?? 0,
|
||||
tags: (post.tags ?? []).map((tag) => tag.name ?? '').filter(Boolean).join(', '),
|
||||
content: stripHtml(post.htmlBody ?? ''),
|
||||
url: `https://${DOMAIN}/posts/${post._id}/${post.slug}`,
|
||||
},
|
||||
];
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,28 @@
|
||||
import { cli, Strategy } from '@jackwener/opencli/registry';
|
||||
import { DOMAIN, SITE, gqlRequest } from './_helpers.js';
|
||||
cli({
|
||||
site: SITE,
|
||||
name: 'sequences',
|
||||
access: 'read',
|
||||
description: 'List post collections',
|
||||
domain: DOMAIN,
|
||||
strategy: Strategy.PUBLIC,
|
||||
browser: false,
|
||||
args: [{ name: 'limit', type: 'int', default: 10, help: 'Number of results' }],
|
||||
columns: ['rank', 'title', 'author'],
|
||||
func: async (kwargs) => {
|
||||
const limit = Number(kwargs.limit ?? 10);
|
||||
const query = `query Sequences {
|
||||
sequences(input: {terms: {view: "communitySequences", limit: ${limit}}}) {
|
||||
results { _id title user { displayName } createdAt }
|
||||
}
|
||||
}`;
|
||||
const data = await gqlRequest(query);
|
||||
const sequences = (data?.sequences?.results ?? []);
|
||||
return sequences.map((item, i) => ({
|
||||
rank: i + 1,
|
||||
title: item.title ?? '',
|
||||
author: item.user?.displayName ?? '',
|
||||
}));
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,31 @@
|
||||
import { cli, Strategy } from '@jackwener/opencli/registry';
|
||||
import { DOMAIN, SITE, gqlRequest } from './_helpers.js';
|
||||
cli({
|
||||
site: SITE,
|
||||
name: 'shortform',
|
||||
access: 'read',
|
||||
description: 'Quick takes / shortform posts',
|
||||
domain: DOMAIN,
|
||||
strategy: Strategy.PUBLIC,
|
||||
browser: false,
|
||||
args: [{ name: 'limit', type: 'int', default: 10, help: 'Number of results' }],
|
||||
columns: ['rank', 'title', 'author', 'karma', 'comments', 'url'],
|
||||
func: async (kwargs) => {
|
||||
const limit = Number(kwargs.limit ?? 10);
|
||||
const query = `query PostsList {
|
||||
posts(input: {terms: {view: "shortform", limit: ${limit}}}) {
|
||||
results { _id title user { displayName } baseScore commentCount slug postedAt tags { name } }
|
||||
}
|
||||
}`;
|
||||
const data = await gqlRequest(query);
|
||||
const posts = (data?.posts?.results ?? []);
|
||||
return posts.map((item, i) => ({
|
||||
rank: i + 1,
|
||||
title: item.title ?? '',
|
||||
author: item.user?.displayName ?? '',
|
||||
karma: item.baseScore ?? 0,
|
||||
comments: item.commentCount ?? 0,
|
||||
url: `https://${DOMAIN}/posts/${item._id}/${item.slug}`,
|
||||
}));
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,46 @@
|
||||
import { cli, Strategy } from '@jackwener/opencli/registry';
|
||||
import { EmptyResultError } from '@jackwener/opencli/errors';
|
||||
import { DOMAIN, SITE, gqlRequest, resolveTagId } from './_helpers.js';
|
||||
cli({
|
||||
site: SITE,
|
||||
name: 'tag',
|
||||
access: 'read',
|
||||
description: 'Posts by tag',
|
||||
domain: DOMAIN,
|
||||
strategy: Strategy.PUBLIC,
|
||||
browser: false,
|
||||
args: [
|
||||
{
|
||||
name: 'tag',
|
||||
type: 'string',
|
||||
required: true,
|
||||
positional: true,
|
||||
help: 'Tag slug or name',
|
||||
},
|
||||
{ name: 'limit', type: 'int', default: 10, help: 'Number of results' },
|
||||
],
|
||||
columns: ['rank', 'title', 'author', 'karma', 'comments', 'url'],
|
||||
func: async (kwargs) => {
|
||||
const tagInput = String(kwargs.tag);
|
||||
const limit = Number(kwargs.limit ?? 10);
|
||||
const tag = await resolveTagId(tagInput);
|
||||
if (!tag?._id) {
|
||||
throw new EmptyResultError(`lesswrong tag ${tagInput}`, 'Use "opencli lesswrong tags" to list available tags');
|
||||
}
|
||||
const query = `query PostsByTag {
|
||||
posts(input: {terms: {view: "tagRelevance", tagId: "${tag._id}", limit: ${limit}}}) {
|
||||
results { _id title user { displayName } baseScore commentCount slug postedAt }
|
||||
}
|
||||
}`;
|
||||
const data = await gqlRequest(query);
|
||||
const posts = (data?.posts?.results ?? []);
|
||||
return posts.map((item, i) => ({
|
||||
rank: i + 1,
|
||||
title: item.title ?? '',
|
||||
author: item.user?.displayName ?? '',
|
||||
karma: item.baseScore ?? 0,
|
||||
comments: item.commentCount ?? 0,
|
||||
url: `https://${DOMAIN}/posts/${item._id}/${item.slug}`,
|
||||
}));
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,28 @@
|
||||
import { cli, Strategy } from '@jackwener/opencli/registry';
|
||||
import { DOMAIN, SITE, gqlRequest } from './_helpers.js';
|
||||
cli({
|
||||
site: SITE,
|
||||
name: 'tags',
|
||||
access: 'read',
|
||||
description: 'List popular tags',
|
||||
domain: DOMAIN,
|
||||
strategy: Strategy.PUBLIC,
|
||||
browser: false,
|
||||
args: [{ name: 'limit', type: 'int', default: 20, help: 'Number of results' }],
|
||||
columns: ['rank', 'name', 'posts'],
|
||||
func: async (kwargs) => {
|
||||
const limit = Number(kwargs.limit ?? 20);
|
||||
const query = `query Tags {
|
||||
tags(input: {terms: {view: "coreTags", limit: ${limit}}}) {
|
||||
results { _id name slug postCount }
|
||||
}
|
||||
}`;
|
||||
const data = await gqlRequest(query);
|
||||
const tags = (data?.tags?.results ?? []);
|
||||
return tags.map((item, i) => ({
|
||||
rank: i + 1,
|
||||
name: item.name ?? '',
|
||||
posts: item.postCount ?? 0,
|
||||
}));
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,31 @@
|
||||
import { cli, Strategy } from '@jackwener/opencli/registry';
|
||||
import { DOMAIN, SITE, daysAgo, gqlRequest } from './_helpers.js';
|
||||
cli({
|
||||
site: SITE,
|
||||
name: 'top-month',
|
||||
access: 'read',
|
||||
description: 'Top this month',
|
||||
domain: DOMAIN,
|
||||
strategy: Strategy.PUBLIC,
|
||||
browser: false,
|
||||
args: [{ name: 'limit', type: 'int', default: 10, help: 'Number of results' }],
|
||||
columns: ['rank', 'title', 'author', 'karma', 'comments', 'url'],
|
||||
func: async (kwargs) => {
|
||||
const limit = Number(kwargs.limit ?? 10);
|
||||
const query = `query PostsList {
|
||||
posts(input: {terms: {view: "top", after: "${daysAgo(30)}", limit: ${limit}}}) {
|
||||
results { _id title user { displayName } baseScore commentCount slug postedAt tags { name } }
|
||||
}
|
||||
}`;
|
||||
const data = await gqlRequest(query);
|
||||
const posts = (data?.posts?.results ?? []);
|
||||
return posts.map((item, i) => ({
|
||||
rank: i + 1,
|
||||
title: item.title ?? '',
|
||||
author: item.user?.displayName ?? '',
|
||||
karma: item.baseScore ?? 0,
|
||||
comments: item.commentCount ?? 0,
|
||||
url: `https://${DOMAIN}/posts/${item._id}/${item.slug}`,
|
||||
}));
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,31 @@
|
||||
import { cli, Strategy } from '@jackwener/opencli/registry';
|
||||
import { DOMAIN, SITE, daysAgo, gqlRequest } from './_helpers.js';
|
||||
cli({
|
||||
site: SITE,
|
||||
name: 'top-week',
|
||||
access: 'read',
|
||||
description: 'Top this week',
|
||||
domain: DOMAIN,
|
||||
strategy: Strategy.PUBLIC,
|
||||
browser: false,
|
||||
args: [{ name: 'limit', type: 'int', default: 10, help: 'Number of results' }],
|
||||
columns: ['rank', 'title', 'author', 'karma', 'comments', 'url'],
|
||||
func: async (kwargs) => {
|
||||
const limit = Number(kwargs.limit ?? 10);
|
||||
const query = `query PostsList {
|
||||
posts(input: {terms: {view: "top", after: "${daysAgo(7)}", limit: ${limit}}}) {
|
||||
results { _id title user { displayName } baseScore commentCount slug postedAt tags { name } }
|
||||
}
|
||||
}`;
|
||||
const data = await gqlRequest(query);
|
||||
const posts = (data?.posts?.results ?? []);
|
||||
return posts.map((item, i) => ({
|
||||
rank: i + 1,
|
||||
title: item.title ?? '',
|
||||
author: item.user?.displayName ?? '',
|
||||
karma: item.baseScore ?? 0,
|
||||
comments: item.commentCount ?? 0,
|
||||
url: `https://${DOMAIN}/posts/${item._id}/${item.slug}`,
|
||||
}));
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,31 @@
|
||||
import { cli, Strategy } from '@jackwener/opencli/registry';
|
||||
import { DOMAIN, SITE, daysAgo, gqlRequest } from './_helpers.js';
|
||||
cli({
|
||||
site: SITE,
|
||||
name: 'top-year',
|
||||
access: 'read',
|
||||
description: 'Top this year',
|
||||
domain: DOMAIN,
|
||||
strategy: Strategy.PUBLIC,
|
||||
browser: false,
|
||||
args: [{ name: 'limit', type: 'int', default: 10, help: 'Number of results' }],
|
||||
columns: ['rank', 'title', 'author', 'karma', 'comments', 'url'],
|
||||
func: async (kwargs) => {
|
||||
const limit = Number(kwargs.limit ?? 10);
|
||||
const query = `query PostsList {
|
||||
posts(input: {terms: {view: "top", after: "${daysAgo(365)}", limit: ${limit}}}) {
|
||||
results { _id title user { displayName } baseScore commentCount slug postedAt tags { name } }
|
||||
}
|
||||
}`;
|
||||
const data = await gqlRequest(query);
|
||||
const posts = (data?.posts?.results ?? []);
|
||||
return posts.map((item, i) => ({
|
||||
rank: i + 1,
|
||||
title: item.title ?? '',
|
||||
author: item.user?.displayName ?? '',
|
||||
karma: item.baseScore ?? 0,
|
||||
comments: item.commentCount ?? 0,
|
||||
url: `https://${DOMAIN}/posts/${item._id}/${item.slug}`,
|
||||
}));
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,31 @@
|
||||
import { cli, Strategy } from '@jackwener/opencli/registry';
|
||||
import { DOMAIN, SITE, gqlRequest } from './_helpers.js';
|
||||
cli({
|
||||
site: SITE,
|
||||
name: 'top',
|
||||
access: 'read',
|
||||
description: 'Top all-time',
|
||||
domain: DOMAIN,
|
||||
strategy: Strategy.PUBLIC,
|
||||
browser: false,
|
||||
args: [{ name: 'limit', type: 'int', default: 10, help: 'Number of results' }],
|
||||
columns: ['rank', 'title', 'author', 'karma', 'comments', 'url'],
|
||||
func: async (kwargs) => {
|
||||
const limit = Number(kwargs.limit ?? 10);
|
||||
const query = `query PostsList {
|
||||
posts(input: {terms: {view: "top", limit: ${limit}}}) {
|
||||
results { _id title user { displayName } baseScore commentCount slug postedAt tags { name } }
|
||||
}
|
||||
}`;
|
||||
const data = await gqlRequest(query);
|
||||
const posts = (data?.posts?.results ?? []);
|
||||
return posts.map((item, i) => ({
|
||||
rank: i + 1,
|
||||
title: item.title ?? '',
|
||||
author: item.user?.displayName ?? '',
|
||||
karma: item.baseScore ?? 0,
|
||||
comments: item.commentCount ?? 0,
|
||||
url: `https://${DOMAIN}/posts/${item._id}/${item.slug}`,
|
||||
}));
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,42 @@
|
||||
import { cli, Strategy } from '@jackwener/opencli/registry';
|
||||
import { DOMAIN, SITE, gqlEscape, gqlRequest, resolveUserId } from './_helpers.js';
|
||||
cli({
|
||||
site: SITE,
|
||||
name: 'user-posts',
|
||||
access: 'read',
|
||||
description: "List a user's posts",
|
||||
domain: DOMAIN,
|
||||
strategy: Strategy.PUBLIC,
|
||||
browser: false,
|
||||
args: [
|
||||
{
|
||||
name: 'username',
|
||||
type: 'string',
|
||||
required: true,
|
||||
positional: true,
|
||||
help: 'LessWrong username or slug',
|
||||
},
|
||||
{ name: 'limit', type: 'int', default: 10, help: 'Number of results' },
|
||||
],
|
||||
columns: ['rank', 'title', 'karma', 'comments', 'date', 'url'],
|
||||
func: async (kwargs) => {
|
||||
const username = String(kwargs.username);
|
||||
const limit = Number(kwargs.limit ?? 10);
|
||||
const user = await resolveUserId(username);
|
||||
const query = `query UserPosts {
|
||||
posts(input: {terms: {view: "userPosts", userId: "${gqlEscape(user._id)}", limit: ${limit}}}) {
|
||||
results { _id title baseScore commentCount slug postedAt }
|
||||
}
|
||||
}`;
|
||||
const data = await gqlRequest(query);
|
||||
const posts = (data?.posts?.results ?? []);
|
||||
return posts.map((item, i) => ({
|
||||
rank: i + 1,
|
||||
title: item.title ?? '',
|
||||
karma: item.baseScore ?? 0,
|
||||
comments: item.commentCount ?? 0,
|
||||
date: item.postedAt ?? '',
|
||||
url: `https://${DOMAIN}/posts/${item._id}/${item.slug}`,
|
||||
}));
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,45 @@
|
||||
import { cli, Strategy } from '@jackwener/opencli/registry';
|
||||
import { EmptyResultError } from '@jackwener/opencli/errors';
|
||||
import { DOMAIN, SITE, gqlEscape, gqlRequest, stripHtml } from './_helpers.js';
|
||||
cli({
|
||||
site: SITE,
|
||||
name: 'user',
|
||||
access: 'read',
|
||||
description: 'User profile',
|
||||
domain: DOMAIN,
|
||||
strategy: Strategy.PUBLIC,
|
||||
browser: false,
|
||||
args: [
|
||||
{
|
||||
name: 'username',
|
||||
type: 'string',
|
||||
required: true,
|
||||
positional: true,
|
||||
help: 'LessWrong username or slug',
|
||||
},
|
||||
],
|
||||
columns: ['field', 'value'],
|
||||
func: async (kwargs) => {
|
||||
const slug = gqlEscape(String(kwargs.username).toLowerCase());
|
||||
const query = `query UserProfile {
|
||||
user(input: {selector: {slug: "${slug}"}}) {
|
||||
result { _id displayName slug bio karma postCount commentCount createdAt }
|
||||
}
|
||||
}`;
|
||||
const data = await gqlRequest(query);
|
||||
const user = data?.user?.result;
|
||||
if (!user?._id) {
|
||||
throw new EmptyResultError(`lesswrong user ${String(kwargs.username)}`, 'Check the username — LessWrong slugs are lowercase (e.g. "zvi", "eliezer-yudkowsky")');
|
||||
}
|
||||
return [
|
||||
{ field: 'Name', value: user.displayName ?? '' },
|
||||
{ field: 'Username', value: user.slug ?? '' },
|
||||
{ field: 'Karma', value: user.karma ?? 0 },
|
||||
{ field: 'Posts', value: user.postCount ?? 0 },
|
||||
{ field: 'Comments', value: user.commentCount ?? 0 },
|
||||
{ field: 'Joined', value: user.createdAt ?? '' },
|
||||
{ field: 'Bio', value: stripHtml(user.bio ?? '') },
|
||||
{ field: 'URL', value: `https://${DOMAIN}/users/${user.slug}` },
|
||||
];
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user