locality_social_cloud_chat 1.0.0 locality_social_cloud_chat: ^1.0.0 copied to clipboard
A chat based on Locality Social Cloud that supported End-to-End encrypted Chats and GroupChats and Chat Message state management.
import 'dart:math';
import 'package:locality_social_cloud/api/discover_users.dart';
import 'package:locality_social_cloud/api/locality_auth.dart';
import 'package:locality_social_cloud/api/locality_social_cloud.dart';
import 'package:locality_social_cloud/api/locality_user.dart';
import 'package:locality_social_cloud/api/logged_in_user.dart';
import 'package:locality_social_cloud_chat/locality_social_cloud_chat.dart';
Future<LoggedInUser> getLoggedInUser() async {
LocalitySocialCloud.configure(appId: "....", appSecret: "........");
LoggedInUserOrError loggedInUserOrError =
await LocalitySocialCloud.auth("userName", "password");
if (loggedInUserOrError.isError()) {
throw Exception(
"Could not log in to the Locality Social Cloud. Please verify that appId and appSecret are correct.");
}
LoggedInUser loggedInUser = loggedInUserOrError.getUser();
LocalitySocialCloud.connect(loggedInUser);
return loggedInUser;
}
const bool testDiscoverUsers = false;
void main() async {
LoggedInUser loggedInUser = await getLoggedInUser();
if (testDiscoverUsers) {
DiscoverUsers discoverUsers = LocalitySocialCloud.discoverUsers();
discoverUsers.addListener(() {
for (int i = 0; i < discoverUsers.discoveredUsers.length; i++) {
print("Discovered user with id ${discoverUsers.discoveredUsers[i].id}");
print("And public key ${discoverUsers.discoveredUsers[i].publicKey}");
}
});
discoverUsers.startingWith("Malti");
// If we don't end this, the program will terminate before the results are known / received
await Future.delayed(const Duration(milliseconds: 1000));
}
DirectMessagesChat chat = await DirectMessagesFacade.getTargetUserChat(
loggedInUser,
LocalityUser("testuser2",
"6a090d80590af1852ff0540a2a370fa209e2662e9d7ccaeb9b704d014cc92b8e69a4cf0695284983040c22d511e46f68f9e8e0c19d3ccd19203d028cf6bdf264/6dfde80699b9e231d90a1a81cc4c5fa60f93de3f7d216a9616a7c8c9dfc686beeb23884cefb2b9f0b45cd264f0f0aa82bd518081056e73dd634e132084d5218d"));
chat.sendMessage({
'content': "Hello World sdf dsf !!",
});
chat.sendMessage({
'content': "Hello World! dsfsd fdsf !",
});
chat.sendMessage({
'content': "Hello World!!",
});
/// It is a throttled provider, so it will call maximum of 120 times per second notifyListeners, to transfer state changes to the render engine in batches.
chat.addListener(() {
print("LOOK! IT IS THROTTLED!");
});
GroupChats.getInstance().addListener(() {
List<GroupChat> groupChats = GroupChats.getInstance().groupChats;
for (GroupChat groupChat in groupChats) {
print("We are connected to the groupchat ... " + groupChat.title);
}
});
/// This will be called once the local timeline is up-to-date with the global timeline.
chat.timeline.whenSynchronized(() {
for (var chatMessage in chat.chatMessages) {
String messageUuid = chatMessage.payload['message_uuid'];
MessageObserver chatMessageObserver =
chat.messageObserversByChatMessageUUID[messageUuid]!;
chatMessageObserver.addListener(() {
print(
"MESSAGE ${messageUuid.substring(0, 12)}... received a like. Likecounter: ${chatMessageObserver.reactions.length}");
});
print("CHAT MESSAGE ${chatMessage.payload}");
if (Random().nextInt(25) > 12) {
chat.reactToMessage(messageUuid, 'like');
}
}
});
// We never know when we can stop the program. In praxis, the UI runs forever.
await Future.delayed(const Duration(milliseconds: 10000));
}