Skip to content

Commit

Permalink
Fix TitleSet.title attribute, add Connect/Disconnect and Login events…
Browse files Browse the repository at this point in the history
…, make MessengerClient not abstract
  • Loading branch information
gave92 committed Apr 19, 2020
1 parent bbe9ba5 commit 1369e57
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 47 deletions.
2 changes: 1 addition & 1 deletion examples/netcore/FBClient_Cookies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class FBClient_Cookies : MessengerClient

public FBClient_Cookies()
{
this.Set2FACallback(get2FACode);
On2FACodeCallback = get2FACode;
}

private async Task<string> get2FACode()
Expand Down
2 changes: 1 addition & 1 deletion examples/netcore/FBClient_Simple.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class FBClient_Simple : MessengerClient
{
public FBClient_Simple()
{
this.Set2FACallback(get2FACode);
On2FACodeCallback = get2FACode;
}

private async Task<string> get2FACode()
Expand Down
4 changes: 2 additions & 2 deletions examples/uwpapp/LoginPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ public LoginPage()

private void LoginPage_Loaded(object sender, RoutedEventArgs e)
{
Client.Set2FACallback(On2FACallback);
Client.On2FACodeCallback = On2FACallback;
}

private void LoginPage_Unloaded(object sender, RoutedEventArgs e)
{
Client.Set2FACallback(null);
Client.On2FACodeCallback = null;
}

private async Task<string> On2FACallback()
Expand Down
2 changes: 1 addition & 1 deletion examples/uwpapp/uwpapp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<AssemblyName>uwpapp</AssemblyName>
<DefaultLanguage>it-IT</DefaultLanguage>
<TargetPlatformIdentifier>UAP</TargetPlatformIdentifier>
<TargetPlatformVersion Condition=" '$(TargetPlatformVersion)' == '' ">10.0.18362.0</TargetPlatformVersion>
<TargetPlatformVersion Condition=" '$(TargetPlatformVersion)' == '' ">10.0.17763.0</TargetPlatformVersion>
<TargetPlatformMinVersion>10.0.16299.0</TargetPlatformMinVersion>
<MinimumVisualStudioVersion>14</MinimumVisualStudioVersion>
<FileAlignment>512</FileAlignment>
Expand Down
4 changes: 2 additions & 2 deletions examples/wpfapp/LoginPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public LoginPage()

private void LoginPage_Loaded(object sender, RoutedEventArgs e)
{
Client.Set2FACallback(On2FACallback);
Client.On2FACodeCallback = On2FACallback;
}

private void LoginPage_Unloaded(object sender, RoutedEventArgs e)
{
Client.Set2FACallback(null);
Client.On2FACodeCallback = null;
}

private async Task<string> On2FACallback()
Expand Down
48 changes: 25 additions & 23 deletions fbchat-sharp/API/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public Client()
/// <param name="user_agent"></param>
public async Task<Session> fromSession(Dictionary<string, List<Cookie>> session_cookies = null, string user_agent = null)
{
await this.onLoggingIn(email: null);

// If session cookies aren't set, not properly loaded or gives us an invalid session, then do the login
if (
session_cookies == null ||
Expand All @@ -58,6 +60,7 @@ public async Task<Session> fromSession(Dictionary<string, List<Cookie>> session_
throw new FBchatException(message: "Login from session failed.");
}

await this.onLoggedIn(email: null);
return _session;
}

Expand Down Expand Up @@ -138,7 +141,8 @@ public async Task<bool> logout()
{
if (await this._session.logout())
{
this._session = null;
await this.onLoggedOut();
this._session = null;
return true;
}
return false;
Expand Down Expand Up @@ -935,44 +939,42 @@ public async Task deleteMessages(List<string> message_ids)

#region EVENTS
/// <summary>
/// Called when the client is logging in
/// Called when a 2FA code is requested
/// </summary>
/// <param name="email">The email of the client</param>
protected virtual async Task onLoggingIn(string email = null)
protected virtual async Task<string> on2FACode()
{
/*
* Called when the client is logging in
* :param email: The email of the client
* */
Debug.WriteLine(string.Format("Logging in {0}...", email));
await Task.Yield();
throw new NotImplementedException("You should override this.");
}

/// <summary>
/// Called when a 2FA code is requested
/// Called when the client is logging in
/// </summary>
protected virtual async Task<string> on2FACode()
/// <param name="email">The email of the client. Null if logging in from cookies</param>
protected virtual async Task onLoggingIn(string email)
{
/*
* Called when a 2FA code is requested
*/
Debug.WriteLine(string.Format("Logging in {0}...", email));
await Task.Yield();
throw new NotImplementedException("You should override this.");
}

/// <summary>
/// Called when the client is successfully logged in
/// Called when the client has successfully logged in
/// </summary>
/// <param name="email">The email of the client</param>
protected virtual async Task onLoggedIn(string email = null)
/// <param name="email">The email of the client. Null if logging in from cookies</param>
protected virtual async Task onLoggedIn(string email)
{
/*
* Called when the client is successfully logged in
* :param email: The email of the client
* */
Debug.WriteLine(string.Format("Login of {0} successful.", email));
await Task.Yield();
}
}

/// <summary>
/// Called when the client has successfully logged out
/// </summary>
protected virtual async Task onLoggedOut()
{
Debug.WriteLine(string.Format("Logout of {0} successful.", this._session?.user?.uid));
await Task.Yield();
}
#endregion

/// <returns>Pretty string representation of the client</returns>
Expand Down
45 changes: 45 additions & 0 deletions fbchat-sharp/API/Events/Event.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,49 @@ internal static FB_Presence _parse(Session session, JToken data)
};
}
}

