Skip to content

Commit 36a1900

Browse files
committed
Update to make note history count in server-side when user logged
1 parent 1d2a982 commit 36a1900

3 files changed

Lines changed: 96 additions & 3 deletions

File tree

lib/models/note.js

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,23 @@ module.exports = function (sequelize, DataTypes) {
218218
return callback(null, null);
219219
});
220220
},
221+
parseNoteInfo: function (body) {
222+
var meta = null;
223+
try {
224+
var obj = metaMarked(body);
225+
body = obj.markdown;
226+
meta = obj.meta;
227+
} catch (err) {
228+
//na
229+
}
230+
if (!meta) meta = {};
231+
var $ = cheerio.load(md.render(body));
232+
return {
233+
title: Note.extractNoteTitle(meta, $),
234+
tags: Note.extractNoteTags(meta, $)
235+
};
236+
},
221237
parseNoteTitle: function (body) {
222-
var title = "";
223238
var meta = null;
224239
try {
225240
var obj = metaMarked(body);
@@ -229,10 +244,14 @@ module.exports = function (sequelize, DataTypes) {
229244
//na
230245
}
231246
if (!meta) meta = {};
247+
var $ = cheerio.load(md.render(body));
248+
return Note.extractNoteTitle(meta, $);
249+
},
250+
extractNoteTitle: function (meta, $) {
251+
var title = "";
232252
if (meta.title && (typeof meta.title == "string" || typeof meta.title == "number")) {
233253
title = meta.title;
234254
} else {
235-
var $ = cheerio.load(md.render(body));
236255
var h1s = $("h1");
237256
if (h1s.length > 0 && h1s.first().text().split('\n').length == 1)
238257
title = S(h1s.first().text()).stripTags().s;
@@ -250,6 +269,40 @@ module.exports = function (sequelize, DataTypes) {
250269
title = !title || title == "Untitled" ? "HackMD - Collaborative markdown notes" : title + " - HackMD";
251270
return title;
252271
},
272+
extractNoteTags: function (meta, $) {
273+
var tags = [];
274+
var rawtags = [];
275+
if (meta.tags && (typeof meta.tags == "string" || typeof meta.tags == "number")) {
276+
var metaTags = ('' + meta.tags).split(',');
277+
for (var i = 0; i < metaTags.length; i++) {
278+
var text = metaTags[i].trim();
279+
if (text) rawtags.push(text);
280+
}
281+
} else {
282+
var h6s = $("h6");
283+
h6s.each(function (key, value) {
284+
if (/^tags/gmi.test($(value).text())) {
285+
var codes = $(value).find("code");
286+
for (var i = 0; i < codes.length; i++) {
287+
var text = $(codes[i]).html().trim();
288+
if (text) rawtags.push(text);
289+
}
290+
}
291+
});
292+
}
293+
for (var i = 0; i < rawtags.length; i++) {
294+
var found = false;
295+
for (var j = 0; j < tags.length; j++) {
296+
if (tags[j] == rawtags[i]) {
297+
found = true;
298+
break;
299+
}
300+
}
301+
if (!found)
302+
tags.push(rawtags[i]);
303+
}
304+
return tags;
305+
},
253306
parseMeta: function (meta) {
254307
var _meta = {};
255308
if (meta) {

lib/realtime.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var moment = require('moment');
1313
//core
1414
var config = require("./config.js");
1515
var logger = require("./logger.js");
16+
var history = require("./history.js");
1617
var models = require("./models");
1718

1819
//ot
@@ -390,6 +391,12 @@ function finishConnection(socket, note, user) {
390391
note.server.setName(socket, user.name);
391392
note.server.setColor(socket, user.color);
392393

394+
// update user note history
395+
setTimeout(function () {
396+
var noteId = note.alias ? note.alias : LZString.compressToBase64(note.id);
397+
history.updateHistory(user.userid, noteId, note.server.document);
398+
}, 0);
399+
393400
emitOnlineUsers(socket);
394401
emitRefresh(socket);
395402

@@ -468,6 +475,7 @@ function startConnection(socket) {
468475

469476
notes[noteId] = {
470477
id: noteId,
478+
alias: note.alias,
471479
owner: owner,
472480
ownerprofile: ownerprofile,
473481
permission: note.permission,
@@ -652,6 +660,12 @@ function operationCallback(socket, operation) {
652660
return logger.error('operation callback failed: ' + err);
653661
});
654662
}
663+
// update user note history
664+
setTimeout(function() {
665+
var noteId = note.alias ? note.alias : LZString.compressToBase64(note.id);
666+
history.updateHistory(userId, noteId, note.server.document);
667+
}, 0);
668+
655669
}
656670
// save authorship
657671
note.authorship = models.Note.updateAuthorshipByOperation(operation, userId, note.authorship);

public/js/history.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ function removeHistory(id, notehistory) {
139139
function writeHistory(view) {
140140
checkIfAuth(
141141
function () {
142-
writeHistoryToServer(view);
142+
// no need to do this anymore, this will count from server-side
143+
// writeHistoryToServer(view);
143144
},
144145
function () {
145146
writeHistoryToStorage(view);
@@ -365,4 +366,29 @@ function parseToHistory(list, notehistory, callback) {
365366
}
366367
}
367368
callback(list, notehistory);
369+
}
370+
371+
function postHistoryToServer(noteId, data, callback) {
372+
$.post(serverurl + '/history/' + noteId, data)
373+
.done(function (result) {
374+
return callback(null, result);
375+
})
376+
.fail(function (xhr, status, error) {
377+
console.error(xhr.responseText);
378+
return callback(error, null);
379+
});
380+
}
381+
382+
function deleteServerHistory(noteId, callback) {
383+
$.ajax({
384+
url: serverurl + '/history' + (noteId ? '/' + noteId : ""),
385+
type: 'DELETE'
386+
})
387+
.done(function (result) {
388+
return callback(null, result);
389+
})
390+
.fail(function (xhr, status, error) {
391+
console.error(xhr.responseText);
392+
return callback(error, null);
393+
});
368394
}

0 commit comments

Comments
 (0)