Commit e6f54204 authored by Lysander Trischler's avatar Lysander Trischler
Browse files

Don't duplicate subject over the wire

This commit is in spirit of the previous two and removes unnecessary
duplication of subjects for twts since they can easily be reconstructed
from the parsed elements.
parent 20ba8ae6
......@@ -216,7 +216,6 @@ class Twt:
class _Twt(ctypes.Structure):
_fields_ = [("created", ctypes.c_char_p),
("hash", ctypes.c_char_p),
("subject", ctypes.POINTER(_Subject)),
("elems_len", ctypes.c_int),
("elems", ctypes.POINTER(ctypes.POINTER(_Elem)))]
......@@ -226,8 +225,6 @@ class _Twt(ctypes.Structure):
twt.created = datetime.datetime.fromisoformat(self.created.decode("utf-8"))
if self.hash is not None:
twt.hash = self.hash.decode("utf-8")
if self.subject is not None:
twt.subject = self.subject.contents.to_python()
twt.mentions = []
twt.links = []
......@@ -243,8 +240,18 @@ class _Twt(ctypes.Structure):
twt.tags.append(elem)
else:
twt.links.append(elem)
elif isinstance(elem, Subject) and elem.tag:
twt.tags.append(elem.tag)
elif isinstance(elem, Subject):
if twt.subject is None:
twt.subject = elem
if elem.tag:
twt.tags.append(elem.tag)
# The lextwt implementation generates a default subject out of the twt
# hash if no subject is actually is present in the twt. Not sure if I
# really like that, I think I will remove it in the future, let's see.
if twt.subject is None:
twt.subject = Subject(text="#%s" % twt.hash,
tag=Link(type=Link.TAG, text=twt.hash, target=None))
return twt
......
......@@ -34,7 +34,6 @@ struct elem {
struct twt {
char *created;
char *hash;
struct subject *subject;
int elems_len;
struct elem *elems;
};
......@@ -155,11 +154,6 @@ func convertTwt(t *lextwt.Twt) *C.struct_twt {
twt := (*C.struct_twt)(ptr)
twt.created = C.CString(t.Created().Format(time.RFC3339))
twt.hash = C.CString(t.Hash())
if subject, ok := t.Subject().(*lextwt.Subject); ok && subject != nil {
twt.subject = convertSubject(subject)
} else {
twt.subject = nil
}
elemsPtr := C.malloc(C.size_t(len(t.Elems())) * C.size_t(unsafe.Sizeof(uintptr(0))))
elems := (*[arraySize]*C.struct_elem)(elemsPtr)
......@@ -176,7 +170,6 @@ func freeTwt(t *C.struct_twt) {
if t != nil {
free_string(t.created)
free_string(t.hash)
freeSubject(t.subject)
freeElems(t.elems, t.elems_len)
C.free(unsafe.Pointer(t))
}
......
......@@ -121,7 +121,8 @@ class ParseFileTest(unittest.TestCase):
def test_subjects(self):
twtfile = parse_file(
"2021-08-03T12:28:26+02:00\t(#<1234567 https://example.com/1234567>) I agree!\n"
"2021-08-03T12:33:17+02:00\t(re: foo) Well, it's quite complicated.\n",
"2021-08-03T12:33:17+02:00\t(re: foo) Well, it's quite complicated.\n"
"2021-10-27T13:41:46+02:00\t(#1234567) Oh really?\n",
TWTER)
self.assertEqual(TwtFile(twter=TWTER, twts=[
Twt(twter=TWTER,
......@@ -144,7 +145,16 @@ class ParseFileTest(unittest.TestCase):
links=[],
tags=[],
elems=[Subject(text="re: foo", tag=None),
" Well, it's quite complicated."])]), twtfile)
" Well, it's quite complicated."]),
Twt(twter=TWTER,
created=datetime.datetime(2021, 10, 27, 13, 41, 46, tzinfo=UTC_PLUS_2),
hash="2jcg3aq",
subject=Subject(text="#1234567", tag=Link(type=Link.TAG, text="1234567", target=None)),
mentions=[],
links=[],
tags=[Link(type=Link.TAG, text="1234567", target=None)],
elems=[Subject(text="#1234567", tag=Link(type=Link.TAG, text="1234567", target=None)),
" Oh really?"])]), twtfile)
def test_mentions(self):
twtfile = parse_file(
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment