wechaty-redux
Version:
Wechaty Redux Plugin Powered By Ducks
101 lines • 4.51 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.skipSelfMessage$ = exports.isWechaty = exports.isTextMessage = exports.toContact$ = exports.toMessage$ = void 0;
/**
* Wechaty Open Source Software - https://github.com/wechaty
*
* @copyright 2016 Huan LI (李卓桓) <https://github.com/huan>, and
* Wechaty Contributors <https://github.com/wechaty>.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
const PUPPET = __importStar(require("wechaty-puppet"));
const rxjs_1 = require("rxjs");
const operators_1 = require("rxjs/operators");
/**
* TODO: use `dependencies` injection for Ducks dependencies, Huan(202111)
*/
const registry_js_1 = require("../registry/registry.js");
const throwUndefined = (value) => {
if (value)
return value;
throw new Error('throwUndefined() value is undefined');
};
const skipUndefined$ = (value) => {
if (value)
return (0, rxjs_1.of)(value);
return rxjs_1.EMPTY;
};
void skipUndefined$;
const extendPayloadWithPuppetId = (puppetId) => (payload) => ({
...payload,
puppetId,
});
/**
* Example: `pipe(mergeMap(toMessage$))`
*/
const toMessage$ = (action) => (0, rxjs_1.from)((0, registry_js_1.getPuppet)(action.meta.puppetId)
?.messagePayload(action.payload.messageId) || rxjs_1.EMPTY).pipe((0, operators_1.map)(throwUndefined), (0, operators_1.map)(extendPayloadWithPuppetId(action.meta.puppetId)));
exports.toMessage$ = toMessage$;
const toContact$ = (action) => (0, rxjs_1.from)((0, registry_js_1.getPuppet)(action.meta.puppetId)
?.contactPayload(action.payload.contactId) || rxjs_1.EMPTY).pipe((0, operators_1.map)(throwUndefined), (0, operators_1.map)(extendPayloadWithPuppetId(action.meta.puppetId)));
exports.toContact$ = toContact$;
const isTextMessage = (text) => (message) => (message.type === PUPPET.types.Message.Text) && (text
? text === message.text
: true);
exports.isTextMessage = isTextMessage;
const isWechaty = (puppetId) => (action) => action.meta.puppetId === puppetId;
exports.isWechaty = isWechaty;
const isSelfMessage = (puppet) => (message) =>
/**
* TODO: remove deprecated `fromId` in v2.0
*/
puppet.isLoggedIn && (message.talkerId || message.fromId)
? (message.talkerId || message.fromId) === puppet.currentUserId
: false;
void isSelfMessage;
const isNotSelfMessage = (puppet) => (message) => puppet.isLoggedIn
/**
* TODO: remove deprecated `fromId` in v2.0
*/
? (message.talkerId || message.fromId) !== puppet.currentUserId
: true;
const skipSelfMessage$ = (action) => (0, rxjs_1.of)((0, registry_js_1.getPuppet)(action.meta.puppetId)).pipe((0, rxjs_1.mergeMap)(puppet => puppet
? (0, rxjs_1.from)(puppet.messagePayload(action.payload.messageId)).pipe((0, operators_1.filter)(isNotSelfMessage(puppet)), (0, operators_1.mapTo)(action))
: rxjs_1.EMPTY));
exports.skipSelfMessage$ = skipSelfMessage$;
//# sourceMappingURL=utils.js.map