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

Displays a user list based on someone who has been chatting #37

Open
ahzanfariz opened this issue Apr 13, 2020 · 1 comment
Open

Displays a user list based on someone who has been chatting #37

ahzanfariz opened this issue Apr 13, 2020 · 1 comment

Comments

@ahzanfariz
Copy link

ahzanfariz commented Apr 13, 2020

Hello Team,
I like your project and it works well to use.

right now, i want to display a user list based on someone who has chat on me or vice versa.
I saw idFrom and idTo there.

how to do query it so that only those who ever chat have been displayed?

@MaheshPeri19
Copy link

I am storing like in below format to get only chatted users.

const kFirebaseUsersCollectionKey = "users";
const kFirebaseTextChattedUsersKey = "chatted_users";

class FirebaseHelpers {

// Saving peer user details in current user collection.
static Future saveChattedUsersListWithCurrentUser(
{String currentUserId,
String currentUserName,
String peerId,
String peerName}) async {
final QuerySnapshot result = await FirebaseFirestore.instance
.collection(kFirebaseUsersCollectionKey)
.doc(currentUserId)
.collection(kFirebaseTextChattedUsersKey)
.where(kFirebaseUserId, isEqualTo: peerId)
.get();

final List<DocumentSnapshot> documents = result.docs;

// print("chatted user documents is $documents");

if (documents.length == 0) {
  await FirebaseFirestore.instance
      .collection(kFirebaseUsersCollectionKey)
      .doc(currentUserId)
      .collection(kFirebaseTextChattedUsersKey)
      .doc(peerId)
      .set({
    kFirebaseChattedUserId: peerId,
    kFirebaseChattedUserName: peerName,
    kFirebaseChattedFollowUp: kFollowUpButtonTitle
  });
} else {
  await FirebaseFirestore.instance
      .collection(kFirebaseUsersCollectionKey)
      .doc(currentUserId)
      .collection(kFirebaseTextChattedUsersKey)
      .doc(peerId)
      .update({
    kFirebaseChattedUserId: peerId,
    kFirebaseChattedUserName: peerName,
    kFirebaseChattedFollowUp: kFollowUpButtonTitle
  });
}

}

// Saving current user details in peer user collection.
static Future saveChattedUsersListWithPeerUser(
{String currentUserId,
String currentUserName,
String peerId,
String peerName}) async {
final QuerySnapshot result = await FirebaseFirestore.instance
.collection(kFirebaseUsersCollectionKey)
.doc(peerId)
.collection(kFirebaseTextChattedUsersKey)
.where(kFirebaseUserId, isEqualTo: currentUserId)
.get();

final List<DocumentSnapshot> documents = result.docs;

// print("chatted user documents is $documents");

if (documents.length == 0) {
  await FirebaseFirestore.instance
      .collection(kFirebaseUsersCollectionKey)
      .doc(peerId)
      .collection(kFirebaseTextChattedUsersKey)
      .doc(currentUserId)
      .set({
    kFirebaseChattedUserId: currentUserId,
    kFirebaseChattedUserName: currentUserName,
    kFirebaseChattedFollowUp: kFollowUpButtonTitle
  });
} else {
  await FirebaseFirestore.instance
      .collection(kFirebaseUsersCollectionKey)
      .doc(peerId)
      .collection(kFirebaseTextChattedUsersKey)
      .doc(currentUserId)
      .update({
    kFirebaseChattedUserId: currentUserId,
    kFirebaseChattedUserName: currentUserName,
    kFirebaseChattedFollowUp: kFollowUpButtonTitle
  });
}

}
}

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

No branches or pull requests

2 participants