/[LeafOK_CVS]/lbbs/src/article_favor.c
ViewVC logotype

Diff of /lbbs/src/article_favor.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.8 by sysadm, Tue Nov 4 14:58:56 2025 UTC Revision 1.13 by sysadm, Sat Jan 3 10:27:14 2026 UTC
# Line 3  Line 3 
3   * article_favor   * article_favor
4   *   - data model and basic operations of user favorite articles   *   - data model and basic operations of user favorite articles
5   *   *
6   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>   * Copyright (C) 2004-2026  Leaflet <leaflet@leafok.com>
7   */   */
8    
9    #ifdef HAVE_CONFIG_H
10    #include "config.h"
11    #endif
12    
13  #include "article_favor.h"  #include "article_favor.h"
14  #include "common.h"  #include "common.h"
15  #include "database.h"  #include "database.h"
# Line 25  int article_favor_load(int uid, ARTICLE_ Line 29  int article_favor_load(int uid, ARTICLE_
29    
30          if (p_favor == NULL)          if (p_favor == NULL)
31          {          {
32                  log_error("NULL pointer error\n");                  log_error("NULL pointer error");
33                  return -1;                  return -1;
34          }          }
35    
# Line 45  int article_favor_load(int uid, ARTICLE_ Line 49  int article_favor_load(int uid, ARTICLE_
49    
50          if ((db = db_open()) == NULL)          if ((db = db_open()) == NULL)
51          {          {
52                  log_error("article_favor_load() error: Unable to open DB\n");                  log_error("article_favor_load() error: Unable to open DB");
53                  return -2;                  return -2;
54          }          }
55    
# Line 55  int article_favor_load(int uid, ARTICLE_ Line 59  int article_favor_load(int uid, ARTICLE_
59                           uid);                           uid);
60          if (mysql_query(db, sql) != 0)          if (mysql_query(db, sql) != 0)
61          {          {
62                  log_error("Query article_favorite error: %s\n", mysql_error(db));                  log_error("Query article_favorite error: %s", mysql_error(db));
63                  return -3;                  return -3;
64          }          }
65          if ((rs = mysql_use_result(db)) == NULL)          if ((rs = mysql_use_result(db)) == NULL)
66          {          {
67                  log_error("Get article_favorite data failed\n");                  log_error("Get article_favorite data failed");
68                  return -3;                  return -3;
69          }          }
70    
# Line 72  int article_favor_load(int uid, ARTICLE_ Line 76  int article_favor_load(int uid, ARTICLE_
76                  (p_favor->aid_base_cnt)++;                  (p_favor->aid_base_cnt)++;
77                  if (p_favor->aid_base_cnt >= MAX_FAVOR_AID_BASE_CNT)                  if (p_favor->aid_base_cnt >= MAX_FAVOR_AID_BASE_CNT)
78                  {                  {
79                          log_error("Too many article_favorite records for uid=%d\n",                          log_error("Too many article_favorite records for uid=%d",
80                                            uid);                                            uid);
81                          break;                          break;
82                  }                  }
# Line 81  int article_favor_load(int uid, ARTICLE_ Line 85  int article_favor_load(int uid, ARTICLE_
85    
86          mysql_close(db);          mysql_close(db);
87    
88          log_common("Loaded %d article_favorite records for uid=%d\n", p_favor->aid_base_cnt, uid);          log_common("Loaded %d article_favorite records for uid=%d", p_favor->aid_base_cnt, uid);
89    
90          if (!keep_inc)          if (!keep_inc)
91          {          {
# Line 95  int article_favor_unload(ARTICLE_FAVOR * Line 99  int article_favor_unload(ARTICLE_FAVOR *
99  {  {
100          if (p_favor == NULL)          if (p_favor == NULL)
101          {          {
102                  log_error("NULL pointer error\n");                  log_error("NULL pointer error");
103                  return -1;                  return -1;
104          }          }
105    
# Line 119  int article_favor_save_inc(const ARTICLE Line 123  int article_favor_save_inc(const ARTICLE
123    
124          if (p_favor == NULL)          if (p_favor == NULL)
125          {          {
126                  log_error("NULL pointer error\n");                  log_error("NULL pointer error");
127                  return -1;                  return -1;
128          }          }
129    
# Line 130  int article_favor_save_inc(const ARTICLE Line 134  int article_favor_save_inc(const ARTICLE
134    
135          if ((db = db_open()) == NULL)          if ((db = db_open()) == NULL)
136          {          {
137                  log_error("article_favor_load() error: Unable to open DB\n");                  log_error("article_favor_load() error: Unable to open DB");
138                  return -2;                  return -2;
139          }          }
140    
# Line 173  int article_favor_save_inc(const ARTICLE Line 177  int article_favor_save_inc(const ARTICLE
177    
178                          if (mysql_query(db, sql_add) != 0)                          if (mysql_query(db, sql_add) != 0)
179                          {                          {
180                                  log_error("Add article_favorite error: %s\n", mysql_error(db));                                  log_error("Add article_favorite error: %s", mysql_error(db));
181                                  log_error("%s\n", sql_add);                                  log_error("%s", sql_add);
182                                  mysql_close(db);                                  mysql_close(db);
183                                  return -3;                                  return -3;
184                          }                          }
# Line 193  int article_favor_save_inc(const ARTICLE Line 197  int article_favor_save_inc(const ARTICLE
197    
198                          if (mysql_query(db, sql_del) != 0)                          if (mysql_query(db, sql_del) != 0)
199                          {                          {
200                                  log_error("Delete article_favorite error: %s\n", mysql_error(db));                                  log_error("Delete article_favorite error: %s", mysql_error(db));
201                                  log_error("%s\n", sql_del);                                  log_error("%s", sql_del);
202                                  mysql_close(db);                                  mysql_close(db);
203                                  return -3;                                  return -3;
204                          }                          }
# Line 208  int article_favor_save_inc(const ARTICLE Line 212  int article_favor_save_inc(const ARTICLE
212                  }                  }
213          }          }
214    
215          log_common("Saved %d and deleted %d article_favorite records for uid=%d\n",          log_common("Saved %d and deleted %d article_favorite records for uid=%d",
216                             cnt_total_add, cnt_total_del, p_favor->uid);                             cnt_total_add, cnt_total_del, p_favor->uid);
217    
218          mysql_close(db);          mysql_close(db);
# Line 224  int article_favor_merge_inc(ARTICLE_FAVO Line 228  int article_favor_merge_inc(ARTICLE_FAVO
228    
229          if (p_favor == NULL)          if (p_favor == NULL)
230          {          {
231                  log_error("NULL pointer error\n");                  log_error("NULL pointer error");
232                  return -1;                  return -1;
233          }          }
234    
# Line 260  int article_favor_merge_inc(ARTICLE_FAVO Line 264  int article_favor_merge_inc(ARTICLE_FAVO
264          }          }
265          if (i < p_favor->aid_base_cnt)          if (i < p_favor->aid_base_cnt)
266          {          {
267                  log_error("Too many base aids, %d will be discarded\n", p_favor->aid_base_cnt - i);                  log_error("Too many base aids, %d will be discarded", p_favor->aid_base_cnt - i);
268          }          }
269    
270          len = MIN(p_favor->aid_inc_cnt - j, MAX_FAVOR_AID_BASE_CNT - k);          len = MIN(p_favor->aid_inc_cnt - j, MAX_FAVOR_AID_BASE_CNT - k);
# Line 273  int article_favor_merge_inc(ARTICLE_FAVO Line 277  int article_favor_merge_inc(ARTICLE_FAVO
277          }          }
278          if (j < p_favor->aid_inc_cnt)          if (j < p_favor->aid_inc_cnt)
279          {          {
280                  log_error("Too many inc aids, %d will be discarded\n", p_favor->aid_inc_cnt - j);                  log_error("Too many inc aids, %d will be discarded", p_favor->aid_inc_cnt - j);
281          }          }
282    
283          memcpy(p_favor->aid_base, aid_new, sizeof(int32_t) * (size_t)k);          memcpy(p_favor->aid_base, aid_new, sizeof(int32_t) * (size_t)k);
# Line 294  int article_favor_check(int32_t aid, con Line 298  int article_favor_check(int32_t aid, con
298    
299          if (p_favor == NULL)          if (p_favor == NULL)
300          {          {
301                  log_error("NULL pointer error\n");                  log_error("NULL pointer error");
302                  return -1;                  return -1;
303          }          }
304    
# Line 345  int article_favor_set(int32_t aid, ARTIC Line 349  int article_favor_set(int32_t aid, ARTIC
349    
350          if (p_favor == NULL)          if (p_favor == NULL)
351          {          {
352                  log_error("NULL pointer error\n");                  log_error("NULL pointer error");
353                  return -1;                  return -1;
354          }          }
355    
# Line 408  int article_favor_set(int32_t aid, ARTIC Line 412  int article_favor_set(int32_t aid, ARTIC
412                  // Save incremental article favorite                  // Save incremental article favorite
413                  if (article_favor_save_inc(p_favor) < 0)                  if (article_favor_save_inc(p_favor) < 0)
414                  {                  {
415                          log_error("article_favor_save_inc() error\n");                          log_error("article_favor_save_inc() error");
416                          return -2;                          return -2;
417                  }                  }
418    
# Line 441  int article_favor_set(int32_t aid, ARTIC Line 445  int article_favor_set(int32_t aid, ARTIC
445          return 1; // Set complete          return 1; // Set complete
446  }  }
447    
448  int query_favor_articles(ARTICLE_FAVOR *p_favor, int page_id, ARTICLE **p_articles,  int query_favor_articles(ARTICLE_FAVOR *p_favor, int page_id, const ARTICLE **p_articles,
449                                                   char p_snames[][BBS_section_name_max_len + 1], int *p_article_count, int *p_page_count)                                                   char p_snames[][BBS_section_name_max_len + 1], int *p_article_count, int *p_page_count)
450  {  {
451          SECTION_LIST *p_section;          SECTION_LIST *p_section;
# Line 450  int query_favor_articles(ARTICLE_FAVOR * Line 454  int query_favor_articles(ARTICLE_FAVOR *
454    
455          if (p_favor == NULL || p_articles == NULL || p_article_count == NULL || p_page_count == NULL)          if (p_favor == NULL || p_articles == NULL || p_article_count == NULL || p_page_count == NULL)
456          {          {
457                  log_error("NULL pointer error\n");                  log_error("NULL pointer error");
458                  return -1;                  return -1;
459          }          }
460    
461          if (article_favor_save_inc(p_favor) < 0)          if (article_favor_save_inc(p_favor) < 0)
462          {          {
463                  log_error("article_favor_save_inc() error\n");                  log_error("article_favor_save_inc() error");
464                  return -2;                  return -2;
465          }          }
466          if (article_favor_merge_inc(p_favor) < 0)          if (article_favor_merge_inc(p_favor) < 0)
467          {          {
468                  log_error("article_favor_merge_inc() error\n");                  log_error("article_favor_merge_inc() error");
469                  return -2;                  return -2;
470          }          }
471    
472          *p_page_count = p_favor->aid_base_cnt / BBS_article_limit_per_page + (p_favor->aid_base_cnt % BBS_article_limit_per_page == 0 ? 0 : 1);          *p_page_count = (p_favor->aid_base_cnt + BBS_article_limit_per_page - 1) / BBS_article_limit_per_page;
473          *p_article_count = 0;          *p_article_count = 0;
474    
475          if (p_favor->aid_base_cnt == 0)          if (p_favor->aid_base_cnt == 0)
# Line 476  int query_favor_articles(ARTICLE_FAVOR * Line 480  int query_favor_articles(ARTICLE_FAVOR *
480    
481          if (page_id < 0 || page_id >= *p_page_count)          if (page_id < 0 || page_id >= *p_page_count)
482          {          {
483                  log_error("Invalid page_id = %d, not in range [0, %d)\n", page_id, *p_page_count);                  log_error("Invalid page_id = %d, not in range [0, %d)", page_id, *p_page_count);
484                  return -1;                  return -1;
485          }          }
486    
# Line 489  int query_favor_articles(ARTICLE_FAVOR * Line 493  int query_favor_articles(ARTICLE_FAVOR *
493                  p_articles[i] = article_block_find_by_aid(aid);                  p_articles[i] = article_block_find_by_aid(aid);
494                  if (p_articles[i] == NULL)                  if (p_articles[i] == NULL)
495                  {                  {
496                          log_error("article_block_find_by_aid(aid=%d) error: page_id=%d, i=%d\n", aid, page_id, i);                          log_error("article_block_find_by_aid(aid=%d) error: page_id=%d, i=%d", aid, page_id, i);
497                          return -3;                          return -3;
498                  }                  }
499    
500                  p_section = section_list_find_by_sid(p_articles[i]->sid);                  p_section = section_list_find_by_sid(p_articles[i]->sid);
501                  if (p_section == NULL)                  if (p_section == NULL)
502                  {                  {
503                          log_error("section_list_find_by_sid(%d) error\n", p_articles[i]->sid);                          log_error("section_list_find_by_sid(%d) error", p_articles[i]->sid);
504                          return -3;                          return -3;
505                  }                  }
506    
507                  if (get_section_info(p_section, p_snames[i], NULL, NULL) < 0)                  if (get_section_info(p_section, p_snames[i], NULL, NULL) < 0)
508                  {                  {
509                          log_error("get_section_info(sid=%d) error\n", p_section->sid);                          log_error("get_section_info(sid=%d) error", p_section->sid);
510                          return -4;                          return -4;
511                  }                  }
512          }          }


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1