···
import '../config/environment_config.dart';
import '../models/comment.dart';
+
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);
+
/// List communities with optional filtering
+
/// Fetches a list of communities with pagination support.
+
/// Requires authentication.
+
/// - [limit]: Number of communities per page (default: 50, max: 100)
+
/// - [cursor]: Pagination cursor from previous response
+
/// - [sort]: Sort order - 'popular', 'new', or 'alphabetical' (default: 'popular')
+
Future<CommunitiesResponse> listCommunities({
+
String sort = 'popular',
+
debugPrint('📡 Fetching communities: sort=$sort, limit=$limit');
+
final queryParams = <String, dynamic>{
+
queryParams['cursor'] = cursor;
+
final response = await _dio.get(
+
'/xrpc/social.coves.community.list',
+
queryParameters: queryParams,
+
'✅ Communities fetched: '
+
'${response.data['communities']?.length ?? 0} communities',
+
return CommunitiesResponse.fromJson(
+
response.data as Map<String, dynamic>,
+
} on DioException catch (e) {
+
_handleDioException(e, 'communities');
+
debugPrint('❌ Error parsing communities response: $e');
+
throw ApiException('Failed to parse server response', originalError: e);
+
/// Create a new post in a community
+
/// Creates a new post with optional title, content, and embed.
+
/// Requires authentication.
+
/// - [community]: Community identifier (required)
+
/// - [title]: Post title (optional)
+
/// - [content]: Post content (optional)
+
/// - [embed]: External embed (link, image, etc.) (optional)
+
/// - [langs]: Language codes for the post (optional)
+
/// - [labels]: Self-applied content labels (optional)
+
Future<CreatePostResponse> createPost({
+
required String community,
+
ExternalEmbedInput? embed,
+
debugPrint('📡 Creating post in community: $community');
+
// Build request body with only non-null fields
+
final requestBody = <String, dynamic>{
+
'community': community,
+
requestBody['title'] = title;
+
requestBody['content'] = content;
+
requestBody['embed'] = embed.toJson();
+
if (langs != null && langs.isNotEmpty) {
+
requestBody['langs'] = langs;
+
requestBody['labels'] = labels.toJson();
+
final response = await _dio.post(
+
'/xrpc/social.coves.community.post.create',
+
debugPrint('✅ Post created successfully');
+
return CreatePostResponse.fromJson(
+
response.data as Map<String, dynamic>,
+
} on DioException catch (e) {
+
_handleDioException(e, 'create post');
+
debugPrint('❌ Error creating post: $e');
+
throw ApiException('Failed to create post', originalError: e);