···
import '../config/environment_config.dart';
import '../models/comment.dart';
6
+
import '../models/community.dart';
import '../models/post.dart';
import 'api_exceptions.dart';
···
debugPrint('❌ Error parsing comments response: $e');
throw ApiException('Failed to parse server response', originalError: e);
362
+
/// List communities with optional filtering
364
+
/// Fetches a list of communities with pagination support.
365
+
/// Requires authentication.
368
+
/// - [limit]: Number of communities per page (default: 50, max: 100)
369
+
/// - [cursor]: Pagination cursor from previous response
370
+
/// - [sort]: Sort order - 'popular', 'new', or 'alphabetical' (default: 'popular')
371
+
Future<CommunitiesResponse> listCommunities({
374
+
String sort = 'popular',
378
+
debugPrint('📡 Fetching communities: sort=$sort, limit=$limit');
381
+
final queryParams = <String, dynamic>{
386
+
if (cursor != null) {
387
+
queryParams['cursor'] = cursor;
390
+
final response = await _dio.get(
391
+
'/xrpc/social.coves.community.list',
392
+
queryParameters: queryParams,
397
+
'✅ Communities fetched: '
398
+
'${response.data['communities']?.length ?? 0} communities',
402
+
return CommunitiesResponse.fromJson(
403
+
response.data as Map<String, dynamic>,
405
+
} on DioException catch (e) {
406
+
_handleDioException(e, 'communities');
409
+
debugPrint('❌ Error parsing communities response: $e');
411
+
throw ApiException('Failed to parse server response', originalError: e);
415
+
/// Create a new post in a community
417
+
/// Creates a new post with optional title, content, and embed.
418
+
/// Requires authentication.
421
+
/// - [community]: Community identifier (required)
422
+
/// - [title]: Post title (optional)
423
+
/// - [content]: Post content (optional)
424
+
/// - [embed]: External embed (link, image, etc.) (optional)
425
+
/// - [langs]: Language codes for the post (optional)
426
+
/// - [labels]: Self-applied content labels (optional)
427
+
Future<CreatePostResponse> createPost({
428
+
required String community,
431
+
ExternalEmbedInput? embed,
432
+
List<String>? langs,
433
+
SelfLabels? labels,
437
+
debugPrint('📡 Creating post in community: $community');
440
+
// Build request body with only non-null fields
441
+
final requestBody = <String, dynamic>{
442
+
'community': community,
445
+
if (title != null) {
446
+
requestBody['title'] = title;
449
+
if (content != null) {
450
+
requestBody['content'] = content;
453
+
if (embed != null) {
454
+
requestBody['embed'] = embed.toJson();
457
+
if (langs != null && langs.isNotEmpty) {
458
+
requestBody['langs'] = langs;
461
+
if (labels != null) {
462
+
requestBody['labels'] = labels.toJson();
465
+
final response = await _dio.post(
466
+
'/xrpc/social.coves.community.post.create',
471
+
debugPrint('✅ Post created successfully');
474
+
return CreatePostResponse.fromJson(
475
+
response.data as Map<String, dynamic>,
477
+
} on DioException catch (e) {
478
+
_handleDioException(e, 'create post');
481
+
debugPrint('❌ Error creating post: $e');
483
+
throw ApiException('Failed to create post', originalError: e);