-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from D-extremity/backend
app completed
- Loading branch information
Showing
4 changed files
with
197 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import 'package:chatapp/models/chatmodel.dart'; | ||
import 'package:cloud_firestore/cloud_firestore.dart'; | ||
import 'package:firebase_auth/firebase_auth.dart'; | ||
|
||
class ChatService { | ||
final FirebaseAuth _auth = FirebaseAuth.instance; | ||
final FirebaseFirestore _firestore = FirebaseFirestore.instance; | ||
|
||
//! Send Message | ||
Future<void> sendMessage( | ||
{required String recieverId, required String message}) async { | ||
//& Get Current User Info | ||
final String currentUserId = _auth.currentUser!.uid; | ||
final String currentUserEmail = _auth.currentUser!.email.toString(); | ||
final Timestamp timeStamp = Timestamp.now(); | ||
//& Create New Message | ||
|
||
Message newMessage = Message( | ||
senderId: currentUserId, | ||
recieverId: recieverId, | ||
message: message, | ||
senderEmail: currentUserEmail, | ||
timeStamp: timeStamp); | ||
//& Construct chat room id from senderId and recieverId (sort senderId and recieverId then create chatRoomId to avoid two different chatrooms for a single conversation room ) | ||
List<String> ids = [currentUserId, recieverId]; | ||
ids.sort(); | ||
final String chatRoomId = ids.join('_'); | ||
//& add new message to database | ||
await _firestore | ||
.collection('Chat Rooms') | ||
.doc(chatRoomId) | ||
.collection('messages') | ||
.add(newMessage.toMap()); | ||
} | ||
|
||
//& get Messages | ||
Stream<QuerySnapshot> getMessages( | ||
{required String userId, required String otherUserId}) { | ||
List<String> ids = [userId, otherUserId]; | ||
ids.sort(); | ||
final String chatRoomId = ids.join('_'); | ||
return _firestore | ||
.collection('Chat Rooms') | ||
.doc(chatRoomId) | ||
.collection('messages') | ||
.orderBy('timeStamp', descending: false) | ||
.snapshots(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// ignore_for_file: public_member_api_docs, sort_constructors_first | ||
import 'dart:convert'; | ||
|
||
import 'package:cloud_firestore/cloud_firestore.dart'; | ||
|
||
class Message { | ||
final String senderId; | ||
final String recieverId; | ||
final String message; | ||
final String senderEmail; | ||
final Timestamp timeStamp; | ||
|
||
Message({ | ||
required this.senderId, | ||
required this.recieverId, | ||
required this.message, | ||
required this.senderEmail, | ||
required this.timeStamp, | ||
}); | ||
|
||
Map<String, dynamic> toMap() { | ||
return <String, dynamic>{ | ||
'senderId': senderId, | ||
'recieverId': recieverId, | ||
'message': message, | ||
'senderEmail': senderEmail, | ||
'timeStamp': timeStamp, | ||
}; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters