annotate src/facebook.js.luan @ 2:e32e4120dc70

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 11 Jul 2025 21:23:39 -0600
parents 8f4df159f06b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 local Luan = require "luan:Luan.luan"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2 local error = Luan.error
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 local ipairs = Luan.ipairs or error()
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 local stringify = Luan.stringify or error()
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 local String = require "luan:String.luan"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 local trim = String.trim or error()
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 local Table = require "luan:Table.luan"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 local copy = Table.copy or error()
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 local Time = require "luan:Time.luan"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 local time_now = Time.now or error()
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 local Parsers = require "luan:Parsers.luan"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 local json_string = Parsers.json_string or error()
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 local json_parse = Parsers.json_parse or error()
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 local Http = require "luan:http/Http.luan"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 local Shared = require "site:/lib/Shared.luan"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 local is_test = not Shared.is_production
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 local has_facebook = not not Shared.has_facebook
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 local Utils = require "site:/lib/Utils.luan"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 local to_list = Utils.to_list or error()
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 local User = require "site:/lib/User.luan"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 local current_user = User.current or error()
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 local Facebook = require "site:/lib/Facebook.luan"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 local call = Facebook.call or error()
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 local Logging = require "luan:logging/Logging.luan"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 local logger = Logging.logger "facebook.js"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 local function run()
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 if not has_facebook or is_test then return end
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 local user = current_user()
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 local request = Http.request
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 local user_data = {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 fbp = request.cookies._fbp
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 fbc = request.cookies._fbc
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 client_user_agent = request.headers["User-Agent"]
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 client_ip_address = request.headers["X-Real-IP"]
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 if user ~= nil then
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 user_data.em = user.email or error()
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 user_data.external_id = user.id or error()
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 end
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 local request_parameters = request.parameters
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 local event = {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 event_id = request_parameters.event_id or error()
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 event_time = time_now() // 1000
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 user_data = user_data
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 action_source = "website"
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 event_source_url = request.headers["Referer"]
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 local event_names = request_parameters.event_name or error()
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 event_names = to_list(event_names)
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 local events = {}
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 for _, event_name in ipairs(event_names) do
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 local ev = copy(event)
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 ev.event_name = event_name
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56 local props = request_parameters[event_name]
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 if props ~= nil then
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 ev.custom_data = json_parse(props)
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59 end
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 events[#events+1] = ev
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 end
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62 -- logger.info(stringify(events))
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63 try
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64 local result = call(events)
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
65 logger.info(result)
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66 catch e
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67 local response_content = e.response_content
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68 try
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
69 response_content = stringify(json_parse(response_content))
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
70 catch e2
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
71 end
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72 logger.error(e.."\nresponse_content = "..e.response_content.."\nevents = "..stringify(events).."\n"..trim(request.raw_head).."\n")
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
73 end
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
74 end
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
75
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
76 return function()
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
77 try
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
78 run()
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
79 catch e
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
80 logger.error(e.."\n"..trim(Http.request.raw_head).."\n")
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
81 end
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
82 end