summaryrefslogtreecommitdiffstats
path: root/comm/chat/protocols/matrix/matrixPowerLevels.sys.mjs
blob: 4ee10274289fc61dcc8e7726b273bd7b6a4a4491 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";
import { l10nHelper } from "resource:///modules/imXPCOMUtils.sys.mjs";

const lazy = {};

XPCOMUtils.defineLazyGetter(lazy, "_", () =>
  l10nHelper("chrome://chat/locale/matrix.properties")
);

// See https://matrix.org/docs/spec/client_server/r0.5.0#m-room-power-levels
export var MatrixPowerLevels = {
  user: 0,
  voice: 10,
  moderator: 50,
  admin: 100,
  /**
   * Turns a power level into a human readable string.
   * Only exactly matching level names are returned, except for restricted
   * power levels.
   *
   * @param {number} powerLevel - Power level to format.
   * @param {number} [defaultLevel=0] - The default power level in the room.
   * @returns {string} Representation of the power level including the raw level.
   */
  toText(powerLevel, defaultLevel = MatrixPowerLevels.user) {
    let levelName = lazy._("powerLevel.custom");
    if (powerLevel == MatrixPowerLevels.admin) {
      levelName = lazy._("powerLevel.admin");
    } else if (powerLevel == MatrixPowerLevels.moderator) {
      levelName = lazy._("powerLevel.moderator");
    } else if (powerLevel < defaultLevel) {
      levelName = lazy._("powerLevel.restricted");
    } else if (powerLevel == defaultLevel) {
      levelName = lazy._("powerLevel.default");
    }
    return lazy._("powerLevel.detailed", levelName, powerLevel);
  },
  /**
   * @param {object} powerLevels - m.room.power_levels event contents.
   * @param {string} key - Power level key to get.
   * @returns {number} The power level if given in the event, else 0.
   */
  _getDefaultLevel(powerLevels, key) {
    const fullKey = `${key}_default`;
    if (Number.isSafeInteger(powerLevels?.[fullKey])) {
      return powerLevels[fullKey];
    }
    return 0;
  },
  /**
   * @param {object} powerLevels - m.room.power_levels event contents.
   * @returns {number} The default power level of users in the room.
   */
  getUserDefaultLevel(powerLevels) {
    return this._getDefaultLevel(powerLevels, "users");
  },
  /**
   *
   * @param {object} powerLevels - m.room.power_levels event contents.
   * @returns {number} The default power level required to send events in the
   *  room.
   */
  getEventDefaultLevel(powerLevels) {
    return this._getDefaultLevel(powerLevels, "events");
  },
  /**
   *
   * @param {object} powerLevels - m.room.power_levels event contents.
   * @param {string} event - Event ID to get the required power level for.
   * @returns {number} The power level required to send this event in the room.
   */
  getEventLevel(powerLevels, event) {
    if (Number.isSafeInteger(powerLevels?.events?.[event])) {
      return powerLevels.events[event];
    }
    return this.getEventDefaultLevel(powerLevels);
  },
};