github.com/mattermosttest/mattermost-server/v5@v5.0.0-20200917143240-9dfa12e121f9/model/push_notification.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package model
     5  
     6  import (
     7  	"encoding/json"
     8  	"errors"
     9  	"io"
    10  	"strings"
    11  )
    12  
    13  const (
    14  	PUSH_NOTIFY_APPLE                = "apple"
    15  	PUSH_NOTIFY_ANDROID              = "android"
    16  	PUSH_NOTIFY_APPLE_REACT_NATIVE   = "apple_rn"
    17  	PUSH_NOTIFY_ANDROID_REACT_NATIVE = "android_rn"
    18  
    19  	PUSH_TYPE_MESSAGE      = "message"
    20  	PUSH_TYPE_CLEAR        = "clear"
    21  	PUSH_TYPE_UPDATE_BADGE = "update_badge"
    22  	PUSH_TYPE_SESSION      = "session"
    23  	PUSH_MESSAGE_V2        = "v2"
    24  
    25  	PUSH_SOUND_NONE = "none"
    26  
    27  	// The category is set to handle a set of interactive Actions
    28  	// with the push notifications
    29  	CATEGORY_CAN_REPLY = "CAN_REPLY"
    30  
    31  	MHPNS = "https://push.mattermost.com"
    32  
    33  	PUSH_SEND_PREPARE = "Prepared to send"
    34  	PUSH_SEND_SUCCESS = "Successful"
    35  	PUSH_NOT_SENT     = "Not Sent due to preferences"
    36  	PUSH_RECEIVED     = "Received by device"
    37  )
    38  
    39  type PushNotificationAck struct {
    40  	Id               string `json:"id"`
    41  	ClientReceivedAt int64  `json:"received_at"`
    42  	ClientPlatform   string `json:"platform"`
    43  	NotificationType string `json:"type"`
    44  	PostId           string `json:"post_id,omitempty"`
    45  	IsIdLoaded       bool   `json:"is_id_loaded"`
    46  }
    47  
    48  type PushNotification struct {
    49  	AckId            string `json:"ack_id"`
    50  	Platform         string `json:"platform"`
    51  	ServerId         string `json:"server_id"`
    52  	DeviceId         string `json:"device_id"`
    53  	PostId           string `json:"post_id"`
    54  	Category         string `json:"category,omitempty"`
    55  	Sound            string `json:"sound,omitempty"`
    56  	Message          string `json:"message,omitempty"`
    57  	Badge            int    `json:"badge,omitempty"`
    58  	ContentAvailable int    `json:"cont_ava,omitempty"`
    59  	TeamId           string `json:"team_id,omitempty"`
    60  	ChannelId        string `json:"channel_id,omitempty"`
    61  	RootId           string `json:"root_id,omitempty"`
    62  	ChannelName      string `json:"channel_name,omitempty"`
    63  	Type             string `json:"type,omitempty"`
    64  	SenderId         string `json:"sender_id,omitempty"`
    65  	SenderName       string `json:"sender_name,omitempty"`
    66  	OverrideUsername string `json:"override_username,omitempty"`
    67  	OverrideIconUrl  string `json:"override_icon_url,omitempty"`
    68  	FromWebhook      string `json:"from_webhook,omitempty"`
    69  	Version          string `json:"version,omitempty"`
    70  	IsIdLoaded       bool   `json:"is_id_loaded"`
    71  }
    72  
    73  func (me *PushNotification) ToJson() string {
    74  	b, _ := json.Marshal(me)
    75  	return string(b)
    76  }
    77  
    78  func (me *PushNotification) DeepCopy() *PushNotification {
    79  	copy := *me
    80  	return &copy
    81  }
    82  
    83  func (me *PushNotification) SetDeviceIdAndPlatform(deviceId string) {
    84  
    85  	index := strings.Index(deviceId, ":")
    86  
    87  	if index > -1 {
    88  		me.Platform = deviceId[:index]
    89  		me.DeviceId = deviceId[index+1:]
    90  	}
    91  }
    92  
    93  func PushNotificationFromJson(data io.Reader) (*PushNotification, error) {
    94  	if data == nil {
    95  		return nil, errors.New("push notification data can't be nil")
    96  	}
    97  	var me *PushNotification
    98  	if err := json.NewDecoder(data).Decode(&me); err != nil {
    99  		return nil, err
   100  	}
   101  	return me, nil
   102  }
   103  
   104  func PushNotificationAckFromJson(data io.Reader) (*PushNotificationAck, error) {
   105  	if data == nil {
   106  		return nil, errors.New("push notification data can't be nil")
   107  	}
   108  	var ack *PushNotificationAck
   109  	if err := json.NewDecoder(data).Decode(&ack); err != nil {
   110  		return nil, err
   111  	}
   112  	return ack, nil
   113  }
   114  
   115  func (ack *PushNotificationAck) ToJson() string {
   116  	b, _ := json.Marshal(ack)
   117  	return string(b)
   118  }