forked from mystiq/hydrogen-web
convert groupby and megolm decryption utils to typescript
This commit is contained in:
parent
66a77519d7
commit
77d10c93d6
7 changed files with 27 additions and 25 deletions
|
@ -15,7 +15,7 @@ limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {OLM_ALGORITHM} from "./e2ee/common.js";
|
import {OLM_ALGORITHM} from "./e2ee/common.js";
|
||||||
import {countBy, groupBy} from "../utils/groupBy.js";
|
import {countBy, groupBy} from "../utils/groupBy";
|
||||||
|
|
||||||
export class DeviceMessageHandler {
|
export class DeviceMessageHandler {
|
||||||
constructor({storage}) {
|
constructor({storage}) {
|
||||||
|
|
|
@ -33,7 +33,7 @@ import {MEGOLM_ALGORITHM} from "./e2ee/common.js";
|
||||||
import {RoomEncryption} from "./e2ee/RoomEncryption.js";
|
import {RoomEncryption} from "./e2ee/RoomEncryption.js";
|
||||||
import {DeviceTracker} from "./e2ee/DeviceTracker.js";
|
import {DeviceTracker} from "./e2ee/DeviceTracker.js";
|
||||||
import {LockMap} from "../utils/LockMap.js";
|
import {LockMap} from "../utils/LockMap.js";
|
||||||
import {groupBy} from "../utils/groupBy.js";
|
import {groupBy} from "../utils/groupBy";
|
||||||
import {
|
import {
|
||||||
keyFromCredential as ssssKeyFromCredential,
|
keyFromCredential as ssssKeyFromCredential,
|
||||||
readKey as ssssReadKey,
|
readKey as ssssReadKey,
|
||||||
|
|
|
@ -15,9 +15,9 @@ limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {MEGOLM_ALGORITHM, DecryptionSource} from "./common.js";
|
import {MEGOLM_ALGORITHM, DecryptionSource} from "./common.js";
|
||||||
import {groupEventsBySession} from "./megolm/decryption/utils.js";
|
import {groupEventsBySession} from "./megolm/decryption/utils";
|
||||||
import {mergeMap} from "../../utils/mergeMap.js";
|
import {mergeMap} from "../../utils/mergeMap.js";
|
||||||
import {groupBy} from "../../utils/groupBy.js";
|
import {groupBy} from "../../utils/groupBy";
|
||||||
import {makeTxnId} from "../common.js";
|
import {makeTxnId} from "../common.js";
|
||||||
|
|
||||||
const ENCRYPTED_TYPE = "m.room.encrypted";
|
const ENCRYPTED_TYPE = "m.room.encrypted";
|
||||||
|
|
|
@ -14,44 +14,46 @@ See the License for the specific language governing permissions and
|
||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {groupByWithCreator} from "../../../../utils/groupBy.js";
|
import {groupByWithCreator} from "../../../../utils/groupBy";
|
||||||
|
import type {TimelineEvent} from "../../../storage/types";
|
||||||
|
|
||||||
function getSenderKey(event) {
|
function getSenderKey(event: TimelineEvent): string | undefined {
|
||||||
return event.content?.["sender_key"];
|
return event.content?.["sender_key"];
|
||||||
}
|
}
|
||||||
|
|
||||||
function getSessionId(event) {
|
function getSessionId(event: TimelineEvent): string | undefined {
|
||||||
return event.content?.["session_id"];
|
return event.content?.["session_id"];
|
||||||
}
|
}
|
||||||
|
|
||||||
function getCiphertext(event) {
|
function getCiphertext(event: TimelineEvent): string | undefined {
|
||||||
return event.content?.ciphertext;
|
return event.content?.ciphertext;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function validateEvent(event) {
|
export function validateEvent(event: TimelineEvent) {
|
||||||
return typeof getSenderKey(event) === "string" &&
|
return typeof getSenderKey(event) === "string" &&
|
||||||
typeof getSessionId(event) === "string" &&
|
typeof getSessionId(event) === "string" &&
|
||||||
typeof getCiphertext(event) === "string";
|
typeof getCiphertext(event) === "string";
|
||||||
}
|
}
|
||||||
|
|
||||||
class SessionKeyGroup {
|
export class SessionKeyGroup {
|
||||||
|
public readonly events: TimelineEvent[];
|
||||||
constructor() {
|
constructor() {
|
||||||
this.events = [];
|
this.events = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
get senderKey() {
|
get senderKey(): string | undefined {
|
||||||
return getSenderKey(this.events[0]);
|
return getSenderKey(this.events[0]!);
|
||||||
}
|
}
|
||||||
|
|
||||||
get sessionId() {
|
get sessionId(): string | undefined {
|
||||||
return getSessionId(this.events[0]);
|
return getSessionId(this.events[0]!);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function groupEventsBySession(events) {
|
export function groupEventsBySession(events: TimelineEvent[]): Map<string, SessionKeyGroup> {
|
||||||
return groupByWithCreator(events,
|
return groupByWithCreator<string, TimelineEvent, SessionKeyGroup>(events,
|
||||||
event => `${getSenderKey(event)}|${getSessionId(event)}`,
|
(event: TimelineEvent) => `${getSenderKey(event)}|${getSessionId(event)}`,
|
||||||
() => new SessionKeyGroup(),
|
() => new SessionKeyGroup(),
|
||||||
(group, event) => group.events.push(event)
|
(group: SessionKeyGroup, event: TimelineEvent) => group.events.push(event)
|
||||||
);
|
);
|
||||||
}
|
}
|
|
@ -15,7 +15,7 @@ limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {DecryptionError} from "../common.js";
|
import {DecryptionError} from "../common.js";
|
||||||
import {groupBy} from "../../../utils/groupBy.js";
|
import {groupBy} from "../../../utils/groupBy";
|
||||||
import {MultiLock} from "../../../utils/Lock.js";
|
import {MultiLock} from "../../../utils/Lock.js";
|
||||||
import {Session} from "./Session.js";
|
import {Session} from "./Session.js";
|
||||||
import {DecryptionResult} from "../DecryptionResult.js";
|
import {DecryptionResult} from "../DecryptionResult.js";
|
||||||
|
|
|
@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {groupByWithCreator} from "../../../utils/groupBy.js";
|
import {groupByWithCreator} from "../../../utils/groupBy";
|
||||||
import {verifyEd25519Signature, OLM_ALGORITHM} from "../common.js";
|
import {verifyEd25519Signature, OLM_ALGORITHM} from "../common.js";
|
||||||
import {createSessionEntry} from "./Session.js";
|
import {createSessionEntry} from "./Session.js";
|
||||||
|
|
||||||
|
|
|
@ -14,14 +14,14 @@ See the License for the specific language governing permissions and
|
||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function groupBy(array, groupFn) {
|
export function groupBy<K, V>(array: V[], groupFn: (V) => K): Map<K, V[]> {
|
||||||
return groupByWithCreator(array, groupFn,
|
return groupByWithCreator<K, V, V[]>(array, groupFn,
|
||||||
() => {return [];},
|
() => {return [];},
|
||||||
(array, value) => array.push(value)
|
(array, value) => array.push(value)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function groupByWithCreator(array, groupFn, createCollectionFn, addCollectionFn) {
|
export function groupByWithCreator<K, V, C>(array: V[], groupFn: (V) => K, createCollectionFn: () => C, addCollectionFn: (C, V) => void): Map<K, C> {
|
||||||
return array.reduce((map, value) => {
|
return array.reduce((map, value) => {
|
||||||
const key = groupFn(value);
|
const key = groupFn(value);
|
||||||
let collection = map.get(key);
|
let collection = map.get(key);
|
||||||
|
@ -31,10 +31,10 @@ export function groupByWithCreator(array, groupFn, createCollectionFn, addCollec
|
||||||
}
|
}
|
||||||
addCollectionFn(collection, value);
|
addCollectionFn(collection, value);
|
||||||
return map;
|
return map;
|
||||||
}, new Map());
|
}, new Map<K, C>());
|
||||||
}
|
}
|
||||||
|
|
||||||
export function countBy(events, mapper) {
|
export function countBy<V>(events: V[], mapper: (V) => string | number): { [key: string]: number } {
|
||||||
return events.reduce((counts, event) => {
|
return events.reduce((counts, event) => {
|
||||||
const mappedValue = mapper(event);
|
const mappedValue = mapper(event);
|
||||||
if (!counts[mappedValue]) {
|
if (!counts[mappedValue]) {
|
Loading…
Reference in a new issue