jaklis/lib/profiles.py

130 lines
3.8 KiB
Python
Raw Normal View History

2023-09-09 09:44:27 +02:00
import sys, json, requests, base64
2020-12-07 16:02:31 +01:00
from time import time
from lib.cesiumCommon import CesiumCommon, PUBKEY_REGEX
class Profiles(CesiumCommon):
# Configure JSON document SET to send
def configDocSet(self, name, description, city, address, pos, socials, avatar):
timeSent = int(time())
data = {}
2023-09-09 09:38:22 +02:00
if name:
data["title"] = name
if description:
data["description"] = description
if address:
data["address"] = address
if city:
data["city"] = city
if pos:
2020-12-07 16:02:31 +01:00
geoPoint = {}
2023-09-09 09:38:22 +02:00
geoPoint["lat"] = pos[0]
geoPoint["lon"] = pos[1]
data["geoPoint"] = geoPoint
2020-12-07 16:02:31 +01:00
if socials:
2023-09-09 09:38:22 +02:00
data["socials"] = []
data["socials"].append({})
data["socials"][0]["type"] = "web"
data["socials"][0]["url"] = socials
2020-12-07 16:02:31 +01:00
if avatar:
2023-09-09 09:38:22 +02:00
avatar = open(avatar, "rb").read()
2020-12-07 16:02:31 +01:00
avatar = base64.b64encode(avatar).decode()
2023-09-09 09:38:22 +02:00
data["avatar"] = {}
data["avatar"]["_content"] = avatar
data["avatar"]["_content_type"] = "image/png"
data["time"] = timeSent
data["issuer"] = self.pubkey
data["version"] = 2
data["tags"] = []
2020-12-07 16:02:31 +01:00
2023-09-09 09:38:22 +02:00
document = json.dumps(data)
2020-12-07 16:02:31 +01:00
return self.signDoc(document)
2023-09-09 09:38:22 +02:00
# Configure JSON document GET to send
def configDocGet(self, profile, scope="title", getAvatar=None):
2020-12-07 16:02:31 +01:00
if getAvatar:
avatar = "avatar"
else:
avatar = "avatar._content_type"
data = {
2023-09-09 09:38:22 +02:00
"query": {
2020-12-07 16:02:31 +01:00
"bool": {
2023-09-09 09:38:22 +02:00
"should": [
{"match": {scope: {"query": profile, "boost": 2}}},
{"prefix": {scope: profile}},
2020-12-07 16:02:31 +01:00
]
}
2023-09-09 09:38:22 +02:00
},
"highlight": {"fields": {"title": {}, "tags": {}}},
"from": 0,
"size": 100,
"_source": [
"title",
avatar,
"description",
"city",
"address",
"socials.url",
"creationTime",
"membersCount",
"type",
"geoPoint",
],
"indices_boost": {"user": 100, "page": 1, "group": 0.01},
2020-12-07 16:02:31 +01:00
}
2023-09-09 09:38:22 +02:00
document = json.dumps(data)
2020-12-07 16:02:31 +01:00
return document
# Configure JSON document SET to send
def configDocErase(self):
timeSent = int(time())
data = {}
2023-09-09 09:38:22 +02:00
data["time"] = timeSent
data["id"] = self.pubkey
data["issuer"] = self.pubkey
data["version"] = 2
data["index"] = "user"
data["type"] = "profile"
2020-12-07 16:02:31 +01:00
2023-09-09 09:38:22 +02:00
document = json.dumps(data)
2020-12-07 16:02:31 +01:00
return self.signDoc(document)
def sendDocument(self, document, type):
headers = {
2023-09-09 09:38:22 +02:00
"Content-type": "application/json",
2020-12-07 16:02:31 +01:00
}
# Send JSON document and get JSON result
2023-09-09 09:38:22 +02:00
if type == "set":
reqQuery = "{0}/user/profile?pubkey={1}/_update?pubkey={1}".format(
self.pod, self.pubkey
)
elif type == "get":
reqQuery = "{0}/user,page,group/profile,record/_search".format(self.pod)
elif type == "erase":
reqQuery = "{0}/history/delete".format(self.pod)
2020-12-07 16:02:31 +01:00
result = requests.post(reqQuery, headers=headers, data=document)
if result.status_code == 200:
# print(result.text)
return result.text
else:
2023-09-09 09:38:22 +02:00
sys.stderr.write("Echec de l'envoi du document...\n" + result.text + "\n")
2020-12-07 16:02:31 +01:00
def parseJSON(self, doc):
2023-09-09 09:38:22 +02:00
doc = json.loads(doc)["hits"]["hits"]
2020-12-07 16:02:31 +01:00
if doc:
2023-09-09 09:38:22 +02:00
pubkey = {"pubkey": doc[0]["_id"]}
rest = doc[0]["_source"]
2020-12-07 16:02:31 +01:00
final = {**pubkey, **rest}
return json.dumps(final, indent=2)
else:
2023-09-09 09:38:22 +02:00
return "Profile vide"