import 'dart:async'; import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:linkchat/models/favorite.dart'; import 'package:linkchat/models/group.dart'; import 'package:linkchat/models/message.dart'; import 'package:simple_link_preview/simple_link_preview.dart'; import '../models/user.dart'; class Database { final FirebaseFirestore _firestore = FirebaseFirestore.instance; // // USERS // Future getUserById(String uid) async { var snap = await _firestore.collection('users').doc(uid).get(); return snap.data() != null ? FsUser.fromMap(snap.data()!) : null; } Stream> getAllUsers() { return _firestore.collection('users').snapshots().map>((e) { return e.docs.map((e) { return FsUser.fromMap(e.data()); }).toList(); }); } Future saveUser(FsUser user) async { await _firestore.collection('users').doc(user.uid).set({ "uid": user.uid, "displayName": user.displayName, "photoUrl": user.photoUrl, "email": user.email, }); } // // GROUPS // Stream> getGroupsByUserID(String uid) { return _firestore .collection('groups') .where('members', arrayContains: uid) .snapshots() .map>((e) { return e.docs.map((e) { return Group.fromMap(e.data(), e.id); }).toList(); }); } Future saveGroup(Group group) async { await _firestore.collection('groups').add(group.toMap()); } // // MESSAGES // Stream> getMessagesByGroupId(String id) { return _firestore .collection('messages') .doc(id) .collection('messages') .orderBy('sentAt') .snapshots() .map>((e) { return e.docs.map((e) { return Message.fromMap(e.data(), e.id); }).toList(); }); } Future saveMessage(Message msg, String groupId) async { LinkPreview? preview = await SimpleLinkPreview.getPreview(msg.messageText); await _firestore .collection('messages') .doc(groupId) .collection('messages') .add(msg.toMap(preview: preview)); await _firestore.collection('groups').doc(groupId).update({ "recentMessage": msg.toMap(preview: preview), }); } // // FAVORITES // Stream> getFavoritesByUserID(String uid) { return _firestore .collection('favorites') .doc(uid) .collection('favorites') .orderBy('savedAt', descending: true) .snapshots() .map>( (e) => e.docs.map((e) => Favorite.fromMap(e.data())).toList(), ); } Stream hasFavoriteForMessage(String userId, String? messageId) { return _firestore .collection('favorites') .doc(userId) .collection('favorites') .where('messageId', isEqualTo: messageId) .snapshots() .map((e) => e.docs.isNotEmpty); } Future saveFavorite(Favorite favorite, String userId) { return _firestore .collection('favorites') .doc(userId) .collection('favorites') .add(favorite.toMap()); } Future removeFavorite(String userId, String? messageId) async { CollectionReference reference = _firestore.collection('favorites').doc(userId).collection('favorites'); var favorites = await reference.where('messageId', isEqualTo: messageId).get(); for (var e in favorites.docs) { reference.doc(e.id).delete(); } } }