BxTimelineResponse.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup Timeline Timeline
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxTimelineResponse extends BxBaseModNotificationsResponse
  12. {
  13. public function __construct()
  14. {
  15. parent::__construct();
  16. $this->_oModule = BxDolModule::getInstance('bx_timeline');
  17. }
  18. /**
  19. * Overwtire the method of parent class.
  20. *
  21. * @param BxDolAlerts $oAlert an instance of alert.
  22. */
  23. public function response($oAlert)
  24. {
  25. $CNF = &$this->_oModule->_oConfig->CNF;
  26. $sMethod = '_process' . bx_gen_method_name($oAlert->sUnit . '_' . $oAlert->sAction);
  27. if(method_exists($this, $sMethod))
  28. return $this->$sMethod($oAlert);
  29. $iObjectAuthorId = $this->_getObjectOwnerId($oAlert->aExtras);
  30. $iObjectPrivacyView = $this->_getObjectPrivacyView($oAlert->aExtras);
  31. if($iObjectPrivacyView == BX_DOL_PG_HIDDEN)
  32. return;
  33. $aHandler = $this->_oModule->_oConfig->getHandlers($oAlert->sUnit . '_' . $oAlert->sAction);
  34. if(empty($aHandler) || !is_array($aHandler))
  35. return;
  36. $iSilentMode = $this->_oModule->getSilentMode($oAlert->aExtras);
  37. if(in_array($iSilentMode, array(BX_BASE_MOD_NTFS_SLTMODE_ABSOLUTE, BX_TIMELINE_SLTMODE_ABSOLUTE)))
  38. return;
  39. switch($aHandler['type']) {
  40. case BX_BASE_MOD_NTFS_HANDLER_TYPE_INSERT:
  41. $iOwnerId = abs($oAlert->iSender);
  42. if($iObjectPrivacyView < 0)
  43. $iOwnerId = abs($iObjectPrivacyView);
  44. $sContent = '';
  45. if(!empty($oAlert->aExtras) && is_array($oAlert->aExtras))
  46. $sContent = serialize(bx_process_input($oAlert->aExtras));
  47. $aEvent = array(
  48. 'owner_id' => $iOwnerId,
  49. 'type' => $oAlert->sUnit,
  50. 'action' => $oAlert->sAction,
  51. 'object_id' => $oAlert->iObject,
  52. 'object_owner_id' => $iObjectAuthorId,
  53. 'object_privacy_view' => $iObjectPrivacyView,
  54. 'content' => $sContent,
  55. 'title' => '',
  56. 'description' => ''
  57. );
  58. $sMethod = '_prepareEvent' . bx_gen_method_name($oAlert->sUnit . '_' . $oAlert->sAction);
  59. if(method_exists($this, $sMethod))
  60. $this->$sMethod($oAlert, $aEvent);
  61. $iId = $this->_oModule->_oDb->insertEvent($aEvent);
  62. if(!empty($iId))
  63. $this->_oModule->onPost($iId);
  64. break;
  65. case BX_BASE_MOD_NTFS_HANDLER_TYPE_UPDATE:
  66. $aHandlers = $this->_oModule->_oDb->getHandlers(array('type' => 'by_group_key_type', 'group' => $aHandler['group']));
  67. $aEvent = $this->_oModule->_oDb->getEvents(array(
  68. 'browse' => 'descriptor',
  69. 'type' => $oAlert->sUnit,
  70. 'action' => $aHandlers[BX_BASE_MOD_NTFS_HANDLER_TYPE_INSERT]['alert_action'],
  71. 'object_id' => $oAlert->iObject
  72. ));
  73. if(empty($aEvent) || !is_array($aEvent))
  74. break;
  75. $aParamsSet = array(
  76. 'content' => !empty($oAlert->aExtras) && is_array($oAlert->aExtras) ? serialize(bx_process_input($oAlert->aExtras)) : ''
  77. );
  78. if($iObjectPrivacyView > 0 && !empty($iObjectAuthorId) && $iObjectAuthorId == $oAlert->iSender)
  79. $aParamsSet = array_merge($aParamsSet, array(
  80. 'owner_id' => $oAlert->iSender,
  81. 'object_privacy_view' => $iObjectPrivacyView
  82. ));
  83. else if($iObjectPrivacyView < 0)
  84. $aParamsSet = array_merge($aParamsSet, array(
  85. 'owner_id' => abs($iObjectPrivacyView),
  86. 'object_privacy_view' => $iObjectPrivacyView
  87. ));
  88. $this->_oModule->_oDb->updateEvent($aParamsSet, array('id' => $aEvent[$CNF['FIELD_ID']]));
  89. //--- Delete feed cached
  90. $this->_oModule->_oDb->deleteCache(array('context_id' => 0)); //--- Delete cache for Public feed
  91. $this->_oModule->_oDb->deleteCache(array('context_id' => $aEvent[$CNF['FIELD_OWNER_ID']])); //--- Delete cache for old context
  92. if(isset($aParamsSet['owner_id']))
  93. $this->_oModule->_oDb->deleteCache(array('context_id' => $aParamsSet['owner_id'])); //--- Delete cache for new context
  94. //--- Delete item cache.
  95. $oCacheItem = $this->_oModule->getCacheItemObject();
  96. $oCacheItem->delData($this->_oModule->_oConfig->getCacheItemKey($aEvent['id']));
  97. $aReposts = $this->_oModule->_oDb->getReposts($aEvent[$CNF['FIELD_ID']]);
  98. foreach($aReposts as $aRepost) {
  99. //--- Delete cache for reposter context
  100. $this->_oModule->_oDb->deleteCache(array('context_id' => $aRepost['author_id']));
  101. //--- Delete item cache for repost.
  102. $oCacheItem->delData($this->_oModule->_oConfig->getCacheItemKey($aRepost['event_id']));
  103. }
  104. break;
  105. case BX_BASE_MOD_NTFS_HANDLER_TYPE_DELETE:
  106. if($oAlert->sUnit == 'profile' && $oAlert->sAction == 'delete') {
  107. $aEvents = $this->_oModule->_oDb->getEvents(array('browse' => 'owner_id', 'value' => $oAlert->iObject));
  108. foreach($aEvents as $aEvent)
  109. $this->_oModule->deleteEvent($aEvent);
  110. if(isset($oAlert->aExtras['delete_with_content']) && $oAlert->aExtras['delete_with_content']) {
  111. $aEvents = $this->_oModule->_oDb->getEvents(array('browse' => 'common_by_object', 'value' => $oAlert->iObject));
  112. foreach($aEvents as $aEvent)
  113. $this->_oModule->deleteEvent($aEvent);
  114. }
  115. //--- Delete cached
  116. $this->_oModule->_oDb->deleteCache(array('context_id' => $oAlert->iObject));
  117. $this->_oModule->_oDb->deleteCache(array('profile_id' => $oAlert->iObject));
  118. break;
  119. }
  120. $aHandlers = $this->_oModule->_oDb->getHandlers(array('type' => 'by_group_key_type', 'group' => $aHandler['group']));
  121. $aEvent = $this->_oModule->_oDb->getEvents(array(
  122. 'browse' => 'descriptor',
  123. 'type' => $oAlert->sUnit,
  124. 'action' => $aHandlers[BX_BASE_MOD_NTFS_HANDLER_TYPE_INSERT]['alert_action'],
  125. 'object_id' => $oAlert->iObject
  126. ));
  127. if(empty($aEvent) || !is_array($aEvent))
  128. break;
  129. $this->_oModule->deleteEvent($aEvent);
  130. break;
  131. }
  132. }
  133. protected function _prepareEventCommentAdded($oAlert, &$aEvent)
  134. {
  135. $aEvent = array_merge($aEvent, array(
  136. 'object_id' => $oAlert->aExtras['comment_uniq_id'],
  137. 'object_owner_id' => $oAlert->aExtras['comment_author_id'],
  138. ));
  139. }
  140. protected function _processSystemClearCache($oAlert)
  141. {
  142. if(!in_array($oAlert->aExtras['type'], array('all', 'custom')))
  143. return;
  144. $this->_clearCache();
  145. }
  146. protected function _processAccountConfirm($oAlert)
  147. {
  148. $this->_clearCache();
  149. }
  150. protected function _processAccountUnconfirm($oAlert)
  151. {
  152. $this->_clearCache();
  153. }
  154. protected function _processProfileApprove($oAlert)
  155. {
  156. $this->_clearCache();
  157. }
  158. protected function _processProfileDisapprove($oAlert)
  159. {
  160. $this->_clearCache();
  161. }
  162. protected function _processProfileActivate($oAlert)
  163. {
  164. $this->_clearCache();
  165. }
  166. protected function _processProfileSuspend($oAlert)
  167. {
  168. $this->_clearCache();
  169. }
  170. protected function _clearCache()
  171. {
  172. //--- Clear item cache.
  173. $this->_oModule->getCacheItemObject()->removeAllByPrefix($this->_oModule->_oConfig->getPrefix('cache_item'));
  174. //--- Clear feed cache.
  175. $this->_oModule->_oDb->clearCache();
  176. }
  177. protected function _processBxTimelineVideosMp4Transcoded($oAlert)
  178. {
  179. $this->_onVideoTranscoded($oAlert->iObject, $oAlert->aExtras['ret']);
  180. }
  181. protected function _onVideoTranscoded($iMediaId, $bResult, $aParams = array())
  182. {
  183. $CNF = &$this->_oModule->_oConfig->CNF;
  184. if(!isset($CNF['FIELD_STATUS']))
  185. return;
  186. $aMedia = $this->_oModule->_oDb->getMediaById($CNF['FIELD_VIDEO'], $iMediaId);
  187. if(empty($aMedia) || !is_array($aMedia))
  188. return;
  189. $iContentId = (int)$aMedia['event_id'];
  190. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  191. if(empty($aContentInfo) || !is_array($aContentInfo))
  192. return;
  193. if(!isset($aContentInfo[$CNF['FIELD_STATUS']]) || $aContentInfo[$CNF['FIELD_STATUS']] != 'awaiting')
  194. return;
  195. $iNow = time();
  196. $bNotify = $iNow - $aContentInfo[$CNF['FIELD_ADDED']] > $this->_oModule->_oConfig->getDpnTime();
  197. $iSystemBotProfileId = (int)getParam('sys_profile_bot');
  198. $iAuthorProfileId = $aContentInfo[$CNF['FIELD_' . ((int)$aContentInfo[$CNF['FIELD_SYSTEM']] == 0 ? 'OBJECT_ID' : 'OWNER_ID')]];
  199. if(!$bResult) {
  200. if((int)$this->_oModule->_oDb->updateEntriesBy(array($CNF['FIELD_STATUS'] => 'failed'), array($CNF['FIELD_ID'] => $iContentId)) > 0) {
  201. $this->_oModule->onFailed($iContentId);
  202. if($bNotify)
  203. bx_alert($this->_oModule->getName(), 'publish_failed', $aContentInfo[$CNF['FIELD_ID']], $iSystemBotProfileId, array(
  204. 'object_author_id' => $iAuthorProfileId,
  205. 'privacy_view' => BX_DOL_PG_ALL
  206. ));
  207. }
  208. return;
  209. }
  210. if(isset($CNF['FIELD_PUBLISHED']) && isset($aContentInfo[$CNF['FIELD_PUBLISHED']]) && $aContentInfo[$CNF['FIELD_PUBLISHED']] > $iNow)
  211. return;
  212. $oTranscoder = BxDolTranscoder::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['mp4']);
  213. $aMediasToCheck = $this->_oModule->_oDb->getMedia($CNF['FIELD_VIDEO'], $iContentId);
  214. foreach($aMediasToCheck as $iMediaToCheck)
  215. if(!$oTranscoder->isFileReady($iMediaToCheck))
  216. return;
  217. if(!$this->_oModule->_oDb->updateEntriesBy(array($CNF['FIELD_STATUS'] => 'active'), array($CNF['FIELD_ID'] => $iContentId)))
  218. return;
  219. $this->_oModule->onPublished($iContentId);
  220. if($bNotify)
  221. bx_alert($this->_oModule->getName(), 'publish_succeeded', $aContentInfo[$CNF['FIELD_ID']], $iSystemBotProfileId, array(
  222. 'object_author_id' => $iAuthorProfileId,
  223. 'privacy_view' => BX_DOL_PG_ALL
  224. ));
  225. }
  226. }
  227. /** @} */