@shaxpir/duiduidui-models 1.24.0 → 1.24.3

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.
@@ -5,6 +5,7 @@ import { ArrayView } from './ArrayView';
5
5
  import { ContentBody, ContentId, ContentMeta } from './Content';
6
6
  import { SharedContent } from './SharedContent';
7
7
  import { SocialUser } from './Social';
8
+ export declare const NEW_CHAT_SCRATCHPAD_KEY = "(new-chat)";
8
9
  export interface TextBlock {
9
10
  type: 'text';
10
11
  text: string;
@@ -62,9 +63,9 @@ export interface ChatScratchpad {
62
63
  }
63
64
  export interface ChatStub {
64
65
  id: ContentId;
65
- title?: string;
66
+ title: string | null;
66
67
  participants: SocialUser[];
67
- last_message_preview?: string;
68
+ last_message_preview: string | null;
68
69
  updated_at: CompactDateTime;
69
70
  has_unread: {
70
71
  [user_id: string]: boolean;
@@ -1,12 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Chat = void 0;
3
+ exports.Chat = exports.NEW_CHAT_SCRATCHPAD_KEY = void 0;
4
4
  const shaxpir_common_1 = require("@shaxpir/shaxpir-common");
5
5
  const repo_1 = require("../repo");
6
6
  const ArrayView_1 = require("./ArrayView");
7
7
  const ContentKind_1 = require("./ContentKind");
8
8
  const Operation_1 = require("./Operation");
9
9
  const SharedContent_1 = require("./SharedContent");
10
+ // Scratchpad key for composing the first message of a new chat.
11
+ // Uses characters outside the base62 ContentId alphabet so it can never
12
+ // collide with an actual chat ID.
13
+ exports.NEW_CHAT_SCRATCHPAD_KEY = '(new-chat)';
10
14
  // ---- Chat model ----
11
15
  class Chat extends SharedContent_1.SharedContent {
12
16
  constructor(doc, shouldAcquire, shareSync) {
@@ -58,12 +62,10 @@ class Chat extends SharedContent_1.SharedContent {
58
62
  for (const userId of userIds) {
59
63
  try {
60
64
  const workspaceId = shaxpir_common_1.CachingHasher.makeMd5Base62Hash(userId + "-" + ContentKind_1.ContentKind.WORKSPACE);
61
- const workspace = this.shareSync.load(ContentKind_1.ContentKind.WORKSPACE, workspaceId);
62
- if (workspace) {
63
- await workspace.acquire();
64
- if (workspace.exists()) {
65
- workspace.upsertChatStub(stub);
66
- }
65
+ const workspace = await this.shareSync.acquire(ContentKind_1.ContentKind.WORKSPACE, workspaceId);
66
+ if (workspace && workspace.exists()) {
67
+ // Cast needed to avoid circular dependency between Chat and Workspace
68
+ workspace.upsertChatStub(stub);
67
69
  workspace.release();
68
70
  }
69
71
  }
@@ -162,7 +164,7 @@ class Chat extends SharedContent_1.SharedContent {
162
164
  toStub() {
163
165
  this.checkDisposed('Chat.toStub');
164
166
  const msgs = this.messages.values;
165
- let preview;
167
+ let preview = null;
166
168
  if (msgs.length > 0) {
167
169
  const lastMsg = msgs[msgs.length - 1];
168
170
  const textBlock = lastMsg.content.find(b => b.type === 'text');
@@ -177,7 +179,7 @@ class Chat extends SharedContent_1.SharedContent {
177
179
  }
178
180
  return {
179
181
  id: this.id,
180
- title: this.title,
182
+ title: this.title || null,
181
183
  participants: this.participants.values,
182
184
  last_message_preview: preview,
183
185
  updated_at: this.updatedAt.utc_time,
@@ -62,6 +62,7 @@ export declare class Workspace extends Content {
62
62
  getChatScratchpad(chatId: string): ChatScratchpad;
63
63
  updateChatScratchpadText(chatId: string, text: string): void;
64
64
  updateChatScratchpadField(chatId: string, key: string, value: any): void;
65
+ private ensureScratchpadExists;
65
66
  clearChatScratchpad(chatId: string): void;
66
67
  get devices(): ArrayView<ContentId>;
67
68
  get uploadedAvatars(): ArrayView<ContentId>;
@@ -124,16 +124,25 @@ class Workspace extends Content_1.Content {
124
124
  }
125
125
  updateChatScratchpadText(chatId, text) {
126
126
  this.checkDisposed("Workspace.updateChatScratchpadText");
127
+ this.ensureScratchpadExists(chatId);
127
128
  const batch = new Operation_1.BatchOperation(this);
128
129
  batch.editPathText(['payload', 'chat_scratchpads', chatId, 'text'], text);
129
130
  batch.commit();
130
131
  }
131
132
  updateChatScratchpadField(chatId, key, value) {
132
133
  this.checkDisposed("Workspace.updateChatScratchpadField");
134
+ this.ensureScratchpadExists(chatId);
133
135
  const batch = new Operation_1.BatchOperation(this);
134
136
  batch.setPathValue(['payload', 'chat_scratchpads', chatId, key], value);
135
137
  batch.commit();
136
138
  }
139
+ ensureScratchpadExists(chatId) {
140
+ if (!this.payload.chat_scratchpads.hasOwnProperty(chatId)) {
141
+ const batch = new Operation_1.BatchOperation(this);
142
+ batch.setPathValue(['payload', 'chat_scratchpads', chatId], {});
143
+ batch.commit();
144
+ }
145
+ }
137
146
  clearChatScratchpad(chatId) {
138
147
  this.checkDisposed("Workspace.clearChatScratchpad");
139
148
  const path = ['payload', 'chat_scratchpads', chatId];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shaxpir/duiduidui-models",
3
- "version": "1.24.0",
3
+ "version": "1.24.3",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/shaxpir/duiduidui-models"