/// <summary>
/// The client was connected to Facebook.
/// This is not guaranteed to be triggered the same amount of times `Disconnect`!
/// </summary>
public class FB_Connect : FB_Event
{

}

/// <summary>
/// The client lost the connection to Facebook.
/// This is not guaranteed to be triggered the same amount of times `Connect`!
/// </summary>
public class FB_Disconnect : FB_Event
{
/// The reason / error string for the disconnect
public string Reason { get; set; }
}

/// <summary>
/// Called when the client is logging in
/// </summary>
public class FB_LoggingIn : FB_Event
{
/// The email of the client
public string Email { get; set; }
}

/// <summary>
/// Called when the client has successfully logged in
/// </summary>
public class FB_LoggedIn : FB_Event
{
/// The email of the client
public string Email { get; set; }
}

/// <summary>
/// Called when the client has successfully logged out
/// </summary>
public class FB_LoggedOut : FB_Event
{

}
}
2 changes: 2 additions & 0 deletions fbchat-sharp/API/Mqtt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ await listener.mqttClient.SubscribeAsync(
await listener._messenger_queue_publish();

Debug.WriteLine("MQTT: subscribed");
if (onEvent != null) await onEvent(new FB_Connect());
});

listener.mqttClient.UseApplicationMessageReceivedHandler(async e =>
Expand All @@ -110,6 +111,7 @@ await listener.mqttClient.SubscribeAsync(
listener.mqttClient.UseDisconnectedHandler(async e =>
{
Debug.WriteLine("MQTT: disconnected from server");
if (onEvent != null) await onEvent(new FB_Disconnect() { Reason = "Connection lost, retrying" });
try
{
await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken);
Expand Down
45 changes: 28 additions & 17 deletions fbchat-sharp/MessengerClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace fbchat_sharp.API
/// <summary>
/// Facebook Client wrapper class. Library users should use this class
/// </summary>
public abstract class MessengerClient : Client
public class MessengerClient : Client
{
/*
* METHODS
Expand Down Expand Up @@ -129,41 +129,52 @@ protected void Log(string message, [CallerMemberName] string method = null)
/// <summary>
/// Use this to set the callback for providing a 2FA code
/// </summary>
public void Set2FACallback(Func<Task<string>> get2FACode)
{
this.get2FACode = get2FACode;
}

private Func<Task<string>> get2FACode;

/// <summary>
/// Called when a 2FA code is requested
/// </summary>
public Func<Task<string>> On2FACodeCallback { get; set; }
/// <inheritdoc />
protected override async Task<string> on2FACode()
{
if (get2FACode == null)
if (On2FACodeCallback == null)
{
this.Log("2FA code callback is not set. Use Set2FACallback().");
this.Log("2FA code callback is not set. Set the On2FACodeCallback property.");
return null;
}
return await get2FACode();
return await On2FACodeCallback?.Invoke();
}

/// <inheritdoc />
protected override async Task onLoggingIn(string email) => await this.OnEvent(new FB_LoggingIn() { Email = email });
/// <inheritdoc />
protected override async Task onLoggedIn(string email) => await this.OnEvent(new FB_LoggedIn() { Email = email });
/// <inheritdoc />
protected override async Task onLoggedOut() => await this.OnEvent(new FB_LoggedOut());

/// <summary>
/// How to delete saved cookies from disk
/// </summary>
protected abstract Task DeleteCookiesAsync();
protected virtual async Task DeleteCookiesAsync()
{
await Task.Yield();
}

/// <summary>
/// How to save a list of cookies to disk
/// </summary>
/// <param name="cookieJar">List of session cookies</param>
protected abstract Task WriteCookiesToDiskAsync(Dictionary<string, List<Cookie>> cookieJar);
protected virtual async Task WriteCookiesToDiskAsync(Dictionary<string, List<Cookie>> cookieJar)
{
this.Log("You should always implement ReadCookiesFromDiskAsync() and WriteCookiesToDiskAsync().");
await Task.Yield();
}

/// <summary>
/// How to load a list of saved cookies
/// </summary>
protected abstract Task<Dictionary<string, List<Cookie>>> ReadCookiesFromDiskAsync();
protected virtual async Task<Dictionary<string, List<Cookie>>> ReadCookiesFromDiskAsync()
{
this.Log("You should always implement ReadCookiesFromDiskAsync() and WriteCookiesToDiskAsync().");
await Task.Yield();
return null;
}

#region PRIVATE
private CancellationTokenSource _cancellationTokenSource;
Expand Down

0 comments on commit 1369e57

Please sign in to comment.