@shaxpir/duiduidui-models 1.23.0 → 1.24.2

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;
@@ -70,6 +71,12 @@ export interface ChatStub {
70
71
  [user_id: string]: boolean;
71
72
  };
72
73
  }
74
+ export type ChatStatus = 'idle' | 'pending' | 'error';
75
+ export interface ChatError {
76
+ message: string;
77
+ code?: string;
78
+ retryable: boolean;
79
+ }
73
80
  export interface ChatPayload {
74
81
  title?: string;
75
82
  participants: SocialUser[];
@@ -77,6 +84,8 @@ export interface ChatPayload {
77
84
  last_read: {
78
85
  [user_id: string]: CompactDateTime;
79
86
  };
87
+ status: ChatStatus;
88
+ error?: ChatError;
80
89
  }
81
90
  export interface ChatBody extends ContentBody {
82
91
  meta: ContentMeta;
@@ -101,7 +110,12 @@ export declare class Chat extends SharedContent {
101
110
  get lastRead(): {
102
111
  [user_id: string]: CompactDateTime;
103
112
  };
113
+ get status(): ChatStatus;
114
+ get error(): ChatError | undefined;
104
115
  setTitle(title: string): void;
116
+ setStatus(status: ChatStatus): void;
117
+ setError(error: ChatError): void;
118
+ clearError(): void;
105
119
  addParticipant(user: SocialUser): void;
106
120
  removeParticipant(userId: ContentId): void;
107
121
  markRead(userId: ContentId): void;
@@ -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) {
@@ -36,6 +40,7 @@ class Chat extends SharedContent_1.SharedContent {
36
40
  participants,
37
41
  messages: [],
38
42
  last_read: {},
43
+ status: 'idle',
39
44
  }
40
45
  });
41
46
  }
@@ -91,6 +96,14 @@ class Chat extends SharedContent_1.SharedContent {
91
96
  this.checkDisposed('Chat.lastRead');
92
97
  return this.payload.last_read;
93
98
  }
99
+ get status() {
100
+ this.checkDisposed('Chat.status');
101
+ return this.payload.status;
102
+ }
103
+ get error() {
104
+ this.checkDisposed('Chat.error');
105
+ return this.payload.error;
106
+ }
94
107
  // ---- Setters ----
95
108
  setTitle(title) {
96
109
  this.checkDisposed('Chat.setTitle');
@@ -98,6 +111,26 @@ class Chat extends SharedContent_1.SharedContent {
98
111
  batch.setPathValue(['payload', 'title'], title);
99
112
  batch.commit();
100
113
  }
114
+ setStatus(status) {
115
+ this.checkDisposed('Chat.setStatus');
116
+ const batch = new Operation_1.BatchOperation(this);
117
+ batch.setPathValue(['payload', 'status'], status);
118
+ batch.commit();
119
+ }
120
+ setError(error) {
121
+ this.checkDisposed('Chat.setError');
122
+ const batch = new Operation_1.BatchOperation(this);
123
+ batch.setPathValue(['payload', 'status'], 'error');
124
+ batch.setPathValue(['payload', 'error'], error);
125
+ batch.commit();
126
+ }
127
+ clearError() {
128
+ this.checkDisposed('Chat.clearError');
129
+ const batch = new Operation_1.BatchOperation(this);
130
+ batch.setPathValue(['payload', 'status'], 'idle');
131
+ batch.removeValueAtPath(['payload', 'error']);
132
+ batch.commit();
133
+ }
101
134
  // ---- Participant operations ----
102
135
  addParticipant(user) {
103
136
  this.checkDisposed('Chat.addParticipant');
@@ -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.23.0",
3
+ "version": "1.24.2",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/shaxpir/duiduidui-models"