|
| 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