summaryrefslogtreecommitdiff
path: root/lib/note.js
blob: dc384b7fec868e079f20c4147aa6e78e53133b0e (plain)
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
198
199
200
201
//note
//external modules
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var LZString = require('lz-string');
var marked = require('marked');
var cheerio = require('cheerio');
var shortId = require('shortid');

//others
var db = require("./db.js");
var logger = require("./logger.js");

//permission types
permissionTypes = ["freely", "editable", "locked"];

// create a note model
var model = mongoose.model('note', {
    id: String,
    shortid: {
        type: String,
        unique: true,
        default: shortId.generate
    },
    permission: {
        type: String,
        enum: permissionTypes
    },
    viewcount: {
        type: Number,
        default: 0
    },
    updated: Date,
    created: Date
});

//public
var note = {
    model: model,
    findNote: findNote,
    newNote: newNote,
    findOrNewNote: findOrNewNote,
    checkNoteIdValid: checkNoteIdValid,
    checkNoteExist: checkNoteExist,
    getNoteTitle: getNoteTitle,
    generateWebTitle: generateWebTitle,
    increaseViewCount: increaseViewCount,
    updatePermission: updatePermission
};

function checkNoteIdValid(noteId) {
    try {
        //logger.info(noteId);
        var id = LZString.decompressFromBase64(noteId);
        if (!id) return false;
        var uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
        var result = id.match(uuidRegex);
        if (result && result.length == 1)
            return true;
        else
            return false;
    } catch (err) {
        logger.error(err);
        return false;
    }
}

function checkNoteExist(noteId) {
    try {
        //logger.info(noteId);
        var id = LZString.decompressFromBase64(noteId);
        db.readFromDB(id, function (err, result) {
            if (err) return false;
            return true;
        });
    } catch (err) {
        logger.error(err);
        return false;
    }
}

//get title
function getNoteTitle(body) {
    var $ = cheerio.load(marked(body));
    var h1s = $("h1");
    var title = "";
    if (h1s.length > 0 && h1s.first().text().split('\n').length == 1)
        title = h1s.first().text();
    else
        title = "Untitled";
    return title;
}

//generate note web page title
function generateWebTitle(title) {
    title = !title || title == "Untitled" ? "HackMD - Collaborative notes" : title + " - HackMD";
    return title;
}

function findNote(id, callback) {
    model.findOne({
        $or: [
            {
                id: id
            },
            {
                shortid: id
            }
        ]
    }, function (err, note) {
        if (err) {
            logger.error('find note failed: ' + err);
            callback(err, null);
        }
        if (!err && note) {
            callback(null, note);
        } else {
            logger.error('find note failed: ' + err);
            callback(err, null);
        };
    });
}

function newNote(id, permission, callback) {
    var note = new model({
        id: id,
        permission: permission,
        updated: Date.now(),
        created: Date.now()
    });
    note.save(function (err) {
        if (err) {
            logger.error('new note failed: ' + err);
            callback(err, null);
        } else {
            logger.info("new note success: " + note.id);
            callback(null, note);
        };
    });
}

function findOrNewNote(id, permission, callback) {
    findNote(id, function (err, note) {
        if (err || !note) {
            newNote(id, permission, function (err, note) {
                if (err) {
                    logger.error('find or new note failed: ' + err);
                    callback(err, null);
                } else {
                    callback(null, note);
                }
            });
        } else {
            if (!note.permission) {
                note.permission = permission;
                note.updated = Date.now();
                note.save(function (err) {
                    if (err) {
                        logger.error('add note permission failed: ' + err);
                        callback(err, null);
                    } else {
                        logger.info("add note permission success: " + note.id);
                        callback(null, note);
                    };
                });
            } else {
                callback(null, note);
            }
        }
    });
}

function increaseViewCount(note, callback) {
    note.viewcount++;
    note.updated = Date.now();
    note.save(function (err) {
        if (err) {
            logger.error('increase note viewcount failed: ' + err);
            callback(err, null);
        } else {
            logger.info("increase note viewcount success: " + note.id);
            callback(null, note);
        };
    });
}

function updatePermission(note, permission, callback) {
    note.permission = permission;
    note.updated = Date.now();
    note.save(function (err) {
        if (err) {
            logger.error('update note permission failed: ' + err);
            callback(err, null);
        } else {
            logger.info("update note permission success: " + note.id);
            callback(null, note);
        };
    });
}

module.exports = note;