void PrintPrivateChatPresence(XmppPersonModel person, Presence pres)
{
Jid jid = pres.From;
XmppResourceModel resource;
if (person.Resources.TryGetValue(jid.Resource??"", out resource)) {
if (resource.Presence.Show == pres.Show
&& resource.Presence.Status == pres.Status
&& resource.Presence.Last == pres.Last
&& resource.Presence.XDelay == pres.XDelay
&& resource.Presence.Priority == pres.Priority
&& resource.Presence.Type == pres.Type
) {
// presence didn't change enough to warrent a display message -> abort
return;
}
}
MessageModel msg = CreatePresenceUpdateMessage(jid, person, pres);
if (!String.IsNullOrEmpty(jid.Resource)) {
var directchat = Session.GetChat(jid, ChatType.Person, this);
if (directchat != null) {
// in case of direct chat we still send this message
Session.AddMessageToChat(directchat, msg);
}
}
// a nonexisting resource going offline?
if (pres.Type == PresenceType.unavailable) {
if (!person.Resources.ContainsKey(jid.Resource??"")) {
return;
}
}
var res = person.GetOrCreateResource(jid);
var oldpres = res.Presence;
res.Presence = pres;
// highest pres
Jid hjid = jid;
Jid nextjid = jid;
// 2nd highest pres
Presence hpres = pres;
Presence nextpres = null;
bool amHighest = true;
bool wasHighest = true;
foreach (var pair in person.Resources) {
if (pair.Value == res) continue;
if (nextpres == null || pair.Value.Presence.Priority > nextpres.Priority) {
nextjid.Resource = pair.Key;
nextpres = pair.Value.Presence;
}
if (pair.Value.Presence.Priority > hpres.Priority) {
// someone has a higher priority than I do
// print the status of that resource
hjid.Resource = pair.Key;
hpres = pair.Value.Presence;
amHighest = false;
}
if (oldpres != null && pair.Value.Presence.Priority > oldpres.Priority) {
wasHighest = false;
}
}
if (pres.Type == PresenceType.available) {
// wasn't and isn't highiest prio -> ignore
if (!wasHighest && !amHighest) return;
// just another below zero prio -> ignore
if (amHighest && pres.Priority < 0) return;
// was highest, isn't anymore -> show presence of new highest
if (wasHighest && !amHighest) {
msg = CreatePresenceUpdateMessage(hjid, person, hpres);
}
} else if (pres.Type == PresenceType.unavailable) {
// still a resource left with positive priority
if (nextpres != null && nextpres.Priority >= 0) {
msg = CreatePresenceUpdateMessage(nextjid, person, nextpres);
}
}
var chat = Session.GetChat(jid.Bare, ChatType.Person, this);
if (chat != null) {
Session.AddMessageToChat(chat, msg);
}
if (ContactChat != null) {
Session.AddMessageToChat(ContactChat, msg);
}
}