This post provides a technical account of integrating Twitter services into a social networking platform. The implementation dovetails with a goal of the UX2.0 project, to explore social networking for digital library. Dealing with OAuth authentication is a prerequisite of any implementation involving Twitter services. The requirement not only underpins identity services (federated sign on), it is also mandatory for consuming the API methods of the social network. Part 1 of this post addresses this prerequisite and provides an account of how Twitter federated sign on can be developed. This post describes the development of tog - an existing open source social networking platform which we are experimenting with. The development involves an underpinning 'shared object' framework and a use scenario of diffusing messages to Twitter.
Digital Library, Social Networking, Shared Objects
Digital libraries are increasingly assuming the role of Sharium - a social place, idea lab, platform where users express themselves and stage their work. Institutional repository (IR) for example, may be a platform for research, collaboration and networking, in addition to just being a static store for research publications (see a related user research). A remit of UX2.0 involves assimilating social networking capability which is based on tog - an open source, modular and customisable Ruby on Rails platform. tog is underpinned by a series of Model-View-Controller (MVC) plugins, each provides unique functionality. The plugins provide default views and interactions which can be customised (overwritten).
One of the plugins, tog_social provides standard social networking facilities such as friendship and group management. However, the plugin provides only services for users to share and remove of existing objects (from other plugins) to groups. This post describes a 'shared objects' MVC framework extending the plugin functionality:
To provide sharing and management services based on a generic class of digital object.
The framework reduces the dependency of tog_social on other plugins. It also enhances the flexibility of the plugin which can underpin a variety of use scenarios based on different shared objects (subclasses) and group setup:
- Homepage: single shared object (web content), group - single member (owner), private (others can't post)
- Wiki: single shared object (web content), group - many members, open (all member can join group and edit) or moderated (membership and edit moderated by editors)
- Personal blog: many shared objects (web content - posts), group - single member (owner), private (others can't post)
- Group (multiple authors) blog: many shared object (web content - posts), group - many members, open (all member can join group and post) or moderated (membership and posting moderated by editors)
- Personal resource list: many shared objects (web content - links), group - single member (owner), private (others can't post)
- Group resource list: many shared object (basic web content - links), group - many members, open (all member can join group and post) or moderated (membership and posting moderated by editors)
Sharing services of tog are provided by two plugins: acts_as_shareable (third party) and tog_social. The following describes the implementation of the shared object framework based on modifications made on the plugins.
1.acts_as_shareable
Adding a SharedObject model (see below) with basic object states (based on AASM state machine) for publishing scenarios. The model is part of a Model-View-Controller framework and contains the data of a shared object. In addition to the shared object model, the previously binary sharing relationship between objects and groups (modelled by the Share class) has been modified to incorporate additional states ('pending','published') for editorial purposes. Other supporting mechanisms added to the plugin include functions for setting publication date and 'express' sharing (by group owner), model migration (creating SQL table for the SharedObject model) and SQL table changes (add a state column into the shares table for publish/pending editorial control) - see all the related code changes.
# Base object for sharing, subclass in STI for various types of object, e.g. link, wiki, blog post, comment etc.
class SharedObject < ActiveRecord::Base
belongs_to :user
acts_as_taggable
include AASM
aasm_column :state
aasm_initial_state :shared
aasm_state :draft
aasm_state :shared
aasm_event :share do
transitions :to => :shared, :from => [:draft]
end
aasm_event :draft do
transitions :to => :draft, :from => [:shared]
end
end
2.tog_social
A new Homepage class is created. It is a type of Group (subclass) which would be created automatically for each new user as a personal (private) group for staging multiple shared objects. See the related code changes.
To complete the MVC framework for the SharedObject model, controllers and views (HTML form editors) corresponding to the model are necessary for the typical form of object management (create, read, update, delete - CRUD). The framework also facilitates sharing of the objects to Groups. It automatically 'saves a copy' (generates a corresponding share reference) to the creator's homepage (user profile). See all the new codes.
In addition to full HTML editors for creating and updating shared object, a Twitter-like quick share box (see below) is added to both the Group and user homepage (profile) where other users can post pending messages: code changes - Group, code changes - Profile.
Diffusing Shared Objects to Twitter
Although digital libraries are currently developing social networking services, the ways with which the services are being integrated are nascent. Some attempt to create local social networks (badly). Others seek to integrate with existing networks already in use by the users, mostly through post hoc services such as those familiar 'bookmark this or share with' (icons) listings which direct users to external networks. An optimal solution or useful social networks for digital libraries may stem from a combination of both approaches. UX2.0 is attempting this at least technologically by implementing tog while developing interoperable services with existing networks such as Twitter.
We are still exploring ways for using Twitter in digital libraries. This post only provides an example of Twitter use scenarios and demonstrate how the integration can be achieved technologically. The scenario involves sharing objects (messages) to both the local network (enabled by the work described above) and Twitter simultaneously. This corresponds to diffusion user experience, a research and evaluation interest of the UX2.0 project.
1.oauth-plugin
Shared objects can be sent to Twitter via its API. For Ruby on Rails platforms, interaction with the API can be accomplished through oauth-plugin and Twitter gem which provides a system client for the API. Both are required for any implementation of Twitter services including the sign on service as described in Part 1 of this post. Message can be sent to Twitter via the Status Update method. To enable this for tog, simply insert the following method in the corresponding OAuth service model (in oauth-plugin) - TwitterToken (further details, see Part 1 of this post):
# send tweet (comment) to Twitter via the OAuth API
def update(tweet)
client.update tweet
end
Once the user sign on with Twitter, the Status/Update (Twitter gem) action can be invoked in tog via the Twitter service token as described below.
2.tog_social
The diffusion use scenario requires a shared object (text message) to also be sent to Twitter while it is being created locally on the tog social network platform. This provides a seamless user experience. Hence the Twitter API interaction has to occur in the 'create' method of the SharedObject controller. Insert the following codes in the 'def create' method of the controller to enable invocation of the Status/Update action call via the service token:
# Send to Twitter if a twitter user opt to do so
twitter_message = ""
if params[:twitter] && current_user.service_provider.include?('twitter')
current_user.consumer_tokens.each do | token |
token.client.update params[:shared_object][:content] if token.instance_of? TwitterToken
twitter_message = " and to Twitter"
end
end
...
flash[:ok] = "Object shared with " + user_message.join(', ') + twitter_message
The following should also be inserted into the corresponding UI of Group and Profile (user) views, i.e. '/app/views/groups/show' and '/app/views/profiles/show'.
<!-- option only available for federated user logged in with Twitter account -->
<% unless current_user.service_provider.nil? %>
<% if current_user.service_provider.include? 'twitter' %>
<span style="margin-left: 2em;"><%= check_box_tag :twitter %> Send to Twitter</span>
<% end %>
<% end %>
The above will create a 'Send to Twitter' checkbox in the 'Share, New Post' user interface allowing users to opt for sending whatever in the share box to Twitter too.
See further details of the above code changes.
User Experience and Demo Application
This post only describes a simple use scenario involving an API method of Twitter and an example of how a particular part of tog can be customised. You may want to check out the wide ranging methods of the API and make use of the API in some other ways, for example providing Twitter support in other tog plugins. Try out an online demo of the tog-Twitter integration described here - select 'Sign in with Twitter', post a message to a group or user profile while opting for 'Send to Twitter'. You can also try the feature out through the UX2.0 project website.
To install a demo application on a Ruby on Rails enabled machine, run:
rails twitter_tog_demo -m http://github.com/boonious/tog_core/raw/master/tog_template.rb
Then edit the config/initializers/oauth_consumers.rb file, inserting the Twitter consumer service credentials (unique key, secret) for the application. The credentials can be obtained by registering the demo application with Twitter.
Comments