polfan-server-js-client 0.2.88 → 0.2.90

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  import { ChatStateTracker } from "./ChatStateTracker";
2
- import { FollowedTopic } from "../types/src";
2
+ import { FollowedTopic, Message, ChatLocation } from "../types/src";
3
3
  import { ObservableIndexedObjectCollection } from "../IndexedObjectCollection";
4
4
  import { RoomMessagesHistory } from "./RoomMessagesHistory";
5
5
  export declare class MessagesManager {
@@ -38,6 +38,11 @@ export declare class MessagesManager {
38
38
  * @internal
39
39
  */
40
40
  _deleteByTopicIds(roomId: string, ...topicIds: string[]): void;
41
+ /**
42
+ * For internal use.
43
+ * @internal
44
+ */
45
+ _resolveLastMessage(location: ChatLocation): Promise<Message | null>;
41
46
  private createHistoryForNewRoom;
42
47
  private handleNewMessage;
43
48
  private handleFollowedTopicUpdated;
@@ -9,8 +9,10 @@ export declare class RoomMessagesHistory {
9
9
  constructor(room: Room, tracker: ChatStateTracker);
10
10
  /**
11
11
  * Returns a history window object for the given topic ID, allowing you to view message history.
12
+ * @param topicId
13
+ * @param peek If true, do not create a cache for this topic and do not allow it to collect new messages.
12
14
  */
13
- getMessagesWindow(topicId: string): Promise<TopicHistoryWindow | undefined>;
15
+ getMessagesWindow(topicId: string, peek?: boolean): Promise<TopicHistoryWindow | undefined>;
14
16
  private handleRoomUpdated;
15
17
  private handleNewTopic;
16
18
  private handleTopicDeleted;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "polfan-server-js-client",
3
- "version": "0.2.88",
3
+ "version": "0.2.90",
4
4
  "description": "JavaScript client library for handling communication with Polfan chat server.",
5
5
  "author": "Jarosław Żak",
6
6
  "license": "MIT",
@@ -7,7 +7,12 @@ import {
7
7
  RoomDeleted,
8
8
  RoomLeft,
9
9
  TopicDeleted,
10
- FollowedTopicUpdated, RoomJoined, NewTopic, Session, Room, MessageType,
10
+ FollowedTopicUpdated,
11
+ RoomJoined,
12
+ NewTopic,
13
+ Session,
14
+ Room,
15
+ Message, ChatLocation,
11
16
  } from "../types/src";
12
17
  import {
13
18
  IndexedCollection,
@@ -129,7 +134,7 @@ export class MessagesManager {
129
134
  return collection.items.reduce(
130
135
  (previousValue: number, currentValue) => previousValue + (currentValue.missed ?? 0),
131
136
  0,
132
- );
137
+ ) as number;
133
138
  }
134
139
 
135
140
  return undefined;
@@ -143,6 +148,30 @@ export class MessagesManager {
143
148
  this.followedTopics.get(roomId)?.delete(...topicIds);
144
149
  }
145
150
 
151
+ /**
152
+ * For internal use.
153
+ * @internal
154
+ */
155
+ public async _resolveLastMessage(location: ChatLocation): Promise<Message|null> {
156
+ // Try to get last message from history window (if cached)
157
+ let message: Message = await this.getRoomHistory(location.roomId)
158
+ .then(roomHistory => roomHistory?.getMessagesWindow(location.topicId, true))
159
+ .then(
160
+ historyWindow =>
161
+ historyWindow?.hasLatest && historyWindow.getAt(historyWindow.length - 1)
162
+ );
163
+
164
+ if (!message) {
165
+ const result = await this.tracker.client.send('GetMessages', {
166
+ location,
167
+ limit: 1,
168
+ });
169
+ message = result.data?.messages[0];
170
+ }
171
+
172
+ return message || null;
173
+ }
174
+
146
175
  private createHistoryForNewRoom(room: Room): void {
147
176
  this.roomHistories.set([room.id, new RoomMessagesHistory(room, this.tracker)]);
148
177
  }
@@ -24,9 +24,11 @@ export class RoomMessagesHistory {
24
24
 
25
25
  /**
26
26
  * Returns a history window object for the given topic ID, allowing you to view message history.
27
+ * @param topicId
28
+ * @param peek If true, do not create a cache for this topic and do not allow it to collect new messages.
27
29
  */
28
- public async getMessagesWindow(topicId: string): Promise<TopicHistoryWindow | undefined> {
29
- if (!this.historyWindows.has(topicId)) {
30
+ public async getMessagesWindow(topicId: string, peek: boolean = false): Promise<TopicHistoryWindow | undefined> {
31
+ if (!this.historyWindows.has(topicId) && !peek) {
30
32
  const topic = (await this.tracker.rooms.getTopics(this.room.id, [topicId])).get(topicId);
31
33
 
32
34
  if (topic) {
@@ -1,5 +1,6 @@
1
1
  import {IndexedCollection, ObservableIndexedObjectCollection} from "../IndexedObjectCollection";
2
2
  import {
3
+ Message,
3
4
  MessagesRedacted,
4
5
  NewMessage,
5
6
  NewTopic,
@@ -368,12 +369,14 @@ export class RoomsManager {
368
369
  }
369
370
  }
370
371
 
371
- private handleMessagesRedacted(ev: MessagesRedacted): void {
372
- // Remove redacted messages from topic.lastMessage
372
+ private async handleMessagesRedacted(ev: MessagesRedacted): Promise<void> {
373
+ // Remove redacted messages from topic and update metadata
373
374
  const topics = this.topics.get(ev.location.roomId);
374
- const updatedTopics: Topic[] = topics.items
375
- .filter(topic => topic.lastMessage?.id && ev.ids.includes(topic.lastMessage.id))
376
- .map(topic => ({ ...topic, lastMessage: null }));
377
- topics.set(...updatedTopics);
375
+ const topic = topics?.get(ev.location.topicId);
376
+ if (topic) {
377
+ const messageCount = Math.max(topic.messageCount - ev.ids.length, 0);
378
+ const lastMessage: Message = messageCount > 0 ? await this.messages._resolveLastMessage(ev.location) : null;
379
+ topics.set({ ...topic, messageCount, lastMessage } as Topic);
380
+ }
378
381
  }
379
382
  }