Implemented E2EE on timezone sharing

This commit is contained in:
Dylan 2022-09-18 18:43:03 +09:30
parent d59b2982a1
commit da576d72e1
2 changed files with 38 additions and 7 deletions

View file

@ -1,4 +1,4 @@
import React, { useState, useEffect, useCallback } from 'react'; import React, { useState } from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import initMatrix from '../../../client/initMatrix'; import initMatrix from '../../../client/initMatrix';
@ -12,19 +12,30 @@ function RoomTimezone({ roomId }) {
const room = mx.getRoom(roomId); const room = mx.getRoom(roomId);
const userId = mx.getUserId(); const userId = mx.getUserId();
const currentTimezone = room.currentState.getStateEvents('in.cinny.share_timezone', userId)?.event?.content?.user_timezone; const timezoneEventId = room.currentState.getStateEvents('in.cinny.shared_timezone', userId)?.event?.content?.user_timezone_event;
const timezoneEvent = room.findEventById(timezoneEventId);
const timezoneContent = timezoneEvent?.getContent();
const [timezone, setTimezone] = useState(currentTimezone); const [timezone, setTimezone] = useState(timezoneContent?.user_timezone);
const clearTimezone = () => { const clearTimezone = () => {
mx.sendStateEvent(roomId, 'in.cinny.share_timezone', { }, userId); const eventKey = room.currentState.getStateEvents('in.cinny.shared_timezone', userId)?.event?.content?.user_timezone_event;
mx.redactEvent(roomId, eventKey);
mx.sendStateEvent(roomId, 'in.cinny.shared_timezone', { }, userId);
setTimezone(null); setTimezone(null);
}; };
const shareTimezone = () => { const shareTimezone = () => {
const userTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone; const userTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
setTimezone(userTimezone); setTimezone(userTimezone);
mx.sendStateEvent(roomId, 'in.cinny.share_timezone', { user_timezone: userTimezone }, userId); const content = {
user_timezone: userTimezone,
msgtype: 'in.cinny.share_timezone',
};
mx.sendEvent(roomId, 'in.cinny.share_timezone', content).then((event) => {
// Append the shared timezone event to the room state
mx.sendStateEvent(roomId, 'in.cinny.shared_timezone', { user_timezone_event: event.event_id }, userId);
});
}; };
return ( return (

View file

@ -21,10 +21,18 @@ function RoomBanner({ roomId }) {
let partner = null; let partner = null;
const updateTime = () => {
if (isDM) { if (isDM) {
partner = room.getAvatarFallbackMember(); partner = room.getAvatarFallbackMember();
const timezone = room.currentState.getStateEvents('in.cinny.share_timezone', partner.userId)?.event?.content?.user_timezone; }
const timezoneEventId = room.currentState.getStateEvents('in.cinny.shared_timezone', partner?.userId)?.event?.content?.user_timezone_event;
const timezoneEvent = room.findEventById(timezoneEventId);
let timezoneContent = null;
const updateTime = () => {
if (isDM) {
const timezone = timezoneContent?.user_timezone;
const date = new Date(); const date = new Date();
try { try {
@ -38,6 +46,18 @@ function RoomBanner({ roomId }) {
} }
}; };
if (timezoneEvent) {
if (!timezoneEvent.getClearContent()) {
timezoneEvent.attemptDecryption(mx.crypto);
}
timezoneContent = timezoneEvent.getContent();
timezoneEvent.getDecryptionPromise()?.then(() => {
timezoneContent = timezoneEvent.getContent();
updateTime();
});
}
useEffect(() => { useEffect(() => {
updateTime(); updateTime();
const interval = setInterval(() => updateTime(), 20000); const interval = setInterval(() => updateTime(), 20000);