Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
71 lines (64 loc) · 2.34 KB
/
Copy pathserver.js
File metadata and controls
71 lines (64 loc) · 2.34 KB
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
import { makeFetch } from "https://code4fukui.github.io/PubkeyUser/serverutil.js";
import { Posts } from "./Posts.js";
import { JSONLWriter } from "https://code4fukui.github.io/JSONL/JSONLWriter.js";
import { DateTime, TimeZone } from "https://js.sabae.cc/DateTime.js";
import { subscribe, unsubscribe, pushAll } from "https://code4fukui.github.io/tsuchichat/webpushutil.js";
const settings = JSON.parse(await Deno.readTextFile("./static/settings.json"));
const title = settings.title;
const url = settings.url;
const posts = await Posts.create();
const logdir = "log";
await Deno.mkdir(logdir, { recursive: true });
const log = async (pubkey, path, param, req, conn) => {
const ua = req.headers.get("user-agent");
const dt = new DateTime();
const ymd = dt.toLocal(TimeZone.JST).day.toStringYMD();
const w = new JSONLWriter(logdir + "/" + ymd + ".jsonl", true);
await w.writeRecord({ pubkey, path, param, ua });
w.close();
};
const sendNotify = async (uuid, text, room) => {
if (!uuid) return;
const data = {
title: room ? room + " - " + title : title,
body: text,
data: { url: url + "#" + encodeURIComponent(room) },
//timeout: 5000, // 通知を消すまでの長さ msec (デフォルト0:消さない)
//delay: 1000, // 表示するまでの時間 msec(デフォルト0)
};
await pushAll(uuid, data);
};
const api = async (path, param, pubkey, req, conn) => {
//console.log("api", path, path == "add", param, pubkey)
log(pubkey, path, param, req, conn);
if (!pubkey) return "no pubkey";
if (path == "add") {
const post = param;
const res = await posts.add(post);
if (param.data.action != "remove" && param.data.action != "like") {
sendNotify(param.data.uuid, param.data.body, param.data.room);
}
return res;
} else if (path == "get") {
const p2 = await posts.get(id);
return p2;
} else if (path == "getLatest") {
const lastdt = param;
const latest = await posts.getLatest(lastdt);
return latest;
} else if (path == "subscribe") {
return subscribe(param);
} else if (path == "unsubscribe") {
return unsubscribe(param);
/*
} else if (path == "push") {
const uuid = param.uuid;
const data = param.data;
return pushAll(uuid, data);
*/
} else {
console.log("path", path)
return "not found";
}
};
export default { fetch: makeFetch(api) };