Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing documentation #5

Open
arthef opened this issue Oct 3, 2020 · 2 comments
Open

Missing documentation #5

arthef opened this issue Oct 3, 2020 · 2 comments
Assignees

Comments

@arthef
Copy link
Contributor

arthef commented Oct 3, 2020

There is no documentation or example code on how to retrieve user's roster.

Please add code example on retrieving user's roster as well as other typical XMPP use cases:

  • roster
  • presence status for contact
  • own user vcard
  • contact vcard
@bmalkow
Copy link
Contributor

bmalkow commented Oct 6, 2020

Before I finish documentation, here you have short examples.

Just after connection is established, Halcyon asks for roster itself. You no need to do anything.
It is possible you will implement own roster storage with versioning. Here is short note about it.

	// Registering events listener before you connect to server
	client.eventBus.register<RosterEvent>(RosterEvent.TYPE) { event ->
		when (event) {
			is RosterEvent.ItemAdded -> println("Item ${event.item.jid} was pushed to local roster store")
			is RosterEvent.ItemUpdated -> println("Item ${event.item.jid} was updated in local roster store")
			is RosterEvent.ItemRemoved -> println("Item ${event.item.jid} was removed from local roster store")
		}
	}
	val rosterModule = client.getModule<RosterModule>(RosterModule.TYPE)!!
	val presenceModule = client.getModule<PresenceModule>(PresenceModule.TYPE)!!

	// Adding item to roster
	rosterModule.addItem(RosterItem("[email protected]".toBareJID(), "Rob Doe")).response {
		it.onSuccess {
			println("Request is accepted by server. Listen for ItemAdded event.")
		}
	}.send()

	// Updating item in roster
	val item = rosterModule.store.getItem("[email protected]".toBareJID())!!
	val itemNew = item.copy(name = "Rob")
	rosterModule.addItem(itemNew).response {
		it.onSuccess {
			println("Request is accepted by server. Listen for ItemUpdated event.")
		}
	}.send()

	// Removing item from roster
	rosterModule.deleteItem("[email protected]".toBareJID()).response {
		it.onSuccess {
			println("Request is accepted by server. Listen for ItemRemoved event.")
		}
	}.send()

Simple presence example:

	client.eventBus.register<ContactChangeStatusEvent> { event->
		// $presence is "best presence for contact", not just received presence. Best presence is presence with high priority.
		println("This is presence of contact ${event.jid}: ${event.presence}")
	}

	// Displaying presence of all items from roster:
	rosterModule.getAllItems().forEach { rosterItem ->
		val presence: Presence? = presenceModule.getBestPresenceOf(rosterItem.jid)
	}

Note, that presence may be null. You have to calculate "logic presence state" from presence stanza. You may use code like this:

	enum class Status {
		Away,
		Chat,
		Dnd,
		Error,
		Offline,
		Online,
		Unknown,
		Xa,
		None
	}

	private fun calculateStatus(presence: Presence?): Status {
		if (presence == null) return Status.Offline
		val type = presence.type
		val show = presence.show
		return when (type) {
			PresenceType.Error -> Status.Error
			PresenceType.Unavailable -> Status.Offline
			null -> when (show) {
				null -> Status.Online
				Show.XA -> Status.Xa
				Show.DnD -> Status.Dnd
				Show.Away -> Status.Away
				Show.Chat -> Status.Chat
			}
			else -> Status.Unknown
		}
	}

I will add similar code to library.

@arthef
Copy link
Contributor Author

arthef commented Oct 6, 2020

Thank you. What about vCard? What would be the best code to request vCard for each roster item that is being retrieved?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants