async def share_save(request): user = await get_user(request) comment_id = to_objectid(request.match_info['comment_id']) comment = await comments.get(comment_id) if comment is not None: #TODO: remove deselected users video_id = comment['video_id'] data = await request.post() for key, other_id in data.items(): if key == 'friend': print('adding', other_id) await shares.add( video_id, comment_id, { 'thumbnail': 'https://i.ytimg.com/vi/%s/mqdefault.jpg' % video_id, 'text': comment['text'] }, user['_id'], to_objectid(other_id)) await history.add( user['_id'], 'share-comment', { 'comment_id': comment_id, 'friends': [v for k, v in data.items() if k == 'friend'] }) raise web.HTTPFound('/watch/' + comment['video_id'] + '#' + str(comment['_id'])) raise web.HTTPBadRequest()
async def dismiss_notification(request): user = await get_user(request) notification_id = request.match_info['notification_id'] await notifications.dismiss(user['_id'], to_objectid(notification_id)) await history.add(user['_id'], 'dismiss-notification', {'notification_id': notification_id}) return web.json_response('ok')
async def post_comment(request): user = await get_user(request) video_id = request.match_info['video_id'] data = await request.post() num_shared = len([key for key in data if key == 'friend']) if num_shared == 0: await redirect(request, '/watch/' + video_id, error='Share at least with one friend') if 'text' in data: comment_id = await comments.add(user['_id'], video_id, data['text']) for key, other_id in data.items(): if key == 'friend': #print('adding', other_id) await shares.add( video_id, comment_id, { 'thumbnail': 'https://i.ytimg.com/vi/%s/mqdefault.jpg' % video_id, 'text': data['text'] }, user['_id'], to_objectid(other_id)) video = await videos.get(video_id) await history.add( user['_id'], 'save-comment', { 'video_id': video['_id'], 'youtube_id': video_id, 'comment': data['text'], 'shared-with': [v for k, v in data.items() if key == 'friend'] }) raise web.HTTPFound('/watch/' + video_id + '#' + str(comment_id)) else: raise web.HTTPBadRequest()
async def show_friend(request): user = await get_user(request) friend_id = to_objectid(request.match_info['friend_id']) if await friends.exists(user['_id'], friend_id): friend = await users.get(friend_id) shared_by_myself = await comments.list(owner_id=user['_id'], recipient_id=friend['_id'], populate=True) shared_with_me = await comments.list(recipient_id=user['_id'], owner_id=friend_id, populate=True) all_items = sorted(shared_by_myself + shared_with_me, key=lambda item: item['date'], reverse=True) await history.add(user['_id'], 'show-friend', { 'friend_id': friend_id, 'name': friend['name'] }) return { 'friend': friend, 'nav': 'friends', 'comments': all_items, 'show_video': True } raise web.HTTPBadRequest(reason='Not a friend')
async def remove_playlist(request): user = await get_user(request) folder_id = to_objectid(request.match_info['folder_id']) result = await playlists.delete_folder(user['_id'], folder_id) if result: await redirect(request, '/playlists', info='Removed playlist') await redirect(request, '/playlists', error='Invalid playlist')
async def accept_friend(request): user = await get_user(request) request_id = to_objectid(request.match_info['request_id']) await friends.accept(request_id) request = await friends.get(request_id) await history.add(user['_id'], 'accept-friend-request', {'request': request}) raise web.HTTPFound('/notifications')
async def show_shared(request): user = await get_user(request) friend_id = to_objectid(request.match_info['friend_id']) shared_with_me = await comments.list(recipient_id=user['_id'], owner_id=friend_id, populate=True) await history.add(user['_id'], 'view-shared-by-friend', {'friend': friend_id}) return {'comments': shared_with_me, 'show_video': True, 'nav': 'friends'}
async def debug_login(request): user_id = to_objectid(request.match_info['user_id']) user = await users.get(user_id) if user is not None: session = await new_session(request) session['user_id'] = str(user_id) await history.add(user_id, 'debug:login') raise web.HTTPFound('/') else: return {'error': 'invalid user_id'}
async def unfriend(request): user = await get_user(request) friend_id = to_objectid(request.match_info['friend_id']) if await friends.remove(user['_id'], friend_id): friend = await users.get(friend_id) await history.add(user['_id'], 'unfriend', { 'friend_id': friend['_id'], 'name': friend['name'] }) # TODO: send notificaiton to other user await redirect(request, '/friends', info='Unfriended %s' % friend['name']) raise web.HTTPBadRequest(reason='Not friend')
async def view_friend_request(request): user = await get_user(request) request_id = to_objectid(request.match_info['request_id']) request = await friends.get(request_id) if request is not None and request['other_id'] == user['_id']: friend = await users.get(request['user_id']) await history.add(user['_id'], 'view-friend-request', {'request': request}) return { 'friend': friend, 'request_id': request_id, 'nav': 'notifications' } raise web.HTTPBadRequest()
async def rename_playlist(request): user = await get_user(request) data = await request.post() folder_id = to_objectid(request.match_info['folder_id']) if 'name' in data: if folder_id is not None: await playlists.rename_folder(folder_id, data['name']) await history.add(user['_id'], 'rename-folder', { 'folder_id': folder_id, 'name': data['name'] }) await redirect(request, '/playlists/%s' % folder_id, info='Playlist renamed to "%s"' % data['name']) await redirect(request, '/playlists', error='Cannot rename playlist')
async def show_playlists(request): user = await get_user(request) folder_id = to_objectid(request.match_info['folder_id']) folder = await playlists.get(folder_id) if folder is not None and folder['user_id'] == user['_id']: await history.add(user['_id'], 'show-playlist', { 'folder_id': folder_id, 'name': folder['name'] }) folder['videos'] = await videos.get([ item['video_id'] for item in await playlists.list(user['_id'], folder['_id']) ]) folder['count'] = await playlists.count(folder['_id']) return {'folder': folder, 'nav': 'playlists'} raise web.HTTPBadRequest()
async def share_select(request): user = await get_user(request) comment_id = to_objectid(request.match_info['comment_id']) comment = await comments.get(comment_id) if comment is not None and comment['user_id'] == user['_id']: friend_items = await friends.list(user['_id'], populate=True) initial_share = set([ item['recipient_id'] for item in await shares.list(comment['video_id'], comment_id=comment_id) ]) for item in friend_items: if item['_id'] in initial_share: item['selected'] = True await history.add(user['_id'], 'share-select-friends', {'comment_id': comment_id}) return {'comment': comment, 'friends': friend_items, 'nav': 'search'} raise web.HTTPBadRequest()
async def set_playlist(request): user = await get_user(request) video_id = request.match_info['video_id'] data = await request.post() folder_id = None if 'new_playlist' in data: name = data['new_playlist'] # TODO: differenciate invalid playist and duplicate name folder_id = await playlists.add_folder(user['_id'], name) if folder_id is None: await redirect(request, '/watch/' + video_id, error='Invalid playlist name') elif 'folder' in data: # playlist selected by user folder_id = to_objectid(data['folder']) if folder_id is None: # remove from playlist await playlists.delete(user['_id'], video_id) await redirect(request, '/watch/' + video_id, info='Removed video from playlist') else: # save video in playlist folder = await playlists.get(folder_id) if folder and folder['user_id'] == user['_id']: await playlists.add(user['_id'], folder_id, video_id) video = await videos.get(video_id) await history.add( user['_id'], 'set-playlist', { 'video_id': video['_id'], 'youtube_id': video_id, 'folder_id': folder_id, 'name': folder['name'] }) await redirect(request, '/watch/' + video_id, info='Saved video to "%s"' % folder['name']) raise web.HTTPBadRequest()
async def process_notification(request): user = await get_user(request) notification_id = to_objectid(request.match_info['notification_id']) notification = await notifications.get(notification_id) if notification is not None and notification['user_id'] == user['_id']: await notifications.dismiss(user['_id'], notification_id) await history.add(user['_id'], 'click-notification', {'notification_id': notification_id}) if notification['type'] == 'shared content': share = await shares.get(notification['data']['share_id']) if share is not None and share['recipient_id'] == user['_id']: raise web.HTTPFound('/watch/' + str(share['video_id']) + '#' + str(share['comment_id'])) elif notification['type'] == 'friend request': raise web.HTTPFound('/friend/request/' + str(notification['data']['friend_id'])) elif notification['type'] == 'friend accept': raise web.HTTPFound('/notifications') elif notification['type'] == 'friend removal': raise web.HTTPFound('/notifications') else: raise web.HTTPBadRequest() raise web.HTTPBadRequest()