Skip to content

Instantly share code, notes, and snippets.

@squallstar
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save squallstar/37c77420c44711afc99e to your computer and use it in GitHub Desktop.
Save squallstar/37c77420c44711afc99e to your computer and use it in GitHub Desktop.
hipchatter bits
namespace HipChatter.Libs
{
public sealed class API
{
private static readonly API instance = new API();
public ObservableCollection<Contact> Contacts = new ObservableCollection<Contact>();
public ObservableCollection<Room> Rooms = new ObservableCollection<Room>();
public ObservableCollection<Chat> Chats = new ObservableCollection<Chat>();
.....
public static API Current
{
get
{
return instance;
}
}
.....
void OnMessage(object sender, MessageEventArgs e)
{
if (e.Message.Body == null || e.Message.Type == MessageType.error) return;
var msg = new HipChatter.Model.ChatMessage
{
From = e.Message.From,
Type = e.Message.Type,
Body = e.Message.Body,
Stamp = e.Message.Delay != null ? e.Message.Delay.Stamp : DateTime.Now
};
if (e.Message.Attribute("mid") != null)
{
msg.Mid = e.Message.Attribute("mid").Value;
}
Chat chat = (from Chat c in Chats where c.Jid.Bare == msg.From.Bare select c).FirstOrDefault();
if (chat != null)
{
if (msg.Mid != null)
{
if (chat.Messages.FirstOrDefault(m => m.Mid == msg.Mid) != null)
{
return;
}
}
chat.Messages.Add(msg);
}
if (msg.IsOneToOne)
{
if (chat == null)
{
Contact person = (from Contact c in Contacts where c.Jid == msg.From.Bare select c).FirstOrDefault();
if (person != null)
{
msg.From.Resource = person.Name;
chat = JoinOneToOneRoom(person);
chat.Messages.Add(msg);
}
}
if (chat != null && msg.IsVeryRecent())
{
Notifications.DisplayToast(msg.FromName, msg.Body, "jid://" + chat.Jid.Bare);
}
}
else if (chat != null)
{
(chat.Resource as Room).LastActive = msg.Stamp;
if (chat != null && msg.IsVeryRecent() && msg.Body.Contains(CurrentUser.MentionName))
{
Notifications.DisplayToast(msg.FromName, msg.Body, "jid://" + chat.Jid.Bare);
}
}
}
void SendKeepAlive()
{
if (!_isConnected) return;
client.Send(new PingIq { To = XmppDomain, Type = "get"});
}
void OnRosterItem(object sender, RosterEventArgs e)
{
var contact = Contacts.FirstOrDefault(c => c.Jid == e.RosterItem.Jid);
if (e.RosterItem.Subscription != Roster.Subscription.remove)
{
if (contact == null)
{
Contacts.Add(
new Contact
{
Name = e.RosterItem.Name ?? e.RosterItem.Jid,
Jid = e.RosterItem.Jid,
Mobile = e.RosterItem.GetAttribute("mobile"),
MentionName = e.RosterItem.GetAttribute("mention_name")
});
}
}
else
{
if (contact != null)
Contacts.Remove(contact);
}
}
}
}
using HipChatter.Libs;
namespace HipChatter.Model
{
public class Chat
{
public object Resource;
public jid Jid { get; set; }
public string Name;
private bool _isTyping = false;
public ObservableCollection<ChatMessage> Messages = new ObservableCollection<ChatMessage>();
/// <summary>
/// Returns true when the chat is a groupchat
/// </summary>
public bool IsRoom
{
get
{
return Resource.GetType() == typeof(Room);
}
}
/// <summary>
/// The Resource name. Could be the room name, or the contact name.
/// </summary>
public string ResourceName
{
get
{
if (IsRoom) return (Resource as Room).Name;
return (Resource as Contact).Name;
}
}
/// <summary>
/// Tells the server whether the user is typing a message into the chatroom
/// </summary>
public bool IsTyping
{
set
{
if (value == _isTyping || IsRoom) return;
_isTyping = value;
API.Current.SetChatTypingStatus(this, value);
}
}
/// <summary>
/// Sends a message to the chatroom
/// </summary>
/// <param name="message"></param>
public void SendMessage(string message)
{
var msg = new ChatMessage
{
From = API.Current.CurrentUser.Jid,
To = Jid,
Type = MsgType.chat,
Body = message
};
if (IsRoom)
{
msg.Type = MsgType.groupchat;
(Resource as Room).LastActive = DateTime.Now;
}
else
{
msg.Stamp = DateTime.Now;
Messages.Add(msg);
}
API.Current.SendMessage(msg);
IsTyping = false;
}
/// <summary>
/// Leaves the chat
/// </summary>
public void Leave()
{
API.Current.LeaveChatRoom(this);
}
}
}
@blackjid
Copy link

Nice!, thansk!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment