Skip to content

Commit

Permalink
Ignore URLs in event descriptions when identifying changes (fixes #72).
Browse files Browse the repository at this point in the history
  • Loading branch information
nmlorg committed Aug 8, 2019
1 parent 22c645d commit a9911df
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 17 deletions.
34 changes: 19 additions & 15 deletions metabot/modules/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,23 +92,23 @@ def _daily_messages(multibot, records): # pylint: disable=too-many-branches,too
edits.append(title(lastevent))
edits.append(' \u2022 Removed.')
continue
pairs = (
(lastevent['summary'], event['summary']),
(humanize_range(lastevent['start'], lastevent['end'], tzinfo),
humanize_range(event['start'], event['end'], tzinfo)),
(lastevent['location'], event['location']),
(html.sanitize(lastevent['description'], strip=True),
html.sanitize(event['description'], strip=True)),
) # yapf: disable
pieces = []
if event['summary'] != lastevent['summary']:
pieces.append((lastevent['summary'], event['summary']))
if event['start'] != lastevent['start'] or event['end'] != lastevent['end']:
pieces.append((humanize_range(lastevent['start'], lastevent['end'], tzinfo),
humanize_range(event['start'], event['end'], tzinfo)))
if event['location'] != lastevent['location']:
pieces.append((lastevent['location'], event['location']))
curdesc = html.sanitize(event['description'], strip=True)
lastdesc = html.sanitize(lastevent['description'], strip=True)
if curdesc != lastdesc:
pieces.append((lastdesc, curdesc))
for left, right in pairs:
diff = _quick_diff(left, right)
if diff:
pieces.append(diff)
if pieces:
edits.append(title(event))
for left, right in pieces:
edits.append(' \u2022 <i>%s</i> \u2192 <b>%s</b>' %
_quick_diff(left, right))
edits.append(' \u2022 <i>%s</i> \u2192 <b>%s</b>' % (left, right))

if not edits:
continue
Expand Down Expand Up @@ -139,8 +139,12 @@ def _daily_messages(multibot, records): # pylint: disable=too-many-branches,too


def _quick_diff(left, right):
left = re.sub(r'\s+', ' ', left)
right = re.sub(r'\s+', ' ', right)
left = re.sub(r'https?://\S+', '', left)
left = re.sub(r'\s+', ' ', left).strip()
right = re.sub(r'https?://\S+', '', right)
right = re.sub(r'\s+', ' ', right).strip()
if left == right:
return
i = 0
while i < len(left) and i < len(right) and right[i] == left[i]:
i += 1
Expand Down
27 changes: 26 additions & 1 deletion metabot/modules/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,31 @@ def test_daily_messages(conversation, monkeypatch): # pylint: disable=redefined
"""
replies.clear()

cal.events['6fc2c510:alpha']['description'] = (
'Multi\n\nLine\nDescription https://www.example.com/?id=1234')

events._daily_messages(conversation.multibot, records) # pylint: disable=protected-access
assert records == {
('modulestestbot', '-1002000002000'): (0, [{
'description': 'Multi\n\nLine\nDescription',
'end': 2060,
'local_id': '6fc2c510:alpha',
'location': 'Alpha Venue, Rest of Alpha Location',
'start': 1000,
'summary': 'Edited Summary',
}, {
'description': 'New Description',
'end': 3000,
'local_id': '6fc2c510:new',
'location': 'New Venue, Rest of New Location',
'start': 2000,
'summary': 'New Summary',
}], {
'message_id': 12345,
}),
}
assert conversation.format_messages(replies) == ''

cal.events['6fc2c510:alpha']['updated'] = 23456

events._daily_messages(conversation.multibot, records) # pylint: disable=protected-access
Expand Down Expand Up @@ -735,7 +760,7 @@ def test_quick_diff():
"""Test description string differ."""

# pylint: disable=protected-access
assert events._quick_diff('', '') == ('', '')
assert events._quick_diff('', '') is None
assert events._quick_diff('0987654321', '0987654321new') == ('0987654321', '0987654321new')
assert events._quick_diff('10987654321', '10987654321new') == ('…987654321', '…987654321new')
assert events._quick_diff('10987654321old', '10987654321') == ('…987654321old', '…987654321')
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setuptools.setup(
name='metabot',
version='0.3.12.2',
version='0.3.13',
author='Daniel Reed',
author_email='[email protected]',
description='Modularized, multi-account bot.',
Expand Down

0 comments on commit a9911df

Please sign in to comment.