-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuserpage.py
137 lines (113 loc) · 4.39 KB
/
userpage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import asyncio
import logging
import os
from urllib.parse import parse_qsl
from urllib.parse import urlencode
from urllib.parse import urljoin
from urllib.parse import urlparse
from urllib.parse import urlunparse
from aiohttp import web
import aiohttp_jinja2
import aiohttp_session
from aiohttp_session.cookie_storage import EncryptedCookieStorage
import aiomysql
import aiozipkin as az
from login import require_login
from models.post import Post
from models.user import User
logger = logging.getLogger(__name__)
@require_login
@aiohttp_jinja2.template('userpage.jinja2')
async def handle_userpage(request: web.Request):
current_user_uid = request.match_info.get('uid')
# TODO: можно сделать страницы по логинам /userpage/<login>/
# username = None
# if uid and uid.isdigit():
# username = None
# else:
# username = uid
session = await aiohttp_session.get_session(request)
uid = session["uid"]
if not current_user_uid:
current_user_uid = uid
pool: aiomysql.pool.Pool = request.app['db_ro_pool']
async def get_friends(user):
async with pool.acquire() as conn:
return await user.get_friends(conn=conn)
async def get_subscribers(user):
async with pool.acquire() as conn:
return await user.get_subscribers(conn=conn)
async def get_user():
async with pool.acquire() as conn:
return await User.get_by_id(uid=current_user_uid, conn=conn)
async def get_posts(user):
async with pool.acquire() as conn:
return await Post.filter(filter=dict(author_id=user.id), conn=conn)
u = User(uid=current_user_uid)
user, friends, subscribers, posts = await asyncio.gather(
get_user(),
get_friends(user=u),
get_subscribers(user=u),
get_posts(user=u)
)
counters_url = os.getenv('COUNTERS_URL')
new_counters_url = None
if counters_url:
storage: EncryptedCookieStorage = request.get(aiohttp_session.STORAGE_KEY)
user_session = storage.load_cookie(request)
new_counters_url = update_url(urljoin(counters_url, 'get_counters/'), dict(
userId=uid,
friends=','.join(map(lambda friend: str(friend['id']), friends)),
session=user_session
))
return dict(current_user=user, session=session, friends=friends,
subscribers=subscribers, posts=posts, uid=uid,
chat_url=os.getenv('CHAT_URL'),
counters_url=new_counters_url)
def update_url(url, params):
url_parse = urlparse(url)
query = url_parse.query
url_dict = dict(parse_qsl(query))
url_dict.update(params)
url_new_query = urlencode(url_dict)
url_parse = url_parse._replace(query=url_new_query)
new_url = urlunparse(url_parse)
return new_url
async def chat_api_get_key(session, user_id, friend_id):
chat_rest_url = os.getenv('CHAT_REST_URL') + 'make_chat/'
if not chat_rest_url:
return None
# async with aiohttp.ClientSession() as session:
resp = await session.post(chat_rest_url,
json={'user_id': user_id,
'friend_id': friend_id})
logger.debug('Chat rest response: %r', resp.status)
res = await resp.json()
return res.get('chat_key')
@require_login
@aiohttp_jinja2.template('chat.jinja2')
async def handle_chat(request: web.Request):
chat_url = os.getenv('CHAT_URL')
if not chat_url:
raise web.HTTPBadRequest(reason='CHAT_URL env not set')
session = await aiohttp_session.get_session(request)
uid = session["uid"]
form = await request.post()
friend_id = form.get('user_id')
client_session = request.app['client_session']
tracer = az.get_tracer(request.app)
span = az.request_span(request)
with tracer.new_child(span.context) as child_span:
child_span.name("chat_api_get_key")
child_span.tag('user_id', uid)
child_span.tag('friend_id', friend_id)
chat_key = await chat_api_get_key(client_session, user_id=uid, friend_id=friend_id)
child_span.tag('chat_key', chat_key)
storage: EncryptedCookieStorage = request.get(aiohttp_session.STORAGE_KEY)
chat_session = storage.load_cookie(request)
new_chat_url = update_url(chat_url, dict(
chat_key=chat_key,
session=chat_session,
userId=uid
))
return dict(uid=uid, chat_url=new_chat_url)