1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2025-06-19 08:15:16 +00:00

Add posts with surveys support

This commit is contained in:
2019-06-28 11:32:36 +02:00
parent 66b4d19004
commit fdf8cd08ef
9 changed files with 314 additions and 39 deletions

View File

@ -2,6 +2,7 @@ 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/helpers/comments_helper.dart';
import 'package:comunic/helpers/survey_helper.dart';
import 'package:comunic/lists/comments_list.dart';
import 'package:comunic/lists/posts_list.dart';
import 'package:comunic/models/api_request.dart';
@ -40,14 +41,11 @@ 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 {
final response = await APIRequest(
uri: "posts/get_latest",
needLogin: true,
args: {
"include_groups": true.toString(),
"startFrom": from.toString(),
}
).exec();
final response =
await APIRequest(uri: "posts/get_latest", needLogin: true, args: {
"include_groups": true.toString(),
"startFrom": from.toString(),
}).exec();
if (response.code != 200) return null;
@ -63,13 +61,10 @@ class PostsHelper {
/// 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();
uri: "posts/get_user",
needLogin: true,
args: {"userID": userID.toString(), "startFrom": from.toString()})
.exec();
if (response.code != 200) return null;
@ -102,7 +97,8 @@ class PostsHelper {
needLogin: true,
args: {
"postID": id.toString(),
"new_level": _APIPostsVisibilityLevelMap.map((k, v) => MapEntry(v, k))[level]
"new_level":
_APIPostsVisibilityLevelMap.map((k, v) => MapEntry(v, k))[level]
},
).exec())
.isOK;
@ -120,6 +116,8 @@ class PostsHelper {
/// Turn an API entry into a [Post] object
Post _apiToPost(Map<String, dynamic> map) {
final postKind = _APIPostsKindsMap[map["kind"]];
// Parse comments
CommentsList comments;
if (map["comments"] != null) {
@ -128,28 +126,32 @@ class PostsHelper {
.forEach((v) => comments.add(CommentsHelper.apiToComment(v)));
}
final survey = postKind == PostKind.SURVEY
? SurveyHelper.apiToSurvey(map["data_survey"])
: null;
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"],
userLike: map["userlike"],
access: _APIUserAccessMap[map["user_access"]],
comments: comments,
);
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: 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);
}
}

View File

@ -0,0 +1,55 @@
import 'package:comunic/models/api_request.dart';
import 'package:comunic/models/survey.dart';
import 'package:comunic/models/survey_choice.dart';
import 'package:meta/meta.dart';
/// Survey helper
///
/// @author Pierre HUBERT
class SurveyHelper {
/// Cancel the response of a user to a survey
Future<bool> cancelResponse(Survey survey) async {
return (await APIRequest(
uri: "surveys/cancel_response",
needLogin: true,
args: {"postID": survey.postID.toString()}).exec())
.isOK;
}
/// Send the response of a user to a survey
Future<bool> respondToSurvey(
{@required Survey survey, @required SurveyChoice choice}) async {
assert(survey != null);
assert(choice != null);
return (await APIRequest(
uri: "surveys/send_response",
needLogin: true,
args: {
"postID": survey.postID.toString(),
"choiceID": choice.id.toString(),
},
).exec())
.isOK;
}
/// Turn an API entry into a [Survey] object
static Survey apiToSurvey(Map<String, dynamic> map) {
// Parse survey responses
Set<SurveyChoice> choices = Set();
map["choices"].forEach((k, e) => choices.add(SurveyChoice(
id: e["choiceID"], name: e["name"], responses: e["responses"])));
return Survey(
id: map["ID"],
userID: map["userID"],
postID: map["postID"],
creationTime: map["creation_time"],
question: map["question"],
userChoice: map["user_choice"],
choices: choices,
);
}
}