-
-
Notifications
You must be signed in to change notification settings - Fork 623
Use mapped types for account data content #4590
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
50b9b67
Use mapped types around account data events
t3chguy b5d1cbd
Iterate
t3chguy 276fbe0
Iterate
t3chguy ae935eb
Iterate
t3chguy d28bf64
Iterate
t3chguy a1635c5
Iterate
t3chguy 49a43fc
Harden types for reading account data too
t3chguy cf0ee8b
Correct empty object type
t3chguy 0e5012d
Update src/secret-storage.ts
t3chguy f72f5b0
Iterate
t3chguy 7040e75
Merge remote-tracking branch 'origin/t3chguy/types-account-data' into…
t3chguy e4f9ddf
Iterate
t3chguy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -136,6 +136,7 @@ import { | |
UpdateDelayedEventAction, | ||
} from "./@types/requests.ts"; | ||
import { | ||
AccountDataEvents, | ||
EventType, | ||
LOCAL_NOTIFICATION_SETTINGS_PREFIX, | ||
MSC3912_RELATION_BASED_REDACTIONS_PROP, | ||
|
@@ -232,6 +233,7 @@ import { | |
import { DeviceInfoMap } from "./crypto/DeviceList.ts"; | ||
import { | ||
AddSecretStorageKeyOpts, | ||
SecretStorageKey, | ||
SecretStorageKeyDescription, | ||
ServerSideSecretStorage, | ||
ServerSideSecretStorageImpl, | ||
|
@@ -3070,7 +3072,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa | |
* | ||
* @deprecated Use {@link MatrixClient#secretStorage} and {@link SecretStorage.ServerSideSecretStorage#isStored}. | ||
*/ | ||
public isSecretStored(name: string): Promise<Record<string, SecretStorageKeyDescription> | null> { | ||
public isSecretStored(name: SecretStorageKey): Promise<Record<string, SecretStorageKeyDescription> | null> { | ||
return this.secretStorage.isStored(name); | ||
} | ||
|
||
|
@@ -4236,7 +4238,10 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa | |
* @returns Promise which resolves: an empty object | ||
* @returns Rejects: with an error response. | ||
*/ | ||
public setAccountData(eventType: EventType | string, content: IContent): Promise<{}> { | ||
public setAccountData<K extends keyof AccountDataEvents>( | ||
t3chguy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
eventType: K, | ||
content: AccountDataEvents[K] | Record<string, never>, | ||
): Promise<{}> { | ||
const path = utils.encodeUri("/user/$userId/account_data/$type", { | ||
$userId: this.credentials.userId!, | ||
$type: eventType, | ||
|
@@ -4251,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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
return this.store.getAccountData(eventType); | ||
} | ||
|
||
|
@@ -4263,15 +4268,17 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa | |
* @returns Promise which resolves: The contents of the given account data event. | ||
* @returns Rejects: with an error response. | ||
*/ | ||
public async getAccountDataFromServer<T extends { [k: string]: any }>(eventType: string): Promise<T | null> { | ||
public async getAccountDataFromServer<K extends keyof AccountDataEvents>( | ||
eventType: K, | ||
): Promise<AccountDataEvents[K] | null> { | ||
if (this.isInitialSyncComplete()) { | ||
const event = this.store.getAccountData(eventType); | ||
if (!event) { | ||
return null; | ||
} | ||
// The network version below returns just the content, so this branch | ||
// does the same to match. | ||
return event.getContent<T>(); | ||
return event.getContent<AccountDataEvents[K]>(); | ||
} | ||
const path = utils.encodeUri("/user/$userId/account_data/$type", { | ||
$userId: this.credentials.userId!, | ||
|
@@ -4287,7 +4294,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa | |
} | ||
} | ||
|
||
public async deleteAccountData(eventType: string): Promise<void> { | ||
public async deleteAccountData(eventType: keyof AccountDataEvents): Promise<void> { | ||
const msc3391DeleteAccountDataServerSupport = this.canSupport.get(Feature.AccountDataDeletion); | ||
// if deletion is not supported overwrite with empty content | ||
if (msc3391DeleteAccountDataServerSupport === ServerSupport.Unsupported) { | ||
|
@@ -4310,7 +4317,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa | |
* @returns The array of users that are ignored (empty if none) | ||
*/ | ||
public getIgnoredUsers(): string[] { | ||
const event = this.getAccountData("m.ignored_user_list"); | ||
const event = this.getAccountData(EventType.IgnoredUserList); | ||
if (!event?.getContent()["ignored_users"]) return []; | ||
return Object.keys(event.getContent()["ignored_users"]); | ||
} | ||
|
@@ -4326,7 +4333,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa | |
userIds.forEach((u) => { | ||
content.ignored_users[u] = {}; | ||
}); | ||
return this.setAccountData("m.ignored_user_list", content); | ||
return this.setAccountData(EventType.IgnoredUserList, content); | ||
} | ||
|
||
/** | ||
|
@@ -9264,7 +9271,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa | |
deviceId: string, | ||
notificationSettings: LocalNotificationSettings, | ||
): Promise<{}> { | ||
const key = `${LOCAL_NOTIFICATION_SETTINGS_PREFIX.name}.${deviceId}`; | ||
const key = `${LOCAL_NOTIFICATION_SETTINGS_PREFIX.name}.${deviceId}` as const; | ||
return this.setAccountData(key, notificationSettings); | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.