Skip to content

Instantly share code, notes, and snippets.

@lamoboos223
Last active September 2, 2024 10:54
Show Gist options
  • Save lamoboos223/c07479d3f1f8403f58818e6d732fb292 to your computer and use it in GitHub Desktop.
Save lamoboos223/c07479d3f1f8403f58818e6d732fb292 to your computer and use it in GitHub Desktop.

enable notification for a room

note that %21RotqTsIrBvXaPAbhiC%3Atwkl.chat is the room_id after url encoding it using this tool

curl --location --request PUT 'http://localhost:8008/_matrix/client/r0/pushrules/global/room/%21RotqTsIrBvXaPAbhiC%3Atwkl.chat' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <user_token>' \
--data '{
    "actions": [
        {
            "set_tweak": "highlight",
            "value": false
        },
        {
            "set_tweak": "sound",
            "value": "default"
        },
        "notify"
    ]
}'

Display user's notifications preferences

curl --location 'http://localhost:8008/_matrix/client/r0/notifications' \
--header 'Authorization: Bearer <user_token>'
Example Response
{
    "notifications": [
        {
            "room_id": "!RotqTsIrBvXaPAbhiC:twkl.chat",
            "profile_tag": null,
            "actions": [
                {
                    "set_tweak": "highlight",
                    "value": false
                },
                {
                    "set_tweak": "sound",
                    "value": "default"
                },
                "notify"
            ],
            "ts": 1724324689536,
            "event": {
                "type": "m.room.message",
                "sender": "@admin:twkl.chat",
                "content": {
                    "msgtype": "m.text",
                    "body": "bye"
                },
                "origin_server_ts": 1724324689451,
                "unsigned": {
                    "age": 19877
                },
                "event_id": "$30gljAwSjcRhYTop57KzK-tXxDx7lpK3tw2YO8vyYDM"
            },
            "read": false
        }
    ],
    "next_token": "92"
}

disable notification for a room

note that %21RotqTsIrBvXaPAbhiC%3Atwkl.chat is the room_id after url encoding it using this tool

curl --location --request PUT 'http://localhost:8008/_matrix/client/r0/pushrules/global/room/%21RotqTsIrBvXaPAbhiC%3Atwkl.chat' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <user_token>' \
--data '{
    "actions": [
        "dont_notify"
    ]
}'

Display user's notifications preferences after setting the pushrule to dont_notify

curl --location 'http://localhost:8008/_matrix/client/r0/notifications' \
--header 'Authorization: Bearer <user_token>'
Example Response
{
    "notifications": [],
    "next_token": null
}

Sending a message from the entity to the room which got setted to dont_notify from the user

curl --location 'http://localhost:8008/_matrix/client/r0/rooms/!RotqTsIrBvXaPAbhiC:twkl.chat/send/m.room.message' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <entity_token>' \
--data '{
    "msgtype": "m.text",
    "body": "hello, world!"
}'

result: the user who sat the room to dont_notify will not get a push notification or a sound that indicates a new message.


Delete a pushrule

note that %21RotqTsIrBvXaPAbhiC%3Atwkl.chat is the room_id after url encoding it using this tool

curl --location --request DELETE 'http://localhost:8008/_matrix/client/r0/pushrules/global/room/%21RotqTsIrBvXaPAbhiC%3Atwkl.chat' \
--header 'Authorization: Bearer <user_token>'

Counting unread notification

