Package iqq.im.action

Source Code of iqq.im.action.GetSessionMsgSigAction

package iqq.im.action;

import iqq.im.QQActionListener;
import iqq.im.QQException;
import iqq.im.QQException.QQErrorCode;
import iqq.im.bean.QQDiscuzMember;
import iqq.im.bean.QQGroupMember;
import iqq.im.bean.QQStranger;
import iqq.im.core.QQConstants;
import iqq.im.core.QQContext;
import iqq.im.core.QQSession;
import iqq.im.event.QQActionEvent;
import iqq.im.http.QQHttpRequest;
import iqq.im.http.QQHttpResponse;

import org.slf4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.LoggerFactory;

/**
* 临时消息信道,用于发送群 U 2 U会话消息
*
* @author ZhiHui_Chen
* @since 2013-4-10
*/
public class GetSessionMsgSigAction extends AbstractHttpAction {
  private static final Logger LOG = LoggerFactory.getLogger(GetSessionMsgSigAction.class);
  private QQStranger user;

  /**
   * <p>Constructor for GetSessionMsgSigAction.</p>
   *
   * @param context a {@link iqq.im.core.QQContext} object.
   * @param listener a {@link iqq.im.QQActionListener} object.
   * @param user a {@link iqq.im.bean.QQStranger} object.
   */
  public GetSessionMsgSigAction(QQContext context, QQActionListener listener,
      QQStranger user) {
    super(context, listener);
    this.user = user;
  }

  /** {@inheritDoc} */
  @Override
  protected QQHttpRequest onBuildRequest() throws QQException, JSONException {
    QQSession session = getContext().getSession();
    QQHttpRequest req = createHttpRequest("GET",
        QQConstants.URL_GET_SESSION_MSG_SIG);
    if(user instanceof QQGroupMember) {
      QQGroupMember mb = (QQGroupMember) user;
      mb.setServiceType(0);
      req.addGetValue("id", mb.getGroup().getGin() + "");
      req.addGetValue("service_type", "0"); // 0为群,1为讨论组
    } else if(user instanceof QQDiscuzMember) {
      QQDiscuzMember mb = (QQDiscuzMember) user;
      mb.setServiceType(1);
      req.addGetValue("id", mb.getDiscuz().getDid() + "");
      req.addGetValue("service_type", "1"); // 0为群,1为讨论组
    } else {
      LOG.info("GetSessionMsgSigAction unknow type :" + user);
    }
    req.addGetValue("to_uin", user.getUin() + "");
    req.addGetValue("clientid", session.getClientId() + "");
    req.addGetValue("psessionid", session.getSessionId());
    req.addGetValue("t", System.currentTimeMillis() / 1000 + "");
    return req;
  }

  /** {@inheritDoc} */
  @Override
  protected void onHttpStatusOK(QQHttpResponse response) throws QQException,
      JSONException {
    // {"retcode":0,"result":{"type":0,"value":"sig","flags":{"text":1,"pic":1,"file":1,"audio":1,"video":1}}}
    JSONObject json = new JSONObject(response.getResponseString());
    int retcode = json.getInt("retcode");

    if (retcode == 0) {
      JSONObject result = json.getJSONObject("result");
      if (result.has("value")) {
        user.setGroupSig(result.getString("value"));
        notifyActionEvent(QQActionEvent.Type.EVT_OK, user);
        return;
      }
    }

    notifyActionEvent(QQActionEvent.Type.EVT_ERROR, new QQException(
        QQErrorCode.UNEXPECTED_RESPONSE, json.toString()));
  }
}
TOP

Related Classes of iqq.im.action.GetSessionMsgSigAction

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.