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

75 lines
2.1 KiB
Dart
Raw Normal View History

2019-05-18 12:58:35 +00:00
import 'package:comunic/models/api_request.dart';
2019-05-16 12:52:22 +00:00
import 'package:comunic/models/comment.dart';
2020-04-16 12:07:21 +00:00
import 'package:comunic/models/displayed_content.dart';
2019-05-18 12:58:35 +00:00
import 'package:comunic/models/new_comment.dart';
2019-05-16 12:52:22 +00:00
/// Comments helper
///
/// @author Pierre HUBERT
class CommentsHelper {
2019-05-18 12:58:35 +00:00
/// Send a new comment
///
/// Returns 0 or below in case of failure, the ID of the comment else
Future<int> createComment(NewComment comment) async {
final request = APIRequest(uri: "comments/create", needLogin: true, args: {
"postID": comment.postID.toString(),
"content": comment.hasContent ? comment.content : "",
});
2021-03-13 17:03:20 +00:00
if (comment.hasImage) request.addBytesFile("image", comment.image);
2019-05-18 12:58:35 +00:00
final response = await request.execWithFiles();
if (response.code != 200) return -1;
return response.getObject()["commentID"];
}
2019-05-18 13:05:26 +00:00
/// Get a single comment from the server, specified by its [id]
Future<Comment?> getSingle(int id) async {
2019-05-18 13:05:26 +00:00
final response = await APIRequest(
uri: "comments/get_single",
needLogin: true,
args: {"commentID": id.toString()}).exec();
if (response.code != 200) return null;
return apiToComment(response.getObject());
}
2019-05-18 16:48:12 +00:00
/// Update comment content
Future<bool> updateContent(int id, String? newContent) async {
2019-05-18 16:48:12 +00:00
return (await APIRequest(uri: "comments/edit", needLogin: true, args: {
"commentID": id.toString(),
"content": newContent,
}).exec())
.isOK;
}
2019-05-18 14:48:19 +00:00
/// Attempt to delete a comment
Future<bool> delete(int id) async {
return (await APIRequest(
2019-05-18 16:48:12 +00:00
uri: "comments/delete",
needLogin: true,
args: {"commentID": id.toString()},
).exec())
.code ==
200;
2019-05-18 14:48:19 +00:00
}
2019-05-16 12:52:22 +00:00
/// Turn an API entry into a [Comment] object
static Comment apiToComment(Map<String, dynamic> entry) {
return Comment(
id: entry["ID"],
userID: entry["userID"],
postID: entry["postID"],
timeSent: entry["time_sent"],
2020-04-16 12:07:21 +00:00
content: DisplayedString(entry["content"]),
2019-05-16 12:52:22 +00:00
imageURL: entry["img_url"],
likes: entry["likes"],
userLike: entry["userlike"],
);
}
}