You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.5 KiB

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023 Joas Schilling <coding@schilljs.com>
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Talk\Model;
  25. class Bot {
  26. public const STATE_DISABLED = 0;
  27. public const STATE_ENABLED = 1;
  28. public const STATE_NO_SETUP = 2;
  29. public const FEATURE_NONE = 0;
  30. public const FEATURE_WEBHOOK = 1;
  31. public const FEATURE_RESPONSE = 2;
  32. public const FEATURE_LABEL_NONE = 'none';
  33. public const FEATURE_LABEL_WEBHOOK = 'webhook';
  34. public const FEATURE_LABEL_RESPONSE = 'response';
  35. public const FEATURE_MAP = [
  36. self::FEATURE_NONE => self::FEATURE_LABEL_NONE,
  37. self::FEATURE_WEBHOOK => self::FEATURE_LABEL_WEBHOOK,
  38. self::FEATURE_RESPONSE => self::FEATURE_LABEL_RESPONSE,
  39. ];
  40. public function __construct(
  41. protected BotServer $botServer,
  42. protected BotConversation $botConversation,
  43. ) {
  44. }
  45. public function getBotServer(): BotServer {
  46. return $this->botServer;
  47. }
  48. public function getBotConversation(): BotConversation {
  49. return $this->botConversation;
  50. }
  51. public function isEnabled(): bool {
  52. return $this->botServer->getState() !== self::STATE_DISABLED
  53. && $this->botConversation->getState() !== self::STATE_DISABLED;
  54. }
  55. public static function featureFlagsToLabels(int $flags): string {
  56. if ($flags === self::FEATURE_NONE) {
  57. return self::FEATURE_LABEL_NONE;
  58. }
  59. $features = [];
  60. foreach (self::FEATURE_MAP as $flag => $label) {
  61. if ($flags & $flag) {
  62. $features[] = $label;
  63. }
  64. }
  65. return implode(', ', $features);
  66. }
  67. public static function featureLabelsToFlags(array $labels): int {
  68. $reverseMap = array_flip(self::FEATURE_MAP);
  69. $flags = 0;
  70. foreach ($labels as $label) {
  71. if ($label === self::FEATURE_LABEL_NONE) {
  72. return self::FEATURE_NONE;
  73. }
  74. if (isset($reverseMap[$label])) {
  75. $flags += $reverseMap[$label];
  76. }
  77. }
  78. return $flags;
  79. }
  80. }