<commit_before>/* global _ */
/* global Firebase */
angular.module("app", ["firebase", "ngStorage"])

    .run(function ($rootScope, $localStorage, $sessionStorage, $log, AuthSrv) {
        // Setup localStorage
        $rootScope.$storage = $localStorage;
        
        // Log-in state
        $rootScope.isLoggedIn = AuthSrv.isLoggedIn();
    })

    .controller("PostCtrl", function ($log, $scope, $firebaseArray, AuthSrv, PostSrv) {
        $scope.posts = PostSrv.posts;
    })

    .controller("LoginCtrl", function ($scope, $window) {
        var _ref = new Firebase("https://happy125.firebaseio.com");

        $scope.loginWithFacebook = function () {
            _ref.authWithOAuthRedirect("facebook", function (error, authData) {
                if (error) {
                    console.log("Login Failed!", error);
                } else {
                    console.log("Authenticated successfully with payload:", authData);
                }
            }, { 'scope': 'email,public_profile' });
        }

        $scope.loginWithEmail = function ($event) {
            var email = $scope.user.email;
            var password = $scope.<PASSWORD>;

            _ref.authWithPassword({
                email: email,
                password: password
            }, function (error, authData) {
                if (error) {
                    console.log("Error:", error);
                } else {
                    console.table(authData);

                    $scope.$storage.authData = authData;
                }
            });
        }

        $scope.logout = function () {
            _ref.unauth();

            $window.location.reload();
        }
    })

    .controller("RegisterCtrl", function ($scope) {
        var _ref = new Firebase("https://happy125.firebaseio.com");

        $scope.register = function ($event) {
            var email = $scope.user.email;
            var password = $scope.user.password;

            _ref.createUser({
                email: email,
                password: password
            }, function (error, userData) {
                if (error) {
                    console.log("Error:", error);
                } else {
                    console.table(userData);
                }
            });
        }
    })

    .factory("PostSrv", function ($log, $firebaseArray) {
        var _postsRef = new Firebase("https://happy125.firebaseio.com/posts");
        var _posts = $firebaseArray(_postsRef);

        return {
            posts: _posts
        }
    })

    .factory("LikeSrv", function ($log, $firebaseObject) {
        var _ref = new Firebase("https://happy125.firebaseio.com/likes");
        function getSnapshotInfo(snapshot) {
            return {
                key: snapshot.key(),
                value: snapshot.val(),
                count: snapshot.numChildren()
            }
        }
        
        // child_added
        _ref.on("child_added", function (snapshot, prevChildKey) {
            var info = getSnapshotInfo(snapshot);
            $log.debug("Like added:", info);
        });
        
        // child_changed
        _ref.on("child_changed", function(snapshot) {
            var info = getSnapshotInfo(snapshot);
            $log.debug("Like changed:", info);
        });
        
        // child_removed
        _ref.on("child_removed", function(snapshot) {
            var info = getSnapshotInfo(snapshot);
            $log.debug("Like deleted:", info);
        })

        return {
            like: function (post, email) {
                var postId = post.$id;
                var newLikeObj = {};
                newLikeObj[postId] = [email];

                _ref.update(newLikeObj);
            },
            unlike: function (post, email) {
                var postId = post.$id;
                var unlikeObj = {};
                unlikeObj[postId] = [];

                _ref.update(unlikeObj);
            }
        }
    })

    .factory("AuthSrv", function ($log, $localStorage, $rootScope) {
        // TODO: Auth.$requireAuth();<commit_msg>feat: like 객체 동기화 완료<commit_after>/* global _ */
/* global Firebase */
angular.module("app", ["firebase", "ngStorage"])

    .run(function ($rootScope, $localStorage, $sessionStorage, $log, AuthSrv) {
        // Setup localStorage
        $rootScope.$storage = $localStorage;
        
        // Log-in state
        $rootScope.isLoggedIn = AuthSrv.isLoggedIn();
    })

    .controller("PostCtrl", function ($log, $scope, $firebaseArray, AuthSrv, PostSrv) {
        $scope.posts = PostSrv.posts;
    })

    .controller("LoginCtrl", function ($scope, $window) {
        var _ref = new Firebase("https://happy125.firebaseio.com");

        $scope.loginWithFacebook = function () {
            _ref.authWithOAuthRedirect("facebook", function (error, authData) {
                if (error) {
                    console.log("Login Failed!", error);
                } else {
                    console.log("Authenticated successfully with payload:", authData);
                }
            }, { 'scope': 'email,public_profile' });
        }

        $scope.loginWithEmail = function ($event) {
            var email = $scope.user.email;
            var password = $<PASSWORD>;

            _ref.authWithPassword({
                email: email,
                password: password
            }, function (error, authData) {
                if (error) {
                    console.log("Error:", error);
                } else {
                    console.table(authData);

                    $scope.$storage.authData = authData;
                }
            });
        }

        $scope.logout = function () {
            _ref.unauth();

            $window.location.reload();
        }
    })

    .controller("RegisterCtrl", function ($scope) {
        var _ref = new Firebase("https://happy125.firebaseio.com");

        $scope.register = function ($event) {
            var email = $scope.user.email;
            var password = $<PASSWORD>;

            _ref.createUser({
                email: email,
                password: password
            }, function (error, userData) {
                if (error) {
                    console.log("Error:", error);
                } else {
                    console.table(userData);
                }
            });
        }
    })

    .factory("PostSrv", function ($log, $firebaseArray) {
        var _postsRef = new Firebase("https://happy125.firebaseio.com/posts");
        var _posts = $firebaseArray(_postsRef);

        return {
            posts: _posts
        }
    })

    .factory("LikeSrv", function ($log, $firebaseArray) {
        var _ref = new Firebase("https://happy125.firebaseio.com/likes");
        function getSnapshotInfo(snapshot) {
            return {
                key: snapshot.key(),
                values: snapshot.val(),
                count: snapshot.numChildren()
            }
        }

        var _likes = {};
        // value
        _ref.on("value", function (snapshot) {
            var info = getSnapshotInfo(snapshot);
            _.forEach(info.values, function (value, key) {
                _likes[key] = value;
            })

            $log.debug(_likes);
        });
        
        // child_added
        _ref.on("child_added", function (snapshot, prevChildKey) {
            var info = getSnapshotInfo(snapshot);
            $log.debug("Like added:", info);
        });
        
        // child_changed
        _ref.on("child_changed", function (snapshot) {
            var info = getSnapshotInfo(snapshot);
            $log.debug("Like changed:", info);
        });
        
        // child_removed
        _ref.on("child_removed", function (snapshot) {
            var info = getSnapshotInfo(snapshot);
            $log.debug("Like deleted:", info);
        })

        return {
            likes: _likes,
            like: function (post, email) {
                var likes = $firebaseArray(_ref.child(post.$id));
                likes.$loaded()
                    .then(function (likes) {

                    })
                    .catch(function (error) {

                    });
            },
            unlike: function (post, email) {
                var postId = post.$id;
                var unlikeObj = {};
                unlikeObj[postId] = [];

                _ref.update(unlikeObj);
            }
        }
    })

    .factory("AuthSrv", function ($log, $localStorage, $rootScope) {
        // TODO: Auth.$requireAuth();
{'fraction_non_alphanumeric': 0.1303531620321483, 'fraction_numerical': 0.0028159098908834917, 'mean_word_length': 1.4750290360046459, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3780051', 'n_tokens_mistral': 2337, 'n_tokens_neox': 2234, 'n_words': 437}