~reedwade/wee-matter-devel

wee-matter: remove pagination params, use qualified name v1 APPLIED

~midgard: 6
 remove pagination params, use qualified name
 handle large responses in a few functions
 add ~… to rooms and (~…) to private rooms
 fix marking channels as read
 add editorconfig
 declare a dependency that I had to install

 8 files changed, 103 insertions(+), 17 deletions(-)
Eddie Barraco
I merged some of your patches. Thanks a lot !

To git.sr.ht:~reedwade/wee-matter
   a279374..2321988  2321988 -> master

I worked on the buffered response on my way. I'll send a patch
in another thread. Could you test it ?
Thanks a lot fot those patches.

I'll definitely have a look at it later this day or this week-end.

Your work on the buffer on splitted response feels good. I'll try to
a refact to clean up just a little bit.
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~reedwade/wee-matter-devel/patches/14961/mbox | git am -3
Learn more about email & git
View this thread in the archives

[PATCH wee-matter 1/6] remove pagination params, use qualified name Export this patch

From: Midgard <2885-Midgard@users.noreply.framagit.org>

Pagination parameters for the channel requests are ignored.

Use the qualified name of wee_matter.server.server_root_url in a place
where this was forgotten. The function is rarely triggered, so this was
easily overlooked.
---
 wee_matter/http.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/wee_matter/http.py b/wee_matter/http.py
index d97ea02..6cf9c58 100644
--- a/wee_matter/http.py
+++ b/wee_matter/http.py
@@ -28,7 +28,7 @@ def run_get_user_teams(user_id, server, cb, cb_data):
    )

