Skip to content

Commit

Permalink
jobs: SetRoomStateJob (with or without state key); setting room topic
Browse files Browse the repository at this point in the history
  • Loading branch information
KitsuneRal committed Sep 21, 2017
1 parent 4eeecd2 commit d51e7a4
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 1 deletion.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ set(libqmatrixclient_SRCS
jobs/checkauthmethods.cpp
jobs/passwordlogin.cpp
jobs/sendeventjob.cpp
jobs/setroomstatejob.cpp
jobs/postreceiptjob.cpp
jobs/joinroomjob.cpp
jobs/leaveroomjob.cpp
Expand Down
12 changes: 12 additions & 0 deletions events/roomtopicevent.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,25 @@ namespace QMatrixClient
class RoomTopicEvent: public RoomEvent
{
public:
explicit RoomTopicEvent(const QString& topic)
: RoomEvent(Type::RoomTopic), _topic(topic)
{ }
explicit RoomTopicEvent(const QJsonObject& obj)
: RoomEvent(Type::RoomTopic, obj)
, _topic(contentJson()["topic"].toString())
{ }

QString topic() const { return _topic; }

QJsonObject toJson() const
{
QJsonObject obj;
obj.insert("topic", _topic);
return obj;
}

static constexpr const char* TypeId = "m.room.topic";

private:
QString _topic;
};
Expand Down
32 changes: 32 additions & 0 deletions jobs/setroomstatejob.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/******************************************************************************
* Copyright (C) 2015 Felix Rohrbach <[email protected]>
*
* 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
*/

#include "setroomstatejob.h"

using namespace QMatrixClient;

BaseJob::Status SetRoomStateJob::parseJson(const QJsonDocument& data)
{
_eventId = data.object().value("event_id").toString();
if (!_eventId.isEmpty())
return Success;

qCDebug(JOBS) << data;
return { UserDefinedError, "No event_id in the JSON response" };
}

65 changes: 65 additions & 0 deletions jobs/setroomstatejob.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/******************************************************************************
* Copyright (C) 2015 Felix Rohrbach <[email protected]>
*
* 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
*/

#pragma once

#include "basejob.h"

#include "connectiondata.h"

namespace QMatrixClient
{
class SetRoomStateJob: public BaseJob
{
public:
/**
* Constructs a job that sets a state using an arbitrary room event
* with a state key.
*/
template <typename EvT>
SetRoomStateJob(const ConnectionData* connection, const QString& roomId,
const EvT* event, const QString& stateKey)
: BaseJob(connection, HttpVerb::Put, "SetRoomStateJob",
QStringLiteral("_matrix/client/r0/rooms/%1/state/%2/%3")
.arg(roomId, EvT::TypeId, stateKey),
Query(),
Data(event->toJson()))
{ }
/**
* Constructs a job that sets a state using an arbitrary room event
* without a state key.
*/
template <typename EvT>
SetRoomStateJob(const ConnectionData* connection, const QString& roomId,
const EvT* event)
: BaseJob(connection, HttpVerb::Put, "SetRoomStateJob",
QStringLiteral("_matrix/client/r0/rooms/%1/state/%2")
.arg(roomId, EvT::TypeId),
Query(),
Data(event->toJson()))
{ }

QString eventId() const { return _eventId; }

protected:
Status parseJson(const QJsonDocument& data) override;

private:
QString _eventId;
};
} // namespace QMatrixClient
8 changes: 7 additions & 1 deletion room.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <QtCore/QHash>
#include <QtCore/QStringBuilder> // for efficient string concats (operator%)
#include <QtCore/QElapsedTimer>
#include <jobs/setroomstatejob.h>

#include "connection.h"
#include "state.h"
Expand Down Expand Up @@ -595,6 +596,12 @@ void Room::postMessage(RoomMessageEvent* event)
connection()->callApi<SendEventJob>(id(), event);
}

void Room::setTopic(const QString& newTopic)
{
RoomTopicEvent evt(newTopic);
connection()->callApi<SetRoomStateJob>(id(), &evt);
}

void Room::getPreviousContent(int limit)
{
d->getPreviousContent(limit);
Expand Down Expand Up @@ -1032,4 +1039,3 @@ bool MemberSorter::operator()(User *u1, User *u2) const
n2.remove(0, 1);
return n1.localeAwareCompare(n2) < 0;
}

1 change: 1 addition & 0 deletions room.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ namespace QMatrixClient
void postMessage(RoomMessageEvent* event);
/** @deprecated */
void postMessage(const QString& type, const QString& plainText);
void setTopic(const QString& newTopic);

void getPreviousContent(int limit = 10);

Expand Down

0 comments on commit d51e7a4

Please sign in to comment.