Show More
@@ -1,185 +1,185 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2010-2020 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 | 21 | import logging |
|
22 | 22 | import uuid |
|
23 | 23 | |
|
24 | 24 | |
|
25 | 25 | from pyramid.httpexceptions import HTTPBadRequest, HTTPForbidden, HTTPBadGateway |
|
26 | 26 | |
|
27 | 27 | from rhodecode.apps._base import BaseAppView |
|
28 | 28 | from rhodecode.lib.channelstream import ( |
|
29 | 29 | channelstream_request, get_channelstream_server_url, |
|
30 | 30 | ChannelstreamConnectionException, |
|
31 | 31 | ChannelstreamPermissionException, |
|
32 | 32 | check_channel_permissions, |
|
33 | 33 | get_connection_validators, |
|
34 | 34 | get_user_data, |
|
35 | 35 | parse_channels_info, |
|
36 | 36 | update_history_from_logs, |
|
37 | 37 | USER_STATE_PUBLIC_KEYS) |
|
38 | 38 | |
|
39 | 39 | from rhodecode.lib.auth import NotAnonymous |
|
40 | 40 | |
|
41 | 41 | log = logging.getLogger(__name__) |
|
42 | 42 | |
|
43 | 43 | |
|
44 | 44 | class ChannelstreamView(BaseAppView): |
|
45 | 45 | |
|
46 | 46 | def load_default_context(self): |
|
47 | 47 | c = self._get_local_tmpl_context() |
|
48 | 48 | self.channelstream_config = \ |
|
49 | 49 | self.request.registry.rhodecode_plugins['channelstream'] |
|
50 | 50 | if not self.channelstream_config.get('enabled'): |
|
51 |
log. |
|
|
51 | log.warning('Channelstream plugin is disabled') | |
|
52 | 52 | raise HTTPBadRequest() |
|
53 | 53 | |
|
54 | 54 | return c |
|
55 | 55 | |
|
56 | 56 | @NotAnonymous() |
|
57 | 57 | def channelstream_connect(self): |
|
58 | 58 | """ handle authorization of users trying to connect """ |
|
59 | 59 | |
|
60 | 60 | self.load_default_context() |
|
61 | 61 | try: |
|
62 | 62 | json_body = self.request.json_body |
|
63 | 63 | except Exception: |
|
64 | 64 | log.exception('Failed to decode json from request') |
|
65 | 65 | raise HTTPBadRequest() |
|
66 | 66 | |
|
67 | 67 | try: |
|
68 | 68 | channels = check_channel_permissions( |
|
69 | 69 | json_body.get('channels'), |
|
70 | 70 | get_connection_validators(self.request.registry)) |
|
71 | 71 | except ChannelstreamPermissionException: |
|
72 | 72 | log.error('Incorrect permissions for requested channels') |
|
73 | 73 | raise HTTPForbidden() |
|
74 | 74 | |
|
75 | 75 | user = self._rhodecode_user |
|
76 | 76 | if user.user_id: |
|
77 | 77 | user_data = get_user_data(user.user_id) |
|
78 | 78 | else: |
|
79 | 79 | user_data = { |
|
80 | 80 | 'id': None, |
|
81 | 81 | 'username': None, |
|
82 | 82 | 'first_name': None, |
|
83 | 83 | 'last_name': None, |
|
84 | 84 | 'icon_link': None, |
|
85 | 85 | 'display_name': None, |
|
86 | 86 | 'display_link': None, |
|
87 | 87 | } |
|
88 | 88 | |
|
89 | 89 | #user_data['permissions'] = self._rhodecode_user.permissions_safe |
|
90 | 90 | |
|
91 | 91 | payload = { |
|
92 | 92 | 'username': user.username, |
|
93 | 93 | 'user_state': user_data, |
|
94 | 94 | 'conn_id': str(uuid.uuid4()), |
|
95 | 95 | 'channels': channels, |
|
96 | 96 | 'channel_configs': {}, |
|
97 | 97 | 'state_public_keys': USER_STATE_PUBLIC_KEYS, |
|
98 | 98 | 'info': { |
|
99 | 99 | 'exclude_channels': ['broadcast'] |
|
100 | 100 | } |
|
101 | 101 | } |
|
102 | 102 | filtered_channels = [channel for channel in channels |
|
103 | 103 | if channel != 'broadcast'] |
|
104 | 104 | for channel in filtered_channels: |
|
105 | 105 | payload['channel_configs'][channel] = { |
|
106 | 106 | 'notify_presence': True, |
|
107 | 107 | 'history_size': 100, |
|
108 | 108 | 'store_history': True, |
|
109 | 109 | 'broadcast_presence_with_user_lists': True |
|
110 | 110 | } |
|
111 | 111 | # connect user to server |
|
112 | 112 | channelstream_url = get_channelstream_server_url( |
|
113 | 113 | self.channelstream_config, '/connect') |
|
114 | 114 | try: |
|
115 | 115 | connect_result = channelstream_request( |
|
116 | 116 | self.channelstream_config, payload, '/connect') |
|
117 | 117 | except ChannelstreamConnectionException: |
|
118 | 118 | log.exception( |
|
119 | 119 | 'Channelstream service at {} is down'.format(channelstream_url)) |
|
120 | 120 | return HTTPBadGateway() |
|
121 | 121 | |
|
122 | 122 | channel_info = connect_result.get('channels_info') |
|
123 | 123 | if not channel_info: |
|
124 | 124 | raise HTTPBadRequest() |
|
125 | 125 | |
|
126 | 126 | connect_result['channels'] = channels |
|
127 | 127 | connect_result['channels_info'] = parse_channels_info( |
|
128 | 128 | channel_info, include_channel_info=filtered_channels) |
|
129 | 129 | update_history_from_logs(self.channelstream_config, |
|
130 | 130 | filtered_channels, connect_result) |
|
131 | 131 | return connect_result |
|
132 | 132 | |
|
133 | 133 | @NotAnonymous() |
|
134 | 134 | def channelstream_subscribe(self): |
|
135 | 135 | """ can be used to subscribe specific connection to other channels """ |
|
136 | 136 | self.load_default_context() |
|
137 | 137 | try: |
|
138 | 138 | json_body = self.request.json_body |
|
139 | 139 | except Exception: |
|
140 | 140 | log.exception('Failed to decode json from request') |
|
141 | 141 | raise HTTPBadRequest() |
|
142 | 142 | try: |
|
143 | 143 | channels = check_channel_permissions( |
|
144 | 144 | json_body.get('channels'), |
|
145 | 145 | get_connection_validators(self.request.registry)) |
|
146 | 146 | except ChannelstreamPermissionException: |
|
147 | 147 | log.error('Incorrect permissions for requested channels') |
|
148 | 148 | raise HTTPForbidden() |
|
149 | 149 | payload = {'conn_id': json_body.get('conn_id', ''), |
|
150 | 150 | 'channels': channels, |
|
151 | 151 | 'channel_configs': {}, |
|
152 | 152 | 'info': { |
|
153 | 153 | 'exclude_channels': ['broadcast']} |
|
154 | 154 | } |
|
155 | 155 | filtered_channels = [chan for chan in channels if chan != 'broadcast'] |
|
156 | 156 | for channel in filtered_channels: |
|
157 | 157 | payload['channel_configs'][channel] = { |
|
158 | 158 | 'notify_presence': True, |
|
159 | 159 | 'history_size': 100, |
|
160 | 160 | 'store_history': True, |
|
161 | 161 | 'broadcast_presence_with_user_lists': True |
|
162 | 162 | } |
|
163 | 163 | |
|
164 | 164 | channelstream_url = get_channelstream_server_url( |
|
165 | 165 | self.channelstream_config, '/subscribe') |
|
166 | 166 | try: |
|
167 | 167 | connect_result = channelstream_request( |
|
168 | 168 | self.channelstream_config, payload, '/subscribe') |
|
169 | 169 | except ChannelstreamConnectionException: |
|
170 | 170 | log.exception( |
|
171 | 171 | 'Channelstream service at {} is down'.format(channelstream_url)) |
|
172 | 172 | return HTTPBadGateway() |
|
173 | 173 | |
|
174 | 174 | channel_info = connect_result.get('channels_info') |
|
175 | 175 | if not channel_info: |
|
176 | 176 | raise HTTPBadRequest() |
|
177 | 177 | |
|
178 | 178 | # include_channel_info will limit history only to new channel |
|
179 | 179 | # to not overwrite histories on other channels in client |
|
180 | 180 | connect_result['channels_info'] = parse_channels_info( |
|
181 | 181 | channel_info, |
|
182 | 182 | include_channel_info=filtered_channels) |
|
183 | 183 | update_history_from_logs( |
|
184 | 184 | self.channelstream_config, filtered_channels, connect_result) |
|
185 | 185 | return connect_result |
General Comments 0
You need to be logged in to leave comments.
Login now