'Insert a receipt, either from local client or remote server.
Automatically does conversion between linearized and graph
representations.'

@defer.inlineCallbacks
def insert_receipt(self, room_id, receipt_type, user_id, event_ids, data):

    if (not event_ids):
        return
    if (len(event_ids) == 1):
        linearized_event_id = event_ids[0]
    else:
        def graph_to_linear(txn):
            query = ('SELECT    event_id    WHERE    room_id    =    ?    AND    stream_ordering    IN    (    SELECT    max(stream_ordering)    WHERE    event_id    IN    (%s))' % ','.join((['?'] * len(event_ids))))
            txn.execute(query, ([room_id] + event_ids))
            rows = txn.fetchall()
            if rows:
                return rows[0][0]
            else:
                raise RuntimeError(('Unrecognized    event_ids:    %r' % (event_ids,)))
        linearized_event_id = (yield self.runInteraction('insert_receipt_conv', graph_to_linear))
    stream_id_manager = self._receipts_id_gen.get_next()
    with stream_id_manager as stream_id:
        have_persisted = (yield self.runInteraction('insert_linearized_receipt', self.insert_linearized_receipt_txn, room_id, receipt_type, user_id, linearized_event_id, data, stream_id=stream_id))
        if (not have_persisted):
            defer.returnValue(None)
    (yield self.insert_graph_receipt(room_id, receipt_type, user_id, event_ids, data))
    max_persisted_id = self._receipts_id_gen.get_current_token()
    defer.returnValue((stream_id, max_persisted_id))
