lol forgot to update this for ages
This commit is contained in:
25
matrix/inbound.py
Normal file
25
matrix/inbound.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from .utils import get_or_create as goc
|
||||
import .models
|
||||
from .chatlib import Listener as _l, Event as _e
|
||||
|
||||
# Default handlers will be given two parts each:
|
||||
# 1) Mould the raw event into a parsed event (update internal state)
|
||||
# 2) Identify event chains and dump them into the queue.
|
||||
# I really don't recommend removing them. (But I won't stop you.)
|
||||
|
||||
def sync_f(client,event):
|
||||
blob=event.raw_data
|
||||
# This event never really gets any processed data in it.
|
||||
for status in ['join','leave','invite']:
|
||||
roomlist=blob.get('rooms',{status:{}}).get(status,{})
|
||||
for rid,room in roomlist.items():
|
||||
client.event_queue.add(_e('m.room',{'id':rid,'state':status},room))
|
||||
sync=_l('m.sync',sync_f)
|
||||
|
||||
def room_f(client,event):
|
||||
blob=event.raw_data
|
||||
rid=event.data['id']
|
||||
room=goc(lambda x:x.id==rid,client.account.rooms,models.Room(rid))
|
||||
event.data=room
|
||||
room.name=
|
||||
room=_l('m.room',room_f)
|
18
matrix/matrixapi.py
Normal file
18
matrix/matrixapi.py
Normal file
@@ -0,0 +1,18 @@
|
||||
class MatrixLibHttp():
|
||||
def __init__(self,instance,token=None,username=None,password=None,chatlib=None):
|
||||
self.instance=instance
|
||||
self.https=True
|
||||
self.token=token
|
||||
if chatlib is None:
|
||||
import chatlib
|
||||
chatlib.addlistener() # TODO: probably should make that a decorator actually. Lol, it already is.
|
||||
|
||||
@property
|
||||
def baseurl(self):
|
||||
return 'http'+['','s'][self.https]+'://'+self.instance+'/_matrix/client'
|
||||
|
||||
async def request(self,endpoint='sync',method='GET', ver=0,headers={}, *args,**kwargs):
|
||||
async with self.session.request(method, f'{self.baseurl}/r{ver}/{endpoint}', headers=headers|{'Authorization':f'Bearer {self.token}'}, *args,**kwargs) as fetched:
|
||||
if fetched.status_code!=200: raise Exception('fix ur shit')
|
||||
try: return await fetched.json()
|
||||
except JSONDecodeError: pass # TODO: Figure out what this is called
|
54
matrix/models.py
Normal file
54
matrix/models.py
Normal file
@@ -0,0 +1,54 @@
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import datetime
|
||||
|
||||
@dataclass
|
||||
class User():
|
||||
@property
|
||||
def mxid(self): return f'@{self.username}:{self.homeserver}'
|
||||
@mxid.setter
|
||||
def set_mxid(self,value):
|
||||
a=value.lstrip('@').split(':')
|
||||
self.username=a[0]; self.homeserver=a[1]
|
||||
username:str
|
||||
homeserver:str
|
||||
nick:str=''
|
||||
avatar:str=''
|
||||
|
||||
@dataclass
|
||||
class Account(User):
|
||||
rooms:List[Room]=field(default_factory=list)
|
||||
|
||||
@dataclass
|
||||
class Room():
|
||||
room_id:str
|
||||
name:str=''
|
||||
viewer:Account=None
|
||||
members:List[Member]=field(default_factory=list)
|
||||
@property
|
||||
def ismember(self): return self.viewer in self.members
|
||||
parents:List[Space]=field(default_factory=list)
|
||||
messages:List[Message]=field(default_factory=list)
|
||||
|
||||
@dataclass
|
||||
class Space(Room):
|
||||
children:List[Room]=field(default_factory=list)
|
||||
|
||||
@dataclass
|
||||
class Event():
|
||||
event_id:str
|
||||
author:Member
|
||||
timestamp:datetime
|
||||
contents:str=''
|
||||
|
||||
@dataclass
|
||||
class Message(Event):
|
||||
format:str=''
|
||||
|
||||
@dataclass
|
||||
class State(Event):
|
||||
key:str
|
||||
|
||||
@dataclass
|
||||
class Member(User):
|
||||
room_av:str=''
|
||||
room_nick:str=''
|
0
matrix/outbound.py
Normal file
0
matrix/outbound.py
Normal file
10
matrix/timers.py
Normal file
10
matrix/timers.py
Normal file
@@ -0,0 +1,10 @@
|
||||
from chatlib import addtimer
|
||||
# This function just dumps /sync blobs in the event queue as a raw event.
|
||||
# ALL handling is deferred to handlers.
|
||||
# ... Except updating the since token. That's important to do here to guarantee that it never calls with the same token twice.
|
||||
|
||||
@addtimer(0)
|
||||
async def sync(self):
|
||||
blob=await self.request(params={'timeout':30000,'since':self.since})
|
||||
self.since=blob['next_batch']
|
||||
self.event_queue.put(Event('m.sync',None,blob))
|
9
matrix/utils.py
Normal file
9
matrix/utils.py
Normal file
@@ -0,0 +1,9 @@
|
||||
def get_or_create(needle,haystack,default):
|
||||
"""
|
||||
This is a wrapper for filter that can add stuff. Nothing special. Needle is a function, default isn't. Haystack is a list. I might fix that later.
|
||||
"""
|
||||
f=filter(needle,haystack)
|
||||
try: return next(f)
|
||||
except StopIteration: pass
|
||||
haystack.append(default)
|
||||
return default
|
Reference in New Issue
Block a user