This repository has been archived by the owner on Mar 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notifications.rb
70 lines (65 loc) · 2.26 KB
/
notifications.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
require 'httparty'
require 'termux.rb'
require 'logging.rb'
def download_file(url, filename)
File.open(filename, "w+") do |file|
file.binmode
HTTParty.get(url, stream_body: true) do |fragment|
file.write(fragment)
end
end
end
module Notifications
class <<self
attr_accessor :displayed
end
@displayed=Set.new([])
def self.poll(endpoint, user, appPassword)
resp=HTTParty.get("https://#{endpoint}/ocs/v2.php/apps/notifications/api/v2/notifications", :basic_auth => {:username => user, :password => appPassword}, :headers => {"Accept" => "application/json"})
if resp.code!=200 then
Logging::warn("Bad response code: #{resp.code}")
end
notifications=resp.parsed_response["ocs"]["data"]
display=[]
active=Set.new([])
notifications.each do |notification|
active<<notification["notification_id"]
if @displayed.add?(notification["notification_id"]) != nil then
display<<notification
end
end
@displayed.each do |_id|
if not active.include?(_id)
@displayed.delete(_id)
Termux::notify_clear(_id)
end
end
display
end
def self.display_notification(notification)
# furl=notification["icon"]
# fname=furl.split("/")[-1]
# download_file(furl,Dir.pwd+"/.cache/"+fname)
Logging::debug("Sending notification: #{notification["message"]}")
Termux::notify(content=notification["message"], group=notification["app"], n_id=notification["notification_id"], title=notification["subject"], image=nil)
end
def self.start_polling(endpoint, user, appPassword, sleep_time=5)
while true
new_set=Set.new([])
begin
Logging::debug("Polling new messages")
notifications=self.poll(endpoint, user, appPassword)
notifications.each do |notification|
self.display_notification(notification)
new_set<<notification["notification_id"]
end
rescue Interrupt
Logging::info("User requested stop. Exiting")
return
rescue Exception => e
Logging::error("Exception while polling: #{e.message}")
end
sleep(sleep_time)
end
end
end