Skip to content

Commit

Permalink
Fix Bulk registration invalid MXID in requests, fixes #30 (#33)
Browse files Browse the repository at this point in the history
* Fix Bulk registration invalid MXID in requests

* update readme
  • Loading branch information
aine-etke authored Sep 17, 2024
1 parent 66c7065 commit 6957cb1
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ The following changes are already implemented:
* [Federation page improvements](https://github.com/Awesome-Technologies/synapse-admin/pull/583) (using theme colors)
* [Add UI option to block deleted rooms from being rejoined](https://github.com/etkecc/synapse-admin/pull/26)
* [Fix required fields check on Bulk registration CSV upload](https://github.com/etkecc/synapse-admin/pull/32)
* [Fix requests with invalid MXIDs on Bulk registration](https://github.com/etkecc/synapse-admin/pull/33)

_the list will be updated as new changes are added_

Expand Down
11 changes: 7 additions & 4 deletions src/components/ImportFeature.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
import { DataProvider, useTranslate } from "ra-core";
import { useDataProvider, useNotify, RaRecord, Title } from "react-admin";

import { generateRandomMxId, generateRandomPassword } from "../synapse/synapse";
import { generateRandomMxId, generateRandomPassword, returnMXID } from "../synapse/synapse";

const LOGGING = true;

Expand Down Expand Up @@ -74,7 +74,7 @@ const FilePicker = () => {

const [conflictMode, setConflictMode] = useState("stop");
const [passwordMode, setPasswordMode] = useState(true);
const [useridMode, setUseridMode] = useState("ignore");
const [useridMode, setUseridMode] = useState("update");

const translate = useTranslate();
const notify = useNotify();
Expand Down Expand Up @@ -266,12 +266,15 @@ const FilePicker = () => {
const userRecord = { ...entry };
// No need to do a bunch of cryptographic random number getting if
// we are using neither a generated password nor a generated user id.
if (useridMode === "ignore" || userRecord.id === undefined) {
if (useridMode === "ignore" || userRecord.id === undefined || userRecord.id === "") {
userRecord.id = generateRandomMxId();
}
if (passwordMode === false || entry.password === undefined) {
if (passwordMode === false || entry.password === undefined || entry.password === "") {
userRecord.password = generateRandomPassword();
}
// we want to ensure that the ID is always full MXID, otherwise randomly-generated MXIDs will be in the full
// form, but the ones from the CSV will be localpart-only.
userRecord.id = returnMXID(userRecord.id);
/* TODO record update stats (especially admin no -> yes, deactivated x -> !x, ... */

/* For these modes we will consider the ID that's in the record.
Expand Down
13 changes: 7 additions & 6 deletions src/synapse/dataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { stringify } from "query-string";
import { DataProvider, DeleteParams, HttpError, Identifier, Options, RaRecord, fetchUtils } from "react-admin";

import storage from "../storage";
import { returnMXID } from "./synapse.ts"
import { MatrixError, displayError } from "../components/error";

// Adds the access token to all requests
Expand Down Expand Up @@ -234,7 +235,7 @@ const resourceMap = {
path: "/_synapse/admin/v2/users",
map: (u: User) => ({
...u,
id: u.name,
id: returnMXID(u.name),
avatar_src: u.avatar_url ? mxcUrlToHttp(u.avatar_url) : undefined,
is_guest: !!u.is_guest,
admin: !!u.admin,
Expand All @@ -245,12 +246,12 @@ const resourceMap = {
data: "users",
total: json => json.total,
create: (data: RaRecord) => ({
endpoint: `/_synapse/admin/v2/users/@${encodeURIComponent(data.id)}:${storage.getItem("home_server")}`,
endpoint: `/_synapse/admin/v2/users/${encodeURIComponent(returnMXID(data.id))}`,
body: data,
method: "PUT",
}),
delete: (params: DeleteParams) => ({
endpoint: `/_synapse/admin/v1/deactivate/${encodeURIComponent(params.id)}`,
endpoint: `/_synapse/admin/v1/deactivate/${encodeURIComponent(returnMXID(params.id))}`,
body: { erase: true },
method: "POST",
}),
Expand Down Expand Up @@ -349,7 +350,7 @@ const resourceMap = {
id: um.media_id,
}),
reference: (id: Identifier) => ({
endpoint: `/_synapse/admin/v1/users/${encodeURIComponent(id)}/media`,
endpoint: `/_synapse/admin/v1/users/${encodeURIComponent(returnMXID(id))}/media`,
}),
data: "media",
total: json => json.total,
Expand Down Expand Up @@ -384,7 +385,7 @@ const resourceMap = {
create: (data: RaServerNotice) => ({
endpoint: "/_synapse/admin/v1/send_server_notice",
body: {
user_id: data.id,
user_id: returnMXID(data.id),
content: {
msgtype: "m.text",
body: data.body,
Expand All @@ -397,7 +398,7 @@ const resourceMap = {
path: "/_synapse/admin/v1/statistics/users/media",
map: (usms: UserMediaStatistic) => ({
...usms,
id: usms.user_id,
id: returnMXID(usms.user_id),
}),
data: "users",
total: json => json.total,
Expand Down
20 changes: 20 additions & 0 deletions src/synapse/synapse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,26 @@ export function generateRandomMxId(): string {
return `@${localpart}:${homeserver}`;
}

/**
* Return the full MXID from an arbitrary input
* @param input the input string
* @returns full MXID as string
*/
export function returnMXID(input: string): string {
const homeserver = storage.getItem("home_server");

// Check if the input already looks like a valid MXID (i.e., starts with "@" and contains ":")
const mxidPattern = /^@[^@:]+:[^@:]+$/;
if (mxidPattern.test(input)) {
return input; // Already a valid MXID
}

// If input is not a valid MXID, assume it's a localpart and construct the MXID
const localpart = input.startsWith('@') ? input.slice(1) : input;
return `@${localpart}:${homeserver}`;
}


/**
* Generate a random user password
* @returns a new random password as string
Expand Down

0 comments on commit 6957cb1

Please sign in to comment.