Skip to content

Commit bc518f5

Browse files
committed
feat: add post feed
1 parent 735566c commit bc518f5

File tree

10 files changed

+130
-21
lines changed

10 files changed

+130
-21
lines changed

.github/workflows/unit-test.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ jobs:
4141
- name: Install deps
4242
run: pnpm i --frozen-lockfile
4343

44+
- name: Lint
45+
run: pnpm lint
46+
4447
- name: Build
4548
run: pnpm build
4649

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"@vue-reactivity/fs": "^0.1.1",
3636
"commander": "^9.1.0",
3737
"enquirer": "^2.3.6",
38-
"jike-sdk": "^0.10.2",
38+
"jike-sdk": "^0.11.1",
3939
"node-fetch": "^3.2.3",
4040
"open": "^8.4.0",
4141
"terminal-image": "^2.0.0"

pnpm-lock.yaml

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/command/msg.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { logger } from '@poppinss/cliui'
22
import { createCommand } from 'commander'
33
import { JikeClient, limit } from 'jike-sdk/node'
44
import { format } from 'date-fns'
5-
import { displayImage, printRaw } from '../utils/terminal'
6-
import { displayUser, filterUsers } from '../utils/user'
5+
import { displayImage, printIfRaw, renderDivider } from '../utils/terminal'
6+
import { displayUsers, filterUsers } from '../utils/user'
77
import type { Spinner } from '@poppinss/cliui/build/src/Logger/Spinner'
88
import type { Entity } from 'jike-sdk/node'
99

@@ -46,10 +46,10 @@ const showNotifications = async (opts: NotificationOptions) => {
4646

4747
logger.success('Loading notifications done!')
4848

49-
printRaw(notifications)
49+
printIfRaw(notifications)
5050

5151
{
52-
const divider = logger.colors.gray('─'.repeat(process.stdout.columns || 30))
52+
const divider = renderDivider()
5353

5454
let spinner: Spinner | undefined
5555
if (opts.image) spinner = logger.await('Downloading images')
@@ -74,12 +74,7 @@ async function renderNotification(
7474
{ avatar, image }: NotificationOptions
7575
): Promise<string[]> {
7676
const users = n.actionItem?.users ?? []
77-
let usersText =
78-
users.length > 1
79-
? `${users.map((user) => displayUser(user)).join(', ')} `
80-
: users[0]
81-
? `${displayUser(users[0], true)} `
82-
: '-'
77+
let usersText = displayUsers(users)
8378
const bio = logger.colors.gray(users[0].bio ?? '')
8479

8580
const usersCount = n.actionItem?.usersCount
@@ -182,7 +177,6 @@ async function renderNotification(
182177
}
183178

184179
const warnUnknownType = (n: Entity.Notification) => {
185-
console.log(n)
186180
const info = [n.type, n.actionType, n.actionItem.type].join('||')
187181
logger.warning(
188182
`Unknown notification: ${info}. Please send it to developer, thanks!`

src/command/post/feed.ts

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import { createCommand } from 'commander'
2+
import { JikeClient, limit } from 'jike-sdk/node'
3+
import { logger } from '@poppinss/cliui'
4+
import { displayUser, displayUsers, filterUsers } from '../../utils/user'
5+
import { displayImage, printIfRaw, renderDivider } from '../../utils/terminal'
6+
import { isMacOS } from '../../utils/os'
7+
import type { Entity } from 'jike-sdk/node'
8+
9+
interface FeedOptions {
10+
count?: number
11+
}
12+
13+
export const feed = createCommand('feed')
14+
.description('feeds you are following')
15+
// TODO interactive next page, lastKey
16+
.option('-c, --count <count>', 'notification max count', '30')
17+
.action(() => {
18+
const opts = feed.opts<FeedOptions>()
19+
viewFeeds(opts)
20+
})
21+
22+
const viewFeeds = async (opts: FeedOptions) => {
23+
const [user] = filterUsers()
24+
const client = JikeClient.fromJSON(user)
25+
const count = +(opts.count ?? 30)
26+
27+
const updates = await client.queryFollowingUpdates({
28+
limit: limit.limitMaxCount(count),
29+
})
30+
printIfRaw(updates)
31+
32+
const divider = renderDivider()
33+
const texts: string[] = [divider]
34+
for (const update of updates) {
35+
texts.push(await renderPost(update), divider)
36+
}
37+
38+
process.stdout.write(`${texts.join('\n')}\n`)
39+
}
40+
41+
async function renderPost(p: Entity.FollowingUpdate) {
42+
const texts: string[] = []
43+
if (p.type === 'PERSONAL_UPDATE') {
44+
texts.push(`${displayUsers(p.users)} 关注了 ${displayUsers(p.targetUsers)}`)
45+
} else if (p.type === 'ORIGINAL_POST') {
46+
const link = isMacOS
47+
? logger.colors.gray(
48+
logger.colors.underline(`jike://page.jk/originalPost/${p.id}`)
49+
)
50+
: ''
51+
texts.push(
52+
(await displayImage(p.user.avatarImage.thumbnailUrl, 3)).result,
53+
`${displayUser(p.user)}${
54+
p.topic ? ` [${p.topic.content}]` : ''
55+
}: ${link}`,
56+
p.content
57+
)
58+
if (p.pictures && p.pictures.length > 0) {
59+
const images = await Promise.all(
60+
p.pictures.map((p) =>
61+
displayImage(p.middlePicUrl).then(({ result }) => result)
62+
)
63+
)
64+
texts.push(...images)
65+
}
66+
if (p.linkInfo) {
67+
texts.push(
68+
(await displayImage(p.linkInfo.pictureUrl)).result,
69+
`分享链接 [${p.linkInfo.title}](${logger.colors.blue(
70+
logger.colors.underline(p.linkInfo.linkUrl)
71+
)})`
72+
)
73+
}
74+
} else {
75+
// @ts-expect-error
76+
texts.push(`UNSUPPORTED: ${p.type}`)
77+
}
78+
79+
return texts.filter((text) => !!text.trim()).join('\n')
80+
// console.log()
81+
// if (p.type === 'PERSONAL_UPDATE') console.log(JSON.stringify(p, undefined, 2))
82+
// types.add(p.type)
83+
84+
// console.log(p)
85+
// const actions = new Set()
86+
87+
// actions.add(p.action)
88+
89+
// if (p.type === 'PERSONAL_UPDATE') console.log(p)
90+
// if (!p.user) console.log(p)
91+
// const userText = displayUser(p.user)
92+
// console.log(p.type === 'ORIGINAL_POST' ? '动态' : '转发')
93+
// console.log(p)
94+
95+
// console.log(`${userText}`)
96+
}

src/command/post/index.ts

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

45
export const post = createCommand('post')
56
.description('post-related operations')
@@ -9,7 +10,9 @@ export const post = createCommand('post')
910
1011
Example call:
1112
$ jike-cli post new --content="hello world"
13+
$ jike-cli post feed
1214
`
1315
)
1416
.usage('<command> [flags]')
1517
.addCommand(create)
18+
.addCommand(feed)

src/command/user/profile.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import process from 'process'
21
import { createCommand } from 'commander'
32
import { logger, sticker, table } from '@poppinss/cliui'
43
import { format } from 'date-fns'
54
import { JikeClient } from 'jike-sdk/node'
65
import { filterUsers } from '../../utils/user'
7-
import { displayImage, printRaw as printIfRaw } from '../../utils/terminal'
6+
import { displayImage, printIfRaw } from '../../utils/terminal'
87
import { PROFILE_URL } from '../../constants'
8+
import { isMacOS } from '../../utils/os'
99
import type { ApiResponses } from 'jike-sdk/node'
1010

1111
const { colors } = logger
@@ -104,7 +104,7 @@ export const queryProfile = async ({
104104
[],
105105
[' Web', colors.underline(PROFILE_URL.web + result.user.username)],
106106
['Mobile', colors.underline(PROFILE_URL.mobile + result.user.username)],
107-
process.platform === 'darwin'
107+
isMacOS
108108
? [' macOS', colors.underline(PROFILE_URL.mac + result.user.username)]
109109
: []
110110
)

src/utils/os.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const isMacOS = process.platform === 'darwin'

src/utils/terminal.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { logger } from '@poppinss/cliui'
12
import { program } from 'commander'
23
import terminalImage from 'terminal-image'
34

@@ -17,7 +18,7 @@ export const displayImage = async (url: string, height = 8) => {
1718
}
1819
}
1920

20-
export const printRaw = (data: any) => {
21+
export const printIfRaw = (data: any) => {
2122
interface Options {
2223
raw?: boolean
2324
pretty?: boolean
@@ -28,3 +29,6 @@ export const printRaw = (data: any) => {
2829
process.stdout.write(`${JSON.stringify(data, null, pretty ? 2 : 0)}\n`)
2930
process.exit(0)
3031
}
32+
33+
export const renderDivider = () =>
34+
logger.colors.gray('─'.repeat(process.stdout.columns || 30))

src/utils/user.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,15 @@ export const filterUsers = (customQueries?: string[], allowEmpty = true) => {
3737
export const displayConfigUser = (user: ConfigUser) =>
3838
user.alias || user.screenName
3939

40-
export const displayUser = (user: Entity.SimpleUser, displayUsername = false) =>
40+
export const displayUser = (user: Entity.User, displayUsername = false) =>
4141
`${logger.colors.yellow(`${user.screenName}`)}${
4242
displayUsername ? ` (${logger.colors.gray(user.username)})` : ''
4343
}`
44+
45+
export const displayUsers = (users: Entity.User[], displayUsername = true) => {
46+
return users.length > 1
47+
? users.map((user) => displayUser(user)).join(', ')
48+
: users[0]
49+
? displayUser(users[0], displayUsername)
50+
: '-'
51+
}

0 commit comments

Comments
 (0)