<commit_before>package com.tonyjhuang.whereyou.api;

import android.util.Log;

import com.parse.FunctionCallback;
import com.parse.ParseCloud;
import com.parse.ParseException;
import com.parse.ParseInstallation;
import com.parse.ParsePush;
import com.parse.ParseQuery;
import com.tonyjhuang.whereyou.services.WhereYouAction;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * Created by tony on 4/29/15.
 */
public class ParseHelper {
    private static ArrayList<String> addedMessages = new ArrayList<>();

    static {
        addedMessages.add("{name} added you. Or whatever. I mean, big deal.");
        addedMessages.add("{name} added you as a 'friend'... ;)");
        addedMessages.add("WOOOOOOOO!!! {name} ADDED YOU!!!! WOAHHHHH");
        addedMessages.add("I have good news and bad news. {name} added you. Welp.");
        addedMessages.add("{name} added you. April fools! It's also opposite day!");
        addedMessages.add("{name} just friendzoned you on WhereYou? How's that make you feel?");
        addedMessages.add("{name} just friended you, did you even give them your username?");
        addedMessages.add("Wow, look at you, Mr./Ms. Popular. {name} just added you.");
        addedMessages.add("Wanna hear a joke? {name} just added you.");
        addedMessages.add("{name} added you. Cool.");
        addedMessages.add("{name} just added you. I guess you guys are friends now.");
        addedMessages.add("{name} just added you and they can totally stalk you now ;)");
        addedMessages.add("oooooo {name} just added you whaaaatttt swag swag");
        addedMessages.add("{name} just added you. Time to file for that restraining order!");
    }

    private Random random = new Random();
    private ParseInstallation currentInstallation;

    public ParseHelper() {
        currentInstallation = ParseInstallation.getCurrentInstallation();
    }

    public void checkName(String name, FunctionCallback<Boolean> callback) {
        Map<String, Object> params = new HashMap<>();
        params.put("name", name.toLowerCase());
        ParseCloud.callFunctionInBackground("checkName", params, callback);
    }

    public void updateName(String name) {
        currentInstallation.put("name", name);
        currentInstallation.put("nameLowercase", name.toLowerCase());
        currentInstallation.saveInBackground();
    }

    // Returns the updated friends list
    public JSONArray removeFriend(String name) {
        JSONArray friendsList = currentInstallation.getJSONArray("friends");
        JSONArray newFriendsList = new JSONArray();
        try {
            if (friendsList != null) {
                for (int i = 0; i < friendsList.length(); i++) {
                    String friend = friendsList.getString(i);
                    if (!friend.equals(name)) {
                        newFriendsList.put(friend);
                    }
                }
            }
        } catch (JSONException e) {
            // if we run into a JSONException, abort.
            Log.e("ParseHelper", e.getMessage());
            return friendsList;
        }

        currentInstallation.put("friends", newFriendsList);
        currentInstallation.saveInBackground();
        return newFriendsList;
    }

    public void addFriend(final String name, final Callback<JSONArray> callback) {
        checkName(name, new FunctionCallback<Boolean>() {
            @Override
            public void done(Boolean nameExists, ParseException e) {
                if (e == null) {
                    if (nameExists) {
                        // Update current installation friends list with new friend.
                        JSONArray friendsList = currentInstallation.getJSONArray("friends");
                        if (friendsList == null) friendsList = new JSONArray();
                        friendsList.put(name);
                        currentInstallation.put("friends", friendsList);
                        currentInstallation.saveInBackground();

                        notifyFriend(name);

                        callback.onFinish(friendsList);
                    } else {
                        callback.onError(new Error("No such user"));
                    }
                } else {
                    Log.e("ParseHelper", e.getMessage());
                    callback.onError(e);
                }
            }
        });
    }

    private void notifyFriend(String name) {
        String myName = currentInstallation.getString("name");
        String message = addedMessages.get(random.nextInt(addedMessages.size()));
        message = message.replace("{name}", myName);

        ParseQuery<ParseInstallation> pushQuery = getQuery(name);

        JSONObject data = new JSONObject();
        try {
            data.put("name", myName);
            data.put("alert", message);
            data.put("action", WhereYouAction.NOTIFY_ADD);
        } catch (JSONException e) {
            Log.e("ParseHelper", e.getMessage());
        }

        ParsePush push = new ParsePush();
        push.setQuery(pushQuery);
        push.setData(data);
        push.sendInBackground();
    }

    private ParseQuery<ParseInstallation> getQuery(String name) {
        ParseQuery<ParseInstallation> pushQuery = ParseInstallation.getQuery();
        pushQuery.whereEqualTo("name", name);
        return pushQuery;
    }

    public void poke(String name) {
        String myName = currentInstallation.getString("name");

        ParseQuery<ParseInstallation> pushQuery = getQuery(name);

        JSONObject data = new JSONObject();
        try {
            data.put("name", myName);
            data.put("alert", myName + " wants to know where you at! Tap here to share your location.");
            data.put("action", WhereYouAction.ASK);
        } catch (JSONException e) {
            Log.e("ParseHelper", e.getMessage());
        }

        ParsePush push = new ParsePush();
        push.setQuery(pushQuery);
        push.setData(data);
        push.sendInBackground();
    }

    public JSONArray addToBlacklist(String name) {
        JSONArray blacklist = currentInstallation.getJSONArray("blacklist");
        if (blacklist == null)
            blacklist = new JSONArray();
        try {
            for (int i = 0; i < blacklist.length(); i++) {
                if (blacklist.getString(i).equals(name))
                    return blacklist;
            }
        } catch (JSONException e) {
            Log.e("ParseHelper", e.getMessage());
        }

        blacklist.put(name);
        currentInstallation.put("blacklist", blacklist);
        currentInstallation.saveInBackground();
        return blacklist;
    }

    public JSONArray removeFromBlacklist(String name) {
        JSONArray blacklist = currentInstallation.getJSONArray("blacklist");
        JSONArray newBlacklist = new JSONArray();
        try {
            if (blacklist != null) {
                for (int i = 0; i < blacklist.length(); i++) {
                    String blacklisted = blacklist.getString(i);
                    if (!blacklisted.equals(name)) {
                        newBlacklist.put(blacklisted);
                    }
                }
            }
        } catch (JSONException e) {
            // if we run into a JSONException, abort.
            Log.e("ParseHelper", e.getMessage());
            return blacklist;
        }

        currentInstallation.put("blacklist", newBlacklist);
        currentInstallation.saveInBackground();
        return newBlacklist;
    }

    public interface Callback<T> {
        void onFinish(T t);<commit_msg>cleanedup parsehelper, added helper methods, added isFriend and isInBlacklist methods<commit_after>package com.tonyjhuang.whereyou.api;

import android.util.Log;

import com.parse.FunctionCallback;
import com.parse.ParseCloud;
import com.parse.ParseException;
import com.parse.ParseInstallation;
import com.parse.ParsePush;
import com.parse.ParseQuery;
import com.tonyjhuang.whereyou.services.WhereYouAction;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * Created by tony on 4/29/15.
 */
public class ParseHelper {
    private static ArrayList<String> addedMessages = new ArrayList<>();

    static {
        addedMessages.add("{name} added you. Or whatever. I mean, big deal.");
        addedMessages.add("{name} added you as a 'friend'... ;)");
        addedMessages.add("WOOOOOOOO!!! {name} ADDED YOU!!!! WOAHHHHH");
        addedMessages.add("I have good news and bad news. {name} added you. Welp.");
        addedMessages.add("{name} added you. April fools! It's also opposite day!");
        addedMessages.add("{name} just friendzoned you on WhereYou? How's that make you feel?");
        addedMessages.add("{name} just friended you, did you even give them your username?");
        addedMessages.add("Wow, look at you, Mr./Ms. Popular. {name} just added you.");
        addedMessages.add("Wanna hear a joke? {name} just added you.");
        addedMessages.add("{name} added you. Cool.");
        addedMessages.add("{name} just added you. I guess you guys are friends now.");
        addedMessages.add("{name} just added you and they can totally stalk you now ;)");
        addedMessages.add("oooooo {name} just added you whaaaatttt swag swag");
        addedMessages.add("{name} just added you. Time to file for that restraining order!");
    }

    private Random random = new Random();
    private ParseInstallation currentInstallation;

    public ParseHelper() {
        currentInstallation = ParseInstallation.getCurrentInstallation();
    }

    public void checkName(String name, FunctionCallback<Boolean> callback) {
        Map<String, Object> params = new HashMap<>();
        params.put("name", name.toLowerCase());
        ParseCloud.callFunctionInBackground("checkName", params, callback);
    }

    public void updateName(String name) {
        currentInstallation.put("name", name);
        currentInstallation.put("nameLowercase", name.toLowerCase());
        currentInstallation.saveInBackground();
    }

    public boolean isFriend(String name) {
        JSONArray friendsList = currentInstallation.getJSONArray("friends");
        if (friendsList == null || friendsList.length() == 0)
            return false;

        try {
            for (int i = 0; i < friendsList.length(); i++) {
                String friend = friendsList.getString(i);
                if (friend.equals(name)) {
                    return true;
                }
            }
        } catch (JSONException e) {
            // if we run into a JSONException, abort.
            Log.e("ParseHelper", e.getMessage());
        }

        return false;
    }

