Jelajahi Sumber

US30.2: Notifications (CLI)

Sander, Paul 4 tahun lalu
induk
melakukan
fba1e1d325

+ 1 - 0
cli/include/batchioman.h

@@ -71,6 +71,7 @@ private:
 	std::string printDeleteme(Json::Value root);
 	std::string printKeyfile(Json::Value root);
 	std::string printClosekey(Json::Value root);
+	std::string printNotifications(Json::Value root);
 
 public:
 	/**

+ 3 - 0
cli/include/cmdman.h

@@ -121,6 +121,8 @@ private:
 	CmdRet cmdKeyfile(vector<string> args);
 	const string descClosekey = "stop using the previously selected keyfile";
 	CmdRet cmdClosekey(vector<string> args);
+	const string descNotifications = "request notifications from the server";
+	CmdRet cmdNotifications(vector<string> args);
 	const string descConnect = "connect to server";
 	CmdRet cmdConnect(vector<string> args);
 	const string descExit = "exit the application";
@@ -161,6 +163,7 @@ private:
 	CmdRet handleHead(Json::Value);
 	CmdRet handleDeletefile(Json::Value);
 	CmdRet handleDeleteme(Json::Value);
+	CmdRet handleNotifications(Json::Value);
 };
 
 #endif

+ 1 - 0
cli/include/userioman.h

@@ -54,6 +54,7 @@ private:
 	void printDeleteme(Json::Value root);
 	void printKeyfile(Json::Value root);
 	void printClosekey(Json::Value root);
+	void printNotifications(Json::Value root);
 
 	/**
 	 * Mutex for synchronized message output

+ 12 - 0
cli/src/batchioman.cpp

@@ -413,3 +413,15 @@ std::string BatchIoMan::printClosekey(Json::Value root) {
 	else
 		return "Key closed.";
 }
+
+std::string BatchIoMan::printNotifications(Json::Value root) {
+	std::string ret;
+	if (!root["accept"].asBool()) {
+		return std::string("Failed to get notifications: ") + root["error"].asString();
+	} else {
+		ret = "New notifications:\n";
+		for (Json::Value i : root["messages"])
+			ret += i.asString() + "\n";
+	}
+	return ret;
+}

+ 27 - 0
cli/src/cmdman.cpp

@@ -34,6 +34,7 @@ CmdMan::CmdMan(FileMan &fm, void (*dpf)(string)) : fileman(fm) {
 	execmap["deleteme"] = &CmdMan::cmdDeleteme;
 	execmap["keyfile"] = &CmdMan::cmdKeyfile;
 	execmap["closekey"] = &CmdMan::cmdClosekey;
+	execmap["notifications"] = &CmdMan::cmdNotifications;
 	execmap["connect"] = &CmdMan::cmdConnect;
 	execmap["exit"] = &CmdMan::cmdExit;
 
@@ -51,6 +52,7 @@ CmdMan::CmdMan(FileMan &fm, void (*dpf)(string)) : fileman(fm) {
 	helpmap["deleteme"] = descDeleteme;
 	helpmap["keyfile"] = descKeyfile;
 	helpmap["closekey"] = descClosekey;
+	helpmap["notifications"] = descNotifications;
 	helpmap["connect"] = descConnect;
 	helpmap["exit"] = descExit;
 
@@ -69,6 +71,7 @@ CmdMan::CmdMan(FileMan &fm, void (*dpf)(string)) : fileman(fm) {
 	handlemap["head"] = &CmdMan::handleHead;
 	handlemap["deletefile"] = &CmdMan::handleDeletefile;
 	handlemap["deleteme"] = &CmdMan::handleDeleteme;
+	handlemap["notifications"] = &CmdMan::handleNotifications;
 
 	debugprintfunc = dpf;
 }
@@ -563,6 +566,17 @@ CmdMan::CmdRet CmdMan::cmdVersion(vector<string> args) {
 	return retval;
 }
 
+CmdMan::CmdRet CmdMan::cmdNotifications(vector<string> args) {
+	CmdRet retval;
+	DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
+	Json::Value root;
+	root["command"] = "notifications";
+	retval.type = send;
+	retval.msg = root;
+
+	return retval;
+}
+
 CmdMan::CmdRet CmdMan::handle(Json::Value root) {
 	CmdRet retval;
 	Json::Value output;
@@ -964,3 +978,16 @@ CmdMan::CmdRet CmdMan::handleDeleteme(Json::Value root) {
 	retval.msg = root;
 	return retval;
 }
+
+CmdMan::CmdRet CmdMan::handleNotifications(Json::Value root) {
+	CmdRet retval;
+	DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
+
+	if (root["accept"].asBool()) {
+		retval.type = print;
+	} else {
+		retval.type = error;
+	}
+	retval.msg = root;
+	return retval;
+}

+ 12 - 0
cli/src/userioman.cpp

@@ -30,6 +30,7 @@ UserIoMan::UserIoMan(bool usessl, bool beverbose) : IoMan(usessl) {
 	printmap["deleteme"] = &UserIoMan::printDeleteme;
 	printmap["keyfile"] = &UserIoMan::printKeyfile;
 	printmap["closekey"] = &UserIoMan::printClosekey;
+	printmap["notifications"] = &UserIoMan::printNotifications;
 }
 
 UserIoMan::~UserIoMan() { delete reader; }
@@ -209,3 +210,14 @@ void UserIoMan::printClosekey(Json::Value root) {
 	else
 		std::cout << "Key closed." << std::endl;
 }
+
+void UserIoMan::printNotifications(Json::Value root) {
+	if (!root["accept"].asBool()) {
+		std::cout << "Failed to get notifications: " << root["error"].asString() << std::endl;
+	} else {
+		std::cout << "New notifications:" << std::endl;
+		for (Json::Value i : root["messages"])
+			std::cout << "\033[94m" << i.asString() << std::endl;
+		std::cout << "\033[0m " << std::endl;
+	}
+}

+ 81 - 0
cli/test/cmdman_test.cpp

@@ -2374,6 +2374,87 @@ TEST(testClosekey, Negative) {
 	EXPECT_TRUE(cm.isLoggedIn());
 }
 
+/* =====================================
+ * test for notifications
+ */
+TEST(testNotifications, Positive) {
+	FileManMock fm;
+	CmdManForTest cm(fm, dummyDebugPrint);
+	cm.initLoggedIn();
+
+	std::string cmd;
+	std::vector<std::string> args;
+	Json::Value root, messagesArr;
+	CmdMan::CmdRet retvalCmd;
+	CmdMan::CmdRet retvalHdl;
+	vector<string> msgvec = {"msg1", "msg2", "msg3 foo", "msg4 hello world!"};
+
+	// prepare cmd/args/root
+	cmd = "notifications";
+	args = {};
+	root["command"] = "notifications";
+	for (string s : msgvec)
+		messagesArr.append(s);
+	root["messages"] = messagesArr;
+	root["accept"] = true;
+
+	// stick into cmdman
+	retvalCmd = cm.execute(cmd, args);
+	retvalHdl = cm.handle(root);
+
+	// check things
+	EXPECT_EQ(retvalCmd.type, CmdMan::send);
+	EXPECT_EQ(retvalCmd.msg["command"].asString(), "notifications");
+
+	EXPECT_EQ(retvalHdl.type, CmdMan::print);
+	EXPECT_EQ(retvalHdl.msg["command"].asString(), "notifications");
+	EXPECT_TRUE(retvalHdl.msg["accept"].asBool());
+
+	vector<string> retval_msgs;
+	for (Json::Value i : retvalHdl.msg["messages"])
+		retval_msgs.push_back(i.asString());
+	EXPECT_EQ(retval_msgs, msgvec);
+
+	EXPECT_TRUE(cm.isLoggedIn());
+}
+
+TEST(testNotifications, Negative) {
+	FileManMock fm;
+	CmdManForTest cm(fm, dummyDebugPrint);
+	cm.initLoggedIn();
+
+	std::string cmd;
+	std::vector<std::string> args;
+	Json::Value root, messagesArr;
+	CmdMan::CmdRet retvalCmd;
+	CmdMan::CmdRet retvalHdl;
+
+	// prepare cmd/args/root
+	cmd = "notifications";
+	args = {};
+	root["command"] = "notifications";
+	root["accept"] = false;
+	root["error"] = "some very descriptive string";
+
+	// stick into cmdman
+	retvalCmd = cm.execute(cmd, args);
+	retvalHdl = cm.handle(root);
+
+	// check things
+	EXPECT_EQ(retvalCmd.type, CmdMan::send);
+	EXPECT_EQ(retvalCmd.msg["command"].asString(), "notifications");
+
+	EXPECT_EQ(retvalHdl.type, CmdMan::error);
+	EXPECT_EQ(retvalHdl.msg["command"].asString(), "notifications");
+	EXPECT_FALSE(retvalHdl.msg["accept"].asBool());
+	EXPECT_THAT(retvalHdl.msg["error"].asString(), testing::HasSubstr("some very descriptive string"));
+
+	EXPECT_TRUE(cm.isLoggedIn());
+}
+
+/* =====================================
+ * test for all commands that require a login
+ */
 TEST(testCommandsWithRequiredLogin, NotLoggedInOrNotConnected) {
 	FileManMock fm;
 	CmdManForTest cm(fm, dummyDebugPrint);