roles support - callbacks, peer screen

This commit is contained in:
ingvar1995 2018-07-23 00:35:52 +03:00
parent 5e1f060fac
commit 20f36e06ad
7 changed files with 139 additions and 33 deletions

View File

@ -237,7 +237,7 @@ class App:
self._profile_manager = ProfileManager(self._settings, self._toxes, profile_path) self._profile_manager = ProfileManager(self._settings, self._toxes, profile_path)
data = self._profile_manager.open_profile() data = self._profile_manager.open_profile()
if self._toxes.is_data_encrypted(data): if self._toxes.is_data_encrypted(data):
data = self._enter_pass(data) data = self._enter_password(data)
self._tox = self._create_tox(data) self._tox = self._create_tox(data)
def _create_new_profile(self, profile_name): def _create_new_profile(self, profile_name):
@ -292,7 +292,7 @@ class App:
# Other private methods # Other private methods
# ----------------------------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------------------------
def _enter_pass(self, data): def _enter_password(self, data):
""" """
Show password screen Show password screen
""" """
@ -326,6 +326,7 @@ class App:
self._calls_manager.set_toxav(self._tox.AV) self._calls_manager.set_toxav(self._tox.AV)
self._contacts_manager.update_friends_numbers() self._contacts_manager.update_friends_numbers()
self._contacts_manager.update_groups_lists()
self._init_callbacks() self._init_callbacks()

View File

@ -260,7 +260,8 @@ class ContactsManager(ToxSave):
text = util_ui.tr("Enter new alias for friend {} or leave empty to use friend's name:").format(name) text = util_ui.tr("Enter new alias for friend {} or leave empty to use friend's name:").format(name)
title = util_ui.tr('Set alias') title = util_ui.tr('Set alias')
text, ok = util_ui.text_dialog(text, title, name) text, ok = util_ui.text_dialog(text, title, name)
if ok: if not ok:
return
aliases = self._settings['friends_aliases'] aliases = self._settings['friends_aliases']
if text: if text:
friend.name = text friend.name = text
@ -362,6 +363,13 @@ class ContactsManager(ToxSave):
contact.reset_avatar(self._settings['identicons']) contact.reset_avatar(self._settings['identicons'])
self._save_profile() self._save_profile()
def remove_group_peer_by_id(self, group, peer_id):
peer = group.get_peer_by_id(peer_id)
if not self.check_if_contact_exists(peer.public_key):
return
contact = self.get_contact_by_tox_id(peer.public_key)
self.remove_group_peer(contact)
def remove_group_peer(self, group_peer_contact): def remove_group_peer(self, group_peer_contact):
contact = self.get_contact_by_tox_id(group_peer_contact.tox_id) contact = self.get_contact_by_tox_id(group_peer_contact.tox_id)
self._cleanup_contact_data(contact) self._cleanup_contact_data(contact)
@ -438,6 +446,11 @@ class ContactsManager(ToxSave):
group.number = i group.number = i
self.update_filtration() self.update_filtration()
def update_groups_lists(self):
groups = self._contact_provider.get_all_groups()
for group in groups:
group.remove_all_peers_except_self()
# ----------------------------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------------------------
# Private methods # Private methods
# ----------------------------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------------------------

View File

@ -28,6 +28,9 @@ class GroupChat(contact.Contact, ToxSave):
# Peers methods # Peers methods
# ----------------------------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------------------------
def get_self_peer(self):
return self._peers[0]
def get_self_name(self): def get_self_name(self):
return self._peers[0].name return self._peers[0].name

View File

@ -109,6 +109,19 @@ class GroupsService(tox_save.ToxSave):
self._peer_screen = widgets_factory.create_peer_screen_window(group, peer_id) self._peer_screen = widgets_factory.create_peer_screen_window(group, peer_id)
self._peer_screen.show() self._peer_screen.show()
# -----------------------------------------------------------------------------------------------------------------
# Peers actions
# -----------------------------------------------------------------------------------------------------------------
def set_new_peer_role(self, group, peer, role):
self._tox.group_mod_set_role(group.number, peer.id, role)
peer.role = role
self.generate_peers_list()
def toggle_ignore_peer(self, group, peer, ignore):
self._tox.group_toggle_ignore(group.number, peer.id, ignore)
peer.is_muted = ignore
# ----------------------------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------------------------
# Private methods # Private methods
# ----------------------------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------------------------

View File

