Building Social Networks


Published on

This session will address how complex social networks of various types can be built with Drupal. The nuances of Feeds, Walls, Sharing (both private and public), Friends, Following, and (most importantly) Privacy will be explored, and options for building these features with Drupal will be discussed, with examples from the real world.

This is an advanced session but anyone with social-networking dreams would benefit from learning the challenges in building one.

How do you make a network "Social"?
A Drupal site is a network of users and content, but it is not inherently social. It's greatest original feature was the ability for multiple users to collaborate in managing the system. We'll talk about what makes networks social and what makes them fun: Feeds, Activity, & Sharing.

"News Feeds" can show not only your friend's content, but your friends-of-your-friends content when the target is your friend. Sound complicated? It is!

"Activity" is when you become friends with someone, join the site, "like" something, commented on something... the list goes on. Without activity display, a social network feels more like a MySpace than Facebook. But be careful... if you list each new activity all of your friends make, it can get clogged with redundant announcements. Learn how we devised a system that lets us smartly group recent activity taken by user, taxonomy term, or node.

Great social networks may be easy to use, but the logic behind true social networks is very complex.

The Details

- Building news feeds for friends and "followed" terms with Search API with Apache Solr
- How to let users "share" content and write on other users "walls".
- Creating an "activity" system that shows users activity around the site and can group similar activity together.
- Privacy & Permissions: How to give control where control is due.

About the Speaker

Jonathan is the Founder & CTO of ThinkDrop Consulting, a Drupal consulting company in Brooklyn, New York and has been developing with Drupal for more than 7 years, coding with PHP for more than 11 years, and hypertexting with HTML since 1997.

This session was originally given at DrupalCampNYC 10 in December of 2012

Slides available at

NOTE: I apologize for the layout problems, Google Docs Presentations look different on different operating systems

