Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block or Unblock a user #107

Open
Melak12 opened this issue Mar 7, 2023 · 1 comment
Open

Block or Unblock a user #107

Melak12 opened this issue Mar 7, 2023 · 1 comment
Labels
feature New feature or request

Comments

@Melak12
Copy link

Melak12 commented Mar 7, 2023

In a certain chat conversation, we might want to block the other user in case of, for instance, abusive content. Currently, I don't see any option in the documentation to achieve this. I came up with this feature after the ios publish review team rejected the app in order to include a feature to block a user in case of EULA violations.

As a user, I want to block the other user so that I don't want to get our chat room and conversations.

@Melak12 Melak12 added the feature New feature or request label Mar 7, 2023
@Riyaz7364
Copy link

I was also facing the same issue while using this plugging after struggling I ask ChatGPT to help me. So 80% credits chatGPT for this code

You have to create a function to block a user.

      ```
          Future<void> blockUser(String userId, String otherUserId) async {
                      final userDoc = await fetchUser(
                          FirebaseFirestore.instance, userId, config.usersCollectionName);
                  
                      final blockUserList = userDoc['blockID'];
                      blockUserList!.add(otherUserId);
                      getFirebaseFirestore()
                          .collection(config.usersCollectionName)
                          .doc(userId)
                          .update({'blockID': blockUserList});
                    }

Also, add this line at the end of the fetchUser function   data['blockID'] = data['blockID'] ?? [];

You can call this function from chat_screen in the Chat widget there is a  method to detect the user clicking on the avatar, of course, you have to enable the avatar for the user first this option exists in the Chat widget.  

Then you can create a dialog or something I'm not gonna tell you this small thing.

Thats all for the user block.

Now the main part Filters the block user message Here ChatGPT helps me to create this function. YEAH, I WAS do some mistakes IA HELP ME to fix all mistakes.

Now Replace the message Stream -> Locates in firebase_core_chat.dart

`  Stream<List<types.Message>> messages(
    types.User meUser,
    types.Room room, {
    List<Object?>? endAt,
    List<Object?>? endBefore,
    int? limit,
    List<Object?>? startAfter,
    List<Object?>? startAt,
  }) async* {
    final userDoc = await fetchUser(
        FirebaseFirestore.instance, meUser.id, config.usersCollectionName);

    final blockUserList = userDoc['blockID'];
    var query = getFirebaseFirestore()
        .collection(config.roomsCollectionName)
        .doc(room.id)
        .collection('messages')
        .orderBy('createdAt', descending: true);

    if (endAt != null) {
      query = query.endAt(endAt);
    }

    if (endBefore != null) {
      query = query.endBefore(endBefore);
    }

    if (limit != null) {
      query = query.limit(limit);
    }

    if (startAfter != null) {
      query = query.startAfter(startAfter);
    }

    if (startAt != null) {
      query = query.startAt(startAt);
    }

    await for (final snapshot in query.snapshots()) {
      final messages = snapshot.docs.fold<List<types.Message>>(
        [],
        (previousValue, doc) {
          final data = doc.data();
          final author = room.users.firstWhere(
            (u) => u.id == data['authorId'],
            orElse: () => types.User(id: data['authorId'] as String),
          );

          data['author'] = author.toJson();
          data['createdAt'] = data['createdAt']?.millisecondsSinceEpoch;
          data['id'] = doc.id;
          data['updatedAt'] = data['updatedAt']?.millisecondsSinceEpoch;
          final message = types.Message.fromJson(data);

          if (blockUserList != null &&
              blockUserList!.contains(message.author.id)) {
            // If the author is blocked, skip this message
            print('Author is blocked, skipping message');
            return previousValue;
          }

          // If the author is not blocked, add the message to the list of previous messages
          print('Adding message to list');
          return [...previousValue, message];
        },
      );
      yield messages;
    }
  }`

That is all you need to do. 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants