Skip to content

Commit dbe7970

Browse files
Merge pull request #131 from pieces-app/login-logic
add login logic
2 parents 438249e + d8631fc commit dbe7970

File tree

8 files changed

+65
-41
lines changed

8 files changed

+65
-41
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ You can also logout from your account by going to the `Logout` option in the Aut
164164

165165
### `:PiecesLogin`
166166

167-
Login to your Pieces account.
167+
Sign into your Pieces account.
168168

169169
```vim
170170
:PiecesLogin

lua/pieces/auth.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ local function create_ui(content, default_hl)
7979
end
8080

8181
local function login_page()
82-
local choice = vim.fn.confirm("Note this command only works if you are logged in, Do you want to login?", "&Yes\n&No", 1)
82+
local choice = vim.fn.confirm("Note this command only works if you are logged in, Do you want to sign in?", "&Yes\n&No", 1)
8383
if choice == 1 then
8484
vim.fn.PiecesLogin(true)
8585
end

lua/pieces/onboarding.lua

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ end
1919

2020
local steps = {
2121
[=[
22-
**Step 1: Save a Material**
22+
**Step 2: Save a Material**
2323
2424
- Let's get started by saving a material to Pieces.
2525
- Select the following material, then run `:PiecesCreateMaterial`
@@ -29,7 +29,7 @@ pip3 install pieces-cli
2929
```
3030
]=],
3131
[=[
32-
**Step 2: Manage your saved Materials**
32+
**Step 3: Manage your saved Materials**
3333
3434
- Now, let's view all of your saved materials by typing **`:PiecesDrive`**.
3535
@@ -43,36 +43,37 @@ pip3 install pieces-cli
4343
iv. Type :q and press <Enter> to exit the material editor.
4444
]=],
4545
[=[
46-
**Step 3: Activate Copilot**
46+
**Step 4: Activate Copilot**
4747
4848
- Run **`:PiecesCopilot`** to Activate the Pieces Copilot to assist you with code.
4949
- Use /change_model in the Copilot input to change the model
5050
- Use /context to change the chat context
5151
5252
]=],
5353
[=[
54-
**Step 4: Manage Chats**
54+
**Step 5: Manage Chats**
5555
5656
- Access your previous chat and interactions with the Pieces Copilot using **`:PiecesChats`**.
5757
]=],
5858
[=[
59-
**Step 5: Check your account status**
59+
**Step 6: Check your account status**
6060
6161
- Run **`:PiecesAccount`** to Manage your Pieces account settings directly from Neovim.
6262
]=],
6363
[=[
64-
**Step 6: Sharing your Feedback**
64+
**Step 7: Sharing your Feedback**
6565
6666
- Your feedback is very **important** to us. Please share some of your feedback by typing `:PiecesFeedback`.
6767
]=],
6868
[=[
69-
**Step 7: Contributing**
69+
**Step 8: Contributing**
7070
7171
- The Pieces for Neovim plugin is an **open source project** and you can contribute to it by creating a pull request or open an issue by typing **`:PiecesContribute`**.
7272
]=]
7373
}
7474

