This commit is contained in:
Peter Maquiran
2022-02-07 13:41:27 +01:00
17 changed files with 1120 additions and 1107 deletions
+48 -23
View File
@@ -2,6 +2,8 @@ import { Injectable } from '@angular/core';
import { Message } from 'src/app/models/chatMethod';
import { chatHistory, ChatMessage, File } from 'src/app/models/chatMethod'
import { Storage } from '@ionic/storage';
import { SessionStore } from 'src/app/store/session.service';
import { capitalizeTxt } from 'src/plugin/text'
@Injectable({
providedIn: 'root'
@@ -17,54 +19,77 @@ export class MessageService {
u = {}
t = ''
_id =''
_updatedAt = ''
_updatedAt
file
attachments
offline = false
offline = true
displayType = ''
constructor(private storage: Storage) {
}
}
setData({customFields, channels, mentions, msg ,rid ,ts, u, t, _id, _updatedAt, file, attachments}:Message) {
this.customFields = customFields
this.channels = channels
this.mentions = mentions
this.msg = msg
this.customFields = customFields
this.channels = channels || []
this.mentions = mentions || []
this.msg = msg || ""
this.rid = rid
this.ts = ts
this.u = u
this.u = u || { name: this.usernameToDisplayName(SessionStore.user.RochetChatUser), username: SessionStore.user.RochetChatUser, _id: ""}
this.t = t
this._id = _id
this._updatedAt = _updatedAt
this._updatedAt = _updatedAt || new Date().getTime()
this.file = file
this.attachments = attachments
if (this.file) {
if (this.file.guid) {
this.storage.get(this.file.guid).then((image) => {
// console.log('IMAGE FROM STORAGE', image)
this.file.image_url = image
});
}
}
if(this.rid == 'offline') {
if(!this.ts) {
this.offline = true
} else {
this.offline = false
}
if (this.file) {
this.getFileFromDb()
if(this.file.type) {
if(this.file.type != 'application/webtrix' && typeof(this.file.type) == 'string') {
this.displayType = this.file.type.replace('application/','').toUpperCase()
}
}
}
}
delete() {}
showDateDuration() {}
resend() {
}
private usernameToDisplayName(username) {
const firstName = capitalizeTxt(username.split('.')[0])
const lastName = capitalizeTxt(username.split('.')[1])
return firstName + ' ' + lastName
}
getFileFromDb() {
if (this.file) {
if (this.file.guid) {
this.storage.get(this.file.guid).then((image) => {
if(image != null) {
this.file.image_url = image
}
});
}
}
}
}
+72 -54
View File
@@ -14,6 +14,7 @@ import { capitalizeTxt } from 'src/plugin/text'
import { SortService } from '../functions/sort.service';
import { chatUser } from 'src/app/models/chatMethod';
import { environment } from 'src/environments/environment';
import { ChatService } from 'src/app/services/chat.service';
@Injectable({
providedIn: 'root'
@@ -53,6 +54,7 @@ export class RoomService {
}
sortRoomList = () => {}
uploadAttachment = (formData) => {}
constructor(
public WsChatService: WsChatService,
@@ -62,6 +64,7 @@ export class RoomService {
private sqlservice: SqliteService,
private NativeNotificationService: NativeNotificationService,
private sortService: SortService,
private chatService: ChatService,
) {
this.NativeNotificationService.askForPermission()
}
@@ -88,9 +91,10 @@ export class RoomService {
this.id,
"stream-room-messages",
(ChatMessage) => {
ChatMessage = ChatMessage.fields.args[0]
if(environment.chatOffline == false || this.isSenderIsNotMe(ChatMessage)) {
ChatMessage = ChatMessage.fields.args[0]
ChatMessage = this.fix_updatedAt(ChatMessage)
console.log('recivemessage', ChatMessage)
@@ -201,61 +205,67 @@ export class RoomService {
/**
* @description sen text message
*/
send() {
send({file = null, attachments = null}) {
if(environment.chatOffline == false) {
this.WsChatService.send(this.id, this.message)
this.message= ''
let offlineChatMessage = {
rid: this.id,
msg: this.message,
attachments,
file,
}
this.addMessageDB(offlineChatMessage)
const message: MessageService = this.prepareMessage(offlineChatMessage)
setTimeout(() => {
this.scrollDown()
}, 150)
this.lastMessage = message
if(file == null && attachments == null) {
console.log('simple send')
this.WsChatService.send({roomId:this.id, msg:message.msg}).then((data: any) => {
let ChatMessage = data.result
this.redefinedMessage(message, ChatMessage)
})
} else {
const offlineChatMessage = {
channels: [],
mentions: [],
rid: 'offline',
msg: this.message,
ts: { $date: new Date().getTime() },
u: {
name: this.usernameToDisplayName(SessionStore.user.RochetChatUser),
username: SessionStore.user.RochetChatUser,
_id: ""
},
_updatedAt: new Date().getTime()
console.log('complex send')
return {
message: message,
updateMessage: (update)=> {
offlineChatMessage = Object.assign(offlineChatMessage, update)
this.WsChatService.send({roomId:this.id, msg:message.msg,attachments:offlineChatMessage.attachments, file:offlineChatMessage.file}).then((data: any) => {
console.log('send sucees', data.result)
let ChatMessage = data.result
this.redefinedMessage(message, ChatMessage)
})
}
}
this.addMessageDB(offlineChatMessage)
const message: MessageService = this.prepareMessage(offlineChatMessage)
setTimeout(() => {
this.scrollDown()
}, 150)
this.lastMessage = message
this.redefinedMessage(message)
this.calDateDuration(message._updatedAt)
this.sortRoomList()
this.message= ''
}
this.calDateDuration(message._updatedAt)
this.sortRoomList()
this.message= ''
}
redefinedMessage = (message: MessageService) => {
this.WsChatService.send(this.id, message.msg).then((data: any) => {
let ChatMessage = data.result
redefinedMessage (message: MessageService, ChatMessage) {
ChatMessage = this.fix_updatedAt(ChatMessage)
ChatMessage = this.fix_updatedAt(ChatMessage)
message.setData(ChatMessage)
message.setData(ChatMessage)
if( new Date(this.lastMessage._updatedAt).getTime() < new Date(message._updatedAt).getTime()) {
this.lastMessage = message
this.calDateDuration(message._updatedAt)
}
this.sortRoomList()
})
if( new Date(this.lastMessage._updatedAt).getTime() < new Date(message._updatedAt).getTime()) {
this.lastMessage = message
this.calDateDuration(message._updatedAt)
}
this.sortRoomList()
}
@@ -330,11 +340,17 @@ export class RoomService {
let localMessages = []
messages.forEach((message = []) => {
const wewMessage = this.prepareMessage(message)
if(messages==null) messages = []
if(wewMessage.rid == 'offline') {
this.redefinedMessage(wewMessage)
messages.reverse().forEach((ChatMessage, index) => {
const wewMessage = this.prepareMessage(ChatMessage)
if(wewMessage.offline == true) {
this.WsChatService.send({roomId:this.id, msg:wewMessage.msg, attachments:wewMessage.attachments, file: wewMessage.file}).then((data: any) => {
let _ChatMessage = data.result
this.redefinedMessage(wewMessage, _ChatMessage)
this.storage.set('chatmsg' + this.id, messages)
})
}
localMessages.push(wewMessage)
@@ -381,7 +397,7 @@ export class RoomService {
prepareMessage(message) {
prepareMessage(message): MessageService {
message = this.fix_updatedAt(message)
const wewMessage = new MessageService(this.storage)
wewMessage.setData(message)
@@ -412,9 +428,11 @@ export class RoomService {
if (message.result) {
//console.log('FIX UPDATE ', message.result)
message.result._updatedAt = message.result._updatedAt['$date']
} else if(message._updatedAt.hasOwnProperty('$date')) {
// console.log('FIX UPDATE 11', message)
message._updatedAt = message._updatedAt['$date']
} else if(message._updatedAt) {
if(message._updatedAt.hasOwnProperty('$date')) {
// console.log('FIX UPDATE 11', message)
message._updatedAt = message._updatedAt['$date']
}
}
return message
}
@@ -203,7 +203,7 @@ export class WsChatMethodsService {
prepareRoom(roomData) {
let room:RoomService;
room = new RoomService(this.WsChatService, new MessageService(this.storage), this.storage, this.platform, this.sqlservice, this.NativeNotificationService, this.sortService)
room = new RoomService(this.WsChatService, new MessageService(this.storage), this.storage, this.platform, this.sqlservice, this.NativeNotificationService, this.sortService, this.ChatService)
room.setData({
customFields: roomData.customFields,
+12 -11
View File
@@ -162,7 +162,7 @@ export class WsChatService {
}
// send message to room
send(roomId, msg) {
send({roomId, msg, attachments = null, file = null}) {
const requestId = uuidv4()
@@ -171,9 +171,10 @@ export class WsChatService {
method: "sendMessage",
id: requestId,
params: [{
_id: uuidv4(),
rid: roomId,
msg: msg
msg: msg,
attachments,
file
}]
}
@@ -181,7 +182,7 @@ export class WsChatService {
return new Promise((resolve, reject) => {
this.ws.registerCallback({type:'Onmessage', funx:(message)=>{
if(message.id == requestId || deepFind(message,'result.id') == requestId) { // same request send
if(message.id == requestId ) { // same request send
resolve(message)
return true
}
@@ -206,7 +207,7 @@ export class WsChatService {
return new Promise((resolve, reject) => {
this.ws.registerCallback({type:'Onmessage', funx:(message)=>{
if(message.id == requestId || deepFind(message,'result.id') == requestId) { // same request send
if(message.id == requestId ) { // same request send
resolve(message)
return true
}
@@ -232,7 +233,7 @@ export class WsChatService {
return new Promise((resolve, reject) => {
this.ws.registerCallback({type:'Onmessage', funx:(message)=>{
if(message.id == requestId || deepFind(message,'result.id') == requestId) { // same request send
if(message.id == requestId ) { // same request send
resolve(message)
return true
}
@@ -255,7 +256,7 @@ export class WsChatService {
return new Promise((resolve, reject) => {
this.ws.registerCallback({type:'Onmessage', funx:(message)=>{
if(message.id == requestId || deepFind(message,'result.id') == requestId) { // same request send
if(message.id == requestId ) { // same request send
resolve(message)
return true
}
@@ -310,7 +311,7 @@ export class WsChatService {
return new Promise((resolve, reject) => {
this.ws.registerCallback({type:'Onmessage', funx:(message)=>{
if(message.id == requestId || deepFind(message,'result.id') == requestId) { // same request send
if(message.id == requestId ) { // same request send
resolve(message)
return true
}
@@ -337,7 +338,7 @@ export class WsChatService {
return new Promise((resolve, reject) => {
this.ws.registerCallback({type:'Onmessage', funx:(message)=>{
if(message.id == requestId || deepFind(message,'result.id') == requestId) { // same request send
if(message.id == requestId ) { // same request send
resolve(message)
return true
}
@@ -565,7 +566,7 @@ updateRoomEventss(roomId, collection:string, funx: Function, ) {
return new Promise((resolve, reject) => {
this.ws.registerCallback({type:'Onmessage', funx:(message)=>{
if(message.id == requestId || deepFind(message,'result.id') == requestId) { // same request send
if(message.id == requestId ) { // same request send
resolve(message)
return true
}
@@ -592,7 +593,7 @@ updateRoomEventss(roomId, collection:string, funx: Function, ) {
return new Promise((resolve, reject) => {
this.ws.registerCallback({type:'Onmessage', funx:(message)=>{
if(message.id == requestId || deepFind(message,'result.id') == requestId) { // same request send
if(message.id == requestId ) { // same request send
resolve(message)
return true
}