From d647b0cc7efbaab31efe8e21066fc4fa0647a65e Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 24 Nov 2018 19:17:21 +0100 Subject: Add change avatar method --- src/Room.cpp | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'src') diff --git a/src/Room.cpp b/src/Room.cpp index 924f2da..a8a8d6a 100644 --- a/src/Room.cpp +++ b/src/Room.cpp @@ -154,6 +154,34 @@ namespace dchat }); } + void Room::setAvatarUrl(const std::string &url) + { + assert(localUser); + assert(publicKeyToKeyPairMap.find(localUser->publicKey) != publicKeyToKeyPairMap.end()); + assert(encryptionKey); + sibs::SafeSerializer serializer; + serializer.add(RoomDataType::CHANGE_AVATAR); + serializer.add((u16)url.size()); + serializer.add((const u8*)url.data(), url.size()); + auto keyPair = publicKeyToKeyPairMap[localUser->publicKey]; + odhtdb::DatabaseNode roomNode(encryptionKey, id); + rooms->database->addData(roomNode, *keyPair, { serializer.getBuffer().data(), serializer.getSize() }); + } + + void Room::setNickname(const std::string &nickname) + { + assert(localUser); + assert(publicKeyToKeyPairMap.find(localUser->publicKey) != publicKeyToKeyPairMap.end()); + assert(encryptionKey); + sibs::SafeSerializer serializer; + serializer.add(RoomDataType::NICKNAME_CHANGE); + serializer.add((u8)nickname.size()); + serializer.add((const u8*)nickname.data(), nickname.size()); + auto keyPair = publicKeyToKeyPairMap[localUser->publicKey]; + odhtdb::DatabaseNode roomNode(encryptionKey, id); + rooms->database->addData(roomNode, *keyPair, { serializer.getBuffer().data(), serializer.getSize() }); + } + void Room::publishMessage(const std::string &msg) { assert(localUser); @@ -291,6 +319,27 @@ namespace dchat } break; } + case RoomDataType::CHANGE_AVATAR: + { + sibs::SafeDeserializer deserializer((const u8*)request.decryptedData.data + 1, request.decryptedData.size - 1); + u16 urlLength = deserializer.extract(); + if(urlLength > 0) + { + std::string url; + url.resize(urlLength); + deserializer.extract((u8*)&url[0], urlLength); + + UserChangeAvatarRequest roomRequest; + roomRequest.room = room; + roomRequest.user = user; + roomRequest.timestampSeconds = timestampSeconds; + roomRequest.loadedFromCache = request.loadedFromCache; + roomRequest.url = std::move(url); + if(callbackFuncs.userChangeAvatarCallbackFunc) + callbackFuncs.userChangeAvatarCallbackFunc(roomRequest); + } + break; + } case RoomDataType::CHANGE_ROOM_NAME: { int userPermissionLevel = database->getUserLowestPermissionLevel(*room->id, user->publicKey); @@ -318,7 +367,10 @@ namespace dchat break; } default: + { + odhtdb::Log::warn("Rooms::addNodeCallbackFunc: Got unexpected room data type %d", roomDataType); break; + } } } catch(std::exception &e) -- cgit v1.2.3