aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2018-11-24 19:17:21 +0100
committerdec05eba <dec05eba@protonmail.com>2018-11-24 19:17:23 +0100
commitd647b0cc7efbaab31efe8e21066fc4fa0647a65e (patch)
treeade7fcbab090ef80deebb2244154a29cb441841e
parent95be16d3a565afc1c0bb1f3d6c95c223ba7ae221 (diff)
Add change avatar method
m---------depends/odhtdb0
-rw-r--r--include/dchat/Room.hpp13
-rw-r--r--include/dchat/User.hpp1
-rw-r--r--src/Room.cpp52
4 files changed, 66 insertions, 0 deletions
diff --git a/depends/odhtdb b/depends/odhtdb
-Subproject 2021485e9efb7bb43a3a68f54efa78108a630f4
+Subproject 4d7a260b2b0acd329e90753dcc4cc680333a08b
diff --git a/include/dchat/Room.hpp b/include/dchat/Room.hpp
index a39f2f0..9eef4de 100644
--- a/include/dchat/Room.hpp
+++ b/include/dchat/Room.hpp
@@ -39,6 +39,8 @@ namespace dchat
// Returns null if group doesn't exist in room
std::shared_ptr<Group> getGroupById(const odhtdb::DataView groupId);
void setLocalUser(std::shared_ptr<User> user, std::shared_ptr<odhtdb::Signature::KeyPair> keyPair);
+ void setAvatarUrl(const std::string &url);
+ void setNickname(const std::string &nickname);
void publishMessage(const std::string &msg);
Rooms *rooms;
@@ -71,6 +73,15 @@ namespace dchat
std::string newNickname;
};
+ struct UserChangeAvatarRequest
+ {
+ std::shared_ptr<Room> room;
+ std::shared_ptr<User> user;
+ uint32_t timestampSeconds;
+ bool loadedFromCache;
+ std::string url;
+ };
+
struct RoomChangeNameRequest
{
std::shared_ptr<Room> room;
@@ -103,6 +114,7 @@ namespace dchat
using RoomAddUserCallbackFunc = std::function<void(const RoomAddUserRequest &request)>;
using RoomAddMessageCallbackFunc = std::function<void(const RoomAddMessageRequest &request)>;
using UserChangeNicknameCallbackFunc = std::function<void(const UserChangeNicknameRequest &request)>;
+ using UserChangeAvatarCallbackFunc = std::function<void(const UserChangeAvatarRequest &request)>;
using ChangeRoomNameCallbackFunc = std::function<void(const RoomChangeNameRequest &request)>;
using ReceiveInviteUserCallbackFunc = std::function<void(const InviteUserRequest &request)>;
struct RoomCallbackFuncs
@@ -112,6 +124,7 @@ namespace dchat
RoomAddUserCallbackFunc addUserCallbackFunc;
RoomAddMessageCallbackFunc addMessageCallbackFunc;
UserChangeNicknameCallbackFunc userChangeNicknameCallbackFunc;
+ UserChangeAvatarCallbackFunc userChangeAvatarCallbackFunc;
ChangeRoomNameCallbackFunc changeRoomNameCallbackFunc;
ReceiveInviteUserCallbackFunc receiveInviteUserCallbackFunc;
};
diff --git a/include/dchat/User.hpp b/include/dchat/User.hpp
index 245197f..9d22cb3 100644
--- a/include/dchat/User.hpp
+++ b/include/dchat/User.hpp
@@ -14,6 +14,7 @@ namespace dchat
User(const odhtdb::Signature::PublicKey &_publicKey) : publicKey(_publicKey), userdata(nullptr) {}
const odhtdb::Signature::PublicKey publicKey;
std::string nickname;
+ std::string avatarUrl;
std::vector<std::shared_ptr<Group>> groups;
void *userdata;
};
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<u16>();
+ 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)