Matrix-appservice-irc: Automatically rename users when the config displayname format changes

Created on 25 Jan 2021  路  7Comments  路  Source: matrix-org/matrix-appservice-irc

hello,

i would like to rename my exits virtual matrix users (the displayname).
currently these are called "$NICK (IRC)". I would prefer "$NICK" though.

Is it possible and if so how can I do it?

in the config I can change it here.
https://github.com/matrix-org/matrix-appservice-irc/blob/develop/config.sample.yaml#L300
this only applies to new virtual users. the existing users are still called "$NICK (IRC)"

many Thanks!

feature-req

Most helpful comment

OK, got that to work. In case anyone else wants to do something similar, this is the Python script I used (to just reset everyone's display name to their IRC nick):

import requests
import sys
from urllib.parse import quote

TOKEN="<bridge as token>"
BRIDGE_HOST="<bridge hostname>"

def set_displayname(username):
    try:
        user, host = username.split(":")
        prefix, suffix = user.split("_", 1)
    except ValueError:
        print(f"Skipping {username}")
        return

    resp = requests.put(f"https://{BRIDGE_HOST}/_matrix/client/r0/profile/{quote(username)}/displayname?user_id={quote(username)}",
                        headers={"Authorization": f"Bearer {TOKEN}"},
                        json={"displayname": suffix})
    if resp.ok:
        print(f"Set {username} displayname to {suffix}")
    else:
        print(f"Error setting displayname for {username}: {resp.json()['error']}")

if __name__ == "__main__":
    for u in sys.argv[1:]:
        set_displayname(u)

To use, just run with the user names (Matrix IDs) of the users to reset. I got a list of those by issuing psql matrix_irc -c 'select user_id from matrix_users' > /tmp/userlist.txt - clean up the file (remove the header and the count at the bottom) and run the script with xargs python reset-matrix-users.py < userlist.txt

All 7 comments

This currently isn't done automagically, although it could be in the future.

In the meantime, is there a way to perform this update manually? Can I just update the displayName property in the matrix_users table in the database? Or is it better to clear out the content of that table? Or will neither of those work?

Those won't work. You'll need to script something to update each user by iterating over what's the in the DB and doing a profile update request to synapse.

Will Hunt notifications@github.com writes:

Those won't work. You'll need to script something to update each user
by iterating over what's the in the DB and doing a profile update
request to synapse.

Right, thought that might be the case. OK, so if I just rename the users
on Synapse will the bridge automatically update the database, or do I
need to update the DB manually as well?

OK, got that to work. In case anyone else wants to do something similar, this is the Python script I used (to just reset everyone's display name to their IRC nick):

import requests
import sys
from urllib.parse import quote

TOKEN="<bridge as token>"
BRIDGE_HOST="<bridge hostname>"

def set_displayname(username):
    try:
        user, host = username.split(":")
        prefix, suffix = user.split("_", 1)
    except ValueError:
        print(f"Skipping {username}")
        return

    resp = requests.put(f"https://{BRIDGE_HOST}/_matrix/client/r0/profile/{quote(username)}/displayname?user_id={quote(username)}",
                        headers={"Authorization": f"Bearer {TOKEN}"},
                        json={"displayname": suffix})
    if resp.ok:
        print(f"Set {username} displayname to {suffix}")
    else:
        print(f"Error setting displayname for {username}: {resp.json()['error']}")

if __name__ == "__main__":
    for u in sys.argv[1:]:
        set_displayname(u)

To use, just run with the user names (Matrix IDs) of the users to reset. I got a list of those by issuing psql matrix_irc -c 'select user_id from matrix_users' > /tmp/userlist.txt - clean up the file (remove the header and the count at the bottom) and run the script with xargs python reset-matrix-users.py < userlist.txt

Thanks for the excellent workaround, @tohojo!

Is there any danger to leaving display_name unmodified in the database?

Joe Hermaszewski @.*> writes:

Thanks for the excellent workaround, @tohojo!

You're welcome!

Is there any danger to leaving display_name unmodified in the database?

None that I have noticed...

Was this page helpful?
0 / 5 - 0 ratings