Use strings instead of opaque magic-number constants for presence states; rename AWAY to UNAVAILABLE

This commit is contained in:
Paul "LeoNerd" Evans 2014-08-13 14:31:48 +01:00
parent 2a0f7541c7
commit 55944ccf72
7 changed files with 34 additions and 37 deletions

View File

@ -37,7 +37,7 @@ class Feedback(object):
class PresenceState(object): class PresenceState(object):
"""Represents the presence state of a user.""" """Represents the presence state of a user."""
OFFLINE = 0 OFFLINE = u"offline"
BUSY = 1 UNAVAILABLE = u"unavailable"
ONLINE = 2 ONLINE = u"online"
FREE_FOR_CHAT = 3 FREE_FOR_CHAT = u"free_for_chat"

View File

@ -27,7 +27,7 @@ from synapse.handlers.presence import PresenceHandler, UserPresenceCache
OFFLINE = PresenceState.OFFLINE OFFLINE = PresenceState.OFFLINE
BUSY = PresenceState.BUSY UNAVAILABLE = PresenceState.UNAVAILABLE
ONLINE = PresenceState.ONLINE ONLINE = PresenceState.ONLINE
@ -149,12 +149,12 @@ class PresenceStateTestCase(unittest.TestCase):
yield self.handler.set_state( yield self.handler.set_state(
target_user=self.u_apple, auth_user=self.u_apple, target_user=self.u_apple, auth_user=self.u_apple,
state={"state": BUSY, "status_msg": "Away"}) state={"state": UNAVAILABLE, "status_msg": "Away"})
mocked_set.assert_called_with("apple", mocked_set.assert_called_with("apple",
{"state": 1, "status_msg": "Away"}) {"state": UNAVAILABLE, "status_msg": "Away"})
self.mock_start.assert_called_with(self.u_apple, self.mock_start.assert_called_with(self.u_apple,
state={"state": 1, "status_msg": "Away"}) state={"state": UNAVAILABLE, "status_msg": "Away"})
yield self.handler.set_state( yield self.handler.set_state(
target_user=self.u_apple, auth_user=self.u_apple, target_user=self.u_apple, auth_user=self.u_apple,
@ -555,7 +555,7 @@ class PresencePushTestCase(unittest.TestCase):
content={ content={
"push": [ "push": [
{"user_id": "@apple:test", {"user_id": "@apple:test",
"state": 2}, "state": "online"},
], ],
}), }),
call( call(
@ -564,7 +564,7 @@ class PresencePushTestCase(unittest.TestCase):
content={ content={
"push": [ "push": [
{"user_id": "@apple:test", {"user_id": "@apple:test",
"state": 2}, "state": "online"},
], ],
}) })
], any_order=True) ], any_order=True)
@ -582,7 +582,7 @@ class PresencePushTestCase(unittest.TestCase):
"remote", "m.presence", { "remote", "m.presence", {
"push": [ "push": [
{"user_id": "@potato:remote", {"user_id": "@potato:remote",
"state": 2}, "state": "online"},
], ],
} }
) )
@ -646,7 +646,7 @@ class PresencePushTestCase(unittest.TestCase):
content={ content={
"push": [ "push": [
{"user_id": "@apple:test", {"user_id": "@apple:test",
"state": 2}, "state": "online"},
], ],
}), }),
call( call(
@ -655,7 +655,7 @@ class PresencePushTestCase(unittest.TestCase):
content={ content={
"push": [ "push": [
{"user_id": "@banana:test", {"user_id": "@banana:test",
"state": 0}, "state": "offline"},
], ],
}), }),
], any_order=True) ], any_order=True)
@ -666,7 +666,7 @@ class PresencePushTestCase(unittest.TestCase):
self.handler._user_cachemap[self.u_clementine] = UserPresenceCache() self.handler._user_cachemap[self.u_clementine] = UserPresenceCache()
self.handler._user_cachemap[self.u_clementine].update( self.handler._user_cachemap[self.u_clementine].update(
{"state": PresenceState.ONLINE}, self.u_clementine) {"state": ONLINE}, self.u_clementine)
self.room_members.append(self.u_potato) self.room_members.append(self.u_potato)
yield self.distributor.fire("user_joined_room", self.u_clementine, yield self.distributor.fire("user_joined_room", self.u_clementine,
@ -680,7 +680,7 @@ class PresencePushTestCase(unittest.TestCase):
content={ content={
"push": [ "push": [
{"user_id": "@clementine:test", {"user_id": "@clementine:test",
"state": 2}, "state": "online"},
], ],
}), }),
) )
@ -882,7 +882,7 @@ class PresencePollingTestCase(unittest.TestCase):
content={ content={
"push": [ "push": [
{"user_id": "@banana:test", {"user_id": "@banana:test",
"state": 0, "state": "offline",
"status_msg": None}, "status_msg": None},
], ],
}, },

