Skip to content

Commit 98dd6de

Browse files
committed
Fix up blocking support
1 parent 5848253 commit 98dd6de

File tree

8 files changed

+133
-7
lines changed

8 files changed

+133
-7
lines changed

Signal-Windows.Lib/IncomingMessages.cs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,11 @@ private void HandleMessage(SignalServiceEnvelope envelope)
184184
}
185185
}
186186
}
187+
else if (content.SynchronizeMessage.BlockedList != null)
188+
{
189+
List<string> blockedNumbers = content.SynchronizeMessage.BlockedList.Numbers;
190+
HandleBlockedNumbers(blockedNumbers);
191+
}
187192
else if (content.SynchronizeMessage.Groups != null)
188193
{
189194
Logger.LogInformation("HandleMessage() handling groups sync message from device {0}", envelope.GetSourceDevice());
@@ -382,6 +387,38 @@ private void HandleSessionResetMessage(SignalServiceEnvelope envelope, SignalSer
382387
SignalLibHandle.Instance.SaveAndDispatchSignalMessage(sm, conversation);
383388
}
384389

390+
/// <summary>
391+
/// Handles a list of blocked numbers. This will update the database to match the
392+
/// blocked numbers list.
393+
/// </summary>
394+
/// <param name="blockedNumbers">The list of blocked numbers.</param>
395+
private void HandleBlockedNumbers(List<string> blockedNumbers)
396+
{
397+
List<SignalContact> blockedContacts = new List<SignalContact>();
398+
List<SignalContact> contacts = SignalDBContext.GetAllContactsLocked();
399+
foreach (var contact in contacts)
400+
{
401+
if (blockedNumbers.Contains(contact.ThreadId))
402+
{
403+
if (!contact.Blocked)
404+
{
405+
contact.Blocked = true;
406+
SignalDBContext.UpdateBlockStatus(contact);
407+
blockedContacts.Add(contact);
408+
}
409+
}
410+
else
411+
{
412+
if (contact.Blocked)
413+
{
414+
contact.Blocked = false;
415+
SignalDBContext.UpdateBlockStatus(contact);
416+
}
417+
}
418+
}
419+
SignalLibHandle.Instance.DispatchHandleBlockedContacts(blockedContacts);
420+
}
421+
385422
private void HandleGroupLeaveMessage(SignalServiceEnvelope envelope, SignalServiceContent content, SignalServiceDataMessage dataMessage, bool isSync, long timestamp)
386423
{
387424
SignalServiceGroup sentGroup = dataMessage.Group;
@@ -575,7 +612,7 @@ private void HandleSignalMessage(SignalServiceEnvelope envelope, SignalServiceCo
575612
if (author != null && author.Blocked)
576613
{
577614
// Don't save blocked messages
578-
//return;
615+
return;
579616
}
580617

581618
List<SignalAttachment> attachments = new List<SignalAttachment>();

Signal-Windows.Lib/Signal-Windows.Lib.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@
166166
</ItemGroup>
167167
<ItemGroup>
168168
<PackageReference Include="libsignal-service-dotnet">
169-
<Version>2.7.5.6</Version>
169+
<Version>2.7.5.8</Version>
170170
</PackageReference>
171171
<PackageReference Include="Microsoft.EntityFrameworkCore">
172172
<Version>1.1.4</Version>

Signal-Windows.Lib/SignalLibHandle.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public interface ISignalFrontend
4747
void ReplaceConversationList(List<SignalConversation> conversations);
4848
void HandleAuthFailure();
4949
void HandleAttachmentStatusChanged(SignalAttachment sa);
50+
void HandleBlockedContacts(List<SignalContact> blockedContacts);
5051
}
5152

5253
public interface ISignalLibHandle
@@ -56,6 +57,7 @@ public interface ISignalLibHandle
5657

5758
void RequestSync();
5859
Task SendMessage(SignalMessage message, SignalConversation conversation);
60+
void SendBlockedMessage();
5961
Task SetMessageRead(long index, SignalMessage message, SignalConversation conversation);
6062
void ResendMessage(SignalMessage message);
6163
List<SignalMessageContainer> GetMessages(SignalConversation thread, int startIndex, int count);
@@ -394,6 +396,19 @@ public void SaveAndDispatchSignalConversation(SignalConversation updatedConversa
394396
SemaphoreSlim.Release();
395397
Logger.LogTrace("SaveAndDispatchSignalConversation() released");
396398
}
399+
400+
public void SendBlockedMessage()
401+
{
402+
List<SignalContact> blockedContacts = SignalDBContext.GetAllContactsLocked().Where(c => c.Blocked).ToList();
403+
List<string> blockedNumbers = new List<string>();
404+
foreach (var contact in blockedContacts)
405+
{
406+
blockedNumbers.Add(contact.ThreadId);
407+
}
408+
var blockMessage = SignalServiceSyncMessage.ForBlocked(new BlockedListMessage(blockedNumbers));
409+
OutgoingMessages.SendMessage(blockMessage);
410+
DispatchHandleBlockedContacts(blockedContacts);
411+
}
397412
#endregion
398413

399414
#region attachment api
@@ -609,6 +624,24 @@ internal void HandleOutgoingKeyChangeLocked(string user, string identity)
609624
SemaphoreSlim.Release();
610625
Logger.LogTrace("HandleOutgoingKeyChange() released");
611626
}
627+
628+
/// <summary>
629+
/// This will notify all windows of newly blocked numbers.
630+
/// This does not save to the database.
631+
/// </summary>
632+
/// <param name="blockedContacts">The list of blocked contacts</param>
633+
internal void DispatchHandleBlockedContacts(List<SignalContact> blockedContacts)
634+
{
635+
List<Task> operations = new List<Task>();
636+
foreach (var dispatcher in Frames.Keys)
637+
{
638+
operations.Add(dispatcher.RunTaskAsync(() =>
639+
{
640+
Frames[dispatcher].HandleBlockedContacts(blockedContacts);
641+
}));
642+
}
643+
Task.WaitAll(operations.ToArray());
644+
}
612645
#endregion
613646

614647
#region private

Signal-Windows/Controls/Conversation.xaml.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,19 @@ private void UpdateHeader(SignalConversation thread)
161161
public void Load(SignalConversation conversation)
162162
{
163163
SignalConversation = conversation;
164-
if (SignalConversation is SignalContact)
164+
var contact = SignalConversation as SignalContact;
165+
if (contact != null)
165166
{
166-
SignalContact contact = (SignalContact)SignalConversation;
167167
Blocked = contact.Blocked;
168168
SendMessageVisible = !Blocked;
169169
}
170+
else
171+
{
172+
// Need to make sure to reset the Blocked and SendMessageVisible values in case
173+
// a group chat is selected. Group chats can never be blocked.
174+
Blocked = false;
175+
SendMessageVisible = !Blocked;
176+
}
170177
LastMarkReadRequest = -1;
171178
InputTextBox.IsEnabled = false;
172179
DisposeCurrentThread();
@@ -383,7 +390,7 @@ private void ScrollViewer_ViewChanged(object sender, ScrollViewerViewChangedEven
383390
}
384391
}
385392

386-
private void UnblockButton_Click(object sender, RoutedEventArgs e)
393+
private async void UnblockButton_Click(object sender, RoutedEventArgs e)
387394
{
388395
if (SignalConversation is SignalContact)
389396
{
@@ -392,6 +399,10 @@ private void UnblockButton_Click(object sender, RoutedEventArgs e)
392399
Blocked = false;
393400
SendMessageVisible = !Blocked;
394401
SignalDBContext.UpdateBlockStatus(contact);
402+
await Task.Run(() =>
403+
{
404+
App.Handle.SendBlockedMessage();
405+
});
395406
}
396407
}
397408
}

