Skip to content

Commit

Permalink
feat(*): Support parsing post section locked with reply
Browse files Browse the repository at this point in the history
  • Loading branch information
realth000 committed Dec 17, 2023
1 parent 37f9d0e commit 4248aa8
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

- 新增解析帖子楼层正文`postmessage`中的隐藏部分。
- 新增解析帖子中由于积分不足而隐藏的部分。
- 新增解析帖子中回复后才可见的部分。

### Fixed

Expand Down
4 changes: 4 additions & 0 deletions lib/i18n/strings.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@
"confirmInfo": "Purchase from $author with is price $price.\nThe author will get $authorProfit coins.\nYou will have $coinsLast coins left after purchase",
"successPurchase": "Purchase success",
"successPurchaseInfo": "Will refresh this page"
},
"reply": {
"title": "Some content requires reply",
"detail": "Reply to see contents here"
}
},
"rateCard": {
Expand Down
4 changes: 4 additions & 0 deletions lib/i18n/strings_zh-CN.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@
"confirmInfo": "从 $author 处以 $price 个天使币的价格购买帖子。\n作者会得到 $authorProfit 个天使币。\n购买后你还有 $coinsLast 个天使币。",
"successPurchase": "购买成功",
"successPurchaseInfo": "即将刷新页面"
},
"reply": {
"title": "部分需要回复才可浏览",
"detail": "回复以查看该部分内容"
}
},
"rateCard": {
Expand Down
4 changes: 4 additions & 0 deletions lib/i18n/strings_zh-TW.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@
"confirmInfo": "从 $author 处以 $price 個天使幣的价格帖子。\n作者會得到 $authorProfit 個天使幣。\n購買后你還有 $coinsLast 個天使幣。",
"successPurchase": "購買成功",
"successPurchaseInfo": "即將重新整理頁面"
},
"reply": {
"title": "部分需要回覆才可瀏覽",
"detail": "回覆以查看該部分內容"
}
},
"rateCard": {
Expand Down
24 changes: 24 additions & 0 deletions lib/models/locked.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ class _LockedInfo {
required String tid,
required String pid,
}) = _LockedWithPurchase;

const factory _LockedInfo.reply() = _LockedWithReply;
}

/// This section is invisible because current user does not have enough points.
@immutable
final class _LockedWithPoints extends _LockedInfo {
const _LockedWithPoints({
Expand All @@ -35,6 +38,7 @@ final class _LockedWithPoints extends _LockedInfo {
final int points;
}

/// This section needs purchase to be visible.
@immutable
final class _LockedWithPurchase extends _LockedInfo {
const _LockedWithPurchase(
Expand All @@ -57,6 +61,12 @@ final class _LockedWithPurchase extends _LockedInfo {
final int purchasedCount;
}

/// This section needs reply to be visible.
@immutable
final class _LockedWithReply extends _LockedInfo {
const _LockedWithReply() : super._();
}

class Locked {
/// Build a [Locked] from html node [element] where [element] is:
/// <div class="locked">
Expand All @@ -69,10 +79,12 @@ class Locked {
uh.Element element, {
bool allowWithPoints = true,
bool allowWithPurchase = true,
bool allowWithReply = true,
}) : _info = _buildLockedFromNode(
element,
allowWithPoints: allowWithPoints,
allowWithPurchase: allowWithPurchase,
allowWithReply: allowWithReply,
);

static final _re =
Expand All @@ -84,6 +96,8 @@ class Locked {

bool get lockedWithPurchase => _info != null && _info is _LockedWithPurchase;

bool get lockedWithReply => _info != null && _info is _LockedWithReply;

String? get tid {
if (_info == null) {
return null;
Expand Down Expand Up @@ -149,7 +163,13 @@ class Locked {
uh.Element element, {
required bool allowWithPoints,
required bool allowWithPurchase,
required bool allowWithReply,
}) {
// Check if is locked with reply.
if (allowWithReply && element.innerText.contains('查看本帖隐藏内容请回复')) {
return const _LockedInfo.reply();
}

final price = element
.querySelector('strong')
?.firstEndDeepText()
Expand Down Expand Up @@ -206,6 +226,10 @@ class Locked {
if (_info == null) {
return false;
}
if (_info is _LockedWithReply) {
return true;
}

if (_info is _LockedWithPurchase) {
return (_info! as _LockedWithPurchase).price > 0 &&
tid != null &&
Expand Down
3 changes: 2 additions & 1 deletion lib/models/post.dart
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ class Post {
final locked = postDataNode
?.querySelectorAll('div.locked')
.where((e) => e.querySelector('span') == null)
.map((e) => Locked.fromLockDivNode(e, allowWithPoints: false))
.map((e) => Locked.fromLockDivNode(e,
allowWithPoints: false, allowWithReply: false))
.toList();

final postFloor = postDataNode
Expand Down
11 changes: 11 additions & 0 deletions lib/widgets/locked_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,17 @@ class _LockedCardState extends ConsumerState<LockedCard> {
: _buildPurchaseButton(context),
),
]);
} else if (widget.locked.lockedWithReply) {
widgets.addAll([
Text(
context.t.lockedCard.reply.title,
style: Theme.of(context).textTheme.titleMedium,
),
Text(
context.t.lockedCard.reply.detail,
style: Theme.of(context).textTheme.labelMedium,
),
]);
}

return Card(
Expand Down

0 comments on commit 4248aa8

Please sign in to comment.