Files
doneit-web/src/app/services/chat/message.service.ts
T

168 lines
4.1 KiB
TypeScript
Raw Normal View History

2022-01-10 23:52:33 +01:00
import { Injectable } from '@angular/core';
2022-01-13 10:52:03 +01:00
import { Message } from 'src/app/models/chatMethod';
2022-01-28 15:28:21 +01:00
import { Storage } from '@ionic/storage';
2022-02-03 21:01:53 +01:00
import { SessionStore } from 'src/app/store/session.service';
import { capitalizeTxt } from 'src/plugin/text'
2022-02-07 17:55:00 +01:00
import { NfService } from 'src/app/services/chat/nf.service'
2022-02-07 20:18:48 +01:00
import { WsChatService } from 'src/app/services/chat/ws-chat.service';
2022-02-10 14:07:16 +01:00
import { environment } from 'src/environments/environment';
2022-01-10 23:52:33 +01:00
@Injectable({
providedIn: 'root'
})
export class MessageService {
2022-01-13 21:24:43 +01:00
customFields
2022-01-11 15:43:09 +01:00
channels = []
mentions = []
msg = ''
rid = ''
2022-01-11 16:07:54 +01:00
ts = {}
u = {}
t = ''
2022-01-11 16:07:54 +01:00
_id =''
2022-02-03 21:01:53 +01:00
_updatedAt
2022-01-13 10:52:03 +01:00
file
attachments
2022-02-03 21:01:53 +01:00
offline = true
2022-02-04 00:22:35 +01:00
displayType = ''
2022-02-07 17:55:00 +01:00
temporaryData: any = {}
hasFile = false
hasSendAttachment = false
2022-02-08 17:44:15 +01:00
sendAttempt = 0
uploadingFile = false
errorUploadingAttachment = false
2022-02-10 14:07:16 +01:00
loadHistory = false
2022-01-11 16:07:54 +01:00
2022-02-07 17:55:00 +01:00
constructor(private storage: Storage,
2022-02-07 20:18:48 +01:00
private NfService: NfService,
private WsChatService: WsChatService) {
2022-02-03 21:01:53 +01:00
}
2022-01-11 16:07:54 +01:00
2022-02-07 15:51:21 +01:00
setData({customFields, channels, mentions, msg ,rid ,ts, u, t, _id, _updatedAt, file, attachments, temporaryData}:Message) {
2022-02-03 21:01:53 +01:00
this.customFields = customFields
this.channels = channels || []
this.mentions = mentions || []
this.msg = msg || ""
2022-01-11 16:07:54 +01:00
this.rid = rid
this.ts = ts
2022-02-03 21:01:53 +01:00
this.u = u || { name: this.usernameToDisplayName(SessionStore.user.RochetChatUser), username: SessionStore.user.RochetChatUser, _id: ""}
this.t = t
2022-01-11 16:07:54 +01:00
this._id = _id
2022-02-03 21:01:53 +01:00
this._updatedAt = _updatedAt || new Date().getTime()
2022-01-13 10:52:03 +01:00
this.file = file
this.attachments = attachments
2022-02-07 20:18:48 +01:00
this.temporaryData = temporaryData
2022-01-28 15:28:21 +01:00
2022-02-03 21:01:53 +01:00
if(!this.ts) {
this.offline = true
} else {
this.offline = false
}
2022-01-28 15:28:21 +01:00
if (this.file) {
2022-02-04 00:22:35 +01:00
if(this.file.type) {
2022-02-07 17:55:00 +01:00
if(typeof(this.file.type) == 'string') {
this.hasFile = true
2022-02-04 00:22:35 +01:00
}
2022-01-28 15:28:21 +01:00
}
}
2022-02-02 20:16:12 +01:00
2022-02-07 17:55:00 +01:00
if(this.hasFile) {
this.getFileFromDb()
if(this.file.type != 'application/webtrix') {
this.displayType = this.file.type.replace('application/','').toUpperCase()
}
}
2022-01-11 16:07:54 +01:00
}
2022-02-03 21:01:53 +01:00
private usernameToDisplayName(username) {
const firstName = capitalizeTxt(username.split('.')[0])
const lastName = capitalizeTxt(username.split('.')[1])
return firstName + ' ' + lastName
}
2022-02-04 00:22:35 +01:00
getFileFromDb() {
2022-02-07 17:55:00 +01:00
if(this.hasFile) {
2022-02-04 00:22:35 +01:00
if (this.file.guid) {
this.storage.get(this.file.guid).then((image) => {
if(image != null) {
this.file.image_url = image
}
});
}
}
}
2022-02-08 17:44:15 +01:00
async send() {
this.sendAttempt++;
if(!this.hasFile) {
2022-02-07 20:18:48 +01:00
this.WsChatService.send({roomId:this.rid, msg:this.msg}).then((data: any) => {
2022-02-10 14:07:16 +01:00
if (environment.chatOffline) {
let ChatMessage = data.result
this.redefinedMessage(this, ChatMessage)
this.offline = false
}
2022-02-07 20:18:48 +01:00
})
} else {
2022-02-08 17:44:15 +01:00
this.uploadingFile = true
let uploadSuccessfully = false
if(this.hasSendAttachment == false) {
uploadSuccessfully = await this.NfService.beforeSendAttachment(this)
}
2022-02-07 20:18:48 +01:00
2022-02-08 17:44:15 +01:00
this.uploadingFile = false
2022-02-07 20:18:48 +01:00
2022-02-08 17:44:15 +01:00
if(uploadSuccessfully || this.hasSendAttachment == false) {
2022-02-07 20:18:48 +01:00
this.hasSendAttachment = true
2022-02-08 17:44:15 +01:00
this.errorUploadingAttachment = false
this.temporaryData = {}
2022-02-07 20:18:48 +01:00
this.WsChatService.send({roomId:this.rid, msg: this.msg, attachments: this.attachments, file: this.file}).then((data: any) => {
2022-02-10 14:07:16 +01:00
if (environment.chatOffline) {
// console.log('send sucees', data.result)
let ChatMessage = data.result
this.redefinedMessage(this, ChatMessage)
this.offline = false
}
2022-02-08 17:44:15 +01:00
})
} else if(this.WsChatService.isLogin == false) {
2022-02-08 19:17:44 +01:00
2022-02-08 17:44:15 +01:00
this.WsChatService.registerCallback({
type: 'reConnect',
funx:()=> {
2022-02-10 14:07:16 +01:00
this.send()
2022-02-08 17:44:15 +01:00
return true
}
2022-02-07 20:18:48 +01:00
})
2022-02-08 17:44:15 +01:00
} else if(uploadSuccessfully == false) {
2022-02-08 19:17:44 +01:00
2022-02-08 17:44:15 +01:00
this.errorUploadingAttachment = true
2022-02-07 20:18:48 +01:00
}
}
}
2022-02-08 17:44:15 +01:00
redefinedMessage(messagem, ChatMessage) {
2022-02-07 20:18:48 +01:00
this.setData(ChatMessage)
2022-02-07 15:51:21 +01:00
}
2022-02-08 17:44:15 +01:00
async downloadFileMsg() {
const result = await this.NfService.beforeSendAttachment(this)
if(result) {
}
}
2022-01-10 23:52:33 +01:00
}