Skip to content

Commit

Permalink
feat: implement list shortcuts v2 api
Browse files Browse the repository at this point in the history
  • Loading branch information
boojack committed Aug 8, 2023
1 parent 07365fd commit 6a9fcb1
Show file tree
Hide file tree
Showing 11 changed files with 485 additions and 62 deletions.
28 changes: 28 additions & 0 deletions api/v2/shortcut_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,34 @@ func NewShortcutService(secret string, store *store.Store) *ShortcutService {
}
}

func (s *ShortcutService) ListShortcuts(ctx context.Context, _ *apiv2pb.ListShortcutsRequest) (*apiv2pb.ListShortcutsResponse, error) {
userID := ctx.Value(UserIDContextKey).(int32)
find := &store.FindShortcut{}
find.VisibilityList = []store.Visibility{store.VisibilityWorkspace, store.VisibilityPublic}
visibleShortcutList, err := s.Store.ListShortcuts(ctx, find)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to fetch visible shortcut list, err: %v", err)
}

find.VisibilityList = []store.Visibility{store.VisibilityPrivate}
find.CreatorID = &userID
shortcutList, err := s.Store.ListShortcuts(ctx, find)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to fetch private shortcut list, err: %v", err)
}

shortcutList = append(shortcutList, visibleShortcutList...)
shortcuts := []*apiv2pb.Shortcut{}
for _, shortcut := range shortcutList {
shortcuts = append(shortcuts, convertShortcutFromStorepb(shortcut))
}

response := &apiv2pb.ListShortcutsResponse{
Shortcuts: shortcuts,
}
return response, nil
}

