mirror of
https://github.com/chatmail/core.git
synced 2026-04-06 15:42:10 +03:00
fix member_added/member_removed event with tests and and provide a group-tracking example
This commit is contained in:
64
python/examples/group_tracking.py
Normal file
64
python/examples/group_tracking.py
Normal file
@@ -0,0 +1,64 @@
|
||||
|
||||
# content of group_tracking.py
|
||||
|
||||
import sys
|
||||
import optparse
|
||||
import deltachat
|
||||
|
||||
|
||||
class GroupTrackingPlugin:
|
||||
@deltachat.hookspec.account_hookimpl
|
||||
def process_incoming_message(self, message):
|
||||
print("*** process_incoming_message addr={} msg={!r}".format(
|
||||
message.get_sender_contact().addr, message.text))
|
||||
for member in message.chat.get_contacts():
|
||||
print("chat member: {}".format(member.addr))
|
||||
|
||||
@deltachat.hookspec.account_hookimpl
|
||||
def member_added(self, chat, contact):
|
||||
print("*** member_added", contact.addr, "from", chat)
|
||||
for member in chat.get_contacts():
|
||||
print("chat member: {}".format(member.addr))
|
||||
|
||||
@deltachat.hookspec.account_hookimpl
|
||||
def member_removed(self, chat, contact):
|
||||
print("*** member_removed", contact.addr, "from", chat)
|
||||
|
||||
|
||||
def main(argv):
|
||||
p = optparse.OptionParser("simple-echo")
|
||||
p.add_option("-l", action="store_true", help="show ffi")
|
||||
p.add_option("--db", type="str", help="database file")
|
||||
p.add_option("--email", type="str", help="email address")
|
||||
p.add_option("--password", type="str", help="password")
|
||||
|
||||
opts, posargs = p.parse_args(argv)
|
||||
|
||||
assert opts.db, "you must specify --db"
|
||||
ac = deltachat.Account(opts.db)
|
||||
|
||||
if opts.l:
|
||||
log = deltachat.eventlogger.FFIEventLogger(ac, "group-tracking")
|
||||
ac.add_account_plugin(log)
|
||||
|
||||
if not ac.is_configured():
|
||||
assert opts.email and opts.password, (
|
||||
"you must specify --email and --password"
|
||||
)
|
||||
ac.set_config("addr", opts.email)
|
||||
ac.set_config("mail_pw", opts.password)
|
||||
ac.set_config("mvbox_watch", "0")
|
||||
ac.set_config("sentbox_watch", "0")
|
||||
|
||||
ac.add_account_plugin(GroupTrackingPlugin())
|
||||
|
||||
# start IO threads and configure if neccessary
|
||||
ac.start()
|
||||
|
||||
print("{}: waiting for message".format(ac.get_config("addr")))
|
||||
|
||||
ac.wait_shutdown()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main(sys.argv)
|
||||
@@ -173,14 +173,29 @@ class TestOfflineChat:
|
||||
def test_add_member_event(self, ac1):
|
||||
chat = ac1.create_group_chat(name="title1")
|
||||
assert chat.is_group()
|
||||
# promote the chat
|
||||
chat.send_text("hello")
|
||||
contact1 = ac1.create_contact("some1@hello.com", name="some1")
|
||||
|
||||
chat.add_contact(contact1)
|
||||
for ev in ac1.iter_events(timeout=1):
|
||||
if ev.name == "member_added":
|
||||
assert ev.kwargs["chat"] == chat
|
||||
if ev.kwargs["contact"] == ac1.get_self_contact():
|
||||
continue
|
||||
assert ev.kwargs["contact"] == contact1
|
||||
break
|
||||
|
||||
def test_remove_member_event(self, ac1):
|
||||
chat = ac1.create_group_chat(name="title1")
|
||||
assert chat.is_group()
|
||||
contact1 = ac1.create_contact("some1@hello.com", name="some1")
|
||||
chat.add_contact(contact1)
|
||||
ac1._handle_current_events()
|
||||
chat.remove_contact(contact1)
|
||||
for ev in ac1.iter_events(timeout=1):
|
||||
if ev.name == "member_removed":
|
||||
assert ev.kwargs["chat"] == chat
|
||||
if ev.kwargs["contact"] == ac1.get_self_contact():
|
||||
continue
|
||||
assert ev.kwargs["contact"] == contact1
|
||||
break
|
||||
|
||||
@@ -471,11 +486,13 @@ class TestOfflineChat:
|
||||
# perform plugin hooks
|
||||
ac1._handle_current_events()
|
||||
|
||||
assert len(in_list) == 10
|
||||
assert len(in_list) == 11
|
||||
chat_contacts = chat.get_contacts()
|
||||
for in_cmd, in_chat, in_contact in in_list:
|
||||
assert in_cmd == "added"
|
||||
assert in_chat == chat
|
||||
assert in_contact in contacts
|
||||
assert in_contact in chat_contacts
|
||||
chat_contacts.remove(in_contact)
|
||||
|
||||
lp.sec("ac1: removing two contacts and checking things are right")
|
||||
chat.remove_contact(contacts[9])
|
||||
@@ -483,10 +500,13 @@ class TestOfflineChat:
|
||||
assert len(chat.get_contacts()) == 9
|
||||
|
||||
ac1._handle_current_events()
|
||||
assert len(in_list) == 12
|
||||
assert len(in_list) == 13
|
||||
assert in_list[-2][0] == "removed"
|
||||
assert in_list[-2][1] == chat
|
||||
assert in_list[-2][2] == contacts[9]
|
||||
assert in_list[-1][0] == "removed"
|
||||
assert in_list[-1][1] == chat
|
||||
assert in_list[-1][2] == contacts[3]
|
||||
|
||||
|
||||
class TestOnlineAccount:
|
||||
@@ -1259,6 +1279,51 @@ class TestOnlineAccount:
|
||||
msg3 = ac2._evtracker.wait_next_incoming_message()
|
||||
assert msg3.get_sender_contact().get_profile_image() is None
|
||||
|
||||
def test_add_remove_member_remote_events(self, acfactory, lp):
|
||||
ac1, ac2 = acfactory.get_two_online_accounts()
|
||||
# activate local plugin for ac2
|
||||
in_list = queue.Queue()
|
||||
|
||||
class InPlugin:
|
||||
@account_hookimpl
|
||||
def member_added(self, chat, contact):
|
||||
in_list.put(("added", chat, contact))
|
||||
|
||||
@account_hookimpl
|
||||
def member_removed(self, chat, contact):
|
||||
in_list.put(("removed", chat, contact))
|
||||
|
||||
ac2.add_account_plugin(InPlugin())
|
||||
|
||||
lp.sec("ac1: create group chat with ac2")
|
||||
chat = ac1.create_group_chat("hello")
|
||||
contact = ac1.create_contact(email=ac2.get_config("addr"))
|
||||
chat.add_contact(contact)
|
||||
|
||||
lp.sec("ac1: send a message to group chat to promote the group")
|
||||
chat.send_text("afterwards promoted")
|
||||
ev1 = in_list.get()
|
||||
ev2 = in_list.get()
|
||||
assert ev1[2] == ac2.get_self_contact()
|
||||
assert ev2[2].addr == ac1.get_config("addr")
|
||||
|
||||
lp.sec("ac1: add address2")
|
||||
contact2 = ac1.create_contact(email="not@example.org")
|
||||
chat.add_contact(contact2)
|
||||
ev1 = in_list.get()
|
||||
assert ev1[2].addr == contact2.addr
|
||||
|
||||
lp.sec("ac1: remove address2")
|
||||
chat.remove_contact(contact2)
|
||||
ev1 = in_list.get()
|
||||
assert ev1[0] == "removed"
|
||||
assert ev1[2].addr == contact2.addr
|
||||
|
||||
lp.sec("ac1: remove ac2 contact from chat")
|
||||
chat.remove_contact(contact)
|
||||
ev1 = in_list.get()
|
||||
assert ev1[2] == ac2.get_self_contact()
|
||||
|
||||
def test_set_get_group_image(self, acfactory, data, lp):
|
||||
ac1, ac2 = acfactory.get_two_online_accounts()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user