UNPKG

@sendbird/chat

Version:
2 lines (1 loc) 28.6 kB
Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./lib/__bundle-846135d7.cjs"),t=require("./lib/__bundle-a756336f.cjs"),n=require("./lib/__bundle-7c5b4d72.cjs"),s=require("./lib/__bundle-7ba98ca0.cjs"),i=require("./lib/__bundle-f1d13cda.cjs"),a=require("./lib/__bundle-a59763e3.cjs");require("./lib/__bundle-6c8aa193.cjs"),require("./lib/__bundle-dae7bdf2.cjs");class o{constructor(e){this.id=e.id,this.name=e.name,this.isDefault=e.is_default}static payloadify(t){return e.deundefined(e.undefineNullProps({id:t.id,name:t.name,is_default:t.isDefault}))}get customType(){return this.isDefault?"*":String(this.id)}}class r extends e.InstancedObject{constructor(e,{sdkState:t,cacheContext:n,channelManager:s}){super(e),this._channels=new Map,this._sdkState=t,this._cacheContext=n,this._channelManager=s}get collection(){const{nestdb:n}=this._cacheContext;return e.unless(!!n).throw(e.SendbirdError.databaseError),n.collection(t.NESTDB_FEEDCHANNEL_COLLECTION_NAME)}get localCacheEnabled(){const{localCacheEnabled:e}=this._cacheContext;return e&&!!this.collection}_serialize(e,t=0){return Object.assign(Object.assign({},e.serialize()),{lastMessageUpdatedAt:e.lastMessage?e.lastMessage.createdAt:0,syncIndex:t})}_deserialize(e){return this._channelManager.buildFeedChannelFromSerializedData(e)}get channels(){return[...this._channels.values()]}isCachedInMemory(e){return this._channels.has(e)}get(t){return e.__awaiter(this,void 0,void 0,(function*(){if(this._channels.has(t))return this._channels.get(t);if(this.localCacheEnabled){const e=yield this.collection.getByKey(t);if(e)return this._channels.set(t,this._deserialize(e)),this._channels.get(t)}}))}fetch({token:n,limit:i=t.DEFAULT_FEED_LIMIT,backward:a=!1,order:o="latest_last_message",borderlineChannelUrl:r}){return e.__awaiter(this,void 0,void 0,(function*(){if(this.localCacheEnabled){const e={where:e=>{if(n&&"latest_last_message"===o)if(!a&&e.lastMessageUpdatedAt>n||a&&e.lastMessageUpdatedAt<n)return!1;return!r||r!==e.url},index:s.getFeedChannelIndexBy(o),backward:a},t=yield this.collection.query(e),l=(yield t.fetch({limit:i})).map((e=>this._deserialize(e)));return l.forEach((e=>{this._channels.has(e.url)||this._channels.set(e.url,e)})),l}return[]}))}upsert(t){return e.__awaiter(this,void 0,void 0,(function*(){const n=[];if(t.forEach((t=>{if(this._channels.has(t.url)){const s=this._channels.get(t.url),i=e.deundefined(t);Object.assign(s,i,{_iid:this._iid}),n.push(s)}else this._channels.set(t.url,t),n.push(t)})),this.localCacheEnabled){const e=[];for(let t=0;t<n.length;t++)e.push(this._serialize(n[t],t));yield this.collection.upsertMany(e)}return n}))}remove(t){return e.__awaiter(this,void 0,void 0,(function*(){for(const e of t)this._channels.delete(e),this.localCacheEnabled&&(yield this.collection.remove(e))}))}clear(){return e.__awaiter(this,void 0,void 0,(function*(){this.clearMemoryCache(),this.localCacheEnabled&&(yield this.collection.clear())}))}clearMemoryCache(){this._channels.clear()}}class l extends e.BaseCommand{constructor({channels:e,context:t,isWebSocketEventComing:n=!1}){super(),this.channels=e,this.context=t,this.isWebSocketEventComing=n}}class d extends e.BaseCommand{constructor({channelUrls:e,context:t,isWebSocketEventComing:n=!1}){super(),this.channelUrls=e,this.context=t,this.isWebSocketEventComing=n}}class c{constructor({feedChannelCache:t,notificationMessageCache:n,dispatcher:s}){this._observers=new Map,s.on((s=>e.__awaiter(this,void 0,void 0,(function*(){if(s instanceof l){const{channels:e,context:i,isWebSocketEventComing:a}=s,o=e.filter((e=>e instanceof x)),r=yield t.upsert(o);yield Promise.all(o.map((e=>n.markAsReadByTimestamp(e.url,e.myLastRead)))),a||this._broadcastUpdateEvent(r,i)}else if(s instanceof d){const{channelUrls:i,context:a,isWebSocketEventComing:o}=s;yield t.remove(i),yield e.runOrNothing((()=>e.__awaiter(this,void 0,void 0,(function*(){for(const e of i)yield n.removeMessagesOfChannel(e)})))),o||this._broadcastRemoveEvent(i,a)}else s instanceof i.DatabaseOpenCommand&&(yield t.fetch({token:Number.MAX_SAFE_INTEGER,limit:Number.MAX_SAFE_INTEGER}))}))))}_broadcastUpdateEvent(e,t){for(const n of this._observers.values())n.onUpdate&&n.onUpdate(e,t)}_broadcastRemoveEvent(e,t){for(const n of this._observers.values())n.onRemove&&n.onRemove(e,t)}subscribe(e,t){this._observers.set(e,t)}unsubscribe(e){this._observers.delete(e)}unsubscribeAll(){this._observers.clear()}}const h=!1;class u extends e.APIRequestCommand{constructor(t){super();const{userId:n,token:s,limit:i,includeEmpty:a}=t;this.method=e.APIRequestMethod.GET,this.path=`${e.API_PATH_USERS}/${encodeURIComponent(n)}/my_group_channels`,this.params=e.deundefined({token:s,limit:i,show_empty:null!=a?a:h,show_read_receipt:!0,show_delivery_receipt:!0,show_member:!0,is_feed_channel:!0,order:"latest_last_message"})}}class _ extends e.APIResponseCommand{constructor(e,t){super(e,t),this.channels=[];const{next:n,channels:s,ts:i}=t;this.token=n,this.ts=null!=i?i:0,this.channels=(null!=s?s:[]).map((t=>(t.ts=i,new x(e,t))))}}class g extends e.APIRequestCommand{constructor({channelUrl:t,isInternalCall:n}){super(),this.method=e.APIRequestMethod.GET,this.path=`${n?e.API_PATH_GROUP_CHANNELS_INTERNAL:e.API_PATH_GROUP_CHANNELS}/${encodeURIComponent(t)}`,this.params={show_member:!0,show_read_receipt:!0,show_delivery_receipt:!0,is_feed_channel:!0}}}class f extends e.APIResponseCommand{constructor(e,t){super(e,t),this.channel=new x(e,t)}}const C={includeEmpty:!0},m=t=>e.isTypeOf("boolean",t.includeEmpty);class p extends e.APIRequestCommand{constructor({userId:t,ts:n,token:s,params:i}){super();const{includeEmpty:a}=Object.assign(Object.assign({},C),i);this.method=e.APIRequestMethod.GET,this.path=`${e.API_PATH_USERS}/${encodeURIComponent(t)}/my_group_channels/changelogs`,this.params=e.deundefined(e.undefineNullProps({show_delivery_receipt:!0,show_member:!0,show_read_receipt:!0,is_feed_channel:!0,show_empty:a,change_ts:n||null,token:s}))}}class v extends e.APIResponseCommand{constructor(e,t){super(e,t),this.updatedChannels=t.updated.map((t=>new x(e,t))),this.deletedChannelUrls=t.deleted,this.hasMore=t.has_more,this.token=t.next}}class E extends e.APIRequestCommand{constructor(){super(),this.method=e.APIRequestMethod.GET,this.path=`${e.API_PATH_NOTIFICATIONS}/settings`}}class y extends e.APIResponseCommand{constructor(e,t){super(e,t),this.jsonString=JSON.stringify(t)}}const b=20;class M extends e.APIRequestCommand{constructor(t){const{reverse:n=!1,keys:s,limit:i=b,token:a}=t;super(),this.method=e.APIRequestMethod.GET,this.path=`${e.API_PATH_NOTIFICATIONS}/templates`,this.params=e.deundefined({token:a,keys:s,limit:i,reverse:n,order:"updated_at",show_ui_template:!0,show_color_variables:!0})}}class w extends e.APIResponseCommand{constructor(t,n){super(t,n);const{next:s,has_more:i=!1}=n,a=e.__rest(n,["next","has_more"]);this.nextToken=s,this.hasMore=i,this.notificationTemplateList={jsonString:JSON.stringify(a)}}}const T={reverse:!1,keys:void 0,limit:20};class S extends e.APIRequestCommand{constructor(t){const{key:n}=t;super(),this.method=e.APIRequestMethod.GET,this.path=`${e.API_PATH_NOTIFICATIONS}/templates/${n}`,this.params=e.deundefined({key:n})}}class I extends e.APIResponseCommand{constructor(e,t){super(e,t),this.jsonString=JSON.stringify(t)}}class N extends e.WebSocketEventCommand{constructor(n,s,i){var a,o,r;super(n,"ADMM",i),this.message=new t.NotificationMessage(n,i);const{sdkState:l}=e.Vault.of(n);this.isMentioned=e.checkIfMentioned(this.message.mentionType,null!==(r=null!==(a=this.message.mentionedUserIds)&&void 0!==a?a:null===(o=this.message.mentionedUsers)||void 0===o?void 0:o.map((e=>e.userId)))&&void 0!==r?r:[],l.userId)}}class A extends e.APIRequestCommand{constructor({userId:t}){super(),this.method=e.APIRequestMethod.GET,this.path=`${e.API_PATH_NOTIFICATIONS_USERS}/${encodeURIComponent(t)}/unread_message_count`}}const O={};class k extends a.BaseChannelManager{get _notificationMessageCache(){return t.NotificationMessageCache.of(this._iid)}constructor(n,s){super(n,Object.assign(Object.assign({},s),{channelType:e.ChannelType.FEED})),this._feedChannelHandlers=new Map,this._feedChannelCache=new r(this._iid,{sdkState:this._sdkState,cacheContext:this._cacheContext,channelManager:this}),this._feedChannelBroadcast=new c({feedChannelCache:this._feedChannelCache,dispatcher:this._dispatcher,notificationMessageCache:t.NotificationMessageCache.of(n)}),this._dispatcher.on((t=>{t instanceof e.WebSocketEventCommand&&this._handleEvent(t).catch((t=>{if(e.isThrowingOutside(t))throw t}))})),O[n]||(O[n]=this)}static of(t){return O[t]||(O[t]=new k(t,e.Vault.of(t))),O[t]}static clear(e){O[e]&&delete O[e]}get handlers(){return[...this._feedChannelHandlers.values()]}buildFeedChannelFromSerializedData(t){const n=e.deserialize(t);return new x(this._iid,x.payloadify(n))}addHandler(e,t){this._feedChannelHandlers.set(e,t)}removeHandler(e){this._feedChannelHandlers.delete(e)}clearHandler(){this._feedChannelHandlers.clear()}getMyFeedChannels(t,n,s){return e.__awaiter(this,void 0,void 0,(function*(){const e=new u(Object.assign(Object.assign({},n),{userId:this._sdkState.userId,token:t,limit:s})),i=yield this._requestQueue.send(e),{channels:a,token:o}=i.as(_);return{channels:a,token:o}}))}getChannel(t,n=!1){return e.__awaiter(this,void 0,void 0,(function*(){e.unless(e.isTypeOf("string",t)).throw(e.SendbirdError.invalidParameters);try{const e=yield this.getChannelFromCache(t);if(e)return e}catch(e){}return yield this.getChannelWithoutCache(t,n)}))}getChannelFromCache(t){var n;return e.__awaiter(this,void 0,void 0,(function*(){return e.unless(e.isTypeOf("string",t)).throw(e.SendbirdError.invalidParameters),null!==(n=yield this._feedChannelCache.get(t))&&void 0!==n?n:null}))}getChannelWithoutCache(t,n=!1,s=!0){return e.__awaiter(this,void 0,void 0,(function*(){e.unless(e.isTypeOf("string",t)).throw(e.SendbirdError.invalidParameters);const{requestDeduplicator:i}=e.Vault.of(this._iid),a=new g({channelUrl:t,isInternalCall:n}),o=yield i.requestGetChannel(a.path,(()=>this._requestQueue.send(a)),a.params,s),{channel:r}=o.as(f),[l]=yield this.upsertChannelsToCache([r]);return l}))}getMyFeedChannelChangeLogs(t,n,s=e.CollectionEventSource.REQUEST_CHANNEL_CHANGELOGS){return e.__awaiter(this,void 0,void 0,(function*(){const i=Object.assign(Object.assign({},C),n);e.unless((e.isTypeOf("string",t)||e.isTypeOf("number",t))&&m(i)).throw(e.SendbirdError.invalidParameters);const a=new p(e.undefineNullProps({userId:this._sdkState.userId,ts:"number"==typeof t?t:null,token:"string"==typeof t?t:null,params:i})),o=yield this._requestQueue.send(a),{updatedChannels:r,deletedChannelUrls:c,hasMore:h,token:u}=o.as(v);return r.length>0&&this._dispatcher.dispatch(new l({channels:r,context:{source:s}})),c.length>0&&this._dispatcher.dispatch(new d({channelUrls:c,context:{source:s}})),{updatedChannels:r,deletedChannelUrls:c,hasMore:h,token:u}}))}getTotalUnreadMessageCount(t){return e.__awaiter(this,void 0,void 0,(function*(){const s=Object.assign(Object.assign({},n.TotalUnreadMessageCountParamsDefault),t);e.unless(n.validateTotalUnreadMessageCountParams(s)).throw(e.SendbirdError.invalidParameters);const{sdkState:i,requestQueue:a}=e.Vault.of(this._iid),o=new n.GetTotalUnreadMessageCountRequestCommand({userId:i.userId,filter:s,includeFeedChannel:!0}),r=yield a.send(o),{unreadFeedCount:l=0}=r.as(n.GetTotalUnreadMessageCountResponseCommand);return l}))}getTotalUnreadNotificationCount(){return e.__awaiter(this,void 0,void 0,(function*(){const{sdkState:t,requestQueue:s}=e.Vault.of(this._iid),i=new A({userId:t.userId}),a=yield s.send(i),{unreadFeedCount:o=0}=a.as(n.GetTotalUnreadMessageCountResponseCommand);return o}))}getGlobalNotificationChannelSetting(){return e.__awaiter(this,void 0,void 0,(function*(){const e=new E,t=yield this._requestQueue.send(e),{jsonString:n}=t.as(y);return{jsonString:n}}))}getNotificationTemplateListByToken(t,n={}){return e.__awaiter(this,void 0,void 0,(function*(){const s=Object.assign(Object.assign({},T),n);e.unless(e.isTypeOf("string",t)&&(t=>e.isTypeOf("boolean",t.reverse,!0)&&e.isArrayOf("string",t.keys,!0)&&e.isTypeOf("number",t.limit,!0))(s)).throw(e.SendbirdError.invalidParameters);const i=new M({token:t,keys:s.keys,reverse:s.reverse,limit:s.limit}),a=yield this._requestQueue.send(i),{hasMore:o,nextToken:r,notificationTemplateList:l}=a.as(w);return{hasMore:o,token:r,notificationTemplateList:l}}))}getNotificationTemplate(t){return e.__awaiter(this,void 0,void 0,(function*(){e.unless(e.isTypeOf("string",t)).throw(e.SendbirdError.invalidParameters);const n=new S({key:t}),s=yield this._requestQueue.send(n),{jsonString:i}=s.as(I);return{jsonString:i}}))}upsertChannelsToCache(t){return e.__awaiter(this,void 0,void 0,(function*(){return yield this._feedChannelCache.upsert(t)}))}removeChannelsFromCache(t){return e.__awaiter(this,void 0,void 0,(function*(){yield this._feedChannelCache.remove(t)}))}getNotificationMessageFromCache(t){return e.__awaiter(this,void 0,void 0,(function*(){return yield this._notificationMessageCache.get(t)}))}refreshChannel(t,s=!0,i=e.CollectionEventSource.REFRESH_CHANNEL){return e.__awaiter(this,void 0,void 0,(function*(){try{const{requestDeduplicator:a}=e.Vault.of(this._iid),o=new g({channelUrl:t,isInternalCall:s}),r=yield a.requestGetChannel(o.path,(()=>this._requestQueue.send(o)),o.params),{channel:c}=r.as(f);if(c.myMemberState===n.MemberState.NONE)this._dispatcher.dispatch(new d({channelUrls:[c.url],context:{source:i}}));else{const e=yield this.upsertChannelsToCache([c]);this._dispatcher.dispatch(new l({channels:e,context:{source:i}}))}}catch(n){n.code!==e.SendbirdErrorCode.NON_AUTHORIZED&&n.code!==e.SendbirdErrorCode.NOT_FOUND_IN_DATABASE||this._dispatcher.dispatch(new d({channelUrls:[t],context:{source:i}}))}}))}refreshNotificationCollections(){this._dispatcher.dispatch(new s.NotificationCollectionRefreshCommand)}subscribeChannelEvent(e,t){this._feedChannelBroadcast.subscribe(e,t)}unsubscribeChannelEvent(e){this._feedChannelBroadcast.unsubscribe(e)}_handleEvent(n){return e.__awaiter(this,void 0,void 0,(function*(){switch(n.code){case"ADMM":case"BRDM":{const s="ADMM"===n.code||"BRDM"===n.code?n.as(N):null;if(s&&s.message.channelType===this._channelType){const{message:n,isMentioned:i}=s,a=yield this.getChannel(n.channelUrl,!0);a._runIfHandleableWithGroupChannel((s=>{var o;n instanceof t.NotificationMessage&&i&&(null===(o=n.mentionedUsers)||void 0===o||o.forEach((e=>{for(const t of s.members)if(e.userId===t.userId){e.nickname=t.nickname,e.plainProfileUrl=t.plainProfileUrl,e.metaData=t.metaData;break}}))),this._dispatcher.dispatch(new l({channels:[a],context:{source:e.CollectionEventSource.EVENT_MESSAGE_RECEIVED}})),this._dispatcher.dispatch(new e.MessageUpdateEventCommand({messages:[n],source:e.CollectionEventSource.EVENT_MESSAGE_RECEIVED})),e.runAsCallback((()=>e.__awaiter(this,void 0,void 0,(function*(){for(const e of this._feedChannelHandlers.values())e.onMessageReceived&&e.onMessageReceived(a,n),i&&e.onMentionReceived&&e.onMentionReceived(a,n)}))))}))}break}case"READ":{const s="READ"===n.code?n.as(t.ReadEventCommand):null;if(s&&s.readStatus.channelType===this._channelType){const{readStatus:t}=s,n=this._feedChannelCache.isCachedInMemory(t.channelUrl),i=yield this.getChannel(t.channelUrl,!0);i._runIfHandleableWithGroupChannel((s=>{n&&s._updateUnreadMemberState(t.reader.userId,t.readAt),t.reader.userId===this._sdkState.userId&&(n?(s.unreadMessageCount>0||s.unreadMentionCount>0)&&(s._updateUnreadCount(0,0),this._dispatcher.dispatch(new l({channels:[i],context:{source:e.CollectionEventSource.EVENT_CHANNEL_READ}})),e.runAsCallback((()=>e.__awaiter(this,void 0,void 0,(function*(){for(const e of this._feedChannelHandlers.values())e.onChannelChanged&&e.onChannelChanged(i)}))))):0!==s.unreadMessageCount&&0!==s.unreadMentionCount||(this._dispatcher.dispatch(new l({channels:[i],context:{source:e.CollectionEventSource.EVENT_CHANNEL_READ}})),e.runAsCallback((()=>e.__awaiter(this,void 0,void 0,(function*(){for(const e of this._feedChannelHandlers.values())e.onChannelChanged&&e.onChannelChanged(i)}))))))}))}break}case"SYEV":{const t="SYEV"===n.code?n.as(a.ChannelEventCommand):null;if(t&&t.event.channelType===this._channelType){const{event:n}=t;switch(n.category){case a.ChannelEventCategory.CHANNEL_DELETED:{const t=yield this.getChannel(n.channelUrl,!0);this._dispatcher.dispatch(new d({channelUrls:[n.channelUrl],context:{source:e.CollectionEventSource.EVENT_CHANNEL_DELETED}})),e.runAsCallback((()=>e.__awaiter(this,void 0,void 0,(function*(){this._feedChannelHandlers.forEach((e=>{e.onChannelDeleted&&e.onChannelDeleted(t.url,t.channelType)}))}))));break}case a.ChannelEventCategory.CHANNEL_PROP_CHANGED:{const t=yield this.getChannelWithoutCache(n.channelUrl,!0,!1);this._dispatcher.dispatch(new l({channels:[t],context:{source:e.CollectionEventSource.EVENT_CHANNEL_UPDATED}})),e.runAsCallback((()=>e.__awaiter(this,void 0,void 0,(function*(){this._feedChannelHandlers.forEach((e=>{e.onChannelChanged&&e.onChannelChanged(t)}))}))));break}}}break}}}))}getMessageFromCache(t){var n;return e.__awaiter(this,void 0,void 0,(function*(){return null!==(n=yield this._notificationMessageCache.get(t))&&void 0!==n?n:null}))}getExactlyMatchingMessagesForTokenFromCache(t,n,s){return e.__awaiter(this,void 0,void 0,(function*(){return yield this._notificationMessageCache.fetch({channelUrl:t,token:n,filter:s,exactMatch:!0})}))}getMessagesFromCache(n,s,i,a,o=t.DEFAULT_NOTIFICATION_LIMIT,r=!0){return e.__awaiter(this,void 0,void 0,(function*(){return yield this._notificationMessageCache.fetch({channelUrl:n,token:s,limit:o,filter:a,backward:"next"===i,inclusive:r})}))}}class R extends n.BaseMessageCollection{keyOf(e){return e.notificationId}get changelogIncludeParams(){return{includeReactions:!1,includeThreadInfo:!0,includeMetaArray:!0,includeParentMessageInfo:!0}}constructor(t,n){super(t,Object.assign(Object.assign({},n),{channelManager:k.of(t)}));const{connectionManager:i,dispatcher:a}=e.Vault.of(this._iid);this._notificationCollectionEventContext=a.on((t=>{t instanceof s.NotificationCollectionRefreshCommand&&e.sleep(10).then((()=>{i.isConnected||i.isConnecting||this.refresh()}))}))}dispose(){super.dispose(),this._notificationCollectionEventContext.close()}setMessageCollectionHandler(e){this._setBaseMessageCollectionHandler(e)}_postprocessChannelUpdateEvent(e,n){const s=this._messages.filter((n=>n.messageStatus===t.NotificationMessageStatus.SENT&&n.createdAt<=e.myLastRead));if(s.length>0){for(const e of s)e.messageStatus=t.NotificationMessageStatus.READ;this._updateMessagesToView(s,n)}}_postprocessMessageUpdateEvent(n,s){switch(s){case e.CollectionEventSource.EVENT_CHANNEL_READ:{const e=[],i=this._messages.map((e=>e.notificationId));for(const{notificationId:s}of n){const n=i.indexOf(s);n>=0&&(this._messages[n].messageStatus=t.NotificationMessageStatus.READ,e.push(this._messages[n]))}e.length>0&&this._updateMessagesToView(e,s);break}}}_addMessagesToView(t,s){const i=this._filterUnderOffsetMessage(t),a=[],o=[];for(const t of i)if(s===e.CollectionEventSource.SYNC_MESSAGE_FILL){if(n.indexOfMessage(this._messages,t)<0){const e=n.placeOfMessage(this._messages,t);this._messages.splice(e,0,t),a.push(t)}}else{const e=n.indexOfMessage(this._messages,t);if(e<0){const e=n.placeOfMessage(this._messages,t);this._messages.splice(e,0,t),a.push(t)}else this._messages[e]=t,o.push(t)}e.shouldGiveEvent(s)&&e.runAsCallback((()=>e.__awaiter(this,void 0,void 0,(function*(){var e,t,n,i;const r={source:s};a.length>0&&(null===(t=null===(e=this._handler)||void 0===e?void 0:e.onMessagesAdded)||void 0===t||t.call(e,r,this.channel,a)),o.length>0&&(null===(i=null===(n=this._handler)||void 0===n?void 0:n.onMessagesUpdated)||void 0===i||i.call(n,r,this.channel,o))}))))}_updateMessagesToView(t,s){const i=[];for(const e of t){const t=n.indexOfMessage(this._messages,e);t>=0&&(i.push(e),this._messages[t]=e)}return e.shouldGiveEvent(s)&&e.runAsCallback((()=>e.__awaiter(this,void 0,void 0,(function*(){var e,t;const n={source:s};i.length>0&&(null===(t=null===(e=this._handler)||void 0===e?void 0:e.onMessagesUpdated)||void 0===t||t.call(e,n,this.channel,i))})))),i}refresh(){return e.__awaiter(this,void 0,void 0,(function*(){yield this._activate()}))}}class x extends t.BaseChannel{static payloadify(e,s=!1){var i;return Object.assign(Object.assign({},n.GroupChannel.payloadify(null!==(i=e._groupChannel)&&void 0!==i?i:e,s)),{is_category_filter_enabled:e.isCategoryFilterEnabled,is_template_label_enabled:e.isTemplateLabelEnabled,categories:e.notificationCategories.map((e=>o.payloadify(e))),last_message:e.lastMessage?t.NotificationMessage.payloadify(e.lastMessage):null})}constructor(s,i){var a,r;super(s,i),this.channelType=e.ChannelType.FEED,this._groupChannel=new n.GroupChannel(s,i),this.isCategoryFilterEnabled=null!==(a=i.is_category_filter_enabled)&&void 0!==a&&a,this.isTemplateLabelEnabled=null===(r=i.is_template_label_enabled)||void 0===r||r,this.notificationCategories=i.categories?i.categories.map((e=>new o(e))):[],this.lastMessage=i.last_message?t.parseMessagePayload(this._iid,Object.assign({channel_type:this.channelType},i.last_message)):null}get groupChannel(){return this._groupChannel}get url(){return this._groupChannel.url}get name(){return this._groupChannel.name}set name(e){this._groupChannel.name=e}get createdAt(){return this._groupChannel.createdAt}get members(){return this._groupChannel.members}get memberCount(){return this._groupChannel.memberCount}get myMemberState(){return this._groupChannel.myMemberState}get myLastRead(){return this._groupChannel.myLastRead}get unreadMessageCount(){return this._groupChannel.unreadMessageCount}serialize(){var t,n;return Object.assign(Object.assign({},this._groupChannel.serialize()),{notificationCategories:this.notificationCategories.map((t=>e.serialize(t))),lastMessage:null!==(n=null===(t=this.lastMessage)||void 0===t?void 0:t.serialize())&&void 0!==n?n:null})}refresh(){return e.__awaiter(this,void 0,void 0,(function*(){return yield this._groupChannel._refresh(!0),this}))}markAsRead(n){return e.__awaiter(this,void 0,void 0,(function*(){const{sdkState:s,dispatcher:i,requestQueue:a}=e.Vault.of(this._iid),o=null==n?void 0:n.filter((e=>!e||e.messageStatus===t.NotificationMessageStatus.SENT)).map((e=>e.notificationId));if(!o||o.length>0){const r=new t.ReadAPIRequestCommand({userId:s.userId,channelUrl:this.url,channelType:e.ChannelType.FEED,notificationIds:o}),d=yield a.send(r),{unreadMessageCount:c,readAt:h}=d.as(t.ReadAPIResponseCommand);if(n){if("number"==typeof c){this._groupChannel._updateUnreadCount(c,this._groupChannel.unreadMentionCount);k.of(this._iid).handlers.map((e=>{e.onChannelChanged&&e.onChannelChanged(this)}))}n.forEach((e=>{e.messageStatus=t.NotificationMessageStatus.READ})),i.dispatch(new e.MessageUpdateEventCommand({messages:n,source:e.CollectionEventSource.EVENT_CHANNEL_READ}))}else if(this._groupChannel._updateUnreadMemberState(s.userId,h),this._groupChannel.unreadMessageCount!==c){this._groupChannel._updateUnreadCount(c,0);k.of(this._iid).handlers.map((e=>{e.onChannelChanged&&e.onChannelChanged(this)}))}i.dispatch(new l({channels:[this],context:{source:e.CollectionEventSource.EVENT_CHANNEL_READ}}))}}))}markAsClicked(t){return e.__awaiter(this,void 0,void 0,(function*(){yield this.logClicked(t)}))}logClicked(t){return e.__awaiter(this,void 0,void 0,(function*(){const{statManager:n}=e.Vault.of(this._iid);if(t.length<=30)for(const s of t)s&&s.notificationId&&s.notificationData&&n.put(new e.StatLog({type:e.StatType.NOTIFICATION,ts:Date.now(),data:{action:"clicked",template_key:s.notificationData.templateKey,channel_url:this.url,message_id:s.notificationId,tags:s.notificationData.tags,source:"notification",message_ts:s.createdAt,notification_event_deadline:s.notificationEventDeadline}}))}))}logImpression(t){return e.__awaiter(this,void 0,void 0,(function*(){return this.logViewed(t)}))}logViewed(t){return e.__awaiter(this,void 0,void 0,(function*(){const{statManager:n}=e.Vault.of(this._iid);if(t.length<=30){for(const s of t)s&&s.notificationId&&s.notificationData&&n.put(new e.StatLog({type:e.StatType.NOTIFICATION,ts:Date.now(),data:{action:"viewed",template_key:s.notificationData.templateKey,channel_url:this.url,message_id:s.notificationId,tags:s.notificationData.tags,source:"notification",message_ts:s.createdAt,notification_event_deadline:s.notificationEventDeadline}}));return!0}return!1}))}logCustom(t,n){return e.__awaiter(this,void 0,void 0,(function*(){const{statManager:s}=e.Vault.of(this._iid);if(0<t.length&&t.length<=15&&n.length<=30){for(const i of n)i&&i.notificationId&&i.notificationData&&s.put(new e.StatLog({type:e.StatType.NOTIFICATION,ts:Date.now(),data:{action:"custom",topic:t,template_key:i.notificationData.templateKey,channel_url:this.url,message_id:i.notificationId,tags:i.notificationData.tags,source:"notification",message_ts:i.createdAt,notification_event_deadline:i.notificationEventDeadline}}));return!0}return!1}))}createNotificationCollection(e={}){return new R(this._iid,Object.assign({channel:this},e))}}class U{constructor(){this.onChannelChanged=e.noop,this.onChannelDeleted=e.noop,this.onMessageReceived=e.noop,this.onMentionReceived=e.noop}}class P extends U{constructor(e={}){super(),Object.keys(e).forEach((t=>{Object.prototype.hasOwnProperty.call(this,t)&&(this[t]=e[t])}))}}class D extends e.BaseListQuery{constructor(e,t){var n;super(e,t),this.includeEmpty=h,this.includeEmpty=null!==(n=t.includeEmpty)&&void 0!==n?n:h}_validate(){return super._validate()&&e.isTypeOf("boolean",this.includeEmpty)}next(){return e.__awaiter(this,void 0,void 0,(function*(){if(this._validate()){if(this._isLoading)throw e.SendbirdError.queryInProgress;if(this._hasNext){this._isLoading=!0;const t=k.of(this._iid),n=e.undefineNullProps(Object.assign({},this)),{channels:s,token:i}=yield t.getMyFeedChannels(this._token,n,this.limit);return this._token=i,this._hasNext=!!i,this._isLoading=!1,s}return[]}throw e.SendbirdError.invalidParameters}))}}class F extends e.Module{constructor(){super(...arguments),this.name="feedChannel"}init(e,t){super.init(e,t),this._manager=new k(e,t)}buildFeedChannelFromSerializedData(e){return this._manager.buildFeedChannelFromSerializedData(e)}createMyFeedChannelListQuery(e={}){return new D(this._iid,e)}addFeedChannelHandler(t,n){e.unless(e.isTypeOf("string",t)&&n instanceof P).throw(e.SendbirdError.invalidParameters),this._manager.addHandler(t,n)}removeFeedChannelHandler(t){e.unless(e.isTypeOf("string",t)).throw(e.SendbirdError.invalidParameters),this._manager.removeHandler(t)}removeAllFeedChannelHandlers(){this._manager.clearHandler()}getChannel(t){return e.__awaiter(this,void 0,void 0,(function*(){return e.unless(e.isTypeOf("string",t)).throw(e.SendbirdError.invalidParameters),this._manager.getChannel(t)}))}getMyFeedChannelChangeLogsByTimestamp(t,n={}){return e.__awaiter(this,void 0,void 0,(function*(){const s=Object.assign(Object.assign({},C),n);return e.unless(e.isTypeOf("number",t)&&m(s)).throw(e.SendbirdError.invalidParameters),yield this._manager.getMyFeedChannelChangeLogs(t,s)}))}getMyFeedChannelChangeLogsByToken(t,n={}){return e.__awaiter(this,void 0,void 0,(function*(){const s=Object.assign(Object.assign({},C),n);return e.unless(e.isTypeOf("string",t)&&m(s)).throw(e.SendbirdError.invalidParameters),yield this._manager.getMyFeedChannelChangeLogs(t,s)}))}getTotalUnreadMessageCount(t={}){return e.__awaiter(this,void 0,void 0,(function*(){return yield this.getTotalUnreadNotificationCount()}))}getTotalUnreadNotificationCount(){return e.__awaiter(this,void 0,void 0,(function*(){return yield this._manager.getTotalUnreadNotificationCount()}))}getGlobalNotificationChannelSetting(){return e.__awaiter(this,void 0,void 0,(function*(){return yield this._manager.getGlobalNotificationChannelSetting()}))}getNotificationTemplateListByToken(t,n={}){return e.__awaiter(this,void 0,void 0,(function*(){return yield this._manager.getNotificationTemplateListByToken(t,n)}))}getNotificationTemplate(t){return e.__awaiter(this,void 0,void 0,(function*(){return yield this._manager.getNotificationTemplate(t)}))}refreshNotificationCollections(){this._manager.refreshNotificationCollections()}}exports.NotificationMessage=t.NotificationMessage,Object.defineProperty(exports,"NotificationMessageStatus",{enumerable:!0,get:function(){return t.NotificationMessageStatus}}),exports.FeedChannel=x,exports.FeedChannelHandler=P,exports.FeedChannelListQuery=D,exports.FeedChannelModule=F,exports.NotificationCategory=o,exports.NotificationCollection=R;