From 744a9628e9437d983f0ace9e6c4ba9fd752f43ab Mon Sep 17 00:00:00 2001 From: Hamish Peebles Date: Mon, 11 Jan 2021 09:35:30 +0000 Subject: [PATCH] Convert Timestamp values from BigNumber to number (#34) --- .../services/candidConverters/localMessage.ts | 13 +++++++++++++ .../public/services/candidConverters/timestamp.ts | 7 +++++++ src/website/public/services/chats/getChats.ts | 15 +++++++-------- src/website/public/services/chats/getMessages.ts | 8 ++------ .../public/services/chats/sendDirectMessage.ts | 3 ++- src/website/public/services/chats/sendMessage.ts | 3 ++- 6 files changed, 33 insertions(+), 16 deletions(-) create mode 100644 src/website/public/services/candidConverters/localMessage.ts create mode 100644 src/website/public/services/candidConverters/timestamp.ts diff --git a/src/website/public/services/candidConverters/localMessage.ts b/src/website/public/services/candidConverters/localMessage.ts new file mode 100644 index 0000000000..4fdd48e0cd --- /dev/null +++ b/src/website/public/services/candidConverters/localMessage.ts @@ -0,0 +1,13 @@ +import { LocalMessage } from "../../model/messages"; +import { fromCandid as timestampFromCandid } from "./timestamp"; +import { fromCandid as userIdFromCandid } from "./userId"; + +export function fromCandid(value: any) : LocalMessage { + return { + kind: "local", + id: value.id, + timestamp: timestampFromCandid(value.timestamp), + sender: userIdFromCandid(value.sender), + text: value.text + }; +} \ No newline at end of file diff --git a/src/website/public/services/candidConverters/timestamp.ts b/src/website/public/services/candidConverters/timestamp.ts new file mode 100644 index 0000000000..c4dfbe82ae --- /dev/null +++ b/src/website/public/services/candidConverters/timestamp.ts @@ -0,0 +1,7 @@ +import BigNumber from "bignumber.js"; + +import { Timestamp } from "../../model/common"; + +export function fromCandid(value: BigNumber) : Timestamp { + return Number(value); +} diff --git a/src/website/public/services/chats/getChats.ts b/src/website/public/services/chats/getChats.ts index ea8830f2da..a58d78dc5c 100644 --- a/src/website/public/services/chats/getChats.ts +++ b/src/website/public/services/chats/getChats.ts @@ -1,9 +1,10 @@ import canister from "ic:canisters/chats"; import { ConfirmedChat, DirectChat, GroupChat } from "../../model/chats"; import { Option } from "../../model/common"; -import { LocalMessage } from "../../model/messages"; import { fromCandid as chatIdFromCandid } from "../candidConverters/chatId"; +import { fromCandid as localMessageFromCandid } from "../candidConverters/localMessage"; import { toCandid as optionToCandid } from "../candidConverters/option"; +import { fromCandid as timestampFromCandid } from "../candidConverters/timestamp"; import { fromCandid as userIdFromCandid } from "../candidConverters/userId"; export default async function(request: GetChatsRequest) : Promise { @@ -54,12 +55,12 @@ function convertToDirectChat(value: any) : DirectChat { kind: "direct", them: userIdFromCandid(value.them), chatId: chatIdFromCandid(value.id), - updatedDate: latestMessage.timestamp, + updatedDate: timestampFromCandid(latestMessage.timestamp), readUpTo: latestMessage.id - value.unread, latestKnownMessageId: latestMessage.id, messagesToDownload: [], messagesDownloading: [], - confirmedMessages: value.latest_messages.reverse().map(convertToLocalMessage), + confirmedMessages: value.latest_messages.reverse().map(localMessageFromCandid), unconfirmedMessages: [] }; } @@ -71,17 +72,15 @@ function convertToGroupChat(value: any) : GroupChat kind: "group", chatId: chatIdFromCandid(value.id), subject: value.subject, - updatedDate: value.updated_date, + updatedDate: timestampFromCandid(value.updated_date), participants: value.participants.map(userIdFromCandid), readUpTo: latestMessageId - value.unread, latestKnownMessageId: latestMessageId, messagesToDownload: [], messagesDownloading: [], - confirmedMessages: value.latest_messages.reverse().map(convertToLocalMessage), + confirmedMessages: value.latest_messages.reverse().map(localMessageFromCandid), unconfirmedMessages: [] }; } -function convertToLocalMessage(value: any) : LocalMessage { - return { kind: "local", ...value, sender: userIdFromCandid(value.sender) }; -} + diff --git a/src/website/public/services/chats/getMessages.ts b/src/website/public/services/chats/getMessages.ts index f4430269f3..20aa803402 100644 --- a/src/website/public/services/chats/getMessages.ts +++ b/src/website/public/services/chats/getMessages.ts @@ -2,7 +2,7 @@ import canister from "ic:canisters/chats"; import { ChatId } from "../../model/chats"; import { LocalMessage } from "../../model/messages"; import { toCandid as chatIdToCandid } from "../candidConverters/chatId"; -import { fromCandid as userIdFromCandid } from "../candidConverters/userId"; +import { fromCandid as localMessageFromCandid } from "../candidConverters/localMessage"; export default async function(chatId: ChatId, fromId: number, pageSize: number) : Promise { let response = await canister.get_messages(chatIdToCandid(chatId), fromId, pageSize); @@ -20,7 +20,7 @@ function handleResponse(response: any) : GetMessagesResponse { return { kind: "success", result: { - messages: success.messages.map(convertToLocalMessage), + messages: success.messages.map(localMessageFromCandid), latestMessageId: success.latest_message_id } }; @@ -33,10 +33,6 @@ function handleResponse(response: any) : GetMessagesResponse { } } -export function convertToLocalMessage(value: any) : LocalMessage { - return { kind: "local", ...value, sender: userIdFromCandid(value.sender) }; -} - export type GetMessagesResponse = Success | ChatNotFound; diff --git a/src/website/public/services/chats/sendDirectMessage.ts b/src/website/public/services/chats/sendDirectMessage.ts index 6b31ef447e..b5584860a7 100644 --- a/src/website/public/services/chats/sendDirectMessage.ts +++ b/src/website/public/services/chats/sendDirectMessage.ts @@ -3,6 +3,7 @@ import { UserId } from "../../model/users"; import { ChatId } from "../../model/chats"; import { Timestamp } from "../../model/common"; import { fromCandid as chatIdFromCandid } from "../candidConverters/chatId"; +import { fromCandid as timestampFromCandid } from "../candidConverters/timestamp"; import { toCandid as userIdToCandid } from "../candidConverters/userId"; export default async function(userId: UserId, message: string) : Promise { @@ -15,7 +16,7 @@ export default async function(userId: UserId, message: string) : Promise { let response = await canister.send_message(chatIdToCandid(chatId), message); @@ -12,7 +13,7 @@ export default async function(chatId: ChatId, message: string) : Promise