def undeletemsg(hash): try: m = Message.select().where(Message.block_hash == hash).get() Message.update(hidden = False).where(Message.id == m.id).execute() click.echo(f"Un-hid message: {hash}") except Message.DoesNotExist: click.echo(f"Couldn't find message with has {hash}")
def get_all_messages(): """Load initial messages""" message_response = [] for m in Message.select().where(Message.hidden == False).order_by( Message.id.desc()).limit(2000): message_response.append(Message.format_message_advanced(m)) return jsonify(message_response)
def deletemsg(hash): try: m = Message.select().where(Message.block_hash == hash).get() Message.update(hidden = True).where(Message.id == m.id).execute() emit('delete_message', json.dumps(Message.format_message(m)), namespace='/mtchannel', broadcast=True) click.echo(f"deleted message: {hash}") except Message.DoesNotExist: click.echo(f"Couldn't find message with has {hash}")
def test_message_count(self): rd = redis.Redis() rd.hdel( 'ban_1ph8tfwan1jd91pcettzn8rg448pooin1tz9juhsq1wbhsijebgcho411kum', RD_COUNT_KEY) msg = Message( address= 'ban_1ph8tfwan1jd91pcettzn8rg448pooin1tz9juhsq1wbhsijebgcho411kum') assert (msg.get_message_count() == 0) assert (msg.inc_message_count() == 1) assert (msg.get_message_count() == 1)
def check_missings(): """Browse acount history for items missing in our database""" # Only run this job once/per process, use a distributed lock for that try: lock = rd.lock(lock_key, timeout=300) have_lock = lock.acquire(blocking=False) if have_lock: rpc = RPC() app = scheduler.app with app.app_context(): account = app.config['MONKEYTALKS_ACCOUNT'] resp = rpc.account_history(account, count=200) if resp is None: return # List of hashes to check (links on receive blocks) links = [] linkObjs = [] for item in resp: if item["subtype"] == "receive": linkObj = { "link": item["link"], "amount": item["amount"], "account": item["account"] } links.append(item["link"]) linkObjs.append(linkObj) # Check links we don't have - remove duplicates for item in Message.select().where( Message.block_hash.in_(links)): links.remove(item.block_hash) # Validate, save and emit messages for obj in linkObjs: if obj['link'] not in links: continue if int(obj['amount']) - FeeModel.get_fee() <= 0: continue elif not Nanote().validate_message(obj['amount']): continue # Is a valid message msg = Message.save_as_message(obj['amount'], obj['link'], obj['account'], account) if msg is not None: emit('new_message', json.dumps(Message.format_message(msg)), namespace='/mtchannel', broadcast=True) finally: lock.release()
def test_block_validation(self, app): fee = app.config['MONKEYTALKS_DEFAULT_FEE'] block_contents = {'link_as_account': app.config['MONKEYTALKS_ACCOUNT']} # Test with invalid message (checksum) resp, reason = Message.validate_block({ 'amount': str(fee), 'contents': json.dumps(block_contents) }) assert (resp == False) # Test with valid message resp, reason = Message.validate_block({ 'amount': str(fee + 3085200816947056507), 'contents': json.dumps(block_contents) }) assert (resp == True) # Test with invalid fee resp, reason = Message.validate_block({ 'amount': str(fee + 3085200816947056507 - fee), 'contents': json.dumps(block_contents) }) assert (resp == False) # Test with invalid link block_contents[ 'link_as_account'] = 'ban_1ph8tfwan1jd91pcettzn8rg448pooin1tz9juhsq1wbhsijebgcho411kum' resp, reason = Message.validate_block({ 'amount': str(fee + 3085200816947056507), 'contents': json.dumps(block_contents) }) assert (resp == False)
def banano_callback(): callback_json = request.get_json(silent=True) if callback_json is None: abort(400, 'failed to parse request json') elif 'hash' not in callback_json: abort(400, 'invalid request json') if 'is_send' in callback_json and callback_json['is_send']: rpc = RPC() block = rpc.retrieve_block(callback_json['hash']) if block is None: abort(400, 'block doesn\'t exist') else: block['hash'] = callback_json['hash'] # Validate message is_valid, message = Message.validate_block(block) if not is_valid: abort(400, message) # Save message to database message = Message.save_block_as_message(block) if message is None: abort(500, 'server error processing message') # Emit message to the UI emit_message(message) return ('', 204)
def db(app): """A database for the tests.""" with app.app_context(): Message.create_table(safe=True)
def fixcounts(): rd = redis.Redis() for r in Message.select(fn.COUNT(Message.id).alias("count"), Message.address).where(Message.hidden == False).group_by(Message.address): click.echo(f"Setting count for {r.address} to {r.count}") rd.hset(r.address, RD_COUNT_KEY, str(r.count))
def dbinit(): """Create database tables""" Message.create_table(safe=True) FaucetPayment.create_table(safe=True)
def emit_message(message: Message): """Emit a new chat message to the UI - Broadcasted to all clients""" emit('new_message', json.dumps(Message.format_message(message)), namespace='/mtchannel', broadcast=True)