Database Reference
In-Depth Information
# unfollow a user
def unfollow(self, graph_db, currentusername, username):
query = neo4j.CypherQuery(graph_db,
" MATCH (user1:User {username:{cu}} )-[f:FOLLOWS]->(user2:User
{username:{u}} ) " +
" DELETE f " +
" WITH user1" +
" MATCH (user1)-[f:FOLLOWS]->(users)" +
" RETURN users.firstname as firstname, users.lastname as lastname, "+
" users.username as username " +
" ORDER BY users.username")
params = {"cu": currentusername, "u": username}
result = query.execute(**params)
return result
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. Second, 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 you 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 9-28. The get_content Method in Content Class
def get_content(self, graph_db, username, skip):
query = neo4j.CypherQuery(graph_db,
" 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 ")
 
Search WWH ::




Custom Search