-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.js
197 lines (162 loc) · 3.57 KB
/
message.js
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
188
189
190
191
192
193
194
195
196
197
const crypto = require('crypto');
const User = require('./user');
const consts = require('./consts').message;
const DB = require('./db');
const wechat = require('./wechat');
class Message{
constructor(id) {
if(!Message.has(id)) throw new Error("Message not exist");
this.id = id;
}
get(k) {
return Message.db.data[this.id][k];
}
set(k, v) {
Message.db.data[this.id][k] = v;
Message.db.update();
}
remove(k) {
if(!k) {
delete Message.db.data[this.id];
} else {
delete Message.db.data[this.id][k];
}
Message.db.update();
}
get to() {
switch(this.get("to")) {
case "officer":
return "干事";
case "admin":
return "管理员";
default:
return (new User(this.get("to"))).name;
}
}
set to(to) {
this.set("to", to);
}
get user() {
return new User(this.userid);
}
get from() {
switch(this.user.role) {
case "association":
return this.user.name;
case "officer":
return "干事";
case "admin":
return "管理员";
}
}
get userid() {
return this.get("userid");
}
set userid(userid) {
this.set("userid", userid);
}
get read() {
return this.get("read");
}
set read(read) {
this.set("read", read);
}
get time() {
return this.get("time");
}
set time(time) {
this.set("time", time);
}
get title() {
return this.get("title");
}
set title(title) {
this.set("title", title)
}
get content() {
return this.get("content");
}
set content(content) {
this.set("content", content);
}
get score() {
return this.get("score");
}
set score(score) {
this.set("score", score);
}
static add(obj) {
Message.db.data[obj.id] = obj;
Message.db.update();
return new Message(obj.id);
}
static create(opts) {
if(!opts) throw new TypeError("Options not defined");
opts.id = Message.createId();
opts.time = Date.now();
const msg = `您有新的消息,请及时查收。${require("./consts").http.origin}/message/${opts.id}`;
if(!User.has(opts.to)) { //admin or officer
if(opts.to === "admin") {
utils.notifyAdmin(msg);
} else {
utils.notifyOfficer(new User(opts.userid), msg);
}
} else {
wechat.send({
user:new User(opts.to).name,
msg
});
}
return Message.add(opts);
}
static has(id) {
return (id in Message.db.data);
}
static createId(){
const id = crypto.randomBytes(consts.length).toString("hex");
if(Message.has(id)) {
return Message.createId();
}
delete Message.db.data[id];
Message.db.update();
return id;
}
static getMessagesById(id) {
const messages = [];
for(let i in Message.db.data) {
if(Message.db.data[i].to === id) {
messages.push(new Message(i));
}
}
return messages;
}
static getMessagesByAuthorId(id) {
const messages = [];
for(let i in Message.db.data) {
if(Message.db.data[i].userid === id) {
messages.push(new Message(i));
}
}
return messages;
}
static getMessagesByType(type) {
const messages = [];
for(let i in Message.db.data) {
const message = new Message(i);
if(message.user.type === type) {
messages.push(message);
}
}
return messages;
}
static get all() {
const messages = [];
for(let i in Message.db.data) {
messages.push(new Message(i));
}
return messages;
}
}
Message.db = new DB(consts.file);
module.exports = Message;
const utils = require('./utils');