/app.js
JavaScript | 338 lines | 279 code | 55 blank | 4 comment | 21 complexity | fdbccb73aded470174147cfb1ff7200f MD5 | raw file
- // Generated by CoffeeScript 1.7.1
- /*
- Module dependencies.
- */
- (function() {
- var Image, Subscription, SubscriptionScheme, app, auth, config, express, fs, http, ig, io, media, mediaProcessingBlock, mongoose, path, routes, server, socketIO, socketIOstream;
- express = require("express");
- config = require("./config");
- routes = require("./routes");
- http = require("http");
- path = require("path");
- fs = require("fs");
- mongoose = require("mongoose");
- Image = require("./models/image.js");
- global.config = config;
- ig = require("instagram-node").instagram();
- media = require("./media").media;
- mediaProcessingBlock = false;
- SubscriptionScheme = new mongoose.Schema({
- tag: String,
- additional: String,
- sid: String
- });
- SubscriptionScheme.pre("save", function(next) {
- var counter, self;
- counter = 0;
- self = this;
- ig.add_tag_subscription(this.tag, config.instagram.listen, function(err, result, limit) {
- if (err) {
- console.log(err);
- ++counter;
- if (counter < 5) {
- err.retry();
- } else {
- err = new Error("IG lost connection");
- next(err);
- }
- return;
- }
- self.sid = result.id;
- next();
- });
- });
- SubscriptionScheme.pre("remove", function(next) {
- return ig.del_subscription({
- id: this.sid
- }, function() {
- return next();
- });
- });
- Subscription = mongoose.model("Subscription", SubscriptionScheme);
- global.Subscription = Subscription;
- global.mediaProccessingBlock = mediaProcessingBlock;
- ig.use(config.instagram);
- global.ig = ig;
- app = express();
- app.set("port", process.env.PORT || 4012);
- app.set("views", path.join(__dirname, "views"));
- app.set("view engine", "jade");
- app.use(express.favicon());
- app.use(express.logger("dev"));
- app.use(express.json());
- app.use(express.urlencoded());
- app.use(express.methodOverride());
- app.use(app.router);
- app.use(require("stylus").middleware(path.join(__dirname, "public")));
- app.use(express["static"](path.join(__dirname, "public")));
- mongoose.connect(config.db);
- if ("development" === app.get("env")) {
- app.use(express.errorHandler());
- }
- socketIO = require("socket.io");
- socketIOstream = require("socket.io-stream");
- global.io = socketIO;
- routes.add = require("./routes/add").add;
- routes.gallery = require("./routes/gallery");
- auth = express.basicAuth(function(user, pass) {
- return user === "admin" && pass === "admTBWA";
- });
- app.get("/", auth, function(req, res) {
- var tries;
- tries = 0;
- return ig.subscriptions(function(err, result, limit) {
- var item, tags;
- if (err) {
- console.log("Subscriptions read err:", err);
- ++tries;
- if (tries < 10) {
- err.retry();
- } else {
- res.render("index", {
- title: "Subscriptions",
- subscriptions: []
- });
- }
- } else {
- console.log("Result at 91", result);
- }
- if (result) {
- tags = (function() {
- var _i, _len, _results;
- _results = [];
- for (_i = 0, _len = result.length; _i < _len; _i++) {
- item = result[_i];
- _results.push(item.object_id);
- }
- return _results;
- })();
- Subscription.remove({
- "tag": {
- $nin: tags
- }
- }).exec(function(err) {
- if (err) {
- return console.log(err);
- }
- });
- return Subscription.find({}, function(err, subs) {
- return res.render("index", {
- title: "Subscriptions",
- subscriptions: subs
- });
- });
- }
- });
- });
- app.post("/listen", routes.add);
- app.get("/gallery", routes.gallery);
- app.get("/listen", function(req, res) {
- res.send(200, req.query["hub.challenge"]);
- });
- server = http.createServer(app).listen(app.get("port"), function() {
- console.log("Express server listening on port " + app.get("port"));
- });
- io = socketIO.listen(server);
- io.set("log level", 1);
- global.io = io;
- io.on("newImage", function(data) {
- var call, result;
- console.log(data);
- result = new Array();
- result.push(data.item);
- call = (config.front.moderation ? "toModerateList" : "unprintedList");
- io.sockets.emit(call, result);
- });
- io.on("reprint", function(id, count) {
- Image.find({
- _id: id
- }).exec(function(err, data) {
- var i;
- console.log("Reprinted:", id, count, data);
- if (err) {
- throw err;
- }
- i = 0;
- while (i < count) {
- io.sockets.emit("unprintedList", data);
- i++;
- }
- });
- });
- io.sockets.on("connection", function(socket) {
- console.log("Socket " + socket.id + " connected");
- socket.emit("config", config.front);
- socketIOstream(socket).on("image", function(stream, data) {
- var filename;
- console.log("Socket stream:", data);
- filename = path.basename(data.nname);
- stream.pipe(fs.createWriteStream(config.images.borPrefix + filename));
- });
- Image.find({
- isPrinted: false,
- moderationStatus: 2
- }).exec(function(err, data) {
- socket.emit("unprintedList", data);
- });
- Image.find({
- isPrinted: false,
- moderationStatus: {
- $nin: [1, 2]
- }
- }).exec(function(err, data) {
- socket.emit("toModerateList", data);
- });
- socket.on("newTag", function(data) {
- var sub;
- sub = new Subscription(data);
- sub.save(function(err, result) {
- if (err) {
- console.log(err);
- return;
- }
- io.sockets.emit("newTagAdded", result);
- });
- });
- socket.on("deleteTag", function(data) {
- console.log(data);
- return Subscription.findOne({
- sid: data
- }, function(err, sub) {
- console.log(err, sub);
- return sub.remove(function() {
- console.log(data);
- return io.sockets.emit("deletedTag", data);
- });
- });
- });
- socket.on("initTag", function(tag) {
- media(tag, function(limit) {
- io.sockets.emit("limitUpdate", limit);
- config.front.limit = limit;
- });
- });
- socket.on("clearDb", function() {
- var i;
- i = new Image();
- i.collection.drop(function(err) {
- if (err) {
- throw err;
- }
- console.log("collection dropped");
- io.sockets.emit("dbCleared");
- });
- });
- socket.on("filePrinted", function(id) {
- console.log("File Printed", id);
- Image.find({
- _id: id
- }).exec(function(err, data) {
- console.log("filePrinted");
- if (err) {
- throw err;
- }
- data[0].isPrinted = true;
- data[0].save(function(err) {
- if (err) {
- throw err;
- }
- io.sockets.emit("filePrinted", data[0]);
- });
- });
- });
- socket.on("fileApproved", function(id) {
- Image.findOneAndUpdate({
- _id: id
- }, {
- $set: {
- moderationStatus: 2
- }
- }, function(err, data) {
- var result;
- if (err) {
- throw err;
- }
- io.sockets.emit("fileApproved", id);
- result = new Array();
- result.push(data);
- io.sockets.emit("unprintedList", result);
- });
- });
- socket.on("fileDenied", function(id) {
- Image.findOneAndUpdate({
- _id: id
- }, {
- $set: {
- moderationStatus: 1
- }
- }, function(err, data) {
- if (err) {
- throw err;
- }
- io.sockets.emit("fileDenied", id);
- });
- });
- socket.on("changeModeration", function(val) {
- global.config.front.moderation = val;
- return config.front.moderation = val;
- });
- });
- io.on("error", function(err) {
- console.log("Caught flash policy server socket error: ", err.stack);
- });
- }).call(this);