Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chat haystack time listing #903

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions apigrpc/apigrpc.swagger.json
Expand Up @@ -1319,6 +1319,14 @@
"in": "query",
"required": false,
"type": "string"
},
{
"name": "instant",
"description": "Time which around to list messages in seconds (since epoch), if any. Used only if no cursor is provided.",
"in": "query",
"required": false,
"type": "string",
"format": "int64"
}
],
"tags": [
Expand Down
1,895 changes: 960 additions & 935 deletions console/console.pb.go

Large diffs are not rendered by default.

14 changes: 9 additions & 5 deletions console/console.proto
Expand Up @@ -716,17 +716,21 @@ 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.
bool forward = 7;
// (Optional) Time to list messages around, if no cursor is provided.
google.protobuf.Int64Value haystack_sec = 8;
}

// List (and optionally filter) groups.
Expand Down
15 changes: 15 additions & 0 deletions console/console.swagger.json
Expand Up @@ -1180,6 +1180,21 @@
"in": "query",
"required": false,
"type": "string"
},
{
"name": "forward",
"description": "Whether to list messages from oldest to newest, or newest to oldest.",
"in": "query",
"required": false,
"type": "boolean"
},
{
"name": "haystack_sec",
"description": "(Optional) Time to list messages around, if no cursor is provided.",
"in": "query",
"required": false,
"type": "string",
"format": "int64"
}
],
"tags": [
Expand Down
32 changes: 25 additions & 7 deletions console/ui/src/app/channels/chatMessages.component.html
@@ -1,13 +1,29 @@
<h2 class="pb-1">Chat Messages</h2>

<div class="btn-group mb-1" ngbDropdown>
<button type="button" class="btn btn-outline-secondary" ngbDropdownToggle>
<span *ngIf="!activeFilter || activeFilter === ''">Filter by type</span>
<span *ngIf="activeFilter && activeFilter !== ''">{{activeFilter}}</span>
</button>
<div class="dropdown-menu" ngbDropdownMenu>
<button *ngFor="let f of filters" type="button" ngbDropdownItem (click)="activeFilter = f;">{{f}}</button>
<div class="row no-gutters mb-1">
<div class="col-md-1 d-flex justify-content-between no-gutters align-items-center">
<div class="btn-group mb-1" ngbDropdown>
<button type="button" class="btn btn-outline-secondary" ngbDropdownToggle>
<span *ngIf="!activeFilter || activeFilter === ''">Filter by type</span>
<span *ngIf="activeFilter && activeFilter !== ''">{{activeFilter}}</span>
</button>
<div class="dropdown-menu" ngbDropdownMenu>
<button *ngFor="let f of filters" type="button" ngbDropdownItem (click)="activeFilter = f;">{{f}}</button>
</div>
</div>
</div>
<div class="col-md-8 d-flex no-gutters align-items-center justify-content-end">
<div class="col-md-2">
<div class="custom-control custom-checkbox">
<input type="checkbox" id="forward" class="custom-control-input mr-2 my-2" [(ngModel)]="forward">
<label class="form-check-label custom-control-label" for="forward">Forward</label>
</div>
</div>
<div class="col-md-4">
<input type="datetime-local" id="haystack" class="form-control" [ngModel]="haystack | date:'yyyy-MM-ddTHH:mm'" (ngModelChange)="setHaystack($event)" pattern="[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}">
</div>
</div>
<div class="col-md-3"></div>
</div>
<div class="row no-gutters mb-4">
<div class="col d-flex justify-content-between no-gutters align-items-center">
Expand Down Expand Up @@ -54,6 +70,8 @@ <h2 class="pb-1">Chat Messages</h2>
<div class="btn-group page-btns" role="group" aria-label="Basic example">
<button type="button" class="btn btn-outline-secondary" (click)="search(0)" [disabled]="messages.length === 0">
<img src="/static/svg/page-first.svg" alt="" width="20" height=""></button>
<button type="button" class="btn btn-outline-secondary" (click)="search(-1)" [disabled]="prevCursor === '' || prevCursor === null"><img
src="/static/svg/page-prev.svg" alt="" width="20" height=""></button>
<button type="button" class="btn btn-outline-secondary" (click)="search(1)" [disabled]="nextCursor === '' || nextCursor === null"><img
src="/static/svg/page-next.svg" alt="" width="20" height=""></button>
</div>
Expand Down
43 changes: 34 additions & 9 deletions console/ui/src/app/channels/chatMessages.component.ts
Expand Up @@ -29,11 +29,14 @@ export class ChatListComponent implements OnInit {
public readonly systemUserId = '00000000-0000-0000-0000-000000000000';
public error = '';
public messages: Array<ApiChannelMessage> = [];
public prevCursor = '';
public nextCursor = '';
public searchForm1: FormGroup;
public searchForm2: FormGroup;
public searchForm3: FormGroup;
public type: number
public forward = false;
public haystack: string;
public confirmDeleteForm: FormGroup;
public deleteError = '';
public deleteSuccess = false;
Expand Down Expand Up @@ -76,6 +79,12 @@ export class ChatListComponent implements OnInit {
this.f3.user_id_two.setValue(qp.get('user_id_two'));

this.nextCursor = qp.get('cursor');
this.forward = qp.get('forward') === 'true';
const haystack = qp.get('haystack');
if (haystack !== null) {
// Trick datetime-local input into assuming the actual zero UTC offset time is the local time, by removing 'Z'
this.haystack = new Date(Number(haystack) * 1000).toISOString().slice(0, -5);
}
let qType = qp.get("type");
this.type = Number(qType)

Expand All @@ -88,6 +97,7 @@ export class ChatListComponent implements OnInit {
this.messages.length = 0;
this.messages.push(...d[0].messages);
this.nextCursor = d[0].next_cursor;
this.prevCursor = d[0].prev_cursor;
}
if (d.error) {
this.error = d.error;
Expand Down Expand Up @@ -119,23 +129,28 @@ export class ChatListComponent implements OnInit {
case 1:
cursor = this.nextCursor;
break;
case -1:
cursor = this.prevCursor;
break;
}
this.updateMessages(this.type, this.f1.label.value, this.f2.group_id.value,
this.f3.user_id_one.value, this.f3.user_id_two.value, cursor)
}

updateMessages(type: number, label: string, group_id: string, user_id_one: string, user_id_two: string, cursor: string): void {
// Add 'Z' to convert the datetime-local input value into zero UTC offset
const haystackUnix = Math.floor(new Date(this.haystack+"Z").getTime() / 1000)
switch(type) {
case (2):
this.consoleService.listChannelMessages('', type.toString(), label, null, null, null, encodeURIComponent(cursor))
this.consoleService.listChannelMessages('', type.toString(), label, null, null, null, encodeURIComponent(cursor), this.forward, (isNaN(haystackUnix) || haystackUnix === 0) ? null : String(haystackUnix))
.subscribe(d => this.postData(d, cursor), err => { this.error = err;});
break;
case (3):
this.consoleService.listChannelMessages('', type.toString(), null, group_id, null, null, encodeURIComponent(cursor))
this.consoleService.listChannelMessages('', type.toString(), null, group_id, null, null, encodeURIComponent(cursor), this.forward, (isNaN(haystackUnix) || haystackUnix === 0) ? null : String(haystackUnix))
.subscribe(d => this.postData(d, cursor), err => { this.error = err;});
break;
case (4):
this.consoleService.listChannelMessages('', type.toString(), null, null, user_id_one, user_id_two, encodeURIComponent(cursor))
this.consoleService.listChannelMessages('', type.toString(), null, null, user_id_one, user_id_two, encodeURIComponent(cursor), this.forward, (isNaN(haystackUnix) || haystackUnix === 0) ? null : String(haystackUnix))
.subscribe(d => this.postData(d, cursor), err => { this.error = err;});
break;
}
Expand All @@ -148,24 +163,30 @@ export class ChatListComponent implements OnInit {
this.messages.length = 0;
this.messages.push(...d.messages);
this.nextCursor = d.next_cursor;
this.prevCursor = d.prev_cursor;

let params: Params;
switch(this.type) {
case (2):
params = {type: this.type, label: this.f1.label.value, cursor};
params = {type: this.type, label: this.f1.label.value, cursor, forward: this.forward};
break;
case (3):
params = {type: this.type, group_id: this.f2.group_id.value, cursor};
params = {type: this.type, group_id: this.f2.group_id.value, cursor, forward: this.forward};
break;
case (4):
params = {
type: this.type,
user_id_one: this.f3.user_id_one.value,
user_id_two: this.f3.user_id_two.value,
cursor
cursor,
forward: this.forward
};
break;
}
const haystackUnix = Math.floor(new Date(this.haystack+"Z").getTime() / 1000)
if (!isNaN(haystackUnix) && haystackUnix !== 0) {
params['haystack'] = haystackUnix
}
this.router.navigate([], {
relativeTo: this.route,
queryParams: params,
Expand Down Expand Up @@ -209,6 +230,10 @@ export class ChatListComponent implements OnInit {
return this.confirmDeleteForm.controls;
}

public setHaystack(e: string) {
this.haystack = e;
}

public openDeleteDataModal(modal): void {
this.modalService.open(modal, {centered: true}).result.then(() => {
this.deleteData();
Expand Down Expand Up @@ -266,19 +291,19 @@ export class ChatSearchResolver implements Resolve<ApiChannelMessageList> {
let type = Number(route.queryParamMap.get('type'));
switch(type) {
case (2):
return this.consoleService.listChannelMessages('', type.toString(), route.queryParamMap.get('label'), null, null, null, encodeURIComponent(route.queryParamMap.get('cursor')))
return this.consoleService.listChannelMessages('', type.toString(), route.queryParamMap.get('label'), null, null, null, encodeURIComponent(route.queryParamMap.get('cursor')), route.queryParamMap.get('forward') === 'true', route.queryParamMap.get('haystack'))
.pipe(catchError(error => {
route.data = {...route.data, error};
return of(null);
}));
case (3):
return this.consoleService.listChannelMessages('', type.toString(), null, route.queryParamMap.get('group_id'), null, null, encodeURIComponent(route.queryParamMap.get('cursor')))
return this.consoleService.listChannelMessages('', type.toString(), null, route.queryParamMap.get('group_id'), null, null, encodeURIComponent(route.queryParamMap.get('cursor')), route.queryParamMap.get('forward') === 'true', route.queryParamMap.get('haystack'))
.pipe(catchError(error => {
route.data = {...route.data, error};
return of(null);
}));
case (4):
return this.consoleService.listChannelMessages('', type.toString(), null, null, route.queryParamMap.get('user_id_one'), route.queryParamMap.get('user_id_two'), encodeURIComponent(route.queryParamMap.get('cursor')))
return this.consoleService.listChannelMessages('', type.toString(), null, null, route.queryParamMap.get('user_id_one'), route.queryParamMap.get('user_id_two'), encodeURIComponent(route.queryParamMap.get('cursor')), route.queryParamMap.get('forward') === 'true', route.queryParamMap.get('haystack'))
.pipe(catchError(error => {
route.data = {...route.data, error};
return of(null);
Expand Down
8 changes: 7 additions & 1 deletion console/ui/src/app/console.service.ts
Expand Up @@ -1057,7 +1057,7 @@ export class ConsoleService {
}

/** List channel messages with the selected filter */
listChannelMessages(auth_token: string, type?: string, label?: string, group_id?: string, user_id_one?: string, user_id_two?: string, cursor?: string): Observable<ApiChannelMessageList> {
listChannelMessages(auth_token: string, type?: string, label?: string, group_id?: string, user_id_one?: string, user_id_two?: string, cursor?: string, forward?: boolean, haystack_sec?: string): Observable<ApiChannelMessageList> {
const urlPath = `/v2/console/channel`;
let params = new HttpParams();
if (type) {
Expand All @@ -1078,6 +1078,12 @@ export class ConsoleService {
if (cursor) {
params = params.set('cursor', cursor);
}
if (forward || forward === false) {
params = params.set('forward', String(forward));
}
if (haystack_sec) {
params = params.set('haystack_sec', haystack_sec);
}
return this.httpClient.get<ApiChannelMessageList>(this.config.host + urlPath, { params: params, headers: this.getTokenAuthHeaders(auth_token) })
}

Expand Down
7 changes: 6 additions & 1 deletion server/api_channel.go
Expand Up @@ -68,12 +68,17 @@ func (s *ApiServer) ListChannelMessages(ctx context.Context, in *api.ListChannel
forward = in.GetForward().Value
}

var instant int64
if in.GetInstant() != nil {
instant = in.GetInstant().Value
}

streamConversionResult, err := ChannelIdToStream(in.ChannelId)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "Invalid channel ID.")
}

messageList, err := ChannelMessagesList(ctx, s.logger, s.db, userID, streamConversionResult.Stream, in.ChannelId, limit, forward, in.Cursor)
messageList, err := ChannelMessagesList(ctx, s.logger, s.db, userID, streamConversionResult.Stream, in.ChannelId, limit, forward, in.Cursor, instant)
if err == runtime.ErrChannelCursorInvalid {
return nil, status.Error(codes.InvalidArgument, "Cursor is invalid or expired.")
} else if err == runtime.ErrChannelGroupNotFound {
Expand Down
2 changes: 1 addition & 1 deletion server/console_channel.go
Expand Up @@ -36,7 +36,7 @@ func (s *ConsoleServer) ListChannelMessages(ctx context.Context, in *console.Lis
return nil, status.Error(codes.InvalidArgument, "Cursor is invalid or expired.")
}

messageList, err := ChannelMessagesList(ctx, s.logger, s.db, uuid.Nil, *stream, channelId, limit, false, cursor)
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 {
Expand Down