##// END OF EJS Templates
docs: don't use deprecated header for channelstream views
marcink -
r2164:c5a68adf default
parent child Browse files
Show More
@@ -1,176 +1,167 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2010-2017 RhodeCode GmbH
4 4 #
5 5 # This program is free software: you can redistribute it and/or modify
6 6 # it under the terms of the GNU Affero General Public License, version 3
7 7 # (only), as published by the Free Software Foundation.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU Affero General Public License
15 15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 16 #
17 17 # This program is dual-licensed. If you wish to learn more about the
18 18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20 20
21 """
22 Channel Stream controller for rhodecode
23
24 :created_on: Oct 10, 2015
25 :author: marcinl
26 :copyright: (c) 2013-2015 RhodeCode GmbH.
27 :license: Commercial License, see LICENSE for more details.
28 """
29
30 21 import logging
31 22 import uuid
32 23
33 24 from pyramid.view import view_config
34 25 from pyramid.httpexceptions import HTTPBadRequest, HTTPForbidden, HTTPBadGateway
35 26
36 27 from rhodecode.lib.channelstream import (
37 28 channelstream_request,
38 29 ChannelstreamConnectionException,
39 30 ChannelstreamPermissionException,
40 31 check_channel_permissions,
41 32 get_connection_validators,
42 33 get_user_data,
43 34 parse_channels_info,
44 35 update_history_from_logs,
45 36 STATE_PUBLIC_KEYS)
46 37 from rhodecode.lib.auth import NotAnonymous
47 38
48 39 log = logging.getLogger(__name__)
49 40
50 41
51 42 class ChannelstreamView(object):
52 43 def __init__(self, context, request):
53 44 self.context = context
54 45 self.request = request
55 46
56 47 # Some of the decorators rely on this attribute to be present
57 48 # on the class of the decorated method.
58 49 self._rhodecode_user = request.user
59 50 registry = request.registry
60 51 self.channelstream_config = registry.rhodecode_plugins['channelstream']
61 52 if not self.channelstream_config.get('enabled'):
62 53 log.error('Channelstream plugin is disabled')
63 54 raise HTTPBadRequest()
64 55
65 56 @NotAnonymous()
66 57 @view_config(route_name='channelstream_connect', renderer='json')
67 58 def connect(self):
68 59 """ handle authorization of users trying to connect """
69 60 try:
70 61 json_body = self.request.json_body
71 62 except Exception:
72 63 log.exception('Failed to decode json from request')
73 64 raise HTTPBadRequest()
74 65
75 66 try:
76 67 channels = check_channel_permissions(
77 68 json_body.get('channels'),
78 69 get_connection_validators(self.request.registry))
79 70 except ChannelstreamPermissionException:
80 71 log.error('Incorrect permissions for requested channels')
81 72 raise HTTPForbidden()
82 73
83 74 user = self._rhodecode_user
84 75 if user.user_id:
85 76 user_data = get_user_data(user.user_id)
86 77 else:
87 78 user_data = {
88 79 'id': None,
89 80 'username': None,
90 81 'first_name': None,
91 82 'last_name': None,
92 83 'icon_link': None,
93 84 'display_name': None,
94 85 'display_link': None,
95 86 }
96 87 user_data['permissions'] = self._rhodecode_user.permissions_safe
97 88 payload = {
98 89 'username': user.username,
99 90 'user_state': user_data,
100 91 'conn_id': str(uuid.uuid4()),
101 92 'channels': channels,
102 93 'channel_configs': {},
103 94 'state_public_keys': STATE_PUBLIC_KEYS,
104 95 'info': {
105 96 'exclude_channels': ['broadcast']
106 97 }
107 98 }
108 99 filtered_channels = [channel for channel in channels
109 100 if channel != 'broadcast']
110 101 for channel in filtered_channels:
111 102 payload['channel_configs'][channel] = {
112 103 'notify_presence': True,
113 104 'history_size': 100,
114 105 'store_history': True,
115 106 'broadcast_presence_with_user_lists': True
116 107 }
117 108 # connect user to server
118 109 try:
119 110 connect_result = channelstream_request(self.channelstream_config,
120 111 payload, '/connect')
121 112 except ChannelstreamConnectionException:
122 113 log.exception('Channelstream service is down')
123 114 return HTTPBadGateway()
124 115
125 116 connect_result['channels'] = channels
126 117 connect_result['channels_info'] = parse_channels_info(
127 118 connect_result['channels_info'],
128 119 include_channel_info=filtered_channels)
129 120 update_history_from_logs(self.channelstream_config,
130 121 filtered_channels, connect_result)
131 122 return connect_result
132 123
133 124 @NotAnonymous()
134 125 @view_config(route_name='channelstream_subscribe', renderer='json')
135 126 def subscribe(self):
136 127 """ can be used to subscribe specific connection to other channels """
137 128 try:
138 129 json_body = self.request.json_body
139 130 except Exception:
140 131 log.exception('Failed to decode json from request')
141 132 raise HTTPBadRequest()
142 133 try:
143 134 channels = check_channel_permissions(
144 135 json_body.get('channels'),
145 136 get_connection_validators(self.request.registry))
146 137 except ChannelstreamPermissionException:
147 138 log.error('Incorrect permissions for requested channels')
148 139 raise HTTPForbidden()
149 140 payload = {'conn_id': json_body.get('conn_id', ''),
150 141 'channels': channels,
151 142 'channel_configs': {},
152 143 'info': {
153 144 'exclude_channels': ['broadcast']}
154 145 }
155 146 filtered_channels = [chan for chan in channels if chan != 'broadcast']
156 147 for channel in filtered_channels:
157 148 payload['channel_configs'][channel] = {
158 149 'notify_presence': True,
159 150 'history_size': 100,
160 151 'store_history': True,
161 152 'broadcast_presence_with_user_lists': True
162 153 }
163 154 try:
164 155 connect_result = channelstream_request(
165 156 self.channelstream_config, payload, '/subscribe')
166 157 except ChannelstreamConnectionException:
167 158 log.exception('Channelstream service is down')
168 159 return HTTPBadGateway()
169 160 # include_channel_info will limit history only to new channel
170 161 # to not overwrite histories on other channels in client
171 162 connect_result['channels_info'] = parse_channels_info(
172 163 connect_result['channels_info'],
173 164 include_channel_info=filtered_channels)
174 165 update_history_from_logs(self.channelstream_config,
175 166 filtered_channels, connect_result)
176 167 return connect_result
General Comments 0
You need to be logged in to leave comments. Login now