1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-26 23:09:21 +00:00
comunicmobile/lib/helpers/posts_helper.dart

285 lines
8.1 KiB
Dart
Raw Normal View History

2019-05-10 17:15:11 +00:00
import 'package:comunic/enums/post_kind.dart';
2019-07-05 09:40:43 +00:00
import 'package:comunic/enums/post_target.dart';
2019-05-10 17:15:11 +00:00
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';
2019-06-28 09:32:36 +00:00
import 'package:comunic/helpers/survey_helper.dart';
2020-04-18 14:07:56 +00:00
import 'package:comunic/helpers/websocket_helper.dart';
2019-05-16 12:52:22 +00:00
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';
2020-04-16 12:07:21 +00:00
import 'package:comunic/models/displayed_content.dart';
2019-07-05 09:40:43 +00:00
import 'package:comunic/models/new_post.dart';
2019-05-10 17:15:11 +00:00
import 'package:comunic/models/post.dart';
2020-04-24 11:35:05 +00:00
import 'package:http_parser/http_parser.dart';
2019-05-10 17:15:11 +00:00
/// 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
};
2019-07-05 09:40:43 +00:00
const _APIPostsTargetKindsMap = {
PostTarget.USER_PAGE: "user",
PostTarget.GROUP_PAGE: "group"
};
2019-05-10 17:15:11 +00:00
class PostsHelper {
2020-04-18 14:07:56 +00:00
/// Stores the list of posts we are registered to
///
/// First int = post ID
/// Second int = number of registered people
static final _registeredPosts = Map<int, int>();
2019-05-10 17:15:11 +00:00
/// 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-06-28 09:32:36 +00:00
final response =
2020-04-24 11:35:05 +00:00
await APIRequest(uri: "posts/get_latest", needLogin: true, args: {
2019-06-28 09:32:36 +00:00
"include_groups": true.toString(),
"startFrom": from.toString(),
}).exec();
2019-05-10 17:15:11 +00:00
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 {
2020-04-16 07:17:10 +00:00
final response = await (APIRequest(uri: "posts/get_user", needLogin: true)
2020-04-24 11:35:05 +00:00
..addInt("userID", userID)
..addInt("startFrom", from == 0 ? 0 : from - 1))
2019-06-28 09:32:36 +00:00
.exec();
2019-06-10 12:47:27 +00:00
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;
}
}
2020-04-16 06:24:34 +00:00
/// Get the list of posts of a group
Future<PostsList> getGroupPosts(int groupID, {int from = 0}) async {
final response = await (APIRequest(uri: "posts/get_group", needLogin: true)
2020-04-24 11:35:05 +00:00
..addInt("groupID", groupID)
..addInt("startFrom", from == 0 ? 0 : from - 1))
2020-04-16 06:24:34 +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());
return null;
}
}
2019-11-02 11:46:17 +00:00
/// Get a single post information
Future<Post> getSingle(int postID) async {
final response = await APIRequest(
uri: "posts/get_single",
args: {"postID": postID.toString()},
needLogin: true,
).exec();
if (!response.isOK)
throw Exception("Could not get information about the post!");
return _apiToPost(response.getObject());
}
2019-07-05 09:40:43 +00:00
/// Create a new post
///
/// This function crash in case of error
Future<void> createPost(NewPost post) async {
APIRequest request =
2020-04-24 11:35:05 +00:00
APIRequest(uri: "posts/create", needLogin: true, args: {
2019-07-05 09:40:43 +00:00
"kind-page": _APIPostsTargetKindsMap[post.target],
"kind-id": post.targetID.toString(),
"visibility": _APIPostsVisibilityLevelMap.map(
2020-04-24 11:35:05 +00:00
(s, v) => MapEntry(v, s))[post.visibility],
2019-07-05 09:40:43 +00:00
"kind": _APIPostsKindsMap.map((s, k) => MapEntry(k, s))[post.kind],
"content": post.content
});
switch (post.kind) {
case PostKind.TEXT:
break;
case PostKind.IMAGE:
2021-02-07 16:09:08 +00:00
request.addPickedFile("image", post.image);
2019-07-05 09:40:43 +00:00
break;
2020-04-25 12:38:15 +00:00
case PostKind.WEB_LINK:
request.addString("url", post.url);
break;
2020-04-24 11:35:05 +00:00
case PostKind.PDF:
request.addBytesFile(
"pdf",
BytesFile("file.pdf", post.pdf,
type: MediaType.parse("application/pdf")));
break;
2020-04-25 06:23:52 +00:00
case PostKind.COUNTDOWN:
request.addInt(
"time-end", (post.timeEnd.millisecondsSinceEpoch / 1000).floor());
break;
2020-04-25 09:58:45 +00:00
case PostKind.SURVEY:
request.addString("question", post.survey.question);
request.addString("answers", post.survey.answers.join("<>"));
request.addBool("allowNewAnswers", post.survey.allowNewChoicesCreation);
2020-04-25 09:58:45 +00:00
break;
2020-04-25 15:16:33 +00:00
case PostKind.YOUTUBE:
request.addString("youtube_id", post.youtubeId);
break;
2019-07-05 09:40:43 +00:00
default:
throw Exception("Unsupported post type :" + post.kind.toString());
break;
}
final response = await request.execWithFiles();
if (!response.isOK) throw Exception("Could not create the post !");
}
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(),
2019-06-28 09:32:36 +00:00
"new_level":
2020-04-24 11:35:05 +00:00
_APIPostsVisibilityLevelMap.map((k, v) => MapEntry(v, k))[level]
2019-05-23 16:27:43 +00:00
},
).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;
}
2020-04-18 14:07:56 +00:00
/// Register to a post events
Future<void> registerPostEvents(int id) async {
if (_registeredPosts.containsKey(id))
_registeredPosts[id]++;
else {
_registeredPosts[id] = 1;
await ws("\$main/register_post", {"postID": id});
}
}
/// Un-register to post events
Future<void> unregisterPostEvents(int id) async {
if (!_registeredPosts.containsKey(id)) return;
_registeredPosts[id]--;
if (_registeredPosts[id] <= 0) {
_registeredPosts.remove(id);
await ws("\$main/unregister_post", {"postID": id});
}
}
2019-05-10 17:15:11 +00:00
/// Turn an API entry into a [Post] object
Post _apiToPost(Map<String, dynamic> map) {
2019-06-28 09:32:36 +00:00
final postKind = _APIPostsKindsMap[map["kind"]];
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-06-28 09:32:36 +00:00
final survey = postKind == PostKind.SURVEY
? SurveyHelper.apiToSurvey(map["data_survey"])
: null;
2019-05-10 17:15:11 +00:00
return Post(
2019-06-28 09:32:36 +00:00
id: map["ID"],
userID: map["userID"],
userPageID: map["user_page_id"],
groupID: map["group_id"],
timeSent: map["post_time"],
2020-04-16 12:07:21 +00:00
content: DisplayedString(map["content"]),
2019-06-28 09:32:36 +00:00
visibilityLevel: _APIPostsVisibilityLevelMap[map["visibility_level"]],
kind: postKind,
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"],
userLike: map["userlike"],
access: _APIUserAccessMap[map["user_access"]],
comments: comments,
survey: survey);
2019-05-10 17:15:11 +00:00
}
}