Skip to content

Commit 444cbdb

Browse files
committed
feat: add post list
1 parent 06e47a2 commit 444cbdb

File tree

3 files changed

+92
-17
lines changed

3 files changed

+92
-17
lines changed

src/command/post/feed.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ interface FeedOptions {
1818
export const feed = createCommand('feed')
1919
.description('feeds you are following')
2020
// TODO interactive next page, lastKey
21-
.option('-c, --count <count>', 'notification max count', '30')
21+
.option('-c, --count <count>', 'post max count', '30')
2222
.action(() => {
2323
const opts = feed.opts<FeedOptions>()
2424
viewFeeds(opts)
@@ -82,20 +82,4 @@ async function renderPost(p: Entity.FollowingUpdate) {
8282
}
8383

8484
return texts.filter((text) => !!text.trim()).join('\n')
85-
// console.log()
86-
// if (p.type === 'PERSONAL_UPDATE') console.log(JSON.stringify(p, undefined, 2))
87-
// types.add(p.type)
88-
89-
// console.log(p)
90-
// const actions = new Set()
91-
92-
// actions.add(p.action)
93-
94-
// if (p.type === 'PERSONAL_UPDATE') console.log(p)
95-
// if (!p.user) console.log(p)
96-
// const userText = displayUser(p.user)
97-
// console.log(p.type === 'ORIGINAL_POST' ? '动态' : '转发')
98-
// console.log(p)
99-
100-
// console.log(`${userText}`)
10185
}

src/command/post/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createCommand } from 'commander'
22
import { create } from './create'
33
import { feed } from './feed'
4+
import { list } from './list'
45

56
export const post = createCommand('post')
67
.description('post-related operations')
@@ -11,8 +12,10 @@ export const post = createCommand('post')
1112
Example call:
1213
$ jike-cli post new --content="hello world"
1314
$ jike-cli post feed
15+
$ jike-cli post list
1416
`
1517
)
1618
.usage('<command> [flags]')
1719
.addCommand(create)
1820
.addCommand(feed)
21+
.addCommand(list)

src/command/post/list.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import { createCommand } from 'commander'
2+
import { ApiOptions, limit } from 'jike-sdk'
3+
import { logger } from '@poppinss/cliui'
4+
import { createClient, displayUser, filterUsers } from '../../utils/user'
5+
import { displayImage, printIfRaw, renderDivider } from '../../utils/terminal'
6+
import { isMacOS } from '../../utils/os'
7+
import type { JikePostWithDetail } from 'jike-sdk'
8+
9+
interface ListOptions {
10+
count?: number
11+
username?: string
12+
}
13+
14+
export const list = createCommand('list')
15+
.alias('ls')
16+
.description('feeds you are following')
17+
.argument('[username]', 'the username of user')
18+
// TODO interactive next page, lastKey
19+
.option('-c, --count <count>', 'post max count', '30')
20+
.action((username: string) => {
21+
const opts = list.opts<ListOptions>()
22+
listPosts({ ...opts, username })
23+
})
24+
25+
const listPosts = async (opts: ListOptions) => {
26+
const [user] = filterUsers()
27+
const client = createClient(user)
28+
const count = +(opts.count ?? 30)
29+
opts.username ||= await client.getSelf().getUsername()
30+
31+
const updates = await client.getUser(opts.username).queryPersonalUpdate({
32+
limit: limit.limitMaxCount(count),
33+
})
34+
printIfRaw(updates)
35+
36+
const divider = renderDivider()
37+
const texts: string[] = [divider]
38+
for (const update of updates) {
39+
texts.push(await renderPost(update), divider)
40+
}
41+
42+
process.stdout.write(`${texts.join('\n')}\n`)
43+
}
44+
45+
async function renderPost(p: JikePostWithDetail) {
46+
const detail = p.getDetail()
47+
const texts: string[] = []
48+
if (p.type === ApiOptions.PostType.ORIGINAL) {
49+
const link = isMacOS
50+
? logger.colors.gray(
51+
logger.colors.underline(`jike://page.jk/originalPost/${p.id}`)
52+
)
53+
: ''
54+
texts.push(
55+
(await displayImage(detail.user.avatarImage.thumbnailUrl, 3)).result,
56+
`${displayUser(detail.user)}${
57+
// @ts-ignore
58+
detail.topic ? ` [${detail.topic.content}]` : ''
59+
}: ${link}`,
60+
detail.content
61+
)
62+
if (detail.pictures && detail.pictures.length > 0) {
63+
const images = await Promise.all(
64+
detail.pictures.map((p) =>
65+
displayImage(p.middlePicUrl).then(({ result }) => result)
66+
)
67+
)
68+
texts.push(...images)
69+
}
70+
// @ts-expect-error
71+
if (detail.linkInfo) {
72+
texts.push(
73+
// @ts-expect-error
74+
(await displayImage(detail.linkInfo.pictureUrl)).result,
75+
// @ts-expect-error
76+
`分享链接 [${detail.linkInfo.title}](${logger.colors.blue(
77+
// @ts-expect-error
78+
logger.colors.underline(detail.linkInfo.linkUrl)
79+
)})`
80+
)
81+
}
82+
} else {
83+
// TODO: repost
84+
texts.push(`UNSUPPORTED: ${p.type}`)
85+
}
86+
87+
return texts.filter((text) => !!text.trim()).join('\n')
88+
}

0 commit comments

Comments
 (0)