-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Feat/custom note url #1699
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
a60814billy
merged 6 commits into
hackmdio:feature/custom-note-url-base
from
Nick0603:feature/custom-note-url
Jul 23, 2021
Merged
Feat/custom note url #1699
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1579f20
create custom-note-url flow
Nick0603 dab0c66
fix linter error
Nick0603 dc9c98e
Update lib/migrations/202106200000000-create-note-aliases-history.js
Nick0603 0777f2d
Update lib/migrations/202106200000000-create-note-aliases-history.js
Nick0603 014790a
Clean code
Nick0603 d72d6a0
revmoe archivedAlias logic
Nick0603 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return 404 for note not found