'Args:
user_id(str): The recipient user_id.
device_id(str): The recipient device_id.
current_stream_id(int): The current position of the to device
message stream.
Returns:
Deferred ([dict], int): List of messages for the device and where
in the stream the messages got to.'

def get_new_messages_for_device(self, user_id, device_id, last_stream_id, current_stream_id, limit=100):

    has_changed = self._device_inbox_stream_cache.has_entity_changed(user_id, last_stream_id)
    if (not has_changed):
        return defer.succeed(([], current_stream_id))
    def get_new_messages_for_device_txn(txn):
        sql = 'SELECT    stream_id,    message_json    FROM    device_inbox    WHERE    user_id    =    ?    AND    device_id    =    ?    AND    ?    <    stream_id    AND    stream_id    <=    ?    ORDER    BY    stream_id    ASC    LIMIT    ?'
        txn.execute(sql, (user_id, device_id, last_stream_id, current_stream_id, limit))
        messages = []
        for row in txn:
            stream_pos = row[0]
            messages.append(ujson.loads(row[1]))
        if (len(messages) < limit):
            stream_pos = current_stream_id
        return (messages, stream_pos)
    return self.runInteraction('get_new_messages_for_device', get_new_messages_for_device_txn)
