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

Use mapped types for account data content #4590

Merged
merged 12 commits into from
Dec 19, 2024
Merged

Conversation

t3chguy
Copy link
Member

@t3chguy t3chguy commented Dec 17, 2024

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
@richvdh
Copy link
Member

richvdh commented Dec 17, 2024

FYI, I hate the fact that we refer to account data as "events". It's not an event by any reasonable definition of the word "event". (You could argue that a change in account data might count as an event when it is sent down /sync, but that's about as far as it goes.) matrix-org/matrix-spec#2030 and matrix-org/matrix-spec#897 contain some of my previous complaining about it.

I appreciate it might be scope-creeping this PR, and diverging from the current wording of the spec, in ways you don't want to take on, but if there was a way to create this PR without propagating the word "Event" further than we have to, it would make me much happier.

@t3chguy
Copy link
Member Author

t3chguy commented Dec 17, 2024

I think for js-sdk naming consistency, given they use MatrixEvent under the hood, it should be tackled more holistically. Maybe even starting at the spec level. The spec does indeed seem to refer to Account Data events as message events in some places https://spec.matrix.org/v1.12/client-server-api/#midentity_server https://spec.matrix.org/v1.12/client-server-api/#mfully_read so definitely needs a clean up.

@richvdh
Copy link
Member

richvdh commented Dec 17, 2024

Using MatrixEvent for account data seems like the world's worst idea, but I can't disagree with your conclusion that fixing it is a job for another day. I was just wondering if we can prepare for that day in any way that would minimise future breaking changes.

Anyway, will butt out.

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
Copy link
Member

@richvdh richvdh left a comment

Choose a reason for hiding this comment

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

I applaud the effort, but to make best use of it, it needs better documentation imho

src/@types/event.ts Outdated Show resolved Hide resolved
src/@types/event.ts Show resolved Hide resolved
src/@types/event.ts Show resolved Hide resolved
src/secret-storage.ts Outdated Show resolved Hide resolved
src/@types/common.ts Outdated Show resolved Hide resolved
@@ -200,7 +202,9 @@ export interface SecretStorageCallbacks {
) => Promise<[string, Uint8Array] | null>;
}

interface SecretInfo {
export type SecretInfoKey = Assignable<AccountDataEvents, SecretInfo>;
Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't it be easier/clearer/make more sense generally to define SecretInfoKey explicitly, and derive AccountData from that, rather than the other way around?

Copy link
Member Author

@t3chguy t3chguy Dec 19, 2024

Choose a reason for hiding this comment

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

That wouldn't be extensible by consumers of this library, only interfaces are extensible via declaration merging.

https://www.typescriptlang.org/docs/handbook/declaration-merging.html

Copy link
Member

Choose a reason for hiding this comment

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

Even so, it seems like it would be more intuitive to declare:

export interface SecretStorageAccountDataEvents {
    "m.cross_signing.master": SecretInfo;
    "m.cross_signing.self_signing": SecretInfo;
    "m.cross_signing.user_signing": SecretInfo;
    // etc
}

... and then derive AccountDataEvents and SecretStorageKey from that, rather than messing about with Assignable.

But it's not a strong opinion.

src/secret-storage.ts Outdated Show resolved Hide resolved
Comment on lines +19 to +21
/// The event type storing the user's individual policies.
///
/// Exported for testing purposes.
Copy link
Member

Choose a reason for hiding this comment

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

I don't think /// works for ts doc?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is just copied out of invites-ignorer.ts verbatim to avoid an import cycle

src/client.ts Show resolved Hide resolved
Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
@richvdh richvdh changed the title Use mapped types around account data events Use mapped types for account data content Dec 19, 2024
Copy link
Member

@richvdh richvdh left a comment

Choose a reason for hiding this comment

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

LGTM otherwise

@@ -4256,7 +4256,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
* @param eventType - The event type
* @returns The contents of the given account data event
*/
public getAccountData(eventType: string): MatrixEvent | undefined {
public getAccountData<K extends keyof AccountDataEvents>(eventType: K): MatrixEvent | undefined {
Copy link
Member

Choose a reason for hiding this comment

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

Extending this PR to cover fetching account data is fine, but it's a significant scope creep. Adding it in the middle of a review cycle doesn't make a reviewer's job any easier. It'd be good just to make another PR next time.

@@ -200,7 +202,9 @@ export interface SecretStorageCallbacks {
) => Promise<[string, Uint8Array] | null>;
}

interface SecretInfo {
export type SecretInfoKey = Assignable<AccountDataEvents, SecretInfo>;
Copy link
Member

Choose a reason for hiding this comment

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

Even so, it seems like it would be more intuitive to declare:

export interface SecretStorageAccountDataEvents {
    "m.cross_signing.master": SecretInfo;
    "m.cross_signing.self_signing": SecretInfo;
    "m.cross_signing.user_signing": SecretInfo;
    // etc
}

... and then derive AccountDataEvents and SecretStorageKey from that, rather than messing about with Assignable.

But it's not a strong opinion.

src/secret-storage.ts Show resolved Hide resolved
src/@types/common.ts Outdated Show resolved Hide resolved
src/models/invites-ignorer-types.ts Outdated Show resolved Hide resolved
t3chguy and others added 4 commits December 19, 2024 22:24
Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com>
Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
@t3chguy t3chguy added this pull request to the merge queue Dec 19, 2024
Merged via the queue into develop with commit 3fcc566 Dec 19, 2024
29 checks passed
@t3chguy t3chguy deleted the t3chguy/types-account-data branch December 19, 2024 23:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-Task Tasks for the team like planning
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants