Skip to content

Commit

Permalink
Convert Timestamp values from BigNumber to number (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Jan 11, 2021
1 parent 0716e9b commit 744a962
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 16 deletions.
13 changes: 13 additions & 0 deletions src/website/public/services/candidConverters/localMessage.ts
Original file line number Diff line number Diff line change
@@ -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
};
}
7 changes: 7 additions & 0 deletions src/website/public/services/candidConverters/timestamp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import BigNumber from "bignumber.js";

import { Timestamp } from "../../model/common";

export function fromCandid(value: BigNumber) : Timestamp {
return Number(value);
}
15 changes: 7 additions & 8 deletions src/website/public/services/chats/getChats.ts
Original file line number Diff line number Diff line change
@@ -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<GetChatsResponse> {
Expand Down Expand Up @@ -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: []
};
}
Expand All @@ -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) };
}

8 changes: 2 additions & 6 deletions src/website/public/services/chats/getMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<GetMessagesResponse> {
let response = await canister.get_messages(chatIdToCandid(chatId), fromId, pageSize);
Expand All @@ -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
}
};
Expand All @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/website/public/services/chats/sendDirectMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SendDirectMessageResponse> {
Expand All @@ -15,7 +16,7 @@ export default async function(userId: UserId, message: string) : Promise<SendDir
result: {
chatId: chatIdFromCandid(success.chat_id),
messageId: success.message_id,
timestamp: success.timestamp
timestamp: timestampFromCandid(success.timestamp)
}
};
} else {
Expand Down
3 changes: 2 additions & 1 deletion src/website/public/services/chats/sendMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import canister from "ic:canisters/chats";
import { ChatId } from "../../model/chats";
import { Timestamp } from "../../model/common";
import { toCandid as chatIdToCandid } from "../candidConverters/chatId";
import { fromCandid as timestampFromCandid } from "../candidConverters/timestamp";

export default async function(chatId: ChatId, message: string) : Promise<SendMessageResponse> {
let response = await canister.send_message(chatIdToCandid(chatId), message);
Expand All @@ -12,7 +13,7 @@ export default async function(chatId: ChatId, message: string) : Promise<SendMes
kind: "success",
result: {
messageId: success.message_id,
timestamp: success.timestamp
timestamp: timestampFromCandid(success.timestamp)
}
};
} else if (response.hasOwnProperty("ChatNotFound")) {
Expand Down

0 comments on commit 744a962

Please sign in to comment.