Database Reference
In-Depth Information
▪ The
posts
within a month are actually stored in chronological order, so the order of these
posts must be reversed before displaying.
▪ As a user pages through her wall, it's preferable to avoid fetching the first few months
from the server each time. To achieve this, the preceding code specifies the first month to
fetch in the
month
argument, passing this in as an
$lte
expression in the query. This can
be substantially faster than using a
.skip()
argument to our
.find()
.
▪ Rather than only yielding the post itself, the post's month is also yielded from the gener-
ator. This provides the
month
argument to be used in any subsequent calls to
get_posts
.
There is one other issue that needs to be considered in selecting posts for display: filtering
posts for display. In order to choose posts for display, we'll need to use some filter functions
on the posts generated by
get_posts
. The first of these filters is used to determine whether to
show a post when the user is viewing his or her own wall:
def
def
visible_on_own_wall
(
user
,
post
):
'''if poster is followed by user, post is visible'''
for
for
circle
,
users
iin
user
[
'circles'
]
.
items
():
iif
post
[
'by'
][
'id'
] iin
users
:
return
return
True
return
return
False
In addition to the user's wall, our social network provides an “incoming” page that contains
all posts directed toward a user regardless of whether that poster is followed by the user. In
this case, we need to use the block list to filter posts:
def
def
visible_on_own_incoming
(
user
,
post
):
'''if poster is not blocked by user, post is visible'''
return
return
post
[
'by'
][
'id'
] not
not iin
user
[
'blocked'
]
When viewing a news feed or another user's wall, the permission check is a bit different based
on the post's
circles
property:
def
def
visible_post
(
user
,
post
):
iif
post
[
'circles'
]
==
[
'*public*'
]:
# public posts always visible
return
return
True
circles_user_is_in
=
set
(
user
[
'followers'
]
.
get
(
post
[
'by'
][
'id'
], []))