aboutsummaryrefslogtreecommitdiffstats
path: root/etherpad/src/etherpad/control/pro_beta_control.js
blob: ec99b438f26bdac17ea1555dc8a0923055c59ef2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
 * Copyright 2009 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS-IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import("funhtml.*", "stringutils.*");
import("sqlbase.sqlobj");
import("sqlbase.sqlcommon");
import("stringutils");
import("email.sendEmail");

import("etherpad.utils.*");
import("etherpad.log");
import("etherpad.sessions.getSession");

jimport("java.lang.System.out.println");

function render_main_get() {
  if (isValveOpen()) {
    response.redirect("/ep/pro-signup/");
  }
  renderFramed("beta/signup.ejs", {
    errorMsg: getSession().betaSignupError
  });
  delete getSession().betaSignupError;
}

function render_signup_post() {
  // record in sql: [id, email, activated=false, activationCode]
  // log to disk

  var email = request.params.email;
  if (!isValidEmail(email)) {
    getSession().betaSignupError = "Invalid email address.";
    response.redirect('/ep/beta-account/');
  }

  // does email already exist?
  if (sqlobj.selectSingle('pro_beta_signups', {email: email})) {
    getSession().betaSignupError = "Email already signed up.";
    response.redirect('/ep/beta-account/');
  }

  sqlobj.insert('pro_beta_signups', {
    email: email,
    isActivated: false,
    signupDate: new Date()
  });

  response.redirect('/ep/beta-account/signup-ok');
}

function render_signup_ok() {
  renderNoticeString(
    DIV({style: "font-size: 16pt; border: 1px solid green; background: #eeffee; margin: 2em 4em; padding: 1em;"},
      P("Great!  We'll be in touch."),
        P("In the meantime, you can ", A({href: '/ep/pad/newpad', style: 'text-decoration: underline;'},
          "create a public pad"), " right now.")));
}

// return string if not valid, falsy otherwise.
function isValidCode(code) {
  if (isValveOpen()) {
    return undefined;
  }

  function wr(m) {
    return DIV(P(m), P("You can sign up for the beta ", 
            A({href: "/ep/beta-account/"}, "here")));
  }

  if (!code) {
    return wr("Invalid activation code.");
  }
  var record = sqlobj.selectSingle('pro_beta_signups', { activationCode: code });
  if (!record) {
    return wr("Invalid activation code.");
  }
  if (record.isActivated) {
    return wr("That activation code has already been used.");
  }
  return undefined;
}

function isValveOpen() {
  if (appjet.cache.proBetaValveIsOpen === undefined) {
    appjet.cache.proBetaValveIsOpen = true;
  }
  return appjet.cache.proBetaValveIsOpen;
}

function toggleValve() {
  appjet.cache.proBetaValveIsOpen = !appjet.cache.proBetaValveIsOpen;
}

function sendInvite(recordId) {
  var record = sqlobj.selectSingle('pro_beta_signups', {id: recordId});
  if (record.activationCode) {
    getSession().betaAdminMessage = "Already active";
    return;
  }

  // create activation code
  var code = stringutils.randomString(10);
  sqlcommon.inTransaction(function() {
    sqlobj.update('pro_beta_signups', {id: recordId}, {activationCode: code});
    var body = renderTemplateAsString('email/pro_beta_invite.ejs', {
      toAddr: record.email,
      signupAgo: timeAgo(record.signupDate),
      signupCode: code,
      activationUrl: "http://"+httpHost(request.host)+"/ep/pro-signup/?sc="+code
    });
    sendEmail(record.email, "EtherPad <support@pad.spline.inf.fu-berlin.de>",
              "Your EtherPad Professional Beta Account", {}, body);
  });

  getSession().betaAdminMessage = "Invite sent.";
}

function notifyActivated(code) {
  println("updating: "+code);
  sqlobj.update('pro_beta_signups', {activationCode: code}, 
                {isActivated: true, activationDate: new Date()});
}