Files
doneit-web/src/app/services/chat/message.service.ts
T
Peter Maquiran 26a209502e improve
2022-02-10 14:07:16 +01:00

168 lines
4.1 KiB
TypeScript

import { Injectable } from '@angular/core';
import { Message } from 'src/app/models/chatMethod';
import { Storage } from '@ionic/storage';
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';
@Injectable({
providedIn: 'root'
})
export class MessageService {
customFields
channels = []
mentions = []
msg = ''
rid = ''
ts = {}
u = {}
t = ''
_id =''
_updatedAt
file
attachments
offline = true
displayType = ''
temporaryData: any = {}
hasFile = false
hasSendAttachment = false
sendAttempt = 0
uploadingFile = false
errorUploadingAttachment = false
loadHistory = false
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
this.channels = channels || []
this.mentions = mentions || []
this.msg = msg || ""
this.rid = rid
this.ts = ts
this.u = u || { name: this.usernameToDisplayName(SessionStore.user.RochetChatUser), username: SessionStore.user.RochetChatUser, _id: ""}
this.t = t
this._id = _id
this._updatedAt = _updatedAt || new Date().getTime()
this.file = file
this.attachments = attachments
this.temporaryData = temporaryData
if(!this.ts) {
this.offline = true
} else {
this.offline = false
}
if (this.file) {
if(this.file.type) {
if(typeof(this.file.type) == 'string') {
this.hasFile = true
}
}
}
if(this.hasFile) {
this.getFileFromDb()
if(this.file.type != 'application/webtrix') {
this.displayType = this.file.type.replace('application/','').toUpperCase()
}
}
}
private usernameToDisplayName(username) {
const firstName = capitalizeTxt(username.split('.')[0])
const lastName = capitalizeTxt(username.split('.')[1])
return firstName + ' ' + lastName
}
getFileFromDb() {
if(this.hasFile) {
if (this.file.guid) {
this.storage.get(this.file.guid).then((image) => {
if(image != null) {
this.file.image_url = image
}
});
}
}
}
async send() {
this.sendAttempt++;
if(!this.hasFile) {
this.WsChatService.send({roomId:this.rid, msg:this.msg}).then((data: any) => {
if (environment.chatOffline) {
let ChatMessage = data.result
this.redefinedMessage(this, ChatMessage)
this.offline = false
}
})
} else {
this.uploadingFile = true
let uploadSuccessfully = false
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) => {
if (environment.chatOffline) {
// console.log('send sucees', data.result)
let ChatMessage = data.result
this.redefinedMessage(this, ChatMessage)
this.offline = false
}
})
} else if(this.WsChatService.isLogin == false) {
this.WsChatService.registerCallback({
type: 'reConnect',
funx:()=> {
this.send()
return true
}
})
} else if(uploadSuccessfully == false) {
this.errorUploadingAttachment = true
}
}
}
redefinedMessage(messagem, ChatMessage) {
this.setData(ChatMessage)
}
async downloadFileMsg() {
const result = await this.NfService.beforeSendAttachment(this)
if(result) {
}
}
}