Update to make note history count in server-side when user logged

This commit is contained in:
Wu Cheng-Han 2016-10-10 20:55:33 +08:00
parent 1d2a9826af
commit 36a1900ce3
3 changed files with 97 additions and 4 deletions

View file

@ -218,8 +218,7 @@ module.exports = function (sequelize, DataTypes) {
return callback(null, null); return callback(null, null);
}); });
}, },
parseNoteTitle: function (body) { parseNoteInfo: function (body) {
var title = "";
var meta = null; var meta = null;
try { try {
var obj = metaMarked(body); var obj = metaMarked(body);
@ -229,10 +228,30 @@ module.exports = function (sequelize, DataTypes) {
//na //na
} }
if (!meta) meta = {}; if (!meta) meta = {};
var $ = cheerio.load(md.render(body));
return {
title: Note.extractNoteTitle(meta, $),
tags: Note.extractNoteTags(meta, $)
};
},
parseNoteTitle: function (body) {
var meta = null;
try {
var obj = metaMarked(body);
body = obj.markdown;
meta = obj.meta;
} catch (err) {
//na
}
if (!meta) meta = {};
var $ = cheerio.load(md.render(body));
return Note.extractNoteTitle(meta, $);
},
extractNoteTitle: function (meta, $) {
var title = "";
if (meta.title && (typeof meta.title == "string" || typeof meta.title == "number")) { if (meta.title && (typeof meta.title == "string" || typeof meta.title == "number")) {
title = meta.title; title = meta.title;
} else { } else {
var $ = cheerio.load(md.render(body));
var h1s = $("h1"); var h1s = $("h1");
if (h1s.length > 0 && h1s.first().text().split('\n').length == 1) if (h1s.length > 0 && h1s.first().text().split('\n').length == 1)
title = S(h1s.first().text()).stripTags().s; title = S(h1s.first().text()).stripTags().s;
@ -250,6 +269,40 @@ module.exports = function (sequelize, DataTypes) {
title = !title || title == "Untitled" ? "HackMD - Collaborative markdown notes" : title + " - HackMD"; title = !title || title == "Untitled" ? "HackMD - Collaborative markdown notes" : title + " - HackMD";
return title; return title;
}, },
extractNoteTags: function (meta, $) {
var tags = [];
var rawtags = [];
if (meta.tags && (typeof meta.tags == "string" || typeof meta.tags == "number")) {
var metaTags = ('' + meta.tags).split(',');
for (var i = 0; i < metaTags.length; i++) {
var text = metaTags[i].trim();
if (text) rawtags.push(text);
}
} else {
var h6s = $("h6");
h6s.each(function (key, value) {
if (/^tags/gmi.test($(value).text())) {
var codes = $(value).find("code");
for (var i = 0; i < codes.length; i++) {
var text = $(codes[i]).html().trim();
if (text) rawtags.push(text);
}
}
});
}
for (var i = 0; i < rawtags.length; i++) {
var found = false;
for (var j = 0; j < tags.length; j++) {
if (tags[j] == rawtags[i]) {
found = true;
break;
}
}
if (!found)
tags.push(rawtags[i]);
}
return tags;
},
parseMeta: function (meta) { parseMeta: function (meta) {
var _meta = {}; var _meta = {};
if (meta) { if (meta) {

View file

@ -13,6 +13,7 @@ var moment = require('moment');
//core //core
var config = require("./config.js"); var config = require("./config.js");
var logger = require("./logger.js"); var logger = require("./logger.js");
var history = require("./history.js");
var models = require("./models"); var models = require("./models");
//ot //ot
@ -390,6 +391,12 @@ function finishConnection(socket, note, user) {
note.server.setName(socket, user.name); note.server.setName(socket, user.name);
note.server.setColor(socket, user.color); note.server.setColor(socket, user.color);
// update user note history
setTimeout(function () {
var noteId = note.alias ? note.alias : LZString.compressToBase64(note.id);
history.updateHistory(user.userid, noteId, note.server.document);
}, 0);
emitOnlineUsers(socket); emitOnlineUsers(socket);
emitRefresh(socket); emitRefresh(socket);
@ -468,6 +475,7 @@ function startConnection(socket) {
notes[noteId] = { notes[noteId] = {
id: noteId, id: noteId,
alias: note.alias,
owner: owner, owner: owner,
ownerprofile: ownerprofile, ownerprofile: ownerprofile,
permission: note.permission, permission: note.permission,
@ -652,6 +660,12 @@ function operationCallback(socket, operation) {
return logger.error('operation callback failed: ' + err); return logger.error('operation callback failed: ' + err);
}); });
} }
// update user note history
setTimeout(function() {
var noteId = note.alias ? note.alias : LZString.compressToBase64(note.id);
history.updateHistory(userId, noteId, note.server.document);
}, 0);
} }
// save authorship // save authorship
note.authorship = models.Note.updateAuthorshipByOperation(operation, userId, note.authorship); note.authorship = models.Note.updateAuthorshipByOperation(operation, userId, note.authorship);

View file

@ -139,7 +139,8 @@ function removeHistory(id, notehistory) {
function writeHistory(view) { function writeHistory(view) {
checkIfAuth( checkIfAuth(
function () { function () {
writeHistoryToServer(view); // no need to do this anymore, this will count from server-side
// writeHistoryToServer(view);
}, },
function () { function () {
writeHistoryToStorage(view); writeHistoryToStorage(view);
@ -365,4 +366,29 @@ function parseToHistory(list, notehistory, callback) {
} }
} }
callback(list, notehistory); callback(list, notehistory);
}
function postHistoryToServer(noteId, data, callback) {
$.post(serverurl + '/history/' + noteId, data)
.done(function (result) {
return callback(null, result);
})
.fail(function (xhr, status, error) {
console.error(xhr.responseText);
return callback(error, null);
});
}
function deleteServerHistory(noteId, callback) {
$.ajax({
url: serverurl + '/history' + (noteId ? '/' + noteId : ""),
type: 'DELETE'
})
.done(function (result) {
return callback(null, result);
})
.fail(function (xhr, status, error) {
console.error(xhr.responseText);
return callback(error, null);
});
} }