forked from rDrama/rDrama
1
0
Fork 0

Include message text in off-site mentions

master
Marco Rebhan 2022-06-20 23:01:42 +02:00 committed by TLSM
parent 59180db450
commit 3c5e2c9455
1 changed files with 14 additions and 6 deletions

View File

@ -6,6 +6,7 @@ import files.helpers.const as const
from files.classes.user import User from files.classes.user import User
from files.classes.comment import Comment from files.classes.comment import Comment
from files.classes.notifications import Notification from files.classes.notifications import Notification
from files.helpers.sanitize import sanitize
# https://api.pushshift.io/meta provides key server_ratelimit_per_minute # https://api.pushshift.io/meta provides key server_ratelimit_per_minute
# At time of writing, the ratelimit is 120 req/min. We get nowhere near this # At time of writing, the ratelimit is 120 req/min. We get nowhere near this
@ -38,23 +39,30 @@ def get_mentions(queries):
+ f'?html_decode=true&q={query}&size=1', timeout=5).json()['data'] + f'?html_decode=true&q={query}&size=1', timeout=5).json()['data']
except: break except: break
for i in data: for i in data:
# Special case: PokemonGoRaids says 'Marsey' a lot unrelated to us. # Special case: PokemonGoRaids says 'Marsey' a lot unrelated to us.
if i['subreddit'] == 'PokemonGoRaids': continue if i['subreddit'] == 'PokemonGoRaids': continue
mentions.append(i['permalink']) mentions.append({
'permalink': i['permalink'],
'text': i['body' if kind == 'comment' else 'title'],
})
return mentions return mentions
def notify_mentions(send_to, mentions, mention_str='site mention'): def notify_mentions(send_to, mentions, mention_str='site mention'):
for m in mentions: for m in mentions:
notif_text = f'<p>New {mention_str}: <a href="https://old.reddit.com{m}' \ permalink = m['permalink']
text = sanitize(m['text'])
notif_text = \
f'<p>New {mention_str}: <a href="https://old.reddit.com{permalink}' \
f'?context=89" rel="nofollow noopener noreferrer" target="_blank">' \ f'?context=89" rel="nofollow noopener noreferrer" target="_blank">' \
f'https://old.reddit.com{m}?context=89</a></p>' f'https://old.reddit.com{permalink}?context=89</a></p>' \
f'<blockquote>{text}</blockquote>'
existing_comment = g.db.query(Comment.id).filter_by( existing_comment = g.db.query(Comment.id).filter_by(
author_id=const.NOTIFICATIONS_ID, author_id=const.NOTIFICATIONS_ID,
parent_submission=None, parent_submission=None,
body_html=notif_text).one_or_none() body_html=notif_text).one_or_none()
if existing_comment: continue if existing_comment: continue