This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 828
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a7dc7b8
commit b008bdb
Showing
6 changed files
with
177 additions
and
130 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
|
@@ -327,4 +327,4 @@ export default class RightPanel extends React.Component<Props, IState> { | |
</aside> | ||
); | ||
} | ||
} | ||
} |
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/* | ||
Copyright 2015, 2016 OpenMarket Ltd | ||
Copyright 2019, 2020 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 { useEffect, useState } from "react"; | ||
import { RoomStateEvent, MatrixEvent, EventType } from "matrix-js-sdk/src/matrix"; | ||
import { DeviceInfo } from "matrix-js-sdk/src/crypto/deviceinfo"; | ||
import { CryptoEvent } from "matrix-js-sdk/src/crypto"; | ||
import { UserVerificationStatus } from "matrix-js-sdk/src/crypto-api"; | ||
|
||
import dis from "../../../dispatcher/dispatcher"; | ||
import { MatrixClientPeg } from "../../../MatrixClientPeg"; | ||
import { Action } from "../../../dispatcher/actions"; | ||
import { PowerStatus } from "./EntityTile"; | ||
import { E2EState } from "../../../models/rooms/E2EState"; | ||
import { asyncSome } from "../../../utils/arrays"; | ||
import { getUserDeviceIds } from "../../../utils/crypto/deviceInfo"; | ||
import { RoomMember } from "../../../models/rooms/RoomMember"; | ||
|
||
interface IProps { | ||
member: RoomMember; | ||
showPresence?: boolean; | ||
} | ||
|
||
export interface MemberTileViewModel extends IProps { | ||
e2eStatus?: E2EState; | ||
name: string; | ||
powerStatus?: PowerStatus; | ||
onClick: () => void; | ||
} | ||
|
||
export default function useMemberTileViewModel(props: IProps): MemberTileViewModel { | ||
const [e2eStatus, setE2eStatus] = useState<E2EState | undefined>(); | ||
|
||
useEffect(() => { | ||
const cli = MatrixClientPeg.safeGet(); | ||
|
||
const updateE2EStatus = async (): Promise<void> => { | ||
const { userId } = props.member; | ||
const isMe = userId === cli.getUserId(); | ||
const userTrust = await cli.getCrypto()?.getUserVerificationStatus(userId); | ||
if (!userTrust?.isCrossSigningVerified()) { | ||
setE2eStatus(userTrust?.wasCrossSigningVerified() ? E2EState.Warning : E2EState.Normal); | ||
return; | ||
} | ||
|
||
const deviceIDs = await getUserDeviceIds(cli, userId); | ||
const anyDeviceUnverified = await asyncSome(deviceIDs, async (deviceId) => { | ||
// For your own devices, we use the stricter check of cross-signing | ||
// verification to encourage everyone to trust their own devices via | ||
// cross-signing so that other users can then safely trust you. | ||
// For other people's devices, the more general verified check that | ||
// includes locally verified devices can be used. | ||
const deviceTrust = await cli.getCrypto()?.getDeviceVerificationStatus(userId, deviceId); | ||
return !deviceTrust || (isMe ? !deviceTrust.crossSigningVerified : !deviceTrust.isVerified()); | ||
}); | ||
setE2eStatus(anyDeviceUnverified ? E2EState.Warning : E2EState.Verified); | ||
}; | ||
|
||
const onRoomStateEvents = (ev: MatrixEvent): void => { | ||
if (ev.getType() !== EventType.RoomEncryption) return; | ||
const { roomId } = props.member; | ||
if (ev.getRoomId() !== roomId) return; | ||
|
||
// The room is encrypted now. | ||
cli.removeListener(RoomStateEvent.Events, onRoomStateEvents); | ||
updateE2EStatus(); | ||
}; | ||
|
||
const onUserTrustStatusChanged = (userId: string, trustStatus: UserVerificationStatus): void => { | ||
if (userId !== props.member.userId) return; | ||
updateE2EStatus(); | ||
}; | ||
|
||
const onDeviceVerificationChanged = (userId: string, deviceId: string, deviceInfo: DeviceInfo): void => { | ||
if (userId !== props.member.userId) return; | ||
updateE2EStatus(); | ||
}; | ||
|
||
const { roomId } = props.member; | ||
if (roomId) { | ||
const isRoomEncrypted = cli.isRoomEncrypted(roomId); | ||
if (isRoomEncrypted) { | ||
cli.on(CryptoEvent.UserTrustStatusChanged, onUserTrustStatusChanged); | ||
cli.on(CryptoEvent.DeviceVerificationChanged, onDeviceVerificationChanged); | ||
updateE2EStatus(); | ||
} else { | ||
// Listen for room to become encrypted | ||
cli.on(RoomStateEvent.Events, onRoomStateEvents); | ||
} | ||
} | ||
|
||
return () => { | ||
if (cli) { | ||
cli.removeListener(RoomStateEvent.Events, onRoomStateEvents); | ||
cli.removeListener(CryptoEvent.UserTrustStatusChanged, onUserTrustStatusChanged); | ||
cli.removeListener(CryptoEvent.DeviceVerificationChanged, onDeviceVerificationChanged); | ||
} | ||
}; | ||
}, [props.member]); | ||
|
||
const onClick = (): void => { | ||
dis.dispatch({ | ||
action: Action.ViewUser, | ||
member: props.member, | ||
push: true, | ||
}); | ||
}; | ||
|
||
const member = props.member; | ||
const name = props.member.name; | ||
|
||
const powerStatusMap = new Map([ | ||
[100, PowerStatus.Admin], | ||
[50, PowerStatus.Moderator], | ||
]); | ||
|
||
// Find the nearest power level with a badge | ||
let powerLevel = props.member.powerLevel; | ||
for (const [pl] of powerStatusMap) { | ||
if (props.member.powerLevel >= pl) { | ||
powerLevel = pl; | ||
break; | ||
} | ||
} | ||
|
||
const powerStatus = powerStatusMap.get(powerLevel); | ||
|
||
return { powerStatus, member, name, onClick, e2eStatus, showPresence: props.showPresence }; | ||
} |
This file contains 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