func (s *ShortcutService) GetShortcut(ctx context.Context, request *apiv2pb.GetShortcutRequest) (*apiv2pb.GetShortcutResponse, error) {
shortcut, err := s.Store.GetShortcut(ctx, &store.FindShortcut{
Name: &request.Name,
Expand Down
8 changes: 5 additions & 3 deletions extension/src/components/PullShortcutsButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useStorage } from "@plasmohq/storage/hook";
import axios from "axios";
import { useEffect, useState } from "react";
import { toast } from "react-hot-toast";
import { Shortcut } from "@/types/proto/api/v2/shortcut_service_pb";
import { ListShortcutsResponse } from "@/types/proto/api/v2/shortcut_service_pb";
import "../style.css";
import Icon from "./Icon";

Expand All @@ -22,12 +22,14 @@ const PullShortcutsButton = () => {
const handlePullShortcuts = async (silence = false) => {
try {
setIsPulling(true);
const { data } = await axios.get<Shortcut[]>(`${domain}/api/v1/shortcut`, {
const {
data: { shortcuts },
} = await axios.get<ListShortcutsResponse>(`${domain}/api/v2/shortcut`, {
headers: {
Authorization: `Bearer ${accessToken}`,
},
});
setShortcuts(data);
setShortcuts(shortcuts);
if (!silence) {
toast.success("Shortcuts pulled");
}
Expand Down
43 changes: 43 additions & 0 deletions extension/src/types/proto/api/v2/shortcut_service_pb.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,49 @@ export declare class OpenGraphMetadata extends Message<OpenGraphMetadata> {
static equals(a: OpenGraphMetadata | PlainMessage<OpenGraphMetadata> | undefined, b: OpenGraphMetadata | PlainMessage<OpenGraphMetadata> | undefined): boolean;
}

/**
* @generated from message slash.api.v2.ListShortcutsRequest
*/
export declare class ListShortcutsRequest extends Message<ListShortcutsRequest> {
constructor(data?: PartialMessage<ListShortcutsRequest>);

static readonly runtime: typeof proto3;
static readonly typeName = "slash.api.v2.ListShortcutsRequest";
static readonly fields: FieldList;

static fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): ListShortcutsRequest;

static fromJson(jsonValue: JsonValue, options?: Partial<JsonReadOptions>): ListShortcutsRequest;

static fromJsonString(jsonString: string, options?: Partial<JsonReadOptions>): ListShortcutsRequest;

static equals(a: ListShortcutsRequest | PlainMessage<ListShortcutsRequest> | undefined, b: ListShortcutsRequest | PlainMessage<ListShortcutsRequest> | undefined): boolean;
}

/**
* @generated from message slash.api.v2.ListShortcutsResponse
*/
export declare class ListShortcutsResponse extends Message<ListShortcutsResponse> {
/**
* @generated from field: repeated slash.api.v2.Shortcut shortcuts = 1;
*/
shortcuts: Shortcut[];

constructor(data?: PartialMessage<ListShortcutsResponse>);

static readonly runtime: typeof proto3;
static readonly typeName = "slash.api.v2.ListShortcutsResponse";
static readonly fields: FieldList;

static fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): ListShortcutsResponse;

static fromJson(jsonValue: JsonValue, options?: Partial<JsonReadOptions>): ListShortcutsResponse;

static fromJsonString(jsonString: string, options?: Partial<JsonReadOptions>): ListShortcutsResponse;

static equals(a: ListShortcutsResponse | PlainMessage<ListShortcutsResponse> | undefined, b: ListShortcutsResponse | PlainMessage<ListShortcutsResponse> | undefined): boolean;
}

/**
* @generated from message slash.api.v2.GetShortcutRequest
*/
Expand Down
18 changes: 18 additions & 0 deletions extension/src/types/proto/api/v2/shortcut_service_pb.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,24 @@ export const OpenGraphMetadata = proto3.makeMessageType(
],
);

/**
* @generated from message slash.api.v2.ListShortcutsRequest
*/
export const ListShortcutsRequest = proto3.makeMessageType(
"slash.api.v2.ListShortcutsRequest",
[],
);

/**
* @generated from message slash.api.v2.ListShortcutsResponse
*/
export const ListShortcutsResponse = proto3.makeMessageType(
"slash.api.v2.ListShortcutsResponse",
() => [
{ no: 1, name: "shortcuts", kind: "message", T: Shortcut, repeated: true },
],
);

/**
* @generated from message slash.api.v2.GetShortcutRequest
*/
Expand Down
9 changes: 9 additions & 0 deletions proto/api/v2/shortcut_service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import "google/api/client.proto";
option go_package = "gen/api/v2";

service ShortcutService {
rpc ListShortcuts(ListShortcutsRequest) returns (ListShortcutsResponse) {
option (google.api.http) = {get: "/api/v2/shortcuts"};
}
// GetShortcut returns a shortcut by name.
rpc GetShortcut(GetShortcutRequest) returns (GetShortcutResponse) {
option (google.api.http) = {get: "/api/v2/shortcuts/{name}"};
Expand Down Expand Up @@ -60,6 +63,12 @@ enum Visibility {
PUBLIC = 3;
}

message ListShortcutsRequest {}

message ListShortcutsResponse {
repeated Shortcut shortcuts = 1;
}

message GetShortcutRequest {
string name = 1;
}
Expand Down
28 changes: 28 additions & 0 deletions proto/gen/api/v2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
- [api/v2/shortcut_service.proto](#api_v2_shortcut_service-proto)
- [GetShortcutRequest](#slash-api-v2-GetShortcutRequest)
- [GetShortcutResponse](#slash-api-v2-GetShortcutResponse)
- [ListShortcutsRequest](#slash-api-v2-ListShortcutsRequest)
- [ListShortcutsResponse](#slash-api-v2-ListShortcutsResponse)
- [OpenGraphMetadata](#slash-api-v2-OpenGraphMetadata)
- [Shortcut](#slash-api-v2-Shortcut)

Expand Down Expand Up @@ -102,6 +104,31 @@



<a name="slash-api-v2-ListShortcutsRequest"></a>

### ListShortcutsRequest







<a name="slash-api-v2-ListShortcutsResponse"></a>

### ListShortcutsResponse



| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| shortcuts | [Shortcut](#slash-api-v2-Shortcut) | repeated | |






<a name="slash-api-v2-OpenGraphMetadata"></a>

### OpenGraphMetadata
Expand Down Expand Up @@ -172,6 +199,7 @@

| Method Name | Request Type | Response Type | Description |
| ----------- | ------------ | ------------- | ------------|
| ListShortcuts | [ListShortcutsRequest](#slash-api-v2-ListShortcutsRequest) | [ListShortcutsResponse](#slash-api-v2-ListShortcutsResponse) | |
| GetShortcut | [GetShortcutRequest](#slash-api-v2-GetShortcutRequest) | [GetShortcutResponse](#slash-api-v2-GetShortcutResponse) | GetShortcut returns a shortcut by name. |


Expand Down
Loading

0 comments on commit 6a9fcb1

Please sign in to comment.