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

Register to post events

This commit is contained in:
2020-04-18 16:07:56 +02:00
parent 469e1e1f92
commit 02034acbbe
4 changed files with 91 additions and 13 deletions

View File

@ -4,6 +4,7 @@ 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/helpers/websocket_helper.dart';
import 'package:comunic/lists/comments_list.dart';
import 'package:comunic/lists/posts_list.dart';
import 'package:comunic/models/api_request.dart';
@ -46,11 +47,17 @@ const _APIPostsTargetKindsMap = {
};
class PostsHelper {
/// 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>();
/// 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: {
await APIRequest(uri: "posts/get_latest", needLogin: true, args: {
"include_groups": true.toString(),
"startFrom": from.toString(),
}).exec();
@ -69,8 +76,7 @@ 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)
..addInt("userID", userID)
..addInt("startFrom", from == 0 ? 0 : from - 1))
..addInt("userID", userID)..addInt("startFrom", from == 0 ? 0 : from - 1))
.exec();
if (response.code != 200) return null;
@ -87,8 +93,7 @@ class PostsHelper {
/// 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)
..addInt("groupID", groupID)
..addInt("startFrom", from == 0 ? 0 : from - 1))
..addInt("groupID", groupID)..addInt("startFrom", from == 0 ? 0 : from - 1))
.exec();
if (response.code != 200) return null;
@ -121,11 +126,11 @@ class PostsHelper {
/// This function crash in case of error
Future<void> createPost(NewPost post) async {
APIRequest request =
APIRequest(uri: "posts/create", needLogin: true, args: {
APIRequest(uri: "posts/create", needLogin: true, args: {
"kind-page": _APIPostsTargetKindsMap[post.target],
"kind-id": post.targetID.toString(),
"visibility": _APIPostsVisibilityLevelMap.map(
(s, v) => MapEntry(v, s))[post.visibility],
(s, v) => MapEntry(v, s))[post.visibility],
"kind": _APIPostsKindsMap.map((s, k) => MapEntry(k, s))[post.kind],
"content": post.content
});
@ -169,7 +174,7 @@ class PostsHelper {
args: {
"postID": id.toString(),
"new_level":
_APIPostsVisibilityLevelMap.map((k, v) => MapEntry(v, k))[level]
_APIPostsVisibilityLevelMap.map((k, v) => MapEntry(v, k))[level]
},
).exec())
.isOK;
@ -185,6 +190,28 @@ class PostsHelper {
.isOK;
}
/// 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});
}
}
/// Turn an API entry into a [Post] object
Post _apiToPost(Map<String, dynamic> map) {
final postKind = _APIPostsKindsMap[map["kind"]];