Moved to local functions
This commit is contained in:
@@ -0,0 +1,46 @@
|
|||||||
|
# chatapp-functions
|
||||||
|
|
||||||
|
## 🧰 Usage
|
||||||
|
|
||||||
|
### GET /ping
|
||||||
|
|
||||||
|
- Returns a "Pong" message.
|
||||||
|
|
||||||
|
**Response**
|
||||||
|
|
||||||
|
Sample `200` Response:
|
||||||
|
|
||||||
|
```text
|
||||||
|
Pong
|
||||||
|
```
|
||||||
|
|
||||||
|
### GET, POST, PUT, PATCH, DELETE /
|
||||||
|
|
||||||
|
- Returns a "Learn More" JSON response.
|
||||||
|
|
||||||
|
**Response**
|
||||||
|
|
||||||
|
Sample `200` Response:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"motto": "Build like a team of hundreds_",
|
||||||
|
"learn": "https://appwrite.io/docs",
|
||||||
|
"connect": "https://appwrite.io/discord",
|
||||||
|
"getInspired": "https://builtwith.appwrite.io"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## ⚙️ Configuration
|
||||||
|
|
||||||
|
| Setting | Value |
|
||||||
|
| ----------------- | --------------- |
|
||||||
|
| Runtime | Dart (2.17) |
|
||||||
|
| Entrypoint | `lib/main.dart` |
|
||||||
|
| Build Commands | `dart pub get` |
|
||||||
|
| Permissions | `any` |
|
||||||
|
| Timeout (Seconds) | 15 |
|
||||||
|
|
||||||
|
## 🔒 Environment Variables
|
||||||
|
|
||||||
|
No environment variables required.
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
include: package:lints/recommended.yaml
|
||||||
@@ -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 '';
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
name: starter_template
|
||||||
|
version: 1.0.0
|
||||||
|
|
||||||
|
environment:
|
||||||
|
sdk: ^3.5.0
|
||||||
|
|
||||||
|
dependencies:
|
||||||
|
dart_appwrite: ^14.0.0
|
||||||
|
|
||||||
|
dev_dependencies:
|
||||||
|
lints: ^2.0.0
|
||||||
@@ -49,7 +49,10 @@ dynamic createConversation(var member, var _members) async {
|
|||||||
List<dynamic> _detailMap = [];
|
List<dynamic> _detailMap = [];
|
||||||
|
|
||||||
var _userDetail = await _database.getDocument(
|
var _userDetail = await _database.getDocument(
|
||||||
databaseId: _databaseId, collectionId: _usersDbId, documentId: _member);
|
databaseId: _databaseId,
|
||||||
|
collectionId: _usersDbId,
|
||||||
|
documentId: _member,
|
||||||
|
);
|
||||||
|
|
||||||
var _imageUrl = _userDetail.data['image'];
|
var _imageUrl = _userDetail.data['image'];
|
||||||
var _name = _userDetail.data['name'];
|
var _name = _userDetail.data['name'];
|
||||||
|
|||||||
@@ -3,85 +3,8 @@
|
|||||||
// {"message":"Cool Bro, you good?", "senderId": "6793b2543197428f812a", "timeStamp": "2025-01-29T16:36: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 'package:dart_appwrite/dart_appwrite.dart';
|
|
||||||
|
|
||||||
late Databases _database;
|
|
||||||
|
|
||||||
var _conversationDbId;
|
|
||||||
var _documentId;
|
|
||||||
var _databaseId;
|
|
||||||
|
|
||||||
//Default call by function
|
//Default call by function
|
||||||
Future<dynamic> main(final context) async {
|
Future<dynamic> main(final context) async {
|
||||||
//
|
return context.res.text('TEST');
|
||||||
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['APPWRITE_COLLECTION_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'];
|
|
||||||
|
|
||||||
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 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 = 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
|
|
||||||
};
|
|
||||||
|
|
||||||
await _database.updateDocument(
|
|
||||||
databaseId: _databaseId,
|
|
||||||
collectionId: _conversationDbId,
|
|
||||||
documentId: _member,
|
|
||||||
data: _conversationMap);
|
|
||||||
}
|
|
||||||
return '';
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,10 +2,10 @@ name: starter_template
|
|||||||
version: 1.0.0
|
version: 1.0.0
|
||||||
|
|
||||||
environment:
|
environment:
|
||||||
sdk: ^2.17.0
|
sdk: ^3.3.0
|
||||||
|
|
||||||
dependencies:
|
dependencies:
|
||||||
dart_appwrite: 8.0.0
|
dart_appwrite: ^8.0.1
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
lints: ^2.0.0
|
lints: ^2.0.0
|
||||||
|
|||||||
Reference in New Issue
Block a user