Signal-Windows/SignalWindowsFrontend.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ public void HandleUnreadMessage(SignalMessage message)
9191
}
9292
}
9393

94+
public void HandleBlockedContacts(List<SignalContact> blockedContacts)
95+
{
96+
Locator.MainPageInstance.HandleBlockedContacts(blockedContacts);
97+
}
98+
9499
private void CheckNotification(SignalConversation conversation)
95100
{
96101
if (ApplicationView.GetForCurrentView().Id == App.MainViewId)

Signal-Windows/ViewModels/ConversationSettingsPageViewModel.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,15 @@ internal void BackButton_Click(object sender, BackRequestedEventArgs e)
129129
e.Handled = true;
130130
}
131131

132-
internal void BlockButton_Click()
132+
internal async void BlockButton_Click()
133133
{
134134
Contact.Blocked = !Contact.Blocked;
135135
Blocked = Contact.Blocked;
136136
SignalDBContext.UpdateBlockStatus(Contact);
137+
await Task.Run(() =>
138+
{
139+
App.Handle.SendBlockedMessage();
140+
});
137141
}
138142
}
139143
}

Signal-Windows/ViewModels/LinkPageViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ await Task.Run(() =>
8282
});
8383

8484
// fetch new device uuid
85-
SignalServiceAccountManager accountManager = new SignalServiceAccountManager(App.ServiceConfiguration, CancelSource.Token, "Signal-Windows");
85+
SignalServiceAccountManager accountManager = new SignalServiceAccountManager(App.ServiceConfiguration, "Signal-Windows");
8686
string uuid = await accountManager.GetNewDeviceUuid(CancelSource.Token);
8787
string tsdevice = "tsdevice:/?uuid=" + Uri.EscapeDataString(uuid) + "&pub_key=" + Uri.EscapeDataString(Base64.EncodeBytesWithoutPadding(tmpIdentity.getPublicKey().serialize()));
8888

Signal-Windows/ViewModels/MainPageViewModel.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using System.Collections.ObjectModel;
1616
using System.Diagnostics;
1717
using System.IO;
18+
using System.Linq;
1819
using System.Threading;
1920
using System.Threading.Tasks;
2021
using Windows.ApplicationModel.Core;
@@ -316,6 +317,41 @@ public void HandleAttachmentStatusChanged(SignalAttachment sa)
316317
View.Thread.UpdateAttachment(sa);
317318
}
318319
}
320+
321+
public void HandleBlockedContacts(List<SignalContact> blockedContacts)
322+
{
323+
// Signal sends a list of all blocked contacts, so if a blocked contacted was
324+
// unblocked it won't appear in the list anymore. So, unblock any blocked
325+
// contacts as well.
326+
foreach (var conversation in ConversationsDictionary)
327+
{
328+
// "as" is more efficient than "is" and a cast because "as" only needs to
329+
// type check once
330+
var conversationContact = conversation.Value as SignalContact;
331+
if (conversationContact != null)
332+
{
333+
if (blockedContacts.FirstOrDefault(c => c.ThreadId == conversationContact.ThreadId) != null)
334+
{
335+
// Check if the contact is blocked and if so make sure it's blocked
336+
if (!conversationContact.Blocked)
337+
{
338+
conversationContact.Blocked = true;
339+
conversationContact.UpdateUI();
340+
}
341+
}
342+
else
343+
{
344+
// If the contact is not in the blocked list then check if it's blocked
345+
// and if so, unblock it.
346+
if (conversationContact.Blocked)
347+
{
348+
conversationContact.Blocked = false;
349+
conversationContact.UpdateUI();
350+
}
351+
}
352+
}
353+
}
354+
}
319355
#endregion
320356
}
321357
}

0 commit comments

Comments
 (0)