    // Returns the updated friends list
    public JSONArray removeFriend(String name) {
        JSONArray friendsList = currentInstallation.getJSONArray("friends");

        JSONArray newFriendsList = new JSONArray();
        try {
            if (friendsList != null) {
                for (int i = 0; i < friendsList.length(); i++) {
                    String friend = friendsList.getString(i);
                    if (!friend.equals(name)) {
                        newFriendsList.put(friend);
                    }
                }
            }
        } catch (JSONException e) {
            // if we run into a JSONException, abort.
            Log.e("ParseHelper", e.getMessage());
            return friendsList;
        }

        currentInstallation.put("friends", newFriendsList);
        currentInstallation.saveInBackground();
        return newFriendsList;
    }

    public void addFriend(final String name, final Callback<JSONArray> callback) {
        if(isFriend(name)) {
            callback.onFinish(currentInstallation.getJSONArray("friends"));
        } else {
            checkName(name, new FunctionCallback<Boolean>() {
                @Override
                public void done(Boolean nameExists, ParseException e) {
                    if (e == null) {
                        if (nameExists) {
                            // Update current installation friends list with new friend.
                            JSONArray friendsList = currentInstallation.getJSONArray("friends");
                            if (friendsList == null) friendsList = new JSONArray();
                            friendsList.put(name);
                            currentInstallation.put("friends", friendsList);
                            currentInstallation.saveInBackground();

                            notifyFriend(name);

                            callback.onFinish(friendsList);
                        } else {
                            callback.onError(new Error("No such user"));
                        }
                    } else {
                        Log.e("ParseHelper", e.getMessage());
                        callback.onError(e);
                    }
                }
            });
        }
    }

    private void notifyFriend(String name) {
        String myName = currentInstallation.getString("name");
        String message = addedMessages.get(random.nextInt(addedMessages.size()));
        message = message.replace("{name}", myName);

        ParseQuery<ParseInstallation> pushQuery = getQuery(name);

        JSONObject data = new JSONObject();
        try {
            data.put("name", myName);
            data.put("alert", message);
            data.put("action", WhereYouAction.NOTIFY_ADD);
        } catch (JSONException e) {
            Log.e("ParseHelper", e.getMessage());
        }

        ParsePush push = new ParsePush();
        push.setQuery(pushQuery);
        push.setData(data);
        push.sendInBackground();
    }

    private ParseQuery<ParseInstallation> getQuery(String name) {
        ParseQuery<ParseInstallation> pushQuery = ParseInstallation.getQuery();
        pushQuery.whereEqualTo("name", name);
        return pushQuery;
    }

    public void poke(String name) {
        String myName = currentInstallation.getString("name");

        ParseQuery<ParseInstallation> pushQuery = getQuery(name);

        JSONObject data = new JSONObject();
        try {
            data.put("name", myName);
            data.put("alert", myName + " wants to know where you at! Tap here to share your location.");
            data.put("action", WhereYouAction.ASK);
        } catch (JSONException e) {
            Log.e("ParseHelper", e.getMessage());
        }

        ParsePush push = new ParsePush();
        push.setQuery(pushQuery);
        push.setData(data);
        push.sendInBackground();
    }

    public boolean isInBlacklist(String name) {
        JSONArray blacklist = currentInstallation.getJSONArray("blacklist");
        if (blacklist == null || blacklist.length() == 0)
            return false;

        try {
            for (int i = 0; i < blacklist.length(); i++) {
                if (blacklist.getString(i).equals(name))
                    return true;
            }
        } catch (JSONException e) {
            Log.e("ParseHelper", e.getMessage());
        }

        return false;
    }

    public JSONArray addToBlacklist(String name) {
        JSONArray blacklist = currentInstallation.getJSONArray("blacklist");
        if (isInBlacklist(name)) return blacklist;

        if (blacklist == null)
            blacklist = new JSONArray();

        blacklist.put(name);
        currentInstallation.put("blacklist", blacklist);
        currentInstallation.saveInBackground();
        return blacklist;
    }

    public JSONArray removeFromBlacklist(String name) {
        JSONArray blacklist = currentInstallation.getJSONArray("blacklist");

        if (!isInBlacklist(name))
            return blacklist;

        JSONArray newBlacklist = new JSONArray();
        try {
            if (blacklist != null) {
                for (int i = 0; i < blacklist.length(); i++) {
                    String blacklisted = blacklist.getString(i);
                    if (!blacklisted.equals(name)) {
                        newBlacklist.put(blacklisted);
                    }
                }
            }
        } catch (JSONException e) {
            // if we run into a JSONException, abort.
            Log.e("ParseHelper", e.getMessage());
            return blacklist;
        }

        currentInstallation.put("blacklist", newBlacklist);
        currentInstallation.saveInBackground();
        return newBlacklist;
    }

    public interface Callback<T> {
        void onFinish(T t);
{'fraction_non_alphanumeric': 0.10921052631578948, 'fraction_numerical': 0.0011363636363636363, 'mean_word_length': 2.1056835066864785, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6126397', 'n_tokens_mistral': 4336, 'n_tokens_neox': 4193, 'n_words': 1078}