diff options
| -rw-r--r-- | data.c | 182 |
1 files changed, 107 insertions, 75 deletions
| @@ -381,76 +381,13 @@ d_char *timeline_list(snac *snac) | |||
| 381 | } | 381 | } |
| 382 | 382 | ||
| 383 | 383 | ||
| 384 | void _timeline_parent(snac *snac, char *parent, char *child) | 384 | d_char *_timeline_new_fn(snac *snac, char *id) |
| 385 | /* add child to parent's children list */ | 385 | /* creates a new filename */ |
| 386 | { | 386 | { |
| 387 | /* FIXME: this must be recursive, but *ONLY* for file renaming */ | 387 | xs *ntid = tid(0); |
| 388 | 388 | xs *md5 = xs_md5_hex(id, strlen(id)); | |
| 389 | if (parent != NULL) { | ||
| 390 | xs *pfn = _timeline_find_fn(snac, parent); | ||
| 391 | |||
| 392 | if (pfn != NULL) { | ||
| 393 | FILE *f; | ||
| 394 | |||
| 395 | if ((f = fopen(pfn, "r")) != NULL) { | ||
| 396 | xs *ji, *msg; | ||
| 397 | |||
| 398 | ji = xs_readall(f); | ||
| 399 | fclose(f); | ||
| 400 | |||
| 401 | msg = xs_json_loads(ji); | ||
| 402 | |||
| 403 | if (msg != NULL) { | ||
| 404 | xs *meta; | ||
| 405 | xs *children; | ||
| 406 | |||
| 407 | /* get the children list */ | ||
| 408 | meta = xs_dup(xs_dict_get(msg, "_snac")); | ||
| 409 | children = xs_dup(xs_dict_get(meta, "children")); | ||
| 410 | |||
| 411 | /* add */ | ||
| 412 | children = xs_list_append(children, child); | ||
| 413 | |||
| 414 | /* re-store */ | ||
| 415 | meta = xs_dict_set(meta, "children", children); | ||
| 416 | msg = xs_dict_set(msg, "_snac", meta); | ||
| 417 | |||
| 418 | xs *jo = xs_json_dumps_pp(msg, 4); | ||
| 419 | xs *ntid = tid(0); | ||
| 420 | xs *md5 = xs_md5_hex(parent, strlen(parent)); | ||
| 421 | xs *nfn = xs_fmt("%s/timeline/%s-%s.json", snac->basedir, ntid, md5); | ||
| 422 | |||
| 423 | if ((f = fopen(nfn, "w")) != NULL) { | ||
| 424 | fwrite(jo, strlen(jo), 1, f); | ||
| 425 | fclose(f); | ||
| 426 | |||
| 427 | snac_debug(snac, 1, | ||
| 428 | xs_fmt("_timeline_parent updated %s %s", parent, nfn)); | ||
| 429 | |||
| 430 | unlink(pfn); | ||
| 431 | |||
| 432 | /* generated by this user? link to local timeline */ | ||
| 433 | if (xs_startswith(parent, snac->actor)) { | ||
| 434 | xs *lfn = xs_replace(nfn, "/timeline/", "/local/"); | ||
| 435 | xs *olfn = xs_replace(pfn, "/timeline/", "/local/"); | ||
| 436 | |||
| 437 | link(nfn, lfn); | ||
| 438 | unlink(olfn); | ||
| 439 | 389 | ||
| 440 | snac_debug(snac, 1, | 390 | return xs_fmt("%s/timeline/%s-%s.json", snac->basedir, ntid, md5); |
| 441 | xs_fmt("_timeline_parent (local) updated %s %s", parent, lfn)); | ||
| 442 | } | ||
| 443 | } | ||
| 444 | else | ||
| 445 | snac_log(snac, xs_fmt("_timeline_parent error writing %s %s", parent, nfn)); | ||
| 446 | } | ||
| 447 | else | ||
| 448 | snac_log(snac, xs_fmt("_timeline_parent error reading %s %s", parent, pfn)); | ||
| 449 | } | ||
| 450 | else | ||
| 451 | snac_log(snac, xs_fmt("_timeline_parent error opening %s %s", parent, pfn)); | ||
| 452 | } | ||
| 453 | } | ||
| 454 | } | 391 | } |
| 455 | 392 | ||
| 456 | 393 | ||
| @@ -466,9 +403,7 @@ void timeline_add(snac *snac, char *id, char *msg, char *parent) | |||
| 466 | } | 403 | } |
| 467 | 404 | ||
| 468 | /* build the new filename */ | 405 | /* build the new filename */ |
| 469 | xs *ntid = tid(0); | 406 | xs *fn = _timeline_new_fn(snac, id); |
| 470 | xs *md5 = xs_md5_hex(id, strlen(id)); | ||
| 471 | xs *fn = xs_fmt("%s/timeline/%s-%s.json", snac->basedir, ntid, md5); | ||
| 472 | xs *md; | 407 | xs *md; |
| 473 | 408 | ||
| 474 | /* add metadata */ | 409 | /* add metadata */ |
| @@ -493,16 +428,113 @@ void timeline_add(snac *snac, char *id, char *msg, char *parent) | |||
| 493 | snac_debug(snac, 1, xs_fmt("timeline_add %s %s", id, fn)); | 428 | snac_debug(snac, 1, xs_fmt("timeline_add %s %s", id, fn)); |
| 494 | } | 429 | } |
| 495 | 430 | ||
| 496 | /* generated by this user? link to local timeline */ | 431 | /* related to this user? link to local timeline */ |
| 497 | if (xs_startswith(id, snac->actor)) { | 432 | if (xs_startswith(id, snac->actor) || |
| 433 | (parent != NULL && xs_startswith(parent, snac->actor))) { | ||
| 498 | xs *lfn = xs_replace(fn, "/timeline/", "/local/"); | 434 | xs *lfn = xs_replace(fn, "/timeline/", "/local/"); |
| 499 | link(fn, lfn); | 435 | link(fn, lfn); |
| 500 | 436 | ||
| 501 | snac_debug(snac, 1, xs_fmt("timeline_add (local) %s %s", id, lfn)); | 437 | snac_debug(snac, 1, xs_fmt("timeline_add (local) %s %s", id, lfn)); |
| 502 | } | 438 | } |
| 503 | 439 | ||
| 504 | /* relink the parent */ | 440 | if (parent != NULL) { |
| 505 | _timeline_parent(snac, parent, id); | 441 | /* update the parent, adding this id to its children list */ |
| 442 | xs *pfn = _timeline_find_fn(snac, parent); | ||
| 443 | xs *p_msg = NULL; | ||
| 444 | |||
| 445 | if (pfn != NULL && (f = fopen(pfn, "r")) != NULL) { | ||
| 446 | xs *j; | ||
| 447 | |||
| 448 | j = xs_readall(f); | ||
| 449 | fclose(f); | ||
| 450 | |||
| 451 | p_msg = xs_json_loads(j); | ||
| 452 | } | ||
| 453 | |||
| 454 | if (p_msg == NULL) | ||
| 455 | return; | ||
| 456 | |||
| 457 | xs *meta = xs_dup(xs_dict_get(p_msg, "_snac")); | ||
| 458 | xs *children = xs_dup(xs_dict_get(meta, "children")); | ||
| 459 | |||
| 460 | /* add the child */ | ||
| 461 | children = xs_list_append(children, id); | ||
| 462 | |||
| 463 | /* re-store */ | ||
| 464 | meta = xs_dict_set(meta, "children", children); | ||
| 465 | p_msg = xs_dict_set(p_msg, "_snac", meta); | ||
| 466 | |||
| 467 | xs *nfn = _timeline_new_fn(snac, parent); | ||
| 468 | |||
| 469 | if ((f = fopen(nfn, "w")) != NULL) { | ||
| 470 | xs *j = xs_json_dumps_pp(p_msg, 4); | ||
| 471 | |||
| 472 | fwrite(j, strlen(j), 1, f); | ||
| 473 | fclose(f); | ||
| 474 | |||
| 475 | unlink(pfn); | ||
| 476 | |||
| 477 | snac_debug(snac, 2, xs_fmt("updated parent %s %s", parent, nfn)); | ||
| 478 | |||
| 479 | /* try to do the same with the local */ | ||
| 480 | xs *olfn = xs_replace(pfn, "/timeline/", "/local/"); | ||
| 481 | |||
| 482 | if (unlink(olfn) != -1) { | ||
| 483 | xs *nlfn = xs_replace(nfn, "/timeline/", "/local/"); | ||
| 484 | |||
| 485 | link(nfn, nlfn); | ||
| 486 | |||
| 487 | snac_debug(snac, 2, xs_fmt("updated parent (local) %s %s", parent, nlfn)); | ||
| 488 | } | ||
| 489 | } | ||
| 490 | else | ||
| 491 | return; | ||
| 492 | |||
| 493 | /* now iterate all parents up, just renaming the files */ | ||
| 494 | xs *grampa = xs_dup(xs_dict_get(meta, "parent")); | ||
| 495 | |||
| 496 | while (grampa != NULL) { | ||
| 497 | xs *gofn = _timeline_find_fn(snac, grampa); | ||
| 498 | |||
| 499 | if (gofn == NULL) | ||
| 500 | break; | ||
| 501 | |||
| 502 | /* create the new filename */ | ||
| 503 | xs *gnfn = _timeline_new_fn(snac, grampa); | ||
| 504 | |||
| 505 | rename(gofn, gnfn); | ||
| 506 | |||
| 507 | snac_debug(snac, 2, xs_fmt("updated grampa %s %s", grampa, gnfn)); | ||
| 508 | |||
| 509 | /* try to do the same with the local */ | ||
| 510 | xs *golfn = xs_replace(gofn, "/timeline/", "/local/"); | ||
| 511 | |||
| 512 | if (unlink(golfn) != -1) { | ||
| 513 | xs *gnlfn = xs_replace(gnfn, "/timeline/", "/local/"); | ||
| 514 | |||
| 515 | link(gnfn, gnlfn); | ||
| 516 | |||
| 517 | snac_debug(snac, 2, xs_fmt("updated grampa (local) %s %s", parent, gnlfn)); | ||
| 518 | } | ||
| 519 | |||
| 520 | /* now open it and get its own parent */ | ||
| 521 | if ((f = fopen(gnfn, "r")) != NULL) { | ||
| 522 | xs *j = xs_readall(f); | ||
| 523 | fclose(f); | ||
| 524 | |||
| 525 | xs *g_msg = xs_json_loads(j); | ||
| 526 | xs *meta = xs_dict_get(g_msg, "_snac"); | ||
| 527 | d_char *p = xs_dict_get(meta, "parent"); | ||
| 528 | |||
| 529 | free(grampa); | ||
| 530 | |||
| 531 | if (p != NULL) | ||
| 532 | p = xs_dup(p); | ||
| 533 | |||
| 534 | grampa = p; | ||
| 535 | } | ||
| 536 | } | ||
| 537 | } | ||
| 506 | } | 538 | } |
| 507 | 539 | ||
| 508 | 540 | ||