Move all polls processing to events-sdk & prep for stable polls (#7517)

* Move all polls processing to events-sdk

This makes polls support the full range of extensible events (both parsing and generation).

* Appease the linter

* Fix & update tests

* Update events-sdk for polls bugfix

* Update events-sdk for typechecking

* Add missing type cast

* Update per review
This commit is contained in:
Travis Ralston 2022-01-17 10:06:30 -07:00 committed by GitHub
parent 12e967a97c
commit 65987e6b72
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 727 additions and 299 deletions

View file

@ -17,7 +17,7 @@ limitations under the License.
import { Room } from "matrix-js-sdk/src/models/room";
import { isNullOrUndefined } from "matrix-js-sdk/src/utils";
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
import { POLL_START_EVENT_TYPE } from "matrix-js-sdk/src/@types/polls";
import { M_POLL_START } from "matrix-events-sdk";
import { ActionPayload } from "../../dispatcher/payloads";
import { AsyncStoreWithClient } from "../AsyncStoreWithClient";
@ -68,7 +68,11 @@ function previews(): Object {
// TODO: when polls comes out of labs, add this to PREVIEWS
if (SettingsStore.getValue("feature_polls")) {
return {
[POLL_START_EVENT_TYPE.name]: {
[M_POLL_START.name]: {
isState: false,
previewer: new PollStartEventPreview(),
},
[M_POLL_START.altName]: {
isState: false,
previewer: new PollStartEventPreview(),
},

View file

@ -15,8 +15,7 @@ limitations under the License.
*/
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
import { POLL_START_EVENT_TYPE } from "matrix-js-sdk/src/@types/polls";
import { TEXT_NODE_TYPE } from "matrix-js-sdk/src/@types/extensible_events";
import { InvalidEventError, M_POLL_START_EVENT_CONTENT, PollStartEvent } from "matrix-events-sdk";
import { IPreview } from "./IPreview";
import { TagID } from "../models";
@ -37,25 +36,31 @@ export class PollStartEventPreview implements IPreview {
}
// Check we have the information we need, and bail out if not
if (!eventContent || !eventContent[POLL_START_EVENT_TYPE.name]) {
if (!eventContent) {
return null;
}
let question =
eventContent[POLL_START_EVENT_TYPE.name].question[TEXT_NODE_TYPE.name];
question = (question || '').trim();
question = sanitizeForTranslation(question);
try {
const poll = new PollStartEvent({
type: event.getType(),
content: eventContent as M_POLL_START_EVENT_CONTENT,
});
if (
isThread ||
isSelf(event) ||
!shouldPrefixMessagesIn(event.getRoomId(), tagId)
) {
return question;
} else {
return _t("%(senderName)s: %(message)s",
{ senderName: getSenderName(event), message: question },
);
let question = poll.question.text.trim();
question = sanitizeForTranslation(question);
if (isThread || isSelf(event) || !shouldPrefixMessagesIn(event.getRoomId(), tagId)) {
return question;
} else {
return _t("%(senderName)s: %(message)s",
{ senderName: getSenderName(event), message: question },
);
}
} catch (e) {
if (e instanceof InvalidEventError) {
return null;
}
throw e; // re-throw unknown errors
}
}
}