def get_by_recipient(self, recipient): global dbpool def _translate(tx, recipient, out): qlist = [ 'SELECT `id`, `timestamp`, `content`, `expire_timestamp` FROM stanzas_%s WHERE `recipient` = ?' % (t, ) for t in self.tables ] qargs = [recipient.user for t in self.tables] tx.execute( 'SELECT * FROM (' + ' UNION '.join(qlist) + ') a ORDER BY `timestamp`', qargs) data = tx.fetchall() for row in data: stanzaId = str(row[0]) d = { 'id': stanzaId, 'timestamp': datetime.datetime.utcfromtimestamp(row[1] / 1e3), 'expire': datetime.datetime.utcfromtimestamp(row[3]) if row[3] else None } d['stanza'] = generic.parseXml( row[2].decode('utf-8').encode('utf-8')) """ Add a <storage/> element to the stanza; this way components have a way to know if stanza is coming from storage. """ stor = d['stanza'].addElement( (xmlstream2.NS_XMPP_STORAGE, 'storage')) stor['id'] = stanzaId out.append(d) return out # include any pending message? out = [] for stanzaId, pend in self._pending_offline.iteritems(): delayed, stanza, unused = pend if util.jid_user(stanza['to']) == recipient.user: stanza.consumed = False out.append({'id': stanzaId, 'stanza': stanza}) # reset delayed timer delayed.reset(self.OFFLINE_STORE_DELAY) return dbpool.runInteraction(_translate, recipient, out)
def get_by_recipient(self, recipient): global dbpool def _translate(tx, recipient, out): qlist = [ "SELECT `id`, `timestamp`, `content`, `expire_timestamp` FROM stanzas_%s WHERE `recipient` = ?" % (t,) for t in self.tables ] qargs = [recipient.user for t in self.tables] tx.execute("SELECT * FROM (" + " UNION ".join(qlist) + ") a ORDER BY `timestamp`", qargs) data = tx.fetchall() for row in data: stanzaId = str(row[0]) d = { "id": stanzaId, "timestamp": datetime.datetime.utcfromtimestamp(row[1] / 1e3), "expire": datetime.datetime.utcfromtimestamp(row[3]) if row[3] else None, } d["stanza"] = generic.parseXml(row[2].decode("utf-8").encode("utf-8")) """ Add a <storage/> element to the stanza; this way components have a way to know if stanza is coming from storage. """ stor = d["stanza"].addElement((xmlstream2.NS_XMPP_STORAGE, "storage")) stor["id"] = stanzaId out.append(d) return out # include any pending message? out = [] for stanzaId, pend in self._pending_offline.iteritems(): delayed, stanza, unused = pend if util.jid_user(stanza["to"]) == recipient.user: stanza.consumed = False out.append({"id": stanzaId, "stanza": stanza}) # reset delayed timer delayed.reset(self.OFFLINE_STORE_DELAY) return dbpool.runInteraction(_translate, recipient, out)
def presence(self, stanza): global dbpool userid = util.jid_user(stanza['from']) def encode_not_empty(val): if val is not None: data = val.__str__().encode('utf-8') if len(data) > 0: return base64.b64encode(val.__str__().encode('utf-8')) return None try: priority = int(stanza.priority.__str__()) except: priority = 0 status = encode_not_empty(stanza.status) show = encode_not_empty(stanza.show) values = (userid, status, show, priority, status, show, priority) return dbpool.runOperation('INSERT INTO presence (`userid`, `timestamp`, `status`, `show`, `priority`) VALUES(?, UTC_TIMESTAMP(), ?, ?, ?) ON DUPLICATE KEY UPDATE `timestamp` = UTC_TIMESTAMP(), `status` = ?, `show` = ?, `priority` = ?', values)