Ejemplo n.º 1
0
 def connect(self):
     """handle authorization of users trying to connect/reconnect"""
     channels = self.request.json_body.get("channels") or []
     random_name = "anon_%s" % random.randint(1, 999999)
     username = self.request.json_body.get("username", random_name)
     state = self.request.json_body.get("state", {})
     payload = {
         "username": username,
         # where user should be subscribed
         "channels": channels,
         # what default state should be set when user is created on channelstream end
         "fresh_user_state": {
             "email": None,
             "status": None,
             "private": "is private",
             "bar": 1,
             "bool": True,
         },
         # update state to this values if user object already exists
         "user_state": state,
         # what state keys should be visible to other users
         "state_public_keys": ["email", "status", "bar", "color"],
         # return only public state in response
         "info": {
             "return_public_state": True
         },
         # set chanel configurations if channels don't exist yet
         "channel_configs": CHANNEL_CONFIGS,
     }
     result = make_server_request(self.request, payload, "/connect")
     self.request.response.status = result.status_code
     server_response = result.json()
     # add a demo message when user connects after a while
     gevent.spawn_later(2, send_welcome_message, self.request, username)
     return server_response
Ejemplo n.º 2
0
 def connect(self):
     """handle authorization of users trying to connect/reconnect"""
     channels = self.request.json_body.get("channels") or []
     random_name = "anon_%s" % random.randint(1, 999999)
     username = self.request.json_body.get("username", random_name)
     state = self.request.json_body.get("state", {})
     payload = {
         "username": username,
         # where user should be subscribed
         "channels": channels,
         # what default state should be set when user is created on channelstream end
         "fresh_user_state": {
             "email": None,
             "status": None,
             "private": "is private",
             "bar": 1,
             "bool": True,
         },
         # update state to this values if user object already exists
         "user_state": state,
         # what state keys should be visible to other users
         "state_public_keys": ["email", "status", "bar", "color"],
         # return only public state in response
         "info": {"return_public_state": True},
         # set chanel configurations if channels don't exist yet
         "channel_configs": CHANNEL_CONFIGS,
     }
     result = make_server_request(self.request, payload, "/connect")
     self.request.response.status = result.status_code
     server_response = result.json()
     # add a demo message when user connects after a while
     gevent.spawn_later(5, send_welcome_message, self.request, username)
     return server_response
Ejemplo n.º 3
0
 def message_delete(self):
     payload = {
         "uuid": self.request.json_body["uuid"],
         "channel": self.request.json_body["channel"],
     }
     result = make_server_request(self.request, [payload],
                                  "/message",
                                  method="delete")
     self.request.response.status = result.status_code
     return result.json()
Ejemplo n.º 4
0
 def message_delete(self):
     payload = {
         "uuid": self.request.json_body["uuid"],
         "channel": self.request.json_body["channel"],
     }
     result = make_server_request(
         self.request, [payload], "/message", method="delete"
     )
     self.request.response.status = result.status_code
     return result.json()
Ejemplo n.º 5
0
 def info(self):
     """gets information for the "admin" demo page"""
     admin = "admin"
     admin_secret = self.request.registry.settings["admin_secret"]
     basic_auth = HTTPBasicAuth(admin, admin_secret)
     result = make_server_request(self.request, {},
                                  "/admin/admin.json",
                                  auth=basic_auth)
     self.request.response.status = result.status_code
     return result.json()
Ejemplo n.º 6
0
 def unsubscribe(self):
     """"can be used to unsubscribe specific connection to other channels"""
     request_data = self.request.json_body
     payload = {
         "conn_id": request_data.get("conn_id", ""),
         "channels": request_data.get("channels", []),
     }
     result = make_server_request(self.request, payload, "/unsubscribe")
     self.request.response.status = result.status_code
     server_response = result.json()
     return server_response
Ejemplo n.º 7
0
 def unsubscribe(self):
     """"can be used to unsubscribe specific connection to other channels"""
     request_data = self.request.json_body
     payload = {
         "conn_id": request_data.get("conn_id", ""),
         "channels": request_data.get("channels", []),
     }
     result = make_server_request(self.request, payload, "/unsubscribe")
     self.request.response.status = result.status_code
     server_response = result.json()
     return server_response
Ejemplo n.º 8
0
 def message_post(self):
     """send message to channel/users"""
     request_data = self.request.json_body
     payload = {
         "type": "message",
         "user": request_data.get("user", "system"),
         "channel": request_data.get("channel", "unknown_channel"),
         "message": request_data.get("message"),
     }
     result = make_server_request(self.request, [payload], "/message")
     self.request.response.status = result.status_code
     return result.json()
Ejemplo n.º 9
0
 def message_post(self):
     """send message to channel/users"""
     request_data = self.request.json_body
     payload = {
         "type": "message",
         "user": request_data.get("user", "system"),
         "channel": request_data.get("channel", "unknown_channel"),
         "message": request_data.get("message"),
     }
     result = make_server_request(self.request, [payload], "/message")
     self.request.response.status = result.status_code
     return result.json()
Ejemplo n.º 10
0
 def user_state(self):
     """"can be used to subscribe specific connection to other channels"""
     request_data = self.request.json_body
     payload = {
         "user": request_data["username"],
         "user_state": {
             "color": request_data["update_state"]["user_state"]["color"],
             "private": "private {}".format(random.randint(1, 99999)),
         },
     }
     result = make_server_request(self.request, payload, "/user_state")
     self.request.response.status = result.status_code
     return result.json()
Ejemplo n.º 11
0
 def user_state(self):
     """"can be used to subscribe specific connection to other channels"""
     request_data = self.request.json_body
     payload = {
         "user": request_data["username"],
         "user_state": {
             "color": request_data["update_state"]["user_state"]["color"],
             "private": "private {}".format(random.randint(1, 99999)),
         },
     }
     result = make_server_request(self.request, payload, "/user_state")
     self.request.response.status = result.status_code
     return result.json()
Ejemplo n.º 12
0
 def info(self):
     """gets information for the "admin" demo page"""
     result = make_server_request(self.request, {}, "/admin/admin.json")
     self.request.response.status = result.status_code
     return result.json()
Ejemplo n.º 13
0
 def info(self):
     """gets information for the "admin" demo page"""
     result = make_server_request(self.request, {}, "/admin/admin.json")
     self.request.response.status = result.status_code
     return result.json()