mirror of
				https://gitlab.com/comunic/comunicmobile
				synced 2025-11-03 19:54:12 +00:00 
			
		
		
		
	Get latest posts from server
This commit is contained in:
		
							
								
								
									
										82
									
								
								lib/helpers/posts_helper.dart
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										82
									
								
								lib/helpers/posts_helper.dart
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,82 @@
 | 
			
		||||
import 'package:comunic/enums/post_kind.dart';
 | 
			
		||||
import 'package:comunic/enums/post_visibility_level.dart';
 | 
			
		||||
import 'package:comunic/enums/user_access_levels.dart';
 | 
			
		||||
import 'package:comunic/lists/posts_list.dart';
 | 
			
		||||
import 'package:comunic/models/api_request.dart';
 | 
			
		||||
import 'package:comunic/models/post.dart';
 | 
			
		||||
 | 
			
		||||
/// Posts helper
 | 
			
		||||
///
 | 
			
		||||
/// @author Pierre HUBERT
 | 
			
		||||
 | 
			
		||||
const _APIPostsVisibilityLevelMap = {
 | 
			
		||||
  "public": PostVisibilityLevel.PUBLIC,
 | 
			
		||||
  "friends": PostVisibilityLevel.FRIENDS,
 | 
			
		||||
  "private": PostVisibilityLevel.USER,
 | 
			
		||||
  "members": PostVisibilityLevel.GROUP_MEMBERS
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
const _APIPostsKindsMap = {
 | 
			
		||||
  "text": PostKind.TEXT,
 | 
			
		||||
  "image": PostKind.IMAGE,
 | 
			
		||||
  "weblink": PostKind.WEB_LINK,
 | 
			
		||||
  "pdf": PostKind.PDF,
 | 
			
		||||
  "movie": PostKind.MOVIE,
 | 
			
		||||
  "countdown": PostKind.COUNTDOWN,
 | 
			
		||||
  "survey": PostKind.SURVEY,
 | 
			
		||||
  "youtube": PostKind.YOUTUBE
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
const _APIUserAccessMap = {
 | 
			
		||||
  "no-access": UserAccessLevels.NONE,
 | 
			
		||||
  "basic": UserAccessLevels.BASIC,
 | 
			
		||||
  "intermediate": UserAccessLevels.INTERMEDIATE,
 | 
			
		||||
  "full": UserAccessLevels.FULL
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
class PostsHelper {
 | 
			
		||||
  /// Get the list of latest posts. Return the list of posts or null in case of
 | 
			
		||||
  /// failure
 | 
			
		||||
  Future<PostsList> getLatest() async {
 | 
			
		||||
    final response = await APIRequest(
 | 
			
		||||
      uri: "posts/get_latest",
 | 
			
		||||
      needLogin: true,
 | 
			
		||||
    ).exec();
 | 
			
		||||
 | 
			
		||||
    if (response.code != 200) return null;
 | 
			
		||||
 | 
			
		||||
    try {
 | 
			
		||||
      // Parse & return the list of posts
 | 
			
		||||
      return PostsList()..addAll(response.getArray().map((f) => _apiToPost(f)));
 | 
			
		||||
    } catch (e) {
 | 
			
		||||
      print(e.toString());
 | 
			
		||||
      return null;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /// Turn an API entry into a [Post] object
 | 
			
		||||
  Post _apiToPost(Map<String, dynamic> map) {
 | 
			
		||||
    return Post(
 | 
			
		||||
      id: map["ID"],
 | 
			
		||||
      userID: map["userID"],
 | 
			
		||||
      userPageID: map["user_page_id"],
 | 
			
		||||
      groupID: map["group_id"],
 | 
			
		||||
      timeSent: map["post_time"],
 | 
			
		||||
      content: map["content"],
 | 
			
		||||
      visibilityLevel: _APIPostsVisibilityLevelMap[map["visibility_level"]],
 | 
			
		||||
      kind: _APIPostsKindsMap[map["kind"]],
 | 
			
		||||
      fileSize: map["file_size"],
 | 
			
		||||
      fileType: map["file_type"],
 | 
			
		||||
      filePath: map["file_path"],
 | 
			
		||||
      fileURL: map["file_path_url"],
 | 
			
		||||
      timeEnd: map["time_end"],
 | 
			
		||||
      linkURL: map["link_url"],
 | 
			
		||||
      linkTitle: map["link_title"],
 | 
			
		||||
      linkDescription: map["link_description"],
 | 
			
		||||
      linkImage: map["link_image"],
 | 
			
		||||
      likes: map["likes"],
 | 
			
		||||
      userLikes: map["userlike"],
 | 
			
		||||
      access: _APIUserAccessMap[map["user_access"]],
 | 
			
		||||
    );
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@@ -51,6 +51,12 @@ class UsersHelper {
 | 
			
		||||
    return list;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /// Get users information from a given [Set]
 | 
			
		||||
  Future<UsersList> getList(Set<int> users,
 | 
			
		||||
      {bool forceDownload = false}) async {
 | 
			
		||||
    return await getUsersInfo(users.toList());
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /// Get users information
 | 
			
		||||
  ///
 | 
			
		||||
  /// If [forceDownload] is set to true, the data will always be retrieved from
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user