Permalink
Browse files

Add async methods to the relationship api endpoint

  • Loading branch information...
yabirgb committed Feb 3, 2019
1 parent 5835a74 commit 9a51b2b1f1df8d7016aae97759adabfc7bec40b0
Showing with 16 additions and 3 deletions.
  1. +4 −3 src/api/v1/user.py
  2. +12 −0 src/managers/user_manager.py
@@ -329,14 +329,15 @@ class FetchFollowing(BaseHandler):

class Relationship(BaseHandler):



@bearerAuth
async def get(self, user):
target_id = self.get_argument('id', None)

if target_id:
try:
target = await self.application.objects.get(UserProfile, id=target_id)
User
data = {
'id': target_id,
}
@@ -346,8 +347,8 @@ class Relationship(BaseHandler):
manager = UserManager(user)
target_manager = UserManager(target)

data['following'] = manager.is_following(target)
data['followed_by'] = target_manager.is_following(user)
data['following'] = await manager.is_following_async(self.application.objects, target)
data['followed_by'] = await target_manager.is_following_async(self.application.objects, user)

self.write(data)
self.set_status(200)
@@ -61,6 +61,18 @@ def is_following(self, target: UserProfile):
# TODO: store this on redis
return self.user.is_following(target)

async def is_following_async(self, app, target):
from models.followers import FollowerRelation

count = await app.count(FollowerRelation
.select()
.where(
(FollowerRelation.user == self.user) &
(FollowerRelation.follows == target))
)

return count > 0

def valid_username(username):
regex = r'[\w\d_.]+$'
return re.match(regex, username) != None

0 comments on commit 9a51b2b

Please sign in to comment.