Commit fe16dad9 authored by MrMan's avatar MrMan

Add messaging, deleting, getting info for matches

parent 72cff68f
......@@ -11,10 +11,12 @@ export default Reflux.createActions([
"matchResultDetected",
"pass",
"pullTinderRecommendations",
"receivedSentMessageReceipt",
"republishTinderUser",
"retrieveMatchedUserInfo",
"retrieveTinderUpdates",
"sentimentSuccessfullyProcessed",
"successfullyDeletedMatch",
"updateLocationFromGPS",
"updateTinderPreferences",
"retrieveMatchedUserInfo"
"updateTinderPreferences"
]);
......@@ -29,6 +29,17 @@ function TINDER_SENTIMENT_URL(strings, ...values) {
return `https://api.gotinder.com/${values[0]}/${values[1]}`;
}
function TINDER_MATCH_MESSAGE_URL(strings, ...values) {
return `https://api.gotinder.com/user/matches/${values[0]}`;
}
function TINDER_USER_INFO_URL(strings, ...values) {
return `https://api.gotinder.com/user/${values[0]}`;
}
function TINDER_DELETE_MATCH_URL(strings, ...values) {
return `https://api.gotinder.com/user/matches/${values[0]}`;
}
// Tinder-related
let TINDER_HEADERS_WITHOUT_AUTH_TOKEN = {
......@@ -70,14 +81,17 @@ export default {
SENTIMENT_PASS,
TINDER_AUTH_URL,
TINDER_CLIENT_ID,
TINDER_DELETE_MATCH_URL,
TINDER_GENDER_FEMALE,
TINDER_GENDER_MALE,
TINDER_HEADERS_WITHOUT_AUTH_TOKEN,
TINDER_LOCATION_URL,
TINDER_MATCH_MESSAGE_URL,
TINDER_MAX_FILTER_AGE,
TINDER_MIN_FILTER_AGE,
TINDER_PROFILE_URL,
TINDER_RECS_URL,
TINDER_SENTIMENT_URL,
TINDER_UPDATES_URL
TINDER_UPDATES_URL,
TINDER_USER_INFO_URL
};
......@@ -2,15 +2,26 @@ import Reflux from "reflux";
import Actions from "../actions";
import Constants from "../constants";
import TinderUpdateStore from "./tinder-update";
import TinderAuthStore from "./tinder-auth";
import TinderAPI from "../tinder-api";
import LocalStorageBackupMixinGenerator from "../mixins/localstorage-backup";
import fetch from "vendor/fetch";
import _ from "lodash";
import FEMALE_USER_FIXTURE from "../../test/fixtures/female-user-fixture";
const DEFAULT_STATE = {matches: {}, matchedUserInfo: {}};
const DEFAULT_STATE = {matches: {}, matchedUserInfo: {}, tinderAccessToken: null};
const MATCH_DEFAULT_STATE = {messages: []};
const LocalStorageBackupMixin = LocalStorageBackupMixinGenerator(Constants.LOCALSTORAGE_KEY_TINDER_MATCH_STORE_STATE);
function generateTinderURLForMatchMessageSend(matchId) {
return Constants.TINDER_MATCH_MESSAGE_URL`${matchId}`;
}
function generateTinderURLForUserInfo(userId) {
return Constants.TINDER_USER_INFO_URL`${userId}`;
}
function generateTinderURLForDeleteMatch(matchId) {
return Constants.TINDER_DELETE_MATCH_URL`${matchId}`;
}
let TinderMatchStore = Reflux.createStore({
mixins: [LocalStorageBackupMixin],
init() {
......@@ -25,25 +36,68 @@ let TinderMatchStore = Reflux.createStore({
this.processTinderUpdate(update.lastUpdate);
});
// Save the Tinder access token when provided by the TinderAuthStore
TinderAuthStore.listen(auth => {
console.log("[TINDER UPDATE STORE] Detected Tinder auth update:", auth);
this.state.tinderAccessToken = auth.token;
this.backupState();
});
// Gather state for the store from localstorage/other sources
// Also attempt to pull state from parent stores if they have initialized and have state
this.state = _.extend(DEFAULT_STATE, this.gatherState());
this.state.tinderAccessToken = this.state.tinderAccessToken || TinderAuthStore.state.token;
console.log("[TINDER MATCH STORE] Initial state:", _.extend({}, this.state));
},
// Check whether the necessary credentials have been provided
hasNecessaryCredentials() {
return !_.isEmpty(this.state.tinderAccessToken);
},
/**
* Retrieve info for a matched user
*
* @param {string} id - Match ID of the matched user
* @param {string} matchId - Match ID of the matched user
*/
retrieveMatchedUserInfo(id) {
retrieveMatchedUserInfo(matchId) {
// Retrieve matched user info
this.state.matchedUserInfo[id] = FEMALE_USER_FIXTURE;
let userId = _.get(this.state.matches, matchId);
if (_.isNull(userId) || _.isUndefined(userId)) {
throw new Error("Failed to find match with given match ID [" + matchId + "]");
}
// Attempt to find user Info for given match
TinderAPI.GETWithToken(generateTinderURLForUserInfo(userId), this.state.tinderAccessToken)
.then(resp => resp.json())
.then(user => {
this.state.matchedUserInfo[matchId] = user;
// Backup if info is new
if (!_.has(this.state.matchedUserInfo, matchId)) { this.backupState(); }
this.trigger(this.state);
});
},
// Backup if info is new
if (!_.has(this.state.matchedUserInfo, id)) { this.backupState(); }
this.trigger(this.state);
/**
* Send a message to a twitter match
*
* @param {string} id - tinder match ID
* @param {string} msg - the message to send
*/
sendMessageToMatch(matchId, message) {
if (!this.hasNecessaryCredentials()) { throw new Error("Necessary creds missing"); }
// Attempt to send message to match
console.log("[TINDER MATCH STORE] Attempting to send message to match with id:", matchId);
TinderAPI.POSTWithToken(generateTinderURLForMatchMessageSend(matchId),
{message},
this.state.tinderAccessToken)
.then(resp => resp.json())
.then(resp => {
Actions.receivedSentMessageReceipt(matchId, resp);
});
},
/**
......@@ -51,8 +105,13 @@ let TinderMatchStore = Reflux.createStore({
*
* @param {string} id - Match ID of the matched user to be unmatched
*/
umatchUser(id) {
umatchUser(matchId) {
if (!this.hasNecessaryCredentials()) { throw new Error("Necessary creds missing"); }
// Attempt to find user Info for given match
TinderAPI.DELETEWithToken(generateTinderURLForDeleteMatch(matchId), this.state.tinderAccessToken)
.then(resp => resp.json())
.then(resp => { Actions.successfullyDeletedMatch(matchId, resp); });
},
/**
......@@ -62,9 +121,9 @@ let TinderMatchStore = Reflux.createStore({
*/
processTinderUpdate(update) {
console.log("[TINDER MATCH STORE] Parsing received tinder update", update);
let matches = update.matches || [];
// Process all matches in the update
console.log(`[TINDER MATCH STORE] Processing ${matches.length} matches...`);
_.forEach(matches, (m) => {
......
......@@ -95,8 +95,6 @@ let TinderSentimentStore = Reflux.createStore({
TinderAPI.GETWithToken(this.state.tinderAccessToken, generateTinderURLFromSentiment(sentiment))
.then(resp => resp.json())
.then(resp => {
console.log("Received response from sentiment:", resp);
// Handle sentiment failure (for whatever reason tinder specifies)
if (_.has(resp, "status") && resp.status !== 200) {
reject(new Error("An error occurred while trying to process sentiment, response:", resp));
......
......@@ -6,7 +6,6 @@ import TinderAPI from "../tinder-api";
import LocalStorageBackupMixinGenerator from "../mixins/localstorage-backup";
import fetch from "vendor/fetch";
import _ from "lodash";
import UPDATE_FIXTURE from "../../test/fixtures/update-fixture";
const DEFAULT_STATE = {lastUpdate: null, tinderAccessToken: null};
const LocalStorageBackupMixin = LocalStorageBackupMixinGenerator(Constants.LOCALSTORAGE_KEY_TINDER_UPDATE_STORE_STATE);
......@@ -48,20 +47,20 @@ let TinderUpdateStore = Reflux.createStore({
return;
}
// console.log("[TINDER UPDATE STORE] Retrieving updates from Tinder API...");
// TinderAPI.GETWithToken(this.state.tinderAccessToken, Constants.TINDER_UPDATES_URL)
// .then(resp => resp.json())
// .then(resp => {
// console.log("[TINDER UPDATE STORE] Received updates from tinder:", resp);
console.log("[TINDER UPDATE STORE] Retrieving updates from Tinder API...");
TinderAPI.POSTWithToken(this.state.tinderAccessToken,
{last_activity_date:""},
Constants.TINDER_UPDATES_URL)
.then(resp => resp.json())
.then(resp => {
console.log("[TINDER UPDATE STORE] Received updates from tinder:", resp);
// REMOVE: using fixture data
//this.state.lastUpdate = resp;
this.state.lastUpdate = UPDATE_FIXTURE;
this.state.lastUpdate = resp;
// Update state/trigger actions
this.backupState();
this.trigger(this.state);
// });
// Update state/trigger actions
this.backupState();
this.trigger(this.state);
});
}
});
......
......@@ -14,12 +14,14 @@ const EXPECTED_ACTIONS = [
"matchResultDetected",
"pass",
"pullTinderRecommendations",
"receivedSentMessageReceipt",
"republishTinderUser",
"retrieveMatchedUserInfo",
"retrieveTinderUpdates",
"sentimentSuccessfullyProcessed",
"successfullyDeletedMatch",
"updateLocationFromGPS",
"updateTinderPreferences",
"retrieveMatchedUserInfo"
"updateTinderPreferences"
];
describe("React actions", () => {
......
......@@ -21,16 +21,19 @@ const EXPECTED_CONSTANTS = [
"SENTIMENT_PASS",
"TINDER_AUTH_URL",
"TINDER_CLIENT_ID",
"TINDER_DELETE_MATCH_URL",
"TINDER_GENDER_FEMALE",
"TINDER_GENDER_MALE",
"TINDER_HEADERS_WITHOUT_AUTH_TOKEN",
"TINDER_LOCATION_URL",
"TINDER_MATCH_MESSAGE_URL",
"TINDER_MAX_FILTER_AGE",
"TINDER_MIN_FILTER_AGE",
"TINDER_PROFILE_URL",
"TINDER_RECS_URL",
"TINDER_SENTIMENT_URL",
"TINDER_UPDATES_URL"
"TINDER_UPDATES_URL",
"TINDER_USER_INFO_URL"
];
describe("React constants", () => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment