spectrum-ts 0.8.0 → 0.9.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.
@@ -2,7 +2,7 @@ import {
2
2
  bufferToStream,
3
3
  readSchema,
4
4
  streamSchema
5
- } from "./chunk-ZNUORCLB.js";
5
+ } from "./chunk-CZIWNTXP.js";
6
6
 
7
7
  // src/content/richlink.ts
8
8
  import z from "zod";
@@ -553,6 +553,19 @@ function custom(raw) {
553
553
  };
554
554
  }
555
555
 
556
+ // src/content/reaction.ts
557
+ import z5 from "zod";
558
+ var reactionSchema = z5.object({
559
+ type: z5.literal("reaction"),
560
+ emoji: z5.string().min(1),
561
+ target: z5.string().min(1)
562
+ });
563
+ var asReaction = (input) => reactionSchema.parse({ type: "reaction", ...input });
564
+ function reaction(emoji, target) {
565
+ const targetId = typeof target === "string" ? target : target.id;
566
+ return { build: async () => asReaction({ emoji, target: targetId }) };
567
+ }
568
+
556
569
  // src/utils/stream.ts
557
570
  import { Repeater } from "@repeaterjs/repeater";
558
571
  function stream(setup) {
@@ -688,6 +701,8 @@ export {
688
701
  contact,
689
702
  asCustom,
690
703
  custom,
704
+ asReaction,
705
+ reaction,
691
706
  stream,
692
707
  mergeStreams,
693
708
  SpectrumCloudError,
@@ -91,43 +91,71 @@ var warnUnsupported = (err, fallbackPlatform) => {
91
91
  function buildSpace(params) {
92
92
  const { spaceRef, extras, typingCtx, definition, client, config } = params;
93
93
  let space;
94
+ async function dispatchReaction(item) {
95
+ try {
96
+ if (!definition.actions.reactToMessage) {
97
+ throw UnsupportedError.action("react", definition.name);
98
+ }
99
+ await definition.actions.reactToMessage({
100
+ space: spaceRef,
101
+ messageId: item.target,
102
+ reaction: item.emoji,
103
+ client,
104
+ config
105
+ });
106
+ } catch (err) {
107
+ if (err instanceof UnsupportedError) {
108
+ warnUnsupported(err, definition.name);
109
+ return;
110
+ }
111
+ throw err;
112
+ }
113
+ }
114
+ async function dispatchSend(item) {
115
+ let sendResult;
116
+ try {
117
+ sendResult = await definition.actions.send({
118
+ ...typingCtx,
119
+ content: item
120
+ });
121
+ } catch (err) {
122
+ if (err instanceof UnsupportedError) {
123
+ warnUnsupported(err, definition.name);
124
+ return;
125
+ }
126
+ throw err;
127
+ }
128
+ if (!sendResult?.id) {
129
+ throw new Error(
130
+ `Platform "${definition.name}" send did not return a message id`
131
+ );
132
+ }
133
+ return buildMessage({
134
+ id: sendResult.id,
135
+ content: item,
136
+ sender: sendResult.sender,
137
+ timestamp: sendResult.timestamp ?? /* @__PURE__ */ new Date(),
138
+ extras: {},
139
+ spaceRef,
140
+ space,
141
+ definition,
142
+ client,
143
+ config,
144
+ direction: "outbound"
145
+ });
146
+ }
94
147
  async function sendImpl(...content) {
95
148
  const resolved = await resolveContents(content);
96
149
  const results = [];
97
150
  for (const item of resolved) {
98
- let sendResult;
99
- try {
100
- sendResult = await definition.actions.send({
101
- ...typingCtx,
102
- content: item
103
- });
104
- } catch (err) {
105
- if (err instanceof UnsupportedError) {
106
- warnUnsupported(err, definition.name);
107
- continue;
108
- }
109
- throw err;
151
+ if (item.type === "reaction") {
152
+ await dispatchReaction(item);
153
+ continue;
110
154
  }
111
- if (!sendResult?.id) {
112
- throw new Error(
113
- `Platform "${definition.name}" send did not return a message id`
114
- );
155
+ const sent = await dispatchSend(item);
156
+ if (sent) {
157
+ results.push(sent);
115
158
  }
116
- results.push(
117
- buildMessage({
118
- id: sendResult.id,
119
- content: item,
120
- sender: sendResult.sender,
121
- timestamp: sendResult.timestamp ?? /* @__PURE__ */ new Date(),
122
- extras: {},
123
- spaceRef,
124
- space,
125
- definition,
126
- client,
127
- config,
128
- direction: "outbound"
129
- })
130
- );
131
159
  }
132
160
  if (content.length === 1) {
133
161
  return results[0];
@@ -169,13 +197,21 @@ function buildMessage(params) {
169
197
  );
170
198
  return;
171
199
  }
172
- await definition.actions.reactToMessage({
173
- space: spaceRef,
174
- messageId: params.id,
175
- reaction,
176
- client,
177
- config
178
- });
200
+ try {
201
+ await definition.actions.reactToMessage({
202
+ space: spaceRef,
203
+ messageId: params.id,
204
+ reaction,
205
+ client,
206
+ config
207
+ });
208
+ } catch (err) {
209
+ if (err instanceof UnsupportedError) {
210
+ warnUnsupported(err, definition.name);
211
+ return;
212
+ }
213
+ throw err;
214
+ }
179
215
  };
180
216
  async function reply(...content) {
181
217
  if (!definition.actions.replyToMessage) {