1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-29 16:26:27 +00:00
comunicmobile/lib/helpers/posts_helper.dart

156 lines
4.2 KiB
Dart
Raw Normal View History

2019-05-10 17:15:11 +00:00
import 'package:comunic/enums/post_kind.dart';
import 'package:comunic/enums/post_visibility_level.dart';
import 'package:comunic/enums/user_access_levels.dart';
2019-05-16 12:52:22 +00:00
import 'package:comunic/helpers/comments_helper.dart';
import 'package:comunic/lists/comments_list.dart';
2019-05-10 17:15:11 +00:00
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({int from = 0}) async {
2019-05-10 17:15:11 +00:00
final response = await APIRequest(
uri: "posts/get_latest",
needLogin: true,
2019-06-10 07:47:02 +00:00
args: {
"include_groups": true.toString(),
"startFrom": from.toString(),
2019-06-10 07:47:02 +00:00
}
2019-05-10 17:15:11 +00:00
).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());
2019-06-10 12:47:27 +00:00
return null;
}
}
/// Get the list of posts of a user
Future<PostsList> getUserPosts(int userID, {int from = 0}) async {
final response = await APIRequest(
uri: "posts/get_user",
needLogin: true,
args: {
"userID": userID.toString(),
"startFrom": from.toString()
}
).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());
2019-05-10 17:15:11 +00:00
return null;
}
}
2019-05-19 15:42:09 +00:00
/// Update a post content
Future<bool> updateContent(int id, String newContent) async {
return (await APIRequest(
uri: "posts/update_content",
needLogin: true,
args: {
"postID": id.toString(),
"new_content": newContent,
},
).exec())
.isOK;
}
2019-05-23 16:27:43 +00:00
/// Update a post visibility
Future<bool> setVisibility(int id, PostVisibilityLevel level) async {
return (await APIRequest(
uri: "posts/set_visibility_level",
needLogin: true,
args: {
"postID": id.toString(),
"new_level": _APIPostsVisibilityLevelMap.map((k, v) => MapEntry(v, k))[level]
},
).exec())
.isOK;
}
2019-05-19 12:54:09 +00:00
/// Delete a post
Future<bool> delete(int id) async {
return (await APIRequest(
uri: "posts/delete",
needLogin: true,
args: {"postID": id.toString()},
).exec())
.isOK;
}
2019-05-10 17:15:11 +00:00
/// Turn an API entry into a [Post] object
Post _apiToPost(Map<String, dynamic> map) {
2019-05-16 12:52:22 +00:00
// Parse comments
CommentsList comments;
if (map["comments"] != null) {
comments = CommentsList();
map["comments"]
.forEach((v) => comments.add(CommentsHelper.apiToComment(v)));
}
2019-05-10 17:15:11 +00:00
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"],
2019-05-18 14:04:21 +00:00
userLike: map["userlike"],
2019-05-10 17:15:11 +00:00
access: _APIUserAccessMap[map["user_access"]],
2019-05-16 12:52:22 +00:00
comments: comments,
2019-05-10 17:15:11 +00:00
);
}
}