Functions To Update Chats and Converation Table
Initial Database Design for Conversations Update Conversations table when new records/modifications are made to chat table Agboola
This commit is contained in:
@@ -1,8 +1,13 @@
|
|||||||
Appwrite Functions to manage project
|
# Appwrite Functions to manage project - https://git.theonasanyas.com/agboola/MessagingApp
|
||||||
|
|
||||||
https://git.theonasanyas.com/agboola/MessagingApp
|
## These functions are server side codes that create and updatte records based on set conditions
|
||||||
|
|
||||||
These functions are server side codes that create and updatte records based on set conditions
|
|
||||||
|
|
||||||
1. When a new chat is created, a conversation collection record is created/updated to reflect same.
|
1. When a new chat is created, a conversation collection record is created/updated to reflect same.
|
||||||
2. The conversation collection is used as a defualt list view for all the chats per user. this makes it easier to pull and reference chats
|
2. The conversation collection is used as a defualt list view for all the chats per user. this makes it easier to pull and reference chats
|
||||||
|
|
||||||
|
|
||||||
|
# functions - all written in dart
|
||||||
|
|
||||||
|
1. https://git.theonasanyas.com/agboola/MessagingApp_Functions/src/branch/main/functions/conversations_upd_function
|
||||||
|
2. https://git.theonasanyas.com/agboola/MessagingApp_Functions/src/branch/main/functions/mesaages_upd_function
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
import 'dart:convert';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
import 'package:dart_appwrite/dart_appwrite.dart';
|
import 'package:dart_appwrite/dart_appwrite.dart';
|
||||||
|
|
||||||
@@ -34,9 +35,9 @@ Future<dynamic> main(final context) async {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} on AppwriteException catch (e) {
|
} on AppwriteException catch (e) {
|
||||||
context.error('error message - ${e.message}');
|
context.error('error message (AppWrite)- ${e.message}');
|
||||||
} on Exception catch (e) {
|
} on Exception catch (e) {
|
||||||
context.error('error message - ${e.toString()}');
|
context.error('error message (General)- ${e.toString()}');
|
||||||
}
|
}
|
||||||
return context.res.empty();
|
return context.res.empty();
|
||||||
}
|
}
|
||||||
@@ -76,14 +77,16 @@ dynamic createConversation(var member, var _members) async {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
String _data = '{"name": "$_name","image": "$_imageUrl","unseenCount": 1}';
|
var _data = '{"name": "$_name","image": "$_imageUrl","unseenCount": 0}';
|
||||||
_chatIdMap.add(_documentId);
|
|
||||||
|
_chatIdMap.add(_documentId.toString().trim());
|
||||||
_detailMap.add(_data);
|
_detailMap.add(_data);
|
||||||
|
|
||||||
Map<dynamic, dynamic> _conversationMap = {
|
Map<String, dynamic> _conversationMap = {
|
||||||
"chatId": _chatIdMap,
|
"chatId": _chatIdMap,
|
||||||
"details": _detailMap
|
"details": _detailMap
|
||||||
};
|
};
|
||||||
|
|
||||||
await _database.createDocument(
|
await _database.createDocument(
|
||||||
databaseId: _databaseId,
|
databaseId: _databaseId,
|
||||||
collectionId: _conversationDbId,
|
collectionId: _conversationDbId,
|
||||||
@@ -91,6 +94,5 @@ dynamic createConversation(var member, var _members) async {
|
|||||||
data: _conversationMap,
|
data: _conversationMap,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
@@ -1,15 +1,20 @@
|
|||||||
|
// 6793b2543197428f812a 679d942812784f70a3d6
|
||||||
|
// {"message":" Hi Daddy ABCDe", "senderId": "679d942812784f70a3d6", "timeStamp": "2025-01-29T16:33:19.480+00:00", "type": "text"}
|
||||||
|
// {"message":"Cool Bro, you good?", "senderId": "6793b2543197428f812a", "timeStamp": "2025-01-29T16:36:19.480+00:00", "type": "text"}
|
||||||
|
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
import 'dart:convert';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
import 'package:dart_appwrite/dart_appwrite.dart';
|
import 'package:dart_appwrite/dart_appwrite.dart';
|
||||||
|
|
||||||
late Databases _database;
|
late Databases _database;
|
||||||
|
|
||||||
var _usersDbId;
|
|
||||||
var _conversationDbId;
|
var _conversationDbId;
|
||||||
var _documentId;
|
var _documentId;
|
||||||
var _databaseId;
|
var _databaseId;
|
||||||
|
|
||||||
Future<dynamic> main(final context) async {
|
Future<dynamic> main(final context) async {
|
||||||
|
//
|
||||||
try {
|
try {
|
||||||
final client = Client()
|
final client = Client()
|
||||||
.setEndpoint(
|
.setEndpoint(
|
||||||
@@ -18,11 +23,74 @@ Future<dynamic> main(final context) async {
|
|||||||
.setKey(context.req.headers['x-appwrite-key'] ?? '');
|
.setKey(context.req.headers['x-appwrite-key'] ?? '');
|
||||||
|
|
||||||
_database = Databases(client);
|
_database = Databases(client);
|
||||||
} on AppwriteException catch (e) {
|
_conversationDbId = Platform.environment['APPWRITE_COLLECTION_ID'];
|
||||||
context.error('error message - ${e.message}');
|
|
||||||
} on Exception catch (e) {
|
|
||||||
context.error('error message - ${e.toString()}');
|
|
||||||
}
|
|
||||||
|
|
||||||
|
String trigger = context.req.headers['x-appwrite-trigger'];
|
||||||
|
if (trigger == 'event') {
|
||||||
|
List<dynamic> _members = context.req.bodyJson['members'];
|
||||||
|
List<dynamic> _messages = context.req.bodyJson['messages'];
|
||||||
|
_documentId = context.req.bodyJson['\$id'];
|
||||||
|
_databaseId = context.req.bodyJson['\$databaseId'];
|
||||||
|
|
||||||
|
for (String _member in _members) {
|
||||||
|
var _membersReduced =
|
||||||
|
_members.where((_data) => _data != _member).toList();
|
||||||
|
await updateConversation(context, _member, _membersReduced, _messages);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} on AppwriteException catch (e) {
|
||||||
|
context.error('error message (AppWrite)- ${e.message}');
|
||||||
|
} on Exception catch (e) {
|
||||||
|
context.error('error message (General)- ${e.toString()}');
|
||||||
|
}
|
||||||
return context.res.empty();
|
return context.res.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dynamic updateConversation(
|
||||||
|
var context, var member, var _members, var _messages) async {
|
||||||
|
//
|
||||||
|
Map _message = json.decode(_messages[_messages.length - 1]);
|
||||||
|
//
|
||||||
|
if (_message.isNotEmpty) {
|
||||||
|
context.log('hre');
|
||||||
|
|
||||||
|
for (var _member in _members) {
|
||||||
|
//
|
||||||
|
List<dynamic> _chatIdMap = [];
|
||||||
|
List<dynamic> _detailMap = [];
|
||||||
|
|
||||||
|
var _conversationDetail = await _database.getDocument(
|
||||||
|
databaseId: _databaseId,
|
||||||
|
collectionId: _conversationDbId,
|
||||||
|
documentId: member,
|
||||||
|
);
|
||||||
|
|
||||||
|
_detailMap = _conversationDetail.data['details'];
|
||||||
|
_chatIdMap = _conversationDetail.data['chatId'];
|
||||||
|
|
||||||
|
var _index = _chatIdMap.indexOf(_documentId);
|
||||||
|
Map _detail = json.decode(_detailMap[_index]);
|
||||||
|
|
||||||
|
_detail['unseenCount'] += 1;
|
||||||
|
_detail['lastMessage'] = _message['message'];
|
||||||
|
_detail['timeStamp'] = _message['timeStamp'];
|
||||||
|
|
||||||
|
_detailMap[_index] = json.encode(_detail);
|
||||||
|
_conversationDetail.data['details'] = _detailMap;
|
||||||
|
|
||||||
|
Map<String, dynamic> _conversationMap = {
|
||||||
|
"chatId": _chatIdMap,
|
||||||
|
"details": _detailMap
|
||||||
|
};
|
||||||
|
|
||||||
|
context.log(_detailMap);
|
||||||
|
|
||||||
|
await _database.updateDocument(
|
||||||
|
databaseId: _databaseId,
|
||||||
|
collectionId: _conversationDbId,
|
||||||
|
documentId: member,
|
||||||
|
data: _conversationMap);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user