From 5e2695464446b078f81466b9ef85c18bb8f7bdc7 Mon Sep 17 00:00:00 2001 From: AlexAlexandre Date: Fri, 17 Dec 2021 22:44:17 -0300 Subject: [PATCH] update IMessage --- app/definitions/IMessage.ts | 45 +++++++++++++++++++++++++++++-- app/definitions/IThread.ts | 10 +++---- app/views/RoomView/List/List.tsx | 3 ++- app/views/RoomView/List/index.tsx | 23 ++++++++++------ app/views/RoomView/index.tsx | 5 ++-- 5 files changed, 68 insertions(+), 18 deletions(-) diff --git a/app/definitions/IMessage.ts b/app/definitions/IMessage.ts index aca651c10..aedb8da2a 100644 --- a/app/definitions/IMessage.ts +++ b/app/definitions/IMessage.ts @@ -1,3 +1,44 @@ -export interface IMessage { - msg: string; +import Model from '@nozbe/watermelondb/Model'; + +import { ISubscriptions } from './ISubscriptions'; + +export interface IMessage extends ISubscriptions { + id: string; + rid: string; + ts: number; + u: string; + alias: string; + parse_urls: string; + _updated_at: number; + msg?: string; + t?: string; + groupable?: boolean; + avatar?: string; + emoji?: string; + attachments?: string; + urls?: string; + status?: number; + pinned?: boolean; + starred?: boolean; + edited_by?: string; + reactions?: string; + role?: string; + drid?: string; + dcount?: number; + dlm?: number; + tmid?: string; + tcount?: number; + tlm?: number; + replies?: string; + mentions?: string; + channels?: string; + auto_translate?: boolean; + translations?: string; + tmsg?: string; + blocks?: string; + e2e?: string; + tshow?: boolean; + md?: string; } + +export type TMessageModel = IMessage & Model; diff --git a/app/definitions/IThread.ts b/app/definitions/IThread.ts index 32e374718..14359441b 100644 --- a/app/definitions/IThread.ts +++ b/app/definitions/IThread.ts @@ -22,14 +22,14 @@ export interface IThread { attachments: IAttachment[]; urls: IUrl[]; status: number; - pinned: null; - starred: null; + pinned: boolean; + starred: boolean; edited_by: { username: string }; reactions: IReaction[]; - role: null; + role: string; drid: string; - dcount: null; - dlm: null; + dcount: number; + dlm: number; tmid: string; tcount: 2; tlm: Date; diff --git a/app/views/RoomView/List/List.tsx b/app/views/RoomView/List/List.tsx index c470eb806..7f146dc89 100644 --- a/app/views/RoomView/List/List.tsx +++ b/app/views/RoomView/List/List.tsx @@ -5,6 +5,7 @@ import Animated from 'react-native-reanimated'; import { isIOS } from '../../../utils/deviceInfo'; import scrollPersistTaps from '../../../utils/scrollPersistTaps'; import { IRoomItem } from '../index'; +import { IMessage } from '../../../definitions/IMessage'; const AnimatedFlatList = Animated.createAnimatedComponent(FlatList); @@ -17,7 +18,7 @@ const styles = StyleSheet.create({ } }); -interface IRoomListProps extends FlatListProps { +interface IRoomListProps extends FlatListProps { listRef: React.Ref; } diff --git a/app/views/RoomView/List/index.tsx b/app/views/RoomView/List/index.tsx index bf42c2863..a0baeae00 100644 --- a/app/views/RoomView/List/index.tsx +++ b/app/views/RoomView/List/index.tsx @@ -5,7 +5,6 @@ import moment from 'moment'; import { dequal } from 'dequal'; import { Value, event } from 'react-native-reanimated'; import { Observable, Subscription } from 'rxjs'; -import Model from '@nozbe/watermelondb/Model'; import { StackNavigationProp } from '@react-navigation/stack'; import database from '../../../lib/database'; @@ -22,6 +21,7 @@ import NavBottomFAB from './NavBottomFAB'; import { ChatsStackParamList } from '../../../stacks/types'; import { IRoomItem } from '../index'; import { IThread } from '../../../definitions/IThread'; +import { IMessage, TMessageModel } from '../../../definitions/IMessage'; const QUERY_SIZE = 50; @@ -52,7 +52,13 @@ interface IRoomListContainerProps { serverVersion: string; } -class ListContainer extends React.Component { +interface IRoomListContainerState { + messages: TMessageModel[]; + refreshing: boolean; + highlightedMessage: string | null; +} + +class ListContainer extends React.Component { private count: number; private mounted: boolean; private animated: boolean; @@ -63,7 +69,7 @@ class ListContainer extends React.Component { private viewabilityConfig: { itemVisiblePercentThreshold: number }; private highlightedMessageTimeout?: ReturnType | false; private thread?: IThread; - private messagesObservable?: Observable; + private messagesObservable?: Observable; private messagesSubscription?: Subscription; private viewableItems?: ViewToken[]; @@ -97,7 +103,7 @@ class ListContainer extends React.Component { console.timeEnd(`${this.constructor.name} mount`); } - shouldComponentUpdate(nextProps: IRoomListContainerProps, nextState: any) { + shouldComponentUpdate(nextProps: IRoomListContainerProps, nextState: IRoomListContainerState) { const { refreshing, highlightedMessage } = this.state; const { hideSystemMessages, theme, tunread, ignored, loading } = this.props; if (theme !== nextProps.theme) { @@ -187,8 +193,9 @@ class ListContainer extends React.Component { if (rid) { this.unsubscribeMessages(); - this.messagesSubscription = this.messagesObservable?.subscribe((messages: any) => { + this.messagesSubscription = this.messagesObservable?.subscribe((messages: TMessageModel[]) => { if (tmid && this.thread) { + // @ts-ignore messages = [...messages, this.thread]; } @@ -197,7 +204,7 @@ class ListContainer extends React.Component { * hide system message is enabled */ if (compareServerVersion(serverVersion, '3.16.0', methods.lowerThan) || hideSystemMessages.length) { - messages = messages.filter((m: { t: string }) => !m.t || !hideSystemMessages?.includes(m.t)); + messages = messages.filter((m: TMessageModel) => !m.t || !hideSystemMessages?.includes(m.t)); } if (this.mounted) { @@ -282,7 +289,7 @@ class ListContainer extends React.Component { this.jumping = true; const { messages } = this.state; const { listRef } = this.props; - const index = messages.findIndex((item: { id: string }) => item.id === messageId); + const index = messages.findIndex((item: TMessageModel) => item.id === messageId); if (index > -1) { listRef.current?.scrollToIndex({ index, viewPosition: 0.5, viewOffset: 100 }); await new Promise(res => setTimeout(res, 300)); @@ -326,7 +333,7 @@ class ListContainer extends React.Component { return null; }; - renderItem = ({ item, index }: { item: IRoomItem; index: number }) => { + renderItem = ({ item, index }: { item: IRoomItem | IMessage; index: number }) => { const { messages, highlightedMessage } = this.state; const { renderRow } = this.props; return renderRow(item, messages[index + 1], highlightedMessage); diff --git a/app/views/RoomView/index.tsx b/app/views/RoomView/index.tsx index ff814c9f8..de22e794c 100644 --- a/app/views/RoomView/index.tsx +++ b/app/views/RoomView/index.tsx @@ -71,6 +71,7 @@ import { IAttachment } from '../../definitions/IAttachment'; import { IThread } from '../../definitions/IThread'; import { ISubscriptions } from '../../definitions/ISubscriptions'; import { ModalStackParamList } from '../../stacks/MasterDetailStack/types'; +import { IMessage } from '../../definitions/IMessage'; type TStateAttrsUpdate = | 'joined' @@ -751,7 +752,7 @@ class RoomView extends React.Component { } }; - onReplyInit = (message: string, mention: boolean) => { + onReplyInit = (message: IMessage, mention: boolean) => { this.setState({ selectedMessage: message, replying: true, @@ -1086,7 +1087,7 @@ class RoomView extends React.Component { this.goRoomActionsView('SearchMessagesView'); } else if (handleCommandReplyLatest(event)) { if (this.list && this.list.current) { - const message = this.list.current.getLastMessage(); + const message = this.list.current.getLastMessage() as IMessage; this.onReplyInit(message, false); } }