Search in sources :

Example 1 with JsPeer

use of im.actor.core.js.entity.JsPeer in project actor-platform by actorapp.

the class JsNotificationsProvider method onNotification.

@Override
public void onNotification(Messenger messenger, List<Notification> topNotifications, int messagesCount, int conversationsCount) {
    String peerTitle;
    String peerKey = null;
    String peerAvatarUrl = null;
    String contentMessage = "";
    Notification notification = topNotifications.get(0);
    // Peer info
    Peer peer = notification.getPeer();
    if (conversationsCount == 1) {
        Avatar peerAvatar;
        JsPeer jsPeer = JsPeer.create(peer);
        if (peer.getPeerType() == PeerType.PRIVATE) {
            UserVM userVM = messenger.getUser(peer.getPeerId());
            peerTitle = userVM.getName().get();
            peerAvatar = userVM.getAvatar().get();
        } else {
            GroupVM groupVM = messenger.getGroup(peer.getPeerId());
            peerTitle = groupVM.getName().get();
            peerAvatar = groupVM.getAvatar().get();
        }
        if (peerAvatar != null && peerAvatar.getSmallImage() != null) {
            peerAvatarUrl = ((JsMessenger) messenger).getFileUrl(peerAvatar.getSmallImage().getFileReference());
        }
        peerKey = jsPeer.getPeerKey();
    } else {
        peerTitle = "New messages";
        peerAvatarUrl = "assets/img/notification_icon_512.png";
    }
    // Notification body
    int nCount = Math.min(topNotifications.size(), 5);
    boolean showCounters = false;
    if (topNotifications.size() > 5) {
        nCount--;
        showCounters = true;
    }
    boolean isChannel = peer.getPeerType() == PeerType.GROUP && messenger.getGroups().get(peer.getPeerId()).getGroupType() == GroupType.CHANNEL;
    if (conversationsCount == 1) {
        for (int i = 0; i < nCount; i++) {
            Notification n = topNotifications.get(i);
            if (contentMessage.length() > 0) {
                contentMessage += "\n";
            }
            if (peer.getPeerType() == PeerType.GROUP) {
                contentMessage += messenger.getUser(notification.getSender()).getName().get() + ": ";
            }
            contentMessage += messenger.getFormatter().formatContentText(n.getSender(), n.getContentDescription().getContentType(), n.getContentDescription().getText(), n.getContentDescription().getRelatedUser(), isChannel);
        }
        if (showCounters) {
            contentMessage += "\n+" + (messagesCount - 4) + " new messages";
        }
    } else {
        for (int i = 0; i < nCount; i++) {
            Notification n = topNotifications.get(i);
            if (contentMessage.length() > 0) {
                contentMessage += "\n";
            }
            String senderName = messenger.getUser(n.getSender()).getName().get();
            if (n.getPeer().getPeerType() == PeerType.GROUP) {
                String groupName = messenger.getGroup(n.getPeer().getPeerId()).getName().get();
                contentMessage += "[" + groupName + "] " + senderName + ": ";
            } else {
                contentMessage += senderName + ": ";
            }
            contentMessage += messenger.getFormatter().formatContentText(n.getSender(), n.getContentDescription().getContentType(), n.getContentDescription().getText(), n.getContentDescription().getRelatedUser(), isChannel);
        }
        if (showCounters) {
            contentMessage += "\n+" + (messagesCount - 4) + " new messages in " + conversationsCount + " conversations";
        }
    }
    if (!JsElectronApp.isElectron()) {
        playSound();
    }
    if (!JsNotification.isSupported()) {
        return;
    }
    if (!JsNotification.isGranted()) {
        return;
    }
    JsManagedNotification.show(peerKey, peerTitle, contentMessage, peerAvatarUrl);
}
Also used : UserVM(im.actor.core.viewmodel.UserVM) GroupVM(im.actor.core.viewmodel.GroupVM) Peer(im.actor.core.entity.Peer) JsPeer(im.actor.core.js.entity.JsPeer) JsPeer(im.actor.core.js.entity.JsPeer) JsNotification(im.actor.core.js.providers.notification.JsNotification) Notification(im.actor.core.entity.Notification) JsManagedNotification(im.actor.core.js.providers.notification.JsManagedNotification) Avatar(im.actor.core.entity.Avatar)

Aggregations

Avatar (im.actor.core.entity.Avatar)1 Notification (im.actor.core.entity.Notification)1 Peer (im.actor.core.entity.Peer)1 JsPeer (im.actor.core.js.entity.JsPeer)1 JsManagedNotification (im.actor.core.js.providers.notification.JsManagedNotification)1 JsNotification (im.actor.core.js.providers.notification.JsNotification)1 GroupVM (im.actor.core.viewmodel.GroupVM)1 UserVM (im.actor.core.viewmodel.UserVM)1