Skip to content

Commit

Permalink
feat: revisit user tracking (#56)
Browse files Browse the repository at this point in the history
* feat: revisit user tracking

* feat: use track_user and untrack_user instead of modifying the flags
  • Loading branch information
JurgenR authored Sep 25, 2023
1 parent b6a1697 commit c7a5930
Show file tree
Hide file tree
Showing 10 changed files with 461 additions and 114 deletions.
6 changes: 3 additions & 3 deletions src/aioslsk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
SharesCache,
)
from .shares.manager import SharesManager
from .model import Room, User
from .model import Room, User, TrackingFlag
from .network.network import Network
from .peer import PeerManager
from .server import ServerManager
Expand Down Expand Up @@ -215,11 +215,11 @@ async def queue_transfer(self, transfer: Transfer):

async def track_user(self, user: Union[str, User]):
username = user.name if isinstance(user, User) else user
await self.server_manager.track_user(username)
await self.server_manager.track_user(username, TrackingFlag.REQUESTED)

async def untrack_user(self, user: Union[str, User]):
username = user.name if isinstance(user, User) else user
await self.server_manager.untrack_user(username)
await self.server_manager.untrack_user(username, TrackingFlag.REQUESTED)

async def get_room_list(self):
await self.server_manager.get_room_list()
Expand Down
26 changes: 13 additions & 13 deletions src/aioslsk/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import logging
from typing import Callable, Dict, List, Type, TYPE_CHECKING

from .model import ChatMessage, Room, RoomMessage, User
from .model import ChatMessage, Room, RoomMessage, User, TrackingFlag
from .protocol.primitives import (
DirectoryData,
MessageDataclass,
Expand Down Expand Up @@ -89,24 +89,25 @@ class Event:

@dataclass(frozen=True)
class ServerConnectedEvent(Event):
pass
"""Emitted when server got connected"""


@dataclass(frozen=True)
class ServerDisconnectedEvent(Event):
pass
"""Emitted when server got disconnected"""


@dataclass(frozen=True)
class LoginEvent:
"""Emitted when we got a response to a login call"""
is_success: bool
greeting: str = None
reason: str = None


@dataclass(frozen=True)
class KickedEvent(Event):
pass
"""Emitted when we are kicked from the server"""


@dataclass(frozen=True)
Expand Down Expand Up @@ -150,23 +151,27 @@ class RoomTickerRemovedEvent:

@dataclass(frozen=True)
class UserJoinedRoomEvent(Event):
"""Emitted when a user joins a chat room"""
room: Room
user: User


@dataclass(frozen=True)
class UserLeftRoomEvent(Event):
"""Emitted when a user leaves a chat room"""
room: Room
user: User


@dataclass(frozen=True)
class RoomJoinedEvent(Event):
"""Emitted after we have joined a chat room"""
room: Room


@dataclass(frozen=True)
class RoomLeftEvent(Event):
"""Emitted after we have left a chat room"""
room: Room


Expand All @@ -182,22 +187,15 @@ class RemovedFromPrivateRoomEvent(Event):
room: Room


@dataclass
class ServerMessageEvent(Event):
"""Emitted when the server sends us a message. This is usually a response
to an action you tried to perform (adding someone to a private room...)
"""
message: str


@dataclass(frozen=True)
class PrivateMessageEvent(Event):
user: User
"""Emitted when a private message has been received"""
message: ChatMessage


@dataclass(frozen=True)
class SearchResultEvent(Event):
"""Emitted when a search result has been received"""
query: SearchRequest
result: SearchResult

Expand Down Expand Up @@ -293,11 +291,13 @@ class PeerInitializedEvent(InternalEvent):
@dataclass(frozen=True)
class TrackUserEvent(InternalEvent):
username: str
flag: TrackingFlag


@dataclass(frozen=True)
class UntrackUserEvent(InternalEvent):
username: str
flag: TrackingFlag


@dataclass(frozen=True)
Expand Down
31 changes: 28 additions & 3 deletions src/aioslsk/model.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
from __future__ import annotations
from dataclasses import dataclass, field
import enum
from enum import auto, Enum, Flag
from typing import Dict, List
from .protocol.primitives import UserStats


class UserStatus(enum.Enum):
class UserStatus(Enum):
"""User status values, everything except the UNKNOWN status are used by the
server
"""
UNKNOWN = -1
OFFLINE = 0
AWAY = 1
ONLINE = 2


class TrackingFlag(Flag):
"""Tracking flags hold information how the user is being tracked"""
REQUESTED = auto()
"""Tracking was explicitly requested by the user"""
TRANSFER = auto()
"""Tracking was requested through transfer manager"""
FRIEND = auto()
"""Tracking because the user is a friend"""
ROOM_USER = auto()
"""Tracking because the user is in a room we are in"""


@dataclass
class User:
name: str
Expand All @@ -31,7 +46,14 @@ class User:
upload_slots: int = None
queue_length: int = None

is_tracking: bool = False
tracking_flags: TrackingFlag = TrackingFlag(0)

def has_add_user_flag(self) -> bool:
"""Returns whether this user has any tracking flags set related to
AddUser
"""
add_user_flags = TrackingFlag.FRIEND | TrackingFlag.REQUESTED | TrackingFlag.TRANSFER
return self.tracking_flags & add_user_flags != TrackingFlag(0)

def update_from_user_stats(self, user_stats: UserStats):
self.avg_speed = user_stats.avg_speed
Expand Down Expand Up @@ -88,6 +110,9 @@ class ChatMessage:
message: str
is_admin: bool

def is_server_message(self) -> bool:
return self.is_admin and self.user.name == 'server'


@dataclass
class RoomMessage:
Expand Down
Loading

0 comments on commit c7a5930

Please sign in to comment.