├── .bowerrc ├── .gitignore ├── .npmignore ├── .travis.yml ├── Gruntfile.js ├── LICENSE ├── Procfile ├── README.md ├── bower.json ├── env.sample ├── lib ├── environment.js ├── hawk.js ├── mapUsernames.js ├── metrics.js ├── middleware.js ├── models │ ├── apiApp.js │ ├── list.js │ └── make.js ├── mongoose.js ├── queryBuilder │ ├── generators │ │ ├── facets.js │ │ ├── generic.js │ │ ├── index.js │ │ ├── likedByUser.js │ │ ├── queries.js │ │ ├── remix.js │ │ ├── search.js │ │ ├── sort.js │ │ └── user.js │ ├── index.js │ └── queryTypes │ │ ├── autocomplete.js │ │ ├── remix.js │ │ └── search.js ├── sanitizer.js ├── strategy.js └── tags.js ├── migrations ├── 20130731-contenturl.js ├── 20130925-locale.js ├── 20131009-updatedAt.js ├── 20140108-ownerApp.js ├── 20140109-remix-edit-url.js └── 20140206-apiApp.js ├── package.json ├── public ├── font │ ├── FontAwesome.otf │ ├── fontawesome-webfont.eot │ ├── fontawesome-webfont.svg │ ├── fontawesome-webfont.ttf │ └── fontawesome-webfont.woff ├── gallery │ ├── index.html │ └── style │ │ └── style.css ├── images │ ├── calendar.gif │ ├── favicon.ico │ └── header-columns-bg.gif ├── index.html ├── js │ ├── admin.js │ ├── external │ │ ├── jquery-ui.min.js │ │ ├── jquery.event.drag-2.2.js │ │ ├── jquery.min.js │ │ ├── slick.core.js │ │ ├── slick.dataview.js │ │ ├── slick.editors.js │ │ ├── slick.grid.js │ │ └── slick.pager.js │ └── login.js └── stylesheets │ ├── admin.css │ ├── bootstrap.min.css │ ├── font-awesome.min.css │ ├── jquery-ui.min.css │ ├── search.css │ ├── slick.grid.css │ └── slick.pager.css ├── routes ├── admin.js ├── index.js ├── list.js └── make.js ├── scripts ├── cleanup.js ├── esindex.js ├── export_to_s3.js ├── generateKeys.js └── mark_deleted_makes.js ├── server.js ├── test ├── index.js └── queryBuilder │ └── search │ ├── complexQueries.unit.js │ ├── core.unit.js │ ├── index.js │ ├── limitSize.unit.js │ ├── page.unit.js │ ├── size.unit.js │ ├── sort.unit.js │ ├── tagFacets.unit.js │ └── termFilters.unit.js └── views ├── admin.html └── login.html /.bowerrc: -------------------------------------------------------------------------------- 1 | { 2 | "directory": "public/bower" 3 | } 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | public/bower/ 3 | scripts/userlookupcache.json 4 | .DS_Store 5 | .env 6 | *~ 7 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | .env 2 | .env.sample 3 | .gitignore 4 | Gruntfile.js 5 | server.js 6 | node_modules/* 7 | public/js/search.js 8 | public/js/README.md 9 | public/js/admin.js 10 | public/js/external/* 11 | public/stylesheets/* 12 | views/* 13 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "0.10" 4 | before_script: 5 | - npm install -g grunt-cli 6 | -------------------------------------------------------------------------------- /Gruntfile.js: -------------------------------------------------------------------------------- 1 | module.exports = function (grunt) { 2 | var jsbeautifyrc = grunt.file.readJSON("node_modules/mofo-style/linters/.jsbeautifyrc"); 3 | var jscsrc = grunt.file.readJSON("node_modules/mofo-style/linters/.jscsrc"); 4 | var jshintrc = grunt.file.readJSON("node_modules/mofo-style/linters/.jshintrc"); 5 | 6 | var javaScriptFiles = [ 7 | "Gruntfile.js", 8 | "server.js", 9 | "lib/**/*.js", 10 | "public/js/*.js", 11 | "routes/**/*.js", 12 | "test/**/*.js" 13 | ]; 14 | 15 | grunt.initConfig({ 16 | pkg: grunt.file.readJSON("package.json"), 17 | 18 | csslint: { 19 | files: [ 20 | "public/gallery/**/*.css", 21 | "public/stylesheets/search.css" 22 | ] 23 | }, 24 | jshint: { 25 | options: jshintrc, 26 | files: javaScriptFiles 27 | }, 28 | jsbeautifier: { 29 | options: { 30 | js: jsbeautifyrc 31 | }, 32 | modify: { 33 | src: javaScriptFiles 34 | }, 35 | verify: { 36 | src: javaScriptFiles, 37 | options: { 38 | mode: "VERIFY_ONLY" 39 | } 40 | } 41 | }, 42 | jscs: { 43 | src: javaScriptFiles, 44 | options: jscsrc 45 | } 46 | }); 47 | 48 | grunt.loadNpmTasks("grunt-contrib-csslint"); 49 | grunt.loadNpmTasks("grunt-contrib-jshint"); 50 | grunt.loadNpmTasks("grunt-jsbeautifier"); 51 | grunt.loadNpmTasks("grunt-jscs"); 52 | 53 | grunt.registerTask("clean", ["jsbeautifier:modify"]); 54 | 55 | grunt.registerTask("validate", ["jsbeautifier:verify", "jshint", "jscs", "csslint"]); 56 | 57 | grunt.registerTask("default", ["validate"]); 58 | }; 59 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Mozilla Public License Version 2.0 2 | ================================== 3 | 4 | 1. Definitions 5 | -------------- 6 | 7 | 1.1. "Contributor" 8 | means each individual or legal entity that creates, contributes to 9 | the creation of, or owns Covered Software. 10 | 11 | 1.2. "Contributor Version" 12 | means the combination of the Contributions of others (if any) used 13 | by a Contributor and that particular Contributor's Contribution. 14 | 15 | 1.3. "Contribution" 16 | means Covered Software of a particular Contributor. 17 | 18 | 1.4. "Covered Software" 19 | means Source Code Form to which the initial Contributor has attached 20 | the notice in Exhibit A, the Executable Form of such Source Code 21 | Form, and Modifications of such Source Code Form, in each case 22 | including portions thereof. 23 | 24 | 1.5. "Incompatible With Secondary Licenses" 25 | means 26 | 27 | (a) that the initial Contributor has attached the notice described 28 | in Exhibit B to the Covered Software; or 29 | 30 | (b) that the Covered Software was made available under the terms of 31 | version 1.1 or earlier of the License, but not also under the 32 | terms of a Secondary License. 33 | 34 | 1.6. "Executable Form" 35 | means any form of the work other than Source Code Form. 36 | 37 | 1.7. "Larger Work" 38 | means a work that combines Covered Software with other material, in 39 | a separate file or files, that is not Covered Software. 40 | 41 | 1.8. "License" 42 | means this document. 43 | 44 | 1.9. "Licensable" 45 | means having the right to grant, to the maximum extent possible, 46 | whether at the time of the initial grant or subsequently, any and 47 | all of the rights conveyed by this License. 48 | 49 | 1.10. "Modifications" 50 | means any of the following: 51 | 52 | (a) any file in Source Code Form that results from an addition to, 53 | deletion from, or modification of the contents of Covered 54 | Software; or 55 | 56 | (b) any new file in Source Code Form that contains any Covered 57 | Software. 58 | 59 | 1.11. "Patent Claims" of a Contributor 60 | means any patent claim(s), including without limitation, method, 61 | process, and apparatus claims, in any patent Licensable by such 62 | Contributor that would be infringed, but for the grant of the 63 | License, by the making, using, selling, offering for sale, having 64 | made, import, or transfer of either its Contributions or its 65 | Contributor Version. 66 | 67 | 1.12. "Secondary License" 68 | means either the GNU General Public License, Version 2.0, the GNU 69 | Lesser General Public License, Version 2.1, the GNU Affero General 70 | Public License, Version 3.0, or any later versions of those 71 | licenses. 72 | 73 | 1.13. "Source Code Form" 74 | means the form of the work preferred for making modifications. 75 | 76 | 1.14. "You" (or "Your") 77 | means an individual or a legal entity exercising rights under this 78 | License. For legal entities, "You" includes any entity that 79 | controls, is controlled by, or is under common control with You. For 80 | purposes of this definition, "control" means (a) the power, direct 81 | or indirect, to cause the direction or management of such entity, 82 | whether by contract or otherwise, or (b) ownership of more than 83 | fifty percent (50%) of the outstanding shares or beneficial 84 | ownership of such entity. 85 | 86 | 2. License Grants and Conditions 87 | -------------------------------- 88 | 89 | 2.1. Grants 90 | 91 | Each Contributor hereby grants You a world-wide, royalty-free, 92 | non-exclusive license: 93 | 94 | (a) under intellectual property rights (other than patent or trademark) 95 | Licensable by such Contributor to use, reproduce, make available, 96 | modify, display, perform, distribute, and otherwise exploit its 97 | Contributions, either on an unmodified basis, with Modifications, or 98 | as part of a Larger Work; and 99 | 100 | (b) under Patent Claims of such Contributor to make, use, sell, offer 101 | for sale, have made, import, and otherwise transfer either its 102 | Contributions or its Contributor Version. 103 | 104 | 2.2. Effective Date 105 | 106 | The licenses granted in Section 2.1 with respect to any Contribution 107 | become effective for each Contribution on the date the Contributor first 108 | distributes such Contribution. 109 | 110 | 2.3. Limitations on Grant Scope 111 | 112 | The licenses granted in this Section 2 are the only rights granted under 113 | this License. No additional rights or licenses will be implied from the 114 | distribution or licensing of Covered Software under this License. 115 | Notwithstanding Section 2.1(b) above, no patent license is granted by a 116 | Contributor: 117 | 118 | (a) for any code that a Contributor has removed from Covered Software; 119 | or 120 | 121 | (b) for infringements caused by: (i) Your and any other third party's 122 | modifications of Covered Software, or (ii) the combination of its 123 | Contributions with other software (except as part of its Contributor 124 | Version); or 125 | 126 | (c) under Patent Claims infringed by Covered Software in the absence of 127 | its Contributions. 128 | 129 | This License does not grant any rights in the trademarks, service marks, 130 | or logos of any Contributor (except as may be necessary to comply with 131 | the notice requirements in Section 3.4). 132 | 133 | 2.4. Subsequent Licenses 134 | 135 | No Contributor makes additional grants as a result of Your choice to 136 | distribute the Covered Software under a subsequent version of this 137 | License (see Section 10.2) or under the terms of a Secondary License (if 138 | permitted under the terms of Section 3.3). 139 | 140 | 2.5. Representation 141 | 142 | Each Contributor represents that the Contributor believes its 143 | Contributions are its original creation(s) or it has sufficient rights 144 | to grant the rights to its Contributions conveyed by this License. 145 | 146 | 2.6. Fair Use 147 | 148 | This License is not intended to limit any rights You have under 149 | applicable copyright doctrines of fair use, fair dealing, or other 150 | equivalents. 151 | 152 | 2.7. Conditions 153 | 154 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted 155 | in Section 2.1. 156 | 157 | 3. Responsibilities 158 | ------------------- 159 | 160 | 3.1. Distribution of Source Form 161 | 162 | All distribution of Covered Software in Source Code Form, including any 163 | Modifications that You create or to which You contribute, must be under 164 | the terms of this License. You must inform recipients that the Source 165 | Code Form of the Covered Software is governed by the terms of this 166 | License, and how they can obtain a copy of this License. You may not 167 | attempt to alter or restrict the recipients' rights in the Source Code 168 | Form. 169 | 170 | 3.2. Distribution of Executable Form 171 | 172 | If You distribute Covered Software in Executable Form then: 173 | 174 | (a) such Covered Software must also be made available in Source Code 175 | Form, as described in Section 3.1, and You must inform recipients of 176 | the Executable Form how they can obtain a copy of such Source Code 177 | Form by reasonable means in a timely manner, at a charge no more 178 | than the cost of distribution to the recipient; and 179 | 180 | (b) You may distribute such Executable Form under the terms of this 181 | License, or sublicense it under different terms, provided that the 182 | license for the Executable Form does not attempt to limit or alter 183 | the recipients' rights in the Source Code Form under this License. 184 | 185 | 3.3. Distribution of a Larger Work 186 | 187 | You may create and distribute a Larger Work under terms of Your choice, 188 | provided that You also comply with the requirements of this License for 189 | the Covered Software. If the Larger Work is a combination of Covered 190 | Software with a work governed by one or more Secondary Licenses, and the 191 | Covered Software is not Incompatible With Secondary Licenses, this 192 | License permits You to additionally distribute such Covered Software 193 | under the terms of such Secondary License(s), so that the recipient of 194 | the Larger Work may, at their option, further distribute the Covered 195 | Software under the terms of either this License or such Secondary 196 | License(s). 197 | 198 | 3.4. Notices 199 | 200 | You may not remove or alter the substance of any license notices 201 | (including copyright notices, patent notices, disclaimers of warranty, 202 | or limitations of liability) contained within the Source Code Form of 203 | the Covered Software, except that You may alter any license notices to 204 | the extent required to remedy known factual inaccuracies. 205 | 206 | 3.5. Application of Additional Terms 207 | 208 | You may choose to offer, and to charge a fee for, warranty, support, 209 | indemnity or liability obligations to one or more recipients of Covered 210 | Software. However, You may do so only on Your own behalf, and not on 211 | behalf of any Contributor. You must make it absolutely clear that any 212 | such warranty, support, indemnity, or liability obligation is offered by 213 | You alone, and You hereby agree to indemnify every Contributor for any 214 | liability incurred by such Contributor as a result of warranty, support, 215 | indemnity or liability terms You offer. You may include additional 216 | disclaimers of warranty and limitations of liability specific to any 217 | jurisdiction. 218 | 219 | 4. Inability to Comply Due to Statute or Regulation 220 | --------------------------------------------------- 221 | 222 | If it is impossible for You to comply with any of the terms of this 223 | License with respect to some or all of the Covered Software due to 224 | statute, judicial order, or regulation then You must: (a) comply with 225 | the terms of this License to the maximum extent possible; and (b) 226 | describe the limitations and the code they affect. Such description must 227 | be placed in a text file included with all distributions of the Covered 228 | Software under this License. Except to the extent prohibited by statute 229 | or regulation, such description must be sufficiently detailed for a 230 | recipient of ordinary skill to be able to understand it. 231 | 232 | 5. Termination 233 | -------------- 234 | 235 | 5.1. The rights granted under this License will terminate automatically 236 | if You fail to comply with any of its terms. However, if You become 237 | compliant, then the rights granted under this License from a particular 238 | Contributor are reinstated (a) provisionally, unless and until such 239 | Contributor explicitly and finally terminates Your grants, and (b) on an 240 | ongoing basis, if such Contributor fails to notify You of the 241 | non-compliance by some reasonable means prior to 60 days after You have 242 | come back into compliance. Moreover, Your grants from a particular 243 | Contributor are reinstated on an ongoing basis if such Contributor 244 | notifies You of the non-compliance by some reasonable means, this is the 245 | first time You have received notice of non-compliance with this License 246 | from such Contributor, and You become compliant prior to 30 days after 247 | Your receipt of the notice. 248 | 249 | 5.2. If You initiate litigation against any entity by asserting a patent 250 | infringement claim (excluding declaratory judgment actions, 251 | counter-claims, and cross-claims) alleging that a Contributor Version 252 | directly or indirectly infringes any patent, then the rights granted to 253 | You by any and all Contributors for the Covered Software under Section 254 | 2.1 of this License shall terminate. 255 | 256 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all 257 | end user license agreements (excluding distributors and resellers) which 258 | have been validly granted by You or Your distributors under this License 259 | prior to termination shall survive termination. 260 | 261 | ************************************************************************ 262 | * * 263 | * 6. Disclaimer of Warranty * 264 | * ------------------------- * 265 | * * 266 | * Covered Software is provided under this License on an "as is" * 267 | * basis, without warranty of any kind, either expressed, implied, or * 268 | * statutory, including, without limitation, warranties that the * 269 | * Covered Software is free of defects, merchantable, fit for a * 270 | * particular purpose or non-infringing. The entire risk as to the * 271 | * quality and performance of the Covered Software is with You. * 272 | * Should any Covered Software prove defective in any respect, You * 273 | * (not any Contributor) assume the cost of any necessary servicing, * 274 | * repair, or correction. This disclaimer of warranty constitutes an * 275 | * essential part of this License. No use of any Covered Software is * 276 | * authorized under this License except under this disclaimer. * 277 | * * 278 | ************************************************************************ 279 | 280 | ************************************************************************ 281 | * * 282 | * 7. Limitation of Liability * 283 | * -------------------------- * 284 | * * 285 | * Under no circumstances and under no legal theory, whether tort * 286 | * (including negligence), contract, or otherwise, shall any * 287 | * Contributor, or anyone who distributes Covered Software as * 288 | * permitted above, be liable to You for any direct, indirect, * 289 | * special, incidental, or consequential damages of any character * 290 | * including, without limitation, damages for lost profits, loss of * 291 | * goodwill, work stoppage, computer failure or malfunction, or any * 292 | * and all other commercial damages or losses, even if such party * 293 | * shall have been informed of the possibility of such damages. This * 294 | * limitation of liability shall not apply to liability for death or * 295 | * personal injury resulting from such party's negligence to the * 296 | * extent applicable law prohibits such limitation. Some * 297 | * jurisdictions do not allow the exclusion or limitation of * 298 | * incidental or consequential damages, so this exclusion and * 299 | * limitation may not apply to You. * 300 | * * 301 | ************************************************************************ 302 | 303 | 8. Litigation 304 | ------------- 305 | 306 | Any litigation relating to this License may be brought only in the 307 | courts of a jurisdiction where the defendant maintains its principal 308 | place of business and such litigation shall be governed by laws of that 309 | jurisdiction, without reference to its conflict-of-law provisions. 310 | Nothing in this Section shall prevent a party's ability to bring 311 | cross-claims or counter-claims. 312 | 313 | 9. Miscellaneous 314 | ---------------- 315 | 316 | This License represents the complete agreement concerning the subject 317 | matter hereof. If any provision of this License is held to be 318 | unenforceable, such provision shall be reformed only to the extent 319 | necessary to make it enforceable. Any law or regulation which provides 320 | that the language of a contract shall be construed against the drafter 321 | shall not be used to construe this License against a Contributor. 322 | 323 | 10. Versions of the License 324 | --------------------------- 325 | 326 | 10.1. New Versions 327 | 328 | Mozilla Foundation is the license steward. Except as provided in Section 329 | 10.3, no one other than the license steward has the right to modify or 330 | publish new versions of this License. Each version will be given a 331 | distinguishing version number. 332 | 333 | 10.2. Effect of New Versions 334 | 335 | You may distribute the Covered Software under the terms of the version 336 | of the License under which You originally received the Covered Software, 337 | or under the terms of any subsequent version published by the license 338 | steward. 339 | 340 | 10.3. Modified Versions 341 | 342 | If you create software not governed by this License, and you want to 343 | create a new license for such software, you may create and use a 344 | modified version of this License if you rename the license and remove 345 | any references to the name of the license steward (except to note that 346 | such modified license differs from this License). 347 | 348 | 10.4. Distributing Source Code Form that is Incompatible With Secondary 349 | Licenses 350 | 351 | If You choose to distribute Source Code Form that is Incompatible With 352 | Secondary Licenses under the terms of this version of the License, the 353 | notice described in Exhibit B of this License must be attached. 354 | 355 | Exhibit A - Source Code Form License Notice 356 | ------------------------------------------- 357 | 358 | This Source Code Form is subject to the terms of the Mozilla Public 359 | License, v. 2.0. If a copy of the MPL was not distributed with this 360 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 361 | 362 | If it is not possible or desirable to put the notice in a particular 363 | file, then You may include the notice in a location (such as a LICENSE 364 | file in a relevant directory) where a recipient would be likely to look 365 | for such a notice. 366 | 367 | You may add additional accurate notices of copyright ownership. 368 | 369 | Exhibit B - "Incompatible With Secondary Licenses" Notice 370 | --------------------------------------------------------- 371 | 372 | This Source Code Form is "Incompatible With Secondary Licenses", as 373 | defined by the Mozilla Public License, v. 2.0. 374 | -------------------------------------------------------------------------------- /Procfile: -------------------------------------------------------------------------------- 1 | web: node server.js 2 | worker: node scripts/export_to_s3.js 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ##### This project is no longer under active development 2 | 3 | [![Build Status](https://travis-ci.org/mozilla/webmaker.org.png)](https://travis-ci.org/mozilla/MakeAPI) 4 | [![Dependency Status](https://gemnasium.com/mozilla/webmaker.org.png)](https://gemnasium.com/mozilla/MakeAPI) 5 | 6 | # MakeAPI (core) 7 | The MakeAPI is a node.js based service for storing and exposing metadata about user created web content, called "makes". It provides consumers with an API to Create, Update, Delete, and Search for metadata about a make. 8 | 9 | **NOTE: This README assumes that you have all the required external dependencies installed and have a working dev environment. New to Webmaker? Make sure to read our developer guide for everything you need in order to get started!** 10 | 11 | ## 1. Installing & Running the Server 12 | 13 | ### Dependencies 14 | 15 | - [MongoDB](http://www.mongodb.org/) 16 | - *Mac OS X*: Run this with `mongod` 17 | - *Ubuntu-based linux systems.*: Run this with `sudo service mongodb start` 18 | - [ElasticSearch](http://www.elasticsearch.org/) 19 | - *Mac OS X*: Run this with `elasticsearch -f` 20 | - *Ubuntu-based linux systems.*: Run this with `sudo service elasticsearch start` 21 | - [Login server](https://github.com/mozilla/login.webmaker.org) 22 | 23 | 24 | ### Installation 25 | 26 | 1. Clone the git repository with `git clone https://github.com/mozilla/MakeAPI.git` 27 | 2. Execute `npm install` in the application directory. 28 | 3. In the root directory of the application, copy `env.sample` to a new file called `.env` 29 | 30 | **NOTE**: The `.env` file contains settings for various aspects of the MakeAPI server's operation. The default settings should be enough for local development. For non-standard configurations, you may need to adjust where variables point to match the locations of your external dependancies. See the [ENV file reference](https://github.com/mozilla/MakeAPI/wiki/ENV-File-Reference) for more details. 31 | 32 | #### Running the Node Server 33 | 34 | 1. Ensure all external dependencies are running 35 | 2. Run `node server.js` from the root of the MakeAPI application 36 | 37 | By default the server will run at http://localhost:5000. You can change this by adding PORT= to your .env file. 38 | 39 | #### Clearing make data 40 | 41 | Clear elastic search: 42 | 43 | `curl -XDELETE "/"` 44 | 45 | Find your mongo files and clear them. 46 | 47 | 1. In a terminal run `mongo` add the --host flag to connect to a remote mongo service 48 | 2. From the mongo shell run `use makeapi` to switch to the makeapi index 49 | 3. Run `db.dropDatabase()` to remove all existing make data and API keys from mongoDB 50 | 51 | ## 2. API Documentation 52 | 53 | API documentation can be found at [https://mozilla.github.io/makeapi-docs/](https://mozilla.github.io/makeapi-docs/) 54 | 55 | The [makeapi-client JavaScript library](https://github.com/mozilla/makeapi-client) can be used to facilitate interaction with the API in web browser and node contexts. Documentation for the client library can be found at [http://mozilla.github.io/makeapi-docs/client-docs/](http://mozilla.github.io/makeapi-docs/client-docs/) 56 | 57 | **The Make API Does not sanitize Data it receives or outputs, so it is up to consumer applications to sanitize data appropriately.** 58 | 59 | ## 3. Resources 60 | 61 | ### Env Variable Reference Section 62 | All the environment variables are listed and detailed here: [https://mozilla.github.io/makeapi-docs/#configuration](https://mozilla.github.io/makeapi-docs/#configuration) 63 | 64 | ## 4. Testing 65 | ### How to test 66 | We use a combination of technologies to "lint" and test our CSS and JavaScript code. These tests **must** pass in order for a pull request to be merged into the Mozilla repository. To run them locally, 67 | 68 | 1. Navigate to the root folder of the MakeAPI server 69 | 2. Run `npm test` 70 | 71 | ### TravisCI 72 | When a pull request is made to the Mozilla repository, it is automatically scheduled for testing on the [Travis-CI continuous-integration platform](https://travis-ci.org/). This verifies that the code passes linting requirements as well as all of its unit tests. You can see the status of these tests on the Github page for the pull request, and on the MakeAPI travisCI page. 73 | 74 | ### Updating tests 75 | Most developers won't need to update the tests, but changes to certain parts of the MakeAPI require that the tests be revised. Keeping these tests accurate is essential for easy maintenence of this code base, and pull requests that change these parts will be rejected without proper unit tests. 76 | 77 | If you need help understanding the unit tests, hop on the #webmaker IRC channel and we'll be happy to help! No idea what IRC is? Check out our [IRC guide](https://wiki.mozilla.org/IRC). 78 | 79 | ## 5. Accessing the MakeAPI with your service 80 | 81 | The MakeAPI uses [HAWK](https://github.com/hueniverse/hawk) to authenticate MakeAPI calls. Hawk is an HTTP authentication scheme that can be used to verify the authenticity of messages using cryptographically generated message authentication codes (MACs). Hawk does not provide Transport Layer security, and only serves to identify the sender and verify message integrity. 82 | 83 | All applications that wish to make authenticated calls must be issued a pair of keys to sign all requests. Keys are optional for search requests. 84 | 85 | ### Authentication in a dev environment 86 | For convenience of development and testing, the `USE_DEV_KEY_LOOKUP` variable can be set to true in the environment file. This flag will use a **DEVELOPMENT ONLY** strategy when verifying keys. 87 | 88 | When development key mode is enabled, clients can sign their requests by passing hawk `"00000000-0000-0000-000000000000" as their public and private key. Any other key combination will fail to authenticate. 89 | 90 | **DO NOT USE DEV KEYS OUTSIDE OF A DEVELOPMENT ENVIRONMENT!** 91 | 92 | ### Generating keys for production or staging servers w/ `generateKeys.js` 93 | 94 | The generation of keys can be done using the [generateKeys](https://github.com/mozilla/MakeAPI/blob/master/scripts/generateKeys.js) script. 95 | 96 | The script is called with two arguments: an email address to associate with the keys and a integer indicating the number of pairs to be generated. Generated keys are added to the database and then outputted to the console. 97 | 98 | ### Generating keys in the admin console 99 | 100 | There is a tool in the Admin Make Editor that generates keys. It can be reached by visiting the /admin page of the MakeAPI in your browser. You must have a Webmaker admin account on the login server that the MakeAPI is using for authentication. 101 | 102 | ## 6. Metrics and logging 103 | The MakeAPI server uses a number of technologies, like [STATSD](https://github.com/etsy/statsd/) and [New Relic](http://newrelic.com/), to optionally collect and analyze useful performance data. For local development, this shouldn't be a concern. 104 | 105 | For more information on configuring the MakeAPI server's New Relic module, see: https://github.com/newrelic/node-newrelic/#configuring-the-agent 106 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "makeapi.webmaker.org", 3 | "dependencies": { 4 | "nav-global": "https://github.com/mozilla/nav-global/archive/v0.1.4.tar.gz", 5 | "persona-btn": "~0.0.6", 6 | "webmaker-auth-client": "0.1.11" 7 | }, 8 | "version": "1.2.18" 9 | } 10 | -------------------------------------------------------------------------------- /env.sample: -------------------------------------------------------------------------------- 1 | # A Secret used to sign Session cookies. 2 | export SESSION_SECRET='dummy secret value' 3 | 4 | # domain for the super cookie - don't use for local dev 5 | # COOKIE_DOMAIN=webmaker.org 6 | 7 | # Port to listen on 8 | export PORT=5000 9 | 10 | # development or production 11 | export NODE_ENV='development' 12 | 13 | # URL of the Mongodb instance 14 | export MONGO_URL='mongodb://localhost/makeapi' 15 | 16 | # Host and port for your Elastic search cluster 17 | export ELASTIC_SEARCH_URL='http://localhost:9200' 18 | 19 | # Persona 20 | export AUDIENCE="http://localhost:7777" 21 | 22 | # statsd metrics collection. If the following are left empty, no stats 23 | # will be collected or sent to a server. Only STATSD_HOST and STATSD_PORT 24 | # are required. STATSD_PREFIX is an optional prefix for all stats (defaults 25 | # to "development.makeapi" or "production.makeapi" if left blank). 26 | export STATSD_HOST= 27 | export STATSD_PORT= 28 | export STATSD_PREFIX= 29 | 30 | # This is used to check if a user is an administrator (include user/password) 31 | # in URL. Don't use this username/password combo 32 | export LOGIN_SERVER_URL_WITH_AUTH="http://testuser:password@localhost:3000" 33 | 34 | # Is this server running behind an SSL-enabled load-balancer? 35 | export FORCE_SSL=false 36 | 37 | # Login server URL without auth for webmaker SSO 38 | export LOGIN_SERVER='http://localhost:3000' 39 | 40 | # Use the development credential lookup strategy 41 | # ONLY use in development environments 42 | # Set your public and private keys to "00000000-0000-0000-000000000000" for all requests that should authenticate successfully 43 | export USE_DEV_KEY_LOOKUP=true 44 | 45 | export ENABLE_GELF_LOGS=false 46 | 47 | export ENFORCE_WRITE_PERMISSIONS=false 48 | 49 | # AWS credentials used for hatchet 50 | AWS_ACCESS_KEY_ID= 51 | AWS_SECRET_ACCESS_KEY= 52 | 53 | # hatchet configuration 54 | HATCHET_APP_NAME= 55 | HATCHET_QUEUE_REGION= 56 | HATCHET_QUEUE_URL= 57 | -------------------------------------------------------------------------------- /lib/environment.js: -------------------------------------------------------------------------------- 1 | module.exports = (function () { 2 | var Habitat = require("habitat"); 3 | Habitat.load(require("path").resolve(__dirname, "../.env")); 4 | return new Habitat(); 5 | }()); 6 | -------------------------------------------------------------------------------- /lib/hawk.js: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | module.exports = function () { 6 | var Hawk = require("hawk"); 7 | 8 | return { 9 | Hawk: Hawk, 10 | respond: function (code, res, creds, artifacts, payload, contentType) { 11 | payload = JSON.stringify(payload); 12 | 13 | var headers = { 14 | "Content-Type": contentType 15 | }, 16 | header = Hawk.server.header(creds, artifacts, { 17 | payload: payload, 18 | contentType: contentType 19 | }); 20 | 21 | headers["Server-Authorization"] = header; 22 | 23 | res.writeHead(code, headers); 24 | res.end(payload); 25 | } 26 | }; 27 | }; 28 | -------------------------------------------------------------------------------- /lib/mapUsernames.js: -------------------------------------------------------------------------------- 1 | var env = require("../lib/environment"), 2 | hyperquest = require("hyperquest"), 3 | sanitize = require("./sanitizer"), 4 | url = require("url"); 5 | 6 | var getByEmailsURL = url.resolve(env.get("LOGIN_SERVER_URL_WITH_AUTH"), "/usernames"); 7 | 8 | module.exports = function (Make) { 9 | return function (makes, callback) { 10 | var emails = makes.slice().map(function (hit) { 11 | return hit._source.email; 12 | }).filter(function (email, pos, self) { 13 | return self.indexOf(email) === pos; 14 | }); 15 | 16 | var get = hyperquest.post(getByEmailsURL, { 17 | headers: { 18 | "Content-Type": "application/json" 19 | } 20 | }); 21 | 22 | get.on("error", function (err) { 23 | callback(new Error("Error fetching usernames: Login API request failed")); 24 | }); 25 | 26 | get.on("response", function (resp) { 27 | if (resp.statusCode !== 200) { 28 | return callback(new Error("Error fetching usernames: Received a status code of " + resp.statusCode)); 29 | } 30 | var bodyParts = [], 31 | bytes = 0; 32 | resp.on("data", function (data) { 33 | bodyParts.push(data); 34 | bytes += data.length; 35 | }); 36 | 37 | resp.on("end", function () { 38 | var responseBody = Buffer.concat(bodyParts, bytes).toString("utf8"), 39 | mappedUsers; 40 | 41 | try { 42 | mappedUsers = JSON.parse(responseBody); 43 | } catch (e) { 44 | return callback(new Error("Error fetching usernames: Unable to parse Login API response body")); 45 | } 46 | 47 | makes = makes.map(function (esMake) { 48 | var safeMake = {}, 49 | source = esMake._source, 50 | userData = mappedUsers[source.email]; 51 | 52 | Make.publicFields.forEach(function (val) { 53 | safeMake[val] = source[val]; 54 | }); 55 | 56 | // _id, createdAt and updatedAt are not a part of our public fields. 57 | // We need to manually assign it to the object we are returning 58 | safeMake._id = esMake._id; 59 | safeMake.createdAt = source.createdAt; 60 | safeMake.updatedAt = source.updatedAt; 61 | 62 | safeMake.tags = source.tags.map(sanitize); 63 | 64 | if (userData) { 65 | // Attach the Maker's username and return the result 66 | safeMake.username = userData.username; 67 | safeMake.emailHash = userData.emailHash; 68 | } else { 69 | // The user account was likely deleted. 70 | // We need cascading delete, so that this code will only be hit on rare circumstances 71 | // cron jobs can be used to clean up anything that slips through the cracks. 72 | safeMake.username = ""; 73 | safeMake.emailHash = ""; 74 | } 75 | return safeMake; 76 | }); 77 | callback(null, makes); 78 | }); 79 | }); 80 | get.end(JSON.stringify(emails), "utf8"); 81 | }; 82 | }; 83 | -------------------------------------------------------------------------------- /lib/metrics.js: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | var env = require("./environment"); 6 | 7 | var options = { 8 | host: env.get("STATSD_HOST"), 9 | port: env.get("STATSD_PORT"), 10 | prefix: env.get("STATSD_PREFIX") || env.get("NODE_ENV") + ".makeapi", 11 | // If we don't have a host configured, use a mock object (no stats sent). 12 | mock: !env.get("STATSD_HOST") 13 | }; 14 | 15 | module.exports = new(require("node-statsd").StatsD)(options); 16 | -------------------------------------------------------------------------------- /lib/middleware.js: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | var http = require("http"), 6 | hyperquest = require("hyperquest"), 7 | url = require("url"), 8 | env = require("./environment"), 9 | hawkModule = require("./hawk")(), 10 | tags = require("./tags")(); 11 | 12 | module.exports = function (makeModel, apiAppModel, List) { 13 | var Make = makeModel, 14 | hawkOptions = {}, 15 | credentialsLookupStrategy = require("./strategy")(apiAppModel, env.get("USE_DEV_KEY_LOOKUP")), 16 | LOGIN_API = env.get("LOGIN_SERVER_URL_WITH_AUTH"); 17 | 18 | // API Key write permissions feature flag 19 | var ENFORCE_WRITE_PERMISSIONS = env.get("ENFORCE_WRITE_PERMISSIONS"); 20 | 21 | // fields that collaborator accounts can update 22 | var COLLABORATOR_FIELDS = [ 23 | "tags" 24 | ]; 25 | 26 | if (env.get("FORCE_SSL")) { 27 | hawkOptions.port = 443; 28 | } 29 | 30 | function findUserElem(id, array) { 31 | var elem; 32 | for (var i = array.length - 1; i >= 0; i--) { 33 | elem = array[i]; 34 | if (elem.userId === id) { 35 | return elem; 36 | } 37 | } 38 | return null; 39 | } 40 | 41 | function userRequest(path, callback) { 42 | var get = hyperquest({ 43 | headers: { 44 | "Content-Type": "application/json" 45 | }, 46 | uri: url.resolve(LOGIN_API, "/user/" + path) 47 | }); 48 | get.on("error", callback); 49 | get.on("response", function (resp) { 50 | if (resp.statusCode !== 200) { 51 | return callback({ 52 | error: "There was an error on the login server", 53 | statusCode: resp.statusCode 54 | }); 55 | } 56 | 57 | var bodyParts = []; 58 | var bytes = 0; 59 | resp.on("data", function (data) { 60 | bodyParts.push(data); 61 | bytes += data.length; 62 | }); 63 | 64 | resp.on("end", function () { 65 | var body = Buffer.concat(bodyParts, bytes).toString("utf8"); 66 | var json; 67 | 68 | try { 69 | json = JSON.parse(body); 70 | } catch (e) { 71 | return callback({ 72 | error: "Error parsing login server response" 73 | }); 74 | } 75 | 76 | callback(null, json.user); 77 | }); 78 | }); 79 | } 80 | 81 | return { 82 | canTag: function (req, res, next) { 83 | var tag = req.params.tag; 84 | if (!tag) { 85 | return hawkModule.respond(400, res, req.credentials, req.artifacts, { 86 | status: "failure", 87 | reason: "tag missing" 88 | }, "application/json"); 89 | } 90 | 91 | if (!tags.isTagChangeAllowed(tag, req.user, req.credentials.admin)) { 92 | return hawkModule.respond(401, res, req.credentials, req.artifacts, { 93 | status: "failure", 94 | reason: "unauthorized" 95 | }, "application/json"); 96 | } 97 | 98 | return process.nextTick(next); 99 | }, 100 | validateAppTags: function (req, res, next) { 101 | var makeTags = req.body.tags, 102 | validTags = []; 103 | 104 | if (!makeTags) { 105 | return next(); 106 | } 107 | 108 | if (makeTags && makeTags.length) { 109 | validTags = tags.validateTags(makeTags, req.user); 110 | } 111 | 112 | // keep app tags 113 | if (req.make) { 114 | validTags = validTags.concat(req.make.appTags); 115 | } 116 | 117 | // update the body of the request with the approved tags, filtering for duplicates 118 | req.body.tags = validTags.filter(function (tag, pos, arr) { 119 | return arr.indexOf(tag) === pos; 120 | }); 121 | 122 | next(); 123 | }, 124 | adminAuth: function (req, res, next) { 125 | var id = (req.session && req.session.user) ? req.session.user.id : ""; 126 | if (id) { 127 | userRequest("id/" + id, function (err, user) { 128 | if (err) { 129 | return next(err); 130 | } 131 | if (!user || !user.isAdmin) { 132 | return res.redirect(302, "/login"); 133 | } 134 | req.hatchet.adminUser = user; 135 | next(); 136 | }); 137 | } else { 138 | res.redirect(302, "/login"); 139 | } 140 | }, 141 | collabAuth: function (req, res, next) { 142 | var id = (req.session && req.session.user) ? req.session.user.id : ""; 143 | if (id) { 144 | userRequest("id/" + id, function (err, user) { 145 | if (err) { 146 | return next(err); 147 | } 148 | if (!user.isCollaborator && !user.isAdmin) { 149 | return res.redirect(302, "/login"); 150 | } 151 | req.isCollab = user.isCollaborator; 152 | // check if an event is associated with this call 153 | if (req.hatchet) { 154 | req.hatchet.adminUser = user; 155 | } 156 | next(); 157 | }); 158 | } else { 159 | res.redirect(302, "/login"); 160 | } 161 | }, 162 | hawkAuth: function (req, res, next) { 163 | hawkModule.Hawk.server.authenticate(req, 164 | credentialsLookupStrategy, 165 | hawkOptions, 166 | function (err, creds, artifacts) { 167 | if (err) { 168 | return hawkModule.respond(401, res, creds, artifacts, { 169 | status: "failure", 170 | reason: "Unauthorized" 171 | }, "application/json"); 172 | } 173 | req.credentials = creds; 174 | req.artifacts = artifacts; 175 | next(); 176 | } 177 | ); 178 | }, 179 | isHawkAdmin: function (req, res, next) { 180 | if (req.credentials.admin) { 181 | return next(); 182 | } 183 | hawkModule.respond(401, res, req.credentials, req.artifacts, { 184 | status: "failure", 185 | reason: "Insufficient permissions" 186 | }, "application/json"); 187 | }, 188 | getMake: function (req, res, next) { 189 | if (!req.params.id) { 190 | return hawkModule.respond(400, res, req.credentials, req.artifacts, { 191 | status: "failure", 192 | reason: "ID missing" 193 | }, "application/json"); 194 | } 195 | Make.findById(req.params.id).where("deletedAt", null).exec(function (err, make) { 196 | if (err) { 197 | if (err.name === "CastError") { 198 | return hawkModule.respond(400, res, req.credentials, req.artifacts, { 199 | status: "failure", 200 | reason: "The supplied value does not look like a Make ID." 201 | }, "application/json"); 202 | } else { 203 | return hawkModule.respond(500, res, req.credentials, req.artifacts, { 204 | status: "failure", 205 | reason: err.toString() 206 | }, "application/json"); 207 | } 208 | } 209 | if (!make) { 210 | return hawkModule.respond(400, res, req.credentials, req.artifacts, { 211 | status: "failure", 212 | reason: "Make Does Not Exist" 213 | }, "application/json"); 214 | } 215 | req.make = make; 216 | next(); 217 | }); 218 | }, 219 | report: function (req, res, next) { 220 | var make = req.make; 221 | userRequest("username/" + req.body.maker, function (err, user) { 222 | if (err) { 223 | return next(err); 224 | } 225 | var report = findUserElem(user.id, make.reports); 226 | if (!report) { 227 | make.reports.push({ 228 | userId: user.id 229 | }); 230 | req.user = user; 231 | next(); 232 | } else { 233 | next({ 234 | status: 400, 235 | message: "User already reported this make" 236 | }); 237 | } 238 | }); 239 | }, 240 | cancelReport: function (req, res, next) { 241 | var make = req.make; 242 | 243 | userRequest("username/" + req.body.maker, function (err, user) { 244 | if (err) { 245 | return next(err); 246 | } 247 | var report = findUserElem(user.id, make.reports); 248 | if (report) { 249 | make.reports.splice(make.reports.indexOf(report), 1); 250 | req.user = user; 251 | next(); 252 | } else { 253 | next({ 254 | status: 400, 255 | message: "User has not reported this make" 256 | }); 257 | } 258 | }); 259 | }, 260 | fieldFilter: function (req, res, next) { 261 | var sanitizedUpdate = {}; 262 | if (req.isCollab) { 263 | COLLABORATOR_FIELDS.forEach(function (safeField) { 264 | sanitizedUpdate[safeField] = req.body[safeField]; 265 | }); 266 | req.body = sanitizedUpdate; 267 | } 268 | next(); 269 | }, 270 | like: function (req, res, next) { 271 | var make = req.make; 272 | userRequest("username/" + req.body.maker, function (err, user) { 273 | if (err) { 274 | return next(err); 275 | } 276 | var userLike = findUserElem(user.id, make.likes); 277 | if (!userLike) { 278 | make.likes.push({ 279 | userId: user.id 280 | }); 281 | req.user = user; 282 | next(); 283 | } else { 284 | next({ 285 | status: 400, 286 | message: "User already Likes" 287 | }); 288 | } 289 | }); 290 | }, 291 | unlike: function (req, res, next) { 292 | var make = req.make; 293 | userRequest("username/" + req.body.maker, function (err, user) { 294 | if (err) { 295 | return next(err); 296 | } 297 | var userLike = findUserElem(user.id, make.likes); 298 | if (userLike) { 299 | make.likes.splice(make.likes.indexOf(userLike), 1); 300 | req.user = user; 301 | next(); 302 | } else { 303 | next({ 304 | status: 400, 305 | message: "User does not like" 306 | }); 307 | } 308 | }); 309 | }, 310 | // modelName must be "make" or "list" only 311 | checkOwnerApp: function (modelName) { 312 | if (["make", "list"].indexOf(modelName) === -1) { 313 | throw new Error( 314 | "checkOwnerApp middleware can only be configured with 'make' or 'list'. You passed in: '" + 315 | modelName + 316 | "'" 317 | ); 318 | } 319 | 320 | return function (req, res, next) { 321 | // Don't enforce write permissions if not enabled. 322 | if (!ENFORCE_WRITE_PERMISSIONS) { 323 | return next(); 324 | } 325 | 326 | var model = req[modelName], 327 | user = req.credentials.user; 328 | // check if the authenticated application has admin permissions, or if it owns the make 329 | if (req.credentials.admin !== true && user !== model.ownerApp) { 330 | return hawkModule.respond(403, res, req.credentials, req.artifacts, { 331 | status: "failure", 332 | reason: "unauthorized" 333 | }, "application/json"); 334 | } 335 | next(); 336 | }; 337 | }, 338 | getUser: function (req, res, next) { 339 | var email; 340 | // Support older POST/PUT body formats for Makes 341 | if (req.body.maker && req.body.make) { 342 | req.body = req.body.make; 343 | } 344 | 345 | if (req.body.email) { 346 | // This is a new Make 347 | email = req.body.email; 348 | } else { 349 | // This is an update/delete 350 | email = req.make.email; 351 | } 352 | 353 | userRequest("email/" + email, function (err, user) { 354 | if (err) { 355 | return next(err); 356 | } 357 | req.user = user; 358 | req.hatchet.userId = user.id; 359 | next(); 360 | }); 361 | }, 362 | getRemixMake: function (req, res, next) { 363 | if (!req.body.remixedFrom) { 364 | return next(); 365 | } 366 | Make.findById(req.body.remixedFrom).exec(function (err, make) { 367 | if (err) { 368 | return next(err); 369 | } 370 | 371 | req.hatchet.remixMake = make; 372 | 373 | // we gotta look it up (username).. 374 | userRequest("email/" + make.email, function (err, user) { 375 | if (err) { 376 | if (err.statusCode !== 404) { 377 | return next(new Error(err.error)); 378 | } 379 | // don't fail on an account that's been deleted 380 | return next(); 381 | } 382 | 383 | req.hatchet.remixUser = user; 384 | next(); 385 | }); 386 | }); 387 | }, 388 | setHatchetEventType: function (type) { 389 | return function (req, res, next) { 390 | req.hatchet = { 391 | type: type 392 | }; 393 | next(); 394 | }; 395 | }, 396 | getList: function (req, res, next) { 397 | if (!req.params.id) { 398 | return hawkModule.respond(400, res, req.credentials, req.artifacts, { 399 | status: "failure", 400 | reason: "ID missing" 401 | }, "application/json"); 402 | } 403 | List.findById(req.params.id, function (err, list) { 404 | if (err) { 405 | if (err.name === "CastError") { 406 | return hawkModule.respond(400, res, req.credentials, req.artifacts, { 407 | status: "failure", 408 | reason: "The supplied value does not look like a List ID." 409 | }, "application/json"); 410 | } else { 411 | return hawkModule.respond(500, res, req.credentials, req.artifacts, { 412 | status: "failure", 413 | reason: err.toString() 414 | }, "application/json"); 415 | } 416 | } 417 | if (!list) { 418 | return hawkModule.respond(400, res, req.credentials, req.artifacts, { 419 | status: "failure", 420 | reason: "List Does Not Exist" 421 | }, "application/json"); 422 | } 423 | req.list = list; 424 | next(); 425 | }); 426 | }, 427 | getListCreator: function (req, res, next) { 428 | var id; 429 | 430 | if (req.body && req.body.userId) { 431 | id = req.body.userId; 432 | } else if (req.list) { 433 | id = req.list.userId; 434 | } else { 435 | return next(new Error("List Creator ID cannot be found. Check the request parameters")); 436 | } 437 | 438 | userRequest("id/" + id, function (err, user) { 439 | if (err) { 440 | return next(err); 441 | } 442 | 443 | req.user = user; 444 | next(); 445 | }); 446 | }, 447 | crossOrigin: function (req, res, next) { 448 | res.header("Access-Control-Allow-Origin", "*"); 449 | next(); 450 | }, 451 | errorHandler: function (err, req, res, next) { 452 | if (typeof err === "string") { 453 | console.error("You're passing a string into next(). Go fix this: %s", err); 454 | } 455 | 456 | var error = { 457 | message: JSON.stringify(err), 458 | status: http.STATUS_CODES[err.status] ? err.status : 500 459 | }; 460 | 461 | res.status(error.status); 462 | res.json(error); 463 | }, 464 | fourOhFourHandler: function (req, res, next) { 465 | var err = { 466 | message: "You found a loose thread!", 467 | status: 404 468 | }; 469 | 470 | res.status(err.status); 471 | res.json(err); 472 | } 473 | }; 474 | }; 475 | -------------------------------------------------------------------------------- /lib/models/apiApp.js: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | module.exports = function (mongoose) { 6 | var validate = require("mongoose-validator").validate; 7 | 8 | var schema = new mongoose.Schema({ 9 | privatekey: { 10 | type: String, 11 | required: true, 12 | unique: true 13 | }, 14 | publickey: { 15 | type: String, 16 | required: true, 17 | unique: true 18 | }, 19 | domain: { 20 | type: String, 21 | required: true 22 | }, 23 | revoked: { 24 | type: Boolean, 25 | required: true, 26 | "default": false 27 | }, 28 | contact: { 29 | type: String, 30 | required: true, 31 | validate: validate("isEmail") 32 | }, 33 | admin: { 34 | type: Boolean, 35 | required: true, 36 | "default": false 37 | } 38 | }); 39 | 40 | var ApiApp = mongoose.model("ApiApp", schema); 41 | 42 | return ApiApp; 43 | }; 44 | -------------------------------------------------------------------------------- /lib/models/list.js: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | module.exports = function (Mongoose) { 6 | var listSchema = new Mongoose.Schema({ 7 | makes: { 8 | type: [String] 9 | }, 10 | userId: { 11 | type: Number, 12 | required: true 13 | }, 14 | ownerApp: { 15 | type: String, 16 | required: true 17 | }, 18 | title: { 19 | type: String 20 | }, 21 | description: { 22 | type: String 23 | } 24 | }); 25 | 26 | listSchema.virtual("id").get(function () { 27 | return this._id; 28 | }); 29 | 30 | var List = Mongoose.model("List", listSchema); 31 | 32 | List.updateFields = [ 33 | "makes", 34 | "title", 35 | "description" 36 | ]; 37 | 38 | return List; 39 | }; 40 | -------------------------------------------------------------------------------- /lib/models/make.js: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | module.exports = function (mongoInstance) { 6 | var mongoosastic = require("mongoosastic"), 7 | mongooseValidator = require("mongoose-validator"), 8 | env = require("../environment"), 9 | validate = mongooseValidator.validate, 10 | url = require("url"), 11 | Mongoose = mongoInstance, 12 | elasticSearchURL = env.get("FOUNDELASTICSEARCH_URL") || 13 | env.get("BONSAI_URL") || 14 | env.get("ELASTIC_SEARCH_URL"); 15 | 16 | elasticSearchURL = url.parse(elasticSearchURL); 17 | 18 | // This is a copy of https://github.com/chriso/node-validator/blob/master/lib/validators.js#L29-L32 which 19 | // is used for "isUrl" with mongoose-validator. Modified to accept underscores in the hostname. 20 | var urlregex = new RegExp("^(?!mailto:)(?:(?:https?|ftp):\\/\\/)?(?:\\S+(?::\\S*)" + 21 | "?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[" + 22 | "0-5])){2}(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\_\\u00a1-" + 23 | "\\uffff0-9]+-?)*[a-z\\_\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*" + 24 | "[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))|localhost)(?::\\d" + 25 | "{2,5})?(?:\\/[^\\s]*)?(?:\\?\\S*)?$", "i" 26 | ); 27 | 28 | mongooseValidator.extend("isURL", function () { 29 | var str = this.str; 30 | return str.length < 2083 && str.match(urlregex); 31 | }); 32 | 33 | var Timestamp = { 34 | type: Number, 35 | es_type: "long", 36 | es_indexed: true, 37 | es_index: "not_analyzed" 38 | }, 39 | 40 | reportSchema = new Mongoose.Schema({ 41 | userId: Number 42 | }), 43 | 44 | likeSchema = new Mongoose.Schema({ 45 | userId: Number 46 | }); 47 | 48 | // Schema 49 | var schema = new Mongoose.Schema({ 50 | url: { 51 | type: String, 52 | required: true, 53 | es_indexed: true, 54 | validate: validate("isURL"), 55 | unique: true, 56 | es_index: "not_analyzed" 57 | }, 58 | contenturl: { 59 | type: String, 60 | es_indexed: true, 61 | validate: validate("isURL"), 62 | unique: true 63 | }, 64 | contentType: { 65 | type: String, 66 | es_indexed: true, 67 | required: true, 68 | es_index: "not_analyzed" 69 | }, 70 | locale: { 71 | type: String, 72 | "default": "en_US", 73 | es_indexed: true, 74 | es_index: "not_analyzed" 75 | }, 76 | title: { 77 | type: String, 78 | es_indexed: true, 79 | required: true 80 | }, 81 | description: { 82 | type: String, 83 | es_indexed: true 84 | }, 85 | thumbnail: { 86 | type: String, 87 | es_indexed: true, 88 | es_index: "not_analyzed" 89 | }, 90 | author: { 91 | type: String, 92 | required: false, 93 | es_indexed: true, 94 | es_index: "not_analyzed" 95 | }, 96 | email: { 97 | type: String, 98 | required: true, 99 | validate: validate("isEmail"), 100 | es_indexed: true, 101 | es_index: "not_analyzed" 102 | }, 103 | published: { 104 | type: Boolean, 105 | "default": true, 106 | es_indexed: true 107 | }, 108 | tags: { 109 | type: [String], 110 | es_indexed: true, 111 | es_index: "not_analyzed", 112 | es_type: "String" 113 | }, 114 | reports: { 115 | type: [reportSchema], 116 | es_indexed: true 117 | }, 118 | remixedFrom: { 119 | type: String, 120 | "default": null, 121 | es_indexed: true, 122 | es_index: "not_analyzed" 123 | }, 124 | remixurl: { 125 | type: String, 126 | es_indexed: true, 127 | es_index: "not_analyzed" 128 | }, 129 | editurl: { 130 | type: String, 131 | es_indexed: true, 132 | es_index: "not_analyzed" 133 | }, 134 | likes: { 135 | type: [likeSchema], 136 | es_indexed: true 137 | }, 138 | ownerApp: { 139 | type: String, 140 | required: true, 141 | es_indexed: false 142 | }, 143 | createdAt: Timestamp, 144 | updatedAt: Timestamp, 145 | deletedAt: { 146 | type: Number, 147 | "default": null, 148 | es_indexed: true, 149 | es_type: "long" 150 | } 151 | }); 152 | 153 | schema.set("toJSON", { 154 | virtuals: true 155 | }); 156 | schema.set("toObject", { 157 | virtuals: false, 158 | getters: false 159 | }); 160 | 161 | schema.virtual("id").get(function () { 162 | return this._id; 163 | }); 164 | 165 | schema.virtual("appTags").get(function () { 166 | return this.tags.filter(function (tag) { 167 | return (/(^[^@]+)\:[^:]+/).test(tag); 168 | }); 169 | }); 170 | 171 | schema.plugin(mongoosastic, { 172 | protocol: elasticSearchURL.protocol.replace(":", "").replace("/", "") || "http", 173 | port: elasticSearchURL.port || 80, 174 | host: (elasticSearchURL.auth ? elasticSearchURL.auth + "@" : "") + elasticSearchURL.hostname 175 | }); 176 | 177 | var Make = Mongoose.model("Make", schema); 178 | 179 | Make.createMapping(function (err, mapping) { 180 | if (err) { 181 | console.log("Failed to create mapping. Is ElasticSearch Running?\n", err.toString()); 182 | } 183 | }); 184 | 185 | Make.publicFields = ["url", "contentType", "contenturl", "locale", 186 | "title", "description", "author", "published", 187 | "tags", "thumbnail", "remixedFrom", "likes", 188 | "reports", "remixurl", "editurl" 189 | ]; 190 | 191 | return Make; 192 | }; 193 | -------------------------------------------------------------------------------- /lib/mongoose.js: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | var mongoose = require("mongoose"), 6 | env = require("./environment"), 7 | isDbOnline = false, 8 | storedError = "Database connection is not online."; 9 | 10 | function defaultDbReadyFn(err) { 11 | if (err) { 12 | storedError = err.toString(); 13 | console.log("Failed to connect to MongoDB - " + storedError); 14 | } 15 | } 16 | 17 | module.exports = function (dbReadyFn) { 18 | dbReadyFn = dbReadyFn || defaultDbReadyFn; 19 | 20 | if (!isDbOnline) { 21 | mongoose.connection.on("open", function () { 22 | isDbOnline = true; 23 | dbReadyFn(); 24 | }); 25 | 26 | mongoose.connection.on("error", function (err) { 27 | isDbOnline = false; 28 | dbReadyFn(err); 29 | }); 30 | 31 | mongoose.connect( 32 | env.get("MONGOLAB_URI") || 33 | env.get("MONGOHQ_URL") || 34 | env.get("MONGO_URL") 35 | ); 36 | } 37 | 38 | return { 39 | mongoInstance: function () { 40 | return mongoose; 41 | }, 42 | isDbOnline: function (req, res, next) { 43 | if (isDbOnline) { 44 | return next(); 45 | } 46 | 47 | next(new Error(storedError)); 48 | } 49 | }; 50 | }; 51 | -------------------------------------------------------------------------------- /lib/queryBuilder/generators/facets.js: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | /* 6 | * Facet Genenerators 7 | * This module generates facets for ElasticSearch Queries 8 | */ 9 | 10 | module.exports = { 11 | // Returns autocomplete suggestions by running a terms facet on a query. 12 | autocompleteTagFacet: function (term, size) { 13 | return { 14 | "tags": { 15 | "terms": { 16 | "field": "tags", 17 | "regex": "^" + term + "[^:]*$", 18 | "size": size 19 | } 20 | } 21 | }; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /lib/queryBuilder/generators/generic.js: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | /* 6 | * This module contains generic helper functions for generating filters 7 | */ 8 | 9 | /* 10 | * generateSearchFilter 11 | * 12 | * This is a helper generator for filters. 13 | * `type` is the type of filter to generate i.e. "term", "query", "prefix" 14 | * `map` is an object whose keys are copied onto the generated filter. 15 | * `not` is a boolean, if set to true, the filter is wrapped in a negation (not) filter 16 | */ 17 | function generateSearchFilter(type, map, not) { 18 | var filter = {}, 19 | filterType = filter[type] = {}; 20 | 21 | Object.keys(map).forEach(function (key) { 22 | filterType[key] = map[key]; 23 | }); 24 | 25 | if (not) { 26 | return { 27 | not: filter 28 | }; 29 | } 30 | return filter; 31 | } 32 | 33 | function generateTermsFilter(terms, field, not) { 34 | var execution, 35 | filterObj = {}; 36 | 37 | // terms will be a comma delimited list of terms provided in the request 38 | terms = terms.map(function (term) { 39 | return term.trim(); 40 | }); 41 | 42 | // The first element will always indicate the type of execution 43 | // for ES to run the terms filter with. 44 | // For example, "the make should have terms a AND b" or 45 | // "the make should have term a OR term b" 46 | if (terms[0] === "and" || terms[0] === "or") { 47 | execution = terms.splice(0, 1)[0]; 48 | } 49 | 50 | filterObj[field] = terms; 51 | 52 | if (execution) { 53 | filterObj.execution = execution; 54 | } 55 | 56 | return generateSearchFilter("terms", filterObj, not); 57 | } 58 | 59 | module.exports = { 60 | generateSearchFilter: generateSearchFilter, 61 | generateTermsFilter: generateTermsFilter 62 | }; 63 | -------------------------------------------------------------------------------- /lib/queryBuilder/generators/index.js: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | module.exports = { 6 | facets: require("./facets"), 7 | likedByUser: require("./likedByUser"), 8 | queries: require("./queries"), 9 | sort: require("./sort"), 10 | remix: require("./remix"), 11 | search: require("./search"), 12 | user: require("./user") 13 | }; 14 | -------------------------------------------------------------------------------- /lib/queryBuilder/generators/likedByUser.js: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | var env = require("../../environment"), 6 | hyperquest = require("hyperquest"); 7 | 8 | var loginServerUrl = env.get("LOGIN_SERVER_URL_WITH_AUTH", "http://localhost:3000"), 9 | getUserURL = require("url").resolve(loginServerUrl, "/user/username/"), 10 | LOGINAPI_ERR = "Error building user search query: "; 11 | 12 | var generic = require("./generic"); 13 | 14 | module.exports = function likesFilter(username, filterOccurence, callback) { 15 | var get = hyperquest.get({ 16 | headers: { 17 | "Content-Type": "application/json" 18 | }, 19 | uri: getUserURL + username 20 | }); 21 | 22 | get.on("error", function (err) { 23 | callback({ 24 | error: LOGINAPI_ERR + "Login API request failed", 25 | code: 500 26 | }); 27 | }); 28 | 29 | get.on("response", function (resp) { 30 | // 404 on a username doesn't necessarily mean we don't want the search to execute 31 | if ([200, 404].indexOf(resp.statusCode) === -1) { 32 | return callback({ 33 | error: LOGINAPI_ERR + "Received a status code of " + resp.statusCode, 34 | code: resp.statusCode || 500 35 | }); 36 | } 37 | var bodyParts = [], 38 | bytes = 0; 39 | 40 | resp.on("data", function (data) { 41 | bodyParts.push(data); 42 | bytes += data.length; 43 | }); 44 | 45 | resp.on("end", function () { 46 | var responseBody = Buffer.concat(bodyParts, bytes).toString("utf8"); 47 | 48 | try { 49 | responseBody = JSON.parse(responseBody); 50 | } catch (exception) { 51 | return callback({ 52 | error: LOGINAPI_ERR + "Unable to parse Login API response body", 53 | code: 500 54 | }); 55 | } 56 | 57 | // Check that a user object was found 58 | if (!responseBody.user) { 59 | if (filterOccurence === "or") { 60 | // If this is an OR filtered query, ignore the undefined user 61 | // because the search may return results for other fields in the query 62 | return callback(null, null); 63 | } else { 64 | // invalid username, 404 the request 65 | return callback({ 66 | code: 404 67 | }); 68 | } 69 | } 70 | 71 | var filter = generic.generateSearchFilter("term", { 72 | "likes.userId": responseBody.user.id 73 | }); 74 | 75 | callback(null, filter); 76 | }); 77 | }); 78 | }; 79 | -------------------------------------------------------------------------------- /lib/queryBuilder/generators/queries.js: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | /* 6 | * Query Genenerators 7 | * This module contains generator functions for ElasticSearch Query DSL objects. 8 | * When creating base query objects for various types of Elastic Search queries, 9 | * add the generator functions here 10 | */ 11 | 12 | module.exports = { 13 | /* 14 | * The baseQuery return object should be used when there aren't 15 | * any additional filters to be applied to a query. For example, 16 | * a search for the 20 most recently created makes can be made using this query. 17 | * This is a filtered query: 18 | * http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-filtered-query.html 19 | */ 20 | baseQuery: function () { 21 | return { 22 | query: { 23 | filtered: { 24 | query: { 25 | match_all: {} 26 | }, 27 | filter: { 28 | bool: { 29 | must: [{ 30 | missing: { 31 | field: "deletedAt", 32 | null_value: true 33 | } 34 | }, { 35 | term: { 36 | published: true 37 | } 38 | }], 39 | should: [] 40 | } 41 | } 42 | } 43 | } 44 | }; 45 | }, 46 | 47 | /* 48 | * Use the advanced query when you need to generate more complex queries 49 | * that will need filters applied for different make fields. 50 | * The advancedQuery inherits the baseQuery as it's query for a 51 | * filtered query: 52 | * http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-filtered-query.html 53 | */ 54 | advancedQuery: function () { 55 | return { 56 | query: { 57 | filtered: { 58 | filter: { 59 | bool: { 60 | must: [], 61 | should: [] 62 | } 63 | }, 64 | query: this.baseQuery().query 65 | } 66 | } 67 | }; 68 | }, 69 | 70 | authenticatedQuery: function (useBasic) { 71 | var authenticatedQuery = useBasic ? this.baseQuery() : this.advancedQuery(); 72 | if (useBasic) { 73 | authenticatedQuery.query.filtered.filter.bool.must.splice(1, 1); 74 | } else { 75 | authenticatedQuery.query.filtered.query.filtered.filter.bool.must.splice(1, 1); 76 | } 77 | return authenticatedQuery; 78 | } 79 | }; 80 | -------------------------------------------------------------------------------- /lib/queryBuilder/generators/remix.js: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | /* 6 | * This module generates an Elastic Search query DSL range filter for remixCount queries 7 | * See: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-filters.html 8 | */ 9 | 10 | var generic = require("./generic"); 11 | 12 | /* 13 | * The properties of this exported object map to properties on the make object model, 14 | * and return a filter to be added to the ES query. 15 | */ 16 | module.exports = function (from, to, not) { 17 | return generic.generateSearchFilter("range", { 18 | createdAt: { 19 | gte: +from, 20 | lte: +to 21 | } 22 | }); 23 | }; 24 | -------------------------------------------------------------------------------- /lib/queryBuilder/generators/search.js: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | /* 6 | * This module generates Elastic Search query DSL filters for search queries 7 | * See: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-filters.html 8 | */ 9 | 10 | var generic = require("./generic"); 11 | 12 | /* 13 | * The properties of this exported object map to properties on the make object model, 14 | * and return a filter to be added to the ES query. 15 | */ 16 | module.exports.filters = { 17 | author: function (name, not) { 18 | return generic.generateSearchFilter("term", { 19 | author: name 20 | }, not); 21 | }, 22 | 23 | contentType: function (contentType, not) { 24 | return generic.generateSearchFilter("term", { 25 | contentType: contentType 26 | }, not); 27 | }, 28 | 29 | description: function (description, not) { 30 | return generic.generateSearchFilter("query", { 31 | match: { 32 | description: { 33 | query: description, 34 | operator: "and" 35 | } 36 | } 37 | }, not); 38 | }, 39 | 40 | id: function (ids, not) { 41 | // It's safe to try and split on ',' because it cannot be a character in an id 42 | ids = ids.split(","); 43 | 44 | if (ids.length === 1) { 45 | return generic.generateSearchFilter("term", { 46 | _id: ids[0] 47 | }, not); 48 | } else { 49 | return generic.generateTermsFilter(ids, "_id", not); 50 | } 51 | }, 52 | 53 | remixedFrom: function (id, not) { 54 | return generic.generateSearchFilter("term", { 55 | remixedFrom: id 56 | }, not); 57 | }, 58 | 59 | tags: function (tags, not) { 60 | return generic.generateTermsFilter(tags.split(","), "tags", not); 61 | }, 62 | 63 | tagPrefix: function (prefix, not) { 64 | return generic.generateSearchFilter("prefix", { 65 | tags: prefix 66 | }, not); 67 | }, 68 | 69 | title: function (title, not) { 70 | return generic.generateSearchFilter("query", { 71 | match: { 72 | title: { 73 | query: title, 74 | operator: "and" 75 | } 76 | } 77 | }, not); 78 | }, 79 | 80 | url: function (url, not) { 81 | return generic.generateSearchFilter("term", { 82 | url: url 83 | }, not); 84 | }, 85 | 86 | remixCount: function (options, not) { 87 | return generic.generateSearchFilter("range", { 88 | createdAt: { 89 | gte: options.from, 90 | lte: options.to 91 | } 92 | }, not); 93 | } 94 | }; 95 | 96 | // Export the keys of the filters object to help consumers determine if a filter is defined for a field 97 | module.exports.KEYS = Object.keys(module.exports.filters); 98 | -------------------------------------------------------------------------------- /lib/queryBuilder/generators/sort.js: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | /* 6 | * This module generates sort instructions for elastic search queries 7 | * See: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html 8 | */ 9 | 10 | // export all valid sort fields that are defined on the make object model 11 | module.exports.VALID_SORT_FIELDS = [ 12 | "author", 13 | "contentType", 14 | "description", 15 | "id", 16 | "remixedFrom", 17 | "title", 18 | "url", 19 | "createdAt", 20 | "updatedAt", 21 | "likes", 22 | "reports" 23 | ]; 24 | 25 | // Generate a regular sort object for a given field 26 | module.exports.generateRegularSort = function (type, order) { 27 | var sortObj = {}; 28 | sortObj[type] = order || "desc"; 29 | return sortObj; 30 | }; 31 | 32 | /* 33 | * Generates a script based sort: 34 | * http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html#_script_based_sorting 35 | * Script language depends on your specific installation of ElasticSearch 36 | */ 37 | module.exports.generateScriptSort = function (script, order) { 38 | return { 39 | _script: { 40 | lang: "js", 41 | order: order || "desc", 42 | script: script, 43 | type: "number" 44 | } 45 | }; 46 | }; 47 | -------------------------------------------------------------------------------- /lib/queryBuilder/generators/user.js: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | /* This module uses the LoginAPI to translate a username into 6 | * an email address for the purpose of searching 7 | * 8 | * TODO: Obsolete this by storing username. 9 | * https://bugzilla.mozilla.org/show_bug.cgi?id=936039 10 | */ 11 | 12 | var env = require("../../environment"), 13 | hyperquest = require("hyperquest"); 14 | 15 | var loginServerUrl = env.get("LOGIN_SERVER_URL_WITH_AUTH", "http://localhost:3000"), 16 | getUserURL = require("url").resolve(loginServerUrl, "/user/username/"), 17 | LOGINAPI_ERR = "Error building user search query: "; 18 | 19 | module.exports = function (options, callback) { 20 | var get = hyperquest.get({ 21 | headers: { 22 | "Content-Type": "application/json" 23 | }, 24 | uri: getUserURL + options.user 25 | }); 26 | 27 | get.on("error", function (err) { 28 | callback({ 29 | error: LOGINAPI_ERR + "Login API request failed", 30 | code: 500 31 | }); 32 | }); 33 | 34 | get.on("response", function (resp) { 35 | // 404 on a username doesn't necessarily mean we don't want the search to execute 36 | if ([200, 404].indexOf(resp.statusCode) === -1) { 37 | return callback({ 38 | error: LOGINAPI_ERR + "Received a status code of " + resp.statusCode, 39 | code: resp.statusCode || 500 40 | }); 41 | } 42 | var bodyParts = [], 43 | bytes = 0; 44 | 45 | resp.on("data", function (data) { 46 | bodyParts.push(data); 47 | bytes += data.length; 48 | }); 49 | 50 | resp.on("end", function () { 51 | var responseBody = Buffer.concat(bodyParts, bytes).toString("utf8"); 52 | 53 | try { 54 | responseBody = JSON.parse(responseBody); 55 | } catch (exception) { 56 | return callback({ 57 | error: LOGINAPI_ERR + "Unable to parse Login API response body", 58 | code: 500 59 | }); 60 | } 61 | 62 | // Check that a user object was found 63 | if (!responseBody.user) { 64 | if (options.isOr) { 65 | // If this is an OR filtered query, ignore the undefined user 66 | // because the search may return results for other fields in the query 67 | return callback(null, null); 68 | } else { 69 | // invalid username, 404 the request 70 | return callback({ 71 | code: 404 72 | }); 73 | } 74 | } 75 | 76 | var filter = { 77 | "term": { 78 | email: responseBody.user.email 79 | } 80 | }; 81 | 82 | if (options.not) { 83 | filter = { 84 | not: filter 85 | }; 86 | } 87 | 88 | callback(null, filter); 89 | }); 90 | }); 91 | }; 92 | -------------------------------------------------------------------------------- /lib/queryBuilder/index.js: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | module.exports = { 6 | remixCount: require("./queryTypes/remix"), 7 | search: require("./queryTypes/search"), 8 | autocomplete: require("./queryTypes/autocomplete") 9 | }; 10 | -------------------------------------------------------------------------------- /lib/queryBuilder/queryTypes/autocomplete.js: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | /* 6 | * Autocomplete Query Generator 7 | * This module will generate Elasticsearch query DSL that can 8 | * be used to get a list of autocomplete suggestions. 9 | */ 10 | 11 | var generators = require("../generators"); 12 | 13 | // limit max results to 100 14 | var MAX_SIZE = 100, 15 | DEFAULT_SIZE = 10; 16 | 17 | // generateAutocompleteQuery( autocompleteTerm ) 18 | // Accepts a string to use as the facet term. 19 | // returns an object representing the auto complete query or 20 | // null if the autocompleteTerm is undefined or not a string. 21 | 22 | module.exports = function generateAutocompleteQuery(autocompleteTerm, size) { 23 | if (!autocompleteTerm || typeof autocompleteTerm !== "string") { 24 | return null; 25 | } 26 | 27 | size = +size; 28 | 29 | if (!size || size < 1) { 30 | size = DEFAULT_SIZE; 31 | } else if (size > MAX_SIZE) { 32 | size = MAX_SIZE; 33 | } 34 | 35 | return { 36 | "query": { 37 | "match_all": {} 38 | }, 39 | "facets": generators.facets.autocompleteTagFacet(autocompleteTerm, size), 40 | "size": 0 41 | }; 42 | }; 43 | -------------------------------------------------------------------------------- /lib/queryBuilder/queryTypes/remix.js: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | /* 6 | * Remix Count Query Generator 7 | * This module will generate Elasticsearch query DSL that can 8 | * be used to count remixes of Makes 9 | * For documentation, see: https://github.com/mozilla/makeapi-client/blob/master/README.md 10 | */ 11 | 12 | var generators = require("../generators"); 13 | 14 | // DSL generator function - accepts an object that defines a query 15 | // and a callback to pass the generated DSL to 16 | module.exports = function (id, from, to, callback) { 17 | var searchQuery = generators.queries.advancedQuery(); 18 | 19 | // don't want any make data, just a total count 20 | searchQuery.size = 0; 21 | searchQuery.query.filtered.filter.bool.must.push(generators.search.filters.remixedFrom(id)); 22 | searchQuery.query.filtered.filter.bool.must.push(generators.remix(from, to)); 23 | 24 | return callback(null, searchQuery); 25 | }; 26 | -------------------------------------------------------------------------------- /lib/queryBuilder/queryTypes/search.js: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | /* 6 | * Search Query Generator 7 | * This module will generate Elasticsearch query DSL that can 8 | * be used to search for makes. 9 | * For documentation, see: https://github.com/mozilla/makeapi-client/blob/master/README.md 10 | */ 11 | 12 | var async = require("async"); 13 | 14 | var DEFAULT_SEARCH_SIZE = 10, 15 | MAX_SEARCH_SIZE = 1000; 16 | 17 | var generators = require("../generators"); 18 | 19 | // if a field is to be negated (i.e. makes not containing the title "yolo"), 20 | // the field will be prefixed with the following string (no quotes): "{!}" 21 | var NOT_REGEX = /^\{!\}(.+)$/; 22 | 23 | // detect if there is a matching search generator for a given field 24 | function hasValidField(field) { 25 | return generators.search.KEYS.indexOf(field) !== -1; 26 | } 27 | 28 | // Validate a given size & checks if the value is in 29 | // the correct bounds: >= 1 <= MAX_SEARCH_SIZE 30 | function validateSize(size) { 31 | size = size && isFinite(size) ? size : DEFAULT_SEARCH_SIZE; 32 | if (size > MAX_SEARCH_SIZE) { 33 | return MAX_SEARCH_SIZE; 34 | } else if (size < 1) { 35 | return 1; 36 | } 37 | return size; 38 | } 39 | 40 | // Validate that the requested page is a number and is 41 | // within correct bounds: page >= 0. Will also calculate 42 | // the correct "from" value based on requested number of results 43 | // see: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-from-size.html 44 | function validatePage(page, size) { 45 | page = page && isFinite(page) ? page : 1; 46 | if (page < 1) { 47 | return 0; 48 | } 49 | return --page * size; 50 | } 51 | 52 | function generateFilters(queryData, callback) { 53 | var authenticated = queryData.authenticated, 54 | filterOccurence = queryData.filterOccurence, 55 | queryKeys = queryData.queryKeys, 56 | query = queryData.query, 57 | searchQuery; 58 | 59 | // If the request contains any of the filter generating keys, or defines a user search, use the advancedQuery object 60 | if (queryKeys.some(hasValidField) || queryData.user || queryData.likedByUser) { 61 | if (authenticated) { 62 | searchQuery = generators.queries.authenticatedQuery(); 63 | } else { 64 | searchQuery = generators.queries.advancedQuery(); 65 | } 66 | queryKeys.forEach(function (key) { 67 | var value = query[key], 68 | notRegexMatch; 69 | if (generators.search.KEYS.indexOf(key) !== -1) { 70 | notRegexMatch = NOT_REGEX.exec(value); 71 | if (notRegexMatch) { 72 | searchQuery.query.filtered.filter.bool[filterOccurence] 73 | .push(generators.search.filters[key](notRegexMatch[1], true)); 74 | } else { 75 | searchQuery.query.filtered.filter.bool[filterOccurence] 76 | .push(generators.search.filters[key](value)); 77 | } 78 | } 79 | }); 80 | } else if (authenticated) { 81 | searchQuery = generators.queries.authenticatedQuery(true); 82 | } else { 83 | searchQuery = generators.queries.baseQuery(); 84 | } 85 | queryData.searchQuery = searchQuery; 86 | callback(null, queryData); 87 | } 88 | 89 | function setSize(queryData, callback) { 90 | queryData.searchQuery.size = validateSize(queryData.size); 91 | callback(null, queryData); 92 | } 93 | 94 | function setFrom(queryData, callback) { 95 | queryData.searchQuery.from = validatePage(queryData.page, queryData.searchQuery.size); 96 | callback(null, queryData); 97 | } 98 | 99 | function sortQuery(queryData, callback) { 100 | var sort = queryData.sort, 101 | sortObj; 102 | if (sort) { 103 | sort = (Array.isArray(sort) ? sort : [sort]).filter(function (pair) { 104 | return typeof pair === "string" && 105 | pair.length && 106 | generators.sort.VALID_SORT_FIELDS.indexOf(pair.split(",")[0]) !== -1; 107 | }); 108 | if (sort.length) { 109 | queryData.searchQuery.sort = []; 110 | sort.forEach(function (pair) { 111 | pair = pair.split(","); 112 | if (["likes", "reports"].indexOf(pair[0]) !== -1) { 113 | sortObj = generators.sort.generateScriptSort("doc['" + pair[0] + ".userId'].values.length", pair[1]); 114 | } else { 115 | sortObj = generators.sort.generateRegularSort(pair[0], pair[1]); 116 | } 117 | queryData.searchQuery.sort.push(sortObj); 118 | }); 119 | } 120 | } 121 | callback(null, queryData); 122 | } 123 | 124 | function userQuery(queryData, callback) { 125 | if (!queryData.user) { 126 | return callback(null, queryData); 127 | } 128 | 129 | var notRegexMatch = NOT_REGEX.exec(queryData.user); 130 | if (notRegexMatch) { 131 | queryData.user = notRegexMatch[1]; 132 | } 133 | generators.user({ 134 | user: queryData.user, 135 | isOr: !!queryData.searchQuery.query.filtered.filter.bool.should.length, 136 | not: !!notRegexMatch 137 | }, function generationComplete(err, filter) { 138 | if (err) { 139 | return callback(err); 140 | } 141 | 142 | // add the returned user filter if it exists 143 | // it may not exist and not be erroneous if 144 | // the filters are running with the "or" 145 | // execution style. 146 | if (filter) { 147 | queryData.searchQuery.query.filtered.filter.bool[queryData.filterOccurence].push(filter); 148 | } 149 | 150 | // continue 151 | return callback(null, queryData); 152 | }); 153 | } 154 | 155 | function likedByUserQuery(queryData, callback) { 156 | if (!queryData.likedByUser) { 157 | return callback(null, queryData); 158 | } 159 | 160 | generators.likedByUser(queryData.likedByUser, queryData.filterOccurence, function (err, filter) { 161 | if (err) { 162 | return callback(err); 163 | } 164 | 165 | if (filter) { 166 | queryData.searchQuery.query.filtered.filter.bool[queryData.filterOccurence].push(filter); 167 | } 168 | 169 | return callback(null, queryData); 170 | }); 171 | } 172 | 173 | // DSL generator function - accepts an object that defines a query 174 | // and a callback to pass the generated DSL to 175 | module.exports = function (query, callback, authenticated) { 176 | query.limit = +query.limit; 177 | query.page = +query.page; 178 | 179 | async.waterfall([ 180 | function (cb) { 181 | return cb(null, { 182 | authenticated: authenticated, 183 | filterOccurence: query.or ? "should" : "must", 184 | likedByUser: query.likedByUser || "", 185 | page: query.page, 186 | queryKeys: Object.keys(query), 187 | query: query, 188 | size: query.limit, 189 | sort: query.sortByField, 190 | user: query.user || "" 191 | }); 192 | }, 193 | generateFilters, 194 | setSize, 195 | setFrom, 196 | sortQuery, 197 | userQuery, 198 | likedByUserQuery 199 | ], function (err, queryData) { 200 | if (err) { 201 | return callback(err); 202 | } 203 | callback(null, queryData.searchQuery); 204 | }); 205 | }; 206 | -------------------------------------------------------------------------------- /lib/sanitizer.js: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | // This function sanitizes tags to be mostly HTML-safe 6 | module.exports = function sanitize(inputStr) { 7 | return inputStr.replace(/[^\w\u00C0-\uFFFF\.\/\-:_%]/g, '').replace(/%3[CE]/g, ''); 8 | }; 9 | -------------------------------------------------------------------------------- /lib/strategy.js: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | module.exports = function (ApiApp, devMode) { 6 | var DEVKEY = "00000000-0000-0000-000000000000", 7 | uuid = require("uuid"); 8 | 9 | function devKeyLookupStrategy(publickey, callback) { 10 | var credentials = { 11 | user: publickey, 12 | algorithm: "sha256" 13 | }; 14 | if (publickey === DEVKEY) { 15 | credentials.key = DEVKEY; 16 | credentials.admin = true; 17 | } else { 18 | // generate a fake password so that credential verification will fail 19 | credentials.key = uuid.v4(); 20 | } 21 | callback(null, credentials); 22 | } 23 | 24 | function databaseKeyLookupStrategy(publickey, callback) { 25 | var credentials = { 26 | algorithm: "sha256", 27 | user: publickey 28 | }; 29 | ApiApp.findOne({ 30 | publickey: publickey 31 | }, function (err, doc) { 32 | if (err || !doc) { 33 | return callback(err); 34 | } 35 | credentials.key = doc.privatekey; 36 | credentials.admin = doc.admin; 37 | callback(null, credentials); 38 | }); 39 | } 40 | 41 | if (devMode) { 42 | return devKeyLookupStrategy; 43 | } else { 44 | return databaseKeyLookupStrategy; 45 | } 46 | }; 47 | -------------------------------------------------------------------------------- /lib/tags.js: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | module.exports = function () { 6 | // User Tags are "some@something.com:foo", which means two 7 | // strings, joined with a ':', and the first string contains 8 | // an email address (i.e., an '@'). 9 | var userTagRegex = /^([^@]+@[^@]+)\:[^:]+/, 10 | 11 | // Raw Tags are "foo" or "#fooBar", which means one string 12 | // which does not include a colon. 13 | rawTagRegex = /^[^:]+$/, 14 | 15 | // Trim any whitespace around tags 16 | trimWhitespace = function (tags) { 17 | return tags.map(function (val) { 18 | return val.trim(); 19 | }); 20 | }; 21 | 22 | return { 23 | isTagChangeAllowed: function (tag, user, isAdmin) { 24 | if (isAdmin || rawTagRegex.test(tag)) { 25 | return true; 26 | } 27 | 28 | var taggedEmail = userTagRegex.exec(tag); 29 | 30 | if (user && taggedEmail && taggedEmail[1] === user.email) { 31 | return true; 32 | } 33 | 34 | return false; 35 | }, 36 | validateTags: function (tags, user) { 37 | var taggedEmail; 38 | 39 | tags = trimWhitespace(tags); 40 | 41 | return tags.filter(function (val) { 42 | // allow if user is an admin, or val is a raw tag 43 | if ((user && user.isAdmin) || rawTagRegex.test(val)) { 44 | return true; 45 | } 46 | 47 | taggedEmail = userTagRegex.exec(val); 48 | 49 | // Allow if val is a user tag, and user is logged in 50 | if (user && taggedEmail && taggedEmail[1] === user.email) { 51 | return true; 52 | } 53 | 54 | return false; 55 | }); 56 | } 57 | }; 58 | }; 59 | -------------------------------------------------------------------------------- /migrations/20130731-contenturl.js: -------------------------------------------------------------------------------- 1 | /* This script will update all makes in the database with their contenturl value 2 | * It is assumed that the schema change has already been applied in the code, 3 | * the .env file contains a proper database configuration file, and that 4 | * your mongo daemon is running. 5 | * 6 | * run this script from the root of the project like so: 7 | * node migrations/20130731-contenturl 8 | */ 9 | 10 | var habitat = require( "habitat" ), 11 | url = require( "url" ), 12 | staticDataStore = process.argv[ 2 ], 13 | ended = false, 14 | env, 15 | mongoose; 16 | 17 | habitat.load(); 18 | 19 | env = new habitat(); 20 | 21 | if ( !staticDataStore ) { 22 | console.log( "You must provide a static data store value as an argument when invoking this script" ); 23 | process.exit( 1 ); 24 | } 25 | 26 | function generateContentURL( vanityUrl ) { 27 | vanityUrl = url.parse( vanityUrl ); 28 | return url.resolve( staticDataStore, vanityUrl.hostname.split( "." )[ 0 ] + vanityUrl.path ); 29 | } 30 | 31 | function saveCallback( err, doc ) { 32 | if ( err ) { 33 | throw err; 34 | } 35 | if ( ended ) { 36 | console.log( "complete!" ); 37 | process.exit( 0 ); 38 | } 39 | } 40 | 41 | dbh = require( "../lib/mongoose" )( env, function( err ) { 42 | if ( err ) { 43 | console.log( err ); 44 | process.exit( 1 ); 45 | } 46 | 47 | var Make = require( "../lib/models/make" )( env, dbh.mongoInstance() ), 48 | stream = Make.find().stream(); 49 | 50 | stream.on( "data", function onData( doc ) { 51 | if ( doc.contenturl ) { 52 | return; 53 | } 54 | doc.contenturl = generateContentURL( doc.url ); 55 | doc.save( saveCallback ); 56 | }).on( "error", function( err ) { 57 | process.exit( 1 ); 58 | }).on( "end", function() { 59 | // simply ending here can break things because the stream ends before the last "data" callback. 60 | ended = true; 61 | }); 62 | }); 63 | -------------------------------------------------------------------------------- /migrations/20130925-locale.js: -------------------------------------------------------------------------------- 1 | /* This script will update all makes in the database and ensure their locale 2 | * value is now properly our new default. 3 | * 4 | * run this script from the root of the project like so: 5 | * node migrations/20130731-locale 6 | */ 7 | 8 | var habitat = require( "habitat" ), 9 | url = require( "url" ), 10 | ended = false, 11 | env; 12 | 13 | habitat.load(); 14 | 15 | env = new habitat(); 16 | 17 | function saveCallback( err, doc ) { 18 | if ( err ) { 19 | console.error( err ); 20 | process.exit( 1 ); 21 | } 22 | if ( ended ) { 23 | console.log( "complete!" ); 24 | process.exit( 0 ); 25 | } 26 | } 27 | 28 | var dbh = require( "../lib/mongoose" )( env, function( err ) { 29 | if ( err ) { 30 | console.error( err ); 31 | process.exit( 1 ); 32 | } 33 | 34 | var Make = require( "../lib/models/make" )( env, dbh.mongoInstance() ), 35 | stream = Make.find().stream(); 36 | 37 | stream.on( "data", function onData( doc ) { 38 | if ( doc.locale === "en_US" ) { 39 | return; 40 | } 41 | if ( !doc.locale || doc.locale === "en_us" ) { 42 | doc.locale = "en_US"; 43 | doc.save( saveCallback ); 44 | } 45 | }).on( "error", function( err ) { 46 | console.error( err ); 47 | process.exit( 1 ); 48 | }).on( "end", function() { 49 | // simply ending here can break things because the stream ends before the last "data" callback. 50 | ended = true; 51 | }); 52 | }); 53 | -------------------------------------------------------------------------------- /migrations/20131009-updatedAt.js: -------------------------------------------------------------------------------- 1 | /* This script will update all makes in the database with an updatedAt 2 | * value if it is not defined. 3 | * 4 | * run this script from the root of the project like so: 5 | * node migrations/20131009-updatedAt 6 | */ 7 | 8 | var habitat = require( "habitat" ), 9 | ended = false, 10 | env, 11 | mongoose; 12 | 13 | habitat.load(); 14 | 15 | env = new habitat(); 16 | 17 | function saveCallback( err, doc ) { 18 | if ( err ) { 19 | throw err; 20 | } 21 | if ( ended ) { 22 | console.log( "complete!" ); 23 | process.exit( 0 ); 24 | } 25 | } 26 | 27 | dbh = require( "../lib/mongoose" )( env, function( err ) { 28 | if ( err ) { 29 | console.log( err ); 30 | process.exit( 1 ); 31 | } 32 | 33 | var Make = require( "../lib/models/make" )( env, dbh.mongoInstance() ), 34 | stream = Make.find({ 35 | updatedAt: null 36 | }).stream(); 37 | 38 | stream.on( "data", function onData( doc ) { 39 | doc.updatedAt = doc.createdAt; 40 | doc.save( saveCallback ); 41 | }).on( "error", function( err ) { 42 | process.exit( 1 ); 43 | }).on( "end", function() { 44 | // simply ending here can break things because the stream ends before the last "data" callback. 45 | ended = true; 46 | }); 47 | }); 48 | -------------------------------------------------------------------------------- /migrations/20140108-ownerApp.js: -------------------------------------------------------------------------------- 1 | /* This script will update all makes in the database with a ownerApp field, based on the contentType. 2 | * It is assumed that the schema change has already been applied in the code, 3 | * the .env file contains a proper database configuration file, and that 4 | * your mongo daemon is running. 5 | * 6 | * run this script from the root of the project like so: 7 | * node migrations/20130917-ownerApp 8 | */ 9 | 10 | var habitat = require( "habitat" ), 11 | async = require( "async" ), 12 | contentType = process.argv[ 2 ], 13 | publicKey = process.argv[ 3 ], 14 | concurrency = process.argv[ 4 ] || 4, 15 | mongoStreamEnded = false, 16 | env, 17 | mongoose; 18 | 19 | habitat.load(); 20 | 21 | env = new habitat(); 22 | 23 | if ( !contentType || !publicKey ) { 24 | console.log( "You must provide a contentType and public key as arguments when invoking this script" ); 25 | process.exit( 1 ); 26 | } 27 | 28 | dbh = require( "../lib/mongoose" )( env, function( err ) { 29 | if ( err ) { 30 | console.log( JSON.stringify( err, null, 2 ) ); 31 | process.exit( 1 ); 32 | } 33 | 34 | var Make = require( "../lib/models/make" )( env, dbh.mongoInstance() ), 35 | stream = Make.find({ 36 | "contentType": contentType, 37 | "ownerApp": null 38 | }).stream(), 39 | queue = async.queue(function( doc, done ) { 40 | doc.ownerApp = publicKey; 41 | doc.save(function( err ) { 42 | if ( err ) { 43 | console.error( "Failure saving document:" ); 44 | console.log( JSON.stringify( doc, null, 2 ) ); 45 | console.log( JSON.stringify( err, null, 2 ) ); 46 | process.exit( 1 ); 47 | } 48 | done(); 49 | }); 50 | }, concurrency ); 51 | 52 | queue.drain = function() { 53 | if ( mongoStreamEnded ) { 54 | console.log( "completed!" ); 55 | process.exit( 0 ); 56 | } 57 | }; 58 | 59 | stream.on( "data", function onData( doc ) { 60 | queue.push( doc ); 61 | }).on( "error", function( err ) { 62 | console.log( JSON.stringify( err, null, 2 ) ); 63 | process.exit( 1 ); 64 | }).on( "end", function() { 65 | mongoStreamEnded = true; 66 | }); 67 | }); 68 | -------------------------------------------------------------------------------- /migrations/20140109-remix-edit-url.js: -------------------------------------------------------------------------------- 1 | /* This script will update all makes in the database with a remixurl and editurl field 2 | * 3 | * run this script from the root of the project like so: 4 | * node migrations/20131009-remix-edit-url 5 | */ 6 | 7 | var habitat = require( "habitat" ), 8 | async = require( "async" ), 9 | mongoStreamEnded = false, 10 | concurrency = process.argv[ 2 ] || 4, 11 | env, 12 | mongoose; 13 | 14 | habitat.load(); 15 | 16 | env = new habitat(); 17 | 18 | dbh = require( "../lib/mongoose" )( env, function( err ) { 19 | if ( err ) { 20 | console.log( err ); 21 | process.exit( 1 ); 22 | } 23 | 24 | var Make = require( "../lib/models/make" )( env, dbh.mongoInstance() ), 25 | stream = Make.find({ 26 | $or: [{ 27 | remixurl: null 28 | }, { 29 | editurl: null 30 | }] 31 | }).stream(), 32 | queue = async.queue(function( doc, done ) { 33 | doc.remixurl = doc.url + "/remix"; 34 | doc.editurl = doc.url + "/edit"; 35 | 36 | doc.save(function( err ) { 37 | if ( err ) { 38 | console.log( "Failure saving document:" ); 39 | console.log( doc ); 40 | console.log( err ); 41 | process.exit( 1 ); 42 | } 43 | done(); 44 | }); 45 | }, concurrency );; 46 | 47 | queue.drain = function() { 48 | if ( mongoStreamEnded ) { 49 | console.log( "completed!" ); 50 | process.exit( 0 ); 51 | } 52 | }; 53 | 54 | stream.on( "data", function onData( doc ) { 55 | queue.push( doc ); 56 | }).on( "error", function( err ) { 57 | console.log( err ); 58 | process.exit( 1 ); 59 | }).on( "end", function() { 60 | mongoStreamEnded = true; 61 | }); 62 | }); 63 | -------------------------------------------------------------------------------- /migrations/20140206-apiApp.js: -------------------------------------------------------------------------------- 1 | /* This script will migrate all ApiUser records into ApiApp records 2 | * 3 | * run this script from the root of the project like so: 4 | * node migrations/20140206-apiApp :: :: ... 5 | */ 6 | 7 | var async = require( "async" ), 8 | env = require( "../lib/environment" ), 9 | validate = require( "mongoose-validator" ).validate, 10 | slicedArgs = process.argv.slice( 2 ), 11 | mongoStreamEnded = false; 12 | 13 | var domainMap = {}; 14 | 15 | slicedArgs.forEach(function( arg ) { 16 | arg = arg.split( '::' ); 17 | domainMap[ arg[0] ] = arg[1]; 18 | }); 19 | 20 | function getApiUser( mongoose ) { 21 | return mongoose.model( "ApiUser" , new mongoose.Schema({ 22 | privatekey: { 23 | type: String, 24 | required: true, 25 | unique: true 26 | }, 27 | publickey: { 28 | type: String, 29 | required: true, 30 | unique: true 31 | }, 32 | revoked: { 33 | type: Boolean, 34 | required: true, 35 | "default": false 36 | }, 37 | contact: { 38 | type: String, 39 | required: true, 40 | validate: validate( "isEmail" ) 41 | }, 42 | admin: { 43 | type: Boolean, 44 | required: true, 45 | "default": false 46 | }, 47 | domain: { 48 | type: String, 49 | required: true, 50 | } 51 | })); 52 | } 53 | 54 | var dbh = require( "../lib/mongoose" )(), 55 | ApiApp = require( "../lib/models/apiApp" )( dbh.mongoInstance() ), 56 | ApiUser = getApiUser( dbh.mongoInstance() ), 57 | stream = ApiUser.find().stream(), 58 | queue = async.queue(function( doc, done ) { 59 | 60 | var app = new ApiApp({ 61 | contact: doc.contact, 62 | privatekey: doc.privatekey, 63 | publickey: doc.publickey, 64 | domain: domainMap[ doc.contact ], 65 | revoked: doc.revoked, 66 | admin: doc.admin 67 | }); 68 | 69 | app.save(function( err ) { 70 | if ( err ) { 71 | console.log( "Failure saving document:" ); 72 | console.log( app ); 73 | console.log( err ); 74 | process.exit( 1 ); 75 | } 76 | done(); 77 | }); 78 | }, 4 );// concurrency of 4 79 | 80 | queue.drain = function() { 81 | if ( mongoStreamEnded ) { 82 | console.log( "completed!" ); 83 | process.exit( 0 ); 84 | } 85 | }; 86 | 87 | stream.on( "data", function onData( doc ) { 88 | queue.push( doc ); 89 | }).on( "error", function( err ) { 90 | console.log( err ); 91 | process.exit( 1 ); 92 | }).on( "end", function() { 93 | mongoStreamEnded = true; 94 | }); 95 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "makeapi", 3 | "version": "1.2.28", 4 | "description": "MakeAPI for Webmaker", 5 | "main": "./lib/api.js", 6 | "scripts": { 7 | "test": "grunt && mocha --reporter spec", 8 | "postinstall": "bower install" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git://github.com/mozilla/MakeAPI.git" 13 | }, 14 | "author": "Webmaker Product Team ", 15 | "license": "MPL-2.0", 16 | "dependencies": { 17 | "async": "1.4.2", 18 | "aws-sdk": "^2.2.39", 19 | "bower": "1.3.8", 20 | "express": "3.4.5", 21 | "gauge": "1.2.7", 22 | "habitat": "3.1.2", 23 | "hatchet": "0.1.0", 24 | "hawk": "2.2.3", 25 | "helmet": "0.1.2", 26 | "hyperquest": "^0.3.0", 27 | "less-middleware": "0.1.15", 28 | "makeapi-client": "0.5.28", 29 | "messina": "0.1.1", 30 | "mongoosastic": "0.2.2", 31 | "mongoose": "3.5.16", 32 | "mongoose-validator": "0.2.1", 33 | "newrelic": "1.5.5", 34 | "node-statsd": "0.0.7", 35 | "nunjucks": "0.1.9", 36 | "request": "^2.64.0", 37 | "uuid": "1.4.1", 38 | "webmaker-auth": "0.1.4" 39 | }, 40 | "devDependencies": { 41 | "grunt": "0.4.5", 42 | "grunt-contrib-csslint": "0.4.0", 43 | "grunt-contrib-jshint": "0.11.0", 44 | "grunt-jsbeautifier": "0.2.8", 45 | "grunt-jscs": "1.5.0", 46 | "mocha": "1.9.0", 47 | "mofo-style": "1.0.1" 48 | }, 49 | "engines": { 50 | "node": ">=0.8.23", 51 | "npm": ">=1.2.10" 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /public/font/FontAwesome.otf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mozilla/MakeAPI/f2893cf6cb81cfed10252a83f721572a1bf2e7ce/public/font/FontAwesome.otf -------------------------------------------------------------------------------- /public/font/fontawesome-webfont.eot: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mozilla/MakeAPI/f2893cf6cb81cfed10252a83f721572a1bf2e7ce/public/font/fontawesome-webfont.eot -------------------------------------------------------------------------------- /public/font/fontawesome-webfont.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mozilla/MakeAPI/f2893cf6cb81cfed10252a83f721572a1bf2e7ce/public/font/fontawesome-webfont.ttf -------------------------------------------------------------------------------- /public/font/fontawesome-webfont.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mozilla/MakeAPI/f2893cf6cb81cfed10252a83f721572a1bf2e7ce/public/font/fontawesome-webfont.woff -------------------------------------------------------------------------------- /public/gallery/index.html: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | Prototype 11 | 12 | 13 | 14 | 15 | 17 | 18 | 76 | 77 | 78 | 79 | -------------------------------------------------------------------------------- /public/gallery/style/style.css: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | 5 | body { 6 | margin: 0; 7 | padding: 0; 8 | font-family: sans-serif; 9 | } 10 | .gallery-item { 11 | height: 315px; 12 | width: 460px; 13 | position: relative; 14 | float: left; 15 | } 16 | 17 | .gallery-item > img { 18 | height: 100%; 19 | width: 100%; 20 | } 21 | 22 | .gallery-item > .title { 23 | margin: 0; 24 | padding: 0.5em; 25 | color: #FFF; 26 | background: rgb( 0 ,0 ,0 ); 27 | background: rgba( 0, 0, 0, 0.5 ); 28 | position: absolute; 29 | top: 0; 30 | left: 0; 31 | right: 0; 32 | } 33 | -------------------------------------------------------------------------------- /public/images/calendar.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mozilla/MakeAPI/f2893cf6cb81cfed10252a83f721572a1bf2e7ce/public/images/calendar.gif -------------------------------------------------------------------------------- /public/images/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mozilla/MakeAPI/f2893cf6cb81cfed10252a83f721572a1bf2e7ce/public/images/favicon.ico -------------------------------------------------------------------------------- /public/images/header-columns-bg.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mozilla/MakeAPI/f2893cf6cb81cfed10252a83f721572a1bf2e7ce/public/images/header-columns-bg.gif -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | MakeAPI 5 | 6 | 26 | 27 | 28 |
29 |
30 |

Halt!

31 |

These are not the droids you're looking for. Maybe you're looking for the MakeAPI Docs ?

32 |
33 |
34 | 35 | 36 | -------------------------------------------------------------------------------- /public/js/external/jquery.event.drag-2.2.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * jquery.event.drag - v 2.2 3 | * Copyright (c) 2010 Three Dub Media - http://threedubmedia.com 4 | * Open Source MIT License - http://threedubmedia.com/code/license 5 | */ 6 | // Created: 2008-06-04 7 | // Updated: 2012-05-21 8 | // REQUIRES: jquery 1.7.x 9 | 10 | (function(a){a.fn.drag=function(b,c,d){var e="string"==typeof b?b:"",f=a.isFunction(b)?b:a.isFunction(c)?c:null;return 0!==e.indexOf("drag")&&(e="drag"+e),d=(b==f?c:d)||{},f?this.bind(e,d,f):this.trigger(e)};var b=a.event,c=b.special,d=c.drag={defaults:{which:1,distance:0,not:":input",handle:null,relative:!1,drop:!0,click:!1},datakey:"dragdata",noBubble:!0,add:function(b){var c=a.data(this,d.datakey),e=b.data||{};c.related+=1,a.each(d.defaults,function(a){void 0!==e[a]&&(c[a]=e[a])})},remove:function(){a.data(this,d.datakey).related-=1},setup:function(){if(!a.data(this,d.datakey)){var c=a.extend({related:0},d.defaults);a.data(this,d.datakey,c),b.add(this,"touchstart mousedown",d.init,c),this.attachEvent&&this.attachEvent("ondragstart",d.dontstart)}},teardown:function(){var c=a.data(this,d.datakey)||{};c.related||(a.removeData(this,d.datakey),b.remove(this,"touchstart mousedown",d.init),d.textselect(!0),this.detachEvent&&this.detachEvent("ondragstart",d.dontstart))},init:function(e){if(!d.touched){var g,f=e.data;if(!(0!=e.which&&f.which>0&&e.which!=f.which||a(e.target).is(f.not)||f.handle&&!a(e.target).closest(f.handle,e.currentTarget).length||(d.touched="touchstart"==e.type?this:null,f.propagates=1,f.mousedown=this,f.interactions=[d.interaction(this,f)],f.target=e.target,f.pageX=e.pageX,f.pageY=e.pageY,f.dragging=null,g=d.hijack(e,"draginit",f),!f.propagates)))return g=d.flatten(g),g&&g.length&&(f.interactions=[],a.each(g,function(){f.interactions.push(d.interaction(this,f))})),f.propagates=f.interactions.length,f.drop!==!1&&c.drop&&c.drop.handler(e,f),d.textselect(!1),d.touched?b.add(d.touched,"touchmove touchend",d.handler,f):b.add(document,"mousemove mouseup",d.handler,f),!d.touched||f.live?!1:void 0}},interaction:function(b,c){var e=a(b)[c.relative?"position":"offset"]()||{top:0,left:0};return{drag:b,callback:new d.callback,droppable:[],offset:e}},handler:function(e){var f=e.data;switch(e.type){case!f.dragging&&"touchmove":e.preventDefault();case!f.dragging&&"mousemove":if(Math.pow(e.pageX-f.pageX,2)+Math.pow(e.pageY-f.pageY,2)++l);return c.type=i.type,c.originalEvent=i.event,d.flatten(f.results)}},properties:function(a,b,c){var e=c.callback;return e.drag=c.drag,e.proxy=c.proxy||c.drag,e.startX=b.pageX,e.startY=b.pageY,e.deltaX=a.pageX-b.pageX,e.deltaY=a.pageY-b.pageY,e.originalX=c.offset.left,e.originalY=c.offset.top,e.offsetX=e.originalX+e.deltaX,e.offsetY=e.originalY+e.deltaY,e.drop=d.flatten((c.drop||[]).slice()),e.available=d.flatten((c.droppable||[]).slice()),e},element:function(a){return a&&(a.jquery||1==a.nodeType)?a:void 0},flatten:function(b){return a.map(b,function(b){return b&&b.jquery?a.makeArray(b):b&&b.length?d.flatten(b):b})},textselect:function(b){a(document)[b?"unbind":"bind"]("selectstart",d.dontstart).css("MozUserSelect",b?"":"none"),document.unselectable=b?"off":"on"},dontstart:function(){return!1},callback:function(){}};d.callback.prototype={update:function(){c.drop&&this.available.length&&a.each(this.available,function(a){c.drop.locate(this,a)})}};var e=b.dispatch;b.dispatch=function(b){return a.data(this,"suppress."+b.type)-(new Date).getTime()>0?(a.removeData(this,"suppress."+b.type),void 0):e.apply(this,arguments)};var f=b.fixHooks.touchstart=b.fixHooks.touchmove=b.fixHooks.touchend=b.fixHooks.touchcancel={props:"clientX clientY pageX pageY screenX screenY".split(" "),filter:function(b,c){if(c){var d=c.touches&&c.touches[0]||c.changedTouches&&c.changedTouches[0]||null;d&&a.each(f.props,function(a,c){b[c]=d[c]})}return b}};c.draginit=c.dragstart=c.dragend=d})(jQuery); 11 | -------------------------------------------------------------------------------- /public/js/external/slick.core.js: -------------------------------------------------------------------------------- 1 | /*** 2 | * Contains core SlickGrid classes. 3 | * @module Core 4 | * @namespace Slick 5 | */ 6 | 7 | (function(a){function b(){var a=!1,b=!1;this.stopPropagation=function(){a=!0},this.isPropagationStopped=function(){return a},this.stopImmediatePropagation=function(){b=!0},this.isImmediatePropagationStopped=function(){return b}}function c(){var a=[];this.subscribe=function(b){a.push(b)},this.unsubscribe=function(b){for(var c=a.length-1;c>=0;c--)a[c]===b&&a.splice(c,1)},this.notify=function(c,d,e){d=d||new b,e=e||this;for(var f,g=0;a.length>g&&!d.isPropagationStopped()&&!d.isImmediatePropagationStopped();g++)f=a[g].call(e,d,c);return f}}function d(){var a=[];this.subscribe=function(b,c){return a.push({event:b,handler:c}),b.subscribe(c),this},this.unsubscribe=function(b,c){for(var d=a.length;d--;)if(a[d].event===b&&a[d].handler===c)return a.splice(d,1),b.unsubscribe(c),void 0;return this},this.unsubscribeAll=function(){for(var b=a.length;b--;)a[b].event.unsubscribe(a[b].handler);return a=[],this}}function e(a,b,c,d){void 0===c&&void 0===d&&(c=a,d=b),this.fromRow=Math.min(a,c),this.fromCell=Math.min(b,d),this.toRow=Math.max(a,c),this.toCell=Math.max(b,d),this.isSingleRow=function(){return this.fromRow==this.toRow},this.isSingleCell=function(){return this.fromRow==this.toRow&&this.fromCell==this.toCell},this.contains=function(a,b){return a>=this.fromRow&&this.toRow>=a&&b>=this.fromCell&&this.toCell>=b},this.toString=function(){return this.isSingleCell()?"("+this.fromRow+":"+this.fromCell+")":"("+this.fromRow+":"+this.fromCell+" - "+this.toRow+":"+this.toCell+")"}}function f(){this.__nonDataRow=!0}function g(){this.__group=!0,this.level=0,this.count=0,this.value=null,this.title=null,this.collapsed=!1,this.totals=null,this.rows=[],this.groups=null,this.groupingKey=null}function h(){this.__groupTotals=!0,this.group=null}function i(){var a=null;this.isActive=function(b){return b?a===b:null!==a},this.activate=function(b){if(b!==a){if(null!==a)throw"SlickGrid.EditorLock.activate: an editController is still active, can't activate another editController";if(!b.commitCurrentEdit)throw"SlickGrid.EditorLock.activate: editController must implement .commitCurrentEdit()";if(!b.cancelCurrentEdit)throw"SlickGrid.EditorLock.activate: editController must implement .cancelCurrentEdit()";a=b}},this.deactivate=function(b){if(a!==b)throw"SlickGrid.EditorLock.deactivate: specified editController is not the currently active one";a=null},this.commitCurrentEdit=function(){return a?a.commitCurrentEdit():!0},this.cancelCurrentEdit=function(){return a?a.cancelCurrentEdit():!0}}a.extend(!0,window,{Slick:{Event:c,EventData:b,EventHandler:d,Range:e,NonDataRow:f,Group:g,GroupTotals:h,EditorLock:i,GlobalEditorLock:new i}}),g.prototype=new f,g.prototype.equals=function(a){return this.value===a.value&&this.count===a.count&&this.collapsed===a.collapsed},h.prototype=new f})(jQuery); 8 | -------------------------------------------------------------------------------- /public/js/external/slick.dataview.js: -------------------------------------------------------------------------------- 1 | (function(e){function t(t){function D(){c=true}function P(){c=false;Dt()}function H(e){v=e}function B(e){g=e}function j(e){e=e||0;var t;for(var n=e,r=s.length;n=c){o[o.length]=f}else{n=t[f];r=e[f];if(x.length&&(s=n.__nonDataRow||r.__nonDataRow)&&n.__group!==r.__group||n.__group&&!n.equals(r)||s&&(n.__groupTotals||r.__groupTotals)||n[i]!=r[i]||l&&l[n[i]]){o[o.length]=f}}}return o}function _t(e){a=null;if(v.isFilterNarrowing!=m.isFilterNarrowing||v.isFilterExpanding!=m.isFilterExpanding){E=[]}var t=Ot(e);A=t.totalRows;var n=t.rows;T=[];if(x.length){T=bt(n);if(T.length){Et(T);St(T);n=xt(T)}}var r=Mt(o,n);o=n;return r}function Dt(){if(c){return}var e=o.length;var t=A;var r=_t(s,f);if(k&&A0){M.notify({rows:r},null,n)}}function Pt(e,t){function s(){if(r.length>0){i=true;var s=n.mapIdsToRows(r);if(!t){r=n.mapRowsToIds(s)}e.setSelectedRows(s);i=false}}var n=this;var r=n.mapRowsToIds(e.getSelectedRows());var i;e.onSelectedRowsChanged.subscribe(function(t,s){if(i){return}r=n.mapRowsToIds(e.getSelectedRows())});this.onRowsChanged.subscribe(s);this.onRowCountChanged.subscribe(s)}function Ht(e,t){function s(e){n={};for(var t in e){var r=o[t][i];n[r]=e[t]}}function u(){if(n){r=true;et();var i={};for(var s in n){var o=a[s];if(o!=undefined){i[o]=n[s]}}e.setCellCssStyles(t,i);r=false}}var n;var r;s(e.getCellCssStyles(t));e.onCellCssStylesChanged.subscribe(function(e,n){if(r){return}if(t!=n.key){return}if(n.hash){s(n.hash)}});this.onRowsChanged.subscribe(u);this.onRowCountChanged.subscribe(u)}var n=this;var r={groupItemMetadataProvider:null,inlineFilters:false};var i="id";var s=[];var o=[];var u={};var a=null;var f=null;var l=null;var c=false;var h=true;var p;var d;var v={};var m={};var g;var y=[];var b;var w;var E=[];var S={getter:null,formatter:null,comparer:function(e,t){return e.value-t.value},predefinedValues:[],aggregators:[],aggregateEmpty:false,aggregateCollapsed:false,aggregateChildGroups:false,collapsed:false,displayTotalsRow:true};var x=[];var T=[];var N=[];var C=":|:";var k=0;var L=0;var A=0;var O=new Slick.Event;var M=new Slick.Event;var _=new Slick.Event;t=e.extend(true,{},r,t);return{beginUpdate:D,endUpdate:P,setPagingOptions:R,getPagingInfo:U,getItems:I,setItems:q,setFilter:V,sort:z,fastSort:W,reSort:X,setGrouping:K,getGrouping:J,groupBy:Q,setAggregators:G,collapseAllGroups:pt,expandAllGroups:dt,collapseGroup:mt,expandGroup:gt,getGroups:yt,getIdxById:Z,getRowById:tt,getItemById:nt,getItemByIdx:Y,mapRowsToIds:it,mapIdsToRows:rt,setRefreshHints:H,setFilterArgs:B,refresh:Dt,updateItem:st,insertItem:ot,addItem:ut,deleteItem:at,syncGridSelection:Pt,syncGridCellCssStyles:Ht,getLength:ft,getItem:lt,getItemMetadata:ct,onRowCountChanged:O,onRowsChanged:M,onPagingInfoChanged:_}}function n(e){this.field_=e;this.init=function(){this.count_=0;this.nonNullCount_=0;this.sum_=0};this.accumulate=function(e){var t=e[this.field_];this.count_++;if(t!=null&&t!==""&&t!==NaN){this.nonNullCount_++;this.sum_+=parseFloat(t)}};this.storeResult=function(e){if(!e.avg){e.avg={}}if(this.nonNullCount_!=0){e.avg[this.field_]=this.sum_/this.nonNullCount_}}}function r(e){this.field_=e;this.init=function(){this.min_=null};this.accumulate=function(e){var t=e[this.field_];if(t!=null&&t!==""&&t!==NaN){if(this.min_==null||tthis.max_){this.max_=t}}};this.storeResult=function(e){if(!e.max){e.max={}}e.max[this.field_]=this.max_}}function s(e){this.field_=e;this.init=function(){this.sum_=null};this.accumulate=function(e){var t=e[this.field_];if(t!=null&&t!==""&&t!==NaN){this.sum_+=parseFloat(t)}};this.storeResult=function(e){if(!e.sum){e.sum={}}e.sum[this.field_]=this.sum_}}e.extend(true,window,{Slick:{Data:{DataView:t,Aggregators:{Avg:n,Min:r,Max:i,Sum:s}}}})})(jQuery) 2 | -------------------------------------------------------------------------------- /public/js/external/slick.editors.js: -------------------------------------------------------------------------------- 1 | /*** 2 | * Contains basic SlickGrid editors. 3 | * @module Editors 4 | * @namespace Slick 5 | */ 6 | 7 | (function($){$.extend(true,window,{"Slick":{"Editors":{"Text":TextEditor,"Integer":IntegerEditor,"Date":DateEditor,"YesNoSelect":YesNoSelectEditor,"Checkbox":CheckboxEditor,"PercentComplete":PercentCompleteEditor,"LongText":LongTextEditor}}});function TextEditor(args){var $input;var defaultValue;var scope=this;this.init=function(){$input=$("").appendTo(args.container).bind("keydown.nav",function(e){if(e.keyCode===$.ui.keyCode.LEFT||e.keyCode===$.ui.keyCode.RIGHT){e.stopImmediatePropagation()}}).focus().select()};this.destroy=function(){$input.remove()};this.focus=function(){$input.focus()};this.getValue=function(){return $input.val()};this.setValue=function(val){$input.val(val)};this.loadValue=function(item){defaultValue=item[args.column.field]||"";$input.val(defaultValue);$input[0].defaultValue=defaultValue;$input.select()};this.serializeValue=function(){return $input.val()};this.applyValue=function(item,state){item[args.column.field]=state};this.isValueChanged=function(){return(!($input.val()==""&&defaultValue==null))&&($input.val()!=defaultValue)};this.validate=function(){if(args.column.validator){var validationResults=args.column.validator($input.val());if(!validationResults.valid){return validationResults}}return{valid:true,msg:null}};this.init()}function IntegerEditor(args){var $input;var defaultValue;var scope=this;this.init=function(){$input=$("");$input.bind("keydown.nav",function(e){if(e.keyCode===$.ui.keyCode.LEFT||e.keyCode===$.ui.keyCode.RIGHT){e.stopImmediatePropagation()}});$input.appendTo(args.container);$input.focus().select()};this.destroy=function(){$input.remove()};this.focus=function(){$input.focus()};this.loadValue=function(item){defaultValue=item[args.column.field];$input.val(defaultValue);$input[0].defaultValue=defaultValue;$input.select()};this.serializeValue=function(){return parseInt($input.val(),10)||0};this.applyValue=function(item,state){item[args.column.field]=state};this.isValueChanged=function(){return(!($input.val()==""&&defaultValue==null))&&($input.val()!=defaultValue)};this.validate=function(){if(isNaN($input.val())){return{valid:false,msg:"Please enter a valid integer"}}return{valid:true,msg:null}};this.init()}function DateEditor(args){var $input;var defaultValue;var scope=this;var calendarOpen=false;this.init=function(){$input=$("");$input.appendTo(args.container);$input.focus().select();$input.datepicker({showOn:"button",buttonImageOnly:true,buttonImage:"../images/calendar.gif",beforeShow:function(){calendarOpen=true},onClose:function(){calendarOpen=false}});$input.width($input.width()-18)};this.destroy=function(){$.datepicker.dpDiv.stop(true,true);$input.datepicker("hide");$input.datepicker("destroy");$input.remove()};this.show=function(){if(calendarOpen){$.datepicker.dpDiv.stop(true,true).show()}};this.hide=function(){if(calendarOpen){$.datepicker.dpDiv.stop(true,true).hide()}};this.position=function(position){if(!calendarOpen){return}$.datepicker.dpDiv.css("top",position.top+30).css("left",position.left)};this.focus=function(){$input.focus()};this.loadValue=function(item){defaultValue=item[args.column.field];$input.val(defaultValue);$input[0].defaultValue=defaultValue;$input.select()};this.serializeValue=function(){return $input.val()};this.applyValue=function(item,state){item[args.column.field]=state};this.isValueChanged=function(){return(!($input.val()==""&&defaultValue==null))&&($input.val()!=defaultValue)};this.validate=function(){return{valid:true,msg:null}};this.init()}function YesNoSelectEditor(args){var $select;var defaultValue;var scope=this;this.init=function(){$select=$("");$select.appendTo(args.container);$select.focus()};this.destroy=function(){$select.remove()};this.focus=function(){$select.focus()};this.loadValue=function(item){$select.val((defaultValue=item[args.column.field])?"yes":"no");$select.select()};this.serializeValue=function(){return($select.val()=="yes")};this.applyValue=function(item,state){item[args.column.field]=state};this.isValueChanged=function(){return($select.val()!=defaultValue)};this.validate=function(){return{valid:true,msg:null}};this.init()}function CheckboxEditor(args){var $select;var defaultValue;var scope=this;this.init=function(){$select=$("");$select.appendTo(args.container);$select.focus()};this.destroy=function(){$select.remove()};this.focus=function(){$select.focus()};this.loadValue=function(item){defaultValue=!!item[args.column.field];if(defaultValue){$select.attr("checked","checked")}else{$select.removeAttr("checked")}};this.serializeValue=function(){return!!$select.attr("checked")};this.applyValue=function(item,state){item[args.column.field]=state};this.isValueChanged=function(){return(this.serializeValue()!==defaultValue)};this.validate=function(){return{valid:true,msg:null}};this.init()}function PercentCompleteEditor(args){var $input,$picker;var defaultValue;var scope=this;this.init=function(){$input=$("");$input.width($(args.container).innerWidth()-25);$input.appendTo(args.container);$picker=$("
").appendTo(args.container);$picker.append("
");$picker.find(".editor-percentcomplete-buttons").append("

