summary refs log tree commit diff
path: root/thread_test.py
blob: 61affb4277b46d2fe7465a761a4d850982ae5114 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import json
from time import monotonic

import requests

HOMESERVER = "http://localhost:8080"

USER_1_TOK = "syt_dGVzdGVy_AywuFarQjsYrHuPkOUvg_25XLNK"
USER_1_HEADERS = {"Authorization": f"Bearer {USER_1_TOK}"}

USER_2_TOK = "syt_b3RoZXI_jtiTnwtlBjMGMixlHIBM_4cxesB"
USER_2_HEADERS = {"Authorization": f"Bearer {USER_2_TOK}"}


def _check_for_status(result):
    # Similar to raise_for_status, but prints the error.
    if 400 <= result.status_code:
        error_msg = result.json()
        result.raise_for_status()
        print(error_msg)
        exit(0)


def _sync_and_show(room_id):
    print("Syncing . . .")
    result = requests.get(
        f"{HOMESERVER}/_matrix/client/v3/sync",
        headers=USER_1_HEADERS,
        params={
            "filter": json.dumps(
                {
                    "room": {
                        "timeline": {"limit": 30, "unread_thread_notifications": True}
                    }
                }
            )
        },
    )
    _check_for_status(result)
    sync_response = result.json()

    room = sync_response["rooms"]["join"][room_id]

    # Find read receipts (this assumes non-overlapping).
    read_receipts = {}  # thread -> event ID -> users
    for event in room["ephemeral"]["events"]:
        if event["type"] != "m.receipt":
            continue

        for event_id, content in event["content"].items():
            for mxid, receipt in content["m.read"].items():
                print(mxid, receipt)
                # Just care about the localpart of the MXID.
                mxid = mxid.split(":", 1)[0]
                read_receipts.setdefault(receipt.get("thread_id"), {}).setdefault(
                    event_id, []
                ).append(mxid)

    print(room["unread_notifications"])
    print(room.get("unread_thread_notifications"))
    print()

    # Convert events to their threads.
    threads = {}
    for event in room["timeline"]["events"]:
        if event["type"] != "m.room.message":
            continue

        event_id = event["event_id"]

        parent_id = event["content"].get("m.relates_to", {}).get("event_id")
        if parent_id:
            threads[parent_id][1].append(event)
        else:
            threads[event_id] = (event, [])

    for root_event_id, (root, thread) in threads.items():
        msg = root["content"]["body"]
        print(f"{root_event_id}: {msg}")

        for event in thread:
            thread_event_id = event["event_id"]

            msg = event["content"]["body"]
            print(f"\t{thread_event_id}: {msg}")

            if thread_event_id in read_receipts.get(root_event_id, {}):
                user_ids = ", ".join(read_receipts[root_event_id][thread_event_id])
                print(f"\t^--------- {user_ids} ---------^")

        if root_event_id in read_receipts[None]:
            user_ids = ", ".join(read_receipts[None][root_event_id])
            print(f"^--------- {user_ids} ---------^")

    print()
    print()


def _send_event(room_id, body, thread_id=None):
    content = {
        "msgtype": "m.text",
        "body": body,
    }
    if thread_id:
        content["m.relates_to"] = {
            "rel_type": "m.thread",
            "event_id": thread_id,
        }

    # Send a msg to the room.
    result = requests.put(
        f"{HOMESERVER}/_matrix/client/v3/rooms/{room_id}/send/m.room.message/msg{monotonic()}",
        json=content,
        headers=USER_2_HEADERS,
    )
    _check_for_status(result)
    return result.json()["event_id"]


def main():
    # Create a new room as user 2, add a bunch of messages.
    result = requests.post(
        f"{HOMESERVER}/_matrix/client/v3/createRoom",
        json={"visibility": "public", "name": f"Thread Read Receipts ({monotonic()})"},
        headers=USER_2_HEADERS,
    )
    _check_for_status(result)
    room_id = result.json()["room_id"]

    # Second user joins the room.
    result = requests.post(
        f"{HOMESERVER}/_matrix/client/v3/rooms/{room_id}/join", headers=USER_1_HEADERS
    )
    _check_for_status(result)

    # Sync user 1.
    _sync_and_show(room_id)

    # User 2 sends some messages.
    event_ids = []

    def _send_and_append(body, thread_id=None):
        event_id = _send_event(room_id, body, thread_id)
        event_ids.append(event_id)
        return event_id

    for msg in range(5):
        root_message_id = _send_and_append(f"Message {msg}")
    for msg in range(10):
        if msg % 2:
            _send_and_append(f"More message {msg}")
        else:
            _send_and_append(f"Thread Message {msg}", root_message_id)

    # User 2 sends a read receipt.
    print("@second reads main timeline")
    result = requests.post(
        f"{HOMESERVER}/_matrix/client/v3/rooms/{room_id}/receipt/m.read/{event_ids[3]}",
        headers=USER_2_HEADERS,
        json={},
    )
    _check_for_status(result)

    _sync_and_show(room_id)

    # User 1 sends a read receipt.
    print("@test reads main timeline")
    result = requests.post(
        f"{HOMESERVER}/_matrix/client/v3/rooms/{room_id}/receipt/m.read/{event_ids[-5]}",
        headers=USER_1_HEADERS,
        json={},
    )
    _check_for_status(result)

    _sync_and_show(room_id)

    # User 1 sends another read receipt.
    print("@test reads thread")
    result = requests.post(
        f"{HOMESERVER}/_matrix/client/v3/rooms/{room_id}/receipt/m.read/{event_ids[-4]}/{root_message_id}",
        headers=USER_1_HEADERS,
        json={},
    )
    _check_for_status(result)

    _sync_and_show(room_id)


if __name__ == "__main__":
    main()