Experience Level: Advanced

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Building Social Networks

  1. 1. Building Social Networks NYC Camp  July 21 2012 #nyccamp @careernerd & @thinkdropNYCJon PughFounder & CTO, ThinkDrop
  2. 2. Jonathan Professional Web Developer sinceJon Pugh on 1999 Exclusively Drupal since 2004ThinkDropConsulting Clients have included Institute Integrative Nutrition, BlogHer, Sony Music, Imbee.Now on Google+! com, & We help organizations learn and leverage Drupal. from Brooklyn New York
  3. 3. WARNING: Some of the modules you are about to see may beinnappropriate for some users (and most developers).
  4. 4. WARNING: If you see something you dont like, pleasewrite a patch, a workaround, or just deal with it.   DO NOT complain to the module But seriously, a patch would be great... contributors.  They work for free. 
  5. 5. WARNING: Building a social network is a challenging, demanding,and often frustrating experience.  Your goals should be modest.  Dont ever agree to build a feature thatworks "like Facebook does" without thorough research. in other words... PROCEED WITH CAUTION
  6. 6. What is a social network? Define network?A group or system of interconnected people or things Define social network?A network of social interactions and personal relationships A dedicated website or other application that enables users to communicate with each other by posting information, comments, messages, images, etcEvery Drupal site is a network, but its social tools are
  7. 7. What does it take to be a social Relationships network? Communication Friends, Followers, Circles... Public & Private.   Choose something. "Write on my Wall" (But dont do everything.) "Send me a message" "Mention Me" "Share This" Distribution ActivityGet content to interested viewers. "Joe, Jane, and Pat changed their (but not too much content!) profile pictures." Let users subscribe to content. "3 of your friends liked kittens" "Sam is now friends with Alex and Integration Privacy &Bob."Permissions Dont build a walled garden. Be thoughtful. Let users have some control, but dont overwhelm them. @thinkdropNYC
  8. 8. How do they do it? Facebook Twitter LinkedIn Drupal CoreRelationships Friends Following Complex None. Networks Connections Fans (now Likes)Communication The Wall Open Stream Exclusive Limited comment.module Comments Direct Messages Network contact.module The "Share" Mentions RetweetDistribution News Feed Following Copy of News /node Content and Activity Feed /taxonomy/term/ by Friends %Privacy & Friends, Friends of Public or Private "access content"Permissions Friends, Everyone Accounts "access user Per Post Permissions profiles"Integration Applications API First Applications Theres a module Platform Open API Platform for that. Connect Open Philosophy Social Plugins Open Graph
  9. 9. How do we do it?Relationships Communication Distribution Privacy & Permissions Integrationuser_relationships privatemsg search_api flag_friend_access twitterflag_friend dxmpp search_api_solr user_relationships_access rpxog references search_api_views spaces_permissions servicesrelation entity views_datasource Oh, and these... addressfield admin_menu advanced_help apachesolr apc backup_migrate  backup_migrate_files captcha coder commentaccess comment_notify compact_forms   contact_importer context ctools date devel diff ds dummyimage dxmpp entity entity cache  eva extlink fbconnect features feeds field_group fivestar flag flag_friend galleryfor matter  google_analytics job_scheduler jquery_ui lexicon libraries link logintoboggan memcac he  messaging mimemail module_filter multiform nodereference_url oauth og panels  path_alias_xt pathauto plupload porterstemmer privatemsg profile2 quicktabs realna @thinkdropNYC me  references remember_me rpx rules search_api search_api_context 
  10. 10. Best Practices Use them or lose
  11. 11. Best Practices!!!Complex Stories, Complex TestingSocial Networks are more complex than most sites becauseyou have to imagine multiple users interacting with the samecontent with different configurations while imagining the dataand your code simultaneously.  In an ideal world, when all best practices are in place,developing a complex platform can actually be fun.Higher ExpectationsSocial Networks are more sensitive to uptime and bug-freefunctionality than most sites, since they usually cater to someinnate human need and are therefor used much more thanmost websites.  
  12. 12. Best Practices!!!Exportable.  If you cant "export", you cant safely and easily deploy.  If its not incode, its volatile, and you cant go back.  EXPORT and COMMIT!See and  Run tests.  Build tests.  Learn test-driven development.Or crying will become a part of your debugging experience and you willlose more of your life to clicking than you would like to keep track of.See, not configurable. Rules.module: BAD.  PHP Input Filter: VERY BAD.  Any PHP you inputthrough a web browser, even in views:  BAD BAD BAD!.   Drupal Hooks:good.  Rules uses hooks to trigger "Rules".  You should too.  If you can
  13. 13. Best Practices!!!One "Drupal" Development Site, localhost for codeonly.  Features and exportables can be troublesome when developers arepassing around exported code and databases from and to adevelopment site and localhost.  An old view in a developers databasecould be exported as new when rebuilding a feature module.Use ONE central site for ALL Views, Pages, Configuration, Contenttypes, Fields development, and other Drupal web-configurablesystems. EXPORT features from the Dev site to code, but the dev site is thebleeding edge configuration model for your system.  When it is nearing update time, use @thinkdropNYC drush features-update-all
  14. 14. Relationships I like you, do you like me?
  15. 15. Relationships How do we do it?User Relationships   ● Complex setup, Complex relations complex relationships ● Overkill for simple relationshipsdrupal. ● UI and UX is rough, at ● Mature (as in Old.  Not Exportable!) ● Simple UI (uses flag.module, for theFlag Friend most part)Two-way approval ● Only supports basic ● Allows grouping of users and content ● Provides generic way to link all content toOrganic Groups group (and give access to only theModerated Membership members of that group) ● Provides a new Entity type: Relation.  Relations are fieldable, butRelations dont have to be.API Module.  Needs Interface, but ● Relations can be "symmetrical" (Friends)
  16. 16. The Purpose of Relationships ● Define Content Privacy: Who can see my content? Who can I limit the visibility of my content to? ● Define Content Subscription: What do I see? Whos content shows up on my feed? ● Define Permissions: Who can do what with me? Write on my Wall, Direct Message me, Request Friendship, etc. Why do you want relationships?What value does adding relationships to your site have?Without functionality behind it, relationships are pointless.Be sure to think about what creating a relationship does.Is this going to help or hurt?Without an extremely polished and fluid User Experience, managinganother social list will become a chore.
  17. 17. Types of Relationships flag.module or user_relationships.moduleFollowing ● One way.  Follow a person (or thing) to subscribe to their content.  They dont have to follow you back. ● Does not necessarily make sense to use for access control.   flag_friend.module or user_relationships.moduleFriendship ● Both users approve of the relationship. ● Usually a misnomer, because its the only relationship on a site. ● Difficult to maintain for popular users (celebrities, etc.) ● Usually comes with a way to make content "friends relation.module or user_relationships.module only".  This implies there is content that is public.  (or at least user_access("access content") ● Both users are subscribed to each others content.
  18. 18. Types of RelationshipsAutomatic & Discovered ● Users can be related by their mutual interests, location, or any other piece of data you collect. ● Use this to help users discover one another, even if they are already friends.
  19. 19. Communication "Youve Got Mail" "Can you hear me now?" "Facebook Me"
  20. 20. Communication How do we do it?Private Messages   ● A simple messaging system. ● UI and UX is rough.  Really rough.another inbox ● Mature (as in Old.)   ● Manage your email templates with care. ● There may be more than one person toDont forget to remind me. email when the time ● Dont spam your users. personalization and use sensible def ● Drupal 7 Core allows fields on a User.User Profiles ● This gets lumped in with all of theuser.module is ok with account settings a Drupal user alreadyfields, profile types are has to deal with.better. ● Profile2.module allows "Profile Types" to be created, just like node types, allowing @thinkdropNYC one user to have one of each type of
  21. 21. Communication How do we do it? ● Allow friends to post somethingStatus Posts &  targeted at one person, but visiblePost to "Wall" Targeted but open messages. to many.Custom Node Type, User Reference Field, & ● Starts discussions among groupsCode of friends ● The ability to reference andSharing share an existing piece ofTake this node and share it.Custom Node Type, Node Reference content on your siteField, & Code @thinkdropNYC
  22. 22. privatemsg.module another inboxPrivate Messages is a very old and reliable module thatlooks and acts its age.You will want to heavily alter the presentation and displaylogic.Think long and hard about whether you really need to give your users another inbox to check.  You will never make it as smooth
  23. 23. "Status & Wall Posts" node.type: statusFieldstitle: 255 Characters max in the database.  You dont need a field for a tweet.uid: The author of this post.field_ref_user:  The target of this post.   ● If posted on another users "wall", field_ref_user will = that user. ● defaults to node:uid for easier filtering: a profile page uses this field for an argument ● This user needs ability to delete this post (and comments!) ● This users friends should be able to see this post (possibly)Usage    node/add/status?  no. (unless you want a popup)Use hook_block_info() and hook_block_view() to create a block.  Loaddrupal_get_form(status_node_form) into the block.  Place the block inthe  content region on (almost) every page.  Voila!  Status Form.
  24. 24. "Share Posts" node.type: shareFieldstitle: 255 Characters max in the database.  You dont need a field for a tweet.uid: The author of this post.field_ref_user:  Same as a status node.  field_ref_node:  A node reference field storing the node to be shared.   ● Drupal paths ignore additional arguments: node/add/share/12354/self returns the same page as node/add/share ● Create a link on every share-able node type to "node/add/share/$NID/self" ● Use form_alter() to set and hide this field, just like field_ref_user.Its nice to add the option to "share with a friend" in addition to "post to my wall".  if(arg(4) != self), UNHIDE field_ref_user and use the autocomplete for "enter afriend to share with"! @thinkdropNYC
  25. 25. User Profiles Drupal 7 GoodnessFields in Core!Fields possible on all Entities: Nodes, Users, Terms, Files, etc.Taxonomy is linked to nodes via "Term Reference" fieldsYou can add "Term Reference" fields to Users, giving us the ability tolink users to content and each other through similar terms.Add image and link fields to Terms.Profile2.moduleSystem for creating "Profile Types", allowing grouping of fields for eachuser.  Emulates the old profile.module which had "categories" of userprofiles.
  26. 26. User Profiles Drupal 7 GoodnessTaxonomy as "Things to Like" Using taxonomy terms as the storage for "things people like" opens upa lot of possibilities.We are able to build lists of the "Most popular things".We are able to show you content that is tagged with thosethings.  (written on that things "Wall")We are able to match you with other users based on those things. @thinkdropNYC
  27. 27. Distribution Feed Me!
  28. 28. Distribution How do we do it?Search API ● So much more than search...Index Everything. ● Basically an interface to No-SQL data (Solr, Mongo, you name it) ● Can index all entities ● All Views can be Search API powered ● Everything Drupal 7 wanted to beEntity API ● Define properties with arbitrary getters andSpecifically... settershook_entity_property_info() ● Automatically access all properties in Search ● References Fields connects the referenced entities and can index attached fields. ● Not MySQL means better, stronger, faster ● Powered by Views means Easy, Powerful,Apache Solr  FlexibleDocument-based index ● Search API meansstorage = fastdrupal. @thinkdropNYCorg/project/search_api_solr
  29. 29. Distribution Channels How are your users fed content?Your Feed Your "Wall" Additional FeedsA list of content and Your posts (when not Usually, having atactivity created by targeted at another least one otherusers you are related to user), and other thing to let usersor want to see content users posts on your find content with isand activity from. wall. helpful.May or may not include Access control isposts on friends walls by important Groups, Lists,non-friends. here.  Users must be Taxonomy Terms...   able to delete posts on their "walls". Something. @thinkdropNYC
  30. 30. Entity API Enhance the EntitiesThe Entity API allows you to do many things, includingset extra properties of entities as they are saved.  In order to build things like a "Friends Feed", a"Following Feed" or a "Wall", we can build extraproperties of a user and a node.Those properties are then loaded by the Search APIinto its index. @thinkdropNYC
  31. 31. Entity API hook_entity_property_info_alter()By creating the"friends" property ofthe user entity, wewill be able to load itwhen we index anode, which meanseach node has a listof "friends" it shouldbe shown to in theirfriends feed.  Then, we can build aSearch API Viewwith the current $user->uid as the
  32. 32. Search API Extrapolate the Index Creates multiple Indexes and allows multiple serversHave an index for each thing you want to search ordisplay from a Search API backend like Solr.
  33. 33. Search API Extrapolate the IndexChoose your entity type, then choose your fields.  Entity relationshipsallow you to branch out and load related fields as well.Allows you to build specific indexes withonly the fieldsyou need.The indexes and Fields you create then become available in Views for your building pleasure.
  34. 34. "Friend Feed" Content from any of your friendsA view with the a Contextual filter for User Friends,  that defaults to $global->user;
  35. 35. "Wall Feed"Your Posts (not on friends walls) and Friends Posts (on your wall) A view with the a Contextual filter for Node: Target,  that defaults to the User ID in the current URL.  Path: user/%    Overrides existing "User Profile Page"
  36. 36. Privacy & Permissions "Control! Control! You must have control!"
  37. 37. Why do you need Privacy & Permissions Control?How much control do yourusers really need?A balance must be struckbetween security and access.With privacy and permissionscontrol, micromanagement canalso become a problem.  Makeit as easy as possible for yourusers. @thinkdropNYC Remember: Every Site is Different!
  38. 38. Privacy & Permissions How do we do it?Node Access Safely hide content using modules.Low Level Protection.Core System.  Requires contrib Nodes only.modules to take advantage. System-Wide access control.User Relationships or Plugs into node_access.  ProvidesFlag Friend Access user interface for choosing whatRelationship modules provide relationships can view your nodes.access control. Create settings based on how yourProfile Fields can be site works and what is best forused for user settings. your users.Or just use your custommodule, a form_alter and
  39. 39. Privacy & Permissions Common Access Checkpointshook_menu_alter()Add your ownfunction to"access callback"to check if yourusers are allowedto view the pageon some specialconditions. @thinkdropNYC
  40. 40. Privacy & Permissions Common Access Checkpoints Build Elements (a.k.a. Forms API a.k.a "render-able arrays") To hide an element in a form or a build array: $element[#access] = FALSE; hook_node_access() Simple True/False access check. hook_menu_alter()  $item[user/%user][access callback] = custom_access_check; @thinkdropNYC
  41. 41. Privacy & Permissions Saving User Settings User or Profile2 Fields More configurable.  (Can be good or bad!)   Data is more accessible.  Uses Field API storage. Dont forget to "Manage Display" and hide setting fields! $user->field_setting[LANGUAGE_NONE][0][value] $user->data Not accessible with views or queries at all.   Can only load if youve loaded the user. Data is serialized into the {users}.data table column.   Custom Modulehook_schema()  +  hook_user_load() +  hook_user_insert() + hook_user_update() @thinkdropNYC
  42. 42. Privacy & Permissions Saving User SettingsUsing a Fieldoffers flexibilityand a nicefriendly, familiar,interface.
  43. 43. Activity Nodes are boring.   What are you doing?
  44. 44. Activity How do we do it?Activity.module   Heartbeat.moduleNot Recommended.  Cant get it Better, but still more troubleto work on Drupal 7. its worth. Statuses.module  Formerly Facebook-Style Statuses We dont recommend any of the existing Drupal Contrib "activity" type modules.  They are buggy, and very rigid because of their custom storage and code.   Drupal FieldAPI can be leveraged to build a new A node can represent system. an activity, and there are a number of reasons this is ideal.
  45. 45. Activity How do we do it? With Nodes!Myth: Nodes are "Heavy".Reality: Nodes have a lot of features that otherentities (or custom "objects" like a "heartbeat"message) dont have, that are required for certainfunctionality: ● node.uid: Nodes have an owner, who is granted higher permissions over the node.   ● Node Access: Nodes have access control, so you can keep activity private using other node access modules. ● Comments: Nodes can be commented on. ● They are Nodes.  This means they can be easily
  46. 46. Activity node.type: activityFields Activity Types:title: Not used.  Display is processed with code.uid: The actor of the activity comment profile changes field_activity_type:  A machine-name defining flag_favoritewhat type it is.  The message theming is changed basedon this. flag_follow    flag_likefield_activity_ref_nodes & friendsfield_activity_ref_terms & photosfield_activity_ref_users:  References to signupeach type of object that might be connected to anactivity. videos
  47. 47. Integration Dont build a walled
  48. 48. IntegrationJanrain RPX Easiest "Social NetworkMultiple Network Registration & Login" service have used. Built and maintained by the Janrain companyTwitter Simple module, not the best UI,It just works but has decent Tweet storage,Imports tweets, allows users totweet when they create nodes. views support, and it just Pluggable API system.ServicesBuild your own API. REST/JSON/XML/ Opens up all basic Drupal functions @thinkdropNYC like user registration, login, update...
  49. 49. Services API Framework for Web Services● Pluggable system for Web Service Endpoints● Allows 3rd Party App developers to interact with your web app.● Lets you provide multiple response formats, authentication types, and more while abstracting the actual API commands.● Most commands simply pass through to respective forms, allowing forms their @thinkdropNYC altering via hook_form_alter()
  50. 50. Questions?
  51. 51. Building Social Networks NYC Camp July 21 2012Jonathan Pugh THINKDROP Pugh on to access these slides and for more information. WERE HIRING