mirror of
				https://gitlab.com/comunic/comunicmobile
				synced 2025-11-04 04:04:18 +00:00 
			
		
		
		
	Register to post events
This commit is contained in:
		@@ -4,6 +4,7 @@ import 'package:comunic/enums/post_visibility_level.dart';
 | 
				
			|||||||
import 'package:comunic/enums/user_access_levels.dart';
 | 
					import 'package:comunic/enums/user_access_levels.dart';
 | 
				
			||||||
import 'package:comunic/helpers/comments_helper.dart';
 | 
					import 'package:comunic/helpers/comments_helper.dart';
 | 
				
			||||||
import 'package:comunic/helpers/survey_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/comments_list.dart';
 | 
				
			||||||
import 'package:comunic/lists/posts_list.dart';
 | 
					import 'package:comunic/lists/posts_list.dart';
 | 
				
			||||||
import 'package:comunic/models/api_request.dart';
 | 
					import 'package:comunic/models/api_request.dart';
 | 
				
			||||||
@@ -46,11 +47,17 @@ const _APIPostsTargetKindsMap = {
 | 
				
			|||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class PostsHelper {
 | 
					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
 | 
					  /// Get the list of latest posts. Return the list of posts or null in case of
 | 
				
			||||||
  /// failure
 | 
					  /// failure
 | 
				
			||||||
  Future<PostsList> getLatest({int from = 0}) async {
 | 
					  Future<PostsList> getLatest({int from = 0}) async {
 | 
				
			||||||
    final response =
 | 
					    final response =
 | 
				
			||||||
        await APIRequest(uri: "posts/get_latest", needLogin: true, args: {
 | 
					    await APIRequest(uri: "posts/get_latest", needLogin: true, args: {
 | 
				
			||||||
      "include_groups": true.toString(),
 | 
					      "include_groups": true.toString(),
 | 
				
			||||||
      "startFrom": from.toString(),
 | 
					      "startFrom": from.toString(),
 | 
				
			||||||
    }).exec();
 | 
					    }).exec();
 | 
				
			||||||
@@ -69,8 +76,7 @@ class PostsHelper {
 | 
				
			|||||||
  /// Get the list of posts of a user
 | 
					  /// Get the list of posts of a user
 | 
				
			||||||
  Future<PostsList> getUserPosts(int userID, {int from = 0}) async {
 | 
					  Future<PostsList> getUserPosts(int userID, {int from = 0}) async {
 | 
				
			||||||
    final response = await (APIRequest(uri: "posts/get_user", needLogin: true)
 | 
					    final response = await (APIRequest(uri: "posts/get_user", needLogin: true)
 | 
				
			||||||
          ..addInt("userID", userID)
 | 
					      ..addInt("userID", userID)..addInt("startFrom", from == 0 ? 0 : from - 1))
 | 
				
			||||||
          ..addInt("startFrom", from == 0 ? 0 : from - 1))
 | 
					 | 
				
			||||||
        .exec();
 | 
					        .exec();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (response.code != 200) return null;
 | 
					    if (response.code != 200) return null;
 | 
				
			||||||
@@ -87,8 +93,7 @@ class PostsHelper {
 | 
				
			|||||||
  /// Get the list of posts of a group
 | 
					  /// Get the list of posts of a group
 | 
				
			||||||
  Future<PostsList> getGroupPosts(int groupID, {int from = 0}) async {
 | 
					  Future<PostsList> getGroupPosts(int groupID, {int from = 0}) async {
 | 
				
			||||||
    final response = await (APIRequest(uri: "posts/get_group", needLogin: true)
 | 
					    final response = await (APIRequest(uri: "posts/get_group", needLogin: true)
 | 
				
			||||||
          ..addInt("groupID", groupID)
 | 
					      ..addInt("groupID", groupID)..addInt("startFrom", from == 0 ? 0 : from - 1))
 | 
				
			||||||
          ..addInt("startFrom", from == 0 ? 0 : from - 1))
 | 
					 | 
				
			||||||
        .exec();
 | 
					        .exec();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (response.code != 200) return null;
 | 
					    if (response.code != 200) return null;
 | 
				
			||||||
@@ -121,11 +126,11 @@ class PostsHelper {
 | 
				
			|||||||
  /// This function crash in case of error
 | 
					  /// This function crash in case of error
 | 
				
			||||||
  Future<void> createPost(NewPost post) async {
 | 
					  Future<void> createPost(NewPost post) async {
 | 
				
			||||||
    APIRequest request =
 | 
					    APIRequest request =
 | 
				
			||||||
        APIRequest(uri: "posts/create", needLogin: true, args: {
 | 
					    APIRequest(uri: "posts/create", needLogin: true, args: {
 | 
				
			||||||
      "kind-page": _APIPostsTargetKindsMap[post.target],
 | 
					      "kind-page": _APIPostsTargetKindsMap[post.target],
 | 
				
			||||||
      "kind-id": post.targetID.toString(),
 | 
					      "kind-id": post.targetID.toString(),
 | 
				
			||||||
      "visibility": _APIPostsVisibilityLevelMap.map(
 | 
					      "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],
 | 
					      "kind": _APIPostsKindsMap.map((s, k) => MapEntry(k, s))[post.kind],
 | 
				
			||||||
      "content": post.content
 | 
					      "content": post.content
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
@@ -169,7 +174,7 @@ class PostsHelper {
 | 
				
			|||||||
      args: {
 | 
					      args: {
 | 
				
			||||||
        "postID": id.toString(),
 | 
					        "postID": id.toString(),
 | 
				
			||||||
        "new_level":
 | 
					        "new_level":
 | 
				
			||||||
            _APIPostsVisibilityLevelMap.map((k, v) => MapEntry(v, k))[level]
 | 
					        _APIPostsVisibilityLevelMap.map((k, v) => MapEntry(v, k))[level]
 | 
				
			||||||
      },
 | 
					      },
 | 
				
			||||||
    ).exec())
 | 
					    ).exec())
 | 
				
			||||||
        .isOK;
 | 
					        .isOK;
 | 
				
			||||||
@@ -185,6 +190,28 @@ class PostsHelper {
 | 
				
			|||||||
        .isOK;
 | 
					        .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
 | 
					  /// Turn an API entry into a [Post] object
 | 
				
			||||||
  Post _apiToPost(Map<String, dynamic> map) {
 | 
					  Post _apiToPost(Map<String, dynamic> map) {
 | 
				
			||||||
    final postKind = _APIPostsKindsMap[map["kind"]];
 | 
					    final postKind = _APIPostsKindsMap[map["kind"]];
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -591,7 +591,8 @@ class _PostTileState extends State<PostTile> {
 | 
				
			|||||||
      context: context,
 | 
					      context: context,
 | 
				
			||||||
      title: tr("Update post content"),
 | 
					      title: tr("Update post content"),
 | 
				
			||||||
      message: tr("Please enter message content: "),
 | 
					      message: tr("Please enter message content: "),
 | 
				
			||||||
      defaultValue: widget.post.content.isNull == null ? "" : widget.post.content.content,
 | 
					      defaultValue:
 | 
				
			||||||
 | 
					      widget.post.content.isNull == null ? "" : widget.post.content.content,
 | 
				
			||||||
      hint: tr("Post content"),
 | 
					      hint: tr("Post content"),
 | 
				
			||||||
    );
 | 
					    );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,4 +1,5 @@
 | 
				
			|||||||
import 'package:comunic/helpers/groups_helper.dart';
 | 
					import 'package:comunic/helpers/groups_helper.dart';
 | 
				
			||||||
 | 
					import 'package:comunic/helpers/posts_helper.dart';
 | 
				
			||||||
import 'package:comunic/helpers/users_helper.dart';
 | 
					import 'package:comunic/helpers/users_helper.dart';
 | 
				
			||||||
import 'package:comunic/lists/groups_list.dart';
 | 
					import 'package:comunic/lists/groups_list.dart';
 | 
				
			||||||
import 'package:comunic/lists/posts_list.dart';
 | 
					import 'package:comunic/lists/posts_list.dart';
 | 
				
			||||||
@@ -6,6 +7,7 @@ import 'package:comunic/lists/users_list.dart';
 | 
				
			|||||||
import 'package:comunic/models/post.dart';
 | 
					import 'package:comunic/models/post.dart';
 | 
				
			||||||
import 'package:comunic/ui/screens/conversation_screen.dart';
 | 
					import 'package:comunic/ui/screens/conversation_screen.dart';
 | 
				
			||||||
import 'package:comunic/ui/tiles/post_tile.dart';
 | 
					import 'package:comunic/ui/tiles/post_tile.dart';
 | 
				
			||||||
 | 
					import 'package:comunic/ui/widgets/safe_state.dart';
 | 
				
			||||||
import 'package:comunic/ui/widgets/scroll_watcher.dart';
 | 
					import 'package:comunic/ui/widgets/scroll_watcher.dart';
 | 
				
			||||||
import 'package:comunic/utils/intl_utils.dart';
 | 
					import 'package:comunic/utils/intl_utils.dart';
 | 
				
			||||||
import 'package:comunic/utils/ui_utils.dart';
 | 
					import 'package:comunic/utils/ui_utils.dart';
 | 
				
			||||||
@@ -41,7 +43,7 @@ class PostsListWidget extends StatefulWidget {
 | 
				
			|||||||
  State<StatefulWidget> createState() => PostsListWidgetState();
 | 
					  State<StatefulWidget> createState() => PostsListWidgetState();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class PostsListWidgetState extends State<PostsListWidget> {
 | 
					class PostsListWidgetState extends SafeState<PostsListWidget> {
 | 
				
			||||||
  // Helpers
 | 
					  // Helpers
 | 
				
			||||||
  final UsersHelper _usersHelper = UsersHelper();
 | 
					  final UsersHelper _usersHelper = UsersHelper();
 | 
				
			||||||
  final GroupsHelper _groupsHelper = GroupsHelper();
 | 
					  final GroupsHelper _groupsHelper = GroupsHelper();
 | 
				
			||||||
@@ -54,6 +56,8 @@ class PostsListWidgetState extends State<PostsListWidget> {
 | 
				
			|||||||
  ErrorLevel _error = ErrorLevel.NONE;
 | 
					  ErrorLevel _error = ErrorLevel.NONE;
 | 
				
			||||||
  bool _loading = false;
 | 
					  bool _loading = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  final _registeredPosts = Set<int>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  set error(ErrorLevel err) => setState(() => _error = err);
 | 
					  set error(ErrorLevel err) => setState(() => _error = err);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @override
 | 
					  @override
 | 
				
			||||||
@@ -63,6 +67,22 @@ class PostsListWidgetState extends State<PostsListWidget> {
 | 
				
			|||||||
    _scrollController = ScrollWatcher(onReachBottom: reachedPostsBottom);
 | 
					    _scrollController = ScrollWatcher(onReachBottom: reachedPostsBottom);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  @override
 | 
				
			||||||
 | 
					  void dispose() {
 | 
				
			||||||
 | 
					    super.dispose();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    _unregisterAllPosts();
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  @override
 | 
				
			||||||
 | 
					  void setState(fn) {
 | 
				
			||||||
 | 
					    if (!mounted) return;
 | 
				
			||||||
 | 
					    super.setState(fn);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Register for posts update
 | 
				
			||||||
 | 
					    _registerRequiredPosts();
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @override
 | 
					  @override
 | 
				
			||||||
  void didChangeDependencies() {
 | 
					  void didChangeDependencies() {
 | 
				
			||||||
    super.didChangeDependencies();
 | 
					    super.didChangeDependencies();
 | 
				
			||||||
@@ -111,6 +131,37 @@ class PostsListWidgetState extends State<PostsListWidget> {
 | 
				
			|||||||
    _loading = false;
 | 
					    _loading = false;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  /// Register for potential new posts
 | 
				
			||||||
 | 
					  void _registerRequiredPosts() async {
 | 
				
			||||||
 | 
					    if (_list == null) return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    final missing = _list
 | 
				
			||||||
 | 
					        .where((f) => !_registeredPosts.contains(f.id))
 | 
				
			||||||
 | 
					        .map((f) => f.id)
 | 
				
			||||||
 | 
					        .toSet();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    _registeredPosts.addAll(missing);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    for (final postID in missing) {
 | 
				
			||||||
 | 
					      await PostsHelper().registerPostEvents(postID);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  /// Unregister from all posts
 | 
				
			||||||
 | 
					  ///
 | 
				
			||||||
 | 
					  /// This method should be called only once
 | 
				
			||||||
 | 
					  void _unregisterAllPosts() async {
 | 
				
			||||||
 | 
					    for (final postID in _registeredPosts) {
 | 
				
			||||||
 | 
					      // We put the try - catch here because we must absolutely unregister ALL
 | 
				
			||||||
 | 
					      // POSTS event if one post fails to remove
 | 
				
			||||||
 | 
					      try {
 | 
				
			||||||
 | 
					        await PostsHelper().unregisterPostEvents(postID);
 | 
				
			||||||
 | 
					      } catch (e, stack) {
 | 
				
			||||||
 | 
					        print("Could not unregister post! $e $stack");
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  Widget _buildErrorCard() {
 | 
					  Widget _buildErrorCard() {
 | 
				
			||||||
    return buildErrorCard(tr("Could not get the list of posts !"));
 | 
					    return buildErrorCard(tr("Could not get the list of posts !"));
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,5 +1,4 @@
 | 
				
			|||||||
import 'package:comunic/models/displayed_content.dart';
 | 
					import 'package:comunic/models/displayed_content.dart';
 | 
				
			||||||
import 'package:comunic/utils/bbcode_parser.dart';
 | 
					 | 
				
			||||||
import 'package:comunic/utils/input_utils.dart';
 | 
					import 'package:comunic/utils/input_utils.dart';
 | 
				
			||||||
import 'package:comunic/utils/navigation_utils.dart';
 | 
					import 'package:comunic/utils/navigation_utils.dart';
 | 
				
			||||||
import 'package:flutter/material.dart';
 | 
					import 'package:flutter/material.dart';
 | 
				
			||||||
@@ -37,11 +36,11 @@ class TextWidget extends StatelessWidget {
 | 
				
			|||||||
    var content = this.content.parsedString;
 | 
					    var content = this.content.parsedString;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Parse BBcode
 | 
					    // Parse BBcode
 | 
				
			||||||
    if (parseBBcode)
 | 
					    /*if (parseBBcode)
 | 
				
			||||||
      return BBCodeParsedWidget(
 | 
					      return BBCodeParsedWidget(
 | 
				
			||||||
        text: content,
 | 
					        text: content,
 | 
				
			||||||
        parseCallback: (style, text) => _parseLinks(context, text, style),
 | 
					        parseCallback: (style, text) => _parseLinks(context, text, style),
 | 
				
			||||||
      );
 | 
					      );*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Just parse link
 | 
					    // Just parse link
 | 
				
			||||||
    return RichText(
 | 
					    return RichText(
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user