示例#1
0
void CVkProto::OnReceiveUnreadNews(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq)
{
    debugLogA("CVkProto::OnReceiveUnreadNews %d", reply->resultCode);
    db_unset(NULL, m_szModuleName, "LastNewsReqTime");
    if (reply->resultCode != 200)
        return;

    JSONNode jnRoot;
    const JSONNode &jnResponse = CheckJsonResponse(pReq, reply, jnRoot);
    if (!jnResponse)
        return;

    OBJLIST<CVkUserInfo> vkUsers(5, NumericKeySortT);
    CreateVkUserInfoList(vkUsers, jnResponse);

    const JSONNode &jnItems = jnResponse["items"];

    OBJLIST<CVKNewsItem> vkNews(5, sttCompareVKNewsItems);
    if (jnItems)
        for (auto it = jnItems.begin(); it != jnItems.end(); ++it) {
            CVKNewsItem *vkNewsItem = GetVkNewsItem((*it), vkUsers);
            if (!vkNewsItem)
                continue;
            CVKNewsItem *vkNewsFoundItem = vkNews.find(vkNewsItem);
            if (vkNewsFoundItem == NULL)
                vkNews.insert(vkNewsItem);
            else if (vkNewsFoundItem->tszType == _T("wall_photo") && vkNewsItem->tszType == _T("post")) {
                vkNews.remove(vkNewsFoundItem);
                vkNews.insert(vkNewsItem);
            }
            else
                delete vkNewsItem;
        }

    bool bNewsAdded = false;
    for (int i = 0; i < vkNews.getCount(); i++)
        if (!(m_bNewsSourceNoReposts && vkNews[i].bIsRepost)) {
            AddFeedEvent(vkNews[i]);
            bNewsAdded = true;
        }

    if (bNewsAdded)
        AddCListEvent(true);

    setDword("LastNewsTime", time(NULL));

    vkNews.destroy();
    vkUsers.destroy();
}
示例#2
0
CVKNewsItem* CVkProto::GetVkNewsItem(const JSONNode &jnItem, OBJLIST<CVkUserInfo> &vkUsers, bool isRepost)
{
	bool bPostLink = true;
	CVKNewsItem *vkNewsItem = new CVKNewsItem();
	if (!jnItem)
		return vkNewsItem;

	LONG iSourceId = jnItem["source_id"].isnull() ? jnItem["owner_id"].as_int() : jnItem["source_id"].as_int();
	LONG iPostId = jnItem["post_id"].as_int();
	CMString tszText(jnItem["text"].as_mstring());

	vkNewsItem->tszType = jnItem["type"].as_mstring();
	vkNewsItem->vkUser = GetVkUserInfo(iSourceId, vkUsers);
	vkNewsItem->bIsGroup = vkNewsItem->vkUser->m_bIsGroup;
	vkNewsItem->tDate = jnItem["date"].as_int();

	if (!tszText.IsEmpty())
		tszText += _T("\n");

	debugLog(_T("CVkProto::GetVkNewsItem %d %d %s"), iSourceId, iPostId, vkNewsItem->tszType);

	if (vkNewsItem->tszType == _T("photo_tag")) {
		bPostLink = false;
		const JSONNode &jnPhotos = jnItem["photo_tags"];
		if (!jnPhotos.isnull()) {			
			const JSONNode &jnPhotoItems = jnPhotos["items"];
			if (!jnPhotoItems.isnull()) {				
				tszText = TranslateT("User was tagged in these photos:");
				for (auto it = jnPhotoItems.begin(); it != jnPhotoItems.end(); ++it)
					tszText += _T("\n") + GetVkPhotoItem((*it), m_iBBCForNews);
			}
		}
	}
	else if (vkNewsItem->tszType == _T("photo") || vkNewsItem->tszType == _T("wall_photo")) {
		bPostLink = false;
		const JSONNode &jnPhotos = jnItem["photos"];
		int i = 0;
		if (!jnPhotos.isnull()) {
			const JSONNode &jnPhotoItems = jnPhotos["items"];
			if (!jnPhotoItems.isnull())
				for (auto it = jnPhotoItems.begin(); it != jnPhotoItems.end(); ++it) {
					const JSONNode &jnPhotoItem = (*it);
					tszText += GetVkPhotoItem(jnPhotoItem, m_iBBCForNews) + _T("\n");
					if (i == 0 && vkNewsItem->tszType == _T("wall_photo")) {						
						if (!jnPhotoItem["post_id"].isnull()) {
							bPostLink = true;
							iPostId = jnPhotoItem["post_id"].as_int();
							break; // max 1 wall_photo when photo post_id !=0
						}					
					}
					i++;
				}
		}
	} 
	else if (vkNewsItem->tszType == _T("post") || vkNewsItem->tszType.IsEmpty()) {
		bPostLink = true;
		const JSONNode &jnRepost = jnItem["copy_history"];
		if (!jnRepost.isnull()) {
			CVKNewsItem *vkRepost = GetVkNewsItem((*jnRepost.begin()), vkUsers, true);
			vkRepost->tszText.Replace(_T("\n"), _T("\n\t"));
			tszText += vkRepost->tszText;
			tszText += _T("\n");
			vkNewsItem->bIsRepost = true;
			delete vkRepost;
		}

		const JSONNode &jnAttachments = jnItem["attachments"];
		if (!jnAttachments.isnull()) {
			if (!tszText.IsEmpty())
				tszText.AppendChar(_T('\n'));
			tszText += GetAttachmentDescr(jnAttachments, m_bUseBBCOnAttacmentsAsNews ? m_iBBCForNews : m_iBBCForAttachments);
		}
	}

	CMString tszResFormat;	

	if (!isRepost)
		tszResFormat = Translate("News from %s\n%s");
	else {
		tszResFormat = Translate("\tRepost from %s\n%s");
		bPostLink = false;
	}
		
	vkNewsItem->tszText.AppendFormat(tszResFormat, 
		SetBBCString(vkNewsItem->vkUser->m_tszUserNick, m_iBBCForNews, vkbbcUrl, 
		vkNewsItem->vkUser->m_tszLink), tszText);
	
	vkNewsItem->tszId.AppendFormat(_T("%d_%d"), vkNewsItem->vkUser->m_UserId, iPostId);
	if (bPostLink) {
		vkNewsItem->tszLink = CMString(_T("https://vk.com/wall")) + vkNewsItem->tszId;
		vkNewsItem->tszText.AppendChar(_T('\n'));
		vkNewsItem->tszText += SetBBCString(TranslateT("Link"), m_iBBCForNews, vkbbcUrl, vkNewsItem->tszLink);
	}

	debugLog(_T("CVkProto::GetVkNewsItem %d %d <\n%s\n>"), iSourceId, iPostId, vkNewsItem->tszText);

	return vkNewsItem;
}