mirror of
				https://gitlab.com/comunic/comunicmobile
				synced 2025-11-04 04:04:18 +00:00 
			
		
		
		
	Handles new comments events
This commit is contained in:
		@@ -1,5 +1,6 @@
 | 
				
			|||||||
import 'dart:async';
 | 
					import 'dart:async';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import 'package:comunic/models/comment.dart';
 | 
				
			||||||
import 'package:event_bus/event_bus.dart';
 | 
					import 'package:event_bus/event_bus.dart';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// Events helper
 | 
					/// Events helper
 | 
				
			||||||
@@ -23,6 +24,13 @@ class NewNumberUnreadConversations {
 | 
				
			|||||||
  NewNumberUnreadConversations(this.newNum);
 | 
					  NewNumberUnreadConversations(this.newNum);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// New comment
 | 
				
			||||||
 | 
					class NewCommentEvent {
 | 
				
			||||||
 | 
					  final Comment comment;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  NewCommentEvent(this.comment);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class EventsHelper {
 | 
					class EventsHelper {
 | 
				
			||||||
  static EventBus _mgr = EventBus();
 | 
					  static EventBus _mgr = EventBus();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,6 +1,7 @@
 | 
				
			|||||||
import 'dart:async';
 | 
					import 'dart:async';
 | 
				
			||||||
import 'dart:convert';
 | 
					import 'dart:convert';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import 'package:comunic/helpers/comments_helper.dart';
 | 
				
			||||||
import 'package:comunic/helpers/events_helper.dart';
 | 
					import 'package:comunic/helpers/events_helper.dart';
 | 
				
			||||||
import 'package:comunic/models/api_request.dart';
 | 
					import 'package:comunic/models/api_request.dart';
 | 
				
			||||||
import 'package:comunic/models/config.dart';
 | 
					import 'package:comunic/models/config.dart';
 | 
				
			||||||
@@ -39,8 +40,7 @@ class WebSocketHelper {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    // Determine WebSocket URI
 | 
					    // Determine WebSocket URI
 | 
				
			||||||
    final wsURL =
 | 
					    final wsURL =
 | 
				
			||||||
        "${(config().apiServerSecure ? "wss" : "ws")}://${config()
 | 
					        "${(config().apiServerSecure ? "wss" : "ws")}://${config().apiServerName}${config().apiServerUri}ws?token=$token";
 | 
				
			||||||
        .apiServerName}${config().apiServerUri}ws?token=$token";
 | 
					 | 
				
			||||||
    final wsURI = Uri.parse(wsURL);
 | 
					    final wsURI = Uri.parse(wsURL);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Connect
 | 
					    // Connect
 | 
				
			||||||
@@ -120,6 +120,12 @@ class WebSocketHelper {
 | 
				
			|||||||
        EventsHelper.emit(NewNumberUnreadConversations(msg.data));
 | 
					        EventsHelper.emit(NewNumberUnreadConversations(msg.data));
 | 
				
			||||||
        break;
 | 
					        break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      // New comment
 | 
				
			||||||
 | 
					      case "new_comment":
 | 
				
			||||||
 | 
					        EventsHelper.emit(
 | 
				
			||||||
 | 
					            NewCommentEvent(CommentsHelper.apiToComment(msg.data)));
 | 
				
			||||||
 | 
					        break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      default:
 | 
					      default:
 | 
				
			||||||
        throw Exception("Unknown message type: ${msg.title}");
 | 
					        throw Exception("Unknown message type: ${msg.title}");
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -4,7 +4,6 @@ import 'package:comunic/enums/post_kind.dart';
 | 
				
			|||||||
import 'package:comunic/enums/post_visibility_level.dart';
 | 
					import 'package:comunic/enums/post_visibility_level.dart';
 | 
				
			||||||
import 'package:comunic/helpers/comments_helper.dart';
 | 
					import 'package:comunic/helpers/comments_helper.dart';
 | 
				
			||||||
import 'package:comunic/helpers/posts_helper.dart';
 | 
					import 'package:comunic/helpers/posts_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/users_list.dart';
 | 
					import 'package:comunic/lists/users_list.dart';
 | 
				
			||||||
import 'package:comunic/models/comment.dart';
 | 
					import 'package:comunic/models/comment.dart';
 | 
				
			||||||
@@ -493,22 +492,6 @@ class _PostTileState extends State<PostTile> {
 | 
				
			|||||||
      if (commentID < 1) throw new Exception("Comment ID is inferior to 1!");
 | 
					      if (commentID < 1) throw new Exception("Comment ID is inferior to 1!");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      clearCommentForm();
 | 
					      clearCommentForm();
 | 
				
			||||||
 | 
					 | 
				
			||||||
      // Get and show new comment
 | 
					 | 
				
			||||||
      final newComment = await _commentsHelper.getSingle(commentID);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      if (newComment == null)
 | 
					 | 
				
			||||||
        return showSimpleSnack(
 | 
					 | 
				
			||||||
            context, tr("Could not retrieve created comment!"));
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      // Get information about the user who created the comment (if required)
 | 
					 | 
				
			||||||
      if (!widget.usersInfo.hasUser(newComment.userID))
 | 
					 | 
				
			||||||
        widget.usersInfo
 | 
					 | 
				
			||||||
            .add(await UsersHelper().getSingleWithThrow(newComment.userID));
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      setState(() {
 | 
					 | 
				
			||||||
        widget.post.comments.add(newComment);
 | 
					 | 
				
			||||||
      });
 | 
					 | 
				
			||||||
    } catch (e) {
 | 
					    } catch (e) {
 | 
				
			||||||
      print(e);
 | 
					      print(e);
 | 
				
			||||||
      showSimpleSnack(context, tr("Could not create comment!"));
 | 
					      showSimpleSnack(context, tr("Could not create comment!"));
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,9 +1,11 @@
 | 
				
			|||||||
 | 
					import 'package:comunic/helpers/events_helper.dart';
 | 
				
			||||||
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/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';
 | 
				
			||||||
import 'package:comunic/lists/users_list.dart';
 | 
					import 'package:comunic/lists/users_list.dart';
 | 
				
			||||||
 | 
					import 'package:comunic/models/comment.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';
 | 
				
			||||||
@@ -65,6 +67,9 @@ class PostsListWidgetState extends SafeState<PostsListWidget> {
 | 
				
			|||||||
    super.initState();
 | 
					    super.initState();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    _scrollController = ScrollWatcher(onReachBottom: reachedPostsBottom);
 | 
					    _scrollController = ScrollWatcher(onReachBottom: reachedPostsBottom);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Register to events
 | 
				
			||||||
 | 
					    this.listen<NewCommentEvent>((ev) => this._addComment(ev.comment));
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @override
 | 
					  @override
 | 
				
			||||||
@@ -219,4 +224,24 @@ class PostsListWidgetState extends SafeState<PostsListWidget> {
 | 
				
			|||||||
  void reachedPostsBottom() {
 | 
					  void reachedPostsBottom() {
 | 
				
			||||||
    if (widget.getOlder != null) loadPostsList(getOlder: true);
 | 
					    if (widget.getOlder != null) loadPostsList(getOlder: true);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  /// Add new comment
 | 
				
			||||||
 | 
					  void _addComment(Comment c) async {
 | 
				
			||||||
 | 
					    if (_list == null) return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    try {
 | 
				
			||||||
 | 
					      final p = _list.singleWhere((p) => p.id == c.postID, orElse: () => null);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      if (p == null) return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      if (!_users.hasUser(c.userID))
 | 
				
			||||||
 | 
					        _users.add(await UsersHelper().getSingleWithThrow(c.userID));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      setState(() {
 | 
				
			||||||
 | 
					        p.comments.add(c);
 | 
				
			||||||
 | 
					      });
 | 
				
			||||||
 | 
					    } catch (e, stack) {
 | 
				
			||||||
 | 
					      print("$e\n$stack");
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user