def run_get_team(team_id, server, cb, cb_data):
    url = server_root_url(server) + "/api/v4/teams/" + team_id
    url = wee_matter.server.server_root_url(server) + "/api/v4/teams/" + team_id
    weechat.hook_process_hashtable(
        "url:" + url,
        {
@@ -116,7 +116,7 @@ def run_get_channel(channel_id, server, cb, cb_data):
    )

def run_get_user_team_channels(user_id, team_id, server, cb, cb_data):
    url = wee_matter.server.server_root_url(server) + "/api/v4/users/" + user_id + "/teams/" + team_id + "/channels?page=0&per_page=5"
    url = wee_matter.server.server_root_url(server) + "/api/v4/users/" + user_id + "/teams/" + team_id + "/channels"
    weechat.hook_process_hashtable(
        "url:" + url,
        {
-- 
2.26.2

[PATCH wee-matter 2/6] handle large responses in a few functions Export this patch

From: Midgard <2885-Midgard@users.noreply.framagit.org>

This introduces ugly global variables and code duplication, waiting
refactoring.
---
 wee_matter/room.py   | 62 ++++++++++++++++++++++++++++++++++++++------
 wee_matter/server.py | 15 +++++++++--
 2 files changed, 67 insertions(+), 10 deletions(-)

diff --git a/wee_matter/room.py b/wee_matter/room.py
index 2d6e822..afc66ae 100644
--- a/wee_matter/room.py
+++ b/wee_matter/room.py
@@ -59,14 +59,27 @@ def room_input_cb(data, buffer, input_data):

    return weechat.WEECHAT_RC_OK

room_post_buffers = {}
def hidrate_room_posts_cb(buffer, command, rc, out, err):
    if rc != 0:
    channel_id = weechat.buffer_get_string(buffer, "localvar_channel_id")
    weechat.prnt("", f"Got room hydrating data for {channel_id}")

    if rc != 0 and rc != weechat.WEECHAT_HOOK_PROCESS_RUNNING:
        room_post_buffers[buffer] = ""
        weechat.prnt("", "An error occured when hidrating room")
        return weechat.WEECHAT_RC_ERROR

    if buffer not in room_post_buffers:
        room_post_buffers[buffer] = ""
    room_post_buffers[buffer] += out

    if rc == weechat.WEECHAT_HOOK_PROCESS_RUNNING:
        return weechat.WEECHAT_RC_OK

    server = wee_matter.server.get_server_from_buffer(buffer)

    response = json.loads(out)
    response = json.loads(room_post_buffers[buffer])
    room_post_buffers[buffer] = ""

    response["order"].reverse()
    for post_id in response["order"]:
@@ -78,14 +91,27 @@ def hidrate_room_posts_cb(buffer, command, rc, out, err):

    return weechat.WEECHAT_RC_OK

room_read_post_buffers = {}
def hidrate_room_read_posts_cb(buffer, command, rc, out, err):
    if rc != 0:
    channel_id = weechat.buffer_get_string(buffer, "localvar_channel_id")
    weechat.prnt("", f"Got read room hydrating data for {channel_id}")

    if rc != 0 and rc != weechat.WEECHAT_HOOK_PROCESS_RUNNING:
        room_read_post_buffers[buffer] = ""
        weechat.prnt("", "An error occured when hidrating room")
        return weechat.WEECHAT_RC_ERROR

    if buffer not in room_read_post_buffers:
        room_read_post_buffers[buffer] = ""
    room_read_post_buffers[buffer] += out

    if rc == weechat.WEECHAT_HOOK_PROCESS_RUNNING:
        return weechat.WEECHAT_RC_OK

    server = wee_matter.server.get_server_from_buffer(buffer)

    response = json.loads(out)
    response = json.loads(room_read_post_buffers[buffer])
    room_read_post_buffers[buffer] = ""

    if not response["order"]:
        return weechat.WEECHAT_RC_OK
@@ -116,29 +142,49 @@ def create_room_user_from_user_data(user_data, buffer, server):

    weechat.nicklist_add_nick(buffer, "", user.username, user.color, "@", user.color, 1)

room_users_buffers = {}
def hidrate_room_users_cb(buffer, command, rc, out, err):
    if rc != 0:
    if rc != 0 and rc != weechat.WEECHAT_HOOK_PROCESS_RUNNING:
        room_users_buffers[buffer] = ""
        weechat.prnt("", "An error occured when hidrating room users")
        return weechat.WEECHAT_RC_ERROR

    response = json.loads(out)
    if buffer not in room_users_buffers:
        room_users_buffers[buffer] = ""
    room_users_buffers[buffer] += out

    if rc == weechat.WEECHAT_HOOK_PROCESS_RUNNING:
        return weechat.WEECHAT_RC_OK

    server = wee_matter.server.get_server_from_buffer(buffer)

    response = json.loads(room_users_buffers[buffer])
    room_users_buffers[buffer] = ""

    for user_data in response:
        create_room_user_from_user_data(user_data, buffer, server)

    return weechat.WEECHAT_RC_OK

room_user_buffers = {}
def hidrate_room_user_cb(buffer, command, rc, out, err):
    if rc != 0:
    if rc != 0 and rc != weechat.WEECHAT_HOOK_PROCESS_RUNNING:
        room_user_buffers[buffer] = ""
        weechat.prnt("", "An error occured when hidrating room user")
        return weechat.WEECHAT_RC_ERROR

    user_data = json.loads(out)
    if buffer not in room_user_buffers:
        room_user_buffers[buffer] = ""
    room_user_buffers[buffer] += out

    if rc == weechat.WEECHAT_HOOK_PROCESS_RUNNING:
        return weechat.WEECHAT_RC_OK

    server = wee_matter.server.get_server_from_buffer(buffer)

    response = json.loads(room_user_buffers[buffer])
    room_user_buffers[buffer] = ""

    create_room_user_from_user_data(user_data, buffer, server)

    return weechat.WEECHAT_RC_OK
diff --git a/wee_matter/server.py b/wee_matter/server.py
index ce29e01..8fa34f8 100644
--- a/wee_matter/server.py
+++ b/wee_matter/server.py
@@ -185,14 +185,25 @@ def connect_server_team_channel_cb(server_name, command, rc, out, err):

    return weechat.WEECHAT_RC_OK

channel_buffers = {}
def connect_server_team_channels_cb(server_name, command, rc, out, err):
    if rc != 0:
    if rc != 0 and rc != weechat.WEECHAT_HOOK_PROCESS_RUNNING:
        channel_buffers[server_name] = ""
        weechat.prnt("", "An error occured when connecting team channels")
        return weechat.WEECHAT_RC_ERROR

    if server_name not in channel_buffers:
        channel_buffers[server_name] = ""
    channel_buffers[server_name] += out

    if rc == weechat.WEECHAT_HOOK_PROCESS_RUNNING:
        return weechat.WEECHAT_RC_OK

    server = get_server(server_name)

    response = json.loads(out)
    response = json.loads(channel_buffers[server_name])
    channel_buffers[server_name] = ""

    for channel_data in response:
        wee_matter.room.create_room_from_channel_data(channel_data, server)

-- 
2.26.2

[PATCH wee-matter 3/6] add ~… to rooms and (~…) to private rooms Export this patch

From: Midgard <2885-Midgard@users.noreply.framagit.org>

---
 wee_matter/room.py | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/wee_matter/room.py b/wee_matter/room.py
index afc66ae..2c905c6 100644
--- a/wee_matter/room.py
+++ b/wee_matter/room.py
@@ -193,10 +193,15 @@ def remove_room_user(buffer, user):
    nick = weechat.nicklist_search_nick(buffer, "", user.username)
    weechat.nicklist_remove_nick(buffer, nick)

FORMATS = {
    "O": "~{}",
    "P": "(~{})",
}
def build_room_name_from_channel_data(channel_data, server):
    room_name = channel_data["name"]
    if "" != channel_data["display_name"]:
        room_name = channel_data["display_name"]
        formt = FORMATS.get(channel_data["type"], "{}")
        room_name = formt.format(channel_data["display_name"])
    else:
        match = re.match('(\w+)__(\w+)', channel_data["name"])
        if match:
-- 
2.26.2

[PATCH wee-matter 4/6] fix marking channels as read Export this patch

From: Midgard <2885-Midgard@users.noreply.framagit.org>

---
 wee_matter/http.py | 10 +++++++---
 wee_matter/room.py |  4 +++-
 2 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/wee_matter/http.py b/wee_matter/http.py
index 6cf9c58..07f454a 100644
--- a/wee_matter/http.py
+++ b/wee_matter/http.py
@@ -215,12 +215,16 @@ def run_get_channel_member(channel_id, member_id, server, cb, cb_data):
        cb_data
    )

def run_post_user_post_unread(user_id, post_id, server, cb, cb_data):
    url = wee_matter.server.server_root_url(server) + "/api/v4/users/" + user_id + "/posts/" + post_id + "/set_unread"
def run_post_channel_view(user_id, channel_id, server, cb, cb_data):
    url = wee_matter.server.server_root_url(server) + "/api/v4/channels/members/" + user_id + "/view"
    params = {
        "channel_id": channel_id,
    }

    weechat.hook_process_hashtable(
        "url:" + url,
        {
            "post": "1",
            "postfields": json.dumps(params),
            "failonerror": "1",
            "httpheader": "Authorization: Bearer " + server.user_token,
        },
diff --git a/wee_matter/room.py b/wee_matter/room.py
index 2c905c6..e5b12bc 100644
--- a/wee_matter/room.py
+++ b/wee_matter/room.py
@@ -41,13 +41,15 @@ def channel_completion_cb(data, completion_item, current_buffer, completion):

def mark_channel_as_read(buffer):
    server = wee_matter.server.get_server_from_buffer(buffer)
    channel_id = weechat.buffer_get_string(buffer, "localvar_channel_id")

    last_post_id = weechat.buffer_get_string(buffer, "localvar_last_post_id")
    last_read_post_id = weechat.buffer_get_string(buffer, "localvar_last_read_post_id")
    if last_post_id == last_read_post_id: # prevent spamming on buffer switch
        return

    wee_matter.http.run_post_user_post_unread(server.user.id, last_post_id, server, "singularity_cb", "")
    wee_matter.http.run_post_channel_view(server.user.id, channel_id, server, "singularity_cb", "")

    weechat.buffer_set(buffer, "localvar_set_last_read_post_id", last_post_id)

def room_input_cb(data, buffer, input_data):
-- 
2.26.2

[PATCH wee-matter 5/6] add editorconfig Export this patch

From: Midgard <2885-Midgard@users.noreply.framagit.org>

---
 .editorconfig | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
 create mode 100644 .editorconfig

diff --git a/.editorconfig b/.editorconfig
new file mode 100644
index 0000000..47d7c1d
--- /dev/null
+++ b/.editorconfig
@@ -0,0 +1,14 @@
# https://editorconfig.org

root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4

[Makefile]
indent_style = tab
indent_size = unset
-- 
2.26.2
Eddie Barraco
I merged some of your patches. Thanks a lot !

To git.sr.ht:~reedwade/wee-matter
   a279374..2321988  2321988 -> master

I worked on the buffered response on my way. I'll send a patch
in another thread. Could you test it ?

[PATCH wee-matter 6/6] declare a dependency that I had to install Export this patch

From: Midgard <2885-Midgard@users.noreply.framagit.org>

---
 README.md | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/README.md b/README.md
index e7d5aff..b4cc86b 100644
--- a/README.md
+++ b/README.md
@@ -4,6 +4,10 @@ Wee-Matter is a Mattermost backend to Weechat.

## Installation

Python dependencies from PyPi:

* [websocket_client](https://pypi.org/project/websocket_client/)

```bash
$ make install # WEECHAT_HOME=$HOME/.another-weechat
```
-- 
2.26.2
Thanks a lot fot those patches.

I'll definitely have a look at it later this day or this week-end.

Your work on the buffer on splitted response feels good. I'll try to
a refact to clean up just a little bit.