react-activity-feed
Version:
React components to create activity and notification feeds
1,399 lines (1,154 loc) • 257 kB
JavaScript
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var _objectSpread = _interopDefault(require('@babel/runtime/helpers/objectSpread'));
var _regeneratorRuntime = _interopDefault(require('@babel/runtime/regenerator'));
var _asyncToGenerator = _interopDefault(require('@babel/runtime/helpers/asyncToGenerator'));
var _classCallCheck = _interopDefault(require('@babel/runtime/helpers/classCallCheck'));
var _createClass = _interopDefault(require('@babel/runtime/helpers/createClass'));
var _possibleConstructorReturn = _interopDefault(require('@babel/runtime/helpers/possibleConstructorReturn'));
var _getPrototypeOf = _interopDefault(require('@babel/runtime/helpers/getPrototypeOf'));
var _inherits = _interopDefault(require('@babel/runtime/helpers/inherits'));
var _assertThisInitialized = _interopDefault(require('@babel/runtime/helpers/assertThisInitialized'));
var _defineProperty = _interopDefault(require('@babel/runtime/helpers/defineProperty'));
var React = require('react');
var React__default = _interopDefault(React);
var stream = _interopDefault(require('getstream'));
var StreamAnalytics = _interopDefault(require('stream-analytics'));
var _toConsumableArray = _interopDefault(require('@babel/runtime/helpers/toConsumableArray'));
var immutable = _interopDefault(require('immutable'));
var URL = _interopDefault(require('url-parse'));
var moment = _interopDefault(require('moment'));
var anchorme = _interopDefault(require('anchorme'));
var _truncate = _interopDefault(require('lodash/truncate'));
var twitter = _interopDefault(require('twitter-text'));
var _isPlainObject = _interopDefault(require('lodash/isPlainObject'));
var _isEqual = _interopDefault(require('lodash/isEqual'));
var _remove = _interopDefault(require('lodash/remove'));
var _extends = _interopDefault(require('@babel/runtime/helpers/extends'));
var reactFileUtils = require('react-file-utils');
var reactFontawesome = require('@fortawesome/react-fontawesome');
var freeRegularSvgIcons = require('@fortawesome/free-regular-svg-icons');
var Lightbox = _interopDefault(require('react-images'));
var emojiMart = require('emoji-mart');
var ReactTextareaAutocomplete = _interopDefault(require('@webscopeio/react-textarea-autocomplete'));
require('@webscopeio/react-textarea-autocomplete/style.css');
var _uniq = _interopDefault(require('lodash/uniq'));
var _debounce = _interopDefault(require('lodash/debounce'));
var _difference = _interopDefault(require('lodash/difference'));
var _includes = _interopDefault(require('lodash/includes'));
var _objectWithoutProperties = _interopDefault(require('@babel/runtime/helpers/objectWithoutProperties'));
var PropTypes = _interopDefault(require('prop-types'));
// import type { UserResponse } from 'getstream';
function humanizeTimestamp(timestamp) {
var time = moment.utc(timestamp); // parse time as UTC
var now = moment(); // Not in future humanized time
return moment.min(time, now).from(now);
}
var smartRender = function smartRender(ElementOrComponentOrLiteral, props, fallback) {
if (ElementOrComponentOrLiteral === undefined) {
ElementOrComponentOrLiteral = fallback;
}
if (React.isValidElement(ElementOrComponentOrLiteral)) {
// Flow cast through any, to make flow believe it's a React.Element
var element = ElementOrComponentOrLiteral;
return element;
} // Flow cast through any to remove React.Element after previous check
var ComponentOrLiteral = ElementOrComponentOrLiteral;
if (typeof ComponentOrLiteral === 'string' || typeof ComponentOrLiteral === 'number' || typeof ComponentOrLiteral === 'boolean' || ComponentOrLiteral == null) {
return ComponentOrLiteral;
}
return React.createElement(ComponentOrLiteral, props);
};
function userOrDefault(user) {
var actor;
var notFound = {
id: '!not-found',
created_at: '',
updated_at: '',
data: {
name: 'Unknown',
profileImage: ''
}
};
if (typeof user === 'string' || typeof user.error === 'string') {
actor = notFound;
} else {
//$FlowBug
actor = user;
}
return actor;
}
function generateRandomId() {
// prettier-ignore
return S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4();
}
function S4() {
return ((1 + Math.random()) * 0x10000 | 0).toString(16).substring(1);
}
function dataTransferItemsHaveFiles(items) {
if (!items || !items.length) {
return false;
}
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = items[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var item = _step.value;
if (item.kind === 'file' || item.type === 'text/html') {
return true;
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return != null) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return false;
}
function dataTransferItemsToFiles(_x) {
return _dataTransferItemsToFiles.apply(this, arguments);
}
function _dataTransferItemsToFiles() {
_dataTransferItemsToFiles = _asyncToGenerator(
/*#__PURE__*/
_regeneratorRuntime.mark(function _callee3(items) {
var fileLikes, blobPromises, _iteratorNormalCompletion2, _didIteratorError2, _iteratorError2, _iterator2, _step2, item, file, parser, _iteratorNormalCompletion3, _didIteratorError3, _iteratorError3, _loop, _iterator3, _step3;
return _regeneratorRuntime.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
if (!(!items || !items.length)) {
_context3.next = 2;
break;
}
return _context3.abrupt("return", []);
case 2:
fileLikes = [];
blobPromises = [];
_iteratorNormalCompletion2 = true;
_didIteratorError2 = false;
_iteratorError2 = undefined;
_context3.prev = 7;
for (_iterator2 = items[Symbol.iterator](); !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
item = _step2.value;
if (item.kind === 'file') {
file = item.getAsFile();
if (file) {
fileLikes.push(file);
}
}
} // If there are files inside the DataTransferItem prefer those
_context3.next = 15;
break;
case 11:
_context3.prev = 11;
_context3.t0 = _context3["catch"](7);
_didIteratorError2 = true;
_iteratorError2 = _context3.t0;
case 15:
_context3.prev = 15;
_context3.prev = 16;
if (!_iteratorNormalCompletion2 && _iterator2.return != null) {
_iterator2.return();
}
case 18:
_context3.prev = 18;
if (!_didIteratorError2) {
_context3.next = 21;
break;
}
throw _iteratorError2;
case 21:
return _context3.finish(18);
case 22:
return _context3.finish(15);
case 23:
if (!fileLikes.length) {
_context3.next = 25;
break;
}
return _context3.abrupt("return", fileLikes);
case 25:
// Otherwise extract images from html
parser = new DOMParser();
_iteratorNormalCompletion3 = true;
_didIteratorError3 = false;
_iteratorError3 = undefined;
_context3.prev = 29;
_loop = function _loop() {
var item = _step3.value;
if (item.type === 'text/html') {
blobPromises.push(new Promise(function (accept) {
item.getAsString(
/*#__PURE__*/
function () {
var _ref = _asyncToGenerator(
/*#__PURE__*/
_regeneratorRuntime.mark(function _callee2(s) {
var doc, imageTags, imagePromises, _iteratorNormalCompletion4, _didIteratorError4, _iteratorError4, _loop2, _iterator4, _step4, _ret;
return _regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
doc = parser.parseFromString(s, 'text/html');
imageTags = doc.getElementsByTagName('img');
imagePromises = [];
_iteratorNormalCompletion4 = true;
_didIteratorError4 = false;
_iteratorError4 = undefined;
_context2.prev = 6;
_loop2 = function _loop2() {
var tag = _step4.value;
if (!tag.src) {
return "continue";
}
imagePromises.push(_asyncToGenerator(
/*#__PURE__*/
_regeneratorRuntime.mark(function _callee() {
var res, contentType, buf, blob;
return _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.prev = 0;
_context.next = 3;
return fetch(tag.src);
case 3:
res = _context.sent;
_context.next = 9;
break;
case 6:
_context.prev = 6;
_context.t0 = _context["catch"](0);
return _context.abrupt("return");
case 9:
contentType = res.headers.get('Content-type') || 'application/octet-stream';
_context.next = 12;
return res.arrayBuffer();
case 12:
buf = _context.sent;
blob = new Blob([buf], {
type: contentType
});
fileLikes.push(blob);
case 15:
case "end":
return _context.stop();
}
}
}, _callee, this, [[0, 6]]);
}))());
};
_iterator4 = imageTags[Symbol.iterator]();
case 9:
if (_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done) {
_context2.next = 16;
break;
}
_ret = _loop2();
if (!(_ret === "continue")) {
_context2.next = 13;
break;
}
return _context2.abrupt("continue", 13);
case 13:
_iteratorNormalCompletion4 = true;
_context2.next = 9;
break;
case 16:
_context2.next = 22;
break;
case 18:
_context2.prev = 18;
_context2.t0 = _context2["catch"](6);
_didIteratorError4 = true;
_iteratorError4 = _context2.t0;
case 22:
_context2.prev = 22;
_context2.prev = 23;
if (!_iteratorNormalCompletion4 && _iterator4.return != null) {
_iterator4.return();
}
case 25:
_context2.prev = 25;
if (!_didIteratorError4) {
_context2.next = 28;
break;
}
throw _iteratorError4;
case 28:
return _context2.finish(25);
case 29:
return _context2.finish(22);
case 30:
_context2.next = 32;
return Promise.all(imagePromises);
case 32:
accept();
case 33:
case "end":
return _context2.stop();
}
}
}, _callee2, this, [[6, 18, 22, 30], [23,, 25, 29]]);
}));
return function (_x2) {
return _ref.apply(this, arguments);
};
}());
}));
}
};
for (_iterator3 = items[Symbol.iterator](); !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
_loop();
}
_context3.next = 38;
break;
case 34:
_context3.prev = 34;
_context3.t1 = _context3["catch"](29);
_didIteratorError3 = true;
_iteratorError3 = _context3.t1;
case 38:
_context3.prev = 38;
_context3.prev = 39;
if (!_iteratorNormalCompletion3 && _iterator3.return != null) {
_iterator3.return();
}
case 41:
_context3.prev = 41;
if (!_didIteratorError3) {
_context3.next = 44;
break;
}
throw _iteratorError3;
case 44:
return _context3.finish(41);
case 45:
return _context3.finish(38);
case 46:
_context3.next = 48;
return Promise.all(blobPromises);
case 48:
return _context3.abrupt("return", fileLikes);
case 49:
case "end":
return _context3.stop();
}
}
}, _callee3, this, [[7, 11, 15, 23], [16,, 18, 22], [29, 34, 38, 46], [39,, 41, 45]]);
}));
return _dataTransferItemsToFiles.apply(this, arguments);
}
function inputValueFromEvent(event) {
if (!event) {
return;
}
var target;
if (event.currentTarget) {
target = event.currentTarget;
} else {
target = event.target;
} // Trick flow into believing the target maybe has a value field
var inputTarget = target;
return inputTarget.value;
}
function sanitizeURL(url) {
if (url == null) {
return url;
}
var proto = URL(url).protocol; // allow http, https, ftp
// IMPORTANT: Don't allow data: protocol because of:
// <a href="data:text/html;base64,PHNjcmlwdD5hbGVydCgneHNzJyk7PC9zY3JpcHQ+" target="_blank">here</a>
if (proto === 'https:' || proto === 'http:' || proto === 'ftp:') {
return url;
}
return undefined;
}
var textRenderer = function textRenderer(text, parentClass, onClickMention, onClickHashtag) {
return text.split(' ').map(function (word, i) {
if (onClickMention && word.includes('@')) {
var mention = twitter.extractMentions(word);
if (!mention.length) return word;
return React.createElement(React.Fragment, {
key: "item-".concat(i)
}, !word.startsWith("@".concat(mention[0])) && word.slice(0, word.indexOf(mention[0]) - 1), React.createElement("a", {
onClick: function onClick() {
return onClickMention && onClickMention(mention[0]);
},
className: "".concat(parentClass, "__mention")
}, "@", mention[0]), !word.endsWith(mention[0]) && word.slice(word.indexOf(mention[0]) + mention[0].length));
} else if (onClickHashtag && word.includes('#')) {
var hashtag = twitter.extractHashtags(word);
if (!hashtag.length) return word;
return React.createElement(React.Fragment, {
key: "item-".concat(i)
}, !word.startsWith("#".concat(hashtag[0])) && word.slice(0, word.indexOf(hashtag[0]) - 1), React.createElement("a", {
onClick: function onClick() {
return onClickHashtag && onClickHashtag(hashtag[0]);
},
className: "".concat(parentClass, "__hashtag")
}, "#", hashtag[0]), !word.endsWith(hashtag[0]) && word.slice(word.indexOf(hashtag[0]) + hashtag[0].length));
}
if (anchorme.validate.url(word) || anchorme.validate.email(word)) {
var link = anchorme(word, {
list: true
});
if (link[0].protocol !== 'http://' && link[0].protocol !== 'https://' && link[0].protocol !== 'mailto:') {
return word;
}
var url = link[0].protocol + link[0].encoded;
var urlText = _truncate(link[0].encoded, {
length: 33
});
return React.createElement("a", {
href: url,
className: "".concat(parentClass, "__link"),
target: "blank",
rel: "noopener",
key: "item-".concat(i)
}, urlText);
}
return word;
}).reduce(function (accu, elem) {
return accu === null ? [elem] : [accu, ' ', elem];
});
};
var FeedManager =
/*#__PURE__*/
function () {
function FeedManager(props) {
var _this = this;
_classCallCheck(this, FeedManager);
_defineProperty(this, "props", void 0);
_defineProperty(this, "state", {
activityOrder: [],
activities: immutable.Map(),
activityIdToPath: {},
activityIdToPaths: {},
reactionIdToPaths: {},
reactionActivities: {},
lastResponse: null,
lastReverseResponse: null,
refreshing: false,
realtimeAdds: [],
realtimeDeletes: [],
subscription: null,
unread: 0,
unseen: 0,
numSubscribers: 0,
reactionsBeingToggled: {},
childReactionsBeingToggled: {}
});
_defineProperty(this, "registeredCallbacks", void 0);
_defineProperty(this, "setState", function (changed) {
if (typeof changed === 'function') {
changed = changed(_this.state);
}
_this.state = _objectSpread({}, _this.state, changed);
_this.triggerUpdate();
});
_defineProperty(this, "trackAnalytics", function (label, activity, track) {
var analyticsClient = _this.props.analyticsClient;
if (!track) {
return;
}
if (!analyticsClient) {
console.warn('trackAnalytics was enabled, but analytics client was not initialized. ' + 'Please set the analyticsToken prop on StreamApp');
return;
}
var feed = _this.props.client.feed(_this.props.feedGroup, _this.props.userId);
analyticsClient.trackEngagement({
label: label,
feed_id: feed.id,
content: {
foreign_id: activity.foreign_id
},
location: _this.props.analyticsLocation
});
});
_defineProperty(this, "getActivityPath", function (activity) {
var activityId;
if (typeof activity === 'string') {
activityId = activity;
} else {
activityId = activity.id;
}
var activityPath = _this.state.activityIdToPath[activityId];
for (var _len = arguments.length, rest = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
rest[_key - 1] = arguments[_key];
}
if (activityPath === undefined) {
return [activityId].concat(rest);
}
return _toConsumableArray(activityPath).concat(rest);
});
_defineProperty(this, "getActivityPaths", function (activity) {
var activityId;
if (typeof activity === 'string') {
activityId = activity;
} else {
activityId = activity.id;
}
return _this.state.activityIdToPaths[activityId];
});
_defineProperty(this, "getReactionPaths", function (reaction) {
var reactionId;
if (typeof reaction === 'string') {
reactionId = reaction;
} else {
reactionId = reaction.id;
}
return _this.state.reactionIdToPaths[reactionId];
});
_defineProperty(this, "onAddReaction",
/*#__PURE__*/
function () {
var _ref = _asyncToGenerator(
/*#__PURE__*/
_regeneratorRuntime.mark(function _callee(kind, activity, data) {
var options,
reaction,
enrichedReaction,
_args = arguments;
return _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
options = _args.length > 3 && _args[3] !== undefined ? _args[3] : {};
_context.prev = 1;
if (!_this.props.doReactionAddRequest) {
_context.next = 8;
break;
}
_context.next = 5;
return _this.props.doReactionAddRequest(kind, activity, data, options);
case 5:
reaction = _context.sent;
_context.next = 11;
break;
case 8:
_context.next = 10;
return _this.props.client.reactions.add(kind, activity, data, options);
case 10:
reaction = _context.sent;
case 11:
_context.next = 17;
break;
case 13:
_context.prev = 13;
_context.t0 = _context["catch"](1);
_this.props.errorHandler(_context.t0, 'add-reaction', {
kind: kind,
activity: activity,
feedGroup: _this.props.feedGroup,
userId: _this.props.userId
});
return _context.abrupt("return");
case 17:
_this.trackAnalytics(kind, activity, options.trackAnalytics);
enrichedReaction = immutable.fromJS(_objectSpread({}, reaction, {
user: _this.props.user.full
}));
_this.setState(function (prevState) {
var activities = prevState.activities;
var reactionIdToPaths = prevState.reactionIdToPaths;
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = _this.getActivityPaths(activity)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var path = _step.value;
_this.removeFoundReactionIdPaths(activities.getIn(path).toJS(), reactionIdToPaths, path);
activities = activities.updateIn(_toConsumableArray(path).concat(['reaction_counts', kind]), function () {
var v = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
return v + 1;
}).updateIn(_toConsumableArray(path).concat(['own_reactions', kind]), function () {
var v = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : immutable.List();
return v.unshift(enrichedReaction);
}).updateIn(_toConsumableArray(path).concat(['latest_reactions', kind]), function () {
var v = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : immutable.List();
return v.unshift(enrichedReaction);
});
_this.addFoundReactionIdPaths(activities.getIn(path).toJS(), reactionIdToPaths, path);
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return != null) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return {
activities: activities,
reactionIdToPaths: reactionIdToPaths
};
});
case 20:
case "end":
return _context.stop();
}
}
}, _callee, this, [[1, 13]]);
}));
return function (_x, _x2, _x3) {
return _ref.apply(this, arguments);
};
}());
_defineProperty(this, "onRemoveReaction",
/*#__PURE__*/
function () {
var _ref2 = _asyncToGenerator(
/*#__PURE__*/
_regeneratorRuntime.mark(function _callee2(kind, activity, id) {
var options,
_args2 = arguments;
return _regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
options = _args2.length > 3 && _args2[3] !== undefined ? _args2[3] : {};
_context2.prev = 1;
if (!_this.props.doReactionDeleteRequest) {
_context2.next = 7;
break;
}
_context2.next = 5;
return _this.props.doReactionDeleteRequest(id);
case 5:
_context2.next = 9;
break;
case 7:
_context2.next = 9;
return _this.props.client.reactions.delete(id);
case 9:
_context2.next = 15;
break;
case 11:
_context2.prev = 11;
_context2.t0 = _context2["catch"](1);
_this.props.errorHandler(_context2.t0, 'delete-reaction', {
kind: kind,
activity: activity,
feedGroup: _this.props.feedGroup,
userId: _this.props.userId
});
return _context2.abrupt("return");
case 15:
_this.trackAnalytics('un' + kind, activity, options.trackAnalytics);
_this.setState(function (prevState) {
var activities = prevState.activities;
var reactionIdToPaths = prevState.reactionIdToPaths;
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
try {
for (var _iterator2 = _this.getActivityPaths(activity)[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
var path = _step2.value;
_this.removeFoundReactionIdPaths(activities.getIn(path).toJS(), reactionIdToPaths, path);
activities = activities.updateIn(_toConsumableArray(path).concat(['reaction_counts', kind]), function () {
var v = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
return v - 1;
}).updateIn(_toConsumableArray(path).concat(['own_reactions', kind]), function () {
var v = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : immutable.List();
return v.remove(v.findIndex(function (r) {
return r.get('id') === id;
}));
}).updateIn(_toConsumableArray(path).concat(['latest_reactions', kind]), function () {
var v = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : immutable.List();
return v.remove(v.findIndex(function (r) {
return r.get('id') === id;
}));
});
_this.addFoundReactionIdPaths(activities.getIn(path).toJS(), reactionIdToPaths, path);
}
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2.return != null) {
_iterator2.return();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
}
}
return {
activities: activities,
reactionIdToPaths: reactionIdToPaths
};
});
if (_this.state.reactionActivities[id]) {
_this._removeActivityFromState(_this.state.reactionActivities[id]);
}
case 18:
case "end":
return _context2.stop();
}
}
}, _callee2, this, [[1, 11]]);
}));
return function (_x4, _x5, _x6) {
return _ref2.apply(this, arguments);
};
}());
_defineProperty(this, "onToggleReaction",
/*#__PURE__*/
function () {
var _ref3 = _asyncToGenerator(
/*#__PURE__*/
_regeneratorRuntime.mark(function _callee3(kind, activity, data) {
var options,
togglingReactions,
currentReactions,
last,
_args3 = arguments;
return _regeneratorRuntime.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
options = _args3.length > 3 && _args3[3] !== undefined ? _args3[3] : {};
togglingReactions = _this.state.reactionsBeingToggled[kind] || {};
if (!togglingReactions[activity.id]) {
_context3.next = 4;
break;
}
return _context3.abrupt("return");
case 4:
togglingReactions[activity.id] = true;
_this.state.reactionsBeingToggled[kind] = togglingReactions;
currentReactions = _this.state.activities.getIn(_toConsumableArray(_this.getActivityPaths(activity)[0]).concat(['own_reactions', kind]), immutable.List());
last = currentReactions.last();
if (!last) {
_context3.next = 13;
break;
}
_context3.next = 11;
return _this.onRemoveReaction(kind, activity, last.get('id'), options);
case 11:
_context3.next = 15;
break;
case 13:
_context3.next = 15;
return _this.onAddReaction(kind, activity, data, options);
case 15:
delete togglingReactions[activity.id];
case 16:
case "end":
return _context3.stop();
}
}
}, _callee3, this);
}));
return function (_x7, _x8, _x9) {
return _ref3.apply(this, arguments);
};
}());
_defineProperty(this, "onAddChildReaction",
/*#__PURE__*/
function () {
var _ref4 = _asyncToGenerator(
/*#__PURE__*/
_regeneratorRuntime.mark(function _callee4(kind, reaction, data) {
var options,
childReaction,
enrichedReaction,
_args4 = arguments;
return _regeneratorRuntime.wrap(function _callee4$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
options = _args4.length > 3 && _args4[3] !== undefined ? _args4[3] : {};
_context4.prev = 1;
if (!_this.props.doChildReactionAddRequest) {
_context4.next = 8;
break;
}
_context4.next = 5;
return _this.props.doChildReactionAddRequest(kind, reaction, data, options);
case 5:
childReaction = _context4.sent;
_context4.next = 11;
break;
case 8:
_context4.next = 10;
return _this.props.client.reactions.addChild(kind, reaction, data, options);
case 10:
childReaction = _context4.sent;
case 11:
_context4.next = 17;
break;
case 13:
_context4.prev = 13;
_context4.t0 = _context4["catch"](1);
_this.props.errorHandler(_context4.t0, 'add-child-reaction', {
kind: kind,
reaction: reaction,
feedGroup: _this.props.feedGroup,
userId: _this.props.userId
});
return _context4.abrupt("return");
case 17:
// this.trackAnalytics(kind, reaction, options.trackAnalytics);
enrichedReaction = immutable.fromJS(_objectSpread({}, childReaction, {
user: _this.props.user.full
}));
_this.setState(function (prevState) {
var activities = prevState.activities;
var _iteratorNormalCompletion3 = true;
var _didIteratorError3 = false;
var _iteratorError3 = undefined;
try {
for (var _iterator3 = _this.getReactionPaths(reaction)[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
var path = _step3.value;
activities = activities.updateIn(_toConsumableArray(path).concat(['children_counts', kind]), function () {
var v = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
return v + 1;
}).updateIn(_toConsumableArray(path).concat(['own_children', kind]), function () {
var v = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : immutable.List();
return v.unshift(enrichedReaction);
}).updateIn(_toConsumableArray(path).concat(['latest_children', kind]), function () {
var v = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : immutable.List();
return v.unshift(enrichedReaction);
});
}
} catch (err) {
_didIteratorError3 = true;
_iteratorError3 = err;
} finally {
try {
if (!_iteratorNormalCompletion3 && _iterator3.return != null) {
_iterator3.return();
}
} finally {
if (_didIteratorError3) {
throw _iteratorError3;
}
}
}
return {
activities: activities
};
});
case 19:
case "end":
return _context4.stop();
}
}
}, _callee4, this, [[1, 13]]);
}));
return function (_x10, _x11, _x12) {
return _ref4.apply(this, arguments);
};
}());
_defineProperty(this, "onRemoveChildReaction",
/*#__PURE__*/
function () {
var _ref5 = _asyncToGenerator(
/*#__PURE__*/
_regeneratorRuntime.mark(function _callee5(kind, reaction, id) {
return _regeneratorRuntime.wrap(function _callee5$(_context5) {
while (1) {
switch (_context5.prev = _context5.next) {
case 0:
_context5.prev = 1;
if (!_this.props.doChildReactionDeleteRequest) {
_context5.next = 7;
break;
}
_context5.next = 5;
return _this.props.doChildReactionDeleteRequest(id);
case 5:
_context5.next = 9;
break;
case 7:
_context5.next = 9;
return _this.props.client.reactions.delete(id);
case 9:
_context5.next = 15;
break;
case 11:
_context5.prev = 11;
_context5.t0 = _context5["catch"](1);
_this.props.errorHandler(_context5.t0, 'delete-reaction', {
kind: kind,
reaction: reaction,
feedGroup: _this.props.feedGroup,
userId: _this.props.userId
});
return _context5.abrupt("return");
case 15:
// this.trackAnalytics('un' + kind, reaction, options.trackAnalytics);
if (_this.state.reactionActivities[id]) {
_this._removeActivityFromState(_this.state.reactionActivities[id]);
}
return _context5.abrupt("return", _this.setState(function (prevState) {
var activities = prevState.activities;
var _iteratorNormalCompletion4 = true;
var _didIteratorError4 = false;
var _iteratorError4 = undefined;
try {
for (var _iterator4 = _this.getReactionPaths(reaction)[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
var path = _step4.value;
activities = activities.updateIn(_toConsumableArray(path).concat(['children_counts', kind]), function () {
var v = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
return v - 1;
}).updateIn(_toConsumableArray(path).concat(['own_children', kind]), function () {
var v = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : immutable.List();
return v.remove(v.findIndex(function (r) {
return r.get('id') === id;
}));
}).updateIn(_toConsumableArray(path).concat(['children', kind]), function () {
var v = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : immutable.List();
return v.remove(v.findIndex(function (r) {
return r.get('id') === id;
}));
});
}
} catch (err) {
_didIteratorError4 = true;
_iteratorError4 = err;
} finally {
try {
if (!_iteratorNormalCompletion4 && _iterator4.return != null) {
_iterator4.return();
}
} finally {
if (_didIteratorError4) {
throw _iteratorError4;
}
}
}
return {
activities: activities
};
}));
case 17:
case "end":
return _context5.stop();
}
}
}, _callee5, this, [[1, 11]]);
}));
return function (_x13, _x14, _x15) {
return _ref5.apply(this, arguments);
};
}());
_defineProperty(this, "onToggleChildReaction",
/*#__PURE__*/
function () {
var _ref6 = _asyncToGenerator(
/*#__PURE__*/
_regeneratorRuntime.mark(function _callee6(kind, reaction, data) {
var options,
togglingReactions,
currentReactions,
last,
_args6 = arguments;
return _regeneratorRuntime.wrap(function _callee6$(_context6) {
while (1) {
switch (_context6.prev = _context6.next) {
case 0:
options = _args6.length > 3 && _args6[3] !== undefined ? _args6[3] : {};
togglingReactions = _this.state.childReactionsBeingToggled[kind] || {};
if (!togglingReactions[reaction.id]) {
_context6.next = 4;
break;
}
return _context6.abrupt("return");
case 4:
togglingReactions[reaction.id] = true;
_this.state.childReactionsBeingToggled[kind] = togglingReactions;
currentReactions = _this.state.activities.getIn(_toConsumableArray(_this.getReactionPaths(reaction)[0]).concat(['own_children', kind]), immutable.List());
last = currentReactions.last();
if (!last) {
_context6.next = 13;
break;
}
_context6.next = 11;
return _this.onRemoveChildReaction(kind, reaction, last.get('id'), options);
case 11:
_context6.next = 15;
break;
case 13:
_context6.next = 15;
return _this.onAddChildReaction(kind, reaction, data, options);
case 15:
delete togglingReactions[reaction.id];
case 16:
case "end":
return _context6.stop();
}
}
}, _callee6, this);
}));
return function (_x16, _x17, _x18) {
return _ref6.apply(this, arguments);
};
}());
_defineProperty(this, "_removeActivityFromState", function (activityId) {
return _this.setState(function (_ref7) {
var activities = _ref7.activities,
activityOrder = _ref7.activityOrder,
activityIdToPath = _ref7.activityIdToPath,
activityIdToPaths = _ref7.activityIdToPaths,
reactionIdToPaths = _ref7.reactionIdToPaths;
var path = _this.getActivityPath(activityId);
var outerId = activityId;
if (path.length > 1) {
// It's an aggregated group we should update the paths of everything in
// the list
var groupArrayPath = path.slice(0, -1);
activityIdToPath = _this.removeFoundActivityIdPath(activities.getIn(groupArrayPath).toJS(), activityIdToPath, groupArrayPath);
activityIdToPaths = _this.removeFoundActivityIdPaths(activities.getIn(groupArrayPath).toJS(), activityIdToPaths, groupArrayPath);
reactionIdToPaths = _this.removeFoundReactionIdPaths(activities.getIn(groupArrayPath).toJS(), reactionIdToPaths, groupArrayPath);
} else {
// Otherwise remove all things inside this activity from the path
// objects
activityIdToPaths = _this.removeFoundActivityIdPaths(activities.get(activityId).toJS(), activityIdToPaths, [activityId]);
reactionIdToPaths = _this.removeFoundReactionIdPaths(activities.get(activityId).toJS(), reactionIdToPaths, [activityId]);
}
activities = activities.removeIn(path);
if (path.length > 1) {
var _groupArrayPath = path.slice(0, -1);
if (activities.getIn(_groupArrayPath).size === 0) {
outerId = path[0]; //
} else {
outerId = null;
}
activityIdToPath = _this.addFoundActivityIdPath(activities.getIn(_groupArrayPath).toJS(), activityIdToPath, _groupArrayPath);
activityIdToPaths = _this.addFoundActivityIdPaths(activities.getIn(_groupArrayPath).toJS(), activityIdToPaths, _groupArrayPath);
reactionIdToPaths = _this.addFoundReactionIdPaths(activities.getIn(_groupArrayPath).toJS(), reactionIdToPaths, _groupArrayPath);
}
if (outerId != null) {
activityOrder = activityOrder.filter(function (id) {
return id !== outerId;
});
}
return {
activities: activities,
activityOrder: activityOrder,
activityIdToPaths: activityIdToPaths,
reactionIdToPaths: reactionIdToPaths,
activityIdToPath: activityIdToPath
};
});
});
_defineProperty(this, "onRemoveActivity",
/*#__PURE__*/
function () {
var _ref8 = _asyncToGenerator(
/*#__PURE__*/
_regeneratorRuntime.mark(function _callee7(activityId) {
return _regeneratorRuntime.wrap(function _callee7$(_context7) {
while (1) {
switch (_context7.prev = _context7.next) {
case 0:
_context7.prev = 0;
if (!_this.props.doActivityDeleteRequest) {
_context7.next = 6;
break;
}
_context7.next = 4;
return _this.props.doActivityDeleteRequest(activityId);
case 4:
_context7.next = 8;
break;
case 6:
_context7.next = 8;
return _this.feed().removeActivity(activityId);
case 8:
_context7.next = 14;
break;
case 10:
_context7.prev = 10;
_context7.t0 = _context7["catch"](0);
_this.props.errorHandler(_context7.t0, 'delete-activity', {
activityId: _this.props.feedGroup,
feedGroup: _this.props.feedGroup,
userId: _this.props.userId
});
return _context7.abrupt("return");
case 14:
return _context7.abrupt("return", _this._removeActivityFromState(activityId));
case 15:
case "end":
return _context7.stop();
}
}
}, _callee7, this, [[0, 10]]);
}));
return function (_x19) {
return _ref8.apply(this, arguments);
};
}());
_defineProperty(this, "onMarkAsRead", function (group) {
return _this._onMarkAs('read', group);
});
_defineProperty(this, "onMarkAsSeen", function (group) {
return _this._onMarkAs('seen', group);
});
_defineProperty(this, "_onMarkAs",
/*#__PURE__*/
function () {
var _ref9 = _asyncToGenerator(
/*#__PURE__*/
_regeneratorRuntime.mark(function _callee8(type, group) {
var groupArray, markArg;
return _regeneratorRuntime.wrap(function _callee8$(_context8) {
while (1) {
switch (_context8.prev = _context8.next) {
case 0:
markArg = group;
if (group === true) {
groupArray = _this.state.activityOrder;
} else if (Array.isArray(group)) {
groupArray = group.map(function (g) {
return g.id;
});
markArg = groupArray;
} else {
markArg = group.id;
groupArray = [group.id];
}
_context8.prev = 2;
_context8.next = 5;
return _this.doFeedRequest(_defineProperty({
limit: 1,
id_lte: _this.state.activityOrder[0]
}, 'mark_' + type, markArg));
case 5:
_context8.next = 10;
break;
case 7:
_context8.prev = 7;
_context8.t0 = _context8["catch"](2);
_this.props.errorHandler(_context8.t0, 'get-notification-counts', {
feedGroup: _this.props.feedGroup,
userId: _this.props.userId
});
case 10:
_this.setState(function (prevState) {
var counterKey = 'un' + type;
var activit