##// END OF EJS Templates
fix(repo): fixed empty config case
fix(repo): fixed empty config case

File last commit:

r5095:aa627a5f default
r5251:711a4878 default
Show More
views.py
183 lines | 6.8 KiB | text/x-python | PythonLexer
copyrights: updated for 2023
r5088 # Copyright (C) 2010-2023 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
application: not use config.scan(), and replace all @add_view decorator into a explicit add_view call for faster app start.
r4610
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,
channelstream: bumped to channelstream==0.6.14
r4479 USER_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'):
channelstream: disabled channelstream no longer produces an error message.
r4787 log.warning('Channelstream plugin is disabled')
channelstream: move into apps
r1504 raise HTTPBadRequest()
pylons: remove pylons as dependency...
r2351 return c
channelstream: move into apps
r1504 @NotAnonymous()
application: not use config.scan(), and replace all @add_view decorator into a explicit add_view call for faster app start.
r4610 def channelstream_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,
}
channelstream: bumped to channelstream==0.6.14
r4479
#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': {},
channelstream: bumped to channelstream==0.6.14
r4479 'state_public_keys': USER_STATE_PUBLIC_KEYS,
channelstream: move into apps
r1504 '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(
modernize: updates for python3
r5095 f'Channelstream service at {channelstream_url} is down')
channelstream: move into apps
r1504 return HTTPBadGateway()
channelstream: bumped to channelstream==0.6.14
r4479 channel_info = connect_result.get('channels_info')
if not channel_info:
raise HTTPBadRequest()
channelstream: move into apps
r1504 connect_result['channels'] = channels
connect_result['channels_info'] = parse_channels_info(
channelstream: bumped to channelstream==0.6.14
r4479 channel_info, include_channel_info=filtered_channels)
channelstream: move into apps
r1504 update_history_from_logs(self.channelstream_config,
filtered_channels, connect_result)
return connect_result
@NotAnonymous()
application: not use config.scan(), and replace all @add_view decorator into a explicit add_view call for faster app start.
r4610 def channelstream_subscribe(self):
channelstream: move into apps
r1504 """ 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(
modernize: updates for python3
r5095 f'Channelstream service at {channelstream_url} is down')
channelstream: move into apps
r1504 return HTTPBadGateway()
channelstream: bumped to channelstream==0.6.14
r4479
channel_info = connect_result.get('channels_info')
if not channel_info:
raise HTTPBadRequest()
channelstream: move into apps
r1504 # 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(
channelstream: bumped to channelstream==0.6.14
r4479 channel_info,
channelstream: move into apps
r1504 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