##// END OF EJS Templates
caches: use repo.lru based Dict cache. This LRUDict uses Timing Algo to not have to use locking...
caches: use repo.lru based Dict cache. This LRUDict uses Timing Algo to not have to use locking for the LRU implementation, this it's safer to use for dogpile. We used it before with beaker, so it's generally more stable.

File last commit:

r2487:fcee5614 default
r2945:ec5716e4 default
Show More
views.py
169 lines | 6.5 KiB | text/x-python | PythonLexer
channelstream: move into apps
r1504 # -*- coding: utf-8 -*-
release: update copyright year to 2018
r2487 # Copyright (C) 2010-2018 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_request,
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):
pylons: remove pylons as dependency...
r2351 self.load_default_context()
channelstream: move into apps
r1504 """ handle authorization of users trying to connect """
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
try:
connect_result = channelstream_request(self.channelstream_config,
payload, '/connect')
except ChannelstreamConnectionException:
log.exception('Channelstream service is down')
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
}
try:
connect_result = channelstream_request(
self.channelstream_config, payload, '/subscribe')
except ChannelstreamConnectionException:
log.exception('Channelstream service is down')
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)
update_history_from_logs(self.channelstream_config,
filtered_channels, connect_result)
return connect_result