Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public clickedOnChatUser(ctx: StateContext, { event }: ChatActions.ClickedOnChatUser) {
const state = ctx.getState()
const foundRoom = state.chatRooms.find(a => a.type === RoomType.Double && a.participants.some(b => b.userName === event.inviee.userName))
let foundUser = state.availableUsers.find(a => a.userName === event.inviee.userName)
foundUser = {
...foundUser,
incomingMessages: 0
}
ctx.setState(
patch({
availableUsers: updateItem(a => a.userName === foundUser.userName, foundUser)
})
)
if (ObjectUtils.isNotNull(foundRoom)) {
return ctx.dispatch(new ChatActions.ActiveDoubleChatRoom({
chatSession: foundRoom.currentSession
}))
}
else {
// No found room, mean need to fetch from server
ctx.setState(
patch({
invitingUser: event.inviee
})
)
// Notify Chat Service to init double chat room
return ctx.dispatch(new ChatActions.FetchDoubleChatRoom())
sessionId: event.chatSession.sessionId,
previousSessionId: state.activeChatSession.previousSessionId // Keep last previous id
}
})
)
}
else {
// If there are no active chat session, so that means this added new session coming
// from unactived or unloaded
const foundRoom = state.chatRooms.find(a => a.chatRoomId === event.chatSession.chatRoomId)
if (foundRoom) {
let clonedFoundRoom: DoubleChatRoom = ObjectUtils.clone(foundRoom)
clonedFoundRoom.currentSession.sessionId = event.chatSession.sessionId
ctx.setState(
patch({
chatRooms: updateItem(a => a.chatRoomId === clonedFoundRoom.chatRoomId, clonedFoundRoom)
})
)
}
else {
// Do nothing because this chat room isn't ready to receive new session
}
}
}
let clonedFoundRoom: DoubleChatRoom = ObjectUtils.clone(foundRoom)
const lastMessage = clonedFoundRoom.currentSession.messages[clonedFoundRoom.currentSession.messages.length - 1]
if(lastMessage){
event.message.renderTime = lastMessage.userName !== event.message.userName
}
else{
event.message.renderTime = true
}
clonedFoundRoom.currentSession.messages.push(event.message)
// Ensure we still keep last session
clonedFoundRoom.currentSession.sessionId = event.chatSessionId
ctx.setState(
patch({
chatRooms: updateItem(a => a.chatRoomId === clonedFoundRoom.chatRoomId, clonedFoundRoom)
})
)
// Now notify for user
ctx.dispatch(new ChatActions.NotifyNewIncomingMessage({
chatRoomId: clonedFoundRoom.chatRoomId
}))
}
}
else {
// New change: we need to check current chat rooms is reached maximum or not
// If it reached maximum rooms, we need to notify new chat instead of fecthing room
if (state.chatRooms.length < MAX_ROOMS) {
const sender = state.availableUsers.find(a => a.userName === event.message.userName)
ctx.setState(
patch({
invitingUser: sender
&& event.chatSession.chatRoomId != state.activeChatSession.chatRoomId) {
// We need to bring current session of another
let foundOldActiveRoom: DoubleChatRoom = ObjectUtils.clone(state.chatRooms.find(a => a.chatRoomId === state.activeChatSession.chatRoomId))
foundOldActiveRoom.currentSession = {
...state.activeChatSession
}
// Bring another current session
let foundNewActiveRoom = state.chatRooms.find(a => a.chatRoomId === event.chatSession.chatRoomId)
foundNewActiveRoom = {
...foundNewActiveRoom,
lastVisited: (new Date()).getDate()
}
ctx.setState(
patch({
chatRooms: updateItem(a => a.chatRoomId === foundNewActiveRoom.chatRoomId, foundNewActiveRoom)
})
)
return ctx.setState(
patch({
activeChatSession: foundNewActiveRoom.currentSession,
chatRooms: updateItem(a => a.chatRoomId === foundOldActiveRoom.chatRoomId, foundOldActiveRoom)
})
)
}
else if (!ObjectUtils.isNotNull(state.activeChatSession)) {
return ctx.setState(
patch({
activeChatSession: event.chatSession
})
public activeChatBox(ctx: StateContext, { }: ChatActions.ActiveChatSearchBox) {
const state = ctx.getState()
// We need to check there are any active chat,
// if had, we need to move it back to chat room
if (ObjectUtils.isNotNull(state.activeChatSession)) {
const foundRoom: DoubleChatRoom = ObjectUtils.clone(state.chatRooms.find(a => a.chatRoomId === state.activeChatSession.chatRoomId))
foundRoom.currentSession = state.activeChatSession
ctx.setState(
patch({
chatRooms: updateItem(a => a.chatRoomId === foundRoom.chatRoomId, foundRoom),
activeChatRoom: null
})
)
}
}
// Bring another current session
let foundNewActiveRoom = state.chatRooms.find(a => a.chatRoomId === event.chatSession.chatRoomId)
foundNewActiveRoom = {
...foundNewActiveRoom,
lastVisited: (new Date()).getDate()
}
ctx.setState(
patch({
chatRooms: updateItem(a => a.chatRoomId === foundNewActiveRoom.chatRoomId, foundNewActiveRoom)
})
)
return ctx.setState(
patch({
activeChatSession: foundNewActiveRoom.currentSession,
chatRooms: updateItem(a => a.chatRoomId === foundOldActiveRoom.chatRoomId, foundOldActiveRoom)
})
)
}
else if (!ObjectUtils.isNotNull(state.activeChatSession)) {
return ctx.setState(
patch({
activeChatSession: event.chatSession
})
)
}
}
tap((result) => {
setState(
patch({
movies: updateItem(movie => movie.id === result.id, result)
})
);
}));
}
public incomingOfflineUser(ctx: StateContext, { event }: ChatActions.IncomingOfflineUser) {
const state = ctx.getState()
let foundUser = state.availableUsers.find(a => a.userName === event.offlineUser)
if (ObjectUtils.isNotNull(foundUser)) {
foundUser = {
...foundUser,
isOnline: false
}
return ctx.setState(
patch({
availableUsers: updateItem(a => a.userName === event.offlineUser, foundUser)
})
)
}
}
public incomingUnloadUser(ctx: StateContext, { event }: ChatActions.IncomingMessageFromUnloadUser) {
const state = ctx.getState()
let foundUser = state.availableUsers.find(a => a.userName === event.sender)
if (ObjectUtils.isNotNull(foundUser)) {
foundUser = {
...foundUser,
incomingMessages: foundUser.incomingMessages + 1
}
return ctx.setState(
patch({
availableUsers: updateItem(a => a.userName === event.sender, foundUser)
})
)
}
}
public incomingOnlineUser(ctx: StateContext, { event }: ChatActions.IncomingOnlineUser) {
const state = ctx.getState()
const foundUser = state.availableUsers.find(a => a.userName === event.onlineUser.userName)
if (ObjectUtils.isNotNull(foundUser)) {
return ctx.setState(
patch({
availableUsers: updateItem(a => a.userName === event.onlineUser.userName, event.onlineUser)
})
)
}
else {
return ctx.setState(
patch({
availableUsers: insertItem(event.onlineUser)
})
)
}
}