summaryrefslogtreecommitdiffstats
blob: 6e78a8dfedb0e4d229aa5e2b2e790b0d3432c713 (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
/*
 * Copyright (c) 2018 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Swiften/Serializer/PayloadSerializers/ReferencePayloadSerializer.h>

#include <memory>

#include <Swiften/Base/Log.h>
#include <Swiften/Serializer/XML/XMLElement.h>
#include <Swiften/Serializer/XML/XMLRawTextNode.h>
#include <Swiften/Serializer/PayloadSerializerCollection.h>

namespace Swift {

ReferencePayloadSerializer::ReferencePayloadSerializer(PayloadSerializerCollection* payloadSerializers) : GenericPayloadSerializer<ReferencePayload>(), payloadSerializers_(payloadSerializers) {
}

std::string ReferencePayloadSerializer::serializePayload(ReferencePayload::ref reference)  const {
    XMLElement element("reference", "urn:xmpp:reference:0");

    auto type = reference->getType();
    if (type != ReferencePayload::Type::Unknown) {
        element.setAttribute("type", getTypeString(type));

        if (auto uri = reference->getUri()) {
            element.setAttribute("uri", *uri);
        }
        if (auto begin = reference->getBegin()) {
            element.setAttribute("begin", *begin);
        }
        if (auto end = reference->getEnd()) {
            element.setAttribute("end", *end);
        }
        if (auto anchor = reference->getAnchor()) {
            element.setAttribute("anchor", *anchor);
        }

        std::string serializedPayloads;
        for (const auto& payload : reference->getPayloads()) {
            if (auto serializer = payloadSerializers_->getPayloadSerializer(payload)) {
                element.addNode(std::make_shared<XMLRawTextNode>(serializer->serialize(payload)));
            }
            else {
                SWIFT_LOG(warning) << "Could not find serializer for " << typeid(*(payload.get())).name() << std::endl;
            }
        }
    }
    return element.serialize();
}

const std::string ReferencePayloadSerializer::getTypeString(const ReferencePayload::Type type) const {
    switch(type) {
        case ReferencePayload::Type::Data:    return "data";
        case ReferencePayload::Type::Mention: return "mention";
        case ReferencePayload::Type::PubSub:  return "pubsub";
        case ReferencePayload::Type::Unknown: return "unknown";
    }
    return "";
}

}