Browse Source

Friendship -> checks if user friendship is complete and sets messages appropriately.
Controller method for testing friendship stream

logicp 7 years ago
parent
commit
1663eb324d

+ 8 - 0
heartbeat.routing.yml

@@ -23,3 +23,11 @@ heartbeat.stream_test_controller_stream:
     _title: 'stream'
   requirements:
     _permission: 'access content'
+
+heartbeat.stream_test_controller_friendstream:
+  path: '/friendactivity'
+  defaults:
+    _controller: '\Drupal\heartbeat\Controller\StreamTestController::friendstream'
+    _title: 'Friendship'
+  requirements:
+    _permission: 'access content'

+ 30 - 0
src/Controller/StreamTestController.php

@@ -94,4 +94,34 @@ class StreamTestController extends ControllerBase {
     ];
   }
 
+
+  /**
+   * Stream.
+   *
+   * @return string
+   *   Return Hello string.
+   */
+  public function friendstream() {
+    $messages = array();
+    $types = $this->heartbeatStreamService->getTypesById(3);
+    foreach ($types as $type) {
+      if ($type != null) {
+
+        $heartbeatType = $type->getValue();
+
+        $heartbeats = $this->heartbeatService->loadByType($heartbeatType);
+
+        foreach($heartbeats as $heartbeat) {
+          $messages[] = $heartbeat->getMessage()->getValue()[0]['value'];
+        }
+      }
+    }
+
+    return [
+      '#theme' => 'heartbeat_stream',
+      '#messages' => array_reverse($messages),
+      '#attached' => array('library' => 'heartbeat/heartbeat')
+    ];
+  }
+
 }

+ 28 - 11
src/EventSubscriber/HeartbeatEventSubscriber.php

@@ -11,6 +11,10 @@ use Drupal\heartbeat\HeartbeatTypeServices;
 use Drupal\heartbeat\HeartbeatStreamServices;
 use Drupal\heartbeat\HeartbeatService;
 
+const NOT_FRIEND = -1;
+const PENDING = 0;
+const FRIEND = 1;
+
 /**
  * Class HeartbeatEventSubscriber.
  *
@@ -68,11 +72,13 @@ class HeartbeatEventSubscriber implements EventSubscriberInterface {
    * dispatched.
    *
    * @param GetResponseEvent $event
+   * @return GetResponseEvent|Event
+   * @throws \Drupal\Core\Entity\EntityStorageException
    */
   public function flag_entity_flagged(Event $event) {
-    $stophere = null;
+    $friendStatus = NOT_FRIEND;
     $flagging = $event->getFlagging();
-    if ($flagging->getFlagId() == 'friendship') {
+    if ($flagging->getFlagId() === 'friendship') {
       $entity = $this->flagService->getFlagById($flagging->getFlagId());
 
       $user = $flagging->getOwner();
@@ -87,15 +93,28 @@ class HeartbeatEventSubscriber implements EventSubscriberInterface {
 
           $heartbeatTypeEntity = $heartbeatTypeService->load($type);
 
-          if ($heartbeatTypeEntity->getMainEntity() == "flagging") {
+          if ($heartbeatTypeEntity->getMainEntity() === "flagging") {
+
             $arguments = json_decode($heartbeatTypeEntity->getArguments());
-//            $entityTypeFlagging = $flagging->getEntityType();
-//            $id = $entityTypeFlagging->id();
-//            $id_other = $flagging->get('entity_id');
-//            $entityTypeId = $flagging->getEntityTypeId();
-//            $flaggable = $flagging->getFlaggableId();
             $user2 = User::load($flagging->getFlaggableId());
+            $targetUserFriendships = $this->flagService->getFlagFlaggings($entity, $user2);
 
+            foreach ($targetUserFriendships as $friendship) {
+              if ($friendship->getFlaggableId() === $user->id()) {
+                $friendStatus = FRIEND;
+                break;
+              }
+            }
+
+            $friendStatus = $friendStatus == FRIEND ? FRIEND : PENDING;
+
+            if ($friendStatus === FRIEND) {
+              drupal_set_message($user->getUsername() . ' is now friends with ' . $user2->getUsername());
+            } else if ($friendStatus === PENDING) {
+              drupal_set_message($user->getUsername() . ' has requested friendship with ' . $user2->getUsername());
+            } else {
+              drupal_set_message($user->getUsername() . ' is unable to request friendship with ' . $user2->getUsername());
+            }
 
             foreach ($arguments as $key => $argument) {
               $variables[$key] = $argument;
@@ -112,8 +131,6 @@ class HeartbeatEventSubscriber implements EventSubscriberInterface {
 
             $heartbeatMessage = Heartbeat::buildMessage($tokenService, $preparsedMessageString, $entities, $entity->getEntityTypeId(), null);
 
-            //      $translatedMessage = t($messageTemplate);
-
             $heartbeatActivity = Heartbeat::create([
               'type' => $heartbeatTypeEntity->id(),
               'uid' => $user->id(),
@@ -123,7 +140,6 @@ class HeartbeatEventSubscriber implements EventSubscriberInterface {
 
             $heartbeatActivity->setMessage($heartbeatMessage);
             $heartbeatActivity->save();
-
           }
         }
       }
@@ -132,6 +148,7 @@ class HeartbeatEventSubscriber implements EventSubscriberInterface {
     drupal_set_message('Event flag.entity_flagged thrown by Subscriber in module heartbeat.', 'status', TRUE);
     return $event;
   }
+
   /**
    * This method is called whenever the flag.entity_unflagged event is
    * dispatched.