aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2018-11-03 01:09:27 +0100
committerdec05eba <dec05eba@protonmail.com>2018-11-03 01:09:28 +0100
commitfe99723d0a6374ee20c52b0f96c45452026da519 (patch)
tree132c0cc73962fbaeb1906f589dd378f788273df6 /src
parent88916c50b83bfad1ec1c5dd5f4e4f5d345851cb0 (diff)
Add room change event, create room func
Diffstat (limited to 'src')
-rw-r--r--src/Room.cpp68
1 files changed, 63 insertions, 5 deletions
diff --git a/src/Room.cpp b/src/Room.cpp
index cd6476d..30375a3 100644
--- a/src/Room.cpp
+++ b/src/Room.cpp
@@ -9,8 +9,8 @@
// TODO: Remove error checks when odhtdb has been improved to take care of such errors
namespace dchat
{
- Room::Room(std::shared_ptr<odhtdb::Database> _database, std::shared_ptr<odhtdb::Hash> _id, std::shared_ptr<odhtdb::OwnedByteArray> _encryptionKey) :
- database(_database),
+ Room::Room(Rooms *_rooms, std::shared_ptr<odhtdb::Hash> _id, std::shared_ptr<odhtdb::OwnedByteArray> _encryptionKey) :
+ rooms(_rooms),
id(_id),
encryptionKey(_encryptionKey),
userdata(nullptr)
@@ -48,7 +48,7 @@ namespace dchat
serializer.add((const u8*)msg.data(), msg.size());
auto &keyPair = publicKeyToKeyPairMap[localUser->publicKey];
odhtdb::DatabaseNode roomNode(encryptionKey, id);
- database->addData(roomNode, *keyPair, { serializer.getBuffer().data(), serializer.getSize() });
+ rooms->database->addData(roomNode, *keyPair, { serializer.getBuffer().data(), serializer.getSize() });
}
Rooms::Rooms(const char *address, u16 port, RoomCallbackFuncs _callbackFuncs) :
@@ -74,12 +74,18 @@ namespace dchat
auto encryptionKey = roomEncryptionKey[*request.nodeHash];
auto roomId = std::make_shared<odhtdb::Hash>(*request.nodeHash);
- auto room = std::make_shared<Room>(database, roomId, encryptionKey);
+ auto room = std::make_shared<Room>(this, roomId, encryptionKey);
roomById[*request.nodeHash] = room;
if(callbackFuncs.createRoomCallbackFunc)
callbackFuncs.createRoomCallbackFunc(room);
auto user = room->addUser(*request.creatorPublicKey, request.groupId);
+ auto localUserIt = roomLocalUser.find(*request.nodeHash);
+ if(localUserIt != roomLocalUser.end())
+ {
+ room->localUser = user;
+ room->publicKeyToKeyPairMap[user->publicKey] = localUserIt->second;
+ }
if(callbackFuncs.addUserCallbackFunc)
callbackFuncs.addUserCallbackFunc(room, user);
}
@@ -153,6 +159,32 @@ namespace dchat
}
break;
}
+ case RoomDataType::CHANGE_ROOM_NAME:
+ {
+ int userPermissionLevel = database->getUserLowestPermissionLevel(*room->id, user->publicKey);
+ if(userPermissionLevel != odhtdb::PERMISSION_LEVEL_ADMIN)
+ {
+ odhtdb::Log::debug("Room change name: attempted by user %s who is not an admin (permission level: %d)\n", user->publicKey.toString().c_str(), userPermissionLevel);
+ return;
+ }
+
+ sibs::SafeDeserializer deserializer((const u8*)request.decryptedData.data + 1, request.decryptedData.size - 1);
+ u16 channelNameLength = deserializer.extract<u16>();
+ if(channelNameLength > 0 && channelNameLength <= 32)
+ {
+ RoomChangeNameRequest roomRequest;
+ roomRequest.room = room;
+ roomRequest.user = user;
+ roomRequest.timestampSeconds = timestampSeconds;
+ roomRequest.loadedFromCache = request.loadedFromCache;
+ roomRequest.newName.resize(channelNameLength);
+ deserializer.extract((u8*)&roomRequest.newName[0], channelNameLength);
+ if(callbackFuncs.changeRoomNameCallbackFunc)
+ callbackFuncs.changeRoomNameCallbackFunc(roomRequest);
+ room->name = std::move(roomRequest.newName);
+ }
+ break;
+ }
default:
break;
}
@@ -207,10 +239,16 @@ namespace dchat
auto storedNodes = database->getStoredNodeUserInfoDecrypted(username, password);
loggedIn = true;
+ currentUsername = username;
+ currentUserPassword = password;
for(auto &nodeInfo : storedNodes)
{
roomLocalUser[nodeInfo.first] = nodeInfo.second.userKeyPair;
roomEncryptionKey[nodeInfo.first] = nodeInfo.second.nodeEncryptionKey;
+
+ auto roomId = std::make_shared<odhtdb::Hash>(nodeInfo.first);
+ odhtdb::DatabaseNode roomNode(nodeInfo.second.nodeEncryptionKey, roomId);
+ database->seed(roomNode);
database->loadNode(nodeInfo.first);
}
}
@@ -222,5 +260,25 @@ namespace dchat
database->storeUserWithoutNodes(username, password);
loggedIn = true;
+ currentUsername = username;
+ currentUserPassword = password;
+ }
+
+ void Rooms::createRoom(const std::string &name)
+ {
+ if(!loggedIn)
+ throw std::runtime_error("You need to be logged in to create a room ");
+
+ auto newNode = database->create();
+ roomLocalUser[*newNode->getRequestHash()] = newNode->getNodeAdminKeyPair();
+ roomEncryptionKey[*newNode->getRequestHash()] = newNode->getNodeEncryptionKey();
+
+ odhtdb::DatabaseNode nodeInfo(newNode->getNodeEncryptionKey(), newNode->getRequestHash());
+ database->storeNodeInfoForUserEncrypted(nodeInfo, currentUsername, currentUserPassword, *newNode->getNodeAdminKeyPair());
+ sibs::SafeSerializer serializer;
+ serializer.add(RoomDataType::CHANGE_ROOM_NAME);
+ serializer.add((u16)name.size());
+ serializer.add(name.data(), name.size());
+ database->addData(nodeInfo, *newNode->getNodeAdminKeyPair(), { serializer.getBuffer().data(), serializer.getSize() });
}
-} \ No newline at end of file
+}