Skip to content

Commit

Permalink
fix(html): recognize user credit and permission page url
Browse files Browse the repository at this point in the history
  • Loading branch information
realth000 committed Dec 27, 2024
1 parent fce3d26 commit cbedd51
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 2 deletions.
5 changes: 5 additions & 0 deletions lib/exceptions/exceptions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,11 @@ final class ThreadPublishLocationNotFoundException extends AppException
final class NotificationUserNotFound extends AppException
with NotificationUserNotFoundMappable {}

/// Notification not found in bloc.
@MappableClass()
final class NotificationNotFound extends AppException
with NotificationNotFoundMappable {}

/// Cookie not found in storage when doing auto checkin for user [userInfo].
///
/// Means a checkin failure.
Expand Down
30 changes: 30 additions & 0 deletions lib/extensions/string.dart
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,36 @@ extension ParseUrl on String {
String? uriQueryParameter(String name) {
return Uri.parse(this).queryParameters[name];
}

/// Check a string is pattern of user space url.
///
/// A string is a valid user space url ONLY if
///
/// 1. Is a valid url.
/// 2. In query parameters, 'mod' == 'space'.
/// 3. In query parameters, contains key 'uid' or 'username'. (email ignored).
/// 4. In query parameters, value of 'ac' is neither 'usergroup' nor 'credit'.
bool get isUserSpaceUrl {
final args = Uri.tryParse(this)?.queryParameters;
if (args == null) {
return false;
}

if (args['mod'] != 'space') {
return false;
}

if (args['uid'] == null && args['username'] == null) {
return false;
}

final ac = args['ac'];
if (ac == 'usergroup' || ac == 'credit') {
return false;
}

return true;
}
}

/// Extension on [String] that enhances modification.
Expand Down
5 changes: 5 additions & 0 deletions lib/features/notification/bloc/notification_bloc.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:bloc/bloc.dart';
import 'package:dart_mappable/dart_mappable.dart';
import 'package:fpdart/fpdart.dart';
import 'package:tsdm_client/exceptions/exceptions.dart';
import 'package:tsdm_client/extensions/date_time.dart';
import 'package:tsdm_client/extensions/fp.dart';
import 'package:tsdm_client/extensions/string.dart';
Expand Down Expand Up @@ -400,6 +401,10 @@ class NotificationBloc extends Bloc<NotificationEvent, NotificationState>
final task = switch (recordMark) {
RecordMarkNotice(:final uid, :final nid, alreadyRead: final read) => () {
final targetIndex = state.noticeList.indexWhere((e) => e.id == nid);
if (targetIndex < 0) {
// target not found.
return AsyncVoidEither(() async => left(NotificationNotFound()));
}
final target = state.noticeList[targetIndex];
final list = state.noticeList.toList();
list[targetIndex] = target.copyWith(alreadyRead: read);
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/html/html_muncher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,7 @@ final class _Muncher with LoggerMixin {
return ret;
}
final Widget content;
if (url.contains('mod=space') && !element.innerText.contains('@')) {
if (url.isUserSpaceUrl && !element.innerText.contains('@')) {
content = Text(
'@',
style: TextStyle(
Expand Down
2 changes: 1 addition & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2078,4 +2078,4 @@ packages:
version: "3.1.3"
sdks:
dart: ">=3.6.0 <4.0.0"
flutter: ">=3.27.0"
flutter: ">=3.27.1"

0 comments on commit cbedd51

Please sign in to comment.