Dictionary::Ptr ApiActions::AddComment(const ConfigObject::Ptr& object, const Dictionary::Ptr& params) { Checkable::Ptr checkable = static_pointer_cast<Checkable>(object); if (!checkable) return ApiActions::CreateResult(404, "Cannot add comment for non-existent object"); if (!params->Contains("author") || !params->Contains("comment")) return ApiActions::CreateResult(403, "Comments require author and comment."); String commentName = Comment::AddComment(checkable, CommentUser, HttpUtility::GetLastParameter(params, "author"), HttpUtility::GetLastParameter(params, "comment"), 0); Comment::Ptr comment = Comment::GetByName(commentName); Dictionary::Ptr additional = new Dictionary(); additional->Set("name", commentName); additional->Set("legacy_id", comment->GetLegacyId()); return ApiActions::CreateResult(200, "Successfully added comment '" + commentName + "' for object '" + checkable->GetName() + "'.", additional); }
String Comment::AddComment(const Checkable::Ptr& checkable, CommentType entryType, const String& author, const String& text, double expireTime, const String& id, const MessageOrigin::Ptr& origin) { String fullName; if (id.IsEmpty()) fullName = checkable->GetName() + "!" + Utility::NewUniqueID(); else fullName = id; Dictionary::Ptr attrs = new Dictionary(); attrs->Set("author", author); attrs->Set("text", text); attrs->Set("expire_time", expireTime); attrs->Set("entry_type", entryType); attrs->Set("entry_time", Utility::GetTime()); Host::Ptr host; Service::Ptr service; tie(host, service) = GetHostService(checkable); attrs->Set("host_name", host->GetName()); if (service) attrs->Set("service_name", service->GetShortName()); String zone = checkable->GetZoneName(); if (!zone.IsEmpty()) attrs->Set("zone", zone); String config = ConfigObjectUtility::CreateObjectConfig(Comment::TypeInstance, fullName, true, Array::Ptr(), attrs); Array::Ptr errors = new Array(); if (!ConfigObjectUtility::CreateObject(Comment::TypeInstance, fullName, config, errors)) { ObjectLock olock(errors); for (const String& error : errors) { Log(LogCritical, "Comment", error); } BOOST_THROW_EXCEPTION(std::runtime_error("Could not create comment.")); } Comment::Ptr comment = Comment::GetByName(fullName); if (!comment) BOOST_THROW_EXCEPTION(std::runtime_error("Could not create comment.")); Log(LogNotice, "Comment") << "Added comment '" << comment->GetName() << "'."; return fullName; }
String CommentNameComposer::MakeName(const String& shortName, const Object::Ptr& context) const { Comment::Ptr comment = dynamic_pointer_cast<Comment>(context); if (!comment) return ""; String name = comment->GetHostName(); if (!comment->GetServiceName().IsEmpty()) name += "!" + comment->GetServiceName(); name += "!" + shortName; return name; }
void Comment::RemoveComment(const String& id, const MessageOrigin::Ptr& origin) { Comment::Ptr comment = Comment::GetByName(id); if (!comment) return; Log(LogNotice, "Comment") << "Removed comment '" << comment->GetName() << "' from object '" << comment->GetCheckable()->GetName() << "'."; Array::Ptr errors = new Array(); if (!ConfigObjectUtility::DeleteObject(comment, false, errors)) { ObjectLock olock(errors); for (const String& error : errors) { Log(LogCritical, "Comment", error); } BOOST_THROW_EXCEPTION(std::runtime_error("Could not remove comment.")); } }
String Checkable::AddComment(CommentType entryType, const String& author, const String& text, double expireTime, const String& id, const MessageOrigin& origin) { String uid; if (id.IsEmpty()) uid = Utility::NewUniqueID(); else uid = id; Comment::Ptr comment = new Comment(); comment->SetId(uid);; comment->SetEntryTime(Utility::GetTime()); comment->SetEntryType(entryType); comment->SetAuthor(author); comment->SetText(text); comment->SetExpireTime(expireTime); int legacy_id; { boost::mutex::scoped_lock lock(l_CommentMutex); legacy_id = l_NextCommentID++; } comment->SetLegacyId(legacy_id); GetComments()->Set(uid, comment); { boost::mutex::scoped_lock lock(l_CommentMutex); l_LegacyCommentsCache[legacy_id] = uid; l_CommentsCache[uid] = this; } OnCommentAdded(this, comment, origin); return uid; }
String Service::AddComment(CommentType entryType, const String& author, const String& text, double expireTime, const String& id, const String& authority) { String uid; if (id.IsEmpty()) uid = Utility::NewUniqueID(); else uid = id; Comment::Ptr comment = make_shared<Comment>(); comment->SetId(uid);; comment->SetEntryTime(Utility::GetTime()); comment->SetEntryType(entryType); comment->SetAuthor(author); comment->SetText(text); comment->SetExpireTime(expireTime); int legacy_id; { boost::mutex::scoped_lock lock(l_CommentMutex); legacy_id = l_NextCommentID++; } comment->SetLegacyId(legacy_id); GetComments()->Set(uid, comment); { boost::mutex::scoped_lock lock(l_CommentMutex); l_LegacyCommentsCache[legacy_id] = uid; l_CommentsCache[uid] = GetSelf(); } OnCommentAdded(GetSelf(), comment, authority); return uid; }