summary refs log tree commit diff
path: root/src/mongoStub.cpp
blob: 7e5c4a6cb8d3e9fa90160821aebb111e70ce80f0 (plain) (blame)
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
#include "mongoStub.hpp"

mongoStub::mongoStub() {
    if (this->client) {
        this->db = client["fosscord"];

        if (this->db) {
            this->col = db["events"];

        } else {
            std::cout << "db not found";
            exit(-1);
        }
    } else {
        std::cout << "Client couldn't be initialized";
        exit(-1);
    }
}

// Too slow for my liking
std::vector<mongoStub::mongoMessage> mongoStub::getNewMessages(
    mongocxx::change_stream* colCs) {
    std::vector<mongoStub::mongoMessage> retVec;

    for (auto&& event : *colCs) {
        mongoStub::mongoMessage returnValue;

        std::cout << bsoncxx::to_json(event) << std::endl;

        // Only listen to insert events (to avoid "precondition failed: data"
        // exception)
        if (event["operationType"].get_utf8().value.to_string() != "insert") {
            continue;
        }
        std::string evNameTmp =
            event["fullDocument"]["event"].get_utf8().value.to_string();
        if (evNameTmp == "UDP_CONNECTION") {
            handleUdpRequest();
        } else if (evNameTmp == "VOICE_REQUEST") {
            continue;
        }

        returnValue.eventName = evNameTmp;
        retVec.push_back(returnValue);
    }

    return retVec;
}

void mongoStub::handleUdpRequest() {
    using bsoncxx::builder::basic::kvp;
    using bsoncxx::builder::basic::sub_array;
    using bsoncxx::builder::basic::sub_document;

    auto builder = bsoncxx::builder::basic::document{};

    builder.append(kvp("event", "VSERVER_UDP_RESPONSE"));
    builder.append(kvp("op", "4"));
    builder.append(kvp("d", [](sub_document subdoc) {
			subdoc.append(kvp("mode", "CRYPT_MODE")),
			subdoc.append(kvp("secret_key", [](sub_array subarr) {
            	subarr.append(1, 2, 3, 5);  // HOW DO I GEN A SKEY?
        	}));
		}));
	
	
	bsoncxx::stdx::optional<mongocxx::result::insert_one> r= col.insert_one(builder.view());
	std::cout << "Insert"<< std::endl;
}

void handleVoiceRequest() {}