1
0
Fork 0
mirror of https://github.com/openstf/stf synced 2025-10-05 10:39:25 +02:00

Make GroupService keep the list of members up to date.

This commit is contained in:
Simo Kinnunen 2014-02-07 11:55:41 +09:00
parent 6ae7c310e0
commit 277c9b401a
4 changed files with 58 additions and 10 deletions

View file

@ -30,6 +30,12 @@ dbapi.loadUser = function(email) {
return db.run(r.table('users').get(email))
}
dbapi.loadGroupMembers = function(email) {
return db.run(r.table('devices').getAll(email, {
index: 'ownerEmail'
})('serial'))
}
dbapi.saveDeviceLog = function(serial, entry) {
return db.run(r.table('logs').insert({
serial: entry.serial

View file

@ -4,6 +4,11 @@ module.exports = {
}
, devices: {
primaryKey: 'serial'
, indexes: {
ownerEmail: function(device) {
return device('owner')('email')
}
}
}
, logs: {
primaryKey: 'id'

View file

@ -101,6 +101,27 @@ module.exports = function(options) {
})
})
app.get('/api/v1/group', function(req, res) {
dbapi.loadGroupMembers(req.user.email)
.then(function(cursor) {
return Promise.promisify(cursor.toArray, cursor)()
.then(function(list) {
res.json({
success: true
, group: {
members: list
}
})
})
})
.catch(function(err) {
log.error('Failed to load group: ', err.stack)
res.json(500, {
success: false
})
})
})
app.get('/api/v1/devices', function(req, res) {
dbapi.loadDevices()
.then(function(cursor) {

View file

@ -1,9 +1,18 @@
define(['./_module', 'lodash'], function(app, _) {
function GroupServiceFactory($rootScope, socket, userService) {
function GroupServiceFactory($rootScope, $http, socket, userService) {
var groupService = {
members: []
}
groupService.group = (function() {
var groupPromise = $http.get('/api/v1/group')
.then(function(response) {
return response.data.group
})
return function() {
return groupPromise
}
})()
userService.user().then(function(user) {
function ownerFilter(listener) {
return function(data) {
@ -14,20 +23,26 @@ define(['./_module', 'lodash'], function(app, _) {
}
socket.on('group.join', ownerFilter(function(data) {
groupService.members.push(data.serial)
console.log('group.join', data)
$rootScope.$digest()
groupService.group().then(function(group) {
group.members.push(data.serial)
console.log('group.join', data)
$rootScope.$digest()
})
}))
socket.on('group.leave', ownerFilter(function(data) {
_.pull(groupService.members, data.serial)
console.log('group.leave', data)
$rootScope.$digest()
groupService.group().then(function(group) {
_.pull(group.members, data.serial)
console.log('group.leave', data)
$rootScope.$digest()
})
}))
socket.on('device.absent', /* unfiltered */ function(data) {
_.pull(groupService.members, data.serial)
$rootScope.$digest()
groupService.group().then(function(group) {
_.pull(group.members, data.serial)
$rootScope.$digest()
})
})
})
@ -48,6 +63,7 @@ define(['./_module', 'lodash'], function(app, _) {
app.factory('GroupService'
, [ '$rootScope'
, '$http'
, 'SocketService'
, 'UserService'
, GroupServiceFactory