-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathservice.js
150 lines (128 loc) · 3.35 KB
/
service.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
'use strict'
const { Note, User } = require('../models')
const config = require('../config')
const logger = require('../logger')
const realtime = require('../realtime/realtime')
const { updateHistory } = require('../history')
const EXIST_WEB_ROUTES = ['', 'new', 'me', 'history', '403', '404', '500', 'config']
const FORBIDDEN_ALIASES = [...EXIST_WEB_ROUTES, ...config.forbiddenNoteIDs]
exports.getNote = async (originAliasOrNoteId, { includeUser } = { includeUser: false }) => {
const id = await Note.parseNoteIdAsync(originAliasOrNoteId)
const includes = []
if (includeUser) {
includes.push({
model: User,
as: 'owner'
}, {
model: User,
as: 'lastchangeuser'
})
}
const note = await Note.findOne({
where: {
id: id
},
include: includes
})
return note
}
exports.createNote = async (userId, noteAlias) => {
if (!config.allowAnonymous && !userId) {
throw new Error('can not create note')
}
const note = await Note.create({
ownerId: userId,
alias: noteAlias
})
if (userId) {
updateHistory(userId, note)
}
return note
}
exports.canViewNote = (note, isLogin, userId) => {
if (note.permission === 'private') {
return note.ownerId === userId
}
if (note.permission === 'limited' || note.permission === 'protected') {
return isLogin
}
return true
}
exports.getMyNoteList = async (userId, callback) => {
const myNotes = await Note.findAll({
where: {
ownerId: userId
}
})
if (!myNotes) {
return callback(null, null)
}
try {
const myNoteList = myNotes.map(note => ({
id: Note.encodeNoteId(note.id),
text: note.title,
tags: Note.parseNoteInfo(note.content).tags,
createdAt: note.createdAt,
lastchangeAt: note.lastchangeAt,
shortId: note.shortid
}))
if (config.debug) {
logger.info('Parse myNoteList success: ' + userId)
}
return callback(null, myNoteList)
} catch (err) {
logger.error('Parse myNoteList failed')
return callback(err, null)
}
}
const sanitizeAlias = (alias) => {
return alias.replace(/( |\/)/g, '')
}
const checkAliasValid = async (originAliasOrNoteId, alias) => {
const sanitizedAlias = sanitizeAlias(alias)
if (FORBIDDEN_ALIASES.includes(sanitizedAlias)) {
return {
isValid: false,
errorMessage: 'The url is exist.'
}
}
if (!/^[0-9a-z-_]+$/.test(alias)) {
return {
isValid: false,
errorMessage: 'The url must be lowercase letters, decimal digits, hyphen or underscore.'
}
}
const conflictNote = await exports.getNote(alias)
const note = await exports.getNote(originAliasOrNoteId)
if (conflictNote && conflictNote.id !== note.id) {
return {
isValid: false,
errorMessage: 'The url is exist.'
}
}
return {
isValid: true
}
}
exports.updateAlias = async (originAliasOrNoteId, alias) => {
const sanitizedAlias = sanitizeAlias(alias)
const note = await exports.getNote(originAliasOrNoteId)
const { isValid, errorMessage } = await checkAliasValid(originAliasOrNoteId, alias)
if (!isValid) {
return {
isSuccess: false,
errorMessage
}
}
const updatedNote = await note.update({
alias: sanitizedAlias,
lastchangeAt: Date.now()
})
realtime.io.to(updatedNote.id)
.emit('alias updated', {
alias: updatedNote.alias
})
return {
isSuccess: true
}
}