Compare commits

...

5 Commits

Author SHA1 Message Date
Adrian Perez de Castro 2946b55810
Make :sync() always add a "since" query parameter if available
...which ensures that multiple consecutive requests to /sync paginate
properly.
2017-02-05 14:11:55 +01:00
Adrian Perez de Castro be1ee939ee
Make :sync() call the API endpoint always at least once
Othewise it could happen that a request to /sync would not be done even
once depending on how the "stop" callback is defined.
2017-02-05 13:50:40 +01:00
Adrian Perez de Castro ae18e3374d
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.
2017-02-05 11:14:25 +01:00
Adrian Perez de Castro fda9fa4e41
Return event_id from room methods that send messages
The "event_id" returned by the HS is returned directly, instead of
a lone table with a single "event_id" member, for the following methods:

  room:send_text()
  room:send_emote()
  room:send_notice()
2016-10-24 18:00:09 +03:00
Adrian Perez de Castro f420a7a8a7
CI: Correct cjson -> lua-cjson 2016-07-10 02:47:04 +03:00
2 changed files with 18 additions and 21 deletions

View File

@ -17,7 +17,7 @@ before_install:
- lua -v
install:
- luarocks install cjson
- luarocks install lua-cjson
- luarocks install luacov-coveralls
- luarocks install cluacov
- luarocks install busted

View File

@ -144,15 +144,18 @@ function Room:_log(fmt, ...)
end
function Room:send_text(text)
return self.client._api:send_message(self.room_id, text)
-- XXX: How does error handling work here?
return self.client._api:send_message(self.room_id, text).event_id
end
function Room:send_emote(text)
return self.client._api:send_emote(self.room_id, text)
-- XXX: How does error handling work here?
return self.client._api:send_emote(self.room_id, text).event_id
end
function Room:send_notice(text)
return self.client._api:send_notice(self.room_id, text)
-- XXX: How does error handling work here?
return self.client._api:send_notice(self.room_id, text).event_id
end
function Room:invite_user(user_id)
@ -320,43 +323,35 @@ Client.__name = "matrix.client"
Client.__index = 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
rooms = {}, -- Indexed by room_id
_log = get_debug_log_function(),
_api = API(base_url, token, http_client),
}, Client))
-- Do an initial sync if a token was provided on construction.
if token then
c:_sync()
end
return c
end })
function Client:__tostring()
return self.__name .. "{" .. self._api.base_url .. "}"
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",
{ user = username, password = password }), no_sync)
{ user = username, password = password }))
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",
{ user = username, password = password }), no_sync)
{ user = username, password = password }))
end
function Client:_logged_in(response, no_sync)
function Client:_logged_in(response)
self._log("logged-in: %s", response.user_id)
self.user_id = response.user_id
self.homeserver = response.home_server
self.token = response.access_token
self._api.token = response.access_token
self:fire("logged-in")
if not no_sync then
self:_sync()
end
return self.token
end
@ -440,7 +435,9 @@ function Client:_sync(options)
if not options then
options = {}
end
options.since = self._sync_next_batch
if not options.since then
options.since = self._sync_next_batch
end
self._log("sync: Requesting with next_batch = %s", options.since)
local response = self._api:sync(options)
@ -470,9 +467,9 @@ function Client:sync(stop, timeout)
if not stop then
stop = return_false
end
while not stop(self) do
repeat
self:_sync { timeout = timeout or 15000 }
end
until stop(self)
end
function Client:_sync_handle_room__join(room_id, data)