Exemplo n.º 1
0
async def test_textual_replying_to(room: Room):
    await room.timeline.send(Text("plain\nmsg"))
    await room.timeline.send(Text.from_html("<b>html</b><br>msg"))
    await room.state.send(Name("Test room"))
    await room.client.sync.once()
    plain, html, other = list(room.timeline.values())[-3:]

    def check_reply_attrs(first_event, reply, fallback_content, reply_content):
        assert reply.replies_to == first_event.id
        assert reply.format == HTML_FORMAT

        assert reply.formatted_body == HTML_REPLY_FALLBACK.format(
            matrix_to=MATRIX_TO,
            room_id=room.id,
            user_id=room.client.user_id,
            event_id=first_event.id,
            content=fallback_content,
        ) + reply_content

    reply = Text("nice").replying_to(plain)
    assert reply.body == f"> <{plain.sender}> plain\n> msg\n\nnice"
    check_reply_attrs(plain, reply, plain2html(plain.content.body), "nice")

    reply = Text("nice").replying_to(html)
    assert reply.body == f"> <{plain.sender}> **html**  \n> msg\n\nnice"
    check_reply_attrs(html, reply, html.content.formatted_body, "nice")

    reply = Text.from_html("<i>nice</i>").replying_to(html)
    assert reply.body == f"> <{plain.sender}> **html**  \n> msg\n\n*nice*"
    check_reply_attrs(html, reply, html.content.formatted_body, "<i>nice</i>")

    reply = Text("nice").replying_to(other)
    assert reply.body == f"> <{plain.sender}> {Name.type}\n\nnice"
    assert other.type
    check_reply_attrs(other, reply, plain2html(other.type), "nice")
Exemplo n.º 2
0
def test_textual_no_reply_fallback():
    assert Text("plain").html_no_reply_fallback is None

    reply = Text.from_html("<b>foo</b>")  # from_html will strip mx-reply
    reply.formatted_body = HTML_REPLY_FALLBACK + reply.formatted_body
    assert reply.html_no_reply_fallback == "<b>foo</b>"

    not_reply = Text.from_html("<b>foo</b>")
    assert not_reply.html_no_reply_fallback == "<b>foo</b>"
Exemplo n.º 3
0
def test_textual_same_html_plaintext():
    text = Text.from_html("abc", plaintext="abc")
    assert text.body == "abc"
    assert text.format is None
    assert text.formatted_body is None
Exemplo n.º 4
0
def test_textual_from_html_manual_plaintext():
    text = Text.from_html("<p>abc</p>", plaintext="123")
    assert text.body == "123"
    assert text.format == HTML_FORMAT
    assert text.formatted_body == "<p>abc</p>"
Exemplo n.º 5
0
async def test_push_rules_triggering(alice: Client, bob: Client, room: Room):
    await room.timeline.send(Text.from_html("<b>abc</b>, def"))
    await room.timeline.send(Text(f"...{alice.profile.name}..."))
    await alice.sync.once()

    assert isinstance(room.timeline[0].content, Creation)
    creation = room.timeline[0]
    abc = room.timeline[-2]
    mention = room.timeline[-1]

    # Individual conditions

    assert PushEventMatch({}, "content.format", "org.*.hTmL").triggered_by(abc)
    assert not PushEventMatch({}, "content.format", "html").triggered_by(abc)
    assert not PushEventMatch({}, "bad field", "blah").triggered_by(abc)

    assert PushEventMatch({}, "content.body", "abc").triggered_by(abc)
    assert PushEventMatch({}, "content.body", "Ab[cd]").triggered_by(abc)
    assert not PushEventMatch({}, "content.body", "ab").triggered_by(abc)

    assert PushContainsDisplayName({}).triggered_by(mention)
    assert not PushContainsDisplayName({}).triggered_by(abc)
    assert not PushContainsDisplayName({}).triggered_by(creation)

    ops = PushRoomMemberCount.Operator
    assert PushRoomMemberCount({}, 1).triggered_by(creation)
    assert PushRoomMemberCount({}, 2, ops.lt).triggered_by(abc)
    assert PushRoomMemberCount({}, 0, ops.gt).triggered_by(abc)
    assert PushRoomMemberCount({}, 1, ops.le).triggered_by(abc)
    assert PushRoomMemberCount({}, 1, ops.ge).triggered_by(abc)
    assert not PushRoomMemberCount({}, 1, ops.gt).triggered_by(abc)
    assert not PushRoomMemberCount({}, 1, ops.lt).triggered_by(abc)
    assert not PushRoomMemberCount({}, 2, ops.eq).triggered_by(abc)

    assert PushSenderNotificationPermission({}, "room").triggered_by(abc)
    assert PushSenderNotificationPermission({}, "unknown").triggered_by(abc)
    users = {alice.user_id: 49}
    await room.state.send(room.state.power_levels.but(users=users))
    await alice.sync.once()
    assert not PushSenderNotificationPermission({}, "room").triggered_by(abc)
    assert not PushSenderNotificationPermission({}, "xyz").triggered_by(abc)

    # PushRule

    kinds = PushRule.Kind
    assert PushRule("test").triggered_by(abc)
    assert not PushRule("test", enabled=False).triggered_by(abc)
    mention_1 = PushRule("test",
                         conditions=[
                             PushContainsDisplayName({}),
                             PushRoomMemberCount({}, 1),
                         ])
    assert mention_1.triggered_by(mention)
    assert not mention_1.triggered_by(abc)

    assert PushRule("c", kind=kinds.content, pattern="abc").triggered_by(abc)
    assert not PushRule("c", kind=kinds.content, pattern="a").triggered_by(abc)

    assert PushRule(room.id, kind=kinds.room).triggered_by(abc)
    assert not PushRule(room.id + "a", kind=kinds.room).triggered_by(abc)

    assert PushRule(alice.user_id, kind=kinds.sender).triggered_by(abc)
    assert not PushRule(bob.user_id, kind=kinds.sender).triggered_by(abc)

    # PushRuleset

    rule = alice.account_data.push_rules.main.triggered(abc)
    assert rule and rule.id == ".m.rule.message"