Skip to content

Commit 8184ce1

Browse files
committed
feat(util): add cache for users name. First startup of plugin my take some time
1 parent cd12714 commit 8184ce1

File tree

5 files changed

+77
-9
lines changed

5 files changed

+77
-9
lines changed

lua/glpi/api.lua

+33-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local curl = require("plenary.curl")
22
local config = require("glpi.config")
3+
local utils = require("glpi.utils")
34
local M = {}
45

56
M.options = {
@@ -167,26 +168,36 @@ local function get_user(user_id)
167168
end
168169

169170
local function get_username(user_id)
171+
local user_name = utils.get_user_in_cache(user_id)
172+
173+
if user_name ~= nil then
174+
return user_name
175+
end
176+
170177
local user = get_user(user_id)
171178
local firstname = user["firstname"]
172179
local lastname = user["realname"]
173-
return lastname .. " " .. firstname
180+
user_name = lastname .. " " .. firstname
181+
utils.add_user_to_cache(user_id, user_name)
182+
utils.write_user_cache()
183+
184+
return user_name
174185
end
175186

176187
local function search_tickets(opts)
177188
opts = opts or {}
178189

179-
local type = opts.type or "new"
190+
local ticket_type = opts.type or "new"
180191

181192
local crit = {}
182193

183-
if type == "new" then
194+
if ticket_type == "new" then
184195
table.insert(crit, {
185196
field = 12,
186197
value = 1,
187198
searchtype = "equals",
188199
})
189-
elseif type == "my_pending" then
200+
elseif ticket_type == "my_pending" then
190201
table.insert(crit, {
191202
field = 5,
192203
value = M.options.user_id,
@@ -203,7 +214,7 @@ local function search_tickets(opts)
203214
},
204215
},
205216
})
206-
elseif type == "my_processing" then
217+
elseif ticket_type == "my_processing" then
207218
table.insert(crit, {
208219
field = 5,
209220
value = M.options.user_id,
@@ -220,7 +231,7 @@ local function search_tickets(opts)
220231
},
221232
},
222233
})
223-
elseif type == "my" then
234+
elseif ticket_type == "my" then
224235
table.insert(crit, {
225236
field = 5,
226237
value = M.options.user_id,
@@ -243,7 +254,7 @@ local function search_tickets(opts)
243254
},
244255
},
245256
})
246-
elseif type == "other" then
257+
elseif ticket_type == "other" then
247258
table.insert(crit, {
248259
field = 5,
249260
value = M.options.user_id,
@@ -274,9 +285,24 @@ local function search_tickets(opts)
274285
return nil
275286
end
276287

288+
local function parse_usernames(users)
289+
if type(users) == "string" then
290+
return get_username(users)
291+
end
292+
293+
local names = {}
294+
for _, user in ipairs(users) do
295+
table.insert(names, get_username(user))
296+
end
297+
return names
298+
end
299+
277300
for i, _ in ipairs(tickets) do
278301
local status = get_status(tickets[i]["12"])
279302
tickets[i]["12"] = status
303+
304+
tickets[i]["4"] = parse_usernames(tickets[i]["4"])
305+
tickets[i]["5"] = parse_usernames(tickets[i]["5"])
280306
end
281307

282308
return tickets

lua/glpi/init.lua

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
local config = require("glpi.config")
22
local view = require("glpi.view")
33
local api = require("glpi.api")
4+
local utils = require("glpi.utils")
45

56
local M = {}
67

@@ -45,6 +46,7 @@ end
4546

4647
function M.setup(opts)
4748
config.setup(opts)
49+
utils.setup()
4850

4951
vim.keymap.set("n", config.keymaps.reload_ticket, reload_tickets, {})
5052
vim.keymap.set("n", config.keymaps.toggle_separation, function()

lua/glpi/utils.lua

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
local path = require("plenary.path")
2+
3+
local M = {}
4+
5+
local data_path = path:new(vim.fn.stdpath("data") .. "/glpi")
6+
local user_cache_file = path:new(data_path):joinpath("user_cache.json")
7+
8+
local user_cache = {}
9+
10+
function M.setup()
11+
if not data_path:exists() then
12+
data_path:mkdir({ parent = true })
13+
end
14+
15+
if not user_cache_file:exists() then
16+
user_cache = {}
17+
return
18+
end
19+
20+
local content = user_cache_file:read()
21+
user_cache = content ~= nil and vim.json.decode(content) or {}
22+
end
23+
24+
function M.get_user_in_cache(user_id)
25+
return user_cache[tostring(user_id)]
26+
end
27+
28+
function M.add_user_to_cache(user_id, user_name)
29+
user_cache[tostring(user_id)] = user_name
30+
end
31+
32+
function M.write_user_cache()
33+
local content = vim.json.encode(user_cache)
34+
user_cache_file:write(content, "w")
35+
end
36+
37+
return M

lua/glpi/view.lua

+4-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,10 @@ local function format_tickets_view(tickets)
166166
table.insert(lines, "# " .. title .. " (" .. #tickets_list .. ")")
167167
table.insert(lines, "")
168168
for _, ticket in ipairs(tickets_list) do
169-
table.insert(lines, "- " .. ticket["1"] .. " (" .. ticket["12"] .. ")")
169+
local ticket_4 = ticket["4"]
170+
local name = type(ticket_4) == "table" and ticket_4[1] or ticket_4
171+
172+
table.insert(lines, "- " .. ticket["1"] .. " (" .. ticket["12"] .. "" .. name .. ")")
170173
end
171174
table.insert(lines, "")
172175
table.insert(lines, "")

lua/telescope/_extensions/glpi/tickets.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ local function get_tickets()
1717
local function extract_ticket(t)
1818
for _, ticket in ipairs(t) do
1919
local id = ticket["2"]
20-
local name = ticket["1"]
20+
local name = ticket["1"] .. " (" .. ticket["4"] .. ")"
2121
tickets[name] = id
2222
end
2323
end

0 commit comments

Comments
 (0)