Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ease usage of a another matrix username #10

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

pierreozoux
Copy link

relates to: #5

then I have a file like this:

src/helpers/ih.ts
/*àçéèêëîïñô*/
let latin_map = new Map<string, string>([
  ["à", "a"],
  ["ç", "c"],
  ["é", "e"],
  ["è", "e"],
  ["ë", "e"],
  ["ê", "e"],
  ["î", "i"],
  ["ï", "i"],
  ["ñ", "n"],
  ["ô", "o"],
]);

function latinise(s: string): string{
  return s.replace(/[^A-Za-z0-9\[\] ]/g,function(char){return latin_map.get(char)||char})
};

export function customConvertion(u: string): string{
  return latinise(u.replace(/é/,'_')).replace(/@toot|@bob|@rr|@toto/,'_').replace(/\.io|\.org|\.fr|\.com|\.net|@.*|[^\.a-z0-9_-]/g,'');
};

and then in users.ts, I do (liiibre is the name of my sso):

...
import { axios } from '../helpers/synapse'
import { customConvertion } from '../helpers/ih'

export type RcUser = {
  _id: string
  username: string
  name: string
  services: {
    liiibre: {
      preferred_username: string
    }
  }
  roles: string[]
  __rooms: string[]
}

export type MatrixUser = {
  user_id: string
  username: string
  displayname: string
  password: string
  admin: boolean
  nonce?: string
  mac?: string
  access_token?: string
}

export type AccessToken = {
  access_token: string
  device_id: string
  home_server: string
  user_id: string
}

export function mapUser(rcUser: RcUser): MatrixUser {
  if (!rcUser.services.liiibre.preferred_username) {
    log.debug(`${rcUser.name}  ${rcUser.username} ${rcUser.services.liiibre.preferred_username} has empty username`)
  }

  return {
    user_id: '',
    username: customConvertion(rcUser.services.liiibre.preferred_username),
    displayname: rcUser.name,
...

The rest, I don't know how to PR it, but the part of the PR can hopefully helps!
(Sorry, I'm no ts expert :/ )

@HerHde
Copy link
Collaborator

HerHde commented Jun 18, 2024

Have you considered using a library to latinise the strings? https://github.com/sindresorhus/slugify looks good for that purpose, it has the same results as latin_map, only é translates to e, but it also has custom replacements. May I ask for the reason behind the custom conversions?

@pierreozoux
Copy link
Author

For my specific use case, I needed an implementation in js, ts and sql, so it was easier to implement it myself

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants