HeartbeatEventSubscriber.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. namespace Drupal\heartbeat\EventSubscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\EventDispatcher\Event;
  5. use Drupal\Core\Database\Database;
  6. use Drupal\flag\FlagService;
  7. use Drupal\flag\Entity\Flagging;
  8. use Drupal\user\Entity\User;
  9. use Drupal\heartbeat\Entity\Heartbeat;
  10. use Drupal\heartbeat\HeartbeatTypeService;
  11. use Drupal\heartbeat\HeartbeatStreamServices;
  12. use Drupal\heartbeat\HeartbeatService;
  13. const NOT_FRIEND = -1;
  14. const PENDING = 0;
  15. const FRIEND = 1;
  16. /**
  17. * Class HeartbeatEventSubscriber.
  18. *
  19. * @package Drupal\heartbeat
  20. */
  21. class HeartbeatEventSubscriber implements EventSubscriberInterface {
  22. /**
  23. * Drupal\flag\FlagService definition.
  24. *
  25. * @var \Drupal\flag\FlagService
  26. */
  27. protected $flagService;
  28. /**
  29. * Drupal\heartbeat\HeartbeatTypeService definition.
  30. *
  31. * @var \Drupal\heartbeat\HeartbeatTypeService
  32. */
  33. protected $heartbeatTypeService;
  34. /**
  35. * Drupal\heartbeat\HeartbeatStreamServices definition.
  36. *
  37. * @var \Drupal\heartbeat\HeartbeatStreamServices
  38. */
  39. protected $heartbeatStreamService;
  40. /**
  41. * Drupal\heartbeat\HeartbeatService definition.
  42. *
  43. * @var \Drupal\heartbeat\HeartbeatService
  44. */
  45. protected $heartbeatService;
  46. /**
  47. * Constructor.
  48. */
  49. public function __construct(FlagService $flag, HeartbeatTypeService $heartbeat_heartbeattype, HeartbeatStreamServices $heartbeatstream, HeartbeatService $heartbeat) {
  50. $this->flagService = $flag;
  51. $this->heartbeatTypeService = $heartbeat_heartbeattype;
  52. $this->heartbeatStreamService = $heartbeatstream;
  53. $this->heartbeatService = $heartbeat;
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public static function getSubscribedEvents() {
  59. $events['flag.entity_flagged'] = ['flag_entity_flagged'];
  60. $events['flag.entity_unflagged'] = ['flag_entity_unflagged'];
  61. return $events;
  62. }
  63. /**
  64. * This method is called whenever the flag.entity_flagged event is
  65. * dispatched.
  66. *
  67. * @param GetResponseEvent $event
  68. * @throws \Drupal\Core\Entity\EntityStorageException
  69. */
  70. public function flag_entity_flagged(Event $event) {
  71. $friendStatus = NOT_FRIEND;
  72. $flagging = $event->getFlagging();
  73. if ($flagging instanceof Flagging) {
  74. $flagId = $flagging->getFlagId();
  75. if ($flagId === 'friendship') {
  76. $entity = $this->flagService->getFlagById($flagging->getFlagId());
  77. $user = $flagging->getOwner();
  78. if ($entity->id() && $user->isAuthenticated()) {
  79. $heartbeatTypeService = \Drupal::service('heartbeat.heartbeattype');
  80. $tokenService = \Drupal::service('token');
  81. foreach ($heartbeatTypeService->getTypes() as $type) {
  82. $heartbeatTypeEntity = $heartbeatTypeService->load($type);
  83. if ($heartbeatTypeEntity->getMainEntity() === "flagging") {
  84. $arguments = json_decode($heartbeatTypeEntity->getArguments());
  85. $user2 = User::load($flagging->getFlaggableId());
  86. $targetUserFriendships = $this->flagService->getFlagFlaggings($entity, $user2);
  87. foreach ($targetUserFriendships as $friendship) {
  88. if ($friendship->getFlaggableId() === $user->id()) {
  89. $friendStatus = FRIEND;
  90. break;
  91. }
  92. }
  93. $friendStatus = $friendStatus == FRIEND ? FRIEND : PENDING;
  94. foreach ($arguments as $key => $argument) {
  95. $variables[$key] = $argument;
  96. }
  97. Heartbeat::updateFriendship($user->id(), $user2->id(), time(), $friendStatus);
  98. $preparsedMessageString = strtr($heartbeatTypeEntity->getMessage(), $variables);
  99. $entitiesObj = new \stdClass();
  100. $entitiesObj->type = 'user';
  101. $entitiesObj->entities = [$user, $user2];
  102. $entities = array(
  103. 'flagging' => $entity,
  104. 'user' => $entitiesObj,
  105. );
  106. $heartbeatMessage = Heartbeat::buildMessage($tokenService, $preparsedMessageString, $entities, $entity->getEntityTypeId(), null);
  107. $heartbeatActivity = Heartbeat::create([
  108. 'type' => $heartbeatTypeEntity->id(),
  109. 'uid' => $user->id(),
  110. 'nid' => $entity->id(),
  111. 'name' => 'Dev Test',
  112. ]);
  113. $heartbeatActivity->setMessage($heartbeatMessage);
  114. $heartbeatActivity->save();
  115. }
  116. }
  117. }
  118. $friendships = Database::getConnection()->select("heartbeat_friendship", "hf")
  119. ->fields('hf', array('status', 'uid', 'uid_target'))
  120. ->execute();
  121. $friendData = $friendships->fetchAll();
  122. $friendConfig = \Drupal::configFactory()->getEditable('heartbeat_friendship.settings');
  123. $friendConfig->set('data', json_encode($friendData))->save();
  124. }
  125. // if ($flagging->getFlaggableType() === 'heartbeat') {
  126. // $oppositeFlag = null;
  127. // if ($flagging->getFlagId() === 'heartbeat_like') {
  128. // $oppositeFlag = $this->flagService->getFlagById('heartbeat_unlike');
  129. // } elseif ($flagging->getFlagId() === 'heartbeat_unlike') {
  130. // $oppositeFlag = $this->flagService->getFlagById('heartbeat_unlike');
  131. // }
  132. // if ($oppositeFlag) {
  133. // $entity = $flagging->getFlaggable();
  134. // $user = \Drupal::currentUser()->getAccount();
  135. // $oppositeFlagging = $this->flagService->getFlagging($oppositeFlag, $entity, $user);
  136. // if ($oppositeFlagging) {
  137. // $this->flagService->unflag($oppositeFlag, $entity, $user);
  138. // }
  139. // }
  140. // }
  141. }
  142. }
  143. /**
  144. * This method is called whenever the flag.entity_unflagged event is
  145. * dispatched.
  146. *
  147. * @param GetResponseEvent $event
  148. */
  149. public function flag_entity_unflagged(Event $event) {
  150. $friendStatus = FRIEND;
  151. $flagging = array_values($event->getFlaggings())[0];
  152. if ($flagging->getFlagId() === 'friendship') {
  153. $entity = $this->flagService->getFlagById($flagging->getFlagId());
  154. $user = $flagging->getOwner();
  155. if ($entity->id() && $user->isAuthenticated()) {
  156. $heartbeatTypeService = \Drupal::service('heartbeat.heartbeattype');
  157. $tokenService = \Drupal::service('token');
  158. foreach ($heartbeatTypeService->getTypes() as $type) {
  159. $heartbeatTypeEntity = $heartbeatTypeService->load($type);
  160. if ($heartbeatTypeEntity->getMainEntity() === "flagging") {
  161. $arguments = json_decode($heartbeatTypeEntity->getArguments());
  162. $user2 = User::load($flagging->getFlaggableId());
  163. $targetUserFriendships = $this->flagService->getFlagFlaggings($entity, $user2);
  164. foreach ($targetUserFriendships as $friendship) {
  165. if ($friendship->getFlaggableId() === $user->id()) {
  166. $friendStatus = NOT_FRIEND;
  167. break;
  168. }
  169. }
  170. $friendStatus = $friendStatus == NOT_FRIEND ? NOT_FRIEND : PENDING;
  171. foreach ($arguments as $key => $argument) {
  172. $variables[$key] = $argument;
  173. }
  174. Heartbeat::updateFriendship($user->id(), $user2->id(), time(), $friendStatus);
  175. }
  176. }
  177. }
  178. }
  179. $friendships = Database::getConnection()->select("heartbeat_friendship", "hf")
  180. ->fields('hf', array('status', 'uid', 'uid_target'))
  181. ->execute();
  182. $friendData = $friendships->fetchAll();
  183. $friendConfig = \Drupal::configFactory()->getEditable('heartbeat_friendship.settings');
  184. $friendConfig->set('data', json_encode($friendData))->save();
  185. }
  186. }