Example #1
0
	def onApiRequest(self):
		id = self.param(settings.CHATROOM_ID_PARAM)
		token = self.param(settings.TOKEN_PARAM)
		name = self.param(settings.CHATROOM_NAME_PARAM, "", settings.ROOM_NAME_CHARACTER_LIMIT, True)
		participants = 0
		chatroom = None
		
		if stringutils.isNotEmpty(id):
			chatroom = chatroomservice.getChatroom(id)
			if chatroom is None: # check for bad IDs
				id = None
		if stringutils.isEmpty(id):
			id = chatroomservice.getIdFromUrl(self.request.referrer)
		
		if stringutils.isNotEmpty(id):
			chatroom = chatroomservice.getChatroom(id)
			if chatroom is None:
				chatroom = Chatroom(id, name)
				chatroomservice.cacheAndEnqueueSave(chatroom)
			elif len(name) > 0 and (name != chatroom.getName()):
				chatroom.setName(name)
				chatroomservice.cacheAndEnqueueSave(chatroom)
			if channelservice.isValidToken(id, token):
				participants = channelservice.activateToken(id, token)
			else:
				token, participants = channelservice.createToken(id)
			channelservice.updateParticipantCount(id, token)
			self.sendApiResponse({
				'token': token,
				'chatroom': chatroom.asLiteral(),
				'participants': participants,
				'settings': settings.getClientSettings()
			})
		else:
			self.sendApiError('invalid or missing referrer', 401)
Example #2
0
    def onApiRequest(self):
		id = self.param(settings.CHATROOM_ID_PARAM)
		if stringutils.isNotEmpty(id):
			chatroom = chatroomservice.getChatroom(id)
			if chatroom is not None:
				token = self.param(settings.TOKEN_PARAM)
				if stringutils.isNotEmpty(token):
					if channelservice.isValidToken(id, token, active=True):
						chat = self.param(settings.CHAT_MESSAGE_PARAM, "",
							settings.MESSAGE_CHARACTER_LIMIT, sanitize=True)
						alias = self.param(settings.CHAT_ALIAS_PARAM, settings.DEFAULT_CHAT_ALIAS,
							settings.ALIAS_CHARACTER_LIMIT, sanitize=True)
						message = Message(alias, chat)
						chatroom.addMessage(message)
						chatroomservice.cacheAndEnqueueSave(chatroom)
						status = channelservice.sendMessage(id, token, message)
						self.sendApiResponse(status)
					else:
						self.sendApiError('expired token', 401, {'expired':True})
				else:
					self.sendApiError('missing access token', 401)
			else:
				self.sendApiError('chatroom does not exist', 404)
		else:
			self.sendApiError('missing chatroom id', 401)