<commit_before>/**
 * Created by weixing on 15/11/8.
 */

var conn = require("../orm/connection.js").mysqlConn;

var friendRoomPrefix = "pri_";

function getRoomname(uid,fuid){
    return friendRoomPrefix+[uid,fuid].sort(function(a,b){ return a > b ? 1 : 0;}).join('_');
};

function checkRoom(uid,fuid){
    conn.query("select id from gr_chat_private_rooms where ? and ?  ",[{fr:uid},{tid:fuid}],function(error,result){
        if(error) throw error;
        if(result[0]){
            return;
        }else{
            conn.query("insert into gr_chat_private_rooms set ? ",{fr:uid,tid:fuid}); //Ǻϵ
        }
    });
};

function getUserByName(username,cb){
    conn.query("select id,username from gr_users where username like "+conn.escape("%"+username+"%"),function(error,records){
        if(error) throw error;
        cb(records);
    })
}

function getUserChatRooms(uid,cb){
    conn.query("select r.fr,r.tid,r.unread,user.id,user.username from gr_chat_private_rooms as r join gr_users as user" +
        " on user.id = r.fr  where tid = ?",uid,function(error,records){
        if(error) throw  error;
        cb(records);
    });
}

function getFriendById(data,cb){
    conn.query("select id,username from gr_users where ? ",{id:data.uid},function(error,user){
        if(error) throw  error;
        cb(user[0]);
    });
}


function hasUnreadMessage(fr,tid){
    conn.query("update gr_chat_private_rooms set unread = 1 where ? and ? ",[{fr:fr},{tid:tid}]);
}

function readMessage(fr,tid){
    conn.query("update gr_chat_private_rooms set unread = 0 where ? and ? ",[{fr:fr},{tid:tid}]);
}

exports.checkRoom = checkRoom;
exports.getFriendRoom = getRoomname;
exports.getUserByName = getUserByName;
exports.userChatRooms = getUserChatRooms;
exports.hasUnreadMessage = hasUnreadMessage;//δϢ
exports.readMessage = readMessage; //δϢΪѶ
exports.getFriendById = getFriendById;<commit_msg>optimize<commit_after>/**
 * Created by weixing on 15/11/8.
 */

var conn = require("../orm/connection.js").mysqlConn;

var friendRoomPrefix = "pri_";

function getRoomname(uid,fuid){
    return friendRoomPrefix+[uid,fuid].sort(function(a,b){ return a > b ? 1 : 0;}).join('_');
};

function checkRoom(uid,fuid){
    conn.query("select id from gr_chat_private_rooms where ? and ?  ",[{fr:uid},{tid:fuid}],function(error,result){
        if(error) throw error;
        if(result[0]){
            return;
        }else{
            conn.query("insert into gr_chat_private_rooms set ? ",{fr:uid,tid:fuid}); //Ǻϵ
        }
    });
};

function getUserByName(username,cb){
    conn.query("select id,username from gr_users where username like "+conn.escape("%"+username+"%"),function(error,records){
        if(error) throw error;
        cb(records);
    })
}

function getUserChatRooms(uid,cb){
    conn.query("select r.fr,r.tid,r.unread,user.id,user.username from gr_chat_private_rooms as r join gr_users as user" +
        " on user.id = r.fr  where tid = ?",uid,function(error,records){
        if(error) throw  error;
        cb(records);
    });
}

function getFriendById(data,cb){
    conn.query("select id,username from gr_users where ? ",{id:data.uid},function(error,user){
        if(error) throw  error;
        cb(user[0]);
    });
}


function hasUnreadMessage(fr,tid){
    conn.query("update gr_chat_private_rooms set unread = 1 where ? and ? ",[{fr:fr},{tid:tid}]);
}

function readMessage(fr,tid){
    conn.query("update gr_chat_private_rooms set unread = 0 where ? and ? ",[{fr:fr},{tid:tid}]);
}

module.exports = {
     checkRoom          :  checkRoom
    ,getFriendRoom     : getRoomname
    ,getUserByName     : getUserByName
    ,userChatRooms     : getUserChatRooms
    ,hasUnreadMessage  :  hasUnreadMessage
    ,readMessage        : readMessage
    ,getFriendById     : getFriendById
}
{'fraction_non_alphanumeric': 0.15279241306638566, 'fraction_numerical': 0.005795574288724974, 'mean_word_length': 3.653186274509804, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3062748', 'n_tokens_mistral': 1297, 'n_tokens_neox': 1281, 'n_words': 249}