Skip to content

Commit

Permalink
Merge pull request #723 from AiursoftWeb/resend
Browse files Browse the repository at this point in the history
click to resend or discard
  • Loading branch information
Anduin2017 authored Oct 27, 2019
2 parents fe8c0ae + b683ea0 commit 8dee78b
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 77 deletions.
47 changes: 37 additions & 10 deletions src/app/Controllers/talking.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ export class TalkingComponent implements OnInit, OnDestroy {
public showUserList = false;
public matchedUsers: Array<KahlaUser> = [];

@ViewChild('imageInput', {static: false}) public imageInput;
@ViewChild('videoInput', {static: false}) public videoInput;
@ViewChild('fileInput', {static: false}) public fileInput;
@ViewChild('header', {static: true}) public header: HeaderComponent;
@ViewChild('imageInput', { static: false }) public imageInput;
@ViewChild('videoInput', { static: false }) public videoInput;
@ViewChild('fileInput', { static: false }) public fileInput;
@ViewChild('header', { static: true }) public header: HeaderComponent;

constructor(
private route: ActivatedRoute,
Expand Down Expand Up @@ -166,7 +166,7 @@ export class TalkingComponent implements OnInit, OnDestroy {
if (this.cacheService.cachedData.conversationDetail[this.conversationID]) {
this.updateConversation(this.cacheService.cachedData.conversationDetail[this.conversationID]);
this.messageService.initMessage(this.conversationID);
this.messageService.getMessages(this.unread, this.conversationID, -1, this.load);
this.messageService.getMessages(this.unread, this.conversationID, -1, this.load, false);
} else {
const listItem = this.cacheService.cachedData.conversations.find(t => t.conversationId === this.conversationID);
if (listItem) {
Expand Down Expand Up @@ -200,11 +200,12 @@ export class TalkingComponent implements OnInit, OnDestroy {
this.updateConversation(conversation);
if (!this.cacheService.cachedData.conversationDetail[this.conversationID]) {
this.messageService.initMessage(this.conversationID);
this.messageService.getMessages(this.unread, this.conversationID, -1, this.load);
this.messageService.getMessages(this.unread, this.conversationID, -1, this.load, false);
}
this.messageService.cleanMessageByTimer();
this.cacheService.cachedData.conversationDetail[this.conversationID] = conversation;
this.cacheService.saveCache();
this.messageService.showFailedMessages();
});
this.windowInnerHeight = window.innerHeight;
}
Expand Down Expand Up @@ -271,10 +272,10 @@ export class TalkingComponent implements OnInit, OnDestroy {
if (unsentMessages.get(_this.conversationID) &&
(<Array<string>>unsentMessages.get(_this.conversationID)).length > 0) {
const tempArray = <Array<string>>unsentMessages.get(_this.conversationID);
tempArray.push(encryptedMessage);
tempArray.push(he.decode(tempMessage.content));
unsentMessages.set(_this.conversationID, tempArray);
} else {
unsentMessages.set(_this.conversationID, [encryptedMessage]);
unsentMessages.set(_this.conversationID, [he.decode(tempMessage.content)]);
}
localStorage.setItem('unsentMessages', JSON.stringify(Array.from(unsentMessages)));
}
Expand All @@ -286,6 +287,32 @@ export class TalkingComponent implements OnInit, OnDestroy {
inputElement.style.height = 34 + 'px';
}

public resend(content: string): void {
const messageIDArry = this.messageService.getAtIDs(content);
const encryptedMessage = AES.encrypt(content, this.messageService.conversation.aesKey).toString();
this.conversationApiService.SendMessage(this.messageService.conversation.id, encryptedMessage, messageIDArry.slice(1))
.subscribe(result => {
if (result.code === 0) {
this.delete(content);
}
});
}

public delete(content: string): void {
for (let i = 0; i < this.messageService.localMessages.length; i++) {
if (this.messageService.localMessages[i].resend && this.messageService.localMessages[i].content === content) {
this.messageService.localMessages.splice(i, 1);
break;
}
}
const unsentMessages = new Map(JSON.parse(localStorage.getItem('unsentMessages')));
const tempArray = <Array<string>>unsentMessages.get(this.conversationID);
const index = tempArray.indexOf(content);
tempArray.splice(index, 1);
unsentMessages.set(this.conversationID, tempArray);
localStorage.setItem('unsentMessages', JSON.stringify(Array.from(unsentMessages)));
}

public startInput(): void {
if (this.showPanel) {
this.showPanel = false;
Expand Down Expand Up @@ -412,7 +439,7 @@ export class TalkingComponent implements OnInit, OnDestroy {
if (this.recording) {
this.mediaRecorder.stop();
} else {
navigator.mediaDevices.getUserMedia({audio: true})
navigator.mediaDevices.getUserMedia({ audio: true })
.then(stream => {
this.recording = true;
this.mediaRecorder = new MediaRecorder(stream);
Expand Down Expand Up @@ -472,7 +499,7 @@ export class TalkingComponent implements OnInit, OnDestroy {


public shareToOther(message: string): void {
this.router.navigate(['share-target', {message: message}]);
this.router.navigate(['share-target', { message: message }]);
}

public getAtListMaxHeight(): number {
Expand Down
2 changes: 1 addition & 1 deletion src/app/Models/Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class Message {
public sender: KahlaUser;
public sendTime: string;
public content: string;

public resend: boolean;
public contentRaw: string;
public isEmoji = false;
public read: boolean;
Expand Down
35 changes: 4 additions & 31 deletions src/app/Services/InitService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { AuthApiService } from './AuthApiService';
import { Router } from '@angular/router';
import { MessageService } from './MessageService';
import { CacheService } from './CacheService';
import { ConversationApiService } from './ConversationApiService';
import { environment } from '../../environments/environment';
import { ElectronService } from 'ngx-electron';
import { DevicesApiService } from './DevicesApiService';
Expand Down Expand Up @@ -35,7 +34,6 @@ export class InitService {
private router: Router,
private messageService: MessageService,
private cacheService: CacheService,
private conversationApiService: ConversationApiService,
private _electronService: ElectronService,
private themeService: ThemeService,
private devicesApiService: DevicesApiService,
Expand All @@ -59,7 +57,7 @@ export class InitService {
this.cacheService.initCache();
this.authApiService.SignInStatus().subscribe(signInStatus => {
if (signInStatus.value === false) {
this.router.navigate(['/signin'], {replaceUrl: true});
this.router.navigate(['/signin'], { replaceUrl: true });
} else {
this.authApiService.Me().subscribe(p => {
if (p.code === 0) {
Expand Down Expand Up @@ -102,13 +100,12 @@ export class InitService {
this.fireNetworkAlert();
};
this.ws.onclose = () => this.errorOrClosedFunc();
this.resend();
if (reconnect) {
this.cacheService.updateConversation();
this.cacheService.updateFriends();
}
if (this.messageService.conversation) {
this.messageService.getMessages(0, this.messageService.conversation.id, -1, 15);
if (this.messageService.conversation) {
this.messageService.getMessages(0, this.messageService.conversation.id, -1, 15, true);
}
}
}, () => {
this.fireNetworkAlert();
Expand Down Expand Up @@ -161,30 +158,6 @@ export class InitService {
}, this.timeoutNumber);
}

private resend(): void {
if (navigator.onLine) {
const unsentMessages = new Map(JSON.parse(localStorage.getItem('unsentMessages')));
unsentMessages.forEach((messages, id) => {
const sendFailMessages = [];
for (let i = 0; i < (<Array<string>>messages).length; i++) {
setTimeout(() => {
const message = (<Array<string>>messages)[i];
this.conversationApiService.SendMessage(Number(id), message, null)
.subscribe({
error(e) {
if (e.status === 0 || e.status === 503) {
sendFailMessages.push(message);
}
}
});
}, 500);
}
unsentMessages.set(id, sendFailMessages);
localStorage.setItem('unsentMessages', JSON.stringify(Array.from(unsentMessages)));
});
}
}

private subscribeUser(): void {
if ('Notification' in window && 'serviceWorker' in navigator && Notification.permission === 'granted') {
const _this = this;
Expand Down
28 changes: 23 additions & 5 deletions src/app/Services/MessageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,22 +97,21 @@ export class MessageService {
}
}
if (this.conversation && this.conversation.id === evt.message.conversationId) {
// this.getMessages(0, this.conversation.id, -1, 15);
this.rawMessages.push(evt.message);
this.localMessages.push(this.modifyMessage(Object.assign({}, evt.message)));
this.reorderLocalMessages();
this.localMessages = this.localMessages.filter((t => !t.local));
this.localMessages = this.localMessages.filter(t => !t.local && !t.resend);
this.updateAtLink();
if (this.belowWindowPercent <= 0.2) {
setTimeout(() => {
this.uploadService.scrollBottom(true);
}, 0);
}
this.conversationApiService.GetMessage(this.conversation.id, -1, 0).subscribe();
if (!document.hasFocus()) {
this.showNotification(evt);
}
this.saveMessage();
this.showFailedMessages();
} else {
this.showNotification(evt);
}
Expand Down Expand Up @@ -209,7 +208,7 @@ export class MessageService {
}
}

public getMessages(unread: number, id: number, skipTill: number, take: number) {
public getMessages(unread: number, id: number, skipTill: number, take: number, reconnect: boolean) {
this.messageLoading = true;
this.conversationApiService.GetMessage(id, skipTill, take)
.pipe(
Expand Down Expand Up @@ -282,6 +281,9 @@ export class MessageService {
}
this.cacheService.updateTotalUnread();
this.messageLoading = false;
if (reconnect) {
this.showFailedMessages();
}
});
}

Expand All @@ -294,7 +296,7 @@ export class MessageService {
if (!this.noMoreMessages) {
this.loadingMore = true;
this.oldScrollHeight = document.documentElement.scrollHeight;
this.getMessages(-1, this.conversation.id, this.localMessages[0].id, 15);
this.getMessages(-1, this.conversation.id, this.localMessages[0].id, 15, false);
}
}

Expand Down Expand Up @@ -508,4 +510,20 @@ export class MessageService {
}
this.saveMessage();
}

public showFailedMessages(): void {
const unsentMessages = new Map(JSON.parse(localStorage.getItem('unsentMessages')));
this.localMessages = this.localMessages.filter(m => !m.local);
if (unsentMessages.has(this.conversation.id)) {
(<Array<string>>unsentMessages.get(this.conversation.id)).forEach(content => {
const message = new Message();
message.content = content;
message.resend = true;
message.senderId = this.cacheService.cachedData.me.id;
message.sender = this.cacheService.cachedData.me;
message.local = true;
this.localMessages.push(message);
}, this);
}
}
}
9 changes: 9 additions & 0 deletions src/app/Styles/talking.scss
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@
width: 70%;
margin-bottom: 1.5%;

* {
vertical-align: middle;
}

.button {
margin-right: 0.5rem;
background-color: red;
}

.share {
cursor: pointer;
}
Expand Down
Loading

0 comments on commit 8dee78b

Please sign in to comment.