Skip to content

Commit 6d373b9

Browse files
committed
fix(post): fix false positive failure when publish new thread
Same as 6a8bf54 Search forward thread url in header location and body content. Fixes #307
1 parent 4910035 commit 6d373b9

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

lib/features/post/repository/post_edit_repository.dart

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'package:fpdart/fpdart.dart';
44
import 'package:tsdm_client/constants/url.dart';
55
import 'package:tsdm_client/exceptions/exceptions.dart';
66
import 'package:tsdm_client/extensions/fp.dart';
7+
import 'package:tsdm_client/extensions/string.dart';
78
import 'package:tsdm_client/features/post/models/models.dart';
89
import 'package:tsdm_client/instance.dart';
910
import 'package:tsdm_client/shared/providers/net_client_provider/net_client_provider.dart';
@@ -132,26 +133,30 @@ final class PostEditRepository with LoggerMixin {
132133
return Right(value.headers!.value(HttpHeaders.locationHeader)!);
133134
case Left(:final value):
134135
return left(value);
135-
case Right(:final value) when value.statusCode == HttpStatus.ok:
136-
return left(
137-
ThreadPublishFailedException(
138-
HttpStatus.ok,
139-
message: parseHtmlDocument(value.data as String).querySelector('div#messagetext > p')?.innerText,
140-
),
141-
);
142-
case Right(:final value) when value.statusCode != HttpStatus.movedPermanently:
143-
return left(ThreadPublishFailedException(value.statusCode!));
144136
case Right(:final value):
145-
if (value.headers.map.containsKey(HttpHeaders.locationHeader)) {
146-
error('location header not found in response');
147-
return left(ThreadPublishLocationNotFoundException());
137+
final doc = parseHtmlDocument(value.data as String);
138+
final messageTextNode = doc.querySelector('div#messagetext > p');
139+
if (messageTextNode != null) {
140+
return left(ThreadPublishFailedException(value.statusCode!, message: messageTextNode.innerText));
148141
}
142+
143+
// On Android platform, redirect is handled and resp body is thread data, find tid in head > link.
144+
//
145+
// On other platforms, redirect is not handled the response is a 301 redirect and location header have
146+
// published thread url.
147+
149148
final locations = value.headers.map[HttpHeaders.locationHeader];
150-
if (locations?.isEmpty ?? true) {
151-
error('empty location header');
152-
return left(ThreadPublishLocationNotFoundException());
149+
if (locations?.isNotEmpty ?? false) {
150+
// Forward thread url in location.
151+
return right(locations!.first);
152+
}
153+
154+
final tid = doc.head?.querySelector('link')?.attributes['href']?.tryParseAsUri()?.queryParameters['tid'];
155+
if (tid != null) {
156+
// Forward thread url in head > link.
157+
return right('$baseUrl/forum.php?mod=viewthread&tid=$tid');
153158
}
154-
return right(locations!.first);
159+
return left(ThreadPublishLocationNotFoundException());
155160
}
156161
});
157162
}

0 commit comments

Comments
 (0)