def change_score(self): assessed_profile = self.get_assessed_profile() issuer_profile = self.get_profile() if assessed_profile == issuer_profile: raise exceptions.SocialCreditError( 'Ranking your own messages is not allowed') # TODO put message_id as a top-level param in Transaction, # bind it together with issuer as an UniqueTogether, # then remove this check if ProfileTransaction.objects( message__message_id=self.message.reply_to_message.message_id, issuer=issuer_profile).first(): raise exceptions.SocialCreditError( 'You have already ranked this message') transaction = self.TRANSACTIONS[self.message.sticker.emoji] assessed_profile.change_score( score_delta=transaction.amount, issuer=issuer_profile, message=self.message.reply_to_message.json, ) reaction = transaction.message_template self.send_reaction(reaction, str_format={ 'username': f'@{assessed_profile.tg_username}' if assessed_profile.tg_username else assessed_profile.tg_full_name, 'score': assessed_profile.current_score, })
def validate_param_count(handler, param_count): # TODO maybe something smarter, than just wrapper functions w/ param_count? command_params = handler.message.text.split(' ')[1:] if len(command_params) < param_count: raise exceptions.SocialCreditError( 'Invalid command parameters. Use /social_credit_admin_help for details.' )
def validate_profile_exist(handler): chat_id = handler.message.chat.id user_id = handler.message.from_user.id try: ChatUserProfile.objects(chat=chat_id, tg_user_id=user_id).get() except ChatUserProfile.DoesNotExist: raise exceptions.SocialCreditError( 'You are not enrolled in Social Credit system')
def validate_user_is_admin(handler): user_id = handler.message.from_user.id chat_id = handler.message.chat.id admin_ids = map(lambda admin: admin.user.id, handler.bot.get_chat_administrators(chat_id)) if user_id not in admin_ids: raise exceptions.SocialCreditError( 'You need to be a Chat Admin to perform this action')
def set_chat_option(self): option, value = self.message.text.split(' ')[1:] try: self.chat.set_option(option, value) except mongoengine.ValidationError as e: error_message = e._format_errors().split(':')[0] # TODO refine translation process of those kind of exceptions # (where template is translated first, and then formatting is applied). # Handler logic shouldn't worry about translations raise exceptions.SocialCreditError( 'Invalid value for option {option}: {error_message}'.format( option=option, error_message=error_message), ) except mongoengine.FieldDoesNotExist: raise exceptions.SocialCreditError( 'Unknown option {option}'.format(option=option)) self.send_system('Option {option} set to {value}', str_format={ 'option': option, 'value': value })
def set_plugin_option(self): plugin, option, value = self.message.text.split(' ')[1:] try: setattr(self.chat.plugin_options.get(plugin_name=plugin), option, value) self.chat.plugin_options.save() except mongoengine.DoesNotExist: # TODO looks like this is never raised for EmbeddedDocuments, # need to investigate and somehow implement manual check raise exceptions.SocialCreditError('Invalid option') except mongoengine.ValidationError as e: error_message = e._format_errors().split(':')[0] raise exceptions.SocialCreditError(error_message) self.send_system( '{plugin} plugin option {option} is set to {value}', str_format={ 'plugin': plugin, 'option': option, 'value': value }, )
def _get_wrapper(self, model, query, error_message): try: return model.objects.get(**query) except model.DoesNotExist: raise exceptions.SocialCreditError(error_message)
def validate_plugin_exists(handler): plugin = handler.message.text.split(' ')[1] plugin_list = list(get_plugin_list()) if plugin not in plugin_list: raise exceptions.SocialCreditError('Unknown plugin.')
def validate_chat_option(handler): command_params = handler.message.text.split(' ')[1:] if len(command_params) < 2: raise exceptions.SocialCreditError( 'Invalid command parameters. Use /social_credit_admin_help for details.' )
def validate_chat_not_exist(handler): if handler.chat: raise exceptions.SocialCreditError( 'Chat is already registered in Social Credit system')
def validate_profile_does_not_exist(handler): chat_id = handler.message.chat.id user_id = handler.message.from_user.id if ChatUserProfile.objects(chat=chat_id, tg_user_id=user_id).first(): raise exceptions.SocialCreditError( 'You are already enrolled in Social Credit system')