92 lines
3.2 KiB
PL/PgSQL
92 lines
3.2 KiB
PL/PgSQL
CREATE OR REPLACE FUNCTION gochat_percent_decode(value TEXT)
|
|
RETURNS TEXT
|
|
LANGUAGE plpgsql
|
|
IMMUTABLE
|
|
STRICT
|
|
AS $$
|
|
DECLARE
|
|
position INTEGER := 1;
|
|
encoded_bytes TEXT := '';
|
|
token TEXT;
|
|
BEGIN
|
|
WHILE position <= char_length(value) LOOP
|
|
token := substr(value, position, 1);
|
|
IF token = '%' AND substr(value, position + 1, 2) ~ '^[0-9A-Fa-f]{2}$' THEN
|
|
encoded_bytes := encoded_bytes || substr(value, position + 1, 2);
|
|
position := position + 3;
|
|
ELSIF token = '+' THEN
|
|
encoded_bytes := encoded_bytes || '20';
|
|
position := position + 1;
|
|
ELSE
|
|
encoded_bytes := encoded_bytes || encode(convert_to(token, 'UTF8'), 'hex');
|
|
position := position + 1;
|
|
END IF;
|
|
END LOOP;
|
|
RETURN convert_from(decode(encoded_bytes, 'hex'), 'UTF8');
|
|
EXCEPTION WHEN OTHERS THEN
|
|
RETURN value;
|
|
END;
|
|
$$;
|
|
|
|
WITH profiles AS (
|
|
SELECT DISTINCT ON (contacts.id)
|
|
contacts.id,
|
|
split_part(
|
|
COALESCE(
|
|
NULLIF(contacts.custom_attributes->>'swt_xst_profile', ''),
|
|
contacts.additional_attributes->>'swt_xst_profile'
|
|
),
|
|
'|||||',
|
|
1
|
|
) AS profile
|
|
FROM contacts
|
|
JOIN contact_inboxes ON contact_inboxes.contact_id = contacts.id
|
|
JOIN inboxes ON inboxes.id = contact_inboxes.inbox_id
|
|
WHERE inboxes.channel_type = 'shangwutong'
|
|
AND COALESCE(
|
|
NULLIF(contacts.custom_attributes->>'swt_xst_profile', ''),
|
|
contacts.additional_attributes->>'swt_xst_profile'
|
|
) IS NOT NULL
|
|
), decoded AS (
|
|
SELECT
|
|
id,
|
|
NULLIF(gochat_percent_decode(split_part(profile, chr(26), 5)), '') AS location,
|
|
NULLIF(gochat_percent_decode(split_part(profile, chr(26), 11)), '') AS resolution,
|
|
NULLIF(gochat_percent_decode(split_part(profile, chr(26), 12)), '') AS user_agent,
|
|
NULLIF(gochat_percent_decode(split_part(profile, chr(26), 13)), '') AS ip_address
|
|
FROM profiles
|
|
)
|
|
UPDATE contacts
|
|
SET
|
|
custom_attributes = COALESCE(contacts.custom_attributes, '{}'::jsonb)
|
|
|| jsonb_strip_nulls(jsonb_build_object(
|
|
'swt_ip_location', decoded.location,
|
|
'city', decoded.location,
|
|
'swt_resolution', decoded.resolution,
|
|
'swt_user_agent', decoded.user_agent,
|
|
'swt_device', decoded.user_agent,
|
|
'swt_os', decoded.user_agent,
|
|
'swt_ip', decoded.ip_address
|
|
)),
|
|
additional_attributes = COALESCE(contacts.additional_attributes, '{}'::jsonb)
|
|
|| jsonb_strip_nulls(jsonb_build_object(
|
|
'swt_ip_location', decoded.location,
|
|
'city', decoded.location,
|
|
'swt_resolution', decoded.resolution,
|
|
'swt_user_agent', decoded.user_agent,
|
|
'swt_device', decoded.user_agent,
|
|
'swt_os', decoded.user_agent,
|
|
'swt_ip', decoded.ip_address
|
|
)),
|
|
updated_at = CURRENT_TIMESTAMP
|
|
FROM decoded
|
|
WHERE contacts.id = decoded.id
|
|
AND (
|
|
decoded.location IS NOT NULL
|
|
OR decoded.resolution IS NOT NULL
|
|
OR decoded.user_agent IS NOT NULL
|
|
OR decoded.ip_address IS NOT NULL
|
|
);
|
|
|
|
DROP FUNCTION gochat_percent_decode(TEXT);
|