UNPKG

2.83 kBJavaScriptView Raw
1/**
2* vim:set sw=2 ts=2 sts=2 ft=javascript expandtab:
3*
4* # Hooks Module
5*
6* ## License
7*
8* Licensed to the Apache Software Foundation (ASF) under one
9* or more contributor license agreements. See the NOTICE file
10* distributed with this work for additional information
11* regarding copyright ownership. The ASF licenses this file
12* to you under the Apache License, Version 2.0 (the
13* "License"); you may not use this file except in compliance
14* with the License. You may obtain a copy of the License at
15*
16* http://www.apache.org/licenses/LICENSE-2.0
17*
18* Unless required by applicable law or agreed to in writing,
19* software distributed under the License is distributed on an
20* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21* KIND, either express or implied. See the License for the
22* specific language governing permissions and limitations
23* under the License.
24*
25* ## Description
26*
27* This module contains server-side hooks used by etherpad.
28*
29* ## Hooks
30*/
31
32module.exports = (function () {
33 'use strict';
34
35 // Dependencies
36 var configuration = require('./configuration.js');
37
38 var hooks = {};
39
40 /**
41 * `init` hook is runned once after plugin installation. At the moment, it
42 * only populates database.
43 */
44
45 hooks.init = function (name, args, callback) {
46 configuration.init(function (err) {
47 if (err) { console.error(err); }
48 callback();
49 });
50 };
51
52 /**
53 * `expressConfigure` hook profits from the args.app express instance to
54 * initialize API before YAJSML and then storage and mail.
55 */
56
57 hooks.expressConfigure = function (name, args, callback) {
58 var storage = require('./storage.js');
59 var api = require('./api.js');
60 var mail = require('./mail.js');
61 api.init(args.app, function () {
62 storage.init(function (err) {
63 if (err) { return callback(err); }
64 mail.init();
65 callback(null);
66 });
67 });
68 };
69
70 /**
71 * `removeAllData` is a hook function used when MyPads is uninstalled from
72 * Etherpad admin interface. It erases all MyPads data from database after
73 * checking that the plugin uninstalled is really MyPads.
74 */
75
76 hooks.removeAllData = function (name, context, callback) {
77 if ((name === 'pluginUninstall') && (context.plugin_name === 'ep_mypads')) {
78 var storage = require('./storage.js');
79 storage.db.findKeys(storage.DBPREFIX.GLOBAL + '*', null,
80 function (err, keys) {
81 console.log('Keys to be removed : ' + keys.join(', '));
82 if (err) { return callback(err); }
83 storage.fn.delKeys(keys, function (err) {
84 if (err) { return callback(err); }
85 console.log('data successfully removed');
86 callback(null);
87 });
88 }
89 );
90 } else {
91 callback(null);
92 }
93 };
94
95 return hooks;
96
97}).call(this);