summaryrefslogtreecommitdiff
path: root/fetchpostsaction.cpp
blob: 426f766f3fdd9194bb9b34322ccd2cc038d73ef6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <QtCore/QDateTime>
#include <QtCore/QDebug>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>

#include "board.h"
#include "xmlrpcinterface.h"
#include "xmlrpcreply.h"
#include "fetchpostsaction.h"

FetchPostsAction::FetchPostsAction(int topicId, int start, int end, Board *board) :
    Action(board), _topicId(topicId), _start(start), _end(end)
{
}

bool FetchPostsAction::isSupersetOf(Action *action) const
{
	FetchPostsAction *other = qobject_cast<FetchPostsAction*>(action);
	if (other) {
		if (other->_topicId == _topicId) {
			if (_start <= other->_start && _end >= other->_end) {
				return true;
			}
		}
	}
	return false;
}

void FetchPostsAction::execute()
{
	_call = _board->service()->asyncCall("get_thread",
	                                     QString::number(_topicId), _start, _end);
	connect(_call, SIGNAL(finished(XmlRpcPendingCall*)), SLOT(handleFinishedCall()));
}

void FetchPostsAction::handleFinishedCall()
{
	XmlRpcReply<QVariantMap> result(_call);
	if (result.isValid()) {
		QVariantMap map = result;
		QVariantList posts = map["posts"].toList();
		QSqlDatabase db = _board->database();
		db.transaction();

		QSqlQuery query(db);
		query.prepare("INSERT OR REPLACE INTO posts (topic_id, post_id, post_title, post_content, post_author_id, post_author_name, can_edit, icon_url, post_time, last_update_time) "
		              "VALUES (:topic_id, :post_id, :post_title, :post_content, :post_author_id, :post_author_name, :can_edit, :icon_url, :post_time, :last_update_time)");

		foreach (const QVariant& post_v, posts) {
			QVariantMap post = post_v.toMap();
			bool ok = false;
			int topic_id = post["topic_id"].toInt(&ok);
			if (!ok) {
				// Not fatal, just assume it's the one we requested
				topic_id = _topicId;
			}
			int post_id = post["post_id"].toInt(&ok);
			if (!ok) {
				qWarning() << "No post_id in" << post;
				continue;
			}

			query.bindValue(":topic_id", topic_id);
			query.bindValue(":post_id", post_id);
			query.bindValue(":post_title", unencodePostText(post["post_title"]));
			query.bindValue(":post_content", unencodePostContent(post["post_content"]));
			query.bindValue(":post_author_id", post["post_author_id"].toInt());
			query.bindValue(":post_author_name", unencodePostText(post["post_author_name"]));
			query.bindValue(":can_edit", post["can_edit"].toBool() ? 1 : 0);
			query.bindValue(":icon_url", post["icon_url"].toString());
			query.bindValue(":post_time", post["post_time"].toDateTime());
			query.bindValue(":last_update_time", QDateTime::currentDateTime());

			if (!query.exec()) {
				qWarning() << "Failed to store topic info for:" << topic_id;
				handleDatabaseError("storing topic info", query);
				continue;
			}
		}

		db.commit();
		if (posts.size() > 0) {
			_board->notifyTopicPostsChanged(_topicId,
			                                _start, _start + posts.size() - 1);
		}
	} else {
		qWarning() << "Could not fetch posts";
		// TODO emit error ...
	}
	emit finished(this);
	_call->deleteLater();
}

QString FetchPostsAction::unencodePostText(const QVariant &v)
{
	QByteArray ba = v.toByteArray();
	return QString::fromUtf8(ba.constData(), ba.length());
}

QString FetchPostsAction::unencodePostContent(const QVariant &v)
{
	return _board->bbcodeToRichText(unencodePostText(v));
}