MM-14289 & MM-14884 Push notification acknowledge id and include sender name (#10736)

* MM-14289 Add Push notification acknowledge identifier and store tracing logs

* MM-14884 include SenderName property in Push Notifications

* Remove @ sign from channel Name in push notifications

* Fix i18n

* Fix push notification model

* fix TestPostNotificationGetChannelName

* Remove colon from model constant

* Fix Notification Registry tests

* Make postId optional for clear notifications

* Update http status when service is not available

Co-Authored-By: enahum <nahumhbl@gmail.com>
This commit is contained in:
Elias Nahum
2019-04-30 18:15:29 -04:00
committed by GitHub
parent e50b642e43
commit e6be06b3fc
18 changed files with 677 additions and 54 deletions

View File

@@ -36,6 +36,8 @@ func (api *API) InitSystem() {
api.BaseRoutes.ApiRoot.Handle("/analytics/old", api.ApiSessionRequired(getAnalytics)).Methods("GET")
api.BaseRoutes.ApiRoot.Handle("/redirect_location", api.ApiSessionRequiredTrustRequester(getRedirectLocation)).Methods("GET")
api.BaseRoutes.ApiRoot.Handle("/notifications/ack", api.ApiSessionRequired(pushNotificationAck)).Methods("POST")
}
func getSystemPing(c *Context, w http.ResponseWriter, r *http.Request) {
@@ -317,3 +319,28 @@ func getRedirectLocation(c *Context, w http.ResponseWriter, r *http.Request) {
w.Write([]byte(model.MapToJson(m)))
return
}
func pushNotificationAck(c *Context, w http.ResponseWriter, r *http.Request) {
ack := model.PushNotificationAckFromJson(r.Body)
if !*c.App.Config().EmailSettings.SendPushNotifications {
c.Err = model.NewAppError("pushNotificationAck", "api.push_notification.disabled.app_error", nil, "", http.StatusNotImplemented)
return
}
err := c.App.SendAckToPushProxy(ack)
if err != nil {
c.Err = model.NewAppError("pushNotificationAck", "api.push_notifications_ack.forward.app_error", nil, err.Error(), http.StatusInternalServerError)
return
}
mlog.Debug("Push notification ack received",
mlog.String("ackId", ack.Id),
mlog.String("platform", ack.ClientPlatform),
mlog.String("type", ack.NotificationType),
mlog.Int64("at", ack.ClientReceivedAt),
)
ReturnStatusOK(w)
return
}

View File

@@ -306,6 +306,18 @@ func (a *App) SendNotifications(post *model.Post, team *model.Team, channel *mod
(channelNotification || hereNotification || allNotification),
replyToThreadType,
)
} else {
// register that a notification was not sent
notificationRegistry := model.NotificationRegistry{
UserId: id,
PostId: post.Id,
SendStatus: model.PUSH_NOT_SENT,
Type: model.PUSH_TYPE_MESSAGE,
}
_, appErr := a.Srv.Store.NotificationRegistry().Save(&notificationRegistry)
if appErr != nil {
mlog.Debug(appErr.Error())
}
}
}
@@ -329,6 +341,18 @@ func (a *App) SendNotifications(post *model.Post, team *model.Team, channel *mod
false,
"",
)
} else {
// register that a notification was not sent
notificationRegistry := model.NotificationRegistry{
UserId: id,
PostId: post.Id,
SendStatus: model.PUSH_NOT_SENT,
Type: model.PUSH_TYPE_MESSAGE,
}
_, appErr := a.Srv.Store.NotificationRegistry().Save(&notificationRegistry)
if appErr != nil {
mlog.Debug(appErr.Error())
}
}
}
}
@@ -691,7 +715,7 @@ type postNotification struct {
func (n *postNotification) GetChannelName(userNameFormat string, excludeId string) string {
switch n.channel.Type {
case model.CHANNEL_DIRECT:
return fmt.Sprintf("@%s", n.sender.GetDisplayName(userNameFormat))
return n.sender.GetDisplayName(userNameFormat)
case model.CHANNEL_GROUP:
names := []string{}
for _, user := range n.profileMap {

View File

@@ -5,6 +5,7 @@ package app
import (
"fmt"
"github.com/pkg/errors"
"hash/fnv"
"net/http"
"strings"
@@ -28,6 +29,7 @@ type PushNotificationsHub struct {
}
type PushNotification struct {
id string
notificationType NotificationType
currentSessionId string
userId string
@@ -58,7 +60,17 @@ func (a *App) sendPushNotificationSync(post *model.Post, user *model.User, chann
return err
}
msg := model.PushNotification{}
msg := model.PushNotification{
Category: model.CATEGORY_CAN_REPLY,
Version: model.PUSH_MESSAGE_V2,
Type: model.PUSH_TYPE_MESSAGE,
TeamId: channel.TeamId,
ChannelId: channel.Id,
PostId: post.Id,
RootId: post.RootId,
SenderId: post.UserId,
}
if badge := <-a.Srv.Store.User().GetUnreadCount(user.Id); badge.Err != nil {
msg.Badge = 1
mlog.Error(fmt.Sprint("We could not get the unread message count for the user", user.Id, badge.Err), mlog.String("user_id", user.Id))
@@ -66,22 +78,15 @@ func (a *App) sendPushNotificationSync(post *model.Post, user *model.User, chann
msg.Badge = int(badge.Data.(int64))
}
msg.Category = model.CATEGORY_CAN_REPLY
msg.Version = model.PUSH_MESSAGE_V2
msg.Type = model.PUSH_TYPE_MESSAGE
msg.TeamId = channel.TeamId
msg.ChannelId = channel.Id
msg.PostId = post.Id
msg.RootId = post.RootId
msg.SenderId = post.UserId
contentsConfig := *cfg.EmailSettings.PushNotificationContents
if contentsConfig != model.GENERIC_NO_CHANNEL_NOTIFICATION || channel.Type == model.CHANNEL_DIRECT {
msg.ChannelName = channelName
}
msg.SenderName = senderName
if ou, ok := post.Props["override_username"].(string); ok && *cfg.ServiceSettings.EnablePostUsernameOverride {
msg.OverrideUsername = ou
msg.SenderName = ou
}
if oi, ok := post.Props["override_icon_url"].(string); ok && *cfg.ServiceSettings.EnablePostIconOverride {
@@ -95,7 +100,7 @@ func (a *App) sendPushNotificationSync(post *model.Post, user *model.User, chann
userLocale := utils.GetUserTranslations(user.Locale)
hasFiles := post.FileIds != nil && len(post.FileIds) > 0
msg.Message = a.getPushNotificationMessage(post.Message, explicitMention, channelWideMention, hasFiles, senderName, channelName, channel.Type, replyToThreadType, userLocale)
msg.Message = a.getPushNotificationMessage(post.Message, explicitMention, channelWideMention, hasFiles, msg.SenderName, channelName, channel.Type, replyToThreadType, userLocale)
for _, session := range sessions {
@@ -103,12 +108,38 @@ func (a *App) sendPushNotificationSync(post *model.Post, user *model.User, chann
continue
}
tmpMessage := *model.PushNotificationFromJson(strings.NewReader(msg.ToJson()))
tmpMessage := model.PushNotificationFromJson(strings.NewReader(msg.ToJson()))
tmpMessage.SetDeviceIdAndPlatform(session.DeviceId)
tmpMessage.AckId = model.NewId()
mlog.Debug(fmt.Sprintf("Sending push notification to device %v for user %v with msg of '%v'", tmpMessage.DeviceId, user.Id, msg.Message), mlog.String("user_id", user.Id))
mlog.Debug(
"Sending push notification",
mlog.String("ackId", tmpMessage.AckId),
mlog.String("deviceId", tmpMessage.DeviceId),
mlog.String("userId", user.Id),
)
a.sendToPushProxy(tmpMessage, session)
err := a.sendToPushProxy(*tmpMessage, session)
if err != nil {
mlog.Error(
"Failed to send Push Notification:",
mlog.String("error", err.Error()),
mlog.String("userId", session.UserId),
mlog.String("sessionId", session.Id),
mlog.String("deviceId", msg.DeviceId),
mlog.String("ackId", msg.AckId),
)
appErr := a.Srv.Store.NotificationRegistry().UpdateSendStatus(tmpMessage.AckId, model.PUSH_SEND_ERROR+": "+err.Error())
if appErr != nil {
mlog.Debug(appErr.Error())
}
continue
}
appErr := a.Srv.Store.NotificationRegistry().UpdateSendStatus(tmpMessage.AckId, model.PUSH_SEND_SUCCESS)
if appErr != nil {
mlog.Debug(appErr.Error())
}
if a.Metrics != nil {
a.Metrics.IncrementPostSentPush()
@@ -133,10 +164,6 @@ func (a *App) sendPushNotification(notification *postNotification, user *model.U
channelName := notification.GetChannelName(nameFormat, user.Id)
senderName := notification.GetSenderName(nameFormat, *cfg.ServiceSettings.EnablePostUsernameOverride)
if senderName == notification.sender.Username {
senderName = "@" + senderName
}
c := a.Srv.PushNotificationsHub.GetGoChannelFromUserId(user.Id)
c <- PushNotification{
notificationType: NOTIFICATION_TYPE_MESSAGE,
@@ -201,10 +228,13 @@ func (a *App) ClearPushNotificationSync(currentSessionId, userId, channelId stri
return
}
msg := model.PushNotification{}
msg.Type = model.PUSH_TYPE_CLEAR
msg.ChannelId = channelId
msg.ContentAvailable = 0
msg := model.PushNotification{
Type: model.PUSH_TYPE_CLEAR,
Version: model.PUSH_MESSAGE_V2,
ChannelId: channelId,
ContentAvailable: 1,
}
if badge := <-a.Srv.Store.User().GetUnreadCount(userId); badge.Err != nil {
msg.Badge = 0
mlog.Error(fmt.Sprint("We could not get the unread message count for the user", userId, badge.Err), mlog.String("user_id", userId))
@@ -214,10 +244,43 @@ func (a *App) ClearPushNotificationSync(currentSessionId, userId, channelId stri
for _, session := range sessions {
if currentSessionId != session.Id {
tmpMessage := *model.PushNotificationFromJson(strings.NewReader(msg.ToJson()))
tmpMessage := model.PushNotificationFromJson(strings.NewReader(msg.ToJson()))
tmpMessage.SetDeviceIdAndPlatform(session.DeviceId)
mlog.Debug(fmt.Sprintf("Clearing push notification to %v with channel_id %v", session.DeviceId, msg.ChannelId))
a.sendToPushProxy(tmpMessage, session)
tmpMessage.AckId = model.NewId()
mlog.Debug(
"Sending clear push notification",
mlog.String("ackId", tmpMessage.AckId),
mlog.String("deviceId", tmpMessage.DeviceId),
mlog.String("userId", session.UserId),
mlog.String("channelId", channelId), //should we remove the message from the logs?
)
err := a.sendToPushProxy(*tmpMessage, session)
if err != nil {
mlog.Error(
"Failed to send Push Notification:",
mlog.String("error", err.Error()),
mlog.String("userId", session.UserId),
mlog.String("sessionId", session.Id),
mlog.String("deviceId", msg.DeviceId),
mlog.String("ackId", msg.AckId),
)
appErr := a.Srv.Store.NotificationRegistry().UpdateSendStatus(tmpMessage.AckId, model.PUSH_SEND_ERROR+": "+err.Error())
if appErr != nil {
mlog.Debug(appErr.Error())
}
continue
}
appErr := a.Srv.Store.NotificationRegistry().UpdateSendStatus(tmpMessage.AckId, model.PUSH_SEND_SUCCESS)
if appErr != nil {
mlog.Debug(appErr.Error())
}
if a.Metrics != nil {
a.Metrics.IncrementPostSentPush()
}
}
}
}
@@ -277,20 +340,30 @@ func (a *App) StopPushNotificationsHubWorkers() {
}
}
func (a *App) sendToPushProxy(msg model.PushNotification, session *model.Session) {
func (a *App) sendToPushProxy(msg model.PushNotification, session *model.Session) error {
msg.ServerId = a.DiagnosticId()
notificationRegistry := model.NotificationRegistry{
AckId: msg.AckId,
DeviceId: msg.DeviceId,
UserId: session.UserId,
PostId: msg.PostId,
Type: msg.Type,
}
_, appErr := a.Srv.Store.NotificationRegistry().Save(&notificationRegistry)
if appErr != nil {
return appErr
}
request, err := http.NewRequest("POST", strings.TrimRight(*a.Config().EmailSettings.PushNotificationServer, "/")+model.API_URL_SUFFIX_V1+"/send_push", strings.NewReader(msg.ToJson()))
if err != nil {
mlog.Error(fmt.Sprintf("Error sending to push proxy: UserId=%v SessionId=%v message=%v",
session.UserId, session.Id, err.Error()), mlog.String("user_id", session.UserId))
return
return err
}
resp, err := a.HTTPService.MakeClient(true).Do(request)
if err != nil {
mlog.Error(fmt.Sprintf("Device push reported as error for UserId=%v SessionId=%v message=%v", session.UserId, session.Id, err.Error()), mlog.String("user_id", session.UserId))
return
return err
}
defer resp.Body.Close()
@@ -298,14 +371,46 @@ func (a *App) sendToPushProxy(msg model.PushNotification, session *model.Session
pushResponse := model.PushResponseFromJson(resp.Body)
if pushResponse[model.PUSH_STATUS] == model.PUSH_STATUS_REMOVE {
mlog.Info(fmt.Sprintf("Device was reported as removed for UserId=%v SessionId=%v removing push for this session", session.UserId, session.Id), mlog.String("user_id", session.UserId))
a.AttachDeviceId(session.Id, "", session.ExpiresAt)
a.ClearSessionCacheForUser(session.UserId)
return errors.New("Device was reported as removed")
}
if pushResponse[model.PUSH_STATUS] == model.PUSH_STATUS_FAIL {
mlog.Error(fmt.Sprintf("Device push reported as error for UserId=%v SessionId=%v message=%v", session.UserId, session.Id, pushResponse[model.PUSH_STATUS_ERROR_MSG]), mlog.String("user_id", session.UserId))
return errors.New(pushResponse[model.PUSH_STATUS_ERROR_MSG])
}
return nil
}
func (a *App) SendAckToPushProxy(ack *model.PushNotificationAck) error {
if ack == nil {
return nil
}
appErr := a.Srv.Store.NotificationRegistry().MarkAsReceived(ack.Id, ack.ClientReceivedAt)
if appErr != nil {
return appErr
}
request, err := http.NewRequest(
"POST",
strings.TrimRight(*a.Config().EmailSettings.PushNotificationServer, "/")+model.API_URL_SUFFIX_V1+"/ack",
strings.NewReader(ack.ToJson()),
)
if err != nil {
return err
}
resp, err := a.HTTPService.MakeClient(true).Do(request)
if err != nil {
return err
}
resp.Body.Close()
return nil
}
func (a *App) getMobileAppSessions(userId string) ([]*model.Session, *model.AppError) {

View File

@@ -997,22 +997,22 @@ func TestPostNotificationGetChannelName(t *testing.T) {
},
"direct channel, unspecified": {
channel: &model.Channel{Type: model.CHANNEL_DIRECT},
expected: "@sender",
expected: "sender",
},
"direct channel, username": {
channel: &model.Channel{Type: model.CHANNEL_DIRECT},
nameFormat: model.SHOW_USERNAME,
expected: "@sender",
expected: "sender",
},
"direct channel, full name": {
channel: &model.Channel{Type: model.CHANNEL_DIRECT},
nameFormat: model.SHOW_FULLNAME,
expected: "@Sender Sender",
expected: "Sender Sender",
},
"direct channel, nickname": {
channel: &model.Channel{Type: model.CHANNEL_DIRECT},
nameFormat: model.SHOW_NICKNAME_FULLNAME,
expected: "@Sender",
expected: "Sender",
},
"group channel, unspecified": {
channel: &model.Channel{Type: model.CHANNEL_GROUP},

View File

@@ -1614,6 +1614,14 @@
"id": "api.preference.update_preferences.set.app_error",
"translation": "Unable to set user preferences."
},
{
"id": "api.push_notification.disabled.app_error",
"translation": "Push Notifications are disabled on this server."
},
{
"id": "api.push_notifications_ack.forward.app_error",
"translation": "An error occurred sending the receipt delivery to the push notification service"
},
{
"id": "api.reaction.delete.archived_channel.app_error",
"translation": "You cannot remove a reaction in an archived channel."
@@ -4774,6 +4782,34 @@
"id": "model.link_metadata.is_valid.url.app_error",
"translation": "Link metadata URL must be set"
},
{
"id": "model.notification_registry.ack_id.app_error",
"translation": "Invalid Acknowledge Id"
},
{
"id": "model.notification_registry.create_at.app_error",
"translation": "Create at must be a valid time"
},
{
"id": "model.notification_registry.device_id.app_error",
"translation": "Invalid Device Id"
},
{
"id": "model.notification_registry.post_id.app_error",
"translation": "Invalid Post Id"
},
{
"id": "model.notification_registry.status.app_error",
"translation": "Invalid Send Status"
},
{
"id": "model.notification_registry.type.app_error",
"translation": "Invalid push type, must be \"clear\" or \"message\""
},
{
"id": "model.notification_registry.user_id.app_error",
"translation": "Invalid User Id"
},
{
"id": "model.oauth.is_valid.app_id.app_error",
"translation": "Invalid app id"
@@ -5842,6 +5878,18 @@
"id": "store.sql_link_metadata.save.app_error",
"translation": "Unable to save the link metadata"
},
{
"id": "store.sql_notification.mark_as_received.app_error",
"translation": "An error occurred marking the notification as received"
},
{
"id": "store.sql_notification.save.app_error",
"translation": "An error occurred while saving the notification registry"
},
{
"id": "store.sql_notification.update_status.app_error",
"translation": "An error occurred while updating the notification status"
},
{
"id": "store.sql_oauth.delete.commit_transaction.app_error",
"translation": "Unable to commit transaction"

View File

@@ -6,6 +6,7 @@ package model
import (
"encoding/json"
"io"
"net/http"
"strings"
)
@@ -24,9 +25,32 @@ const (
CATEGORY_CAN_REPLY = "CAN_REPLY"
MHPNS = "https://push.mattermost.com"
PUSH_SEND_SUCCESS = "Successful"
PUSH_SEND_ERROR = "Error"
PUSH_NOT_SENT = "Not Sent due to preferences"
)
type NotificationRegistry struct {
AckId string
CreateAt int64
UserId string
DeviceId string
PostId string
SendStatus string
Type string
ReceiveAt int64
}
type PushNotificationAck struct {
Id string `json:"id"`
ClientReceivedAt int64 `json:"received_at"`
ClientPlatform string `json:"platform"`
NotificationType string `json:"type"`
}
type PushNotification struct {
AckId string `json:"ack_id"`
Platform string `json:"platform"`
ServerId string `json:"server_id"`
DeviceId string `json:"device_id"`
@@ -42,6 +66,7 @@ type PushNotification struct {
ChannelName string `json:"channel_name"`
Type string `json:"type"`
SenderId string `json:"sender_id"`
SenderName string `json:"sender_name"`
OverrideUsername string `json:"override_username"`
OverrideIconUrl string `json:"override_icon_url"`
FromWebhook string `json:"from_webhook"`
@@ -68,3 +93,55 @@ func PushNotificationFromJson(data io.Reader) *PushNotification {
json.NewDecoder(data).Decode(&me)
return me
}
func PushNotificationAckFromJson(data io.Reader) *PushNotificationAck {
var ack *PushNotificationAck
json.NewDecoder(data).Decode(&ack)
return ack
}
func (ack *PushNotificationAck) ToJson() string {
b, _ := json.Marshal(ack)
return string(b)
}
func (o *NotificationRegistry) IsValid() *AppError {
if len(o.AckId) != 26 {
return NewAppError("NotificationRegistry.IsValid", "model.notification_registry.ack_id.app_error", nil, "", http.StatusBadRequest)
}
if o.CreateAt == 0 {
return NewAppError("NotificationRegistry.IsValid", "model.notification_registry.create_at.app_error", nil, "AckId="+o.AckId, http.StatusBadRequest)
}
if len(o.UserId) != 26 {
return NewAppError("NotificationRegistry.IsValid", "model.notification_registry.user_id.app_error", nil, "", http.StatusBadRequest)
}
if len(o.PostId) > 0 && len(o.PostId) != 26 {
return NewAppError("NotificationRegistry.IsValid", "model.notification_registry.post_id.app_error", nil, "", http.StatusBadRequest)
}
if len(o.DeviceId) > 512 {
return NewAppError("NotificationRegistry.IsValid", "model.notification_registry.device_id.app_error", nil, "", http.StatusBadRequest)
}
if len(o.SendStatus) > 4096 {
return NewAppError("NotificationRegistry.IsValid", "model.notification_registry.status.app_error", nil, "", http.StatusBadRequest)
}
if o.Type != PUSH_TYPE_CLEAR && o.Type != PUSH_TYPE_MESSAGE {
return NewAppError("NotificationRegistry.IsValid", "model.notification_registry.type.app_error", nil, "", http.StatusBadRequest)
}
return nil
}
func (o *NotificationRegistry) PreSave() {
if o.AckId == "" {
o.AckId = NewId()
}
o.CreateAt = GetMillis()
}

View File

@@ -195,6 +195,10 @@ func (s *LayeredStore) LinkMetadata() LinkMetadataStore {
return s.DatabaseLayer.LinkMetadata()
}
func (s *LayeredStore) NotificationRegistry() NotificationRegistryStore {
return s.DatabaseLayer.NotificationRegistry()
}
func (s *LayeredStore) MarkSystemRanUnitTests() {
s.DatabaseLayer.MarkSystemRanUnitTests()
}

View File

@@ -0,0 +1,81 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package sqlstore
import (
"net/http"
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/store"
)
type SqlNotificationRegistryStore struct {
SqlStore
}
func NewSqlNotificationRegistryStore(sqlStore SqlStore) store.NotificationRegistryStore {
s := &SqlNotificationRegistryStore{sqlStore}
for _, db := range sqlStore.GetAllConns() {
table := db.AddTableWithName(model.NotificationRegistry{}, "NotificationRegistry").SetKeys(false, "AckId")
table.ColMap("AckId").SetMaxSize(26)
table.ColMap("UserId").SetMaxSize(26)
table.ColMap("PostId").SetMaxSize(26)
table.ColMap("DeviceId").SetMaxSize(512)
table.ColMap("Type").SetMaxSize(26)
table.ColMap("SendStatus").SetMaxSize(4096)
}
return s
}
func (s *SqlNotificationRegistryStore) CreateIndexesIfNotExists() {
s.CreateIndexIfNotExists("idx_notification_ack_id", "NotificationRegistry", "AckId")
s.CreateIndexIfNotExists("idx_notification_create_at", "NotificationRegistry", "CreateAt")
s.CreateIndexIfNotExists("idx_notification_receive_at", "NotificationRegistry", "ReceiveAt")
s.CreateIndexIfNotExists("idx_notification_post_id", "NotificationRegistry", "PostId")
s.CreateIndexIfNotExists("idx_notification_user_id", "NotificationRegistry", "UserId")
s.CreateIndexIfNotExists("idx_notification_type", "NotificationRegistry", "Type")
}
func (s *SqlNotificationRegistryStore) Save(notification *model.NotificationRegistry) (*model.NotificationRegistry, *model.AppError) {
notification.PreSave()
appErr := notification.IsValid()
if appErr != nil {
return nil, appErr
}
err := s.GetMaster().Insert(notification)
if err != nil {
appErr = model.NewAppError("SqlNotificationRegistryStore.Save", "store.sql_notification.save.app_error", nil, "id="+notification.AckId+", "+err.Error(), http.StatusInternalServerError)
return nil, appErr
}
return notification, nil
}
func (s *SqlNotificationRegistryStore) MarkAsReceived(ackId string, time int64) *model.AppError {
result, err := s.GetMaster().Exec("UPDATE NotificationRegistry SET ReceiveAt = :ReceiveAt WHERE AckId = :AckId AND ReceiveAt = 0", map[string]interface{}{"AckId": ackId, "ReceiveAt": time})
if err != nil {
return model.NewAppError("SqlNotificationRegistryStore.Save", "store.sql_notification.mark_as_received.app_error", nil, "id="+ackId+", "+err.Error(), http.StatusInternalServerError)
}
affected, err := result.RowsAffected()
if err != nil {
return model.NewAppError("SqlNotificationRegistryStore.Save", "store.sql_notification.mark_as_received.app_error", nil, "id="+ackId+", "+err.Error(), http.StatusInternalServerError)
} else if affected != 1 {
return model.NewAppError("SqlNotificationRegistryStore.Save", "store.sql_notification.mark_as_received.app_error", nil, "id="+ackId+", Message already received", http.StatusInternalServerError)
}
return nil
}
func (s *SqlNotificationRegistryStore) UpdateSendStatus(ackId, status string) *model.AppError {
_, err := s.GetMaster().Exec("UPDATE NotificationRegistry SET SendStatus = :Status WHERE AckId = :AckId", map[string]interface{}{"AckId": ackId, "Status": status})
if err != nil {
return model.NewAppError("SqlNotificationRegistryStore.Save", "store.sql_notification.update_status.app_error", nil, "id="+ackId+", "+err.Error(), http.StatusInternalServerError)
}
return nil
}

View File

@@ -0,0 +1,14 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package sqlstore
import (
"testing"
"github.com/mattermost/mattermost-server/store/storetest"
)
func TestNotificationRegistryStore(t *testing.T) {
StoreTest(t, storetest.TestNotificationRegistryStore)
}

View File

@@ -99,4 +99,5 @@ type SqlStore interface {
UserTermsOfService() store.UserTermsOfServiceStore
LinkMetadata() store.LinkMetadataStore
getQueryBuilder() sq.StatementBuilderType
NotificationRegistry() store.NotificationRegistryStore
}

View File

@@ -99,6 +99,7 @@ type SqlSupplierOldStores struct {
group store.GroupStore
UserTermsOfService store.UserTermsOfServiceStore
linkMetadata store.LinkMetadataStore
notificationRegistry store.NotificationRegistryStore
}
type SqlSupplier struct {
@@ -151,6 +152,7 @@ func NewSqlSupplier(settings model.SqlSettings, metrics einterfaces.MetricsInter
supplier.oldStores.TermsOfService = NewSqlTermsOfServiceStore(supplier, metrics)
supplier.oldStores.UserTermsOfService = NewSqlUserTermsOfServiceStore(supplier)
supplier.oldStores.linkMetadata = NewSqlLinkMetadataStore(supplier)
supplier.oldStores.notificationRegistry = NewSqlNotificationRegistryStore(supplier)
initSqlSupplierReactions(supplier)
initSqlSupplierRoles(supplier)
@@ -1053,6 +1055,10 @@ func (ss *SqlSupplier) LinkMetadata() store.LinkMetadataStore {
return ss.oldStores.linkMetadata
}
func (ss *SqlSupplier) NotificationRegistry() store.NotificationRegistryStore {
return ss.oldStores.notificationRegistry
}
func (ss *SqlSupplier) DropAllTables() {
ss.master.TruncateTables()
}

View File

@@ -78,6 +78,7 @@ type Store interface {
TotalMasterDbConnections() int
TotalReadDbConnections() int
TotalSearchDbConnections() int
NotificationRegistry() NotificationRegistryStore
}
type TeamStore interface {
@@ -602,3 +603,9 @@ type LinkMetadataStore interface {
Save(linkMetadata *model.LinkMetadata) StoreChannel
Get(url string, timestamp int64) StoreChannel
}
type NotificationRegistryStore interface {
Save(notification *model.NotificationRegistry) (*model.NotificationRegistry, *model.AppError)
MarkAsReceived(ackId string, time int64) *model.AppError
UpdateSendStatus(ackId, status string) *model.AppError
}

View File

@@ -734,6 +734,22 @@ func (_m *LayeredStoreDatabaseLayer) Next() store.LayeredStoreSupplier {
return r0
}
// NotificationRegistry provides a mock function with given fields:
func (_m *LayeredStoreDatabaseLayer) NotificationRegistry() store.NotificationRegistryStore {
ret := _m.Called()
var r0 store.NotificationRegistryStore
if rf, ok := ret.Get(0).(func() store.NotificationRegistryStore); ok {
r0 = rf()
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(store.NotificationRegistryStore)
}
}
return r0
}
// OAuth provides a mock function with given fields:
func (_m *LayeredStoreDatabaseLayer) OAuth() store.OAuthStore {
ret := _m.Called()

View File

@@ -0,0 +1,70 @@
// Code generated by mockery v1.0.0. DO NOT EDIT.
// Regenerate this file using `make store-mocks`.
package mocks
import mock "github.com/stretchr/testify/mock"
import model "github.com/mattermost/mattermost-server/model"
// NotificationRegistryStore is an autogenerated mock type for the NotificationRegistryStore type
type NotificationRegistryStore struct {
mock.Mock
}
// MarkAsReceived provides a mock function with given fields: ackId, time
func (_m *NotificationRegistryStore) MarkAsReceived(ackId string, time int64) *model.AppError {
ret := _m.Called(ackId, time)
var r0 *model.AppError
if rf, ok := ret.Get(0).(func(string, int64) *model.AppError); ok {
r0 = rf(ackId, time)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*model.AppError)
}
}
return r0
}
// Save provides a mock function with given fields: notification
func (_m *NotificationRegistryStore) Save(notification *model.NotificationRegistry) (*model.NotificationRegistry, *model.AppError) {
ret := _m.Called(notification)
var r0 *model.NotificationRegistry
if rf, ok := ret.Get(0).(func(*model.NotificationRegistry) *model.NotificationRegistry); ok {
r0 = rf(notification)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*model.NotificationRegistry)
}
}
var r1 *model.AppError
if rf, ok := ret.Get(1).(func(*model.NotificationRegistry) *model.AppError); ok {
r1 = rf(notification)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
}
}
return r0, r1
}
// UpdateSendStatus provides a mock function with given fields: ackId, status
func (_m *NotificationRegistryStore) UpdateSendStatus(ackId string, status string) *model.AppError {
ret := _m.Called(ackId, status)
var r0 *model.AppError
if rf, ok := ret.Get(0).(func(string, string) *model.AppError); ok {
r0 = rf(ackId, status)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*model.AppError)
}
}
return r0
}

View File

@@ -482,6 +482,22 @@ func (_m *SqlStore) MarkSystemRanUnitTests() {
_m.Called()
}
// NotificationRegistry provides a mock function with given fields:
func (_m *SqlStore) NotificationRegistry() store.NotificationRegistryStore {
ret := _m.Called()
var r0 store.NotificationRegistryStore
if rf, ok := ret.Get(0).(func() store.NotificationRegistryStore); ok {
r0 = rf()
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(store.NotificationRegistryStore)
}
}
return r0
}
// OAuth provides a mock function with given fields:
func (_m *SqlStore) OAuth() store.OAuthStore {
ret := _m.Called()

View File

@@ -256,6 +256,22 @@ func (_m *Store) MarkSystemRanUnitTests() {
_m.Called()
}
// NotificationRegistry provides a mock function with given fields:
func (_m *Store) NotificationRegistry() store.NotificationRegistryStore {
ret := _m.Called()
var r0 store.NotificationRegistryStore
if rf, ok := ret.Get(0).(func() store.NotificationRegistryStore); ok {
r0 = rf()
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(store.NotificationRegistryStore)
}
}
return r0
}
// OAuth provides a mock function with given fields:
func (_m *Store) OAuth() store.OAuthStore {
ret := _m.Called()

View File

@@ -0,0 +1,106 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package storetest
import (
"testing"
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/store"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNotificationRegistryStore(t *testing.T, ss store.Store) {
t.Run("Save", func(t *testing.T) { testNotificationRegistryStoreSave(t, ss) })
t.Run("MarkAsReceived", func(t *testing.T) { testNotificationRegistryStoreMarkAsReceived(t, ss) })
t.Run("UpdateSendStatus", func(t *testing.T) { testNotificationRegistryStoreUpdateSendStatus(t, ss) })
}
func testNotificationRegistryStoreSave(t *testing.T, ss store.Store) {
t.Run("should save notification registry", func(t *testing.T) {
notificationRegistry := &model.NotificationRegistry{
AckId: model.NewId(),
DeviceId: "apple_or_android_device_identifier",
PostId: model.NewId(),
UserId: model.NewId(),
Type: model.PUSH_TYPE_MESSAGE,
}
result, err := ss.NotificationRegistry().Save(notificationRegistry)
require.Nil(t, err)
require.IsType(t, notificationRegistry, result)
assert.NotZero(t, result.CreateAt)
})
t.Run("should save notification registry without AckId", func(t *testing.T) {
notificationRegistry := &model.NotificationRegistry{
DeviceId: "apple_or_android_device_identifier",
PostId: model.NewId(),
UserId: model.NewId(),
Type: model.PUSH_TYPE_MESSAGE,
}
result, err := ss.NotificationRegistry().Save(notificationRegistry)
require.Nil(t, err)
require.IsType(t, notificationRegistry, result)
assert.NotEmpty(t, result.AckId)
assert.NotZero(t, result.CreateAt)
})
t.Run("should fail to save invalid notification registry", func(t *testing.T) {
notificationRegistry := &model.NotificationRegistry{
DeviceId: "apple_or_android_device_identifier",
PostId: model.NewId(),
UserId: model.NewId(),
Type: "garbage",
}
_, err := ss.NotificationRegistry().Save(notificationRegistry)
assert.NotNil(t, err)
})
}
func testNotificationRegistryStoreMarkAsReceived(t *testing.T, ss store.Store) {
notificationRegistry := &model.NotificationRegistry{
DeviceId: "apple_or_android_device_identifier",
PostId: model.NewId(),
UserId: model.NewId(),
Type: model.PUSH_TYPE_MESSAGE,
}
t.Run("should update when the notification was received", func(t *testing.T) {
result, err := ss.NotificationRegistry().Save(notificationRegistry)
if err != nil {
t.Fatal("Notification should have been saved")
}
err = ss.NotificationRegistry().MarkAsReceived(result.AckId, model.GetMillis())
assert.Nil(t, err)
})
}
func testNotificationRegistryStoreUpdateSendStatus(t *testing.T, ss store.Store) {
notificationRegistry := &model.NotificationRegistry{
DeviceId: "apple_or_android_device_identifier",
UserId: model.NewId(),
Type: model.PUSH_TYPE_CLEAR,
}
t.Run("should update when the notification send status", func(t *testing.T) {
result, err := ss.NotificationRegistry().Save(notificationRegistry)
if err != nil {
t.Fatal("Notification should have been saved")
}
err = ss.NotificationRegistry().UpdateSendStatus(result.AckId, "Some Status")
assert.Nil(t, err)
})
}

View File

@@ -50,24 +50,28 @@ type Store struct {
GroupStore mocks.GroupStore
UserTermsOfServiceStore mocks.UserTermsOfServiceStore
LinkMetadataStore mocks.LinkMetadataStore
NotificationRegistryStore mocks.NotificationRegistryStore
}
func (s *Store) Team() store.TeamStore { return &s.TeamStore }
func (s *Store) Channel() store.ChannelStore { return &s.ChannelStore }
func (s *Store) Post() store.PostStore { return &s.PostStore }
func (s *Store) User() store.UserStore { return &s.UserStore }
func (s *Store) Bot() store.BotStore { return &s.BotStore }
func (s *Store) Audit() store.AuditStore { return &s.AuditStore }
func (s *Store) ClusterDiscovery() store.ClusterDiscoveryStore { return &s.ClusterDiscoveryStore }
func (s *Store) Compliance() store.ComplianceStore { return &s.ComplianceStore }
func (s *Store) Session() store.SessionStore { return &s.SessionStore }
func (s *Store) OAuth() store.OAuthStore { return &s.OAuthStore }
func (s *Store) System() store.SystemStore { return &s.SystemStore }
func (s *Store) Webhook() store.WebhookStore { return &s.WebhookStore }
func (s *Store) Command() store.CommandStore { return &s.CommandStore }
func (s *Store) CommandWebhook() store.CommandWebhookStore { return &s.CommandWebhookStore }
func (s *Store) Preference() store.PreferenceStore { return &s.PreferenceStore }
func (s *Store) License() store.LicenseStore { return &s.LicenseStore }
func (s *Store) Team() store.TeamStore { return &s.TeamStore }
func (s *Store) Channel() store.ChannelStore { return &s.ChannelStore }
func (s *Store) Post() store.PostStore { return &s.PostStore }
func (s *Store) User() store.UserStore { return &s.UserStore }
func (s *Store) Bot() store.BotStore { return &s.BotStore }
func (s *Store) Audit() store.AuditStore { return &s.AuditStore }
func (s *Store) ClusterDiscovery() store.ClusterDiscoveryStore { return &s.ClusterDiscoveryStore }
func (s *Store) Compliance() store.ComplianceStore { return &s.ComplianceStore }
func (s *Store) Session() store.SessionStore { return &s.SessionStore }
func (s *Store) OAuth() store.OAuthStore { return &s.OAuthStore }
func (s *Store) System() store.SystemStore { return &s.SystemStore }
func (s *Store) Webhook() store.WebhookStore { return &s.WebhookStore }
func (s *Store) Command() store.CommandStore { return &s.CommandStore }
func (s *Store) CommandWebhook() store.CommandWebhookStore { return &s.CommandWebhookStore }
func (s *Store) Preference() store.PreferenceStore { return &s.PreferenceStore }
func (s *Store) License() store.LicenseStore { return &s.LicenseStore }
func (s *Store) NotificationRegistry() store.NotificationRegistryStore {
return &s.NotificationRegistryStore
}
func (s *Store) Token() store.TokenStore { return &s.TokenStore }
func (s *Store) Emoji() store.EmojiStore { return &s.EmojiStore }
func (s *Store) Status() store.StatusStore { return &s.StatusStore }
@@ -112,6 +116,7 @@ func (s *Store) AssertExpectations(t mock.TestingT) bool {
&s.CommandWebhookStore,
&s.PreferenceStore,
&s.LicenseStore,
&s.NotificationRegistryStore,
&s.TokenStore,
&s.EmojiStore,
&s.StatusStore,