2017-04-06 02:06:42 +03:00
|
|
|
/*
|
|
|
|
* nheko Copyright (C) 2017 Konstantinos Sideris <siderisk@auth.gr>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2017-12-04 19:41:19 +03:00
|
|
|
#include <QDebug>
|
2017-07-29 11:49:00 +03:00
|
|
|
#include <QJsonArray>
|
2017-12-04 19:41:19 +03:00
|
|
|
#include <QJsonObject>
|
2017-05-26 21:34:16 +03:00
|
|
|
#include <QSettings>
|
|
|
|
|
2017-05-07 17:15:38 +03:00
|
|
|
#include "RoomState.h"
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-10-28 20:46:34 +03:00
|
|
|
RoomState::RoomState() {}
|
2017-12-04 19:41:19 +03:00
|
|
|
RoomState::RoomState(const mtx::responses::Timeline &timeline)
|
|
|
|
{
|
|
|
|
updateFromEvents(timeline.events);
|
|
|
|
}
|
|
|
|
RoomState::RoomState(const mtx::responses::State &state) { updateFromEvents(state.events); }
|
2017-10-28 20:46:34 +03:00
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
|
|
|
RoomState::resolveName()
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
2017-08-26 13:49:16 +03:00
|
|
|
name_ = "Empty Room";
|
|
|
|
userAvatar_.clear();
|
|
|
|
|
2017-12-04 19:41:19 +03:00
|
|
|
if (!name.content.name.empty()) {
|
|
|
|
name_ = QString::fromStdString(name.content.name).simplified();
|
2017-08-26 13:49:16 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-04 19:41:19 +03:00
|
|
|
if (!canonical_alias.content.alias.empty()) {
|
|
|
|
name_ = QString::fromStdString(canonical_alias.content.alias).simplified();
|
2017-08-26 13:49:16 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Doesn't follow the spec guidelines.
|
2017-12-04 19:41:19 +03:00
|
|
|
if (aliases.content.aliases.size() != 0) {
|
|
|
|
name_ = QString::fromStdString(aliases.content.aliases[0]).simplified();
|
2017-08-26 13:49:16 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QSettings settings;
|
|
|
|
auto user_id = settings.value("auth/user_id");
|
|
|
|
|
|
|
|
// TODO: Display names should be sorted alphabetically.
|
|
|
|
for (const auto membership : memberships) {
|
2017-12-04 19:41:19 +03:00
|
|
|
const auto stateKey = QString::fromStdString(membership.second.state_key);
|
|
|
|
|
|
|
|
if (stateKey == user_id)
|
2017-08-26 13:49:16 +03:00
|
|
|
continue;
|
|
|
|
|
2017-12-04 19:41:19 +03:00
|
|
|
if (membership.second.content.membership == mtx::events::state::Membership::Join) {
|
|
|
|
userAvatar_ = stateKey;
|
|
|
|
auto displayName =
|
|
|
|
QString::fromStdString(membership.second.content.display_name);
|
2017-08-26 13:49:16 +03:00
|
|
|
|
2017-12-04 19:41:19 +03:00
|
|
|
if (displayName.isEmpty())
|
|
|
|
name_ = stateKey;
|
2017-08-26 13:49:16 +03:00
|
|
|
else
|
2017-12-04 19:41:19 +03:00
|
|
|
name_ = displayName;
|
2017-08-26 13:49:16 +03:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: pluralization
|
|
|
|
if (memberships.size() > 2)
|
|
|
|
name_ = QString("%1 and %2 others").arg(name_).arg(memberships.size());
|
2017-05-26 21:34:16 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
|
|
|
RoomState::resolveAvatar()
|
2017-05-26 21:34:16 +03:00
|
|
|
{
|
2017-08-26 13:49:16 +03:00
|
|
|
if (userAvatar_.isEmpty()) {
|
2017-12-04 19:41:19 +03:00
|
|
|
avatar_ = QString::fromStdString(avatar.content.url);
|
2017-08-26 13:49:16 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-04 19:41:19 +03:00
|
|
|
if (memberships.count(userAvatar_.toStdString()) != 0) {
|
|
|
|
avatar_ =
|
|
|
|
QString::fromStdString(memberships[userAvatar_.toStdString()].content.avatar_url);
|
2017-08-26 13:49:16 +03:00
|
|
|
} else {
|
|
|
|
qWarning() << "Setting room avatar from unknown user id" << userAvatar_;
|
|
|
|
}
|
2017-05-26 21:34:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Should be used only after initial sync.
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
|
|
|
RoomState::removeLeaveMemberships()
|
2017-05-26 21:34:16 +03:00
|
|
|
{
|
2017-12-04 19:41:19 +03:00
|
|
|
for (auto it = memberships.cbegin(); it != memberships.cend();) {
|
|
|
|
if (it->second.content.membership == mtx::events::state::Membership::Leave) {
|
2017-08-26 13:49:16 +03:00
|
|
|
it = memberships.erase(it);
|
|
|
|
} else {
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
2017-05-26 21:34:16 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
|
|
|
RoomState::update(const RoomState &state)
|
2017-05-26 21:34:16 +03:00
|
|
|
{
|
2017-08-26 13:49:16 +03:00
|
|
|
bool needsNameCalculation = false;
|
|
|
|
bool needsAvatarCalculation = false;
|
|
|
|
|
2017-12-04 19:41:19 +03:00
|
|
|
if (aliases.event_id != state.aliases.event_id)
|
2017-08-26 13:49:16 +03:00
|
|
|
aliases = state.aliases;
|
|
|
|
|
2017-12-04 19:41:19 +03:00
|
|
|
if (avatar.event_id != state.avatar.event_id) {
|
2017-08-26 13:49:16 +03:00
|
|
|
avatar = state.avatar;
|
|
|
|
needsAvatarCalculation = true;
|
|
|
|
}
|
|
|
|
|
2017-12-04 19:41:19 +03:00
|
|
|
if (canonical_alias.event_id != state.canonical_alias.event_id) {
|
2017-08-26 13:49:16 +03:00
|
|
|
canonical_alias = state.canonical_alias;
|
|
|
|
needsNameCalculation = true;
|
|
|
|
}
|
|
|
|
|
2017-12-04 19:41:19 +03:00
|
|
|
if (create.event_id != state.create.event_id)
|
2017-08-26 13:49:16 +03:00
|
|
|
create = state.create;
|
2017-12-04 19:41:19 +03:00
|
|
|
|
|
|
|
if (history_visibility.event_id != state.history_visibility.event_id)
|
2017-08-26 13:49:16 +03:00
|
|
|
history_visibility = state.history_visibility;
|
2017-12-04 19:41:19 +03:00
|
|
|
|
|
|
|
if (join_rules.event_id != state.join_rules.event_id)
|
2017-08-26 13:49:16 +03:00
|
|
|
join_rules = state.join_rules;
|
|
|
|
|
2017-12-04 19:41:19 +03:00
|
|
|
if (name.event_id != state.name.event_id) {
|
2017-08-26 13:49:16 +03:00
|
|
|
name = state.name;
|
|
|
|
needsNameCalculation = true;
|
|
|
|
}
|
|
|
|
|
2017-12-04 19:41:19 +03:00
|
|
|
if (power_levels.event_id != state.power_levels.event_id)
|
2017-08-26 13:49:16 +03:00
|
|
|
power_levels = state.power_levels;
|
2017-12-04 19:41:19 +03:00
|
|
|
|
|
|
|
if (topic.event_id != state.topic.event_id)
|
2017-08-26 13:49:16 +03:00
|
|
|
topic = state.topic;
|
|
|
|
|
2017-12-04 19:41:19 +03:00
|
|
|
for (auto it = state.memberships.cbegin(); it != state.memberships.cend(); ++it) {
|
|
|
|
auto membershipState = it->second.content.membership;
|
2017-08-26 13:49:16 +03:00
|
|
|
|
2017-12-04 19:41:19 +03:00
|
|
|
if (it->first == userAvatar_.toStdString()) {
|
2017-08-26 13:49:16 +03:00
|
|
|
needsNameCalculation = true;
|
|
|
|
needsAvatarCalculation = true;
|
|
|
|
}
|
|
|
|
|
2017-12-04 19:41:19 +03:00
|
|
|
if (membershipState == mtx::events::state::Membership::Leave)
|
|
|
|
this->memberships.erase(this->memberships.find(it->first));
|
2017-08-26 13:49:16 +03:00
|
|
|
else
|
2017-12-04 19:41:19 +03:00
|
|
|
this->memberships.emplace(it->first, it->second);
|
2017-08-26 13:49:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (needsNameCalculation)
|
|
|
|
resolveName();
|
|
|
|
|
|
|
|
if (needsAvatarCalculation)
|
|
|
|
resolveAvatar();
|
2017-05-07 17:15:38 +03:00
|
|
|
}
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-12-04 19:41:19 +03:00
|
|
|
std::string
|
2017-08-20 13:47:22 +03:00
|
|
|
RoomState::serialize() const
|
2017-07-29 11:49:00 +03:00
|
|
|
{
|
2017-12-04 19:41:19 +03:00
|
|
|
nlohmann::json obj;
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-12-04 19:41:19 +03:00
|
|
|
if (!aliases.event_id.empty())
|
|
|
|
obj["aliases"] = aliases;
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-12-04 19:41:19 +03:00
|
|
|
if (!avatar.event_id.empty())
|
|
|
|
obj["avatar"] = avatar;
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-12-04 19:41:19 +03:00
|
|
|
if (!canonical_alias.event_id.empty())
|
|
|
|
obj["canonical_alias"] = canonical_alias;
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-12-04 19:41:19 +03:00
|
|
|
if (!create.event_id.empty())
|
|
|
|
obj["create"] = create;
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-12-04 19:41:19 +03:00
|
|
|
if (!history_visibility.event_id.empty())
|
|
|
|
obj["history_visibility"] = history_visibility;
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-12-04 19:41:19 +03:00
|
|
|
if (!join_rules.event_id.empty())
|
|
|
|
obj["join_rules"] = join_rules;
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-12-04 19:41:19 +03:00
|
|
|
if (!name.event_id.empty())
|
|
|
|
obj["name"] = name;
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-12-04 19:41:19 +03:00
|
|
|
if (!power_levels.event_id.empty())
|
|
|
|
obj["power_levels"] = power_levels;
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-12-04 19:41:19 +03:00
|
|
|
if (!topic.event_id.empty())
|
|
|
|
obj["topic"] = topic;
|
2017-07-29 11:49:00 +03:00
|
|
|
|
2017-12-04 19:41:19 +03:00
|
|
|
return obj.dump();
|
2017-07-29 11:49:00 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
2017-12-04 19:41:19 +03:00
|
|
|
RoomState::parse(const nlohmann::json &object)
|
2017-07-29 11:49:00 +03:00
|
|
|
{
|
2017-12-04 19:41:19 +03:00
|
|
|
if (object.count("aliases") != 0) {
|
2017-08-26 13:49:16 +03:00
|
|
|
try {
|
2017-12-04 19:41:19 +03:00
|
|
|
aliases = object.at("aliases")
|
|
|
|
.get<mtx::events::StateEvent<mtx::events::state::Aliases>>();
|
|
|
|
} catch (std::exception &e) {
|
2017-08-26 13:49:16 +03:00
|
|
|
qWarning() << "RoomState::parse - aliases" << e.what();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-04 19:41:19 +03:00
|
|
|
if (object.count("avatar") != 0) {
|
2017-08-26 13:49:16 +03:00
|
|
|
try {
|
2017-12-04 19:41:19 +03:00
|
|
|
avatar = object.at("avatar")
|
|
|
|
.get<mtx::events::StateEvent<mtx::events::state::Avatar>>();
|
|
|
|
} catch (std::exception &e) {
|
2017-08-26 13:49:16 +03:00
|
|
|
qWarning() << "RoomState::parse - avatar" << e.what();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-04 19:41:19 +03:00
|
|
|
if (object.count("canonical_alias") != 0) {
|
2017-08-26 13:49:16 +03:00
|
|
|
try {
|
2017-12-04 19:41:19 +03:00
|
|
|
canonical_alias =
|
|
|
|
object.at("canonical_alias")
|
|
|
|
.get<mtx::events::StateEvent<mtx::events::state::CanonicalAlias>>();
|
|
|
|
} catch (std::exception &e) {
|
2017-08-26 13:49:16 +03:00
|
|
|
qWarning() << "RoomState::parse - canonical_alias" << e.what();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-04 19:41:19 +03:00
|
|
|
if (object.count("create") != 0) {
|
2017-08-26 13:49:16 +03:00
|
|
|
try {
|
2017-12-04 19:41:19 +03:00
|
|
|
create = object.at("create")
|
|
|
|
.get<mtx::events::StateEvent<mtx::events::state::Create>>();
|
|
|
|
} catch (std::exception &e) {
|
2017-08-26 13:49:16 +03:00
|
|
|
qWarning() << "RoomState::parse - create" << e.what();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-04 19:41:19 +03:00
|
|
|
if (object.count("history_visibility") != 0) {
|
2017-08-26 13:49:16 +03:00
|
|
|
try {
|
2017-12-04 19:41:19 +03:00
|
|
|
history_visibility =
|
|
|
|
object.at("history_visibility")
|
|
|
|
.get<mtx::events::StateEvent<mtx::events::state::HistoryVisibility>>();
|
|
|
|
} catch (std::exception &e) {
|
2017-08-26 13:49:16 +03:00
|
|
|
qWarning() << "RoomState::parse - history_visibility" << e.what();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-04 19:41:19 +03:00
|
|
|
if (object.count("join_rules") != 0) {
|
2017-08-26 13:49:16 +03:00
|
|
|
try {
|
2017-12-04 19:41:19 +03:00
|
|
|
join_rules =
|
|
|
|
object.at("join_rules")
|
|
|
|
.get<mtx::events::StateEvent<mtx::events::state::JoinRules>>();
|
|
|
|
} catch (std::exception &e) {
|
2017-08-26 13:49:16 +03:00
|
|
|
qWarning() << "RoomState::parse - join_rules" << e.what();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-04 19:41:19 +03:00
|
|
|
if (object.count("name") != 0) {
|
2017-08-26 13:49:16 +03:00
|
|
|
try {
|
2017-12-04 19:41:19 +03:00
|
|
|
name = object.at("name")
|
|
|
|
.get<mtx::events::StateEvent<mtx::events::state::Name>>();
|
|
|
|
} catch (std::exception &e) {
|
2017-08-26 13:49:16 +03:00
|
|
|
qWarning() << "RoomState::parse - name" << e.what();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-04 19:41:19 +03:00
|
|
|
if (object.count("power_levels") != 0) {
|
2017-08-26 13:49:16 +03:00
|
|
|
try {
|
2017-12-04 19:41:19 +03:00
|
|
|
power_levels =
|
|
|
|
object.at("power_levels")
|
|
|
|
.get<mtx::events::StateEvent<mtx::events::state::PowerLevels>>();
|
|
|
|
} catch (std::exception &e) {
|
2017-08-26 13:49:16 +03:00
|
|
|
qWarning() << "RoomState::parse - power_levels" << e.what();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-04 19:41:19 +03:00
|
|
|
if (object.count("topic") != 0) {
|
2017-08-26 13:49:16 +03:00
|
|
|
try {
|
2017-12-04 19:41:19 +03:00
|
|
|
topic = object.at("topic")
|
|
|
|
.get<mtx::events::StateEvent<mtx::events::state::Topic>>();
|
|
|
|
} catch (std::exception &e) {
|
2017-08-26 13:49:16 +03:00
|
|
|
qWarning() << "RoomState::parse - topic" << e.what();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|