From 3aaf71fdea914af1a7f2b2fb97bb6ae44132fcc4 Mon Sep 17 00:00:00 2001
From: Debanshu Kundu <debanshu.kundu@joshtechnologygroup.com>
Date: Fri, 20 Jan 2017 23:11:13 +0530
Subject:  #4257 Added functionality to create previews for post links using
 open graph data from those links. (#4890)

---
 webapp/stores/opengraph_store.jsx | 68 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 68 insertions(+)
 create mode 100644 webapp/stores/opengraph_store.jsx

(limited to 'webapp/stores/opengraph_store.jsx')

diff --git a/webapp/stores/opengraph_store.jsx b/webapp/stores/opengraph_store.jsx
new file mode 100644
index 000000000..4ad156df0
--- /dev/null
+++ b/webapp/stores/opengraph_store.jsx
@@ -0,0 +1,68 @@
+import EventEmitter from 'events';
+
+import AppDispatcher from 'dispatcher/app_dispatcher.jsx';
+import Constants from 'utils/constants.jsx';
+
+const ActionTypes = Constants.ActionTypes;
+
+const CHANGE_EVENT = 'change';
+const URL_DATA_CHANGE_EVENT = 'url_data_change';
+
+class OpenGraphStoreClass extends EventEmitter {
+    constructor() {
+        super();
+        this.ogDataObject = {};  // Format: {<url>: <data-object>}
+    }
+
+    emitChange() {
+        this.emit(CHANGE_EVENT);
+    }
+
+    addChangeListener(callback) {
+        this.on(CHANGE_EVENT, callback);
+    }
+
+    removeChangeListener(callback) {
+        this.removeListener(CHANGE_EVENT, callback);
+    }
+
+    emitUrlDataChange(url) {
+        this.emit(URL_DATA_CHANGE_EVENT, url);
+    }
+
+    addUrlDataChangeListener(callback) {
+        this.on(URL_DATA_CHANGE_EVENT, callback);
+    }
+
+    removeUrlDataChangeListener(callback) {
+        this.removeListener(URL_DATA_CHANGE_EVENT, callback);
+    }
+
+    storeOgInfo(url, ogInfo) {
+        this.ogDataObject[url] = ogInfo;
+    }
+
+    getOgInfo(url) {
+        return this.ogDataObject[url];
+    }
+}
+
+var OpenGraphStore = new OpenGraphStoreClass();
+
+// Not expecting more that `Constants.POST_CHUNK_SIZE` post previews rendered at a time
+OpenGraphStore.setMaxListeners(Constants.POST_CHUNK_SIZE);
+
+OpenGraphStore.dispatchToken = AppDispatcher.register((payload) => {
+    var action = payload.action;
+
+    switch (action.type) {
+    case ActionTypes.RECIVED_OPEN_GRAPH_METADATA:
+        OpenGraphStore.storeOgInfo(action.url, action.data);
+        OpenGraphStore.emitUrlDataChange(action.url);
+        OpenGraphStore.emitChange();
+        break;
+    default:
+    }
+});
+
+export default OpenGraphStore;
-- 
cgit v1.2.3-1-g7c22

