This commit is contained in:
Peter Maquiran
2022-02-10 20:16:15 +01:00
24 changed files with 400 additions and 257 deletions
+56 -25
View File
@@ -5,6 +5,8 @@ import { SessionStore } from 'src/app/store/session.service';
import { capitalizeTxt } from 'src/plugin/text'
import { NfService } from 'src/app/services/chat/nf.service'
import { WsChatService } from 'src/app/services/chat/ws-chat.service';
import { environment } from 'src/environments/environment';
import { showDateDuration } from 'src/plugin/showDateDuration';
@Injectable({
providedIn: 'root'
})
@@ -30,14 +32,17 @@ export class MessageService {
sendAttempt = 0
uploadingFile = false
errorUploadingAttachment = false
loadHistory = false
duration = ''
localReference = null
constructor(private storage: Storage,
private NfService: NfService,
private WsChatService: WsChatService) {
}
setData({customFields, channels, mentions, msg ,rid ,ts, u, t, _id, _updatedAt, file, attachments, temporaryData}:Message) {
this.customFields = customFields
setData({customFields, channels, mentions, msg ,rid ,ts, u, t, _id, _updatedAt, file, attachments, temporaryData, localReference}:Message) {
this.customFields = customFields
this.channels = channels || []
this.mentions = mentions || []
this.msg = msg || ""
@@ -50,6 +55,7 @@ export class MessageService {
this.file = file
this.attachments = attachments
this.temporaryData = temporaryData
this.localReference = localReference || null
if(!this.ts) {
this.offline = true
@@ -71,6 +77,8 @@ export class MessageService {
this.displayType = this.file.type.replace('application/','').toUpperCase()
}
}
this.calDateDuration()
}
private usernameToDisplayName(username) {
@@ -78,7 +86,7 @@ export class MessageService {
const firstName = capitalizeTxt(username.split('.')[0])
const lastName = capitalizeTxt(username.split('.')[1])
return firstName + ' ' + lastName
}
}
getFileFromDb() {
@@ -93,19 +101,26 @@ export class MessageService {
}
}
async send() {
async send(): Promise<any> {
this.sendAttempt++;
if(!this.hasFile) {
console.log('simple send')
this.WsChatService.send({roomId:this.rid, msg:this.msg}).then((data: any) => {
let ChatMessage = data.result
this.redefinedMessage(this, ChatMessage)
this.offline = false
this.WsChatService.send({roomId:this.rid, msg:this.msg, requestId: this.localReference}).then(({message, requestId}) => {
let ChatMessage = message.result
if (environment.chatOffline) {
this.redefinedMessage(ChatMessage)
alert('redifie')
this.offline = false
}
return new Promise((resolve, reject)=>{
resolve(ChatMessage)
})
})
} else {
console.log('complex send')
this.uploadingFile = true
@@ -113,41 +128,52 @@ export class MessageService {
if(this.hasSendAttachment == false) {
uploadSuccessfully = await this.NfService.beforeSendAttachment(this)
}
this.uploadingFile = false
if(uploadSuccessfully || this.hasSendAttachment == false) {
this.hasSendAttachment = true
this.errorUploadingAttachment = false
this.temporaryData = {}
this.WsChatService.send({roomId:this.rid, msg: this.msg, attachments: this.attachments, file: this.file}).then((data: any) => {
console.log('send sucees', data.result)
let ChatMessage = data.result
this.redefinedMessage(this, ChatMessage)
this.offline = false
alert('to send')
this.WsChatService.send({roomId:this.rid, msg: this.msg, attachments: this.attachments, file: this.file, requestId: this.localReference}).then(({message, requestId}) => {
alert('receive11')
console.log('message', message)
let ChatMessage = message.result
if (environment.chatOffline) {
this.redefinedMessage(ChatMessage)
this.offline = false
}
return new Promise((resolve, reject)=>{
resolve(ChatMessage)
})
})
} else if(this.WsChatService.isLogin == false) {
this.WsChatService.registerCallback({
type: 'reConnect',
funx:()=> {
alert('reConnect')
// this.send()
return true
funx: async ()=> {
return await this.send()
}
})
} else if(uploadSuccessfully == false) {
this.errorUploadingAttachment = true
return new Promise((resolve, reject)=>{
reject(false)
})
}
}
}
redefinedMessage(messagem, ChatMessage) {
redefinedMessage(ChatMessage) {
ChatMessage = this.NfService.fix_updatedAt(ChatMessage)
this.setData(ChatMessage)
}
@@ -160,4 +186,9 @@ export class MessageService {
}
private calDateDuration(date = null) {
this.duration = showDateDuration(date || this._updatedAt);
}
}