Skip to content

Commit

Permalink
[WIP] Add Caching for users
Browse files Browse the repository at this point in the history
  • Loading branch information
Chethan2k1 committed Jun 30, 2020
1 parent 2f1d026 commit 362ab16
Show file tree
Hide file tree
Showing 8 changed files with 283 additions and 60 deletions.
18 changes: 9 additions & 9 deletions resources/qml/TimelineView.qml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Page {
property real highlightHue: colors.highlight.hslHue
property real highlightSat: colors.highlight.hslSaturation
property real highlightLight: colors.highlight.hslLightness
property variant userProfile

palette: colors

Expand Down Expand Up @@ -232,6 +233,11 @@ Page {

}

Component{
id: userProfileComponent
UserProfile{}
}

section {
property: "section"
}
Expand Down Expand Up @@ -268,8 +274,6 @@ Page {
}
}

property variant userProfile

Row {
height: userName.height
spacing: 8
Expand All @@ -284,9 +288,7 @@ Page {
MouseArea {
anchors.fill: parent
onClicked: {
if(userProfile) userProfile.destroy()
var component = Qt.createComponent("UserProfile.qml");
userProfile = component.createObject(timelineRoot,{user_data : modelData});
userProfile = userProfileComponent.createObject(timelineRoot,{user_data: modelData,avatarUrl:chat.model.avatarUrl(modelData.userId)});
userProfile.show();
}
cursorShape: Qt.PointingHandCursor
Expand All @@ -304,10 +306,8 @@ Page {
anchors.fill: parent
Layout.alignment: Qt.AlignHCenter
onClicked: {
if(userProfile) userProfile.destroy()
var component = Qt.createComponent("UserProfile.qml")
userProfile = component.createObject(timelineRoot,{user_data : modelData})
userProfile.show()
userProfile = userProfileComponent.createObject(timelineRoot,{user_data: modelData,avatarUrl:chat.model.avatarUrl(modelData.userId)});
userProfile.show();
}
cursorShape: Qt.PointingHandCursor
propagateComposedEvents: true
Expand Down
11 changes: 6 additions & 5 deletions resources/qml/UserProfile.qml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import "./device-verification"

ApplicationWindow{
property var user_data
property var avatarUrl
property var colors: currentActivePalette

id:userProfileDialog
Expand Down Expand Up @@ -52,11 +53,11 @@ ApplicationWindow{

Avatar{
id: userProfileAvatar
url:chat.model.avatarUrl(user_data.userId).replace("mxc://", "image://MxcImage/")
url: avatarUrl.replace("mxc://", "image://MxcImage/")
height: 130
width: 130
displayName: modelData.userName
userid: modelData.userId
displayName: user_data.userName
userid: user_data.userId
Layout.alignment: Qt.AlignHCenter
Layout.margins : {
top: 10
Expand All @@ -68,7 +69,7 @@ ApplicationWindow{
text: user_data.userName
fontSizeMode: Text.HorizontalFit
font.pixelSize: 20
color:TimelineManager.userColor(modelData.userId, colors.window)
color:TimelineManager.userColor(user_data.userId, colors.window)
font.bold: true
Layout.alignment: Qt.AlignHCenter
}
Expand Down Expand Up @@ -207,7 +208,7 @@ ApplicationWindow{

Layout.margins : {
right : 10
bottom : 10
bottom: 5
}

palette {
Expand Down
138 changes: 138 additions & 0 deletions src/Cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2315,6 +2315,115 @@ Cache::statusMessage(const std::string &user_id)
return status_msg;
}

void
to_json(json &j, const UserCache &info)
{
j["user_id"] = info.user_id;
j["is_user_verified"] = info.is_user_verified;
j["cross_verified"] = info.cross_verified;
j["keys"] = info.keys;
}

void
from_json(const json &j, UserCache &info)
{
info.user_id = j.at("user_id");
info.is_user_verified = j.at("is_user_verified");
info.cross_verified = j.at("cross_verified").get<std::vector<std::string>>();
info.keys = j.at("keys").get<mtx::responses::QueryKeys>();
}

UserCache
Cache::getUserCache(const std::string &user_id)
{
lmdb::val verifiedVal;

auto txn = lmdb::txn::begin(env_);
auto db = getUserCacheDb(txn);
auto res = lmdb::dbi_get(txn, db, lmdb::val(user_id), verifiedVal);

UserCache verified_state;
if (res) {
verified_state = json::parse(std::string(verifiedVal.data(), verifiedVal.size()));
}

txn.commit();

return verified_state;
}

//! be careful when using make sure is_user_verified is not changed
int
Cache::setUserCache(const std::string &user_id, const UserCache &body)
{
auto txn = lmdb::txn::begin(env_);
auto db = getUserCacheDb(txn);

auto res = lmdb::dbi_put(txn, db, lmdb::val(user_id), lmdb::val(json(body).dump()));

txn.commit();

return res;
}

int
Cache::deleteUserCache(const std::string &user_id)
{
auto txn = lmdb::txn::begin(env_);
auto db = getUserCacheDb(txn);
auto res = lmdb::dbi_del(txn, db, lmdb::val(user_id), nullptr);

txn.commit();

return res;
}

void
to_json(json &j, const DeviceVerifiedCache &info)
{
j["user_id"] = info.user_id;
j["device_verified"] = info.device_verified;
}

void
from_json(const json &j, DeviceVerifiedCache &info)
{
info.user_id = j.at("user_id");
info.device_verified = j.at("device_verified").get<std::vector<std::string>>();
}

DeviceVerifiedCache
Cache::getVerifiedCache(const std::string &user_id)
{
lmdb::val verifiedVal;

auto txn = lmdb::txn::begin(env_);
auto db = getDeviceVerifiedDb(txn);
auto res = lmdb::dbi_get(txn, db, lmdb::val(user_id), verifiedVal);

DeviceVerifiedCache verified_state;
if (res) {
verified_state = json::parse(std::string(verifiedVal.data(), verifiedVal.size()));
}

txn.commit();

return verified_state;
}

int
Cache::setVerifiedCache(const std::string &user_id, const DeviceVerifiedCache &body)
{
auto txn = lmdb::txn::begin(env_);
auto db = getDeviceVerifiedDb(txn);

auto res = lmdb::dbi_put(txn, db, lmdb::val(user_id), lmdb::val(json(body).dump()));

txn.commit();

return res;
}

void
to_json(json &j, const RoomInfo &info)
{
Expand Down Expand Up @@ -2496,6 +2605,35 @@ statusMessage(const std::string &user_id)
{
return instance_->statusMessage(user_id);
}
UserCache
getUserCache(const std::string &user_id)
{
return instance_->getUserCache(user_id);
}

int
setUserCache(const std::string &user_id, const UserCache &body)
{
return instance_->setUserCache(user_id, body);
}

int
deleteUserCache(const std::string &user_id)
{
return instance_->deleteUserCache(user_id);
}

DeviceVerifiedCache
getVerifiedCache(const std::string &user_id)
{
return instance_->getVerifiedCache(user_id);
}

int
setVerifiedCache(const std::string &user_id, const DeviceVerifiedCache &body)
{
return instance_->setVerifiedCache(user_id, body);
}

//! Load saved data for the display names & avatars.
void
Expand Down
17 changes: 17 additions & 0 deletions src/Cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,23 @@ presenceState(const std::string &user_id);
std::string
statusMessage(const std::string &user_id);

//! user Cache
UserCache
getUserCache(const std::string &user_id);

int
setUserCache(const std::string &user_id, const UserCache &body);

int
deleteUserCache(const std::string &user_id);

//! verified Cache
DeviceVerifiedCache
getVerifiedCache(const std::string &user_id);

int
setVerifiedCache(const std::string &user_id, const DeviceVerifiedCache &body);

//! Load saved data for the display names & avatars.
void
populateMembers();
Expand Down
30 changes: 30 additions & 0 deletions src/CacheCryptoStructs.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,33 @@ struct OlmSessionStorage
std::mutex group_outbound_mtx;
std::mutex group_inbound_mtx;
};

struct UserCache
{
//! user_id of the user
std::string user_id;
//! this stores if the user is verified (with cross-signing)
bool is_user_verified = false;
//! list of verified device_ids with cross-signing
std::vector<std::string> cross_verified;
//! map of public key key_ids and their public_key
mtx::responses::QueryKeys keys;
};

void
to_json(nlohmann::json &j, const UserCache &info);
void
from_json(const nlohmann::json &j, UserCache &info);

struct DeviceVerifiedCache
{
//! user_id of the user
std::string user_id;
//! list of verified device_ids with device-verification
std::vector<std::string> device_verified;
};

void
to_json(nlohmann::json &j, const DeviceVerifiedCache &info);
void
from_json(const nlohmann::json &j, DeviceVerifiedCache &info);
19 changes: 19 additions & 0 deletions src/Cache_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ class Cache : public QObject
mtx::presence::PresenceState presenceState(const std::string &user_id);
std::string statusMessage(const std::string &user_id);

// user cache stores user keys
UserCache getUserCache(const std::string &user_id);
int setUserCache(const std::string &user_id, const UserCache &body);
int deleteUserCache(const std::string &user_id);

// device verified cache
DeviceVerifiedCache getVerifiedCache(const std::string &user_id);
int setVerifiedCache(const std::string &user_id, const DeviceVerifiedCache &body);

static void removeDisplayName(const QString &room_id, const QString &user_id);
static void removeAvatarUrl(const QString &room_id, const QString &user_id);

Expand Down Expand Up @@ -443,6 +452,16 @@ class Cache : public QObject
return lmdb::dbi::open(txn, "presence", MDB_CREATE);
}

lmdb::dbi getUserCacheDb(lmdb::txn &txn)
{
return lmdb::dbi::open(txn, std::string("user_cache").c_str(), MDB_CREATE);
}

lmdb::dbi getDeviceVerifiedDb(lmdb::txn &txn)
{
return lmdb::dbi::open(txn, std::string("verified").c_str(), MDB_CREATE);
}

//! Retrieves or creates the database that stores the open OLM sessions between our device
//! and the given curve25519 key which represents another device.
//!
Expand Down
Loading

0 comments on commit 362ab16

Please sign in to comment.