7575
local commands = {
76+
"PiecesLogin",
7677
"'<,'>PiecesCreateMaterial",
7778
"PiecesDrive",
7879
"PiecesCopilot",

lua/pieces/utils.lua

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,15 @@ local function notify_pieces_os()
1717
vim.cmd("PiecesOpenPiecesOS")
1818
end
1919
end
20+
21+
local function notify_login()
22+
local choice = vim.fn.confirm("You must login to use this feature, Do you want to open the login page?", "&Yes\n&No", 1)
23+
if choice == 1 then
24+
vim.cmd("PiecesLogin")
25+
end
26+
end
2027
return {
2128
make_buffer_read_only=make_buffer_read_only,
29+
notify_login=notify_login,
2230
notify_pieces_os=notify_pieces_os
23-
}
31+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "1.4.2"
1+
__version__ = "1.5.0"

rplugin/python3/pieces_python/main.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,13 @@ def open_pieces(self):
155155
return self.nvim.async_call(self.nvim.err_write, "Could not start Pieces OS\n")
156156

157157
@pynvim.command("PiecesClosePiecesOS")
158-
@is_pieces_opened
158+
@is_pieces_opened()
159159
def close_pieces_os(self):
160160
Settings.api_client.os_api.os_terminate()
161161
return self.nvim.out_write("Closed PiecesOS\n")
162162

163163
@pynvim.command('PiecesOSVersion')
164-
@is_pieces_opened
164+
@is_pieces_opened(True)
165165
def get_version(self):
166166
self.nvim.out_write(f"{Settings.api_client.version}\n")
167167

@@ -170,22 +170,22 @@ def get_plugin_version(self):
170170
self.nvim.out_write(f"{__version__}\n")
171171

172172
@pynvim.command('PiecesLogin')
173-
@is_pieces_opened
173+
@is_pieces_opened(True)
174174
def login(self):
175175
self.auth.login()
176176

177177
@pynvim.command('PiecesLogout')
178-
@is_pieces_opened
178+
@is_pieces_opened(True)
179179
def logout(self):
180180
self.auth.logout()
181181

182182
@pynvim.command('PiecesConnectCloud')
183-
@is_pieces_opened
183+
@is_pieces_opened()
184184
def connect(self):
185185
self.auth.connect()
186186

187187
@pynvim.command('PiecesDisconnectCloud')
188-
@is_pieces_opened
188+
@is_pieces_opened(True)
189189
def disconnect(self):
190190
self.auth.disconnect()
191191

@@ -195,12 +195,12 @@ def install(self):
195195

196196
# LUA COMMANDS
197197
@pynvim.command("PiecesDrive")
198-
@is_pieces_opened
198+
@is_pieces_opened()
199199
def open_snippets(self):
200200
self.nvim.exec_lua("require('pieces.assets').setup()")
201201

202202
@pynvim.command("PiecesCopilot")
203-
@is_pieces_opened
203+
@is_pieces_opened()
204204
def open_copilot(self):
205205
if Settings.get_copilot_mode() == "BROWSER":
206206
return Settings.open_website(
@@ -214,17 +214,17 @@ def open_copilot(self):
214214
self.nvim.exec_lua("require('pieces.copilot').setup()")
215215

216216
@pynvim.command("PiecesChats")
217-
@is_pieces_opened
217+
@is_pieces_opened()
218218
def open_conversations(self):
219219
self.nvim.exec_lua(
220220
"require('pieces.copilot.conversations_ui').setup()")
221221

222222
@pynvim.command("PiecesAccount")
223-
@is_pieces_opened
223+
@is_pieces_opened(True)
224224
def auth_command(self):
225225
self.nvim.exec_lua("require('pieces.auth').setup()")
226226

227227
@pynvim.command('PiecesCreateMaterial', range='', nargs='*')
228-
@is_pieces_opened
228+
@is_pieces_opened()
229229
def pieces_create_snippet(self, args, range):
230-
self.nvim.exec_lua(f"require('pieces.assets.create').setup()")
230+
self.nvim.exec_lua("require('pieces.assets.create').setup()")

rplugin/python3/pieces_python/settings.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,12 @@ def get_os_id(cls):
105105

106106
@classmethod
107107
def open_website(cls, url: str):
108-
from .auth.auth_user import AuthUser
108+
from .auth import Auth
109109
if (not cls.api_client.is_pos_stream_running) and ("pieces.app" not in url):
110110
return webbrowser.open(url)
111111
para = {}
112-
if AuthUser.user_profile:
113-
para["user"] = AuthUser.user_profile.id
112+
if Auth.user_profile:
113+
para["user"] = Auth.user_profile.id
114114
_id = cls.get_os_id()
115115
if _id:
116116
para["os"] = _id
@@ -121,5 +121,4 @@ def open_website(cls, url: str):
121121

122122
url_parts[4] = urlencode(query)
123123
new_url = urlunparse(url_parts)
124-
print(new_url)
125124
webbrowser.open(new_url)

rplugin/python3/pieces_python/utils.py

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
from .settings import Settings
55
import os
66

7-
PIECES_OS_MIN_VERSION = "11.0.0" # Minium version (11.0.0)
8-
PIECES_OS_MAX_VERSION = "12.0.0" # Maxium version (12.0.0)
7+
PIECES_OS_MIN_VERSION = "12.0.0" # Minimum version (12.0.0)
8+
PIECES_OS_MAX_VERSION = "13.0.0" # Maximum version (13.0.0)
99

1010

1111
def convert_to_lua_table(python_dict):
@@ -84,22 +84,38 @@ def check_compatibility(notify_if_pos_off=False):
8484
else:
8585
return True
8686

87+
def check_login() -> bool:
88+
from .auth import Auth
8789

88-
def is_pieces_opened(func):
89-
def wrapper(*args, **kwargs):
90-
if not check_compatibility(True):
91-
return
90+
if not Auth.user_profile:
91+
Settings.nvim.exec_lua("require('pieces.utils').notify_login()")
92+
return False
93+
return True
9294

93-
if Settings.api_client.is_pos_stream_running:
94-
return func(*args, **kwargs)
95-
else:
96-
# Run the health request to check if the server is running
97-
if Settings.api_client.is_pieces_running():
98-
HealthWS.get_instance().start()
95+
def is_pieces_opened(bypass_login=False):
96+
"""
97+
Decorator that checks if POS is running and shows a notification if it's not.
98+
It also checks if the user is logged in or allows bypass via parameter.
99+
"""
100+
def decorator(func):
101+
def wrapper(*args, **kwargs):
102+
if not check_compatibility(True):
103+
return
104+
105+
login_state = check_login() or bypass_login
106+
if Settings.api_client.is_pos_stream_running and login_state:
99107
return func(*args, **kwargs)
100108
else:
101-
return Settings.nvim.exec_lua("require('pieces.utils').notify_pieces_os()")
102-
return wrapper
109+
# Run the health request to check if the server is running
110+
if Settings.api_client.is_pieces_running():
111+
if not login_state:
112+
return
113+
HealthWS.get_instance().start()
114+
return func(*args, **kwargs)
115+
else:
116+
return Settings.nvim.exec_lua("require('pieces.utils').notify_pieces_os()")
117+
return wrapper
118+
return decorator
103119

104120

105121
def install_pieces_os():

0 commit comments

Comments
 (0)