diff options
Diffstat (limited to 'lib/events/roommemberevent.cpp')
-rw-r--r-- | lib/events/roommemberevent.cpp | 138 |
1 files changed, 69 insertions, 69 deletions
diff --git a/lib/events/roommemberevent.cpp b/lib/events/roommemberevent.cpp index eaa3302c..4e7eae1b 100644 --- a/lib/events/roommemberevent.cpp +++ b/lib/events/roommemberevent.cpp @@ -1,102 +1,102 @@ -/****************************************************************************** - * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library 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 - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ +// SPDX-FileCopyrightText: 2017 Kitsune Ral <Kitsune-Ral@users.sf.net> +// SPDX-FileCopyrightText: 2019 Karol Kosek <krkkx@protonmail.com> +// SPDX-License-Identifier: LGPL-2.1-or-later #include "roommemberevent.h" - -#include "converters.h" #include "logging.h" -#include <array> - -using namespace QMatrixClient; - -static const std::array<QString, 5> membershipStrings = { { - QStringLiteral("invite"), QStringLiteral("join"), - QStringLiteral("knock"), QStringLiteral("leave"), - QStringLiteral("ban") -} }; - -namespace QMatrixClient -{ - template <> - struct FromJson<MembershipType> +namespace Quotient { +template <> +struct JsonConverter<Membership> { + static Membership load(const QJsonValue& jv) { - MembershipType operator()(const QJsonValue& jv) const - { - const auto& membershipString = jv.toString(); - for (auto it = membershipStrings.begin(); - it != membershipStrings.end(); ++it) - if (membershipString == *it) - return MembershipType(it - membershipStrings.begin()); + if (const auto& ms = jv.toString(); !ms.isEmpty()) + return flagFromJsonString<Membership>(ms, MembershipStrings); - qCWarning(EVENTS) << "Unknown MembershipType: " << membershipString; - return MembershipType::Undefined; - } - }; + qCWarning(EVENTS) << "Empty membership state"; + return Membership::Invalid; + } +}; +} // namespace Quotient -} +using namespace Quotient; MemberEventContent::MemberEventContent(const QJsonObject& json) - : membership(fromJson<MembershipType>(json["membership"_ls])) + : membership(fromJson<Membership>(json["membership"_ls])) , isDirect(json["is_direct"_ls].toBool()) - , displayName(json["displayname"_ls].toString()) - , avatarUrl(json["avatar_url"_ls].toString()) -{ } + , displayName(fromJson<Omittable<QString>>(json["displayname"_ls])) + , avatarUrl(fromJson<Omittable<QString>>(json["avatar_url"_ls])) + , reason(json["reason"_ls].toString()) +{ + if (displayName) + displayName = sanitized(*displayName); +} + +QJsonObject MemberEventContent::toJson() const +{ + QJsonObject o; + if (membership != Membership::Invalid) + o.insert(QStringLiteral("membership"), + flagToJsonString(membership, MembershipStrings)); + if (displayName) + o.insert(QStringLiteral("displayname"), *displayName); + if (avatarUrl && avatarUrl->isValid()) + o.insert(QStringLiteral("avatar_url"), avatarUrl->toString()); + if (!reason.isEmpty()) + o.insert(QStringLiteral("reason"), reason); + return o; +} -void MemberEventContent::fillJson(QJsonObject* o) const +bool RoomMemberEvent::changesMembership() const { - Q_ASSERT(o); - Q_ASSERT_X(membership != MembershipType::Undefined, __FUNCTION__, - "The key 'membership' must be explicit in MemberEventContent"); - if (membership != MembershipType::Undefined) - o->insert(QStringLiteral("membership"), membershipStrings[membership]); - o->insert(QStringLiteral("displayname"), displayName); - if (avatarUrl.isValid()) - o->insert(QStringLiteral("avatar_url"), avatarUrl.toString()); + return !prevContent() || prevContent()->membership != membership(); } bool RoomMemberEvent::isInvite() const { - return membership() == MembershipType::Invite && - (!prevContent() || prevContent()->membership != membership()); + return membership() == Membership::Invite && changesMembership(); +} + +bool RoomMemberEvent::isRejectedInvite() const +{ + return membership() == Membership::Leave && prevContent() + && prevContent()->membership == Membership::Invite; } bool RoomMemberEvent::isJoin() const { - return membership() == MembershipType::Join && - (!prevContent() || prevContent()->membership != membership()); + return membership() == Membership::Join && changesMembership(); } bool RoomMemberEvent::isLeave() const { - return membership() == MembershipType::Leave && - prevContent() && prevContent()->membership != membership() && - prevContent()->membership != MembershipType::Ban; + return membership() == Membership::Leave && prevContent() + && prevContent()->membership != membership() + && prevContent()->membership != Membership::Ban + && prevContent()->membership != Membership::Invite; +} + +bool RoomMemberEvent::isBan() const +{ + return membership() == Membership::Ban && changesMembership(); +} + +bool RoomMemberEvent::isUnban() const +{ + return membership() == Membership::Leave && prevContent() + && prevContent()->membership == Membership::Ban; } bool RoomMemberEvent::isRename() const { - auto prevName = prevContent() ? prevContent()->displayName : QString(); - return displayName() != prevName; + return prevContent() && prevContent()->displayName + ? newDisplayName() != *prevContent()->displayName + : newDisplayName().has_value(); } bool RoomMemberEvent::isAvatarUpdate() const { - auto prevAvatarUrl = prevContent() ? prevContent()->avatarUrl : QUrl(); - return avatarUrl() != prevAvatarUrl; + return prevContent() && prevContent()->avatarUrl + ? newAvatarUrl() != *prevContent()->avatarUrl + : newAvatarUrl().has_value(); } |