Mercurial > libervia-backend
comparison src/memory/sqlite.py @ 448:17c7e48bf68f
core: - history management improved
- better timestamp precision for history
bridge + core: history signature change (now return a list instead of a dict, and timestamp is now float)
quick_frontend: - use of new history API
- removed deprecated keep_last argument in getHistory (was only used by Sortilège)
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 04 Dec 2011 16:18:56 +0100 |
parents | a6640bb8ee95 |
children | cf005701624b |
comparison
equal
deleted
inserted
replaced
447:485a6d125498 | 448:17c7e48bf68f |
---|---|
176 @param message: message | 176 @param message: message |
177 @param timestamp: timestamp in seconds since epoch, or None to use current time | 177 @param timestamp: timestamp in seconds since epoch, or None to use current time |
178 """ | 178 """ |
179 assert(profile!=None) | 179 assert(profile!=None) |
180 d = self.dbpool.runQuery("INSERT INTO history(source, source_res, dest, dest_res, timestamp, message, profile_id) VALUES (?,?,?,?,?,?,?)", | 180 d = self.dbpool.runQuery("INSERT INTO history(source, source_res, dest, dest_res, timestamp, message, profile_id) VALUES (?,?,?,?,?,?,?)", |
181 (from_jid.userhost(), from_jid.resource, to_jid.userhost(), to_jid.resource, timestamp or int(time.time()), | 181 (from_jid.userhost(), from_jid.resource, to_jid.userhost(), to_jid.resource, timestamp or time.time(), |
182 message, self.profiles[profile])) | 182 message, self.profiles[profile])) |
183 d.addErrback(lambda ignore: error(_("Can't save following message in history: from [%(from_jid)s] to [%(to_jid)s] ==> [%(message)s]" % | 183 d.addErrback(lambda ignore: error(_("Can't save following message in history: from [%(from_jid)s] to [%(to_jid)s] ==> [%(message)s]" % |
184 {"from_jid":from_jid.full(), "to_jid":to_jid.full(), "message":message}))) | 184 {"from_jid":from_jid.full(), "to_jid":to_jid.full(), "message":message}))) |
185 return d | 185 return d |
186 | 186 |
188 """Store a new message in history | 188 """Store a new message in history |
189 @param from_jid: source JID (full, or bare for catchall | 189 @param from_jid: source JID (full, or bare for catchall |
190 @param to_jid: dest JID (full, or bare for catchall | 190 @param to_jid: dest JID (full, or bare for catchall |
191 @param size: maximum number of messages to get, or 0 for unlimited | 191 @param size: maximum number of messages to get, or 0 for unlimited |
192 """ | 192 """ |
193 def sqliteToDict(result): | 193 def sqliteToDict(query_result): |
194 result_dict = {} | 194 query_result.reverse() |
195 for row in result: | 195 result = [] |
196 for row in query_result: | |
196 timestamp, source, source_res, dest, dest_res, message = row | 197 timestamp, source, source_res, dest, dest_res, message = row |
197 result_dict[timestamp] = ("%s/%s" % (source, source_res) if source_res else source, | 198 result.append((timestamp, "%s/%s" % (source, source_res) if source_res else source, |
198 "%s/%s" % (dest, dest_res) if dest_res else dest, | 199 "%s/%s" % (dest, dest_res) if dest_res else dest, |
199 message) | 200 message)) |
200 return result_dict | 201 return result |
201 | 202 |
202 query_parts = ["SELECT timestamp, source, source_res, dest, dest_res, message FROM history WHERE"] | 203 query_parts = ["SELECT timestamp, source, source_res, dest, dest_res, message FROM history WHERE"] |
203 values = [] | 204 values = [] |
204 | 205 |
205 if between: | 206 if between: |
212 query_parts.append("AND source_res=?") | 213 query_parts.append("AND source_res=?") |
213 values.append(from_jid.resource) | 214 values.append(from_jid.resource) |
214 if to_jid.resource: | 215 if to_jid.resource: |
215 query_parts.append("AND dest_res=?") | 216 query_parts.append("AND dest_res=?") |
216 values.append(to_jid.resource) | 217 values.append(to_jid.resource) |
218 | |
219 query_parts.append("ORDER BY timestamp DESC") | |
220 | |
217 if limit: | 221 if limit: |
218 query_parts.append("LIMIT ?") | 222 query_parts.append("LIMIT ?") |
219 values.append(limit) | 223 values.append(limit) |
220 d = self.dbpool.runQuery(" ".join(query_parts), values) | 224 d = self.dbpool.runQuery(" ".join(query_parts), values) |
221 return d.addCallback(sqliteToDict) | 225 return d.addCallback(sqliteToDict) |