github.com/adacta-ru/mattermost-server/v6@v6.0.0/app/expirynotify.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package app
     5  
     6  import (
     7  	"net/http"
     8  
     9  	"github.com/adacta-ru/mattermost-server/v6/mlog"
    10  	"github.com/adacta-ru/mattermost-server/v6/model"
    11  	"github.com/adacta-ru/mattermost-server/v6/utils"
    12  )
    13  
    14  const (
    15  	OneHourMillis = 60 * 60 * 1000
    16  )
    17  
    18  // NotifySessionsExpired is called periodically from the job server to notify any mobile sessions that have expired.
    19  func (a *App) NotifySessionsExpired() *model.AppError {
    20  	if *a.Config().EmailSettings.SendPushNotifications {
    21  		pushServer := *a.Config().EmailSettings.PushNotificationServer
    22  		if license := a.srv.License(); pushServer == model.MHPNS && (license == nil || !*license.Features.MHPNS) {
    23  			mlog.Warn("Push notifications are disabled. Go to System Console > Notifications > Mobile Push to enable them.")
    24  			return nil
    25  		}
    26  	}
    27  
    28  	// Get all mobile sessions that expired within the last hour.
    29  	sessions, err := a.srv.Store.Session().GetSessionsExpired(OneHourMillis, true, true)
    30  	if err != nil {
    31  		return model.NewAppError("NotifySessionsExpired", "app.session.analytics_session_count.app_error", nil, err.Error(), http.StatusInternalServerError)
    32  	}
    33  
    34  	msg := &model.PushNotification{
    35  		Version: model.PUSH_MESSAGE_V2,
    36  		Type:    model.PUSH_TYPE_SESSION,
    37  	}
    38  
    39  	for _, session := range sessions {
    40  		tmpMessage := msg.DeepCopy()
    41  		tmpMessage.SetDeviceIdAndPlatform(session.DeviceId)
    42  		tmpMessage.AckId = model.NewId()
    43  		tmpMessage.Message = a.getSessionExpiredPushMessage(session)
    44  
    45  		errPush := a.sendToPushProxy(tmpMessage, session)
    46  		if errPush != nil {
    47  			a.NotificationsLog().Error("Notification error",
    48  				mlog.String("ackId", tmpMessage.AckId),
    49  				mlog.String("type", tmpMessage.Type),
    50  				mlog.String("userId", session.UserId),
    51  				mlog.String("deviceId", tmpMessage.DeviceId),
    52  				mlog.String("status", errPush.Error()),
    53  			)
    54  			continue
    55  		}
    56  
    57  		a.NotificationsLog().Info("Notification sent",
    58  			mlog.String("ackId", tmpMessage.AckId),
    59  			mlog.String("type", tmpMessage.Type),
    60  			mlog.String("userId", session.UserId),
    61  			mlog.String("deviceId", tmpMessage.DeviceId),
    62  			mlog.String("status", model.PUSH_SEND_SUCCESS),
    63  		)
    64  
    65  		if a.Metrics() != nil {
    66  			a.Metrics().IncrementPostSentPush()
    67  		}
    68  
    69  		err = a.srv.Store.Session().UpdateExpiredNotify(session.Id, true)
    70  		if err != nil {
    71  			mlog.Error("Failed to update ExpiredNotify flag", mlog.String("sessionid", session.Id), mlog.Err(err))
    72  		}
    73  	}
    74  	return nil
    75  }
    76  
    77  func (a *App) getSessionExpiredPushMessage(session *model.Session) string {
    78  	locale := model.DEFAULT_LOCALE
    79  	user, err := a.GetUser(session.UserId)
    80  	if err == nil {
    81  		locale = user.Locale
    82  	}
    83  	T := utils.GetUserTranslations(locale)
    84  
    85  	siteName := *a.Config().TeamSettings.SiteName
    86  	props := map[string]interface{}{"siteName": siteName, "daysCount": *a.Config().ServiceSettings.SessionLengthMobileInDays}
    87  
    88  	return T("api.push_notifications.session.expired", props)
    89  }