Skip to content
Permalink
Browse files

Added and fixed MUC nickname changing

  • Loading branch information...
COM8 committed Sep 29, 2019
1 parent 9267372 commit 335f3cb7825a369d74364222908bf6e78b704909
@@ -31,6 +31,15 @@ public static ConfiguredTaskAwaitable ConfAwaitFalse(this Task task)
return task.ConfigureAwait(false);
}

/// <summary>
/// Configures await to false.
/// Same as: .ConfigureAwait(false)
/// </summary>
public static ConfiguredTaskAwaitable<T> ConfAwaitFalse<T>(this Task<T> task)
{
return task.ConfigureAwait(false);
}

#endregion

#region --Misc Methods (Private)--
@@ -57,27 +57,12 @@
Text="{x:Bind VIEW_MODEL.MODEL.MucState, Mode=OneWay, Converter={StaticResource MucStateStringValueConverter}}"/>
</Border>
</controls:DropShadowPanel>
<Grid Grid.Row="2"
Margin="0,10,0,0"
HorizontalAlignment="Center">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0"
VerticalAlignment="Center"
Foreground="{ThemeResource CaptionTextBrush}"
Text="{x:Bind VIEW_MODEL.MODEL.Nickname, Mode=OneWay}"
TextTrimming="CharacterEllipsis"/>
<Button Grid.Column="1"
Margin="5,0,0,0"
VerticalAlignment="Center"
Content="&#xE70F;"
FontSize="15"
FontWeight="Normal"
Style="{ThemeResource TransparentIconButtonStyle}"
ToolTipService.ToolTip="Change Nickname"/>
</Grid>
<TextBlock Grid.Row="2"
Margin="0,10,0,0"
HorizontalAlignment="Center"
Foreground="{ThemeResource CaptionTextBrush}"
Text="{x:Bind VIEW_MODEL.MODEL.Nickname, Mode=OneWay}"
TextTrimming="CharacterEllipsis"/>
</Grid>
<Grid x:Name="details_grid"
Grid.Column="1"
@@ -166,6 +151,13 @@
Glyph="&#xE734;"/>
</MenuFlyoutItem.Icon>
</MenuFlyoutItem>
<MenuFlyoutItem x:Name="changeNickname_mfo"
Click="ChangeNickname_mfo_Click"
Text="Change Nickname">
<MenuFlyoutItem.Icon>
<FontIcon Glyph="&#xE70F;"/>
</MenuFlyoutItem.Icon>
</MenuFlyoutItem>
<ToggleMenuFlyoutItem x:Name="autoJoin_tmfo"
Click="AutoJoin_tmfo_Click"
IsChecked="{x:Bind VIEW_MODEL.MODEL.AutoJoin, Mode=OneWay}"
@@ -1,5 +1,7 @@
using Data_Manager2.Classes.DBTables;
using Shared.Classes;
using UWPX_UI.Dialogs;
using UWPX_UI_Context.Classes;
using UWPX_UI_Context.Classes.DataContext.Controls.Chat;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
@@ -117,6 +119,12 @@ private async void AutoJoin_tmfo_Click(object sender, RoutedEventArgs e)
await VIEW_MODEL.ToggleMucAutoJoinAsync(MucInfo).ConfAwaitFalse();
}

private async void ChangeNickname_mfo_Click(object sender, RoutedEventArgs e)
{
ChangeNicknameDialog dialog = new ChangeNicknameDialog(Chat, MucInfo, Client);
await UiUtils.ShowDialogAsync(dialog);
}

#endregion
}
}
@@ -0,0 +1,33 @@
<ContentDialog x:Class="UWPX_UI.Dialogs.ChangeNicknameDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="using:UWPX_UI.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="Change Nickname"
Background="{ThemeResource AppBackgroundAcrylicElementBrush}"
IsPrimaryButtonEnabled="{x:Bind VIEW_MODEL.MODEL.IsSaving, Mode=OneWay, Converter={StaticResource BoolInverterValueConverter}}"
IsSecondaryButtonEnabled="{x:Bind VIEW_MODEL.MODEL.IsSaveEnabled, Mode=OneWay}"
PrimaryButtonClick="ContentDialog_PrimaryButtonClick"
PrimaryButtonStyle="{ThemeResource DeclineButtonRevealStyle}"
PrimaryButtonText="Cancel"
SecondaryButtonClick="ContentDialog_SecondaryButtonClick"
SecondaryButtonStyle="{ThemeResource SaveButtonRevealStyle}"
SecondaryButtonText="Save"
mc:Ignorable="d">
<StackPanel>
<ProgressBar IsIndeterminate="True"
Visibility="{x:Bind VIEW_MODEL.MODEL.IsSaving, Mode=OneWay, Converter={StaticResource BoolVisibilityValueConverter}}"/>
<controls:IconTextBlockControl Foreground="Red"
GlyphForeground="Red"
Text="Changing nickname failed!"
Visibility="{x:Bind VIEW_MODEL.MODEL.Error, Mode=OneWay, Converter={StaticResource BoolVisibilityValueConverter}}"/>
<TextBox IsEnabled="{x:Bind VIEW_MODEL.MODEL.IsSaving, Mode=OneWay, Converter={StaticResource BoolInverterValueConverter}}"
PlaceholderText="Nickname"
Text="{x:Bind VIEW_MODEL.MODEL.Nickname, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<TextBlock Margin="0,10,0,0"
Foreground="{ThemeResource CaptionTextBrush}"
Text="{x:Bind VIEW_MODEL.MODEL.MucName, Mode=OneWay}"
TextTrimming="CharacterEllipsis"/>
</StackPanel>
</ContentDialog>
@@ -0,0 +1,64 @@
using Data_Manager2.Classes.DBTables;
using UWPX_UI_Context.Classes.DataContext.Dialogs;
using Windows.UI.Xaml.Controls;
using XMPP_API.Classes;