View File

@ -29,7 +29,7 @@ from synapse.handlers.profile import ProfileHandler
OFFLINE = PresenceState.OFFLINE OFFLINE = PresenceState.OFFLINE
BUSY = PresenceState.BUSY UNAVAILABLE = PresenceState.UNAVAILABLE
ONLINE = PresenceState.ONLINE ONLINE = PresenceState.ONLINE
@ -125,12 +125,12 @@ class PresenceProfilelikeDataTestCase(unittest.TestCase):
yield self.handlers.presence_handler.set_state( yield self.handlers.presence_handler.set_state(
target_user=self.u_apple, auth_user=self.u_apple, target_user=self.u_apple, auth_user=self.u_apple,
state={"state": BUSY, "status_msg": "Away"}) state={"state": UNAVAILABLE, "status_msg": "Away"})
mocked_set.assert_called_with("apple", mocked_set.assert_called_with("apple",
{"state": 1, "status_msg": "Away"}) {"state": UNAVAILABLE, "status_msg": "Away"})
self.mock_start.assert_called_with(self.u_apple, self.mock_start.assert_called_with(self.u_apple,
state={"state": 1, "status_msg": "Away", state={"state": UNAVAILABLE, "status_msg": "Away",
"displayname": "Frank", "displayname": "Frank",
"avatar_url": "http://foo"}) "avatar_url": "http://foo"})
@ -220,7 +220,7 @@ class PresenceProfilelikeDataTestCase(unittest.TestCase):
content={ content={
"push": [ "push": [
{"user_id": "@apple:test", {"user_id": "@apple:test",
"state": 2, "state": "online",
"displayname": "Frank", "displayname": "Frank",
"avatar_url": "http://foo"}, "avatar_url": "http://foo"},
], ],
@ -238,7 +238,7 @@ class PresenceProfilelikeDataTestCase(unittest.TestCase):
"remote", "m.presence", { "remote", "m.presence", {
"push": [ "push": [
{"user_id": "@potato:remote", {"user_id": "@potato:remote",
"state": 2, "state": "online",
"displayname": "Frank", "displayname": "Frank",
"avatar_url": "http://foo"}, "avatar_url": "http://foo"},
], ],

View File

@ -31,7 +31,7 @@ logging.getLogger().addHandler(logging.NullHandler())
OFFLINE = PresenceState.OFFLINE OFFLINE = PresenceState.OFFLINE
BUSY = PresenceState.BUSY UNAVAILABLE = PresenceState.UNAVAILABLE
ONLINE = PresenceState.ONLINE ONLINE = PresenceState.ONLINE
@ -69,7 +69,7 @@ class PresenceStateTestCase(unittest.TestCase):
def test_get_my_status(self): def test_get_my_status(self):
mocked_get = self.mock_handler.get_state mocked_get = self.mock_handler.get_state
mocked_get.return_value = defer.succeed( mocked_get.return_value = defer.succeed(
{"state": 2, "status_msg": "Available"}) {"state": ONLINE, "status_msg": "Available"})
(code, response) = yield self.mock_server.trigger("GET", (code, response) = yield self.mock_server.trigger("GET",
"/presence/%s/status" % (myid), None) "/presence/%s/status" % (myid), None)
@ -87,12 +87,12 @@ class PresenceStateTestCase(unittest.TestCase):
(code, response) = yield self.mock_server.trigger("PUT", (code, response) = yield self.mock_server.trigger("PUT",
"/presence/%s/status" % (myid), "/presence/%s/status" % (myid),
'{"state": 1, "status_msg": "Away"}') '{"state": "unavailable", "status_msg": "Away"}')
self.assertEquals(200, code) self.assertEquals(200, code)
mocked_set.assert_called_with(target_user=self.u_apple, mocked_set.assert_called_with(target_user=self.u_apple,
auth_user=self.u_apple, auth_user=self.u_apple,
state={"state": 1, "status_msg": "Away"}) state={"state": UNAVAILABLE, "status_msg": "Away"})
class PresenceListTestCase(unittest.TestCase): class PresenceListTestCase(unittest.TestCase):
@ -234,7 +234,7 @@ class PresenceEventStreamTestCase(unittest.TestCase):
# I'll already get my own presence state change # I'll already get my own presence state change
self.assertEquals({"start": "0", "end": "1", "chunk": [ self.assertEquals({"start": "0", "end": "1", "chunk": [
{"type": "m.presence", {"type": "m.presence",
"content": {"user_id": "@apple:test", "state": 2}}, "content": {"user_id": "@apple:test", "state": ONLINE}},
]}, response) ]}, response)
self.mock_datastore.set_presence_state.return_value = defer.succeed( self.mock_datastore.set_presence_state.return_value = defer.succeed(
@ -251,5 +251,5 @@ class PresenceEventStreamTestCase(unittest.TestCase):
self.assertEquals(200, code) self.assertEquals(200, code)
self.assertEquals({"start": "1", "end": "2", "chunk": [ self.assertEquals({"start": "1", "end": "2", "chunk": [
{"type": "m.presence", {"type": "m.presence",
"content": {"user_id": "@banana:test", "state": 2}}, "content": {"user_id": "@banana:test", "state": ONLINE}},
]}, response) ]}, response)

View File

@ -106,7 +106,7 @@ h1 {
background-color: #38AF00; background-color: #38AF00;
} }
.away { .unavailable {
background-color: #FFCC00; background-color: #FFCC00;
} }

View File

@ -123,17 +123,14 @@ angular.module('RoomController', [])
var member = $scope.members[chunk.content.user_id]; var member = $scope.members[chunk.content.user_id];
if ("state" in chunk.content) { if ("state" in chunk.content) {
var ONLINE = 2; if (chunk.content.state === "online") {
var AWAY = 1;
var OFFLINE = 0;
if (chunk.content.state === ONLINE) {
member.presenceState = "online"; member.presenceState = "online";
} }
else if (chunk.content.state === OFFLINE) { else if (chunk.content.state === "offline") {
member.presenceState = "offline"; member.presenceState = "offline";
} }
else if (chunk.content.state === AWAY) { else if (chunk.content.state === "unavailable") {
member.presenceState = "away"; member.presenceState = "unavailable";
} }
} }

View File

@ -14,7 +14,7 @@
<img class="userAvatarGradient" src="img/gradient.png" width="80" height="24"/> <img class="userAvatarGradient" src="img/gradient.png" width="80" height="24"/>
<div class="userName">{{ info.displayname || name }}</div> <div class="userName">{{ info.displayname || name }}</div>
</td> </td>
<td class="userPresence" ng-class="info.presenceState === 'online' ? 'online' : (info.presenceState === 'away' ? 'away' : '')" /> <td class="userPresence" ng-class="info.presenceState === 'online' ? 'online' : (info.presenceState === 'unavailable' ? 'unavailable' : '')" />
</table> </table>
</div> </div>