@celljs/ta-town 4.21.0 → 4.23.0

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.
Files changed (2) hide show
  1. package/package.json +4 -4
  2. package/web/index.js +173 -9
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@celljs/ta-town",
3
- "version": "4.21.0",
3
+ "version": "4.23.0",
4
4
  "description": "A pi package for Ta: a pixel town after Stanford's Smallville, where the double, the agents of ta-channels and the tasks live and go about what they are doing",
5
5
  "keywords": [
6
6
  "pi-package"
@@ -25,9 +25,9 @@
25
25
  "watch": "tsc -b -w --preserveWatchOutput"
26
26
  },
27
27
  "devDependencies": {
28
- "@celljs/ta-protocol": "4.21.0",
29
- "@celljs/ta-relay": "4.21.0",
30
- "@celljs/ta-web": "4.21.0",
28
+ "@celljs/ta-protocol": "4.23.0",
29
+ "@celljs/ta-relay": "4.23.0",
30
+ "@celljs/ta-web": "4.23.0",
31
31
  "@types/react": "^19.3.0",
32
32
  "phaser": "^4.2.1"
33
33
  },
package/web/index.js CHANGED
@@ -5038,10 +5038,19 @@ function TalkBar({
5038
5038
  }
5039
5039
 
5040
5040
  // ../ta-relay/esm/common/relay-room-chat.js
5041
+ var RELAY_CHAT_FILE_BYTES = { file: 50 * 1024 * 1024, video: 30 * 1024 * 1024, voice: 2 * 1024 * 1024 };
5042
+ var RELAY_CHAT_VOICE_MS = 60000;
5043
+ var RELAY_CHAT_VIDEO_MS = 3 * 60000;
5044
+ var RELAY_CHAT_FILES = 9;
5041
5045
  var RELAY_CHAT_KEY = "chat:";
5042
5046
  var RELAY_CHAT_PART_BYTES = 8 * 1024;
5043
5047
  var RELAY_CHAT_PARTS = 6;
5044
5048
  var RELAY_CHAT_TEXT_MAX = 2000;
5049
+ var RELAY_CHAT_IMAGES = 9;
5050
+ var RELAY_CHAT_IMAGE_BYTES = 2 * 1024 * 1024;
5051
+ var RELAY_CHAT_THUMB_CHARS = 900;
5052
+ var IMAGE_TYPES = ["image/jpeg", "image/png", "image/gif", "image/webp"];
5053
+ var RELAY_CHAT_WINDOW = 500;
5045
5054
  var TRIES = 6;
5046
5055
  var RelayRoomChatUtil;
5047
5056
  (function(RelayRoomChatUtil2) {
@@ -5076,18 +5085,99 @@ var RelayRoomChatUtil;
5076
5085
  ...to.length ? { to } : {},
5077
5086
  ...line.person === true && to.length && line.double !== true && !line.agent ? { person: true } : {},
5078
5087
  ...typeof line.re === "string" ? { re: line.re } : {},
5079
- ...line.task === true && to.length ? { task: true } : {}
5088
+ ...line.task === true && to.length ? { task: true } : {},
5089
+ ...images(line.images),
5090
+ ...files(line.files)
5080
5091
  };
5081
5092
  }
5082
5093
  RelayRoomChatUtil2.extra = extra;
5094
+ function files(value) {
5095
+ if (!Array.isArray(value)) {
5096
+ return {};
5097
+ }
5098
+ const files2 = [];
5099
+ for (const file of value.slice(0, RELAY_CHAT_FILES)) {
5100
+ const kind = file === null || file === undefined ? undefined : file.kind;
5101
+ if (!file || kind !== "file" && kind !== "voice" && kind !== "video" || typeof file.id !== "string" || !/^[a-f0-9]{24}$/.test(file.id) || typeof file.sha !== "string" || !/^[0-9a-f]{64}$/.test(file.sha) || !Number.isInteger(file.bytes) || file.bytes <= 0 || file.bytes > RELAY_CHAT_FILE_BYTES[kind]) {
5102
+ continue;
5103
+ }
5104
+ const name = [...typeof file.name === "string" ? file.name : ""].filter((char) => char.charCodeAt(0) >= 32 && char !== "/" && char !== "\\").join("").slice(0, 120) || kind;
5105
+ const type = typeof file.type === "string" && /^[\w.+-]+\/[\w.+-]+$/.test(file.type) ? file.type.slice(0, 100) : "application/octet-stream";
5106
+ const most = kind === "voice" ? RELAY_CHAT_VOICE_MS : RELAY_CHAT_VIDEO_MS;
5107
+ const ms = kind !== "file" && Number.isFinite(file.ms) ? Math.min(Math.max(Math.round(file.ms), 0), most) : undefined;
5108
+ const size = kind === "video" && Number.isInteger(file.w) && Number.isInteger(file.h) && file.w > 0 && file.h > 0;
5109
+ const thumb = kind === "video" && typeof file.thumb === "string" && file.thumb.length <= RELAY_CHAT_THUMB_CHARS && /^data:image\/(jpeg|png|webp);base64,/.test(file.thumb) ? file.thumb : undefined;
5110
+ files2.push({
5111
+ id: file.id,
5112
+ kind,
5113
+ name,
5114
+ type,
5115
+ bytes: file.bytes,
5116
+ sha: file.sha,
5117
+ ...ms !== undefined ? { ms } : {},
5118
+ ...size ? { w: file.w, h: file.h } : {},
5119
+ ...thumb ? { thumb } : {}
5120
+ });
5121
+ }
5122
+ return files2.length ? { files: files2 } : {};
5123
+ }
5124
+ RelayRoomChatUtil2.files = files;
5125
+ function images(value) {
5126
+ if (!Array.isArray(value)) {
5127
+ return {};
5128
+ }
5129
+ const images2 = [];
5130
+ for (const image of value.slice(0, RELAY_CHAT_IMAGES)) {
5131
+ if (image && typeof image.id === "string" && /^[A-Za-z0-9-]{8,64}$/.test(image.id) && typeof image.type === "string" && IMAGE_TYPES.includes(image.type) && typeof image.sha === "string" && /^[0-9a-f]{64}$/.test(image.sha) && Number.isInteger(image.w) && Number.isInteger(image.h) && Number.isInteger(image.bytes) && image.w > 0 && image.h > 0 && image.bytes > 0 && image.bytes <= RELAY_CHAT_IMAGE_BYTES) {
5132
+ const thumb = typeof image.thumb === "string" && image.thumb.length <= RELAY_CHAT_THUMB_CHARS && /^data:image\/(jpeg|png|webp);base64,/.test(image.thumb) ? image.thumb : undefined;
5133
+ const blob = image.blob;
5134
+ const ok = !!blob && typeof blob.id === "string" && /^[A-Za-z0-9_-]{16,64}$/.test(blob.id) && typeof blob.read === "string" && /^[A-Za-z0-9_-]{43}$/.test(blob.read) && typeof blob.key === "string" && /^[A-Za-z0-9_-]{43}$/.test(blob.key) && Number.isFinite(blob.until);
5135
+ images2.push({
5136
+ id: image.id,
5137
+ type: image.type,
5138
+ w: image.w,
5139
+ h: image.h,
5140
+ bytes: image.bytes,
5141
+ sha: image.sha,
5142
+ ...thumb ? { thumb } : {},
5143
+ ...ok && blob ? { blob: { id: blob.id, read: blob.read, key: blob.key, until: blob.until } } : {}
5144
+ });
5145
+ }
5146
+ }
5147
+ return images2.length ? { images: images2 } : {};
5148
+ }
5149
+ function preview(line, picture, words = { file: "[file]", voice: "[voice]", video: "[video]" }) {
5150
+ var _a;
5151
+ var _b;
5152
+ const pictures = ((_a = line.images) === null || _a === undefined ? undefined : _a.length) ? line.images.length > 1 ? `${picture}×${line.images.length}` : picture : "";
5153
+ const files2 = ((_b = line.files) !== null && _b !== undefined ? _b : []).map((file) => file.kind === "file" ? `${words.file} ${file.name}` : words[file.kind]);
5154
+ return [pictures, ...files2, line.text].filter(Boolean).join(" ");
5155
+ }
5156
+ RelayRoomChatUtil2.preview = preview;
5083
5157
  function stored(lines2) {
5084
- return lines2.map(({ from: _from, ...line }) => line);
5158
+ return lines2.map(({ from: _from, seq: _seq, pending: _pending, failed: _failed, ...line }) => line);
5085
5159
  }
5086
5160
  RelayRoomChatUtil2.stored = stored;
5161
+ function signed(member, value, seq) {
5162
+ const line = value;
5163
+ if (!line || typeof line.id !== "string" || typeof line.text !== "string" || typeof line.at !== "number") {
5164
+ return;
5165
+ }
5166
+ const name = typeof line.name === "string" ? line.name.slice(0, 64) : "";
5167
+ const text = line.text.slice(0, RELAY_CHAT_TEXT_MAX);
5168
+ return { id: line.id.slice(0, 40), from: member, name, text, at: line.at, ...extra(line), ...seq ? { seq } : {} };
5169
+ }
5170
+ RelayRoomChatUtil2.signed = signed;
5087
5171
  function bytes(lines2) {
5088
5172
  return new TextEncoder().encode(JSON.stringify(lines2)).length;
5089
5173
  }
5090
5174
  RelayRoomChatUtil2.bytes = bytes;
5175
+ function unknownMethod(error) {
5176
+ var _a;
5177
+ const found = error;
5178
+ return (found === null || found === undefined ? undefined : found.code) === -32601 || /method not found|not a function|unknown method|no such method/i.test(String((_a = found === null || found === undefined ? undefined : found.message) !== null && _a !== undefined ? _a : error));
5179
+ }
5180
+ RelayRoomChatUtil2.unknownMethod = unknownMethod;
5091
5181
  function id() {
5092
5182
  return `${Date.now().toString(36)}${Math.random().toString(36).slice(2, 8)}`;
5093
5183
  }
@@ -5101,6 +5191,8 @@ class RelayRoomChat {
5101
5191
  this.parts = new Map;
5102
5192
  this.listeners = new Set;
5103
5193
  this.all = [];
5194
+ this.olderKept = false;
5195
+ this.log = typeof rooms.readChat === "function" && typeof rooms.say === "function";
5104
5196
  }
5105
5197
  get lines() {
5106
5198
  return this.all;
@@ -5109,7 +5201,25 @@ class RelayRoomChat {
5109
5201
  this.listeners.add(listener);
5110
5202
  return () => this.listeners.delete(listener);
5111
5203
  }
5204
+ get more() {
5205
+ return this.olderKept;
5206
+ }
5112
5207
  async load() {
5208
+ if (this.log && this.rooms.readChat) {
5209
+ const read = await this.rooms.readChat(this.room, { limit: RELAY_CHAT_WINDOW }).catch((error) => {
5210
+ if (!RelayRoomChatUtil.unknownMethod(error)) {
5211
+ throw error;
5212
+ }
5213
+ this.log = false;
5214
+ return;
5215
+ });
5216
+ if (read) {
5217
+ this.olderKept = read.more;
5218
+ this.all = Array.isArray(read.lines) ? read.lines : [];
5219
+ this.tell();
5220
+ return;
5221
+ }
5222
+ }
5113
5223
  const state = await this.rooms.getState(this.room, RELAY_CHAT_KEY);
5114
5224
  this.parts.clear();
5115
5225
  for (const [key, value] of Object.entries(state)) {
@@ -5117,7 +5227,51 @@ class RelayRoomChat {
5117
5227
  }
5118
5228
  this.changed();
5119
5229
  }
5230
+ async older(limit = 100) {
5231
+ const first = this.all[0];
5232
+ if (!this.log || !this.rooms.readChat || !first || !this.olderKept) {
5233
+ return 0;
5234
+ }
5235
+ const read = await this.rooms.readChat(this.room, { before: first.id, limit });
5236
+ const known = new Set(this.all.map((line) => line.id));
5237
+ const lines = read.lines.filter((line) => !known.has(line.id));
5238
+ this.olderKept = read.more;
5239
+ this.all = [...lines, ...this.all];
5240
+ this.tell();
5241
+ return lines.length;
5242
+ }
5243
+ hear(event) {
5244
+ if (event.room !== undefined && event.room !== this.room) {
5245
+ return false;
5246
+ }
5247
+ if (event.type === "chat" && event.line) {
5248
+ this.heardLine(event.line);
5249
+ return true;
5250
+ }
5251
+ return event.type === "state" && typeof event.key === "string" && this.heard(event.key, event);
5252
+ }
5253
+ heardLine(line) {
5254
+ const index = this.all.findIndex((each) => each.id === line.id);
5255
+ if (index >= 0) {
5256
+ const next = [...this.all];
5257
+ next[index] = line;
5258
+ this.all = next;
5259
+ } else {
5260
+ const next = [...this.all, line];
5261
+ const pending = next.findIndex((each) => each.pending);
5262
+ if (pending >= 0 && !line.pending) {
5263
+ next.splice(next.length - 1, 1);
5264
+ next.splice(pending, 0, line);
5265
+ }
5266
+ this.all = next.length > RELAY_CHAT_WINDOW * 2 ? next.slice(-RELAY_CHAT_WINDOW) : next;
5267
+ this.olderKept || (this.olderKept = next.length > this.all.length);
5268
+ }
5269
+ this.tell();
5270
+ }
5120
5271
  heard(key, value) {
5272
+ if (this.log) {
5273
+ return !!RelayRoomChatUtil.parse(key);
5274
+ }
5121
5275
  if (!RelayRoomChatUtil.parse(key)) {
5122
5276
  return false;
5123
5277
  }
@@ -5130,16 +5284,23 @@ class RelayRoomChat {
5130
5284
  return true;
5131
5285
  }
5132
5286
  async say(text, self, extra = {}) {
5133
- var _a;
5134
- var _b, _c;
5287
+ var _a, _b, _c;
5288
+ var _d, _e;
5135
5289
  const said = text.trim().slice(0, RELAY_CHAT_TEXT_MAX);
5136
- if (!said) {
5290
+ if (!said && !((_a = extra.images) === null || _a === undefined ? undefined : _a.length) && !((_b = extra.files) === null || _b === undefined ? undefined : _b.length)) {
5137
5291
  return;
5138
5292
  }
5293
+ if (this.log && this.rooms.say) {
5294
+ const line = await this.rooms.say(this.room, said, RelayRoomChatUtil.extra(extra));
5295
+ if (line) {
5296
+ this.heardLine(line);
5297
+ }
5298
+ return line;
5299
+ }
5139
5300
  for (let tries = 0;tries < TRIES; tries++) {
5140
5301
  const latest = this.latest(self.member);
5141
5302
  const last = latest === null || latest === undefined ? undefined : latest.lines[latest.lines.length - 1];
5142
- const at = Math.max(Date.now(), ((_b = last === null || last === undefined ? undefined : last.at) !== null && _b !== undefined ? _b : 0) + 1);
5303
+ const at = Math.max(Date.now(), ((_d = last === null || last === undefined ? undefined : last.at) !== null && _d !== undefined ? _d : 0) + 1);
5143
5304
  const line = {
5144
5305
  id: RelayRoomChatUtil.id(),
5145
5306
  from: self.member,
@@ -5153,7 +5314,7 @@ class RelayRoomChat {
5153
5314
  const lines = fits && latest ? [...latest.lines, line] : [line];
5154
5315
  const key = RelayRoomChatUtil.key(self.member, part);
5155
5316
  const value = RelayRoomChatUtil.stored(lines);
5156
- const result = await this.rooms.setState(this.room, key, value, (_c = (_a = this.parts.get(key)) === null || _a === undefined ? undefined : _a.version) !== null && _c !== undefined ? _c : 0);
5317
+ const result = await this.rooms.setState(this.room, key, value, (_e = (_c = this.parts.get(key)) === null || _c === undefined ? undefined : _c.version) !== null && _e !== undefined ? _e : 0);
5157
5318
  if (result.ok) {
5158
5319
  this.put(key, { value, version: result.version, by: self.member });
5159
5320
  this.changed();
@@ -5194,6 +5355,9 @@ class RelayRoomChat {
5194
5355
  }
5195
5356
  changed() {
5196
5357
  this.all = [...this.parts.values()].flatMap((part) => part.lines).sort((a, b) => a.at - b.at || (a.id < b.id ? -1 : a.id > b.id ? 1 : 0));
5358
+ this.tell();
5359
+ }
5360
+ tell() {
5197
5361
  for (const listener of this.listeners) {
5198
5362
  listener();
5199
5363
  }
@@ -5253,8 +5417,8 @@ class TownRoomsWatch {
5253
5417
  heard(event) {
5254
5418
  if (event.type === "rooms") {
5255
5419
  this.read();
5256
- } else if (event.type === "state") {
5257
- this.chats.get(event.room)?.chat.heard(event.key, { value: event.value, version: event.version, by: event.by });
5420
+ } else if (event.type === "state" || event.type === "chat") {
5421
+ this.chats.get(event.room)?.chat.hear(event);
5258
5422
  }
5259
5423
  }
5260
5424
  read() {