Skip to content

Commit

Permalink
News feed comment fix (#2248)
Browse files Browse the repository at this point in the history
* Updated the code

* Updated

* Updated

* Updated

* Updated

* Updated

* Updated

* Updated

* Fixed View Model Test

* Updated

* Updated

* Updated

* Update pubspec.lock

* fixed order

* Updated
  • Loading branch information
imshivam-gupta authored Dec 23, 2023
1 parent 1401b74 commit f292741
Show file tree
Hide file tree
Showing 6 changed files with 342 additions and 95 deletions.
47 changes: 29 additions & 18 deletions lib/services/comment_service.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// ignore_for_file: talawa_api_doc, avoid_dynamic_calls
// ignore_for_file: talawa_good_doc_comments

import 'package:graphql_flutter/graphql_flutter.dart';
import 'package:talawa/locator.dart';
import 'package:talawa/services/database_mutation_functions.dart';
import 'package:talawa/utils/comment_queries.dart';
import 'package:talawa/utils/post_queries.dart';

/// CommentService class have different member functions which provides service in the context of commenting.
///
Expand All @@ -18,11 +17,14 @@ class CommentService {

/// This function is used to add comment on the post.
///
/// parameters:
/// * [postId] - Post id where comment need to be added.
/// * [text] - content of the comment.
/// To verify things are working, check out the native platform logs.
/// **params**:
/// * `postId`: The post id on which comment is to be added.
/// * `text`: The comment text.
///
/// **returns**:
/// * `Future<void>`: promise that will be fulfilled message background activities are successful.
Future<void> createComments(String postId, String text) async {
print("comment service called");
final String createCommentQuery = CommentQueries().createComment();
final result = await _dbFunctions.gqlAuthMutation(
createCommentQuery,
Expand All @@ -31,21 +33,30 @@ class CommentService {
'text': text,
},
);
print("comment added");
print(result);
return result;
}

/// This function is used to fetch all comments on the post.
/// This function is used to get all comments on the post.
///
/// To verify things are working, check out the native platform logs.
/// **params**:
/// * `postId`: The post id for which comments are to be fetched.
///
/// **returns**:
/// * `Future<List<dynamic>>`: promise that will be fulfilled with list of comments.
///
/// parameters:
/// * [postId] - Post id for which comments need to be fetched.
Future getCommentsForPost(String postId) async {
final String getCommmentQuery = CommentQueries().getPostsComments(postId);
final result = await _dbFunctions.gqlAuthMutation(getCommmentQuery);
if (result.data != null) {
return result.data["commentsByPost"] as List;
Future<List<dynamic>> getCommentsForPost(String postId) async {
final String getCommmentQuery = PostQueries().getPostById(postId);

final dynamic result = await _dbFunctions.gqlAuthMutation(getCommmentQuery);

if (result == null) {
return [];
}
return [];
final resultData = (result as QueryResult<Object?>).data;

final resultDataPostComments = (resultData?['post']
as Map<String, dynamic>)['comments'] as List<dynamic>;
return resultDataPostComments;
}
}
46 changes: 46 additions & 0 deletions lib/utils/post_queries.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,52 @@ class PostQueries {
""";
}

/// Getting Post by Post Id.
///
/// **params**:
/// * `postId`: The post id
///
/// **returns**:
/// * `String`: The query related to gettingPostsbyId
String getPostById(String postId) {
return """
query {
post(id: "$postId")
{
_id
text
createdAt
imageUrl
videoUrl
title
commentCount
likeCount
creator{
_id
firstName
lastName
image
}
organization{
_id
}
likedBy{
_id
}
comments{
_id,
text,
createdAt
creator{
firstName
lastName
}
}
}
}
""";
}

/// Add Like to a post.
///
/// **params**:
Expand Down
2 changes: 0 additions & 2 deletions lib/view_model/access_request_view_model.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// ignore_for_file: talawa_api_doc
import 'package:flutter/cupertino.dart';
import 'package:graphql_flutter/graphql_flutter.dart';
import 'package:talawa/constants/routing_constants.dart';
Expand All @@ -23,7 +22,6 @@ class AccessScreenViewModel extends BaseModel {

/// initialization function.
///
///
/// **params**:
/// * `org`: Org to send request to.
///
Expand Down
66 changes: 47 additions & 19 deletions lib/view_model/widgets_view_models/comments_view_model.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// ignore_for_file: talawa_api_doc
// ignore_for_file: talawa_good_doc_comments

import 'package:talawa/enums/enums.dart';
import 'package:talawa/locator.dart';
import 'package:talawa/models/comment/comment_model.dart';
Expand All @@ -9,25 +6,41 @@ import 'package:talawa/services/post_service.dart';
import 'package:talawa/services/user_config.dart';
import 'package:talawa/view_model/base_view_model.dart';

/// CommentsViewModel class helps to serve the data from model
/// and to react to user's input for Comment Widget.
/// CommentsViewModel class helps to serve the data from model and to react to user's input for Comment Widget.
///
/// Methods include:
/// * `getComments` : to get all comments on the post.
/// * `createComment` : to add comment on the post.
class CommentsViewModel extends BaseModel {
/// Constructor
late CommentService _commentService;

/// PostService instance.
late PostService _postService;

/// Post id on which comments are to be fetched.
late String _postID;

/// List of comments on the post.
late List<Comment> _commentlist;

/// UserConfig instance.
late UserConfig _userConfig;

// Getters
List<Comment> get commentList => _commentlist;
String get postId => _postID;

// initialiser.
Future initialise(String postID) async {
/// This function is used to initialise the CommentViewModel.
///
/// To verify things are working, check out the native platform logs.
/// **params**:
/// * `postID`: The post id for which comments are to be fetched.
///
/// **returns**:
/// * `Future<void>`: promise that will be fulfilled message background activities are successful.
///
Future<void> initialise(String postID) async {
_commentlist = [];
_postID = postID;
_commentService = locator<CommentService>();
Expand All @@ -37,31 +50,46 @@ class CommentsViewModel extends BaseModel {
await getComments();
}

/// This methods fetch all comments on the post.
/// The function uses `getCommentsForPost` method by Comment Service.
Future getComments() async {
/// This function is used to get all comments on the post.
///
/// To verify things are working, check out the native platform logs.
/// **params**:
/// None
///
/// **returns**:
/// * `Future<void>`: promise that will be fulfilled when comments are fetched.
///
Future<void> getComments() async {
setState(ViewState.busy);
final List commentsJSON =
await _commentService.getCommentsForPost(_postID) as List;
final List commentsJSON = await _commentService.getCommentsForPost(_postID);
print(commentsJSON);
commentsJSON.forEach((commentJson) {
_commentlist.add(Comment.fromJson(commentJson as Map<String, dynamic>));
});
setState(ViewState.idle);
}

/// This function add comment on the post.
/// The function uses `createComments` method provided by Comment Service.
/// This function add comment on the post. The function uses `createComments` method provided by Comment Service.
///
/// **params**:
/// * `msg`: The comment text.
///
/// params:
/// * `msg` : text of the comment to add.
Future createComment(String msg) async {
/// **returns**:
/// * `Future<void>`: promise that will be fulfilled when comment is added.
///
Future<void> createComment(String msg) async {
print("comment viewModel called");
await _commentService.createComments(_postID, msg);
addCommentLocally(msg);
}

// This function add comment locally.
/// This function add comment locally.
///
/// **params**:
/// * `msg`: BuildContext, contain parent info
///
/// **returns**:
/// None
void addCommentLocally(String msg) {
_postService.addCommentLocally(_postID);
final creator = _userConfig.currentUser;
Expand All @@ -70,7 +98,7 @@ class CommentsViewModel extends BaseModel {
createdAt: DateTime.now().toString(),
creator: creator,
);
_commentlist.insert(0, localComment);
_commentlist.add(localComment);
notifyListeners();
}
}
27 changes: 19 additions & 8 deletions test/helpers/test_helpers.mocks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2822,14 +2822,25 @@ class MockCommentService extends _i2.Mock implements _i35.CommentService {
) as _i4.Future<void>);

@override
_i4.Future<dynamic> getCommentsForPost(String? postId) => (super.noSuchMethod(
Invocation.method(
#getCommentsForPost,
[postId],
),
returnValue: _i4.Future<dynamic>.value(),
returnValueForMissingStub: _i4.Future<dynamic>.value(),
) as _i4.Future<dynamic>);
_i4.Future<List<dynamic>> getCommentsForPost(String? postId) {
final result = super.noSuchMethod(
Invocation.method(
#getCommentsForPost,
[postId],
),
returnValue: _i4.Future<List<dynamic>>.value(
[]), // Provide an empty list as a default value
returnValueForMissingStub: _i4.Future<List<dynamic>>.value([]),
);

// Check if the result is null, and return an empty list if it is
if (result == null) {
return _i4.Future<List<dynamic>>.value([]);
}

// Otherwise, cast the result to List<dynamic>
return result as _i4.Future<List<dynamic>>;
}
}

/// A class which mocks [AppTheme].
Expand Down
Loading

0 comments on commit f292741

Please sign in to comment.