Commit ece43904 authored by Fernando Takagi's avatar Fernando Takagi
Browse files

Refactor core fn to accept int

parent bb89ac36
Loading
Loading
Loading
Loading
+7 −7
Original line number Diff line number Diff line
@@ -2173,20 +2173,20 @@ type ListChannelMessagesRequest struct {
	sizeCache     protoimpl.SizeCache
	unknownFields protoimpl.UnknownFields

	// Type of the chat channel
	// Type of the chat channel.
	Type ListChannelMessagesRequest_Type `protobuf:"varint,1,opt,name=type,proto3,enum=nakama.console.ListChannelMessagesRequest_Type" json:"type,omitempty"`
	// Label of the channel, if a standard chat room
	// Label of the channel, if a standard chat room.
	Label string `protobuf:"bytes,2,opt,name=label,proto3" json:"label,omitempty"`
	// Group ID of the channel, if a group chat
	// Group ID of the channel, if a group chat.
	GroupId string `protobuf:"bytes,3,opt,name=group_id,json=groupId,proto3" json:"group_id,omitempty"`
	// User IDs, if a direct chat
	// User IDs, if a direct chat.
	UserIdOne string `protobuf:"bytes,4,opt,name=user_id_one,json=userIdOne,proto3" json:"user_id_one,omitempty"`
	UserIdTwo string `protobuf:"bytes,5,opt,name=user_id_two,json=userIdTwo,proto3" json:"user_id_two,omitempty"`
	// Cursor to start from
	// Cursor to start from.
	Cursor string `protobuf:"bytes,6,opt,name=cursor,proto3" json:"cursor,omitempty"`
	// Whether to list messages from oldest to newest, or newest to oldest
	// Whether to list messages from oldest to newest, or newest to oldest.
	Forward bool `protobuf:"varint,7,opt,name=forward,proto3" json:"forward,omitempty"`
	// (Optional) Timestamp to list messages around, if no cursor is provided
	// (Optional) Time to list messages around, if no cursor is provided.
	HaystackSec *wrapperspb.Int64Value `protobuf:"bytes,8,opt,name=haystack_sec,json=haystackSec,proto3" json:"haystack_sec,omitempty"`
}

+7 −7
Original line number Diff line number Diff line
@@ -716,20 +716,20 @@ message ListChannelMessagesRequest {
    GROUP = 3;
    DIRECT = 4;
  }
  // Type of the chat channel
  // Type of the chat channel.
  Type type = 1;
  // Label of the channel, if a standard chat room
  // Label of the channel, if a standard chat room.
  string label = 2;
  // Group ID of the channel, if a group chat
  // Group ID of the channel, if a group chat.
  string group_id = 3;
  // User IDs, if a direct chat
  // User IDs, if a direct chat.
  string user_id_one = 4;
  string user_id_two = 5;
  // Cursor to start from
  // Cursor to start from.
  string cursor = 6;
  // Whether to list messages from oldest to newest, or newest to oldest
  // Whether to list messages from oldest to newest, or newest to oldest.
  bool forward = 7;
  // (Optional) Timestamp to list messages around, if no cursor is provided
  // (Optional) Time to list messages around, if no cursor is provided.
  google.protobuf.Int64Value haystack_sec = 8;
}

+1 −1
Original line number Diff line number Diff line
@@ -1190,7 +1190,7 @@
          },
          {
            "name": "haystack_sec",
            "description": "(Optional) Timestamp to list messages around, if no cursor is provided.",
            "description": "(Optional) Time to list messages around, if no cursor is provided.",
            "in": "query",
            "required": false,
            "type": "string",
+1 −8
Original line number Diff line number Diff line
@@ -36,14 +36,7 @@ func (s *ConsoleServer) ListChannelMessages(ctx context.Context, in *console.Lis
		return nil, status.Error(codes.InvalidArgument, "Cursor is invalid or expired.")
	}

	var haystack *time.Time
	if in.HaystackSec != nil {
		h := time.Unix(in.HaystackSec.Value, 0).UTC()
		haystack = &h
	} else {
		haystack = nil
	}
	messageList, err := ChannelMessagesList(ctx, s.logger, s.db, uuid.Nil, *stream, channelId, limit, in.Forward, cursor, haystack)
	messageList, err := ChannelMessagesList(ctx, s.logger, s.db, uuid.Nil, *stream, channelId, limit, in.Forward, cursor, in.HaystackSec.GetValue())
	if err == runtime.ErrChannelCursorInvalid {
		return nil, status.Error(codes.InvalidArgument, "Cursor is invalid or expired.")
	} else if err != nil {
+4 −4
Original line number Diff line number Diff line
@@ -59,7 +59,7 @@ type channelMessageListCursor struct {
	IsNext           bool
}

func ChannelMessagesList(ctx context.Context, logger *zap.Logger, db *sql.DB, caller uuid.UUID, stream PresenceStream, channelID string, limit int, forward bool, cursor string, haystack *time.Time) (*api.ChannelMessageList, error) {
func ChannelMessagesList(ctx context.Context, logger *zap.Logger, db *sql.DB, caller uuid.UUID, stream PresenceStream, channelID string, limit int, forward bool, cursor string, haystack int64) (*api.ChannelMessageList, error) {
	var incomingCursor *channelMessageListCursor

	if cursor != "" {
@@ -101,7 +101,7 @@ func ChannelMessagesList(ctx context.Context, logger *zap.Logger, db *sql.DB, ca
		}
	}

	if cursor == "" && haystack != nil {
	if cursor == "" && haystack > 0 {
		return getChannelMessagesHaystack(ctx, logger, db, stream, channelID, limit, forward, haystack)
	} else {

@@ -285,11 +285,11 @@ WHERE stream_mode = $1 AND stream_subject = $2::UUID AND stream_descriptor = $3:
	}
}

func getChannelMessagesHaystack(ctx context.Context, logger *zap.Logger, db *sql.DB, stream PresenceStream, channelID string, limit int, forward bool, haystack *time.Time) (*api.ChannelMessageList, error) {
func getChannelMessagesHaystack(ctx context.Context, logger *zap.Logger, db *sql.DB, stream PresenceStream, channelID string, limit int, forward bool, haystack int64) (*api.ChannelMessageList, error) {
	query := `SELECT id, code, sender_id, username, content, create_time, update_time FROM message
WHERE stream_mode = $1 AND stream_subject = $2::UUID AND stream_descriptor = $3::UUID AND stream_label = $4`

	params := []any{stream.Mode, stream.Subject, stream.Subcontext, stream.Label, *haystack}
	params := []any{stream.Mode, stream.Subject, stream.Subcontext, stream.Label, time.Unix(haystack, 0).UTC()}
	// First half.
	firstQuery := query + " AND create_time <= $5 ORDER BY create_time DESC, id DESC LIMIT $6"
	firstParams := append(params, limit+1)