Skip to content

Commit

Permalink
Merge pull request #3802 from dpalou/MOBILE-4362
Browse files Browse the repository at this point in the history
Mobile 4362
  • Loading branch information
crazyserver authored Oct 2, 2023
2 parents 54a2e6e + 31bd7a6 commit b510d95
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 16 deletions.
15 changes: 8 additions & 7 deletions src/addons/messages/pages/discussion/discussion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class AddonMessagesDiscussionPage implements OnInit, OnDestroy, AfterView

protected messagesBeingSent = 0;
protected pagesLoaded = 1;
protected lastMessage = { text: '', timecreated: 0 };
protected lastMessage?: { text: string; timecreated: number };
protected keepMessageMap: {[hash: string]: boolean} = {};
protected syncObserver: CoreEventObserver;
protected oldContentHeight = 0;
Expand Down Expand Up @@ -465,7 +465,7 @@ export class AddonMessagesDiscussionPage implements OnInit, OnDestroy, AfterView

// If we received a new message while using group messaging, force mark messages as read.
const last = this.messages[this.messages.length - 1];
const forceMark = this.groupMessagingEnabled && last && last.useridfrom != this.currentUserId && this.lastMessage.text != ''
const forceMark = this.groupMessagingEnabled && last && last.useridfrom !== this.currentUserId && !!this.lastMessage
&& (last.text !== this.lastMessage.text || last.timecreated !== this.lastMessage.timecreated);

// Notify that there can be a new message.
Expand Down Expand Up @@ -773,14 +773,14 @@ export class AddonMessagesDiscussionPage implements OnInit, OnDestroy, AfterView
* Notify the last message found so discussions list controller can tell if last message should be updated.
*/
protected notifyNewMessage(): void {
const last = this.messages[this.messages.length - 1];
const last = this.messages[this.messages.length - 1] as AddonMessagesConversationMessageFormatted | undefined;

let trigger = false;

if (!last) {
this.lastMessage = { text: '', timecreated: 0 };
this.lastMessage = undefined;
trigger = true;
} else if (last.text !== this.lastMessage.text || last.timecreated !== this.lastMessage.timecreated) {
} else if (last.text !== this.lastMessage?.text || last.timecreated !== this.lastMessage?.timecreated) {
this.lastMessage = { text: last.text || '', timecreated: last.timecreated };
trigger = true;
}
Expand All @@ -790,8 +790,9 @@ export class AddonMessagesDiscussionPage implements OnInit, OnDestroy, AfterView
CoreEvents.trigger(AddonMessagesProvider.NEW_MESSAGE_EVENT, {
conversationId: this.conversationId,
userId: this.userId,
message: this.lastMessage.text,
timecreated: this.lastMessage.timecreated,
message: this.lastMessage?.text,
timecreated: this.lastMessage?.timecreated ?? 0,
userFrom: last?.useridfrom ? this.members[last.useridfrom] : undefined,
isfavourite: !!this.conversation?.isfavourite,
type: this.conversation?.type,
}, this.siteId);
Expand Down
2 changes: 1 addition & 1 deletion src/addons/messages/pages/discussions-35/discussions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class AddonMessagesDiscussions35Page implements OnInit, OnDestroy {
});
} else if (discussion.message) {
// An existing discussion has a new message, update the last message.
discussion.message.message = data.message;
discussion.message.message = data.message ?? '';
discussion.message.timecreated = data.timecreated;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ <h2>{{ 'addon.messages.individualconversations' | translate }} ({{ individual.co
<p *ngIf="conversation.subname">
<core-format-text [text]="conversation.subname" contextLevel="system" [contextInstanceId]="0"></core-format-text>
</p>
<p class="addon-message-last-message">
<p *ngIf="conversation.lastmessage !== undefined" class="addon-message-last-message">
<span *ngIf="conversation.sentfromcurrentuser" class="addon-message-last-message-user">
{{ 'addon.messages.you' | translate }}
</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export class AddonMessagesGroupConversationsPage implements OnInit, OnDestroy {
this.newMessagesObserver = CoreEvents.on(
AddonMessagesProvider.NEW_MESSAGE_EVENT,
(data) => {
// Check if the new message belongs to the option that is currently expanded.
// Check if the new message belongs to the option that is currently expanded.
const expandedOption = this.getExpandedOption();
const messageOption = this.getConversationOption(data);

Expand All @@ -124,27 +124,42 @@ export class AddonMessagesGroupConversationsPage implements OnInit, OnDestroy {
const conversation = this.findConversation(data.conversationId, data.userId, expandedOption);

if (conversation === undefined) {
// Probably a new conversation, refresh the list.
// Probably a new conversation, refresh the list.
this.loaded = false;
this.refreshData().finally(() => {
this.loaded = true;
});

return;
}
if (conversation.lastmessage != data.message || conversation.lastmessagedate != data.timecreated / 1000) {

if (data.message === undefined) {
conversation.lastmessage = undefined;
conversation.lastmessagedate = undefined;
conversation.sentfromcurrentuser = undefined;

return;
}

if (conversation.lastmessage !== data.message || conversation.lastmessagedate !== data.timecreated / 1000) {
const isNewer = data.timecreated / 1000 > (conversation.lastmessagedate || 0);

// An existing conversation has a new message, update the last message.
conversation.lastmessage = data.message;
conversation.lastmessagedate = data.timecreated / 1000;
if (data.userFrom) {
conversation.sentfromcurrentuser = data.userFrom.id === this.currentUserId;
if (conversation.type === AddonMessagesProvider.MESSAGE_CONVERSATION_TYPE_GROUP) {
conversation.members[0] = data.userFrom;
}
}

// Sort the affected list.
const option = this.getConversationOption(conversation);
option.conversations = AddonMessages.sortConversations(option.conversations || []);

if (isNewer) {
// The last message is newer than the previous one, scroll to top to keep viewing the conversation.
// The last message is newer than the previous one, scroll to top to keep viewing the conversation.
this.content?.scrollToTop();
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/addons/messages/services/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3669,8 +3669,9 @@ export type AddonMessagesReadChangedEventData = {
export type AddonMessagesNewMessagedEventData = {
conversationId?: number;
userId?: number;
message: string;
message?: string; // If undefined it means the conversation has no messages, e.g. last message was deleted.
timecreated: number;
userFrom?: AddonMessagesConversationMember;
isfavourite: boolean;
type?: number;
};
Expand Down
2 changes: 1 addition & 1 deletion src/addons/notes/lang.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
"personalnotes": "Personal notes",
"publishstate": "Context",
"sitenotes": "Site notes",
"warningnotenotsent": "Couldn't add note(s) to course {{course}}. {{error}}"
"warningnotenotsent": "Couldn't add or delete note(s). {{error}}"
}
2 changes: 1 addition & 1 deletion src/addons/notes/services/notes-sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ export class AddonNotesSyncProvider extends CoreSyncBaseProvider<AddonNotesSyncR

result.warnings = errors.map((error) =>
Translate.instant('addon.notes.warningnotenotsent', {
course: 'fullname' in course ? course.fullname : courseId,
course: 'fullname' in course ? course.fullname : courseId, // @deprecated since 4.3.
error: error,
}));
}
Expand Down

0 comments on commit b510d95

Please sign in to comment.