curl --location 'http://localhost:8008/_matrix/client/v3/sync?filter=0&timeout=30000&since=s100_584_0_65_100_73_1_72_0_1' \
--header 'Authorization: Bearer <user_token>'
Example Response
{
    "next_batch": "s104_597_0_67_104_73_1_75_0_1",
    "account_data": {
        "events": [
            {
                "type": "im.vector.setting.breadcrumbs",
                "content": {
                    "recent_rooms": [
                        "!RotqTsIrBvXaPAbhiC:twkl.chat",
                        "!EgpyACdjjXWIzMDTSd:twkl.chat",
                        "!orRoqZTnBbAfUsQlSt:twkl.chat"
                    ]
                }
            },
            {
                "type": "org.matrix.msc3890.local_notification_settings.FQUBGKTJMA",
                "content": {
                    "is_silenced": false
                }
            }
        ]
    },
    "presence": {
        "events": [
            {
                "type": "m.presence",
                "sender": "@rahaf:twkl.chat",
                "content": {
                    "presence": "online",
                    "last_active_ago": 24,
                    "currently_active": true
                }
            },
            {
                "type": "m.presence",
                "sender": "@admin:twkl.chat",
                "content": {
                    "presence": "offline",
                    "last_active_ago": 38900
                }
            }
        ]
    },
    "device_lists": {
        "changed": [
            "@rahaf:twkl.chat"
        ]
    },
    "device_one_time_keys_count": {
        "signed_curve25519": 0
    },
    "org.matrix.msc2732.device_unused_fallback_key_types": [],
    "device_unused_fallback_key_types": [],
    "rooms": {
        "join": {
            "!RotqTsIrBvXaPAbhiC:twkl.chat": {
                "timeline": {
                    "events": [
                        {
                            "type": "m.room.message",
                            "sender": "@admin:twkl.chat",
                            "content": {
                                "msgtype": "m.text",
                                "body": "bye"
                            },
                            "origin_server_ts": 1724338492441,
                            "unsigned": {
                                "age": 404586
                            },
                            "event_id": "$ew4mbWQaAJ95LzAdHK3ysmCnY0p9tWs4t4UVylp5AdI"
                        },
                        {
                            "type": "m.room.message",
                            "sender": "@admin:twkl.chat",
                            "content": {
                                "msgtype": "m.text",
                                "body": "bye"
                            },
                            "origin_server_ts": 1724338572426,
                            "unsigned": {
                                "age": 324601
                            },
                            "event_id": "$LS9l8z30tyJUg6C_pnuOv3Q6UgTm-PSlDYCUi5x-gjM"
                        },
                        {
                            "type": "m.room.message",
                            "sender": "@admin:twkl.chat",
                            "content": {
                                "msgtype": "m.text",
                                "body": "bye"
                            },
                            "origin_server_ts": 1724338809491,
                            "unsigned": {
                                "age": 87536
                            },
                            "event_id": "$SfARhd-gLqCiMm5XGD3xvfqW37b5gQ4HPC8lahPvIKw"
                        },
                        {
                            "type": "m.room.message",
                            "sender": "@admin:twkl.chat",
                            "content": {
                                "msgtype": "m.text",
                                "body": "hello world!"
                            },
                            "origin_server_ts": 1724338858062,
                            "unsigned": {
                                "age": 38965
                            },
                            "event_id": "$OtoflM-SnQui8tEZUBToIl_UtGtjjmvjfVGDbeloql8"
                        }
                    ],
                    "prev_batch": "s100_597_0_67_104_73_1_75_0_1",
                    "limited": false
                },
                "state": {
                    "events": []
                },
                "account_data": {
                    "events": []
                },
                "ephemeral": {
                    "events": [
                        {
                            "type": "m.receipt",
                            "content": {
                                "$ew4mbWQaAJ95LzAdHK3ysmCnY0p9tWs4t4UVylp5AdI": {
                                    "m.read": {
                                        "@rahaf:twkl.chat": {
                                            "ts": 1724338546692,
                                            "thread_id": "main"
                                        }
                                    }
                                }
                            }
                        }
                    ]
                },
                "unread_notifications": {
                    "notification_count": 3,
                    "highlight_count": 0
                },
                "summary": {}
            }
        }
    }
}

@lamoboos223
Copy link
Author

lamoboos223 commented Aug 27, 2024

Tags

Tags the starts with m.* it is a matrix reserved tags and tags start with u.* is user defined tags.

Tag a Room

curl --location --request PUT 'http://localhost:8008/_matrix/client/r0/user/<user_id>/rooms/<room_id>/tags/m.favourite' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <access_token>' \
--data '{
  "order": 0.25
}'

Delete a Tag

curl --location --request DELETE 'http://localhost:8008/_matrix/client/r0/user/<user_id>/rooms/<room_id>/tags/m.favourite' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <access_token>' \
--data '{
  "order": 0.25
}'

List Tags on a room

curl --location 'http://localhost:8008/_matrix/client/r0/user/<user_id>/rooms/<room_id>/tags' \
--header 'Authorization: Bearer <access_token>'

@lamoboos223
Copy link
Author

Search Messages

The room must be unencrypted to get a search result for the desired search term

curl --location 'http://localhost:8008/_matrix/client/v3/search' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer<access_token>' \
--data '{
    "search_categories": {
        "room_events": {
            "search_term": "hello, world",
            "filter": {
                "limit": 10,
                "rooms": [
                    "<room_id>"
                ]
            },
            "order_by": "recent",
            "event_context": {
                "before_limit": 1,
                "after_limit": 1,
                "include_profile": true
            }
        }
    }
}'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment