Moved to local functions
This commit is contained in:
@@ -0,0 +1,214 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'package:dart_appwrite/dart_appwrite.dart';
|
||||
|
||||
late Databases _database;
|
||||
|
||||
var _usersDbId;
|
||||
var _conversationDbId;
|
||||
var _documentId;
|
||||
var _databaseId;
|
||||
|
||||
var _conversationLinkDbId;
|
||||
late DateTime createdAt;
|
||||
late DateTime updatedAt;
|
||||
|
||||
//Default call by function
|
||||
Future<dynamic> main(final _context) async {
|
||||
try {
|
||||
final client = Client()
|
||||
.setEndpoint(
|
||||
Platform.environment['APPWRITE_FUNCTION_API_ENDPOINT'] ?? '')
|
||||
.setProject(Platform.environment['APPWRITE_FUNCTION_PROJECT_ID'] ?? '')
|
||||
.setKey(_context.req.headers['x-appwrite-key'] ?? '');
|
||||
|
||||
_database = Databases(client);
|
||||
_conversationDbId = Platform.environment['CONVERSATION_COL_ID'];
|
||||
_usersDbId = Platform.environment['USERS_COL_ID'];
|
||||
_conversationLinkDbId = Platform.environment['CONVERSATION_LINK_COL_ID'];
|
||||
|
||||
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'];
|
||||
|
||||
createdAt = DateTime.parse(_context.req.bodyJson['\$createdAt']);
|
||||
updatedAt = DateTime.parse(_context.req.bodyJson['\$updatedAt']);
|
||||
|
||||
_context.log(_messages);
|
||||
|
||||
if (createdAt.isAtSameMomentAs(updatedAt)) {
|
||||
//
|
||||
for (var _member in _members) {
|
||||
var _linkId = await createConversation(
|
||||
_context,
|
||||
_member,
|
||||
_members.where((_data) => _data != _member).toList(),
|
||||
);
|
||||
|
||||
if (_linkId != null) {
|
||||
await updateConversationLink(_context, _linkId, _documentId);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
//
|
||||
for (String _member in _members) {
|
||||
await updateConversation(_context, _member, _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();
|
||||
}
|
||||
|
||||
dynamic createConversation(final _context, String _member, var _members) async {
|
||||
//
|
||||
|
||||
List<dynamic> _chatIdMap = [];
|
||||
List<dynamic> _detailMap = [];
|
||||
|
||||
var _userDetail = await _database.getDocument(
|
||||
databaseId: _databaseId,
|
||||
collectionId: _usersDbId,
|
||||
documentId: _members[0],
|
||||
);
|
||||
|
||||
var _imageUrl = _userDetail.data['image'];
|
||||
var _name = _userDetail.data['name'];
|
||||
|
||||
var _conversationDetail;
|
||||
try {
|
||||
_conversationDetail = await _database.getDocument(
|
||||
databaseId: _databaseId,
|
||||
collectionId: _conversationDbId,
|
||||
documentId: _member);
|
||||
} on AppwriteException catch (e) {
|
||||
e.message;
|
||||
} on Exception catch (e) {
|
||||
e.toString();
|
||||
}
|
||||
|
||||
if (_conversationDetail != null) {
|
||||
_chatIdMap = _conversationDetail.data['chatId'];
|
||||
_detailMap = _conversationDetail.data['details'];
|
||||
|
||||
await _database.deleteDocument(
|
||||
databaseId: _databaseId,
|
||||
collectionId: _conversationDbId,
|
||||
documentId: _member,
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
var _data = '{"name": "$_name","image": "$_imageUrl","unseenCount": 0}';
|
||||
|
||||
_chatIdMap.add(_documentId);
|
||||
_detailMap.add(_data);
|
||||
|
||||
Map<String, dynamic> _conversationMap = {
|
||||
"chatId": _chatIdMap,
|
||||
"details": _detailMap
|
||||
};
|
||||
|
||||
await _database.createDocument(
|
||||
databaseId: _databaseId,
|
||||
collectionId: _conversationDbId,
|
||||
documentId: _member,
|
||||
data: _conversationMap,
|
||||
);
|
||||
return '$_member${_members[0]}';
|
||||
} on AppwriteException catch (e) {
|
||||
_context.log('amessage - $e.message');
|
||||
return '';
|
||||
} on Exception catch (e) {
|
||||
_context.log('emessage - $e');
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
dynamic updateConversation(var _context, var _member, var _messages) async {
|
||||
//
|
||||
Map _message = json.decode(_messages[_messages.length - 1]);
|
||||
if (_message.isNotEmpty) {
|
||||
List<dynamic> _chatIdMap = [];
|
||||
List<dynamic> _detailMap = [];
|
||||
|
||||
var _conversationDetail;
|
||||
try {
|
||||
_conversationDetail = await _database.getDocument(
|
||||
databaseId: _databaseId,
|
||||
collectionId: _conversationDbId,
|
||||
documentId: _member,
|
||||
);
|
||||
} on AppwriteException catch (e) {
|
||||
e.message;
|
||||
} on Exception catch (e) {
|
||||
e.toString();
|
||||
}
|
||||
|
||||
if (_conversationDetail != null) {
|
||||
_chatIdMap = _conversationDetail.data['chatId'];
|
||||
_detailMap = _conversationDetail.data['details'];
|
||||
|
||||
var _index = _chatIdMap.indexOf(_documentId);
|
||||
Map _detail = json.decode(_detailMap[_index]);
|
||||
|
||||
_detail['unseenCount'] += 1;
|
||||
_detail['lastMessage'] = _message['message'];
|
||||
_detail['timeStamp'] = _message['timeStamp'];
|
||||
_detail['type'] = _message['type'];
|
||||
|
||||
_detailMap[_index] = json.encode(_detail);
|
||||
_conversationDetail.data['details'] = _detailMap;
|
||||
|
||||
Map<String, dynamic> _conversationMap = {
|
||||
"chatId": _chatIdMap,
|
||||
"details": _detailMap
|
||||
};
|
||||
|
||||
await _database.updateDocument(
|
||||
databaseId: _databaseId,
|
||||
collectionId: _conversationDbId,
|
||||
documentId: _member,
|
||||
data: _conversationMap,
|
||||
);
|
||||
}
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
dynamic updateConversationLink(
|
||||
var _context, var _linkId, var _documentId) async {
|
||||
//
|
||||
dynamic _conversationLink;
|
||||
|
||||
try {
|
||||
_conversationLink = await _database.getDocument(
|
||||
databaseId: _databaseId,
|
||||
collectionId: _conversationLinkDbId,
|
||||
documentId: _linkId);
|
||||
} on AppwriteException catch (e) {
|
||||
e.message;
|
||||
} on Exception catch (e) {
|
||||
e.toString();
|
||||
}
|
||||
|
||||
if (_conversationLink == null) {
|
||||
await _database.createDocument(
|
||||
databaseId: _databaseId,
|
||||
collectionId: _conversationLinkDbId,
|
||||
documentId: _linkId.substring(0, 36),
|
||||
data: {"chatId": _documentId},
|
||||
);
|
||||
}
|
||||
return '';
|
||||
}
|
||||
Reference in New Issue
Block a user