1 | // SPDX-FileCopyrightText: 2017 Kitsune Ral <Kitsune-Ral@users.sf.net> |
2 | // SPDX-FileCopyrightText: 2019 Karol Kosek <krkkx@protonmail.com> |
3 | // SPDX-License-Identifier: LGPL-2.1-or-later |
4 | |
5 | #include "roommemberevent.h" |
6 | |
7 | #include "../logging_categories_p.h" |
8 | |
9 | namespace Quotient { |
10 | template <> |
11 | struct JsonConverter<Membership> { |
12 | static Membership load(const QJsonValue& jv) |
13 | { |
14 | if (const auto& ms = jv.toString(); !ms.isEmpty()) |
15 | return flagFromJsonString<Membership>(s: ms, flagValues: MembershipStrings); |
16 | |
17 | qCWarning(EVENTS) << "Empty membership state" ; |
18 | return Membership::Invalid; |
19 | } |
20 | }; |
21 | } // namespace Quotient |
22 | |
23 | using namespace Quotient; |
24 | |
25 | MemberEventContent::MemberEventContent(const QJsonObject& json) |
26 | : membership(fromJson<Membership>(json: json["membership" _ls])) |
27 | , isDirect(json["is_direct" _ls].toBool()) |
28 | , displayName(fromJson<Omittable<QString>>(json: json["displayname" _ls])) |
29 | , avatarUrl(fromJson<Omittable<QString>>(json: json["avatar_url" _ls])) |
30 | , reason(json["reason" _ls].toString()) |
31 | { |
32 | if (displayName) |
33 | displayName = sanitized(plainText: *displayName); |
34 | } |
35 | |
36 | QJsonObject MemberEventContent::toJson() const |
37 | { |
38 | QJsonObject o; |
39 | if (membership != Membership::Invalid) |
40 | o.insert(QStringLiteral("membership" ), |
41 | value: flagToJsonString(v: membership, flagValues: MembershipStrings)); |
42 | if (displayName) |
43 | o.insert(QStringLiteral("displayname" ), value: *displayName); |
44 | if (avatarUrl && avatarUrl->isValid()) |
45 | o.insert(QStringLiteral("avatar_url" ), value: avatarUrl->toString()); |
46 | if (!reason.isEmpty()) |
47 | o.insert(QStringLiteral("reason" ), value: reason); |
48 | return o; |
49 | } |
50 | |
51 | bool RoomMemberEvent::changesMembership() const |
52 | { |
53 | return !prevContent() || prevContent()->membership != membership(); |
54 | } |
55 | |
56 | bool RoomMemberEvent::isInvite() const |
57 | { |
58 | return membership() == Membership::Invite && changesMembership(); |
59 | } |
60 | |
61 | bool RoomMemberEvent::isRejectedInvite() const |
62 | { |
63 | return membership() == Membership::Leave && prevContent() |
64 | && prevContent()->membership == Membership::Invite; |
65 | } |
66 | |
67 | bool RoomMemberEvent::isJoin() const |
68 | { |
69 | return membership() == Membership::Join && changesMembership(); |
70 | } |
71 | |
72 | bool RoomMemberEvent::isLeave() const |
73 | { |
74 | return membership() == Membership::Leave && prevContent() |
75 | && prevContent()->membership != membership() |
76 | && prevContent()->membership != Membership::Ban |
77 | && prevContent()->membership != Membership::Invite; |
78 | } |
79 | |
80 | bool RoomMemberEvent::isBan() const |
81 | { |
82 | return membership() == Membership::Ban && changesMembership(); |
83 | } |
84 | |
85 | bool RoomMemberEvent::isUnban() const |
86 | { |
87 | return membership() == Membership::Leave && prevContent() |
88 | && prevContent()->membership == Membership::Ban; |
89 | } |
90 | |
91 | bool RoomMemberEvent::isRename() const |
92 | { |
93 | return prevContent() && prevContent()->displayName |
94 | ? newDisplayName() != *prevContent()->displayName |
95 | : newDisplayName().has_value(); |
96 | } |
97 | |
98 | bool RoomMemberEvent::isAvatarUpdate() const |
99 | { |
100 | return prevContent() && prevContent()->avatarUrl |
101 | ? newAvatarUrl() != *prevContent()->avatarUrl |
102 | : newAvatarUrl().has_value(); |
103 | } |
104 | |