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

Adds ability to search for templates by title/description messages. #28

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

JeffreyDoyle
Copy link
Member

Adds ability to search for templates by title/description messages.

eg:

POST /v2/templates/search/messages

Body: 
{
    "searchMessagesTitleENUS": "tokens",
    "searchMessagesDescriptionENUS": "transfer"
}

returns:

{
    "page": 0,
    "range": 100,
    "count": 1,
    "results": [
        {
            "f_type": "InteractionTemplate",
            "f_version": "1.0.0",
            "id": "290b6b6222b2a77b16db896a80ddf29ebd1fa3038c9e6625a933fa213fce51fa",
            "data": {
                "type": "transaction",
                "interface": "",
                "messages": {
                    "title": {
                        "i18n": {
                            "en-US": "Transfer Tokens"
                        }
                    },
                    "description": {
                        "i18n": {
                            "en-US": "Transfer tokens from one account to another"
                        }
                    }
                },
                "cadence": "import FungibleToken from 0xFUNGIBLETOKENADDRESS\ntransaction(amount: UFix64, to: Address) {\nlet vault: @FungibleToken.Vault\nprepare(signer: AuthAccount) {\nself.vault <- signer\n.borrow<&{FungibleToken.Provider}>(from: /storage/flowTokenVault)!\n.withdraw(amount: amount)\n}\nexecute {\ngetAccount(to)\n.getCapability(/public/flowTokenReceiver)!\n.borrow<&{FungibleToken.Receiver}>()!\n.deposit(from: <-self.vault)\n}\n}",
                "dependencies": {
                    "0xFUNGIBLETOKENADDRESS": {
                        "FungibleToken": {
                            "mainnet": {
                                "address": "0xf233dcee88fe0abe",
                                "fq_address": "A.0xf233dcee88fe0abe.FungibleToken",
                                "contract": "FungibleToken",
                                "pin": "83c9e3d61d3b5ebf24356a9f17b5b57b12d6d56547abc73e05f820a0ae7d9cf5",
                                "pin_block_height": 34166296
                            },
                            "testnet": {
                                "address": "0x9a0766d93b6608b7",
                                "fq_address": "A.0x9a0766d93b6608b7.FungibleToken",
                                "contract": "FungibleToken",
                                "pin": "83c9e3d61d3b5ebf24356a9f17b5b57b12d6d56547abc73e05f820a0ae7d9cf5",
                                "pin_block_height": 74776482
                            }
                        }
                    }
                },
                "arguments": {
                    "amount": {
                        "index": 0,
                        "type": "UFix64",
                        "messages": {
                            "title": {
                                "i18n": {
                                    "en-US": "The amount of FLOW tokens to send"
                                }
                            }
                        },
                        "balance": ""
                    },
                    "to": {
                        "index": 1,
                        "type": "Address",
                        "messages": {
                            "title": {
                                "i18n": {
                                    "en-US": "The Flow account the tokens will go to"
                                }
                            }
                        },
                        "balance": ""
                    }
                }
            }
        }
    ]
}

@vercel
Copy link

vercel bot commented Oct 18, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
flow-interaction-template-service ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 18, 2023 10:56pm

Copy link
Member

@chasefleming chasefleming left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some responses return strings and others objects. Should have consistent formatting.

let template;
try {
template = await templateService.getTemplateByCadenceASTHash(
await genHash(cadenceAST),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're awaiting the genHash function multiple times for the same input. Compute it once.

@@ -0,0 +1,185 @@
import express, { Request, Response, Router } from "express";
import { body } from "express-validator";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove

let templateId: string = "";
let _name: string = name;
while (_name !== undefined) {
let foundName = namesJSONFile[_name];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are names guaranteed to be unique?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the names json file, yes

@chasefleming
Copy link
Member

@JeffreyDoyle this also needs documentation on the readme or something

try {
const templates = await templateService.searchTemplates(
page ?? 0,
range ?? 100,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need a max range or someone could slam it with too large a number

@bthaile
Copy link

bthaile commented Oct 18, 2023

Vercel preview link doesn't work. Is it suppose to?

@chasefleming
Copy link
Member

Also, is there a library you can use to make search better? Exact text match does not seem ideal. Tagging may be a better option since search is hard.

Copy link

@bartolomej bartolomej left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, I'm coming from this issue #18 (comment).

As I mentioned, Flowser app would benefit from this kind of functionality greatly. Exciting stuff!

Added a few thoughts of my own below.

return res.send(template);
});

router.post("/templates/search/messages", async (req: Request, res: Response) => {
Copy link

@bartolomej bartolomej Nov 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: The endpoint path is a bit confusing to me, as it could also be interpreted as searching for a "message" resource. But that's not true, I think it's only searching by "message" fields like title/description.

Could it make more sense for it to be named /templates/search or even just GET /templates?search={query} (where an empty query or no query parameter present could list all templates)? Using the GET verb could also make sense (semantically at least), since this only reads data.

Copy link

@bartolomej bartolomej Nov 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see there is also an endpoint for searching by the AST hash. Maybe that could instead just be another query parameter like /templates/search?cadence_ast_hash?

It could lead to simpler API if there was a single endpoint that supported different search options, as opposed to multiple endpoints, each supporting a different search option.

Another search option could be searching by "dependencies", as I mentioned here: #18 (comment)

Thoughts?

}

if (searchMessagesDescriptionENUS) {
queryBuilder = queryBuilder.where("messages_description_enUS", "like", `%${searchMessagesDescriptionENUS}%`)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering if it would be possible to do full-text, instead of searching for exact substring matches?

I can see that SQLite does support some kind of full-text search capabilities: https://www.sqlite.org/fts5.html. But I'm not sure if this feature is available in better-sqlite3 which is used in this project.

Copy link

@bartolomej bartolomej Nov 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we update the documentation in README with v2 endpoints?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants