Giter Site home page Giter Site logo

activitystreams-php's Introduction

activitystreams-php

  • Version: 1.2.0
  • Date: 2013/05/28
  • Build Status: Build Status, 100% Code Coverage

This is an activity stream server and client. It's intended to implement a RESTful service to create, publish, (un)subscribe activity streams (according to the "JSON Activity Streams 1.0"1).

Terminology

A Stream contains multiple Activities. Those Activities are posted by Objects. An Object can subscribe/unsubscribe to/from multiple Streams. The Object can pull the most recent Activities (from the Streams he subscribed to) by fetching his Feed.

A Stream can be created with auto_subscribe attribute, which marks it as subscribed by all Objects. Objects are still able to unsubscribe these Streams.

Example

Creating a new Client

The endpoint for the client is here:

$client = new AsClient('http://localhost/server/pub/index.php/');

Create/Open an Application

Now you have to create your application:

$application = $client->createApplication('my_app');

You should store the credentials for this application somewhere. You can recieve the id by using $application->getId() and the secret by using $application->getSecret().

If you already have id and secret, you can get the application by using the credentials.

$application = $client->getApplicationByIdAndSecret($id, $secret);

Create/Open a Stream

To create a new stream, choose if you want to make it public or private. A public stream will automatically be subscribed by all actors.

// every actor subscribes this stream!
$public_stream = $application->createStream("public_stream");
// actors have to subscribe to this stream manually!
$private_stream = $application->createStream("private_stream", array("auto_subscribe" => 0));

You can fetch a stream by id, if you created the stream earlier:

$public_stream = $application->getStreamById("public_stream");

Subscribe/Unsubscribe to/from a Stream

To create a new reader or writer for the stream, you have to create an object.

$actor1 = $application->createObject('user1');

Now you can write with that object to any stream (it does not matter if the object subscribed to this stream or not).

$public_stream->createActivity(array('title' => 'I posted a (public) new link', 'verb' => 'post'), $actor1);
$private_stream->createActivity(array('title' => 'I posted a (private) new link', 'verb' => 'post'), $actor1);

For now the object would only get the data from the public_stream, so you have to subscribe to the private_stream to receive the activities from this stream, too.

$client->subscribeObjectToStream($actor1, $private_stream);

Finally we can fetch the most recent posts of an actor (will be 2!).

$activities = $client->getFeedForObject($actor1, 0, 20);
assert(count($activities) == 2);

Starting with 1.2.0, you can add a $before_activity to the gedFeedForObject-Method:

$activities = $client->getFeedForObject($actor1, 0, 20, $before_activity);
assert(count($activities) == 2);

This will return only those activities, which have been posted before a the $before_activity was created. Useful if you want to implement a load more button.

Ondemand object and stream creation

In case of e.g. user activity streams, you may want to create those streams on demand. To avoid checking if the stream exists, before creating it on demand, you can use the AsApplication#recreateStream and the AsApplication#recreateObject method.

Best practice for creation of objects in those streams:

$actor1 = $application->recreateObject('user1');
$user_stream = $application->recreateStream('user1');
$user_stream->createActivity(array('title' => 'I posted a new link in my personal stream', 'verb' => 'post'), $actor1);

Updating events with recreateObject

If you just want to push the latest object into a stream and don't want to check if the object already exists or not, you can use the AsApplication#recreateObject.

// will create or update the guest-object, so that it has only set the foo=bar property
$guest = $application->recreateObject('guest', array('foo' => 'bar'));
// post an activity with that object
$media_comments_stream->createActivity(array('title' => 'I posted a link!', 'verb' => 'post'), $guest);

Updating streams with recreateStream

If you just want to push the latest version of a stream and don't want to check if the stream already exists or not, you can use the AsApplication#recreateStream.

// will create or update the public_stream-stream, so that it has only set the name='The Public Stream' property
$public_stream = $application->recreateStream('public_stream', array('name' => 'The Public Stream'));

TODO

  • Implement the paper "Feeding Frenzy: Selectively Materializing Users’ Event Feeds" from http://research.yahoo.com/pub/3203
  • Add database structure and way to create the database from scratch

Changelog

  • 1.2.0 (2013/05/28)
    • added AsApplication#getActivityById (see #1)
    • added parameter $before_activity to AsObject#getFeed($offset = 0, $limit = 20, AsActivity $before_activity = null) and to AsApplication#getFeedForObject(AsObject $object, $offset = 0, $limit = 20, AsActivity $before_activity) (see #4)
  • 1.1.0 (2013/02/04)
    • added possibility to set the activity id when creating the activity
    • added new methods on AsActivity (getTitle, getVerb, getId, getPublished, getUrl)
    • added AsActivity#getActor() and AsActivity#getActorId()
    • added AsActivity#getTarget() and AsActivity#getTargetId()
    • added AsActivity#getObject() and AsActivity#getObjectId()
    • added AsApplication#deleteActivity
    • added AsApplication#recreateStream
    • added AsStream#getName
    • added AsStream#isAutoSubscribe
    • remove all activites as soon as an object was removed
    • added update-Link and auto_subscribe:bool property to stream resources
  • 1.0.0 (2013/01/13)
    • Initial release

Contributors

License

This work is copyright by DracoBlue (http://dracoblue.net) and licensed under the terms of MIT License.

activitystreams-php's People

Contributors

dracoblue avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

activitystreams-php's Issues

Prevent showing again the same activities when loading the next page of activities

Right now I've got a profile page where I show one users latest activities. At the bottom of the page there's a "load more" button that loads with ajax the next "page" of activities.

So the ajax call basically runs the getFeed method with a different offset.

The problem I find is that if in the meanwhile more activities were created, or were deleted, then when we load the new ones, we will either repeat some activities that were already shown, or we skip some activities that haven't been shown yet.

Add possibility to delete a Activity

I could not find a method to delete a Activity, so I had to add a workaround.

It would be good to add the possibility to delete Activity by ID or by other fields inside the activity table, such as stream_id, actor_id, object_id, verb, and so on.

I needed this feature because I wanted to delete a activity without deleting the object from the database because it could be used by other activities.

So, in my Yii Framework component wrapper for the Activity Stream client, I've added the following method:

public function deleteActivityByValues($values=array()) { if (count($values)==0) return false; $criteria=new CDbCriteria(array( 'limit'=>1, )); $values['application_id']=$this->application_id; foreach ($values as $k => $v) { $criteria->addCondition("$k=:$k"); $criteria->params[$k]=$v; } return ActivityStreamActivity::model()->deleteAll($criteria); }

And now I can call the following to delete a Activity from a certain Stream related to a certain Object:
Yii::app()->as->deleteActivityByValues(array( 'stream_id'=>$stream->getId(), 'object_id'=>$object->getId(), ));

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.