Skip to content

Commit 25c4cf9

Browse files
committed
Switch history.py to use only sqlalchemy queries
1 parent 494405d commit 25c4cf9

1 file changed

Lines changed: 17 additions & 14 deletions

File tree

plugins/history.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import time
44
from collections import deque
55

6-
from sqlalchemy import Table, Column, String, PrimaryKeyConstraint, Float
6+
from sqlalchemy import Table, Column, String, PrimaryKeyConstraint, Float, select
77

88
from cloudbot import hook
99
from cloudbot.event import EventType
@@ -27,11 +27,19 @@ def track_seen(event, db):
2727
:type db: sqlalchemy.orm.Session
2828
"""
2929
# keep private messages private
30+
now = time.time()
3031
if event.chan[:1] == "#" and not re.findall('^s/.*/.*/$', event.content.lower()):
31-
db.execute(
32-
"insert or replace into seen_user(name, time, quote, chan, host) values(:name,:time,:quote,:chan,:host)",
33-
{'name': event.nick.lower(), 'time': time.time(), 'quote': event.content, 'chan': event.chan,
34-
'host': event.mask})
32+
res = db.execute(
33+
table.update().values(time=now, quote=event.content, host=str(event.mask))
34+
.where(table.c.name == event.nick.lower()).where(table.c.chan == event.chan)
35+
)
36+
if res.rowcount == 0:
37+
db.execute(
38+
table.insert().values(
39+
name=event.nick.lower(), time=now, quote=event.content, chan=event.chan, host=str(event.mask)
40+
)
41+
)
42+
3543
db.commit()
3644

3745

@@ -98,18 +106,13 @@ def seen(text, nick, chan, db, event, is_nick_valid):
98106
if not is_nick_valid(text):
99107
return "I can't look up that name, its impossible to use!"
100108

101-
if '_' in text:
102-
text = text.replace("_", "/_")
103-
104-
last_seen = db.execute("SELECT name, time, quote FROM seen_user WHERE name LIKE :name ESCAPE '/' AND chan = :chan",
105-
{'name': text, 'chan': chan}).fetchone()
106-
107-
text = text.replace("/", "")
109+
last_seen = db.execute(
110+
select([table.c.name, table.c.time, table.c.quote])
111+
.where(table.c.name == text.lower()).where(table.c.chan == chan)
112+
).fetchone()
108113

109114
if last_seen:
110115
reltime = timeformat.time_since(last_seen[1])
111-
if last_seen[0] != text.lower(): # for glob matching
112-
text = last_seen[0]
113116
if last_seen[2][0:1] == "\x01":
114117
return '{} was last seen {} ago: * {} {}'.format(text, reltime, text, last_seen[2][8:-1])
115118
else:

0 commit comments

Comments
 (0)