createGroupRoom method

Future<Room> createGroupRoom({
  1. Role creatorRole = types.Role.admin,
  2. String? imageUrl,
  3. Map<String, dynamic>? metadata,
  4. required String name,
  5. required List<User> users,
})

Creates a chat group room with users. Creator is automatically added to the group. name is required and will be used as a group name. Add an optional imageUrl that will be a group avatar and metadata for any additional custom data.

Implementation

Future<types.Room> createGroupRoom({
  types.Role creatorRole = types.Role.admin,
  String? imageUrl,
  Map<String, dynamic>? metadata,
  required String name,
  required List<types.User> users,
}) async {
  if (firebaseUser == null) return Future.error('User does not exist');

  final currentUser = await fetchUser(
    getFirebaseFirestore(),
    firebaseUser!.uid,
    config.usersCollectionName,
    role: creatorRole.toShortString(),
  );

  final roomUsers = [types.User.fromJson(currentUser)] + users;

  final room = await getFirebaseFirestore()
      .collection(config.roomsCollectionName)
      .add({
    'createdAt': FieldValue.serverTimestamp(),
    'imageUrl': imageUrl,
    'metadata': metadata,
    'name': name,
    'type': types.RoomType.group.toShortString(),
    'updatedAt': FieldValue.serverTimestamp(),
    'userIds': roomUsers.map((u) => u.id).toList(),
    'userRoles': roomUsers.fold<Map<String, String?>>(
      {},
      (previousValue, user) => {
        ...previousValue,
        user.id: user.role?.toShortString(),
      },
    ),
  });

  return types.Room(
    id: room.id,
    imageUrl: imageUrl,
    metadata: metadata,
    name: name,
    type: types.RoomType.group,
    users: roomUsers,
  );
}