docs/Chat.html
<!DOCTYPE html>
<html>
<head>
<title>Chat.js</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
<link rel="stylesheet" media="all" href="docco.css" />
</head>
<body>
<div id="container">
<div id="background"></div>
<ul id="jump_to">
<li>
<a class="large" href="javascript:void(0);">Jump To …</a>
<a class="small" href="javascript:void(0);">+</a>
<div id="jump_wrapper">
<div id="jump_page_wrapper">
<div id="jump_page">
<a class="source" href="Chat.html">
Chat.js
</a>
<a class="source" href="User.html">
User.js
</a>
<a class="source" href="Gruntfile.html">
Gruntfile.js
</a>
<a class="source" href="app.html">
app.js
</a>
</div>
</div>
</li>
</ul>
<ul class="sections">
<li id="title">
<div class="annotation">
<h1>Chat.js</h1>
</div>
</li>
<li id="section-1">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-1">¶</a>
</div>
</div>
<div class="content"><div class='highlight'><pre>exports.ChatController = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">db</span>)</span>{</pre></div></div>
</li>
<li id="section-2">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-2">¶</a>
</div>
<p>Redis Client</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">this</span>.dbredis = db;
<span class="hljs-keyword">this</span>.add = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">userid, touserid, encryptedMsgUser, encryptedMsgToUser, cb</span>)</span>{</pre></div></div>
</li>
<li id="section-3">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-3">¶</a>
</div>
<p>Adds message to DB</p>
</div>
<div class="content"><div class='highlight'><pre>
<span class="hljs-keyword">var</span> redis = <span class="hljs-keyword">this</span>.dbredis;</pre></div></div>
</li>
<li id="section-4">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-4">¶</a>
</div>
<p>Increment unread user chat messages</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">this</span>.dbredis.hgetall(<span class="hljs-string">"chats:"</span>+ touserid, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">err, result</span>)</span>{
<span class="hljs-keyword">var</span> unread = <span class="hljs-number">0</span>;
<span class="hljs-keyword">if</span>(result && result[touserid])
{
unread = result[touserid] + <span class="hljs-number">1</span>;
}
redis.hmset(<span class="hljs-string">"chats:"</span>+ touserid, userid, unread);
});</pre></div></div>
</li>
<li id="section-5">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-5">¶</a>
</div>
<p>Add messages to USERID messages in DB</p>
</div>
<div class="content"><div class='highlight'><pre> dbredis.hmset(userid +<span class="hljs-string">":"</span>+ touserid, encryptedMsgUser, <span class="hljs-number">1</span>);</pre></div></div>
</li>
<li id="section-6">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-6">¶</a>
</div>
<p>Add messages to TOUSERID messages in DB</p>
</div>
<div class="content"><div class='highlight'><pre> dbredis.hmset(touserid +<span class="hljs-string">":"</span>+ userid, encryptedMsgToUser, <span class="hljs-number">0</span>);
};
<span class="hljs-keyword">this</span>.getChatsForUser = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">uid, cb</span>)</span>{</pre></div></div>
</li>
<li id="section-7">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-7">¶</a>
</div>
<p>Get user chats by USERID</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">this</span>.dbredis.hgetall(<span class="hljs-string">"chats:"</span>+ uid, cb);
};
<span class="hljs-keyword">this</span>.getChatMessages = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">uid, withotheruid, cb</span>)</span>{</pre></div></div>
</li>
<li id="section-8">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-8">¶</a>
</div>
<p>Get user chat messages with other user</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">this</span>.dbredis.hgetall(uid +<span class="hljs-string">":"</span>+ withotheruid, cb);
};
};</pre></div></div>
</li>
<li id="section-9">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-9">¶</a>
</div>
<p>Test redis</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-comment">/*
dbredis.set("test:1", 0);
dbredis.get("test:1", function(err, result){
console.log(result);
});
*/</span></pre></div></div>
</li>
<li id="section-10">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-10">¶</a>
</div>
<p>Chat index example
It contains an object like this:
{“USERID1”:0, “USERID2”:2, “USERID3”:1}
the number counts how many unread messages from the USERID are</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-comment">/*
dbredis.hmset("chats:USERID", "USERID1", 0, "USERID2", 0, "USERID3", 0);
dbredis.hgetall("chats:USERID", function(err, result){
console.log(result);
console.log(err);
});
*/</span></pre></div></div>
</li>
<li id="section-11">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-11">¶</a>
</div>
<p>Users Chat Messages example
It contains an object with the messages encrypted with public key of user USERID
for the chat between USERID and OTHERUSERID.
When USERID sends a message to OTHERUSERID we must save the message in objects:
“USERID:OTHERUSERID” (encrypted with public key of USERID) -> this chat messages will be send to USERID when log in
“OTHERUSERID:USERID” (encrypted with public key of OTHERUSERID) -> this chat messages will be send to OTHERUSERID when log in
{“MESSAGE1”:0, “MESSAGE2”:1, “MESSAGE3”:0}
the number of each message:
1 -> Send by me
0 -> Send by OTHERUSERID</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-comment">/*
dbredis.hmset("USERID:OTHERUSERID", "MESSAGE1", 0, "MESSAGE2", 0, "MESSAGE3", 0);
dbredis.hgetall("USERID:OTHERUSERID", function(err, result){
console.log(result);
console.log(err);
});
*/</span></pre></div></div>
</li>
</ul>
</div>
</body>
</html>