Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Polls: show warning about undecryptable relations #10179

Merged
merged 9 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/components/views/messages/MPollBody.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,14 @@ export default class MPollBody extends React.Component<IBodyProps, IState> {
private addListeners(): void {
this.state.poll?.on(PollEvent.Responses, this.onResponsesChange);
this.state.poll?.on(PollEvent.End, this.onRelationsChange);
this.state.poll?.on(PollEvent.UndecryptableRelations, this.render.bind(this));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of calling render, would it be better to add the undecryptable event count to the state?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that this is kind of weird, but I also can't think of a concrete reason that duplicating the event count in MPollBody state would be better.

}

private removeListeners(): void {
if (this.state.poll) {
this.state.poll.off(PollEvent.Responses, this.onResponsesChange);
this.state.poll.off(PollEvent.End, this.onRelationsChange);
this.state.poll.off(PollEvent.UndecryptableRelations, this.render.bind(this));
}
}

Expand Down Expand Up @@ -297,7 +299,9 @@ export default class MPollBody extends React.Component<IBodyProps, IState> {
const showResults = poll.isEnded || (disclosed && myVote !== undefined);

let totalText: string;
if (poll.isEnded) {
if (showResults && poll.undecryptableRelationsCount) {
totalText = _t("Due to decryption errors, some votes may not be counted");
} else if (poll.isEnded) {
totalText = _t("Final result based on %(count)s votes", { count: totalVotes });
} else if (!disclosed) {
totalText = _t("Results will be visible when the poll is ended");
Expand Down
1 change: 1 addition & 0 deletions src/i18n/strings/en_EN.json
Original file line number Diff line number Diff line change
Expand Up @@ -2412,6 +2412,7 @@
"Sorry, you can't edit a poll after votes have been cast.": "Sorry, you can't edit a poll after votes have been cast.",
"Vote not registered": "Vote not registered",
"Sorry, your vote was not registered. Please try again.": "Sorry, your vote was not registered. Please try again.",
"Due to decryption errors, some votes may not be counted": "Due to decryption errors, some votes may not be counted",
"Final result based on %(count)s votes|other": "Final result based on %(count)s votes",
"Final result based on %(count)s votes|one": "Final result based on %(count)s vote",
"Results will be visible when the poll is ended": "Results will be visible when the poll is ended",
Expand Down
20 changes: 20 additions & 0 deletions src/utils/poll/countPollDecryptionFailures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
Copyright 2023 The Matrix.org Foundation C.I.C.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import { Relations } from "matrix-js-sdk/src/models/relations";

export const countPollDecryptionFailures = (responseRelations: Relations): number =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cannot find any usage of this in the PR.
Does a poll not already provide the number here?

responseRelations.getRelations().filter((relationEvent) => relationEvent.isDecryptionFailure()).length;
14 changes: 14 additions & 0 deletions test/components/views/messages/MPollBody-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,20 @@ describe("MPollBody", () => {
expect(container).toMatchSnapshot();
});

it("renders a warning message when poll has undecryptable relations", async () => {
const votes = [
responseEvent("@op:example.com", "pizza", 12),
responseEvent("@op:example.com", [], 13),
responseEvent("@op:example.com", "italian", 14),
responseEvent("@me:example.com", "wings", 15),
responseEvent("@qr:example.com", "italian", 16),
];

jest.spyOn(votes[1], "isDecryptionFailure").mockReturnValue(true);
const { getByText } = await newMPollBody(votes);
expect(getByText("Due to decryption errors, some votes may not be counted")).toBeInTheDocument();
});

it("renders a poll with local, non-local and invalid votes", async () => {
const votes = [
responseEvent("@a:example.com", "pizza", 12),
Expand Down