Support implicitly syncing on login
Whether to sync after connecting or not is desired depends on the application, and it's better to remove the possibility of doing so. Having to done one explicit call to :sync() is not a big deal, and makes the intention of code using the module clearer. And our code simpler.
This commit is contained in:
@@ -323,43 +323,35 @@ Client.__name = "matrix.client"
|
|||||||
Client.__index = Client
|
Client.__index = Client
|
||||||
|
|
||||||
setmetatable(Client, { __call = function (self, base_url, token, http_client)
|
setmetatable(Client, { __call = function (self, base_url, token, http_client)
|
||||||
local c = eventable.object(setmetatable({
|
return eventable.object(setmetatable({
|
||||||
presence = {}, -- Indexed by user_id
|
presence = {}, -- Indexed by user_id
|
||||||
rooms = {}, -- Indexed by room_id
|
rooms = {}, -- Indexed by room_id
|
||||||
_log = get_debug_log_function(),
|
_log = get_debug_log_function(),
|
||||||
_api = API(base_url, token, http_client),
|
_api = API(base_url, token, http_client),
|
||||||
}, Client))
|
}, Client))
|
||||||
-- Do an initial sync if a token was provided on construction.
|
|
||||||
if token then
|
|
||||||
c:_sync()
|
|
||||||
end
|
|
||||||
return c
|
|
||||||
end })
|
end })
|
||||||
|
|
||||||
function Client:__tostring()
|
function Client:__tostring()
|
||||||
return self.__name .. "{" .. self._api.base_url .. "}"
|
return self.__name .. "{" .. self._api.base_url .. "}"
|
||||||
end
|
end
|
||||||
|
|
||||||
function Client:register_with_password(username, password, no_sync)
|
function Client:register_with_password(username, password)
|
||||||
return self:_logged_in(self._api:register("m.login.password",
|
return self:_logged_in(self._api:register("m.login.password",
|
||||||
{ user = username, password = password }), no_sync)
|
{ user = username, password = password }))
|
||||||
end
|
end
|
||||||
|
|
||||||
function Client:login_with_password(username, password, no_sync)
|
function Client:login_with_password(username, password)
|
||||||
return self:_logged_in(self._api:login("m.login.password",
|
return self:_logged_in(self._api:login("m.login.password",
|
||||||
{ user = username, password = password }), no_sync)
|
{ user = username, password = password }))
|
||||||
end
|
end
|
||||||
|
|
||||||
function Client:_logged_in(response, no_sync)
|
function Client:_logged_in(response)
|
||||||
self._log("logged-in: %s", response.user_id)
|
self._log("logged-in: %s", response.user_id)
|
||||||
self.user_id = response.user_id
|
self.user_id = response.user_id
|
||||||
self.homeserver = response.home_server
|
self.homeserver = response.home_server
|
||||||
self.token = response.access_token
|
self.token = response.access_token
|
||||||
self._api.token = response.access_token
|
self._api.token = response.access_token
|
||||||
self:fire("logged-in")
|
self:fire("logged-in")
|
||||||
if not no_sync then
|
|
||||||
self:_sync()
|
|
||||||
end
|
|
||||||
return self.token
|
return self.token
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user