@ -424,9 +424,10 @@ def group_peer_join(contacts_provider, groups_service):
return wrapped return wrapped
def group_peer_exit(contacts_provider, groups_service): def group_peer_exit(contacts_provider, groups_service, contacts_manager):
def wrapped(tox, group_number, peer_id, message, length, user_data): def wrapped(tox, group_number, peer_id, message, length, user_data):
group = contacts_provider.get_group_by_number(group_number) group = contacts_provider.get_group_by_number(group_number)
contacts_manager.remove_group_peer_by_id(group, peer_id)
group.remove_peer(peer_id) group.remove_peer(peer_id)
invoke_in_main_thread(groups_service.generate_peers_list) invoke_in_main_thread(groups_service.generate_peers_list)
@ -461,6 +462,37 @@ def group_topic(contacts_provider):
return wrapped return wrapped
def group_moderation(groups_service, contacts_provider, contacts_manager, messenger):
def update_peer_role(group, mod_peer_id, peer_id, new_role):
peer = group.get_peer_by_id(peer_id)
peer.role = new_role
# TODO: add info message
def remove_peer(group, mod_peer_id, peer_id, is_ban):
contacts_manager.remove_group_peer_by_id(group, peer_id)
group.remove_peer(peer_id)
# TODO: add info message
def wrapped(tox, group_number, mod_peer_id, peer_id, event_type, user_data):
group = contacts_provider.get_group_by_number(group_number)
if event_type == TOX_GROUP_MOD_EVENT['KICK']:
remove_peer(group, mod_peer_id, peer_id, False)
elif event_type == TOX_GROUP_MOD_EVENT['BAN']:
remove_peer(group, mod_peer_id, peer_id, True)
elif event_type == TOX_GROUP_MOD_EVENT['OBSERVER']:
update_peer_role(group, mod_peer_id, peer_id, TOX_GROUP_ROLE['OBSERVER'])
elif event_type == TOX_GROUP_MOD_EVENT['USER']:
update_peer_role(group, mod_peer_id, peer_id, TOX_GROUP_ROLE['USER'])
elif event_type == TOX_GROUP_MOD_EVENT['MODERATOR']:
update_peer_role(group, mod_peer_id, peer_id, TOX_GROUP_ROLE['MODERATOR'])
groups_service.generate_peers_list()
return wrapped
# ----------------------------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------------------------
# Callbacks - initialization # Callbacks - initialization
# ----------------------------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------------------------
@ -523,7 +555,8 @@ def init_callbacks(tox, profile, settings, plugin_loader, contacts_manager,
tox.callback_group_invite(group_invite(groups_service), 0) tox.callback_group_invite(group_invite(groups_service), 0)
tox.callback_group_self_join(group_self_join(contacts_provider, groups_service), 0) tox.callback_group_self_join(group_self_join(contacts_provider, groups_service), 0)
tox.callback_group_peer_join(group_peer_join(contacts_provider, groups_service), 0) tox.callback_group_peer_join(group_peer_join(contacts_provider, groups_service), 0)
tox.callback_group_peer_exit(group_peer_exit(contacts_provider, groups_service), 0) tox.callback_group_peer_exit(group_peer_exit(contacts_provider, groups_service, contacts_manager), 0)
tox.callback_group_peer_name(group_peer_name(contacts_provider, groups_service), 0) tox.callback_group_peer_name(group_peer_name(contacts_provider, groups_service), 0)
tox.callback_group_peer_status(group_peer_status(contacts_provider, groups_service), 0) tox.callback_group_peer_status(group_peer_status(contacts_provider, groups_service), 0)
tox.callback_group_topic(group_topic(contacts_provider), 0) tox.callback_group_topic(group_topic(contacts_provider), 0)
tox.callback_group_moderation(group_moderation(groups_service, contacts_provider, contacts_manager, messenger), 0)

View File

@ -14,36 +14,69 @@ class PeerScreen(CenteredWidget):
self._group = group self._group = group
self._peer = group.get_peer_by_id(peer_id) self._peer = group.get_peer_by_id(peer_id)
self._roles = {
TOX_GROUP_ROLE['FOUNDER']: util_ui.tr('Administrator'),
TOX_GROUP_ROLE['MODERATOR']: util_ui.tr('Moderator'),
TOX_GROUP_ROLE['USER']: util_ui.tr('User'),
TOX_GROUP_ROLE['OBSERVER']: util_ui.tr('Observer')
}
uic.loadUi(util.get_views_path('peer_screen'), self) uic.loadUi(util.get_views_path('peer_screen'), self)
self._update_ui() self._update_ui()
def _update_ui(self): def _update_ui(self):
self.statusCircle = StatusCircle(self) self.statusCircle = StatusCircle(self)
self.statusCircle.setGeometry(50, 20, 20, 20) self.statusCircle.setGeometry(50, 15, 30, 30)
self.statusCircle.update(self._peer.status) self.statusCircle.update(self._peer.status)
self.peerNameLabel.setText(self._peer.name) self.peerNameLabel.setText(self._peer.name)
self.ignorePeerCheckBox.setChecked(self._peer.is_muted) self.ignorePeerCheckBox.setChecked(self._peer.is_muted)
self.ignorePeerCheckBox.clicked.connect(self._toggle_ignore)
self.sendPrivateMessagePushButton.clicked.connect(self._send_private_message) self.sendPrivateMessagePushButton.clicked.connect(self._send_private_message)
self.copyPublicKeyPushButton.clicked.connect(self._copy_public_key) self.copyPublicKeyPushButton.clicked.connect(self._copy_public_key)
self.roleNameLabel.setText(self._get_role_name()) self.roleNameLabel.setText(self._get_role_name())
can_change_role = self._can_change_role()
self.rolesComboBox.setVisible(can_change_role)
self.roleNameLabel.setVisible(not can_change_role)
self._retranslate_ui() self._retranslate_ui()
self.rolesComboBox.currentIndexChanged.connect(self._role_set)
def _retranslate_ui(self): def _retranslate_ui(self):
self.setWindowTitle(util_ui.tr('Peer details')) self.setWindowTitle(util_ui.tr('Peer details'))
self.ignorePeerCheckBox.setText(util_ui.tr('Ignore peer')) self.ignorePeerCheckBox.setText(util_ui.tr('Ignore peer'))
self.roleLabel.setText(util_ui.tr('Role:')) self.roleLabel.setText(util_ui.tr('Role:'))
self.copyPublicKeyPushButton.setText(util_ui.tr('Copy public key')) self.copyPublicKeyPushButton.setText(util_ui.tr('Copy public key'))
self.sendPrivateMessagePushButton.setText(util_ui.tr('Send private message')) self.sendPrivateMessagePushButton.setText(util_ui.tr('Send private message'))
self.banGroupBox.setTitle(util_ui.tr('Moderation'))
self.rolesComboBox.clear()
index = self._group.get_self_peer().role
roles = list(self._roles.values())
for role in roles[index + 1:]:
self.rolesComboBox.addItem(role)
self.rolesComboBox.setCurrentIndex(self._peer.role - index - 1)
def _can_change_role(self):
self_peer = self._group.get_self_peer()
if self_peer.role > TOX_GROUP_ROLE['MODERATOR']:
return False
return self_peer.role < self._peer.role
def _role_set(self):
index = self.rolesComboBox.currentIndex()
all_roles_count = len(self._roles)
diff = all_roles_count - self.rolesComboBox.count()
self._groups_service.set_new_peer_role(self._group, self._peer, index + diff)
def _get_role_name(self): def _get_role_name(self):
roles = { return self._roles[self._peer.role]
0: util_ui.tr('Administrator'),
1: util_ui.tr('Moderator'),
2: util_ui.tr('User'),
3: util_ui.tr('Observer')
}
return roles[self._peer.role] def _toggle_ignore(self):
ignore = self.ignorePeerCheckBox.isChecked()
self._groups_service.toggle_ignore_peer(self._group, self._peer, ignore)
def _send_private_message(self): def _send_private_message(self):
self._contacts_manager.add_group_peer(self._group, self._peer) self._contacts_manager.add_group_peer(self._group, self._peer)

View File

@ -93,9 +93,9 @@
<widget class="QLabel" name="roleNameLabel"> <widget class="QLabel" name="roleNameLabel">
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>140</x> <x>130</x>
<y>60</y> <y>60</y>
<width>401</width> <width>411</width>
<height>20</height> <height>20</height>
</rect> </rect>
</property> </property>
@ -116,6 +116,16 @@
<string>PushButton</string> <string>PushButton</string>
</property> </property>
</widget> </widget>
<widget class="QComboBox" name="rolesComboBox">
<property name="geometry">
<rect>
<x>130</x>
<y>55</y>
<width>291</width>
<height>30</height>
</rect>
</property>
</widget>
</widget> </widget>
<resources/> <resources/>
<connections/> <connections/>