");$input.focus().select();$picker.find(".editor-percentcomplete-slider").slider({orientation:"vertical",range:"min",value:defaultValue,slide:function(event,ui){$input.val(ui.value)}});$picker.find(".editor-percentcomplete-buttons button").bind("click",function(e){$input.val($(this).attr("val"));$picker.find(".editor-percentcomplete-slider").slider("value",$(this).attr("val"))})};this.destroy=function(){$input.remove();$picker.remove()};this.focus=function(){$input.focus()};this.loadValue=function(item){$input.val(defaultValue=item[args.column.field]);$input.select()};this.serializeValue=function(){return parseInt($input.val(),10)||0};this.applyValue=function(item,state){item[args.column.field]=state};this.isValueChanged=function(){return(!($input.val()==""&&defaultValue==null))&&((parseInt($input.val(),10)||0)!=defaultValue)};this.validate=function(){if(isNaN(parseInt($input.val(),10))){return{valid:false,msg:"Please enter a valid positive number"}}return{valid:true,msg:null}};this.init()}function LongTextEditor(args){var $input,$wrapper;var defaultValue;var scope=this;this.init=function(){var $container=$("body");$wrapper=$("
").appendTo($container);$input=$(" 162 |
163 | 164 | 165 | {% endif %} 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | -------------------------------------------------------------------------------- /views/login.html: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | 7 | 8 | Sign in 9 | 10 | 11 | 12 | 13 | 14 | 22 | 23 | 24 |
25 | 39 |
40 | 41 |

Make Editor Login

42 |

Sign in with your Webmaker account Above

43 |
44 | 45 |
46 | 47 | 48 | 49 | 50 | 51 | 52 | --------------------------------------------------------------------------------