@streamlayer/feature-gamification 1.3.3 → 1.4.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.
@@ -1,6 +1,5 @@
1
- import { createMapStore } from '@streamlayer/sdk-web-interfaces';
1
+ import { createMapStore, StreamLayerContext } from '@streamlayer/sdk-web-interfaces';
2
2
  import { PromotionOptions } from '@streamlayer/sdk-web-types';
3
- import { type Transport } from '@streamlayer/sdk-web-api';
4
3
  import { type GamificationBackground } from '../background';
5
4
  import { getPromotionDetail } from '../queries';
6
5
  type AdvertisementData = {
@@ -30,7 +29,7 @@ export type Advertisement = {
30
29
  * - we subscribe to $feedList, and show last advertisement from list
31
30
  * - we subscribe to $feedSubscription, and show advertisement on activate
32
31
  */
33
- export declare const advertisement: ($slStreamId: GamificationBackground["slStreamId"], $feedSubscription: GamificationBackground["feedSubscription"], transport: Transport) => {
32
+ export declare const advertisement: ($slStreamId: GamificationBackground["slStreamId"], $feedSubscription: GamificationBackground["feedSubscription"], instance: StreamLayerContext) => {
34
33
  hide: (notificationId: string) => void;
35
34
  show: (advertisementId: string, data?: Awaited<ReturnType<typeof getPromotionDetail>>) => void;
36
35
  $list: import("nanostores").WritableAtom<Map<string, Advertisement> | undefined>;
@@ -17,7 +17,8 @@ import { AdvertisementsQueue } from './queue';
17
17
  * - we subscribe to $feedList, and show last advertisement from list
18
18
  * - we subscribe to $feedSubscription, and show advertisement on activate
19
19
  */
20
- export const advertisement = ($slStreamId, $feedSubscription, transport) => {
20
+ export const advertisement = ($slStreamId, $feedSubscription, instance) => {
21
+ const transport = instance.transport;
21
22
  const logger = createLogger('advertisement_queue');
22
23
  const queue = new AdvertisementsQueue({ concurrency: 1, animationDelay: 1000 });
23
24
  const storage = new AdvertisementStorage();
@@ -65,6 +66,10 @@ export const advertisement = ($slStreamId, $feedSubscription, transport) => {
65
66
  const hide = (notificationId) => {
66
67
  queue.closeAdvertisement(notificationId);
67
68
  markAsViewed(notificationId);
69
+ instance.sdk.onAdvertisementActivate({
70
+ stage: 'deactivate',
71
+ id: notificationId,
72
+ });
68
73
  };
69
74
  const getActiveAdvertisement = (persistent) => {
70
75
  const advertisements = queue.advertisementList.get();
@@ -89,6 +94,49 @@ export const advertisement = ($slStreamId, $feedSubscription, transport) => {
89
94
  });
90
95
  return advertisement;
91
96
  };
97
+ $advertisementList.subscribe((list) => {
98
+ if (list.data) {
99
+ const last = list.data[list.data.length - 1];
100
+ if (last) {
101
+ instance.sdk.onAdvertisementActivate({
102
+ stage: 'activate',
103
+ id: last.id,
104
+ isShowed: !!storage.isShowed(last.id),
105
+ });
106
+ }
107
+ }
108
+ });
109
+ $feedSubscription.addListener('promotion cb', (response) => {
110
+ const feedItem = response.data?.attributes?.feedItem?.attributes?.attributes?.case === 'promotion'
111
+ ? response.data.attributes.feedItem.attributes
112
+ : undefined;
113
+ const promotionItem = response.data?.attributes?.question?.options?.options.case === 'promotion'
114
+ ? response.data.attributes.question.options.options.value
115
+ : undefined;
116
+ if (feedItem === undefined || promotionItem === undefined) {
117
+ logger.debug('not promotion');
118
+ return;
119
+ }
120
+ if (feedItem.status === QuestionStatus.RESOLVED) {
121
+ instance.sdk.onAdvertisementActivate({
122
+ stage: 'deactivate',
123
+ id: feedItem.id,
124
+ });
125
+ logger.debug({ feedItem }, 'resolved: %o');
126
+ return;
127
+ }
128
+ if (feedItem.status === QuestionStatus.ACTIVE) {
129
+ logger.debug({ feedItem }, 'active: %o');
130
+ instance.sdk.onAdvertisementActivate({
131
+ stage: 'activate',
132
+ id: feedItem.id,
133
+ isShowed: !!storage.isShowed(feedItem.id),
134
+ });
135
+ return;
136
+ }
137
+ logger.debug({ feedItem }, 'skip: %o');
138
+ return;
139
+ });
92
140
  onMount(queue.advertisementList, () => {
93
141
  $advertisementList.subscribe((list) => {
94
142
  if (list.data) {
package/lib/background.js CHANGED
@@ -120,7 +120,7 @@ export class GamificationBackground {
120
120
  }
121
121
  };
122
122
  });
123
- this.advertisement = advertisement(this.slStreamId, this.feedSubscription, instance.transport);
123
+ this.advertisement = advertisement(this.slStreamId, this.feedSubscription, instance);
124
124
  }
125
125
  /**
126
126
  * Get id for notifications and link with current session
@@ -66,6 +66,7 @@ export declare class Gamification extends AbstractFeature<'games', PlainMessage<
66
66
  openQuestion: (questionId?: string, question?: FeedItem & {
67
67
  openedFrom?: "list" | "notification";
68
68
  }) => void | (() => void);
69
+ getFeedItem: (id: string) => Promise<import("@streamlayer/sl-eslib/interactive/feed/interactive.feed_pb").FeedItem | undefined>;
69
70
  isOpenedQuestion: (questionId: string) => string | undefined;
70
71
  closeQuestion: (questionId?: string) => void;
71
72
  openUser: (friendId: string) => Promise<void>;
@@ -397,6 +397,9 @@ export class Gamification extends AbstractFeature {
397
397
  });
398
398
  return this.background.openQuestion(questionId, question);
399
399
  };
400
+ getFeedItem = (id) => {
401
+ return queries.getFeedItem(id, this.transport);
402
+ };
400
403
  isOpenedQuestion = (questionId) => {
401
404
  return this.notifications.isViewed(questionId);
402
405
  };
@@ -4,6 +4,7 @@ import { ReadableAtom } from 'nanostores';
4
4
  import type { SubscriptionRequest, SubscriptionResponse, VotingSubscriptionRequest, VotingSubscriptionResponse, QuestionSubscriptionRequest, QuestionSubscriptionResponse } from '@streamlayer/sl-eslib/interactive/feed/interactive.feed_pb';
5
5
  import { InteractiveAllowed } from '../background';
6
6
  export declare const $activeQuestion: (slStreamId: ReadableAtom<string | undefined>, transport: Transport) => import("@nanostores/query").FetcherStore<import("@streamlayer/sl-eslib/interactive/feed/interactive.feed_pb").FeedQuestion | undefined, any>;
7
+ export declare const getFeedItem: (questionId: string, transport: Transport) => Promise<import("@streamlayer/sl-eslib/interactive/feed/interactive.feed_pb").FeedItem | undefined>;
7
8
  export declare const feedSubscription: ($slStreamId: ReadableAtom<string | undefined>, transport: Transport) => import("packages/sdk-web-api/lib/grpc/subscription").ServerStreamSubscription<import("@bufbuild/protobuf").ServiceType, import("@bufbuild/protobuf").Message<import("@bufbuild/protobuf").AnyMessage>, import("@bufbuild/protobuf").Message<import("@bufbuild/protobuf").AnyMessage>, never, never> | import("packages/sdk-web-api/lib/grpc/subscription").ServerStreamSubscription<{
8
9
  readonly typeName: "streamlayer.interactive.feed.Feed";
9
10
  readonly methods: {
@@ -103,6 +104,12 @@ export declare const feedSubscription: ($slStreamId: ReadableAtom<string | undef
103
104
  readonly O: typeof import("@streamlayer/sl-eslib/interactive/feed/interactive.feed_pb").FeedListResponse;
104
105
  readonly kind: import("@bufbuild/protobuf").MethodKind.Unary;
105
106
  };
107
+ readonly item: {
108
+ readonly name: "Item";
109
+ readonly I: typeof import("@streamlayer/sl-eslib/interactive/feed/interactive.feed_pb").FeedItemRequest;
110
+ readonly O: typeof import("@streamlayer/sl-eslib/interactive/feed/interactive.feed_pb").FeedItemResponse;
111
+ readonly kind: import("@bufbuild/protobuf").MethodKind.Unary;
112
+ };
106
113
  readonly feedSubscription: {
107
114
  readonly name: "FeedSubscription";
108
115
  readonly I: typeof import("@streamlayer/sl-eslib/interactive/feed/interactive.feed_pb").FeedSubscriptionRequest;
@@ -213,6 +220,12 @@ export declare const votingSubscription: (params: {
213
220
  readonly O: typeof import("@streamlayer/sl-eslib/interactive/feed/interactive.feed_pb").FeedListResponse;
214
221
  readonly kind: import("@bufbuild/protobuf").MethodKind.Unary;
215
222
  };
223
+ readonly item: {
224
+ readonly name: "Item";
225
+ readonly I: typeof import("@streamlayer/sl-eslib/interactive/feed/interactive.feed_pb").FeedItemRequest;
226
+ readonly O: typeof import("@streamlayer/sl-eslib/interactive/feed/interactive.feed_pb").FeedItemResponse;
227
+ readonly kind: import("@bufbuild/protobuf").MethodKind.Unary;
228
+ };
216
229
  readonly feedSubscription: {
217
230
  readonly name: "FeedSubscription";
218
231
  readonly I: typeof import("@streamlayer/sl-eslib/interactive/feed/interactive.feed_pb").FeedSubscriptionRequest;
@@ -320,6 +333,12 @@ export declare const questionSubscription: (questionId: string, transport: Trans
320
333
  readonly O: typeof import("@streamlayer/sl-eslib/interactive/feed/interactive.feed_pb").FeedListResponse;
321
334
  readonly kind: import("@bufbuild/protobuf").MethodKind.Unary;
322
335
  };
336
+ readonly item: {
337
+ readonly name: "Item";
338
+ readonly I: typeof import("@streamlayer/sl-eslib/interactive/feed/interactive.feed_pb").FeedItemRequest;
339
+ readonly O: typeof import("@streamlayer/sl-eslib/interactive/feed/interactive.feed_pb").FeedItemResponse;
340
+ readonly kind: import("@bufbuild/protobuf").MethodKind.Unary;
341
+ };
323
342
  readonly feedSubscription: {
324
343
  readonly name: "FeedSubscription";
325
344
  readonly I: typeof import("@streamlayer/sl-eslib/interactive/feed/interactive.feed_pb").FeedSubscriptionRequest;
@@ -20,6 +20,13 @@ export const $activeQuestion = (slStreamId, transport) => {
20
20
  refetchInterval: 0,
21
21
  });
22
22
  };
23
+ export const getFeedItem = async (questionId, transport) => {
24
+ const { client } = transport.createPromiseClient(Feed, { method: 'item', params: [questionId] });
25
+ const res = await client.item({
26
+ id: questionId,
27
+ });
28
+ return res.data;
29
+ };
23
30
  export const feedSubscription = ($slStreamId, transport) => {
24
31
  const { client } = transport.createStreamClient(Feed);
25
32
  const params = atom({ eventId: $slStreamId.get() || '', feedId: '' });
package/package.json CHANGED
@@ -1,18 +1,18 @@
1
1
  {
2
2
  "name": "@streamlayer/feature-gamification",
3
- "version": "1.3.3",
3
+ "version": "1.4.0",
4
4
  "peerDependencies": {
5
5
  "@bufbuild/protobuf": "^1.10.0",
6
6
  "@fastify/deepmerge": "^2.0.0",
7
- "@streamlayer/sl-eslib": "^5.117.0",
7
+ "@streamlayer/sl-eslib": "^5.122.0",
8
8
  "nanostores": "^0.10.3",
9
- "@streamlayer/sdk-web-api": "^1.2.3",
10
- "@streamlayer/sdk-web-core": "^1.1.3",
11
- "@streamlayer/sdk-web-logger": "^1.0.8",
12
- "@streamlayer/sdk-web-interfaces": "^1.1.3",
13
- "@streamlayer/sdk-web-notifications": "^1.1.3",
14
- "@streamlayer/sdk-web-storage": "^1.0.8",
15
- "@streamlayer/sdk-web-types": "^1.2.3"
9
+ "@streamlayer/sdk-web-api": "^1.3.0",
10
+ "@streamlayer/sdk-web-core": "^1.2.0",
11
+ "@streamlayer/sdk-web-interfaces": "^1.1.4",
12
+ "@streamlayer/sdk-web-logger": "^1.0.9",
13
+ "@streamlayer/sdk-web-notifications": "^1.1.4",
14
+ "@streamlayer/sdk-web-storage": "^1.0.9",
15
+ "@streamlayer/sdk-web-types": "^1.3.0"
16
16
  },
17
17
  "devDependencies": {
18
18
  "tslib": "^2.7.0"