From 4a386d649ef205b6d64fb8bc872255d4f9deba08 Mon Sep 17 00:00:00 2001 From: Bryan Ashby Date: Thu, 22 Oct 2015 15:44:44 -0600 Subject: [PATCH] * WIP on message last read - not fully functional yet --- core/database.js | 9 +++++++++ core/fse.js | 21 ++++++++++++++------- core/message.js | 4 ++++ core/message_area.js | 35 +++++++++++++++++++++++++++++++++++ mods/msg_list.js | 10 ++++++++-- 5 files changed, 70 insertions(+), 9 deletions(-) diff --git a/core/database.js b/core/database.js index e0bde8e8..efac0436 100644 --- a/core/database.js +++ b/core/database.js @@ -148,6 +148,15 @@ function createMessageBaseTables() { ');' ); + dbs.message.run( + 'CREATE TABLE IF NOT EXISTS user_message_area_last_read (' + + ' user_id INTEGER NOT NULL,' + + ' area_name VARCHAR NOT NULL,' + + ' message_id INTEGER NOT NULL,' + + ' UNIQUE(user_id, area_name)' + + ');' + ); + dbs.message.run( 'CREATE TABLE IF NOT EXISTS user_message_status (' + ' user_id INTEGER NOT NULL,' + diff --git a/core/fse.js b/core/fse.js index b6e42118..9fca5f81 100644 --- a/core/fse.js +++ b/core/fse.js @@ -1,13 +1,14 @@ /* jslint node: true */ 'use strict'; -var MenuModule = require('../core/menu_module.js').MenuModule; -var ViewController = require('../core/view_controller.js').ViewController; -var ansi = require('../core/ansi_term.js'); -var theme = require('../core/theme.js'); -var MultiLineEditTextView = require('../core/multi_line_edit_text_view.js').MultiLineEditTextView; -var Message = require('../core/message.js'); -var getMessageAreaByName = require('../core/message_area.js').getMessageAreaByName; +var MenuModule = require('../core/menu_module.js').MenuModule; +var ViewController = require('../core/view_controller.js').ViewController; +var ansi = require('../core/ansi_term.js'); +var theme = require('../core/theme.js'); +var MultiLineEditTextView = require('../core/multi_line_edit_text_view.js').MultiLineEditTextView; +var Message = require('../core/message.js'); +var getMessageAreaByName = require('../core/message_area.js').getMessageAreaByName; +var updateMessageAreaLastReadId = require('../core/message_area.js').updateMessageAreaLastReadId; var async = require('async'); var assert = require('assert'); @@ -209,6 +210,10 @@ function FullScreenEditorModule(options) { this.setMessage = function(message) { self.message = message; + if(!self.message.isPrivate()) { + updateMessageAreaLastReadId(self.client.user.userId, self.messageAreaName, self.message.messageId); + } + if(self.isReady) { self.initHeaderViewMode(); self.initFooterViewMode(); @@ -864,6 +869,8 @@ require('util').inherits(FullScreenEditorModule, MenuModule); FullScreenEditorModule.prototype.enter = function(client) { FullScreenEditorModule.super_.prototype.enter.call(this, client); + + }; FullScreenEditorModule.prototype.mciReady = function(mciData, cb) { diff --git a/core/message.js b/core/message.js index ef2b6060..e81d6a84 100644 --- a/core/message.js +++ b/core/message.js @@ -54,6 +54,10 @@ function Message(options) { return true; }; + this.isPrivate = function() { + return this.areaName === Message.WellKnownAreaNames.Private ? true : false; + }; + this.getMessageTimestampString = function(ts) { ts = ts || new Date(); return ts.toISOString(); diff --git a/core/message_area.js b/core/message_area.js index 90f5f4f0..859f516b 100644 --- a/core/message_area.js +++ b/core/message_area.js @@ -15,6 +15,8 @@ exports.getMessageAreaByName = getMessageAreaByName; exports.changeMessageArea = changeMessageArea; exports.getMessageListForArea = getMessageListForArea; exports.gotoMsgAreaFSEModuleForMessage = gotoMsgAreaFSEModuleForMessage; +exports.getMessageAreaLastReadId = getMessageAreaLastReadId; +exports.updateMessageAreaLastReadId = updateMessageAreaLastReadId; function getAvailableMessageAreas(options) { // example: [ { "name" : "local_music", "desc" : "Music Discussion", "groups" : ["somegroup"] }, ... ] @@ -183,3 +185,36 @@ function gotoMsgAreaFSEModuleForMessage(options, cb) { } }); } + +function getMessageAreaLastReadId(userId, areaName, cb) { + msgDb.get( + 'SELECT message_id ' + + 'FROM user_message_area_last_read ' + + 'WHERE user_id = ? AND area_name = ?;', + [ userId, areaName ], + cb // (err, lastId) + ); +} + +function updateMessageAreaLastReadId(userId, areaName, messageId) { + // :TODO: likely a better way to do this... + async.waterfall( + [ + function getCurrent(callback) { + getMessageAreaLastReadId(userId, areaName, function result(err, lastId) { + lastId = lastId || 0; + callback(null, lastId); // ignore errors as we default to 0 + }); + }, + function update(lastId, callback) { + if(messageId > lastId) { + msgDb.run( + 'REPLACE INTO user_message_area_last_read (user_id, area_name, message_id) ' + + 'VALUES (?, ?, ?);', + [ userId, areaName, messageId ] + ); + } + } + ] + ); +} diff --git a/mods/msg_list.js b/mods/msg_list.js index 056db7a2..942b08d4 100644 --- a/mods/msg_list.js +++ b/mods/msg_list.js @@ -120,11 +120,17 @@ MessageListModule.prototype.mciReady = function(mciData, cb) { } }); }, + function getLastReadMesageId(callback) { + messageArea.getMessageAreaLastReadId(self.client.user.userId, self.messageAreaName, function lastRead(err, lastReadId) { + self.lastReadId = lastReadId || 0; + callback(null); // ignore any errors, e.g. missing value + }); + }, function populateList(callback) { var msgListView = vc.getView(MciCodesIds.MsgList); // :TODO: fix default format - var listFormat = self.menuConfig.config.listFormat || '{msgNum} - {subj} |{to}'; + var listFormat = self.menuConfig.config.listFormat || '{msgNum} - {subj} - {to}'; var focusListFormat = self.menuConfig.config.focusListFormat || listFormat; // :TODO: default change color here var dateTimeFormat = self.menuConfig.config.dateTimeFormat || 'ddd MMM DDD'; var newIndicator = self.menuConfig.config.newIndicator || '*'; @@ -138,7 +144,7 @@ MessageListModule.prototype.mciReady = function(mciData, cb) { from : mle.fromUserName, to : mle.toUserName, ts : moment(mle.modTimestamp).format(dateTimeFormat), - newIndicator : newIndicator, // :TODO: These should only be for actual new messages! + newIndicator : mle.messageId > self.lastReadId ? newIndicator : '', } }