Database Reference
In-Depth Information
User-Generated Content
Another important feature in social media applications is being able to have users view, add, edit, and remove
content—sometimes referred to as user-generated content . In the case of this content, you will not be creating
connections between the content and its owner but creating a linked list of status updates. In other words, you are
connecting a User to their most recent status update and then connecting each subsequent status to the next update
through the CURRENTPOST and NEXTPOST directed relationship types, respectively.
This approach is used for two reasons. First, the sample application displays a given number of posts at a time,
and using a limited linked list is more efficient than getting all status updates connected directly to a user and then
sorting and limiting the number of items to return. In addition, it also helps to limit the number of relationships that
are placed on the User and Content entities. Therefore, the overall graph operations should be made more efficient by
using the linked list approach.
Getting the Status Updates
To display the first set of status updates start with the social route of the social section of grapstory.py. This method
accesses the get_content method within Content service class, which takes an argument of the current user's
username and the page being requested. The page refers to set number of objects within a collection. In this instance,
the paging is zero-based, and so we will request page 0 and limit the page size to 4 in order to return the first page.
The get_content method in Content class shown in Listing 9-28 will first determine whom the user is following and
then match that set of user with the status updates starting with the CURRENTPOST . The CURRENTPOST is then matched on
the next three status updates via the [:NEXTPOST*0..3] section of the query. Finally, the method uses a loop to add a
readable date and time string property—based on the timestamp—on the results returned to the controller and view.
Listing 10-31. The get_content Method in the Content Service Class
def get_content(neo,username,skip)
cypher = " MATCH (u:User {username: {u} })-[:FOLLOWS*0..1]->f "+
" WITH DISTINCT f,u "+
" MATCH f-[:CURRENTPOST]-lp-[:NEXTPOST*0..3]-p "+
" RETURN p.contentId as contentId, p.title as title, "+
" p.tagstr as tagstr, p.timestamp as timestamp, "+
" p.url as url, f.username as username, f=u as owner "+
" ORDER BY p.timestamp desc SKIP {s} LIMIT 4 "
results=neo.execute_query(cypher, {:u => username, :s => skip} )
r=results["data"].map {|row| Hash[*results["columns"].zip(row).flatten] }
r.each do |e|
#convert the timestamp to readable date and time
e.merge!("timestampAsStr" => Time.at(e["timestamp"]).strftime("%m/%d/%Y") +
" at " +
Time.at(e["timestamp"]).strftime("%l:%M %p"))
end
r
end
Adding a Status Update
The page shown in Figure 10-10 shows the form to add a status update for the current user, which is displayed when clicking
on the “Add Content” link just under the “Graph Story - Social Feed” header. The HTML for the form can be found in
{PROJECTROOT}/app/views/graphs/social/posts.mustache . The form uses the add_content function in graphstory.js
to POST a new status update as well as return the response and add it to the top of the status update stream.
 
Search WWH ::




Custom Search