Click to expand
10 |
11 | - [Introduction](#introduction)
12 | - [Installation](#installation)
13 | - [Usage](#usage)
14 | - [Facade](#facade)
15 | - [Giving a model an ability to have a Feed](#Giving-a-model-an-ability-to-have-a-Feed)
16 | - [Create a model Feed](#create-a-model-feed)
17 | - [On-Demand Feeds](#On-Demand-Feeds)
18 | - [Create an Activity](#create-an-activity)
19 | - [Actors](#actors)
20 | - [Valid Actors](#valid-actors)
21 | - [Targets](#targets)
22 | - [Valid Targets](#valid-targets)
23 | - [Objects](#objects)
24 | - [Valid Objects](#valid-objects)
25 | - [Get supported verbs](#Get-supported-verbs)
26 | - [Add an activity to a Feed](#Add-an-activity-to-a-Feed)
27 | - [Add multiple activities to a Feed](#Add-multiple-activities-to-a-Feed)
28 | - [Events](#events)
29 | - [Configuration](#configuration)
30 | - [FAQ](#faq)
31 |
32 |
33 |
34 | ## Introduction
35 |
36 | This package enables you to have activity streams in your laravel applications.
37 |
38 | ## Installation
39 |
40 | Install with composer
41 |
42 | ```sh
43 | composer require musonza/laravel-activity-streams
44 | ```
45 |
46 | Once the composer installation is finished, you can add alias for the facade. Open `config/app.php`, and make the following update:
47 |
48 | 1) Add a new item to the `aliases` array:
49 |
50 | ```php
51 | 'ActivityStreams' => Musonza\ActivityStreams\ActivityStreamsFacade::class,
52 | ```
53 |
54 | 1) Publish the configuration file into your app's `config` directory, by running the following command:
55 |
56 | ```
57 | php artisan vendor:publish --tag="activity.streams.config"
58 | ```
59 |
60 | 1) Publish the migrations into your app's `migrations` directory, by running the following command:
61 |
62 | ```
63 | php artisan vendor:publish --tag="activity.streams.migrations"
64 | ```
65 |
66 | 1) Run the migrations:
67 |
68 | ```
69 | php artisan migrate
70 | ```
71 |
72 | ## Usage
73 |
74 | #### Facade
75 |
76 | Whenever you use the `ActivityStreams` facade in your code, remember to add the following line to your namespace imports:
77 |
78 | ```php
79 | use ActivityStreams;
80 | ```
81 |
82 | #### Giving a model an ability to have a Feed
83 |
84 | Use the `HasFeed` trait to allow a model to have a feed.
85 | ```php
86 | createFeed();
102 | ```
103 |
104 | #### On-Demand Feeds
105 |
106 | Sometimes you may want to create a Feed that's does not belong to a Model. For example,
107 | you want to add activities to a Trending Feed for your application:
108 |
109 | Create a class to represent the Trending feed under a namespace of choice
110 |
111 | ```php
112 | unique(['some-unique-id', 'App\Trending']);`
129 |
130 | #### Create an Activity
131 |
132 | An example of an activity will be something like **John liked a photo in 2018Album**
133 |
134 | | | |
135 | | ------------- |:-------------:|
136 | | Actor |John |
137 | | Verb | like |
138 | | Object | photo |
139 | | Target | 2018Album |
140 |
141 |
142 | ```php
143 | use ActivityStreams;
144 | use Musonza\ActivityStreams\ValueObjects\Verbs;
145 |
146 | $activity = ActivityStreams::setActor($actor)
147 | ->setVerb(Verbs::VERB_LIKE)
148 | ->setObject($object)
149 | ->setTarget($target)
150 | ->createActivity();
151 | ```
152 |
153 | #### Actors
154 |
155 | ##### Valid Actors
156 |
157 | You can pass in an Eloquent Model as an actor or any Object that implements `Musonza\ActivityStreams\Contracts\ActivityActor` interface
158 |
159 | #### Targets
160 |
161 | ##### Valid Targets
162 |
163 | You can pass in an Eloquent Model as a target or any Object that implements `Musonza\ActivityStreams\Contracts\ActivityTarget` interface
164 |
165 | #### Objects
166 |
167 | ##### Valid Objects
168 |
169 | You can pass in an Eloquent Model as an object or any Object that implements `Musonza\ActivityStreams\Contracts\ActivityObject` interface
170 |
171 |
172 | #### Get supported verbs
173 | ```php
174 | $verbs = ActivityStreams::verbs();
175 | ```
176 |
177 | #### Add an activity to a Feed
178 | ```php
179 | ActivityStreams::addActivityToFeed($feed, $activity);
180 | ```
181 |
182 | #### Add multiple activities to a Feed
183 | Adds a `Collection` of activities to a `Feed`
184 |
185 | ```php
186 | ActivityStreams::addActivityToFeed($feed, $activities);
187 | ```
188 |
189 | #### Add an activity to multiple Feeds
190 | Adds an `Activity` to a `Collection` feeds
191 |
192 | ```php
193 | ActivityStreams::addActivityToMultipleFeeds($feeds, $activity);
194 | ```
195 |
196 | ## Events
197 |
198 | You can leverage and listen for the following events to perform actions in
199 | your application. For instance you can listen for an `ActivityCreated` event and depending on
200 | your business logic add the created event to a `Feed` or multiple feeds.
201 |
202 | #### ActivityCreated
203 | `Musonza\ActivityStreams\Models\Activity\ActivityCreated`
204 |
205 | #### ActivityDeleted
206 | `Musonza\ActivityStreams\Models\Activity\ActivityDeleted`
207 |
208 | #### FeedCreated
209 | `Musonza\ActivityStreams\Models\Activity\FeedCreated`
210 |
211 | #### FeedDeleted
212 | `Musonza\ActivityStreams\Models\Activity\FeedDeleted`
213 |
214 | ## Configuration
215 |
216 |
217 | ## FAQ
218 | See more on Activity Streams specifications [here](http://activitystrea.ms/)
219 |
--------------------------------------------------------------------------------
/tests/Unit/FacadeTest.php:
--------------------------------------------------------------------------------
1 | activityStreams = app(ActivityStreams::class);
28 | }
29 |
30 | public function testGetDefinedVerbs()
31 | {
32 | $verbs = $this->activityStreams->verbs();
33 |
34 | $this->assertIsArray($verbs);
35 | $this->assertEquals('post', $verbs['VERB_POST']);
36 | }
37 |
38 | /**
39 | * @dataProvider activitiesDataProvider
40 | * @throws InvalidActivityVerbException
41 | */
42 | public function testCreateActivity($actor, $target, $object)
43 | {
44 | $actor = isset($actor['is_model']) ? factory(User::class)->create() : $actor['value'];
45 | $target = isset($target['is_model']) ? factory(User::class)->create() : $target['value'];
46 | $object = isset($object['is_model']) ? factory(User::class)->create() : $object['value'];
47 |
48 | $activity = $this->activityStreams->setActor($actor)
49 | ->setVerb(Verbs::VERB_POST)
50 | ->setTarget($target)
51 | ->setObject($object)
52 | ->createActivity();
53 |
54 | $this->assertInstanceOf(Activity::class, $activity);
55 | }
56 |
57 | public function testAddActivityToFeed()
58 | {
59 | $user = factory(User::class)->create();
60 | $feed = $user->createFeed();
61 |
62 | $activity = $this->activityStreams->setActor($user)
63 | ->setVerb(Verbs::VERB_POST)
64 | ->setObject(new SampleObject())
65 | ->setTarget(new SampleTarget())
66 | ->createActivity();
67 |
68 | $this->activityStreams->addActivityToFeed($feed, $activity);
69 |
70 | $this->assertEquals(1, $feed->activities()->count());
71 | }
72 |
73 | public function testAddMultipleActivitiesToFeed()
74 | {
75 | $user = factory(User::class)->create();
76 |
77 | /** @var Feed $feed */
78 | $feed = $user->createFeed();
79 |
80 | $activities = factory(Activity::class, 2)->create();
81 |
82 | $this->activityStreams->addActivityToFeed($feed, $activities);
83 |
84 | $this->assertEquals(2, $feed->activities()->count());
85 | }
86 |
87 | public function testAddActivityToMultipleFeeds()
88 | {
89 | $users = factory(User::class, 5)->create();
90 |
91 | $feeds = [];
92 | foreach ($users as $user) {
93 | $feeds[] = $user->createFeed();
94 | }
95 |
96 | $feeds = collect($feeds);
97 |
98 | /** @var Activity $activity */
99 | $activity = factory(Activity::class)->create();
100 |
101 | $this->activityStreams->addActivityToMultipleFeeds($feeds, $activity);
102 |
103 | $this->assertEquals(5, $activity->feeds()->count());
104 | }
105 |
106 | public function testFeedFollowing()
107 | {
108 | /** @var User $user1 */
109 | $user1 = factory(User::class)->create();
110 | /** @var Feed $user1Feed */
111 | $user1Feed = $user1->createFeed();
112 |
113 | /** @var User $user1 */
114 | $user2 = factory(User::class)->create();
115 | /** @var Feed $user2Feed */
116 | $user2Feed = $user2->createFeed();
117 |
118 | $user1Feed->follow($user2Feed);
119 |
120 | $this->assertDatabaseHas('follows',[
121 | 'follower_id' => $user1Feed->getKey(),
122 | 'follower_type' => get_class($user1Feed),
123 | 'followable_type' => get_class($user2Feed),
124 | 'followable_id' => $user2Feed->getKey(),
125 | ]);
126 |
127 | }
128 |
129 | public function activitiesDataProvider()
130 | {
131 | return [
132 | [
133 | 'actor' => [
134 | 'is_model' => true,
135 | 'value' => null,
136 | ],
137 | 'target' => [
138 | 'is_model' => true,
139 | 'value' => null,
140 | ],
141 | 'object' => [
142 | 'is_model' => true,
143 | 'value' => null,
144 | ],
145 | ],
146 | [
147 | 'actor' => [
148 | 'value' => new Actor('twitter_user', 126626)
149 | ],
150 | 'target' => [
151 | 'value' => new SampleTarget()
152 | ],
153 | 'object' => [
154 | 'is_model' => true,
155 | 'value' => null,
156 | ],
157 | ],
158 | [
159 | 'actor' => [
160 | 'is_model' => true,
161 | 'value' => null,
162 | ],
163 | 'target' => [
164 | 'value' => new SampleTarget()
165 | ],
166 | 'object' => [
167 | 'value' => new SampleObject()
168 | ],
169 | ],
170 | [
171 | 'actor' => [
172 | 'value' => new Actor('twitter_user', 126626)
173 | ],
174 | 'target' => [
175 | 'value' => new SampleTarget()
176 | ],
177 | 'object' => [
178 | 'value' => new SampleObject()
179 | ],
180 | ],
181 | [
182 | 'actor' => [
183 | 'value' => new Actor('twitter_user', 126626)
184 | ],
185 | 'target' => [
186 | 'value' => new SampleTarget()
187 | ],
188 | 'object' => [
189 | 'is_model' => true,
190 | 'value' => null,
191 | ],
192 | ],
193 | ];
194 | }
195 | }
--------------------------------------------------------------------------------