check power levels to see if we can react
This commit is contained in:
parent
fd54539e1c
commit
099f99a96b
3 changed files with 61 additions and 8 deletions
|
@ -119,8 +119,7 @@ export class BaseMessageTile extends SimpleTile {
|
||||||
}
|
}
|
||||||
|
|
||||||
get canReact() {
|
get canReact() {
|
||||||
// TODO
|
return this._powerLevels.canSendType("m.reaction");
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
react(key, log = null) {
|
react(key, log = null) {
|
||||||
|
|
|
@ -29,8 +29,16 @@ export class PowerLevels {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
canSendType(eventType) {
|
||||||
|
return this._myLevel >= this._getEventTypeLevel(eventType);
|
||||||
|
}
|
||||||
|
|
||||||
get canRedact() {
|
get canRedact() {
|
||||||
return this._getUserLevel(this._ownUserId) >= this._getActionLevel("redact");
|
return this._myLevel >= this._getActionLevel("redact");
|
||||||
|
}
|
||||||
|
|
||||||
|
get _myLevel() {
|
||||||
|
return this._getUserLevel(this._ownUserId);
|
||||||
}
|
}
|
||||||
|
|
||||||
_getUserLevel(userId) {
|
_getUserLevel(userId) {
|
||||||
|
@ -59,25 +67,51 @@ export class PowerLevels {
|
||||||
return 50;
|
return 50;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_getEventTypeLevel(eventType) {
|
||||||
|
const level = this._plEvent?.content.events?.[eventType];
|
||||||
|
if (typeof level === "number") {
|
||||||
|
return level;
|
||||||
|
} else {
|
||||||
|
const level = this._plEvent?.content.events_default;
|
||||||
|
if (typeof level === "number") {
|
||||||
|
return level;
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function tests() {
|
export function tests() {
|
||||||
const alice = "@alice:hs.tld";
|
const alice = "@alice:hs.tld";
|
||||||
const bob = "@bob:hs.tld";
|
const bob = "@bob:hs.tld";
|
||||||
|
const charly = "@charly:hs.tld";
|
||||||
const createEvent = {content: {creator: alice}};
|
const createEvent = {content: {creator: alice}};
|
||||||
const powerLevelEvent = {content: {
|
const redactPowerLevelEvent = {content: {
|
||||||
redact: 50,
|
redact: 50,
|
||||||
users: {
|
users: {
|
||||||
[alice]: 50
|
[alice]: 50
|
||||||
},
|
},
|
||||||
users_default: 0
|
users_default: 0
|
||||||
}};
|
}};
|
||||||
|
const eventsPowerLevelEvent = {content: {
|
||||||
|
events_default: 5,
|
||||||
|
events: {
|
||||||
|
"m.room.message": 45
|
||||||
|
},
|
||||||
|
users: {
|
||||||
|
[alice]: 50,
|
||||||
|
[bob]: 10
|
||||||
|
},
|
||||||
|
users_default: 0
|
||||||
|
}};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"redact somebody else event with power level event": assert => {
|
"redact somebody else event with power level event": assert => {
|
||||||
const pl1 = new PowerLevels({powerLevelEvent, ownUserId: alice});
|
const pl1 = new PowerLevels({powerLevelEvent: redactPowerLevelEvent, ownUserId: alice});
|
||||||
assert.equal(pl1.canRedact, true);
|
assert.equal(pl1.canRedact, true);
|
||||||
const pl2 = new PowerLevels({powerLevelEvent, ownUserId: bob});
|
const pl2 = new PowerLevels({powerLevelEvent: redactPowerLevelEvent, ownUserId: bob});
|
||||||
assert.equal(pl2.canRedact, false);
|
assert.equal(pl2.canRedact, false);
|
||||||
},
|
},
|
||||||
"redact somebody else event with create event": assert => {
|
"redact somebody else event with create event": assert => {
|
||||||
|
@ -91,5 +125,22 @@ export function tests() {
|
||||||
assert.equal(pl.canRedactFromSender(alice), true);
|
assert.equal(pl.canRedactFromSender(alice), true);
|
||||||
assert.equal(pl.canRedactFromSender(bob), false);
|
assert.equal(pl.canRedactFromSender(bob), false);
|
||||||
},
|
},
|
||||||
|
"can send event without power levels": assert => {
|
||||||
|
const pl = new PowerLevels({createEvent, ownUserId: charly});
|
||||||
|
assert.equal(pl.canSendType("m.room.message"), true);
|
||||||
|
},
|
||||||
|
"can't send any event below events_default": assert => {
|
||||||
|
const pl = new PowerLevels({powerLevelEvent: eventsPowerLevelEvent, ownUserId: charly});
|
||||||
|
assert.equal(pl.canSendType("m.foo"), false);
|
||||||
|
},
|
||||||
|
"can't send event below events[type]": assert => {
|
||||||
|
const pl = new PowerLevels({powerLevelEvent: eventsPowerLevelEvent, ownUserId: bob});
|
||||||
|
assert.equal(pl.canSendType("m.foo"), true);
|
||||||
|
assert.equal(pl.canSendType("m.room.message"), false);
|
||||||
|
},
|
||||||
|
"can send event below events[type]": assert => {
|
||||||
|
const pl = new PowerLevels({powerLevelEvent: eventsPowerLevelEvent, ownUserId: alice});
|
||||||
|
assert.equal(pl.canSendType("m.room.message"), true);
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -110,12 +110,15 @@ export class BaseMessageView extends TemplateView {
|
||||||
|
|
||||||
createMenuOptions(vm) {
|
createMenuOptions(vm) {
|
||||||
const options = [];
|
const options = [];
|
||||||
|
if (vm.canReact) {
|
||||||
|
options.push(Menu.option(vm.i18n`React with 👍`, () => vm.react("👍")))
|
||||||
|
options.push(Menu.option(vm.i18n`React with 🙈`, () => vm.react("🙈")))
|
||||||
|
}
|
||||||
if (vm.canAbortSending) {
|
if (vm.canAbortSending) {
|
||||||
options.push(Menu.option(vm.i18n`Cancel`, () => vm.abortSending()));
|
options.push(Menu.option(vm.i18n`Cancel`, () => vm.abortSending()));
|
||||||
} else if (vm.canRedact) {
|
} else if (vm.canRedact) {
|
||||||
options.push(Menu.option(vm.i18n`Delete`, () => vm.redact()).setDestructive());
|
options.push(Menu.option(vm.i18n`Delete`, () => vm.redact()).setDestructive());
|
||||||
}
|
}
|
||||||
options.push(Menu.option(vm.i18n`React with 👍`, () => vm.react("👍")))
|
|
||||||
return options;
|
return options;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue