利用IPFS构建短视频应用开发经历(三)

本系列文章是针对 https://blog.csdn.net/weixin_43668031/article/details/83962959 内容的实现所编写的。开发经历包括思考过程、重构和推翻重来。

继续智能合约的编写:

	function makeLable (uint _videoId, string memory _lable) public {
	    if( videos[_videoId].permission != 0) return;
	    Video storage _video = videos[_videoId];
	    for(uint i=0; i<_video.lableNum;i++){
	        if(keccak256(abi.encode(_video.lables[i].lable))==keccak256(abi.encode(_lable))){
	            _video.lables[i].times++;
	            return;
	        }
	    }
	    _video.lables[_video.lableNum++] = Lable(_lable, 1);
	}
	function getVideoLable (uint _videoId, uint _lableId) view public returns (string memory lable, uint times) {
      if( videos[_videoId].permission != 0 && videos[_videoId].author != msg.sender) return ("",0);
      return (videos[_videoId].lables[_lableId].lable,
      videos[_videoId].lables[_lableId].times);
	}
	function makeComment (uint _videoId,uint _videotimestamp, string memory _comment) public {
	    if( videos[_videoId].permission != 0) return;
	    if( videos[_videoId].duration <= _videotimestamp) return;
	    videos[_videoId].comments[videos[_videoId].commentsNum++] = Comment(_comment, now,_videotimestamp, msg.sender);
	}
	function changeCommen (uint _videoId,uint _commentId,uint _videotimestamp, string memory _comment) public {
	    if( videos[_videoId].permission != 0) return;
	    if( videos[_videoId].duration <= _videotimestamp || videos[_videoId].comments[_commentId].author != msg.sender) return;
	    videos[_videoId].comments[_commentId].videotimestamp = _videotimestamp;
	    videos[_videoId].comments[_commentId].content = _comment;
	    videos[_videoId].comments[_commentId].videotimestamp = now;
	}
	function getVideoComment (uint _videoId,uint _commentId) view public returns (string memory content, uint timestamp,uint videotimestamp, address author) {
      if( videos[_videoId].permission != 0 && videos[_videoId].author != msg.sender) return ("",0,0,msg.sender);
      return (videos[_videoId].comments[_commentId].content,
      videos[_videoId].comments[_commentId].timestamp,
      videos[_videoId].comments[_commentId].videotimestamp,
      videos[_videoId].comments[_commentId].author);
	}
    function reward (uint _videoId) public payable  {
        if( videos[_videoId].permission != 0) return;
        videos[_videoId].author.transfer(msg.value);
        videos[_videoId].gratuitys[videos[_videoId].gratuityNum++] = Gratuity(msg.value, msg.sender);
    }
    function getGratuitys (uint _videoId,uint _gratuitysId) view public returns (address author, uint gratuity) {
        if( videos[_videoId].permission != 0) return (msg.sender,0);
        return (videos[_videoId].gratuitys[_gratuitysId].author,videos[_videoId].gratuitys[_gratuitysId].gratuity);
    }

完成了打标签、评论/弹幕、打赏的插入、查看、修改评论。
标签这里是不允许重复的,有一个权值存在。
这里写到了标签和评论,这2个内容非常具有语言特征,不同语言的人对于这些信息的辨识度非常高,我这个应用中没有存在字典的功能,同一个含义的标签可能会被不同语言打多次,这让我重新思考内容国际化

到目前为止的智能合约已完成的样子:
https://github.com/bill080307/douyinWithEth/blob/26b7469f4d53d17067f36961a733f71f52a409a3/VideoShare.sol
也许在前文中并不是这样的,但是在后续的编写过程中会对不合理的代码修改。

原创文章 29 获赞 21 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43668031/article/details/85256010