2021-04-07 12:57:14 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
2021-12-02 14:23:02 +00:00
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
2022-02-09 13:15:57 +00:00
|
|
|
"github.com/pkg/errors"
|
2022-05-30 15:34:48 +00:00
|
|
|
"go.uber.org/zap"
|
2022-02-09 13:15:57 +00:00
|
|
|
|
2021-04-07 12:57:14 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2021-12-02 14:23:02 +00:00
|
|
|
"github.com/status-im/status-go/protocol/common"
|
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
2021-04-07 12:57:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (m *Messenger) UnreadActivityCenterNotificationsCount() (uint64, error) {
|
|
|
|
return m.persistence.UnreadActivityCenterNotificationsCount()
|
|
|
|
}
|
|
|
|
|
2021-12-02 14:23:02 +00:00
|
|
|
func toHexBytes(b [][]byte) []types.HexBytes {
|
|
|
|
hb := make([]types.HexBytes, len(b))
|
|
|
|
|
|
|
|
for i, v := range b {
|
|
|
|
hb[i] = types.HexBytes(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return hb
|
|
|
|
}
|
|
|
|
|
|
|
|
func fromHexBytes(hb []types.HexBytes) [][]byte {
|
|
|
|
b := make([][]byte, len(hb))
|
|
|
|
|
|
|
|
for i, v := range hb {
|
|
|
|
b[i] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) MarkAllActivityCenterNotificationsRead(ctx context.Context) error {
|
|
|
|
if m.hasPairedDevices() {
|
|
|
|
ids, err := m.persistence.GetNotReadActivityCenterNotificationIds()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = m.MarkActivityCenterNotificationsRead(ctx, toHexBytes(ids), true)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-07 12:57:14 +00:00
|
|
|
return m.persistence.MarkAllActivityCenterNotificationsRead()
|
|
|
|
}
|
|
|
|
|
2021-12-02 14:23:02 +00:00
|
|
|
func (m *Messenger) MarkActivityCenterNotificationsRead(ctx context.Context, ids []types.HexBytes, sync bool) (*MessengerResponse, error) {
|
|
|
|
err := m.persistence.MarkActivityCenterNotificationsRead(ids)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !sync {
|
|
|
|
notifications, err := m.persistence.GetActivityCenterNotificationsByID(ids)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return m.processActivityCenterNotifications(notifications, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
syncMessage := &protobuf.SyncActivityCenterRead{
|
|
|
|
Clock: m.getTimesource().GetCurrentTime(),
|
|
|
|
Ids: fromHexBytes(ids),
|
|
|
|
}
|
|
|
|
|
|
|
|
encodedMessage, err := proto.Marshal(syncMessage)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = m.sendToPairedDevices(ctx, common.RawMessage{
|
|
|
|
Payload: encodedMessage,
|
|
|
|
MessageType: protobuf.ApplicationMetadataMessage_SYNC_ACTIVITY_CENTER_READ,
|
|
|
|
ResendAutomatically: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil, err
|
2021-06-11 16:47:53 +00:00
|
|
|
}
|
|
|
|
|
2021-09-24 10:57:15 +00:00
|
|
|
func (m *Messenger) MarkActivityCenterNotificationsUnread(ids []types.HexBytes) error {
|
|
|
|
return m.persistence.MarkActivityCenterNotificationsUnread(ids)
|
|
|
|
}
|
|
|
|
|
2021-12-02 14:23:02 +00:00
|
|
|
func (m *Messenger) processActivityCenterNotifications(notifications []*ActivityCenterNotification, addNotifications bool) (*MessengerResponse, error) {
|
2021-04-07 12:57:14 +00:00
|
|
|
response := &MessengerResponse{}
|
|
|
|
var chats []*Chat
|
|
|
|
for _, notification := range notifications {
|
|
|
|
if notification.ChatID != "" {
|
|
|
|
chat, ok := m.allChats.Load(notification.ChatID)
|
|
|
|
if !ok {
|
|
|
|
// This should not really happen, but ignore just in case it was deleted in the meantime
|
|
|
|
m.logger.Warn("chat not found")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
chat.Active = true
|
|
|
|
|
2022-05-30 15:34:48 +00:00
|
|
|
if chat.PrivateGroupChat() {
|
|
|
|
// Send Joined message for backward compatibility
|
|
|
|
_, err := m.ConfirmJoiningGroup(context.Background(), chat.ID)
|
|
|
|
if err != nil {
|
|
|
|
m.logger.Error("failed to join group", zap.Error(err))
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-07 12:57:14 +00:00
|
|
|
chats = append(chats, chat)
|
|
|
|
response.AddChat(chat)
|
|
|
|
}
|
2021-12-02 14:23:02 +00:00
|
|
|
|
|
|
|
if addNotifications {
|
|
|
|
response.AddActivityCenterNotification(notification)
|
|
|
|
}
|
2021-04-07 12:57:14 +00:00
|
|
|
}
|
|
|
|
if len(chats) != 0 {
|
|
|
|
err := m.saveChats(chats)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
|
2021-12-02 14:23:02 +00:00
|
|
|
func (m *Messenger) processAcceptedActivityCenterNotifications(ctx context.Context, notifications []*ActivityCenterNotification, sync bool) (*MessengerResponse, error) {
|
|
|
|
ids := make([][]byte, len(notifications))
|
|
|
|
|
|
|
|
for i := range notifications {
|
|
|
|
ids[i] = notifications[i].ID
|
|
|
|
notifications[i].Accepted = true
|
2021-12-07 14:34:43 +00:00
|
|
|
notifications[i].Read = true
|
2021-12-02 14:23:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if sync {
|
|
|
|
syncMessage := &protobuf.SyncActivityCenterAccepted{
|
|
|
|
Clock: m.getTimesource().GetCurrentTime(),
|
|
|
|
Ids: ids,
|
|
|
|
}
|
|
|
|
|
|
|
|
encodedMessage, err := proto.Marshal(syncMessage)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = m.sendToPairedDevices(ctx, common.RawMessage{
|
|
|
|
Payload: encodedMessage,
|
|
|
|
MessageType: protobuf.ApplicationMetadataMessage_SYNC_ACTIVITY_CENTER_ACCEPTED,
|
|
|
|
ResendAutomatically: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.processActivityCenterNotifications(notifications, !sync)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) AcceptAllActivityCenterNotifications(ctx context.Context) (*MessengerResponse, error) {
|
2021-04-07 12:57:14 +00:00
|
|
|
notifications, err := m.persistence.AcceptAllActivityCenterNotifications()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-12-02 14:23:02 +00:00
|
|
|
|
|
|
|
return m.processAcceptedActivityCenterNotifications(ctx, notifications, true)
|
2021-04-07 12:57:14 +00:00
|
|
|
}
|
|
|
|
|
2021-12-02 14:23:02 +00:00
|
|
|
func (m *Messenger) AcceptActivityCenterNotifications(ctx context.Context, ids []types.HexBytes, sync bool) (*MessengerResponse, error) {
|
2022-02-09 13:15:57 +00:00
|
|
|
|
|
|
|
if len(ids) == 0 {
|
|
|
|
return nil, errors.New("notifications ids are not provided")
|
|
|
|
}
|
|
|
|
|
2021-04-07 12:57:14 +00:00
|
|
|
notifications, err := m.persistence.AcceptActivityCenterNotifications(ids)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-12-02 14:23:02 +00:00
|
|
|
|
|
|
|
return m.processAcceptedActivityCenterNotifications(ctx, notifications, sync)
|
2021-04-07 12:57:14 +00:00
|
|
|
}
|
|
|
|
|
2021-12-02 14:23:02 +00:00
|
|
|
func (m *Messenger) DismissAllActivityCenterNotifications(ctx context.Context) error {
|
|
|
|
if m.hasPairedDevices() {
|
|
|
|
ids, err := m.persistence.GetToProcessActivityCenterNotificationIds()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = m.DismissActivityCenterNotifications(ctx, toHexBytes(ids), true)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-07 12:57:14 +00:00
|
|
|
return m.persistence.DismissAllActivityCenterNotifications()
|
|
|
|
}
|
|
|
|
|
2021-12-02 14:23:02 +00:00
|
|
|
func (m *Messenger) DismissActivityCenterNotifications(ctx context.Context, ids []types.HexBytes, sync bool) (*MessengerResponse, error) {
|
|
|
|
err := m.persistence.DismissActivityCenterNotifications(ids)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !sync {
|
|
|
|
notifications, err := m.persistence.GetActivityCenterNotificationsByID(ids)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.processActivityCenterNotifications(notifications, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
syncMessage := &protobuf.SyncActivityCenterDismissed{
|
|
|
|
Clock: m.getTimesource().GetCurrentTime(),
|
|
|
|
Ids: fromHexBytes(ids),
|
|
|
|
}
|
|
|
|
|
|
|
|
encodedMessage, err := proto.Marshal(syncMessage)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = m.sendToPairedDevices(ctx, common.RawMessage{
|
|
|
|
Payload: encodedMessage,
|
|
|
|
MessageType: protobuf.ApplicationMetadataMessage_SYNC_ACTIVITY_CENTER_DISMISSED,
|
|
|
|
ResendAutomatically: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil, err
|
2021-04-07 12:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) ActivityCenterNotifications(cursor string, limit uint64) (*ActivityCenterPaginationResponse, error) {
|
|
|
|
cursor, notifications, err := m.persistence.ActivityCenterNotifications(cursor, limit)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-08-31 14:41:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &ActivityCenterPaginationResponse{
|
|
|
|
Cursor: cursor,
|
|
|
|
Notifications: notifications,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) ReadActivityCenterNotifications(cursor string, limit uint64, activityType ActivityCenterType) (*ActivityCenterPaginationResponse, error) {
|
|
|
|
cursor, notifications, err := m.persistence.ReadActivityCenterNotifications(cursor, limit, activityType)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ActivityCenterPaginationResponse{
|
|
|
|
Cursor: cursor,
|
|
|
|
Notifications: notifications,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) UnreadActivityCenterNotifications(cursor string, limit uint64, activityType ActivityCenterType) (*ActivityCenterPaginationResponse, error) {
|
|
|
|
cursor, notifications, err := m.persistence.UnreadActivityCenterNotifications(cursor, limit, activityType)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-04-07 12:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &ActivityCenterPaginationResponse{
|
|
|
|
Cursor: cursor,
|
|
|
|
Notifications: notifications,
|
|
|
|
}, nil
|
|
|
|
}
|
2021-12-02 14:23:02 +00:00
|
|
|
|
|
|
|
func (m *Messenger) handleActivityCenterRead(state *ReceivedMessageState, message protobuf.SyncActivityCenterRead) error {
|
|
|
|
resp, err := m.MarkActivityCenterNotificationsRead(context.TODO(), toHexBytes(message.Ids), false)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return state.Response.Merge(resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) handleActivityCenterAccepted(state *ReceivedMessageState, message protobuf.SyncActivityCenterAccepted) error {
|
|
|
|
resp, err := m.AcceptActivityCenterNotifications(context.TODO(), toHexBytes(message.Ids), false)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return state.Response.Merge(resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) handleActivityCenterDismissed(state *ReceivedMessageState, message protobuf.SyncActivityCenterDismissed) error {
|
|
|
|
resp, err := m.DismissActivityCenterNotifications(context.TODO(), toHexBytes(message.Ids), false)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return state.Response.Merge(resp)
|
|
|
|
}
|