Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
/r/friends: use friends that posted most recently
Browse files Browse the repository at this point in the history
This takes advantage of the new last_submit_time and last_comment_time
timestamps on the Account objects to determine which friends have posted
most recently.

Ideally the ability to sort on data columns would have been added into
the general _query() function, but that seems to be fairly complex, so
this was a lot simpler of a method for addressing this specific problem.
  • Loading branch information
Deimos committed Oct 1, 2015
1 parent 22f566b commit e7adea1
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 10 deletions.
34 changes: 34 additions & 0 deletions r2/r2/lib/db/tdb_sql.py
Expand Up @@ -928,6 +928,40 @@ def find_data(type_id, get_cols, sort, limit, offset, constraints):
return Results(r, lambda(row): row if get_cols else row.thing_id)


def sort_thing_ids_by_data_value(type_id, thing_ids, value_name,
limit=None, desc=False):
"""Order thing_ids by the value of a data column."""

thing_table, data_table = get_thing_table(type_id)

join = thing_table.join(data_table,
data_table.c.thing_id == thing_table.c.thing_id)

query = (sa.select(
[thing_table.c.thing_id],
sa.and_(
thing_table.c.thing_id.in_(thing_ids),
thing_table.c.deleted == False,
thing_table.c.spam == False,
data_table.c.key == value_name,
)
)
.select_from(join)
)

sort_column = data_table.c.value
if desc:
sort_column = sa.desc(sort_column)
query = query.order_by(sort_column)

if limit:
query = query.limit(limit)

rows = query.execute()

return Results(rows, lambda(row): row.thing_id)


def find_rels(rel_type_id, get_cols, sort, limit, offset, constraints):
tables = get_rel_table(rel_type_id)
r_table, t1_table, t2_table, d_table = tables
Expand Down
6 changes: 6 additions & 0 deletions r2/r2/lib/db/thing.py
Expand Up @@ -652,6 +652,12 @@ def _query(cls, *all_rules, **kw):

return Things(cls, *rules, **kw)

@classmethod
def sort_ids_by_data_value(cls, thing_ids, value_name,
limit=None, desc=False):
return tdb.sort_thing_ids_by_data_value(
cls._type_id, thing_ids, value_name, limit, desc)

def update_search_index(self, boost_only=False):
msg = {'fullname': self._fullname}
if boost_only:
Expand Down
25 changes: 18 additions & 7 deletions r2/r2/models/account.py
Expand Up @@ -452,13 +452,24 @@ def add_friend_note(self, friend, note):
rel.note = note
rel._commit()

@memoize("get_random_friends", time=30*60)
def get_random_friends(self, limit=100):
friends = self.friend_ids()
if len(friends) > limit:
friends = random.sample(friends, limit)

return friends
def _get_friend_ids_by(self, data_value_name, limit):
friend_ids = self.friend_ids()
if len(friend_ids) <= limit:
return friend_ids

with g.stats.get_timer("friends_query.%s" % data_value_name):
result = self.sort_ids_by_data_value(
friend_ids, data_value_name, limit=limit, desc=True)

return result.fetchall()

@memoize("get_recently_submitted_friend_ids", time=10*60)
def get_recently_submitted_friend_ids(self, limit=100):
return self._get_friend_ids_by("last_submit_time", limit)

@memoize("get_recently_commented_friend_ids", time=10*60)
def get_recently_commented_friend_ids(self, limit=100):
return self._get_friend_ids_by("last_comment_time", limit)

def delete(self, delete_message=None):
self.delete_message = delete_message
Expand Down
6 changes: 3 additions & 3 deletions r2/r2/models/subreddit.py
Expand Up @@ -1547,7 +1547,7 @@ class FriendsSR(FakeSubreddit):
def get_links(self, sort, time):
from r2.lib.db import queries

friends = c.user.get_random_friends()
friends = c.user.get_recently_submitted_friend_ids()
if not friends:
return []

Expand All @@ -1567,7 +1567,7 @@ def get_links(self, sort, time):
def get_all_comments(self):
from r2.lib.db import queries

friends = c.user.get_random_friends()
friends = c.user.get_recently_commented_friend_ids()
if not friends:
return []

Expand All @@ -1588,7 +1588,7 @@ def get_all_comments(self):
def get_gilded(self):
from r2.lib.db.queries import get_gilded_users

friends = c.user.get_random_friends()
friends = c.user.friend_ids()

if not friends:
return []
Expand Down

0 comments on commit e7adea1

Please sign in to comment.