namespace UWPX_UI.Dialogs
{
public sealed partial class ChangeNicknameDialog: ContentDialog
{
//--------------------------------------------------------Attributes:-----------------------------------------------------------------\\
#region --Attributes--
public readonly ChangeNicknameDialogContext VIEW_MODEL = new ChangeNicknameDialogContext();
private readonly ChatTable CHAT;
private readonly MUCChatInfoTable MUC_INFO;
private readonly XMPPClient CLIENT;

#endregion
//--------------------------------------------------------Constructor:----------------------------------------------------------------\\
#region --Constructors--
public ChangeNicknameDialog(ChatTable chat, MUCChatInfoTable mucInfo, XMPPClient client)
{
CHAT = chat;
MUC_INFO = mucInfo;
CLIENT = client;
VIEW_MODEL.UpdateView(chat, mucInfo);
InitializeComponent();
}

#endregion
//--------------------------------------------------------Set-, Get- Methods:---------------------------------------------------------\\
#region --Set-, Get- Methods--


#endregion
//--------------------------------------------------------Misc Methods:---------------------------------------------------------------\\
#region --Misc Methods (Public)--


#endregion

#region --Misc Methods (Private)--


#endregion

#region --Misc Methods (Protected)--


#endregion
//--------------------------------------------------------Events:---------------------------------------------------------------------\\
#region --Events--
private void ContentDialog_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
{
Hide();
}

private async void ContentDialog_SecondaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
{
args.Cancel = await VIEW_MODEL.SaveAsync(CHAT, MUC_INFO, CLIENT);
}

#endregion
}
}
@@ -108,15 +108,25 @@ private async void INSTANCE_ChatChanged(ChatDBManager handler, ChatChangedEventA
{
if (!(args.CHAT is null) && string.Equals(args.CHAT.id, chatId))
{
await SharedUtils.CallDispatcherAsync(() => Chat = args.CHAT).ConfAwaitFalse();
await SharedUtils.CallDispatcherAsync(() =>
{
// Force update property:
Chat = null;
Chat = args.CHAT;
}).ConfAwaitFalse();
}
}

private async void INSTANCE_MUCInfoChanged(MUCDBManager handler, MUCInfoChangedEventArgs args)
{
if (!(args.MUC_INFO is null) && string.Equals(args.MUC_INFO.chatId, chatId))
{
await SharedUtils.CallDispatcherAsync(() => MucInfo = args.MUC_INFO).ConfAwaitFalse();
await SharedUtils.CallDispatcherAsync(() =>
{
// Force update property:
MucInfo = null;
MucInfo = args.MUC_INFO;
}).ConfAwaitFalse();
}
}

@@ -316,6 +316,9 @@
<Compile Include="Dialogs\CertificateRequirementsDialog.xaml.cs">
<DependentUpon>CertificateRequirementsDialog.xaml</DependentUpon>
</Compile>
<Compile Include="Dialogs\ChangeNicknameDialog.xaml.cs">
<DependentUpon>ChangeNicknameDialog.xaml</DependentUpon>
</Compile>
<Compile Include="Dialogs\ChangePresenceDialog.xaml.cs">
<DependentUpon>ChangePresenceDialog.xaml</DependentUpon>
</Compile>
@@ -786,6 +789,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Dialogs\ChangeNicknameDialog.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Dialogs\ChangePresenceDialog.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
@@ -0,0 +1,141 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Data_Manager2.Classes.DBManager;
using Data_Manager2.Classes.DBTables;
using Logging;
using UWPX_UI_Context.Classes.DataTemplates.Dialogs;
using XMPP_API.Classes;
using XMPP_API.Classes.Network.XML.Messages;
using XMPP_API.Classes.Network.XML.Messages.Helper;
using XMPP_API.Classes.Network.XML.Messages.XEP_0045;
using XMPP_API.Classes.Network.XML.Messages.XEP_0048;

namespace UWPX_UI_Context.Classes.DataContext.Dialogs
{
public class ChangeNicknameDialogContext
{
//--------------------------------------------------------Attributes:-----------------------------------------------------------------\\
#region --Attributes--
public readonly ChangeNicknameDialogDataTemplate MODEL = new ChangeNicknameDialogDataTemplate();

#endregion
//--------------------------------------------------------Constructor:----------------------------------------------------------------\\
#region --Constructors--


#endregion
//--------------------------------------------------------Set-, Get- Methods:---------------------------------------------------------\\
#region --Set-, Get- Methods--


#endregion
//--------------------------------------------------------Misc Methods:---------------------------------------------------------------\\
#region --Misc Methods (Public)--
public void UpdateView(ChatTable chat, MUCChatInfoTable mucInfo)
{
MODEL.MucName = string.IsNullOrEmpty(mucInfo.name) ? chat.chatJabberId : mucInfo.name;
MODEL.Nickname = mucInfo.nickname;
}

/// <summary>
/// Updates the nickname in the local DB.
/// Also updates bookmarks if any exist.
/// </summary>
/// <returns>Returns true on success.</returns>
public async Task<bool> SaveAsync(ChatTable chat, MUCChatInfoTable mucInfo, XMPPClient client)
{
MODEL.IsSaving = true;
bool success = await Task.Run(async () =>
{
mucInfo.nickname = MODEL.Nickname;

// Try to update the nickname at the chat room:
bool result = await UpdateNicknameAsync(chat, mucInfo, client);
if (!result)
{
return false;
}

// Update the DB entry since the server could respond with a different nickname:
MUCDBManager.INSTANCE.setMUCChatInfo(mucInfo, false, true);

// Update bookmarks:
return chat.inRoster ? await UpdateBookmarksAsync(chat, client) : true;
});
MODEL.IsSaving = false;
MODEL.Error = !success;
if (success)
{
Logger.Info("Updating nickname to \"" + MODEL.Nickname + "\" in chat \"" + MODEL.MucName + "\" was successful!");
}
else
{
Logger.Info("Updating nickname to \"" + MODEL.Nickname + "\" in chat \"" + MODEL.MucName + "\" failed!");
}
return success;
}

#endregion

#region --Misc Methods (Private)--
private async Task<bool> UpdateBookmarksAsync(ChatTable chat, XMPPClient client)
{
List<ConferenceItem> conferences = MUCDBManager.INSTANCE.getXEP0048ConferenceItemsForAccount(client.getXMPPAccount().getBareJid());
MessageResponseHelperResult<IQMessage> result = await client.PUB_SUB_COMMAND_HELPER.setBookmars_xep_0048Async(conferences);
if (string.Equals(result.RESULT.TYPE, IQMessage.RESULT))
{
return true;
}
if (result.RESULT is IQErrorMessage errorMessage)
{
Logger.Warn("Failed to update XEP-0048 Bookmarks: " + errorMessage.ERROR_OBJ.ToString());
}
else
{
Logger.Warn("Failed to update XEP-0048 Bookmarks: " + result.RESULT.TYPE);
}
return false;
}

private async Task<bool> UpdateNicknameAsync(ChatTable chat, MUCChatInfoTable mucInfo, XMPPClient client)
{
MessageResponseHelperResult<MUCMemberPresenceMessage> result = await client.MUC_COMMAND_HELPER.changeNicknameAsync(chat.chatJabberId, mucInfo.nickname);
if (!string.IsNullOrEmpty(result.RESULT.ERROR_TYPE))
{
Logger.Warn("Failed to change nickname for room \"" + chat.chatJabberId + "\" to \"" + mucInfo.nickname + "\" with: " + result.RESULT.ERROR_MESSAGE);
return false;
}
// Nickname has been updated successfully:
else if (result.RESULT.STATUS_CODES.Contains(MUCPresenceStatusCode.MEMBER_NICK_CHANGED))
{
return true;
}
// The room has change the nickname:
else if (result.RESULT.STATUS_CODES.Contains(MUCPresenceStatusCode.ROOM_NICK_CHANGED))
{
if (!Utils.isFullJid(result.RESULT.JID))
{
Logger.Error("Expected a full JID as a MUC nickname changed message with status code 210, but received: " + result.RESULT.JID);
return false;
}
mucInfo.nickname = Utils.getJidResourcePart(result.RESULT.JID);
return true;
}
// Should not happen:
Logger.Error("Unknown MUC member presence status code combination received. This should not happen!");
return false;
}

#endregion

#region --Misc Methods (Protected)--


#endregion
//--------------------------------------------------------Events:---------------------------------------------------------------------\\
#region --Events--


#endregion
}
}

0 comments on commit 335f3cb

Please sign in to comment.
You can’t perform that action at this time.