mirror of
https://github.com/chatmail/relay.git
synced 2026-05-16 23:18:57 +00:00
post notifications via a background thread
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import pwd
|
import pwd
|
||||||
|
|
||||||
from queue import Queue
|
from queue import Queue
|
||||||
|
from threading import Thread
|
||||||
from socketserver import (
|
from socketserver import (
|
||||||
UnixStreamServer,
|
UnixStreamServer,
|
||||||
StreamRequestHandler,
|
StreamRequestHandler,
|
||||||
@@ -31,21 +32,24 @@ class Notifier:
|
|||||||
def new_message_for_guid(self, guid):
|
def new_message_for_guid(self, guid):
|
||||||
self.to_notify_queue.put(guid)
|
self.to_notify_queue.put(guid)
|
||||||
|
|
||||||
def thread_run(self):
|
def thread_run_loop(self):
|
||||||
requests_session = requests.Session()
|
requests_session = requests.Session()
|
||||||
while 1:
|
while 1:
|
||||||
guid = self.to_notify_queue.get()
|
self.thread_run_one(requests_session)
|
||||||
token = self.guid2token.get(guid)
|
|
||||||
if token:
|
def thread_run_one(self, requests_session):
|
||||||
response = requests_session.post(
|
guid = self.to_notify_queue.get()
|
||||||
"https://notifications.delta.chat/notify",
|
token = self.guid2token.get(guid)
|
||||||
data=token,
|
if token:
|
||||||
timeout=60,
|
response = requests_session.post(
|
||||||
)
|
"https://notifications.delta.chat/notify",
|
||||||
if response.status_code == 410:
|
data=token,
|
||||||
# 410 Gone status code
|
timeout=60,
|
||||||
# means the token is no longer valid.
|
)
|
||||||
del self.guid2token[guid]
|
if response.status_code == 410:
|
||||||
|
# 410 Gone status code
|
||||||
|
# means the token is no longer valid.
|
||||||
|
del self.guid2token[guid]
|
||||||
|
|
||||||
|
|
||||||
def handle_dovecot_protocol(rfile, wfile, notifier):
|
def handle_dovecot_protocol(rfile, wfile, notifier):
|
||||||
@@ -131,6 +135,13 @@ def main():
|
|||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
# start notifier thread for signalling new messages to
|
||||||
|
# Delta Chat notification server
|
||||||
|
|
||||||
|
t = Thread(target=notifier.thread_run_loop)
|
||||||
|
t.setDaemon(1)
|
||||||
|
t.start()
|
||||||
|
|
||||||
with ThreadedUnixStreamServer(socket, Handler) as server:
|
with ThreadedUnixStreamServer(socket, Handler) as server:
|
||||||
os.chown(socket, uid=passwd_entry.pw_uid, gid=passwd_entry.pw_gid)
|
os.chown(socket, uid=passwd_entry.pw_uid, gid=passwd_entry.pw_gid)
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -87,3 +87,45 @@ def test_handle_dovecot_protocol_messagenew():
|
|||||||
assert wfile.getvalue() == b"O\n"
|
assert wfile.getvalue() == b"O\n"
|
||||||
assert notifier.to_notify_queue.get() == "guid00"
|
assert notifier.to_notify_queue.get() == "guid00"
|
||||||
assert notifier.to_notify_queue.qsize() == 0
|
assert notifier.to_notify_queue.qsize() == 0
|
||||||
|
|
||||||
|
|
||||||
|
def test_notifier_thread_run():
|
||||||
|
requests = []
|
||||||
|
|
||||||
|
class ReqMock:
|
||||||
|
def post(self, url, data, timeout):
|
||||||
|
requests.append((url, data, timeout))
|
||||||
|
|
||||||
|
class Result:
|
||||||
|
status_code = 200
|
||||||
|
|
||||||
|
return Result()
|
||||||
|
|
||||||
|
notifier = Notifier()
|
||||||
|
notifier.set_token("guid00", "01234")
|
||||||
|
notifier.new_message_for_guid("guid00")
|
||||||
|
notifier.thread_run_one(ReqMock())
|
||||||
|
url, data, timeout = requests[0]
|
||||||
|
assert data == "01234"
|
||||||
|
|
||||||
|
|
||||||
|
def test_notifier_thread_run_gone_removes_token():
|
||||||
|
requests = []
|
||||||
|
|
||||||
|
class ReqMock:
|
||||||
|
def post(self, url, data, timeout):
|
||||||
|
requests.append((url, data, timeout))
|
||||||
|
|
||||||
|
class Result:
|
||||||
|
status_code = 410
|
||||||
|
|
||||||
|
return Result()
|
||||||
|
|
||||||
|
notifier = Notifier()
|
||||||
|
notifier.set_token("guid00", "01234")
|
||||||
|
notifier.new_message_for_guid("guid00")
|
||||||
|
assert notifier.guid2token["guid00"] == "01234"
|
||||||
|
notifier.thread_run_one(ReqMock())
|
||||||
|
url, data, timeout = requests[0]
|
||||||
|
assert data == "01234"
|
||||||
|
assert len(notifier.guid2token) == 0
|
||||||
|
|||||||
Reference in New Issue
Block a user