Skip to content

Commit

Permalink
Parse buddylist presence updates, reconnect to MQTT
Browse files Browse the repository at this point in the history
  • Loading branch information
gave92 committed Nov 24, 2019
1 parent f0c4795 commit fe14554
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
43 changes: 41 additions & 2 deletions fbchat-sharp/API/Client.cs
Expand Up @@ -3618,9 +3618,18 @@ public async Task<bool> startListening(CancellationTokenSource _cancellationToke
}
});

mqttClient.UseDisconnectedHandler(e =>
mqttClient.UseDisconnectedHandler(async e =>
{
Debug.WriteLine("### DISCONNECTED FROM SERVER ###");
try
{
await Task.Delay(TimeSpan.FromSeconds(5), _cancellationTokenSource.Token);
await mqttClient.ConnectAsync(options, _cancellationTokenSource.Token);
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
}
});

await mqttClient.ConnectAsync(options, _cancellationTokenSource.Token);
Expand Down Expand Up @@ -3649,7 +3658,37 @@ private async Task _parse_mqtt(string event_type, JToken event_data)
event_data.get("lastIssuedSeqId")?.Value<int>() ?? event_data.get("deltas")?.LastOrDefault()?.get("irisSeqId")?.Value<int>() ?? _seq);
foreach (var delta in event_data.get("deltas") ?? new JObject())
await this._parseDelta(new JObject() { { "delta", delta } });
}
}
else if (new string[] { "/thread_typing", "/orca_typing_notifications" }.Contains(event_type))
{
var author_id = event_data.get("sender_fbid")?.Value<string>();
var thread_id = event_data.get("thread")?.Value<string>() ?? author_id;
var typing_status = (TypingStatus)(event_data.get("state")?.Value<int>());
await this.onTyping(
author_id: author_id,
status: typing_status,
thread_id: thread_id,
thread_type: thread_id == author_id ? ThreadType.USER : ThreadType.GROUP,
msg: event_data
);
}
else if (event_type == "/orca_presence")
{
var statuses = new Dictionary<string, FB_ActiveStatus>();
foreach (var data in event_data.get("list"))
{
var user_id = data["u"]?.Value<string>();

bool old_in_game = false;
if (this._buddylist.ContainsKey(user_id))
old_in_game = this._buddylist[user_id].in_game;

statuses[user_id] = FB_ActiveStatus._from_orca_presence(data, old_in_game);
this._buddylist[user_id] = statuses[user_id];

await this.onBuddylistOverlay(statuses: statuses, msg: event_data);
}
}
}

/// <summary>
Expand Down
3 changes: 2 additions & 1 deletion fbchat-sharp/API/Thread.cs
Expand Up @@ -12,7 +12,8 @@ public enum ThreadType
GROUP = 2,
ROOM = 2,
PAGE = 3,
INVALID = 4
MARKETPLACE = 4,
INVALID = 5
}

/// <summary>
Expand Down
8 changes: 8 additions & 0 deletions fbchat-sharp/API/User.cs
Expand Up @@ -301,5 +301,13 @@ public static FB_ActiveStatus _from_buddylist_update(JToken data)
last_active: null,
in_game: false);
}

public static FB_ActiveStatus _from_orca_presence(JToken data, bool in_game = false)
{
return new FB_ActiveStatus(
active: new int[] { 2, 3 }.Contains(data.get("p")?.Value<int>() ?? 0),
last_active: data.get("l")?.Value<string>(),
in_game: in_game);
}
}
}
1 change: 0 additions & 1 deletion fbchat-sharp/API/Utils.cs
Expand Up @@ -234,7 +234,6 @@ public static Type _to_class(this ThreadType threadType)
// return typeof(FB_Room);
case ThreadType.PAGE:
return typeof(FB_Page);
case ThreadType.INVALID:
default:
throw new FBchatException($"Invalid thread type: {threadType}.");
}
Expand Down

0 comments on commit fe14554

Please sign in to comment.