Created
June 30, 2015 18:15
-
-
Save korrio/dd79269933e699c9d54d to your computer and use it in GitHub Desktop.
Android Conversation History
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
router.get("/chat/:conversationId/history/android", function(req, res, next) { | |
// params | |
var conversationId = req.params.conversationId; | |
var pageParams = ModelMethod.getPageParams(req.query); | |
// sequel | |
var base = squel.select() | |
.field("ch.id") | |
.field("ch.conversation_id", "conversationId") | |
.field("ch.sender_id", "senderId") | |
.field("ch.message") | |
.field("ch.message_type", "messageType") | |
.field("ch.data") | |
.field("ch.read_count", "readCount") | |
.field("ch.ip_address", "ipAddress") | |
.field("ch.created_at", "createdAt") | |
.field("a.id", "sender.senderId") | |
.field("a.id", "sender.id") | |
.field("a.username", "sender.username") | |
.field("m.url", "sender.avatar") | |
.field("m.extension", "sender.extension") | |
.from("conversation_history", "ch") | |
.join("vmmax.accounts", "a", "a.id = ch.sender_id") | |
.join("vmmax.media", "m" ,"m.id = a.avatar_id") | |
.where("ch.conversation_id = ?") | |
.limit(pageParams.size) | |
.offset(pageParams.offset) | |
.order("ch.created_at", false) | |
.order("ch.id", false) | |
; | |
var countBase = squel.select() | |
.field("count(*) as count") | |
.from("conversation_history", "ch") | |
.join("vmmax.accounts", "a", "a.id = ch.sender_id") | |
.join("vmmax.media", "m" ,"m.id = a.avatar_id") | |
.where("ch.conversation_id = ?") | |
; | |
var dataQuery = Model.chatDB.query(base.toString(), | |
{ | |
replacements: [conversationId], | |
type: Model.chatDB.QueryTypes.SELECT | |
} | |
); | |
var countQuery = Model.chatDB.query(countBase.toString(), | |
{ | |
replacements: [conversationId], | |
type: Model.chatDB.QueryTypes.SELECT | |
} | |
); | |
Q.all([ | |
countQuery, | |
dataQuery | |
]) | |
.then(function(results){ | |
var count = lodash.result(lodash.first(results[0]), "count"); | |
var rows = results[1]; | |
var formatRows = formattedResult(rows, "id", "senderId", true); | |
//lodash.forEach(formatRows, function(n){ | |
//n.data = JSON.parse(n.data); | |
//}); | |
var page = ModelMethod.getPageObject(pageParams, formatRows, count); | |
res.json(ModelMethod.paginateJSON(formatRows, page)); | |
}) | |
.catch(function(error){ | |
next(error); | |
}) | |
; | |
// Q.all([ | |
// countQuery, dataQuery | |
// ]) | |
// .then(function(results){ | |
// var count = lodash.result(lodash.first(results[0]), "count"); | |
// var rows = results[1]; | |
// var page = ModelMethod.getPageObject(pageParams, rows, count); | |
// res.json(ModelMethod.paginateJSON(rows, page)); | |
// }) | |
// .catch(function(error){ | |
// next(error); | |
// }) | |
// ; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment