Skip to content

Commit

Permalink
feat: added generic user data and conversation data
Browse files Browse the repository at this point in the history
  • Loading branch information
supersnager committed Apr 16, 2022
1 parent df74f52 commit 2a3447e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 16 deletions.
26 changes: 21 additions & 5 deletions src/BasicStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export interface BasicStorageParams {
messageIdGenerator?: MessageIdGenerator;
}

export class BasicStorage implements IStorage {
export class BasicStorage<ConversationData = any>
implements IStorage<ConversationData> {
private readonly _groupIdGenerator: GroupIdGenerator;
private readonly _messageIdGenerator?: MessageIdGenerator;

Expand All @@ -31,7 +32,7 @@ export class BasicStorage implements IStorage {
// TODO: Users by Id
private users: Array<User> = [];
// TODO: Conversations By Id (Dedicated conversations collection)
private conversations: Array<Conversation> = [];
private conversations: Array<Conversation<ConversationData>> = [];
private activeConversationId?: ConversationId;
private messages: GroupedMessages = {};
private currentMessage = "";
Expand Down Expand Up @@ -137,7 +138,7 @@ export class BasicStorage implements IStorage {
*/
getConversation(
conversationId: ConversationId
): [Conversation, number] | [undefined, undefined] {
): [Conversation<ConversationData>, number] | [undefined, undefined] {
const idx = this.conversations.findIndex((c) => c.id === conversationId);

if (idx !== -1) {
Expand All @@ -152,7 +153,7 @@ export class BasicStorage implements IStorage {
* Conversation will be added only when item with its id not exists in the collection.
* @param conversation
*/
addConversation(conversation: Conversation): boolean {
addConversation(conversation: Conversation<ConversationData>): boolean {
const notExists = !this.conversationExists(conversation.id);
if (notExists) {
this.conversations = this.conversations.concat(conversation);
Expand Down Expand Up @@ -201,7 +202,21 @@ export class BasicStorage implements IStorage {
return false;
}

private replaceConversation(conversation: Conversation, idx: number) {
/**
* Replace the conversation in the collection with the new one specified in the parameter
* @param conversation
*/
updateConversation(conversation: Conversation<ConversationData>) {
const [con, idx] = this.getConversation(conversation.id);
if (con) {
this.replaceConversation(conversation, idx as number);
}
}

private replaceConversation(
conversation: Conversation<ConversationData>,
idx: number
) {
this.conversations = this.conversations
.slice(0, idx)
.concat(
Expand All @@ -213,6 +228,7 @@ export class BasicStorage implements IStorage {
draft: conversation.draft,
description: conversation.description,
readonly: conversation.readonly,
data: conversation.data,
})
)
.concat(this.conversations.slice(idx + 1));
Expand Down
10 changes: 7 additions & 3 deletions src/Conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,26 @@ import { Participant, TypingUser } from "./";
import { ConversationId, UserId } from "./Types";
import { TypingUsersList } from "./TypingUsersList";

export interface ConversationParams {
export interface ConversationParams<ConversationData> {
readonly id: ConversationId;
readonly participants?: Array<Participant>;
readonly unreadCounter?: number;
readonly typingUsers?: TypingUsersList;
readonly draft?: string;
readonly description?: string;
readonly readonly?: boolean;
readonly data?: ConversationData;
}

export class Conversation {
export class Conversation<ConversationData = any> {
readonly id: ConversationId;
unreadCounter = 0;
participants: Array<Participant>;
typingUsers: TypingUsersList;
description = "";
draft = "";
readonly = false;
data?: ConversationData;

constructor({
id,
Expand All @@ -29,14 +31,16 @@ export class Conversation {
draft = "",
description = "",
readonly = false,
}: ConversationParams) {
data,
}: ConversationParams<ConversationData>) {
this.id = id;
this.unreadCounter = unreadCounter;
this.participants = participants;
this.typingUsers = typingUsers;
this.draft = draft;
this.description = description;
this.readonly = readonly;
this.data = data;
}

/**
Expand Down
8 changes: 6 additions & 2 deletions src/User.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { UserId } from "./Types";
import { Presence } from "./Presence";

export type UserParams = {
export type UserParams<UserData = any> = {
readonly id: string;
firstName?: string;
lastName?: string;
Expand All @@ -10,9 +10,10 @@ export type UserParams = {
avatar?: string;
bio?: string;
presence?: Presence;
data?: UserData;
};

export class User {
export class User<UserData = any> {
readonly id: UserId;
presence: Presence = new Presence({});
firstName: string;
Expand All @@ -21,6 +22,7 @@ export class User {
email: string;
avatar: string;
bio: string;
data?: UserData;

constructor({
id,
Expand All @@ -31,6 +33,7 @@ export class User {
email = "",
avatar = "",
bio = "",
data,
}: UserParams) {
this.id = id;
this.presence = presence;
Expand All @@ -40,5 +43,6 @@ export class User {
this.email = email;
this.avatar = avatar;
this.bio = bio;
this.data = data;
}
}
20 changes: 14 additions & 6 deletions src/interfaces/IStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from "../";
import { User } from "../User";

export interface IStorage {
export interface IStorage<ConversationData = any, UserData = any> {
readonly groupIdGenerator: GroupIdGenerator;

readonly messageIdGenerator?: MessageIdGenerator;
Expand All @@ -21,15 +21,15 @@ export interface IStorage {
* Sets current (logged in) user object
* @param user
*/
setCurrentUser: (user: User) => void;
setCurrentUser: (user: User<UserData>) => void;

/**
* Add user to collection of users.
* User will be added only when item with its id not exists in the collection.
* Returns true if user has been added, otherwise returns false.
* @param user
*/
addUser: (user: User) => boolean;
addUser: (user: User<UserData>) => boolean;

/**
* Remove user from users collection.
Expand All @@ -43,7 +43,9 @@ export interface IStorage {
* @param userId
* @return [User, number]|[undefined,undefined]
*/
getUser: (userId: UserId) => [User, number] | [undefined, undefined];
getUser: (
userId: UserId
) => [User<UserData>, number] | [undefined, undefined];

/**
* Set active conversation and reset unread counter of this conversation if second parameter is set.
Expand Down Expand Up @@ -74,7 +76,7 @@ export interface IStorage {
* Conversation will be added only when item with its id not exists in the collection.
* @param conversation
*/
addConversation: (conversation: Conversation) => boolean;
addConversation: (conversation: Conversation<ConversationData>) => boolean;

/**
* Set unread messages for conversation
Expand All @@ -94,14 +96,20 @@ export interface IStorage {
removeMessages: boolean
) => boolean;

/**
* Replace the conversation in the collection with the new one specified in the parameter
* @param conversation
*/
updateConversation: (conversation: Conversation) => void;

/**
* Get conversation by id
* @param conversationId
* @return [Conversation, number]|[undefined, undefined]
*/
getConversation: (
conversationId: ConversationId
) => [Conversation, number] | [undefined, undefined];
) => [Conversation<ConversationData>, number] | [undefined, undefined];

/**
* Add participant to conversation
Expand Down

0 comments on commit 2a3447e

Please sign in to comment.