Select Git revision
PresenceService.swift
Andreas Traczyk authored and
Kateryna Kostiuk
committed
- Subscribes to presence update signals for that have been added.
- Update events can be subscribed to via the presence service.
- A contact's last known presence status can be queried via the presence
service.
Change-Id: I4e1ed87f980978469b267c34936da25d15cef7eb
Reviewed-by:
Kateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com>
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
PresenceService.swift 2.79 KiB
/*
* Copyright (C) 2017 Savoir-faire Linux Inc.
*
* Author: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
import SwiftyBeaver
import RxSwift
class PresenceService {
fileprivate let presenceAdapter: PresenceAdapter
fileprivate let log = SwiftyBeaver.self
var contactPresence: [String: Bool]
fileprivate let disposeBag = DisposeBag()
fileprivate let responseStream = PublishSubject<ServiceEvent>()
var sharedResponseStream: Observable<ServiceEvent>
init(withPresenceAdapter presenceAdapter: PresenceAdapter) {
self.responseStream.disposed(by: disposeBag)
self.sharedResponseStream = responseStream.share()
self.contactPresence = [String: Bool]()
self.presenceAdapter = presenceAdapter
PresenceAdapter.delegate = self
}
func subscribeBuddies(withAccount account: AccountModel, withContacts contacts: [ContactModel]) {
for contact in contacts {
subscribeBuddy(withAccountId: account.id,
withUri: contact.ringId,
withFlag: true)
}
}
func subscribeBuddy(withAccountId accountId: String,
withUri uri: String,
withFlag flag: Bool) {
presenceAdapter.subscribeBuddy(withURI: uri, withAccountId: accountId, withFlag: flag)
contactPresence[uri] = false
}
}
extension PresenceService: PresenceAdapterDelegate {
func newBuddyNotification(withAccountId accountId: String,
withUri uri: String,
withStatus status: Int,
withLineStatus lineStatus: String) {
contactPresence[uri] = status > 0 ? true : false
/*
The subscriber is intended to query the contactPresence dictionary
with the contact's ringId
*/
var event = ServiceEvent(withEventType: .presenceUpdated)
event.addEventInput(.uri, value: uri)
self.responseStream.onNext(event)
log.debug("newBuddyNotification: uri=\(uri), status=\(status)")
}
}