File size: 5,473 Bytes
5c2ed06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var remote_exports = {};
__export(remote_exports, {
  ATTRIBUTES: () => ATTRIBUTES,
  Limiter: () => Limiter,
  PM: () => PM,
  RemoteClassifier: () => RemoteClassifier,
  limiter: () => limiter
});
module.exports = __toCommonJS(remote_exports);
var import_lib = require("../../lib");
var import_config_loader = require("../config-loader");
var import_dex_data = require("../../sim/dex-data");
const PM_TIMEOUT = 20 * 60 * 1e3;
const ATTRIBUTES = {
  "SEVERE_TOXICITY": {},
  "TOXICITY": {},
  "IDENTITY_ATTACK": {},
  "INSULT": {},
  "PROFANITY": {},
  "THREAT": {},
  "SEXUALLY_EXPLICIT": {},
  "FLIRTATION": {}
};
function time() {
  return Math.floor(Math.floor(Date.now() / 1e3) / 60);
}
class Limiter {
  constructor(max) {
    this.lastTick = time();
    this.count = 0;
    this.max = max;
  }
  shouldRequest() {
    const now = time();
    if (this.lastTick !== now) {
      this.count = 0;
      this.lastTick = now;
    }
    this.count++;
    return this.count < this.max;
  }
}
function isCommon(message) {
  message = message.toLowerCase().replace(/\?!\., ;:/g, "");
  return ["gg", "wp", "ggwp", "gl", "hf", "glhf", "hello"].includes(message);
}
let throttleTime = null;
const limiter = new Limiter(800);
const PM = new import_lib.ProcessManager.QueryProcessManager(module, async (text) => {
  if (isCommon(text) || !limiter.shouldRequest())
    return null;
  if (throttleTime && Date.now() - throttleTime < 1e4) {
    return null;
  }
  if (throttleTime)
    throttleTime = null;
  const requestData = {
    // todo - support 'es', 'it', 'pt', 'fr' - use user.language? room.settings.language...?
    languages: ["en"],
    requestedAttributes: ATTRIBUTES,
    comment: { text }
  };
  try {
    const raw = await (0, import_lib.Net)(`https://commentanalyzer.googleapis.com/v1alpha1/comments:analyze`).post({
      query: {
        key: import_config_loader.Config.perspectiveKey
      },
      body: JSON.stringify(requestData),
      headers: {
        "Content-Type": "application/json"
      },
      timeout: 10 * 1e3
      // 10s
    });
    if (!raw)
      return null;
    const data = JSON.parse(raw);
    if (data.error)
      throw new Error(data.message);
    const result = {};
    for (const k in data.attributeScores) {
      const score = data.attributeScores[k];
      result[k] = score.summaryScore.value;
    }
    return result;
  } catch (e) {
    throttleTime = Date.now();
    if (e.message.startsWith("Request timeout") || e.statusCode === 429) {
      return null;
    }
    Monitor.crashlog(e, "A Perspective API request", { request: JSON.stringify(requestData) });
    return null;
  }
}, PM_TIMEOUT);
if (require.main === module) {
  global.Config = import_config_loader.Config;
  global.Monitor = {
    crashlog(error, source = "A remote Artemis child process", details = null) {
      const repr = JSON.stringify([error.name, error.message, source, details]);
      process.send(`THROW
@!!@${repr}
${error.stack}`);
    },
    slow(text) {
      process.send(`CALLBACK
SLOW
${text}`);
    }
  };
  global.toID = import_dex_data.toID;
  process.on("uncaughtException", (err) => {
    if (import_config_loader.Config.crashguard) {
      Monitor.crashlog(err, "A remote Artemis child process");
    }
  });
  import_lib.Repl.start(`abusemonitor-remote-${process.pid}`, (cmd) => eval(cmd));
} else if (!process.send) {
  PM.spawn(import_config_loader.Config.remoteartemisprocesses || 1);
}
class RemoteClassifier {
  classify(text) {
    if (!import_config_loader.Config.perspectiveKey)
      return Promise.resolve(null);
    return PM.query(text);
  }
  async suggestScore(text, data) {
    if (!import_config_loader.Config.perspectiveKey)
      return Promise.resolve(null);
    const body = {
      comment: { text },
      attributeScores: {}
    };
    for (const k in data) {
      body.attributeScores[k] = { summaryScore: { value: data[k] } };
    }
    try {
      const raw = await (0, import_lib.Net)(`https://commentanalyzer.googleapis.com/v1alpha1/comments:suggestscore`).post({
        query: {
          key: import_config_loader.Config.perspectiveKey
        },
        body: JSON.stringify(body),
        headers: {
          "Content-Type": "application/json"
        },
        timeout: 10 * 1e3
        // 10s
      });
      return JSON.parse(raw);
    } catch (e) {
      return { error: e.message };
    }
  }
  destroy() {
    return PM.destroy();
  }
  respawn() {
    return PM.respawn();
  }
  spawn(number) {
    PM.spawn(number);
  }
  getActiveProcesses() {
    return PM.processes.length;
  }
}
RemoteClassifier.PM = PM;
RemoteClassifier.ATTRIBUTES = ATTRIBUTES;
//# sourceMappingURL=remote.js.map