instagram_task.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #include <include/task/instagram_task.hpp>
  2. #include <QDebug>
  3. using namespace Scheduler;
  4. /**
  5. * These values contained inside the TaskIndex namespace represent the order in which the tasks are to be stored.
  6. */
  7. namespace TaskIndex {
  8. static const uint8_t HEADER = 0;
  9. static const uint8_t DESCRIPTION = 1;
  10. static const uint8_t DATETIME = 2;
  11. static const uint8_t PROMOTE_SHARE = 3;
  12. static const uint8_t LINK_IN_BIO = 4;
  13. static const uint8_t HASHTAGS = 5;
  14. static const uint8_t HASHTAGS_STRING = 6;
  15. static const uint8_t REQUESTED_BY = 7;
  16. static const uint8_t REQUESTED_BY_STRING = 8;
  17. static const uint8_t REQUESTED_BY_PHRASE = 9;
  18. static const uint8_t FILES = 10;
  19. static const uint8_t USER = 11;
  20. static const uint8_t IS_VIDEO = 12;
  21. } // namespace TaskIndex
  22. /**
  23. * @constructor
  24. */
  25. InstagramTask::InstagramTask() {}
  26. /**
  27. * @brief InstagramTask::defineTaskArguments
  28. *
  29. * This method defines all of the arguments that are available to be set for the Task
  30. *
  31. */
  32. void InstagramTask::defineTaskArguments() {
  33. m_arguments.clear();
  34. m_arguments.emplace_back(std::move(new TaskArgument{"header", Type::TEXT, QString{}}));
  35. m_arguments.emplace_back(std::move(new TaskArgument{"description", Type::TEXT, TypeVariant{QString{}}}));
  36. m_arguments.emplace_back(std::move(new TaskArgument{"datetime", Type::TEXT, QString{}}));
  37. m_arguments.emplace_back(std::move(new TaskArgument{"promote_share", Type::TEXT, QString{}}));
  38. m_arguments.emplace_back(std::move(new TaskArgument{"link_in_bio", Type::TEXT, QString{}}));
  39. m_arguments.emplace_back(std::move(new TaskArgument{"hashtags", Type::STRINGVECTOR, QVector<QString>{}}));
  40. m_arguments.emplace_back(std::move(new TaskArgument{"hashtags_string", Type::TEXT, QString{}}));
  41. m_arguments.emplace_back(std::move(new TaskArgument{"requested_by", Type::STRINGVECTOR, QVector<QString>{}}));
  42. m_arguments.emplace_back(std::move(new TaskArgument{"requested_by_string", Type::TEXT, QString{}}));
  43. m_arguments.emplace_back(std::move(new TaskArgument{"requested_by_phrase", Type::TEXT, QString{}}));
  44. m_arguments.emplace_back(std::move(new TaskArgument{"files", Type::FILEVECTOR, QVector<KFileData>{}}));
  45. m_arguments.emplace_back(std::move(new TaskArgument{"user", Type::TEXT, QString{}}));
  46. m_arguments.emplace_back(std::move(new TaskArgument{"is_video", Type::BOOLEAN, bool{}}));
  47. }
  48. /**
  49. * @brief InstagramTask::setArgument
  50. * @param [in] {QString} name The name of the argment
  51. * @param [in] {TypeVariant} value The value of the argument
  52. */
  53. void InstagramTask::setArgument(QString name, TypeVariant value) {
  54. for (auto&& argument : m_arguments) {
  55. if (argument->text() == name) {
  56. argument->setValue(value);
  57. return;
  58. }
  59. }
  60. }
  61. /**
  62. * @warning This method is used to add values to an argument, and can only be used on arguments whose type is a form of container.
  63. *
  64. * @brief InstagramTask::addArgument
  65. *
  66. * @param [in] {QString} name The name of the argument
  67. * @param [in] {KFileData} file A data structure to be added to a container of files.
  68. * The KFileData structure contains metadata about a file and
  69. * its data as a byte array
  70. */
  71. void InstagramTask::addArgument(QString name, Scheduler::KFileData file) {
  72. for (const auto& argument : m_arguments) {
  73. if (argument->text() == name) {
  74. argument->insert(file);
  75. return;
  76. }
  77. }
  78. }
  79. /**
  80. * @warning This method is used to remove a value to an argument, and can only be used on arguments whose type is a form of container.
  81. *
  82. * @brief InstagramTask::addArgument
  83. *
  84. * @param [in] {QString} name The name of the argument
  85. * @param [in] {TypeVariant} file A data structure to be added to a container of files.
  86. * The KFileData structure contains metadata about a file and
  87. * its data as a byte array
  88. */
  89. void InstagramTask::removeArgument(QString name, Scheduler::TypeVariant value) {
  90. for (auto&& argument : m_arguments) {
  91. if (argument->text() == name) {
  92. if (argument->isContainer()) {
  93. argument->remove(value);
  94. } else {
  95. argument->clear();
  96. }
  97. return;
  98. }
  99. }
  100. }
  101. /**
  102. * @warning This method is used to add values to an argument, and can only be used on arguments whose type is a form of container.
  103. *
  104. * @brief InstagramTask::addArgument
  105. *
  106. * @param [in] {QString} name The name of the argument
  107. * @param [in] {QString} string A string value intended to be added to a container of strings
  108. */
  109. void InstagramTask::addArgument(QString name, QString string) {
  110. for (const auto& argument : m_arguments) {
  111. if (argument->text() == name) {
  112. argument->insert(string);
  113. return;
  114. }
  115. }
  116. }
  117. /**
  118. * @brief InstagramTask::getTaskArgument
  119. *
  120. * @param [in] {QString} name The name of the argument to retrieve
  121. * @return [out] {TaskArgument} The argument
  122. */
  123. TaskArgument&& InstagramTask::getTaskArgument(QString name) {
  124. for (const auto& argument : m_arguments) {
  125. if (argument->text() == name) {
  126. return std::move(*argument);
  127. }
  128. }
  129. throw std::invalid_argument("Argument not found");
  130. }
  131. /**
  132. * @brief InstagramTask::getTaskArgumentValue
  133. *
  134. * @param [in] {QString} name The name of the argument to retrieve
  135. * @return [out] {TypeVariant} The value of the argument
  136. */
  137. const TypeVariant InstagramTask::getTaskArgumentValue(QString name) {
  138. for (const auto& argument : m_arguments) {
  139. if (argument->text() == name) {
  140. return argument->getValue();
  141. }
  142. }
  143. return ""; // Perhaps we should throw
  144. }
  145. /**
  146. * @warning This method does not return any task value whose type is a form of container.
  147. *
  148. * @brief InstagramTask::getArgumentValues
  149. * @typedef QVector<QString> is aliased to ArgumentValues
  150. *
  151. * @return [out] {ArgumentValues} A vector of strings for all of the arguments that can be represented as a string.
  152. */
  153. ArgumentValues InstagramTask::getArgumentValues() {
  154. ArgumentValues values{static_cast<int>(m_arguments.size())};
  155. for (auto& argument : m_arguments) {
  156. if (!argument->isContainer()) {
  157. values.push_back(argument->getStringValue());
  158. }
  159. }
  160. return values;
  161. }
  162. /**
  163. * @warning This method does not necessarily need to return all argument names.
  164. * In this use case, we are retrieving names to populate options in our
  165. * ArgType widget, thus we only return names for arguments whose values are
  166. * to be set or modified using the ArgType widget. For this reason, we have
  167. * hardcoded the return to be explicit about which arguments names are available.
  168. *
  169. * @brief InstagramTask::getArgumentNames
  170. *
  171. * @return [out] {QVector<QString>} A vector of argument names as strings.
  172. */
  173. QVector<QString> InstagramTask::getArgumentNames() {
  174. return QVector<QString>{
  175. Scheduler::Args::DESCRIPTION_TYPE,
  176. Scheduler::Args::HASHTAG_TYPE,
  177. Scheduler::Args::REQUESTED_BY_TYPE,
  178. Scheduler::Args::PROMOTE_TYPE,
  179. Scheduler::Args::LINK_BIO_TYPE,
  180. Scheduler::Args::HEADER_TYPE,
  181. Scheduler::Args::REQUESTED_BY_PHRASE
  182. };
  183. }
  184. /**
  185. * @warning This method is used to claim ownership of the task's arguments. Use of this method will effectively REMOVE all arguments from
  186. * the task upon which it is called.
  187. *
  188. * @brief InstagramTask::getTaskArguments
  189. * @typedef std::vector<std::unique_ptr<TaskArgument> is aliased to TaskArguments
  190. *
  191. * @return [out] {std::vector<std::unique_ptr<TaskArgument>} An R-value reference to a vector of unique pointers to the task's arguments.
  192. *
  193. */
  194. const TaskArguments&& InstagramTask::getTaskArguments() { return std::move(m_arguments); }
  195. /**
  196. * @brief InstagramTask::setDefaultValues
  197. *
  198. * Sets default values for the task's arguments
  199. */
  200. void InstagramTask::setDefaultValues() {
  201. setArgument("header", TypeVariant{QString{"Learn to speak like native Korean speakers 🙆‍♀️🇰🇷"}});
  202. setArgument("promote_share", TypeVariant{QString{"Share the post through IG story if you enjoy the phrase 🙋‍♀️"}});
  203. setArgument("link_in_bio", TypeVariant{QString{"Subscribe to my YouTube channel (link 🔗in bio) to learn more about Korean language and culture ❤"}});
  204. setArgument("requested_by_phrase", TypeVariant{QString{"The phrase was requested by "}});
  205. }
  206. /**
  207. * @brief getType
  208. *
  209. * @return [out] {TaskType} The type of task
  210. */
  211. Scheduler::TaskType InstagramTask::getType() { return Scheduler::TaskType::INSTAGRAM; };
  212. /**
  213. * @brief InstagramTask::clear
  214. *
  215. * Clears the value of each task argument
  216. */
  217. void InstagramTask::clear() {
  218. for (const auto& argument : m_arguments) {
  219. argument->clear();
  220. }
  221. }
  222. /**
  223. * @brief InstagramTask::hasFiles
  224. *
  225. * @return [out] {bool} Indicates whether the task has files.
  226. */
  227. bool InstagramTask::hasFiles() {
  228. return !std::get<VariantIndex::FILEVEC>(getTaskArgumentValue("files")).empty();
  229. }
  230. /**
  231. * @brief InstagramTask::hasFiles
  232. *
  233. * @return [out] {QVector<KFileData>} A vector of data structures representing file metadata and the file data as bytes.
  234. */
  235. const QVector<Scheduler::KFileData> InstagramTask::getFiles() {
  236. return std::get<VariantIndex::FILEVEC>(getTaskArgumentValue("files"));
  237. }
  238. /**
  239. * @brief InstagramTask::isReady
  240. *
  241. * @return [out] {bool} A boolean value indicating whether the minimal requirements sufficient to appropriately
  242. * perform the task have been met.
  243. */
  244. bool InstagramTask::isReady() {
  245. auto header_size = std::get<VariantIndex::QSTRING>(getTaskArgumentValue("header")).size();
  246. auto description_size = std::get<VariantIndex::QSTRING>(getTaskArgumentValue("description")).size();
  247. auto datetime_size = std::get<VariantIndex::QSTRING>(getTaskArgumentValue("datetime")).size();
  248. auto promote_share_size = std::get<VariantIndex::QSTRING>(getTaskArgumentValue("promote_share")).size();
  249. auto link_in_bio_size = std::get<VariantIndex::QSTRING>(getTaskArgumentValue("link_in_bio")).size();
  250. auto hashtags_size = std::get<VariantIndex::QSTRING>(getTaskArgumentValue("hashtags_string")).size();
  251. auto requested_by_size = std::get<VariantIndex::QSTRING>(getTaskArgumentValue("requested_by_string")).size();
  252. auto hasFiles = std::get<VariantIndex::FILEVEC>(getTaskArgumentValue("files")).size();
  253. auto user_size = std::get<VariantIndex::QSTRING>(getTaskArgumentValue("user")).size();
  254. return header_size > 0 && description_size > 0 && datetime_size > 0 &&
  255. promote_share_size > 0 && link_in_bio_size > 0 &&
  256. hashtags_size > 0 && requested_by_size > 0 && hasFiles && user_size > 0;
  257. }
  258. /**
  259. * @destructor
  260. */
  261. InstagramTask::~InstagramTask() {
  262. }