mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-19 15:48:04 +01:00
rewrite: add basic chat functionality, `POST
/api/v1/groups/:gid/chat` (which returns chat list). Some bugs present
This commit is contained in:
@@ -121,39 +121,6 @@ module.exports.app = (appExports, model, app) ->
|
||||
model.on 'unshift', '_party.chat', -> $('.chat-message').tooltip()
|
||||
model.on 'unshift', '_habitrpg.chat', -> $('.chat-message').tooltip()
|
||||
|
||||
appExports.sendChat = (e,el) ->
|
||||
text = model.get '_chatMessage'
|
||||
# Check for non-whitespace characters
|
||||
return unless /\S/.test text
|
||||
|
||||
group = e.at()
|
||||
|
||||
# get rid of duplicate member ids - this is a weird place to put it, but works for now
|
||||
members = group.get('members'); uniqMembers = _.uniq(members)
|
||||
group.set('members', uniqMembers) if !_.isEqual(uniqMembers, members)
|
||||
|
||||
chat = group.at('chat')
|
||||
model.set('_chatMessage', '')
|
||||
|
||||
message =
|
||||
id: model.id()
|
||||
uuid: user.get('id')
|
||||
contributor: user.get('backer.contributor')
|
||||
npc: user.get('backer.npc')
|
||||
text: text
|
||||
user: helpers.username(model.get('_user.auth'), model.get('_user.profile.name'))
|
||||
timestamp: +new Date
|
||||
|
||||
# FIXME - sometimes racer will send many duplicates via chat.unshift. I think because it can't make connection, keeps
|
||||
# trying, but all attempts go through. Unfortunately we can't do chat.set without potentially clobbering other chatters
|
||||
messages = chat.get() or []
|
||||
messages.unshift(message)
|
||||
messages.splice(200)
|
||||
chat.set messages
|
||||
|
||||
type = $(el).attr('data-type')
|
||||
model.set '_user.party.lastMessageSeen', chat.get()[0].id if group.get('type') is 'party'
|
||||
|
||||
appExports.chatKeyup = (e, el, next) ->
|
||||
return next() unless e.keyCode is 13
|
||||
appExports.sendChat(e, el)
|
||||
|
||||
@@ -2,11 +2,22 @@
|
||||
|
||||
habitrpg
|
||||
|
||||
.controller("GroupsCtrl", ['$scope', '$rootScope', 'Groups', '$http', '$location',
|
||||
function($scope, $rootScope, Groups) {
|
||||
.controller("GroupsCtrl", ['$scope', '$rootScope', 'Groups', '$http', '$location', '$http', 'API_URL',
|
||||
function($scope, $rootScope, Groups, $http, API_URL) {
|
||||
$scope._chatMessage = '';
|
||||
$scope.groups = Groups.query(function(groups){
|
||||
$scope.members = groups.members;
|
||||
});
|
||||
$scope.postChat = function(group, message){
|
||||
if (_.isEmpty(message)) return
|
||||
$('.chat-btn').addClass('disabled');
|
||||
$http.post('/api/v1/groups/'+group._id+'/chat', {message:message})
|
||||
.success(function(data){
|
||||
$scope._chatMessage = '';
|
||||
group.chat = data;
|
||||
$('.chat-btn').removeClass('disabled');
|
||||
});
|
||||
}
|
||||
$scope.party = true;
|
||||
}
|
||||
])
|
||||
|
||||
@@ -9,7 +9,11 @@ angular.module('groupServices', ['ngResource']).
|
||||
function(API_URL, $resource, User) {
|
||||
var Group = $resource(API_URL + '/api/v1/groups/:gid',
|
||||
{gid:'@_id'},
|
||||
{'query': {method: "GET", isArray:false}});
|
||||
{
|
||||
'query': {method: "GET", isArray:false}
|
||||
//postChat: {method: "POST"}
|
||||
});
|
||||
|
||||
return Group;
|
||||
}
|
||||
]);
|
||||
|
||||
@@ -12,7 +12,7 @@ var api = module.exports;
|
||||
|
||||
/*
|
||||
------------------------------------------------------------------------
|
||||
Party
|
||||
Groups
|
||||
------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
@@ -40,13 +40,7 @@ api.getGroups = function(req, res, next) {
|
||||
Group.findOne({_id: 'habitrpg'}, cb);
|
||||
},
|
||||
"public": function(cb) {
|
||||
Group.find({
|
||||
privacy: 'public'
|
||||
}, {
|
||||
name: 1,
|
||||
description: 1,
|
||||
members: 1
|
||||
}, cb);
|
||||
Group.find({privacy: 'public'}, {name:1, description:1, members:1}, cb);
|
||||
}
|
||||
}, function(err, results){
|
||||
if (err) return res.json(500, {err: err});
|
||||
@@ -55,6 +49,46 @@ api.getGroups = function(req, res, next) {
|
||||
var i = _.findIndex(results.party.members, {_id:user._id});
|
||||
if (~i) results.party.members.splice(i,1);
|
||||
|
||||
// Sort public groups by members length (not easily doable in mongoose)
|
||||
results.public = _.sortBy(results.public, function(group){
|
||||
return -group.members.length;
|
||||
})
|
||||
|
||||
res.json(results);
|
||||
})
|
||||
};
|
||||
|
||||
api.attachGroup = function(req, res, next) {
|
||||
Group.findById(req.params.gid, function(err, group){
|
||||
if(err) return res.json(500, {err:err});
|
||||
res.locals.group = group;
|
||||
next();
|
||||
})
|
||||
}
|
||||
|
||||
api.postChat = function(req, res, next) {
|
||||
var user = res.locals.user
|
||||
var group = res.locals.group;
|
||||
var message = {
|
||||
id: helpers.uuid(),
|
||||
uuid: user._id,
|
||||
contributor: user.backer && user.backer.contributor,
|
||||
npc: user.backer && user.backer.npc,
|
||||
text: req.body.message,
|
||||
user: helpers.username(user.auth, user.profile.name),
|
||||
timestamp: +(new Date)
|
||||
};
|
||||
|
||||
group.chat.unshift(message);
|
||||
group.chat.splice(200);
|
||||
|
||||
if (group.type === 'party') {
|
||||
user.party.lastMessageSeen = message.id;
|
||||
user.save();
|
||||
}
|
||||
|
||||
group.save(function(err, group){
|
||||
if (err) return res.json(500, {err:err});
|
||||
res.json(group.chat);
|
||||
})
|
||||
}
|
||||
@@ -62,7 +62,7 @@ router.get('/groups', auth, groups.getGroups);
|
||||
//DELETE /groups/:gid
|
||||
|
||||
//GET /groups/:gid/chat
|
||||
//POST /groups/:gid/chat
|
||||
router.post('/groups/:gid/chat', auth, groups.attachGroup, groups.postChat);
|
||||
//PUT /groups/:gid/chat/:messageId
|
||||
//DELETE /groups/:gid/chat/:messageId
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
form(x-bind='submit:sendChat')
|
||||
textarea.span6(rows='3', x-bind='keyup:chatKeyup') {{_chatMessage}}
|
||||
form(ng-submit='postChat(group,_chatMessage)')
|
||||
textarea.span6(rows='3', x-bind='keyup:chatKeyup', ng-model='_chatMessage')
|
||||
br
|
||||
input.btn(type='submit', value='Send Chat')
|
||||
input.btn.chat-btn(type='submit', value='Send Chat')
|
||||
|
||||
@@ -49,7 +49,7 @@ div(ng-controller='GroupsCtrl')
|
||||
tr(ng-repeat='group in groups.public')
|
||||
td
|
||||
ul.pull-right.challenge-accordion-header-specs
|
||||
li {{count(group.members)}} member(s)
|
||||
li {{group.members.length}} member(s)
|
||||
li
|
||||
// join / leave
|
||||
a.btn.btn-small.btn-danger(ng-show='indexOf(group.members,user.id)', x-bind='click:groupLeave', data-id='{{group.id}}')
|
||||
|
||||
Reference in New Issue
Block a user