Create NeochatRoomMember as a shim for RoomMember so it can be safely passed to QML

This commit is contained in:
James Graham
2024-07-21 16:00:25 +01:00
parent 4d9452b862
commit a2a8ad09fd
4 changed files with 384 additions and 1 deletions

204
src/neochatroommember.h Normal file
View File

@@ -0,0 +1,204 @@
// SPDX-FileCopyrightText: 2024 James Graham <james.h.graham@protonmail.com>
// SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
#pragma once
#include <QObject>
#include <QPointer>
#include <qqmlintegration.h>
#include <Quotient/roommember.h>
#include <Quotient/uri.h>
class NeoChatRoom;
//! This class is for visualizing a user in a room context.
//!
//! The class is intentionally a read-only data object that is effectively a wrapper
//! around an m.room.member event for the desired user. This is designed provide the
//! data in a format ready for visualizing a user (avatar or name) in the context
//! of the room it was generated in. This means that if a user has set a unique
//! name or avatar for a particular room that is what will be returned.
//!
//! \note The RoomMember class is not intended for interacting with a User's profile.
//! For that a Quotient::User object should be obtained from a
//! Quotient::Connection as that has the support functions for modifying profile
//! information.
//!
//! \sa Quotient::User
class NeochatRoomMember : public QObject
{
Q_OBJECT
QML_ELEMENT
QML_UNCREATABLE("")
Q_PROPERTY(QString id READ id CONSTANT)
Q_PROPERTY(Quotient::Uri uri READ uri CONSTANT)
Q_PROPERTY(bool isLocalMember READ isLocalMember CONSTANT)
Q_PROPERTY(QString displayName READ displayName NOTIFY displayNameUpdated)
Q_PROPERTY(QString htmlSafeDisplayName READ htmlSafeDisplayName NOTIFY displayNameUpdated)
Q_PROPERTY(QString fullName READ fullName NOTIFY displayNameUpdated)
Q_PROPERTY(QString htmlSafeFullName READ htmlSafeFullName NOTIFY displayNameUpdated)
Q_PROPERTY(QString disambiguatedName READ disambiguatedName NOTIFY displayNameUpdated)
Q_PROPERTY(QString htmlSafeDisambiguatedName READ htmlSafeDisambiguatedName NOTIFY displayNameUpdated)
Q_PROPERTY(int hue READ hue CONSTANT)
Q_PROPERTY(qreal hueF READ hueF CONSTANT)
Q_PROPERTY(QColor color READ color CONSTANT)
Q_PROPERTY(QUrl avatarUrl READ avatarUrl NOTIFY avatarUpdated)
public:
NeochatRoomMember() = default;
explicit NeochatRoomMember(NeoChatRoom *room, const QString &memberId);
bool operator==(const NeochatRoomMember &other) const;
/**
* @brief Get unique stable user id
*
* The Matrix user ID is generated by the server and is never changed.
*/
QString id() const;
/**
* @brief The matrix.to URI for the user
*
* Typically used when you want to visit a user (see
* Quotient::UriResolverBase::visitResource()).
*
* @sa Quotient::UriResolverBase::visitResource()
*/
Quotient::Uri uri() const;
//! Whether this member is the local user
bool isLocalMember() const;
//! The membership state of the member
Quotient::Membership membershipState() const;
//! \brief The raw unmodified display name for the user in the given room
//!
//! The value will be empty if no display name has been set.
//!
//! \warning This value is not sanitized or HTML escape so use appropriately.
//! For ready to display values use displayName() or fullName() for
//! plain text and htmlSafeDisplayName() or htmlSafeFullName() fo
//! rich text.
//!
//! \sa displayName(), htmlSafeDisplayName(), fullName(), htmlSafeFullName()
QString name() const;
//! \brief Get the user display name ready for display
//!
//! This function always aims to return something that can be displayed in a
//! UI, so if no display name is set the user's Matrix ID will be returned.
//!
//! The output is sanitized and suitable for a plain text field. For a rich
//! field use htmlSafeDisplayName().
//!
//! \sa htmlSafeDisplayName()
QString displayName() const;
//! \brief Get the user display name ready for display
//!
//! This function always aims to return something that can be displayed in a
//! UI, so if no display name is set the user's Matrix ID will be returned.
//!
//! The output is sanitized and html escaped ready for a rich text field. For
//! a plain field use displayName().
//!
//! \sa displayName()
QString htmlSafeDisplayName() const;
//! \brief Get user name and id in a single string
//!
//! This function always aims to return something that can be displayed in a
//! UI, so if no display name is set the just user's Matrix ID will be returned.
//! The constructed string follows the format 'name (id)' which the spec
//! recommends for users disambiguation in a room context and in other places.
//!
//! The output is sanitized and suitable for a plain text field. For a rich
//! field use htmlSafeFullName().
//!
//! \sa htmlSafeFullName()
QString fullName() const;
//! \brief Get user name and id in a single string
//!
//! This function always aims to return something that can be displayed in a
//! UI, so if no display name is set the just user's Matrix ID will be returned.
//! The constructed string follows the format 'name (id)' which the spec
//! recommends for users disambiguation in a room context and in other places.
//!
//! The output is sanitized and html escaped ready for a rich text field. For
//! a plain field use fullName().
//!
//! \sa fullName()
QString htmlSafeFullName() const;
//! \brief Get the disambiguated user name
//!
//! This function always aims to return something that can be displayed in a
//! UI, so if no display name is set the just user's Matrix ID will be returned.
//! The output is equivalent to fullName() if there is another user in the room
//! with the same name. Otherwise it is equivalent to displayName().
//!
//! The output is sanitized and suitable for a plain text field. For a rich
//! field use htmlSafeDisambiguatedName().
//!
//! \sa htmlSafeDisambiguatedName(), fullName(), displayName()
QString disambiguatedName() const;
//! \brief Get the disambiguated user name
//!
//! This function always aims to return something that can be displayed in a
//! UI, so if no display name is set the just user's Matrix ID will be returned.
//! The output is equivalent to htmlSafeFullName() if there is another user in the room
//! with the same name. Otherwise it is equivalent to htmlSafeDisplayName().
//!
//! The output is sanitized and html escaped ready for a rich text field. For
//! a plain field use disambiguatedName().
//!
//! \sa disambiguatedName(), htmlSafeFullName(), htmlSafeDisplayName()
QString htmlSafeDisambiguatedName() const;
//! \brief Hue color component of this user based on the user's Matrix ID
//!
//! The implementation is based on XEP-0392:
//! https://xmpp.org/extensions/xep-0392.html
//! Naming and ranges are the same as QColor's hue methods:
//! https://doc.qt.io/qt-5/qcolor.html#integer-vs-floating-point-precision
int hue() const;
//! \brief HueF color component of this user based on the user's Matrix ID
//!
//! The implementation is based on XEP-0392:
//! https://xmpp.org/extensions/xep-0392.html
//! Naming and ranges are the same as QColor's hue methods:
//! https://doc.qt.io/qt-5/qcolor.html#integer-vs-floating-point-precision
qreal hueF() const;
//! \brief Color based on the user's Matrix ID
//!
//! See https://github.com/quotient-im/libQuotient/wiki/User-color-coding-standard-draft-proposal
//! for the methodology.
QColor color() const;
//! \brief The mxc URL as a string for the user avatar in the room
//!
//! This can be empty if none set.
QString avatarMediaId() const;
//! \brief The mxc URL for the user avatar in the room
//!
//! This can be empty if none set.
QUrl avatarUrl() const;
Q_SIGNALS:
void displayNameUpdated();
void avatarUpdated();
private:
QPointer<NeoChatRoom> m_room;
const QString m_memberId;
};