##// END OF EJS Templates
maintainance: added update caches to mercurial.
maintainance: added update caches to mercurial.

File last commit:

r3363:f08e98b1 default
r3928:739550ba default
Show More
views.py
177 lines | 6.8 KiB | text/x-python | PythonLexer
channelstream: move into apps
r1504 # -*- coding: utf-8 -*-
docs: updated copyrights to 2019
r3363 # Copyright (C) 2010-2019 RhodeCode GmbH
channelstream: move into apps
r1504 #
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License, version 3
# (only), as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# This program is dual-licensed. If you wish to learn more about the
# RhodeCode Enterprise Edition, including its added features, Support services,
# and proprietary license terms, please see https://rhodecode.com/licenses/
import logging
import uuid
from pyramid.view import view_config
channelstream: use pyramid http exception instead of webob....
r2163 from pyramid.httpexceptions import HTTPBadRequest, HTTPForbidden, HTTPBadGateway
channelstream: move into apps
r1504
pylons: remove pylons as dependency...
r2351 from rhodecode.apps._base import BaseAppView
channelstream: move into apps
r1504 from rhodecode.lib.channelstream import (
channelstream: nicer handle of connection errors for debug.
r3169 channelstream_request, get_channelstream_server_url,
channelstream: move into apps
r1504 ChannelstreamConnectionException,
ChannelstreamPermissionException,
check_channel_permissions,
get_connection_validators,
get_user_data,
parse_channels_info,
update_history_from_logs,
STATE_PUBLIC_KEYS)
pylons: remove pylons as dependency...
r2351
channelstream: move into apps
r1504 from rhodecode.lib.auth import NotAnonymous
log = logging.getLogger(__name__)
pylons: remove pylons as dependency...
r2351 class ChannelstreamView(BaseAppView):
channelstream: move into apps
r1504
pylons: remove pylons as dependency...
r2351 def load_default_context(self):
c = self._get_local_tmpl_context()
self.channelstream_config = \
self.request.registry.rhodecode_plugins['channelstream']
channelstream: move into apps
r1504 if not self.channelstream_config.get('enabled'):
log.error('Channelstream plugin is disabled')
raise HTTPBadRequest()
pylons: remove pylons as dependency...
r2351 return c
channelstream: move into apps
r1504 @NotAnonymous()
pylons: remove pylons as dependency...
r2351 @view_config(route_name='channelstream_connect', renderer='json_ext')
channelstream: move into apps
r1504 def connect(self):
channelstream: nicer handle of connection errors for debug.
r3169 """ handle authorization of users trying to connect """
pylons: remove pylons as dependency...
r2351 self.load_default_context()
channelstream: move into apps
r1504 try:
json_body = self.request.json_body
except Exception:
log.exception('Failed to decode json from request')
raise HTTPBadRequest()
auth: don't expose full set of permissions into channelstream payload....
r2157
channelstream: move into apps
r1504 try:
channels = check_channel_permissions(
json_body.get('channels'),
get_connection_validators(self.request.registry))
except ChannelstreamPermissionException:
log.error('Incorrect permissions for requested channels')
raise HTTPForbidden()
pyramid: removed pylons reference from apps....
r1782 user = self._rhodecode_user
channelstream: move into apps
r1504 if user.user_id:
user_data = get_user_data(user.user_id)
else:
user_data = {
'id': None,
'username': None,
'first_name': None,
'last_name': None,
'icon_link': None,
'display_name': None,
'display_link': None,
}
auth: don't expose full set of permissions into channelstream payload....
r2157 user_data['permissions'] = self._rhodecode_user.permissions_safe
channelstream: move into apps
r1504 payload = {
'username': user.username,
'user_state': user_data,
'conn_id': str(uuid.uuid4()),
'channels': channels,
'channel_configs': {},
'state_public_keys': STATE_PUBLIC_KEYS,
'info': {
'exclude_channels': ['broadcast']
}
}
filtered_channels = [channel for channel in channels
if channel != 'broadcast']
for channel in filtered_channels:
payload['channel_configs'][channel] = {
'notify_presence': True,
'history_size': 100,
'store_history': True,
'broadcast_presence_with_user_lists': True
}
# connect user to server
channelstream: nicer handle of connection errors for debug.
r3169 channelstream_url = get_channelstream_server_url(
self.channelstream_config, '/connect')
channelstream: move into apps
r1504 try:
channelstream: nicer handle of connection errors for debug.
r3169 connect_result = channelstream_request(
self.channelstream_config, payload, '/connect')
channelstream: move into apps
r1504 except ChannelstreamConnectionException:
channelstream: nicer handle of connection errors for debug.
r3169 log.exception(
'Channelstream service at {} is down'.format(channelstream_url))
channelstream: move into apps
r1504 return HTTPBadGateway()
connect_result['channels'] = channels
connect_result['channels_info'] = parse_channels_info(
connect_result['channels_info'],
include_channel_info=filtered_channels)
update_history_from_logs(self.channelstream_config,
filtered_channels, connect_result)
return connect_result
@NotAnonymous()
pylons: remove pylons as dependency...
r2351 @view_config(route_name='channelstream_subscribe', renderer='json_ext')
channelstream: move into apps
r1504 def subscribe(self):
""" can be used to subscribe specific connection to other channels """
pylons: remove pylons as dependency...
r2351 self.load_default_context()
channelstream: move into apps
r1504 try:
json_body = self.request.json_body
except Exception:
log.exception('Failed to decode json from request')
raise HTTPBadRequest()
try:
channels = check_channel_permissions(
json_body.get('channels'),
get_connection_validators(self.request.registry))
except ChannelstreamPermissionException:
log.error('Incorrect permissions for requested channels')
raise HTTPForbidden()
payload = {'conn_id': json_body.get('conn_id', ''),
'channels': channels,
'channel_configs': {},
'info': {
'exclude_channels': ['broadcast']}
}
filtered_channels = [chan for chan in channels if chan != 'broadcast']
for channel in filtered_channels:
payload['channel_configs'][channel] = {
'notify_presence': True,
'history_size': 100,
'store_history': True,
'broadcast_presence_with_user_lists': True
}
channelstream: nicer handle of connection errors for debug.
r3169
channelstream_url = get_channelstream_server_url(
self.channelstream_config, '/subscribe')
channelstream: move into apps
r1504 try:
connect_result = channelstream_request(
self.channelstream_config, payload, '/subscribe')
except ChannelstreamConnectionException:
channelstream: nicer handle of connection errors for debug.
r3169 log.exception(
'Channelstream service at {} is down'.format(channelstream_url))
channelstream: move into apps
r1504 return HTTPBadGateway()
# include_channel_info will limit history only to new channel
# to not overwrite histories on other channels in client
connect_result['channels_info'] = parse_channels_info(
connect_result['channels_info'],
include_channel_info=filtered_channels)
channelstream: nicer handle of connection errors for debug.
r3169 update_history_from_logs(
self.channelstream_config, filtered_channels, connect_result)
channelstream: move into apps
r1504 return connect_result