big refactor + respond to mentions
This commit is contained in:
		
							parent
							
								
									7273f71132
								
							
						
					
					
						commit
						73e5b2cc85
					
				| 
						 | 
					@ -179,34 +179,14 @@ async def update_followers(c, me):
 | 
				
			||||||
	else:
 | 
						else:
 | 
				
			||||||
		log('No accounts to unfollow.')
 | 
							log('No accounts to unfollow.')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async def listen(c, me):
 | 
					async def handle_status(c, status):
 | 
				
			||||||
	log('Listening...')
 | 
						# Ignore all reblogs
 | 
				
			||||||
	async with c.streaming('user') as stream:
 | 
						if status.get('reblog'): return
 | 
				
			||||||
		async for msg in stream:
 | 
					 | 
				
			||||||
			status = json.loads(msg.json()['payload'])
 | 
					 | 
				
			||||||
			try:
 | 
					 | 
				
			||||||
				# Two events come in for each status on the timeline. I don't know why.
 | 
					 | 
				
			||||||
				# One of them has the status nested deeper. Just ignore that one I guess.
 | 
					 | 
				
			||||||
				if 'status' in status: continue
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
				# Don't activate on boosts at all
 | 
					 | 
				
			||||||
				if 'reblog' in status and status['reblog'] is not None: continue
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	status_id = status['id']
 | 
						status_id = status['id']
 | 
				
			||||||
	status_author = '@' + status['account']['acct']
 | 
						status_author = '@' + status['account']['acct']
 | 
				
			||||||
	status_text = status['content']
 | 
						status_text = status['content']
 | 
				
			||||||
	status_visibility = status['visibility']
 | 
						status_visibility = status['visibility']
 | 
				
			||||||
			except:
 | 
					 | 
				
			||||||
				try:
 | 
					 | 
				
			||||||
					if status['type'] == 'follow':
 | 
					 | 
				
			||||||
						id = status['account']['id']
 | 
					 | 
				
			||||||
						log(f'Received follow from {id}, following back')
 | 
					 | 
				
			||||||
						await c.account_follow(id)
 | 
					 | 
				
			||||||
				except:
 | 
					 | 
				
			||||||
					log('Event came in that we don\'t know how to handle.', Severity.WARNING)
 | 
					 | 
				
			||||||
					log(status, Severity.WARNING)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
				continue
 | 
					 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
	# Reply unlisted or at the same visibility as the parent, whichever is
 | 
						# Reply unlisted or at the same visibility as the parent, whichever is
 | 
				
			||||||
	# more restrictive
 | 
						# more restrictive
 | 
				
			||||||
| 
						 | 
					@ -221,7 +201,7 @@ async def listen(c, me):
 | 
				
			||||||
		card_names = re.findall(r'\[\[(.*?)\]\]', status_text)
 | 
							card_names = re.findall(r'\[\[(.*?)\]\]', status_text)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		# ignore any statuses without cards in them
 | 
							# ignore any statuses without cards in them
 | 
				
			||||||
				if not card_names: continue
 | 
							if not card_names: return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		cards, media = await get_cards(card_names)
 | 
							cards, media = await get_cards(card_names)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -258,6 +238,32 @@ async def listen(c, me):
 | 
				
			||||||
		error_msg = 'An error occured sending the reply. This most likely means that it would have been greater than 500 characters. If it was something else, please let Holly know!'
 | 
							error_msg = 'An error occured sending the reply. This most likely means that it would have been greater than 500 characters. If it was something else, please let Holly know!'
 | 
				
			||||||
		await c.create_status(status=f'{status_author} {error_msg}', in_reply_to_id=status_id, visibility=reply_visibility)
 | 
							await c.create_status(status=f'{status_author} {error_msg}', in_reply_to_id=status_id, visibility=reply_visibility)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					async def handle_follow(c, follow):
 | 
				
			||||||
 | 
						id = follow['account']['id']
 | 
				
			||||||
 | 
						log(f'Received follow from {id}, following back')
 | 
				
			||||||
 | 
						await c.account_follow(id)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					async def listen(c, me):
 | 
				
			||||||
 | 
						log('Listening...')
 | 
				
			||||||
 | 
						async with c.streaming('user') as stream:
 | 
				
			||||||
 | 
							async for msg in stream:
 | 
				
			||||||
 | 
								event = msg.json()['event']
 | 
				
			||||||
 | 
								payload = json.loads(msg.json()['payload'])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								# We only care about these two events
 | 
				
			||||||
 | 
								if event == 'update':
 | 
				
			||||||
 | 
									mentions_me = any((mentioned['id'] == me['id'] for mentioned in payload['mentions']))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									# Ignore any incoming status that mentions us
 | 
				
			||||||
 | 
									# We're also going to get a ntification event, we'll handle it there
 | 
				
			||||||
 | 
									if not mentions_me:
 | 
				
			||||||
 | 
										await handle_status(c, payload)
 | 
				
			||||||
 | 
								elif event == 'notification':
 | 
				
			||||||
 | 
									if payload['type'] == 'follow':
 | 
				
			||||||
 | 
										await handle_follow(c, payload)
 | 
				
			||||||
 | 
									elif payload['type'] == 'mention':
 | 
				
			||||||
 | 
										await handle_status(c, payload['status'])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# https://stackoverflow.com/a/55505152/2114129
 | 
					# https://stackoverflow.com/a/55505152/2114129
 | 
				
			||||||
async def repeat(interval, func, *args, **kwargs):
 | 
					async def repeat(interval, func, *args, **kwargs):
 | 
				
			||||||
	"""Run func every interval seconds.
 | 
						"""Run func every interval seconds.
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue